CLI AI: Add --json flag for headless NDJSON output#2913
CLI AI: Add --json flag for headless NDJSON output#2913wesleyfantinel wants to merge 12 commits intoAutomattic:trunkfrom
--json flag for headless NDJSON output#2913Conversation
- 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
2d3ca2e to
21c2375
Compare
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>
sejas
left a comment
There was a problem hiding this comment.
@wesleyfantinel, thanks for creating this PR. I assume it’s necessary for “Telex.” I was also exploring the Studio AI headless implementation to build the Studio Assistant UI: #2870. I think the best path forward is to merge this PR, and then I can expand this implementation to meet our needs.
I left a few suggestions, but my main concern is implementing autoApprove as a separate parameter. Would that be ok?
Let me know how I can help with this PR.
|
Hey @sejas, thanks for the review and the suggestions. I went through all of them:
Also fixed a bug I found along the way: running Let me know if you want any changes to the approach, specially around |
|
@wesleyfantinel , changes look good. Do you mind fixing the lint and test CI errors? |
|
Opened #3012 |
|
Awesome! Thank you Wesley! |
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 ai "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 ai "hello, what can you do?" --jsonturn.startedis the first event andturn.completedis the lastnode apps/cli/dist/cli/main.mjs ai "hello"--jsonwithout a message shows an error:--no-auto-approveworks with--json:node apps/cli/dist/cli/main.mjs ai "hello" --json --no-auto-approvenpm test -- apps/cli/commands/ai/tests/ai.test.tsPre-merge Checklist