Headless enhancement: add stream-json as input-format/output-format to support programmatically use
#926
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.
TLDR
This PR introduces a new architecture for stream-json implementation in
packages/cli/src/services/, establishing a controller-based pattern alongside the existingpackages/cli/src/streamJson/implementation.Known Issue
Todo Actions
Type System Alignment: Refine types in
types/protocol.tsandstreamJson/types.ts, align them with claude-code-like types to gain compatibilityControl Request Refactoring: Refactor current control request handling code to corresponding controllers (gradual rollout)
systemController.ts- System operations refactoringpermissionController.ts- Permission request handling refactoringmcpController.ts- MCP server lifecycle refactoringhookController.ts- Extension hooks refactoringEntry Point Refactoring: Refactor
gemini.tsxandnonInteractiveCli.tsfor better support of the arguments matrix:[input-format, output-format] × [text, json, stream-json]Test Coverage: Ensure E2E tests and unit tests remain green with every task completion
SDK Alignment: Update SDK to align with CLI changes
Comprehensive Testing: Test coverage across all scenarios
Documentation & Examples: Create docs and examples for normal use cases
Behavior Comparison: Let users know the behavior differences with *
Dive Deeper
Background: Existing Implementation
The previous commits (task1-2-2 through task1-2-5) implemented stream-json support in
packages/cli/src/streamJson/:types.ts: Message envelopes and type definitionswriter.ts: StreamJsonWriter for output serializationcontroller.ts: StreamJsonController for control request handlinginput.ts: Input parsing and validationsession.ts: Session management (runStreamJsonSession)io.ts: I/O utilities (extractUserMessageText, writeStreamJsonEnvelope)What's New: Services Architecture
This PR adds a new architecture in
packages/cli/src/services/:Core Service Layer (
packages/cli/src/services/)StreamJson (
StreamJson.ts): JSON Lines protocol handlerMessageRouter (
MessageRouter.ts): Routes control requests to handlersControl Framework (
services/control/):systemController: System operations (config access, abort, lifecycle)permissionController: Permission request handlingmcpController: MCP server lifecycle and tool invocationshookController: Extension hooksProtocol Definition (
packages/cli/src/types/protocol.ts)nonInteractiveStreamJson.tsfor stream-json modeConfiguration Changes
packages/cli/src/config/config.ts: Stream-json format configurationpackages/core/src/config/config.ts: Configuration optionspackages/core/src/output/types.ts: Stream-json output typesTesting Infrastructure
The SDK prototype includes tests that exercise the stream-json features:
Reviewer Test Plan
The review should focus on changes made to the CLI and Core. The SDK implementation is currently only used to evaluate the effectiveness of CLI-side changes.
Running Tests
Manual Testing
Key Areas to Review
New Architecture Implementation:
services/StreamJson.ts: Protocol implementation, message handling, stream managementstreamJson/writer.tsandstreamJson/input.tsservices/MessageRouter.ts: Request routing and validationstreamJson/controller.tsservices/control/framework:ControlDispatcher: Request orchestrationControlContext: Shared statetypes/protocol.ts: Protocol message typesstreamJson/types.tsIntegration Points:
nonInteractiveStreamJson.tsgemini.tsx)streamJson/directoryTesting:
Testing Matrix
Note: Primary testing done on macOS with
npm run.Linked issues / bugs
This PR introduces a new architecture for stream-json implementation.
Development Context:
Phase 0 (commits task1-2-2 through task1-2-5): Initial stream-json implementation
packages/cli/src/streamJson/Phase 1 (this PR): New services architecture
services/directory with controller patterntypes/protocol.tsFuture Phases:
streamJson/toservices/