CLI AI: Add --json flag for headless NDJSON output - #3012
Merged
Conversation
- Introduced `JsonAdapter` for emitting NDJSON events to stdout. - Enhanced `runCommand` to support headless mode. - Updated AI agent to handle `autoApprove` for non-interactive prompts. - Added tests to validate NDJSON output behavior and error handling.
…andling, so that the type is conformant to zod
Make costUsd optional since only JsonAdapter provides it, guard replaySessionHistory behind InteractiveAdapter instanceof check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 task
Integrate upstream trunk changes (activeSite/wpcomAccessToken for remote sites) with branch changes (autoApprove/JSON mode). Add missing interface methods (showOnboarding, showCapabilities, showSuccess, showProgress, setBusy) to AiOutputAdapter and fix test config for JSON mode tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
📊 Performance Test ResultsComparing 52e5a81 vs trunk app-size
site-editor
site-startup
Results are median values from multiple test runs. Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff) |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…larity in tests and CLI parser
…on-flag # Conflicts: # apps/cli/commands/ai/index.ts
1 task
…and adapter consistency (#3081) - Replace `process.exit()` with `process.exitCode` in JSON mode single-turn path and `JsonAdapter.askUser()` so the event loop drains naturally — stdout flushes and async session persistence completes before the process terminates - Refactor `HandleMessageResult` from a fragile optional-undefined union to a proper discriminated union with a `type` field (`'result' | 'max_turns'`) - Make `JsonAdapter.showProgress()` emit `progress` NDJSON events consistently with `setLoaderMessage()` instead of silently discarding input - Replace unsafe `ui as AiChatUI` cast with a runtime `instanceof` guard that throws a clear error if the adapter isn't `AiChatUI`
sejas
approved these changes
Apr 14, 2026
sejas
left a comment
Member
There was a problem hiding this comment.
Works as expected. Thank you! I pushed a couple of changes. Let me know if you want discuss or change anything. I'll be happy to create a follow-up if necessary.
studio-json-parameter.mov
sejas
enabled auto-merge (squash)
April 14, 2026 09:56
1 task
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Related issues
How AI was used in this PR
Claude was used to brainstorm the design, write the implementation plan, and implement the changes. All code was reviewed and validated by the author.
Proposed Changes
--jsonflag and positionalmessageargument tostudio aicommand for headless modeAiOutputAdapterinterface withAiChatUIimplementing it directly (no wrapper class)JsonAdapterthat streams typed NDJSON events to stdout--auto-approveas a separate CLI flag, independent of--json(defaults totruein JSON mode, overridable with--no-auto-approve)question.askedevents and the process exits after cleanupmessage,progress,info,error,question.asked,turn.started,turn.completedstudio code "hello"in interactive mode now stays in the input loop after the first replyUsage
Files changed
apps/cli/ai/json-events.ts(new)emitEvent()helperapps/cli/ai/output-adapter.ts(new)AiOutputAdapterinterface andJsonAdapterapps/cli/ai/agent.tsautoApprovefield toAiAgentConfig, short-circuitcanUseToolapps/cli/ai/ui.tsAiChatUIimplementsAiOutputAdapterapps/cli/commands/ai/index.tsmessage,--jsonand--auto-approveflags, adapter pattern, single-turn path, initial message fixapps/cli/commands/ai/sessions/resume.tsAiChatUIdirectly instead of removedInteractiveAdapterapps/cli/commands/ai/tests/ai.test.tsTesting Instructions
npm run cli:buildnode apps/cli/dist/cli/main.mjs code "hello, what can you do?" --jsonturn.startedis the first event andturn.completedis the lastnode apps/cli/dist/cli/main.mjs code "hello"--jsonwithout a message shows an error:--no-auto-approveworks with--json:node apps/cli/dist/cli/main.mjs code "hello" --json --no-auto-approvenpm test -- apps/cli/commands/ai/tests/ai.test.tsPre-merge Checklist