-
Notifications
You must be signed in to change notification settings - Fork 65
CLI AI: Add --json flag for headless NDJSON output
#2913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
wesleyfantinel
wants to merge
12
commits into
Automattic:trunk
from
wesleyfantinel:add/cli-studio-ai-json-flag
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6979e40
Add JSON output support for AI commands
wesleyfantinel 21c2375
Update `canUseTool` to use empty input updates during `autoApprove` h…
wesleyfantinel 5804af6
Merge branch 'trunk' into add/cli-studio-ai-json-flag
wesleyfantinel 7da4fcd
Merge branch 'trunk' into add/cli-studio-ai-json-flag
wesleyfantinel c74ffe6
Merge branch 'trunk' into add/cli-studio-ai-json-flag
wesleyfantinel b82d08f
Fix TypeScript errors in AI JSON mode output adapter
wesleyfantinel 38decee
Use DEFAULT_MODEL constant in JsonAdapter
wesleyfantinel f70f01c
Remove InteractiveAdapter, make AiChatUI implement AiOutputAdapter di…
wesleyfantinel ec678b3
Decouple autoApprove from --json as a separate CLI flag
wesleyfantinel eb1284d
Add cleanup before process.exit in JsonAdapter.askUser()
wesleyfantinel e308fd0
Fix interactive mode with initial message exiting after first reply
wesleyfantinel 6a45b6b
Merge branch 'trunk' into add/cli-studio-ai-json-flag
wesleyfantinel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk'; | ||
| import type { AskUserQuestion } from 'cli/ai/agent'; | ||
|
|
||
| export type JsonEventType = | ||
| | 'message' | ||
| | 'progress' | ||
| | 'info' | ||
| | 'error' | ||
| | 'question.asked' | ||
| | 'turn.started' | ||
| | 'turn.completed'; | ||
|
|
||
| export type TurnCompletedStatus = 'success' | 'error' | 'paused' | 'max_turns'; | ||
|
|
||
| export type JsonEvent = | ||
| | { | ||
| type: 'message'; | ||
| timestamp: string; | ||
| message: SDKMessage; | ||
| } | ||
| | { | ||
| type: 'progress'; | ||
| timestamp: string; | ||
| message: string; | ||
| } | ||
| | { | ||
| type: 'info'; | ||
| timestamp: string; | ||
| message: string; | ||
| } | ||
| | { | ||
| type: 'error'; | ||
| timestamp: string; | ||
| message: string; | ||
| } | ||
| | { | ||
| type: 'question.asked'; | ||
| timestamp: string; | ||
| questions: Array< { | ||
| question: string; | ||
| options: AskUserQuestion[ 'options' ]; | ||
| } >; | ||
| } | ||
| | { | ||
| type: 'turn.started'; | ||
| timestamp: string; | ||
| } | ||
| | { | ||
| type: 'turn.completed'; | ||
| timestamp: string; | ||
| sessionId: string; | ||
| status: TurnCompletedStatus; | ||
| usage?: { | ||
| numTurns: number; | ||
| costUsd?: number; | ||
| }; | ||
| }; | ||
|
|
||
| export function emitEvent( event: JsonEvent ): void { | ||
| process.stdout.write( JSON.stringify( event ) + '\n' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { DEFAULT_MODEL, type AiModelId, type AskUserQuestion } from 'cli/ai/agent'; | ||
| import { emitEvent, type TurnCompletedStatus } from 'cli/ai/json-events'; | ||
| import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk'; | ||
| import type { AiProviderId } from 'cli/ai/providers'; | ||
| import type { SiteInfo } from 'cli/ai/ui'; | ||
|
|
||
| export type HandleMessageResult = | ||
| | { sessionId: string; success: boolean; maxTurnsReached?: undefined } | ||
| | { sessionId: string; maxTurnsReached: true; numTurns: number; costUsd?: number }; | ||
|
|
||
| export interface AiOutputAdapter { | ||
| currentProvider: AiProviderId; | ||
| currentModel: AiModelId; | ||
| activeSite: SiteInfo | null; | ||
| onSiteSelected: ( ( site: SiteInfo ) => void ) | null; | ||
| onInterrupt: ( () => void ) | null; | ||
|
|
||
| start(): void; | ||
| stop(): void; | ||
| showWelcome(): void; | ||
|
|
||
| showInfo( message: string ): void; | ||
| showError( message: string ): void; | ||
| setStatusMessage( message: string | null ): void; | ||
| setLoaderMessage( message: string ): void; | ||
|
|
||
| beginAgentTurn(): void; | ||
| endAgentTurn(): void; | ||
| addUserMessage( text: string ): void; | ||
| handleMessage( message: SDKMessage ): HandleMessageResult | undefined; | ||
|
|
||
| waitForInput(): Promise< string >; | ||
| askUser( questions: AskUserQuestion[] ): Promise< Record< string, string > >; | ||
| openActiveSiteInBrowser(): Promise< boolean >; | ||
| } | ||
|
|
||
| export class JsonAdapter implements AiOutputAdapter { | ||
| currentProvider: AiProviderId = 'wpcom'; | ||
| currentModel: AiModelId = DEFAULT_MODEL; | ||
| activeSite: SiteInfo | null = null; | ||
| onSiteSelected: ( ( site: SiteInfo ) => void ) | null = null; | ||
| onInterrupt: ( () => void ) | null = null; | ||
| onBeforeExit: ( () => Promise< void > ) | null = null; | ||
|
|
||
| private sessionId: string | undefined; | ||
|
|
||
| start(): void { | ||
| // No-op in JSON mode | ||
| } | ||
|
|
||
| stop(): void { | ||
| // No-op in JSON mode | ||
| } | ||
|
|
||
| showWelcome(): void { | ||
| // No-op in JSON mode | ||
| } | ||
|
|
||
| showInfo( message: string ): void { | ||
| emitEvent( { type: 'info', timestamp: new Date().toISOString(), message } ); | ||
| } | ||
|
|
||
| showError( message: string ): void { | ||
| emitEvent( { type: 'error', timestamp: new Date().toISOString(), message } ); | ||
| } | ||
|
|
||
| setStatusMessage(): void { | ||
| // No-op in JSON mode | ||
| } | ||
|
|
||
| setLoaderMessage( message: string ): void { | ||
| emitEvent( { type: 'progress', timestamp: new Date().toISOString(), message } ); | ||
| } | ||
|
|
||
| beginAgentTurn(): void { | ||
| emitEvent( { type: 'turn.started', timestamp: new Date().toISOString() } ); | ||
| } | ||
|
|
||
| endAgentTurn(): void { | ||
| // turn.completed is emitted separately with status and usage | ||
| } | ||
|
|
||
| addUserMessage( _text: string ): void { | ||
| // No-op in JSON mode — the service already knows the message it sent | ||
| } | ||
|
|
||
| handleMessage( message: SDKMessage ): HandleMessageResult | undefined { | ||
| emitEvent( { type: 'message', timestamp: new Date().toISOString(), message } ); | ||
|
|
||
| if ( message.type === 'result' ) { | ||
| if ( message.subtype === 'success' ) { | ||
| this.sessionId = message.session_id; | ||
| return { sessionId: message.session_id, success: true }; | ||
| } | ||
| if ( message.subtype === 'error_max_turns' ) { | ||
| this.sessionId = message.session_id; | ||
| return { | ||
| sessionId: message.session_id, | ||
| maxTurnsReached: true, | ||
| numTurns: message.num_turns, | ||
| costUsd: message.total_cost_usd, | ||
| }; | ||
| } | ||
| this.sessionId = message.session_id; | ||
| return { sessionId: message.session_id, success: false }; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| emitTurnCompleted( | ||
| status: TurnCompletedStatus, | ||
| usage?: { numTurns: number; costUsd?: number } | ||
| ): void { | ||
| emitEvent( { | ||
| type: 'turn.completed', | ||
| timestamp: new Date().toISOString(), | ||
| sessionId: this.sessionId ?? '', | ||
| status, | ||
| usage, | ||
| } ); | ||
| } | ||
|
|
||
| waitForInput(): Promise< string > { | ||
| throw new Error( 'waitForInput is not available in JSON mode' ); | ||
| } | ||
|
|
||
| async askUser( questions: AskUserQuestion[] ): Promise< Record< string, string > > { | ||
| emitEvent( { | ||
| type: 'question.asked', | ||
| timestamp: new Date().toISOString(), | ||
| questions: questions.map( ( q ) => ( { | ||
| question: q.question, | ||
| options: q.options, | ||
| } ) ), | ||
| } ); | ||
| this.emitTurnCompleted( 'paused' ); | ||
| await this.onBeforeExit?.(); | ||
| process.exit( 0 ); | ||
|
|
||
| // Unreachable, but satisfies TypeScript | ||
| return {}; | ||
| } | ||
|
|
||
| openActiveSiteInBrowser(): Promise< boolean > { | ||
| throw new Error( 'openActiveSiteInBrowser is not available in JSON mode' ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.