[PR #533] [CLOSED] feat: add print mode flag to run command #9636

Closed
opened 2026-02-16 18:13:54 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/anomalyco/opencode/pull/533
Author: @monotykamary
Created: 6/29/2025
Status: Closed

Base: devHead: feat/print-mode-flag


📝 Commits (10+)

  • 1f7e9ed feat: add print mode flag to run command
  • 2f24b62 feat: replace hard-coded values with dynamic data in print mode
  • 0d3b40b feat: improve tool name mapping and add missing tools
  • 65f62d0 fix: distinguish TodoWrite and TodoRead tools in display names
  • b4311a3 feat: add separate tool name mapping for stream-json output
  • a60697c fix: add missing newline to print mode text output
  • 0fe6f06 Merge dev into feat/print-mode-flag
  • 350ef16 Merge dev into feat/print-mode-flag - resolved conflicts
  • 1e79fde Merge dev branch into feat/print-mode-flag
  • d8a0b77 Merge upstream/dev into feat/print-mode-flag

📊 Changes

1 file changed (+369 additions, -44 deletions)

View changed files

📝 packages/opencode/src/cli/cmd/run.ts (+369 -44)

📄 Description

Summary

Adds a -p/--print flag to the run command that enables print mode with multiple output formats, solving the feature request in #278. The implementation uses dynamic data sources to accurately reflect the actual runtime configuration.

Changes

  • Print Mode Flag: Added -p/--print flag to run command
  • Output Formats: Support for text (default), json, and stream-json formats
  • Dynamic Data: Replaced hard-coded values with dynamic functions that reflect actual runtime state
  • Validation: stream-json format requires --verbose flag
  • Error Handling: Proper validation and error messages for edge cases
  • Backward Compatibility: Existing run command behavior is unchanged

Usage Examples

Text Output (Default)

$ opencode run -p "what is 2+2?"
4

JSON Output

$ opencode run --print --output-format json "what is 2+2?"
{"type":"result","subtype":"success","is_error":false,"duration_ms":2231,"duration_api_ms":2227,"num_turns":1,"result":"4","session_id":"ses_844cdda80ffeaNCnvw65ycUC6k","total_cost_usd":0,"usage":{"input_tokens":4,"cache_creation_input_tokens":0,"cache_read_input_tokens":11329,"output_tokens":5,"server_tool_use":{"web_search_requests":0},"service_tier":"standard"}}

Stream JSON Output (with verbose)

$ opencode run -p --output-format stream-json --verbose "what is 2+2?"
{"type":"system","subtype":"init","cwd":"/path/to/project","session_id":"ses_844cdd08affeNGo8Caw4wSm6Gw","tools":["Bash","Edit","WebFetch","Glob","Grep","LS","lsp_diagnostics","lsp_hover","Read","Edit","Write","TodoWrite","TodoRead"],"mcp_servers":[],"model":"anthropic-claude-sonnet-4-20250514","permissionMode":"default","apiKeySource":"ANTHROPIC_OAUTH"}
{"type":"assistant","message":{"id":"msg_7bb322f8c001n8Ehhz6liEBL9L","type":"message","role":"assistant","model":"anthropic-claude-sonnet-4-20250514","content":[{"type":"text","text":"4"}],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":4,"cache_creation_input_tokens":0,"cache_read_input_tokens":11329,"output_tokens":5,"service_tier":"standard"}},"parent_tool_use_id":null,"session_id":"ses_844cdd08affeNGo8Caw4wSm6Gw"}
{"type":"result","subtype":"success","is_error":false,"duration_ms":2508,"duration_api_ms":2504,"num_turns":1,"result":"4","session_id":"ses_844cdd08affeNGo8Caw4wSm6Gw","total_cost_usd":0,"usage":{"input_tokens":4,"cache_creation_input_tokens":0,"cache_read_input_tokens":11329,"output_tokens":5,"server_tool_use":{"web_search_requests":0},"service_tier":"standard"}}

Validation & Error Handling

Stream-JSON requires verbose flag

$ opencode run -p --output-format stream-json "test"
Error: When using --print, --output-format=stream-json requires --verbose

Empty message validation

$ opencode run -p
Error: No message provided

Features

  • Multiple Output Formats:

    • text: Simple text output (default)
    • json: Structured JSON with session metadata, timing, costs, and token usage
    • stream-json: Multiple JSON objects including system init, assistant message, and final result
  • Dynamic Runtime Data: System init output reflects actual configuration:

    • Tools: Shows available tools from Provider.tools() and active MCP tools
    • MCP Servers: Lists active MCP server names from MCP.clients()
    • API Key Source: Detects actual authentication method (API key, OAuth, env vars)
  • Comprehensive Metadata: JSON formats include:

    • Session ID and timing information
    • API call duration and total duration
    • Token usage (input, output, cache read/write)
    • Cost tracking
    • Error status and handling
  • Validation:

    • stream-json format requires --verbose flag
    • Empty message validation with appropriate error responses
    • Proper exit codes for error conditions
  • Backward Compatibility: Normal run command behavior is completely unchanged

Implementation Details

  • Integrated into existing RunCommand rather than creating a separate command
  • Reuses existing session management and chat infrastructure
  • Maintains all existing run command functionality (continue, session, share, model options)
  • Clean separation between print mode and interactive mode logic
  • Dynamic data functions that query actual runtime state instead of using hard-coded values
  • Proper TypeScript interfaces for all output formats

Testing

All output formats and validation scenarios have been tested:

  • Text output format
  • JSON output format with full metadata
  • Stream-JSON output format with verbose mode
  • Dynamic tools, MCP servers, and API key source detection
  • Validation error for stream-json without verbose
  • Empty message validation
  • Normal run command behavior unchanged
  • Error handling and exit codes

Closes #278

🤖 Generated with opencode


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/anomalyco/opencode/pull/533 **Author:** [@monotykamary](https://github.com/monotykamary) **Created:** 6/29/2025 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `feat/print-mode-flag` --- ### 📝 Commits (10+) - [`1f7e9ed`](https://github.com/anomalyco/opencode/commit/1f7e9ed1b1091d322383fa73a0b73e7c5afebc5c) feat: add print mode flag to run command - [`2f24b62`](https://github.com/anomalyco/opencode/commit/2f24b6284af7517b16c62a783ab80504141ad181) feat: replace hard-coded values with dynamic data in print mode - [`0d3b40b`](https://github.com/anomalyco/opencode/commit/0d3b40b60a579e8d0f56da1f4670f4498da559b3) feat: improve tool name mapping and add missing tools - [`65f62d0`](https://github.com/anomalyco/opencode/commit/65f62d00b29f5b79a97ec08bd8dff8ab49114063) fix: distinguish TodoWrite and TodoRead tools in display names - [`b4311a3`](https://github.com/anomalyco/opencode/commit/b4311a3611716288a99035571e5702dc8ae91b3f) feat: add separate tool name mapping for stream-json output - [`a60697c`](https://github.com/anomalyco/opencode/commit/a60697ce1fd0d1e0f2c4f930a456b8fe73ccbeda) fix: add missing newline to print mode text output - [`0fe6f06`](https://github.com/anomalyco/opencode/commit/0fe6f06da8d88482d921e23a7422a7be737b95d6) Merge dev into feat/print-mode-flag - [`350ef16`](https://github.com/anomalyco/opencode/commit/350ef16cc647415f1b473cdea50b18a9fc0ba87e) Merge dev into feat/print-mode-flag - resolved conflicts - [`1e79fde`](https://github.com/anomalyco/opencode/commit/1e79fde5daf57a8bbbd226c4d046c6cea24f0bf8) Merge dev branch into feat/print-mode-flag - [`d8a0b77`](https://github.com/anomalyco/opencode/commit/d8a0b779795844b1966c8b68d6872bf00cb5e9d2) Merge upstream/dev into feat/print-mode-flag ### 📊 Changes **1 file changed** (+369 additions, -44 deletions) <details> <summary>View changed files</summary> 📝 `packages/opencode/src/cli/cmd/run.ts` (+369 -44) </details> ### 📄 Description ## Summary Adds a `-p/--print` flag to the `run` command that enables print mode with multiple output formats, solving the feature request in #278. The implementation uses dynamic data sources to accurately reflect the actual runtime configuration. ## Changes - **Print Mode Flag**: Added `-p/--print` flag to `run` command - **Output Formats**: Support for `text` (default), `json`, and `stream-json` formats - **Dynamic Data**: Replaced hard-coded values with dynamic functions that reflect actual runtime state - **Validation**: `stream-json` format requires `--verbose` flag - **Error Handling**: Proper validation and error messages for edge cases - **Backward Compatibility**: Existing `run` command behavior is unchanged ## Usage Examples ### Text Output (Default) ```bash $ opencode run -p "what is 2+2?" 4 ``` ### JSON Output ```bash $ opencode run --print --output-format json "what is 2+2?" {"type":"result","subtype":"success","is_error":false,"duration_ms":2231,"duration_api_ms":2227,"num_turns":1,"result":"4","session_id":"ses_844cdda80ffeaNCnvw65ycUC6k","total_cost_usd":0,"usage":{"input_tokens":4,"cache_creation_input_tokens":0,"cache_read_input_tokens":11329,"output_tokens":5,"server_tool_use":{"web_search_requests":0},"service_tier":"standard"}} ``` ### Stream JSON Output (with verbose) ```bash $ opencode run -p --output-format stream-json --verbose "what is 2+2?" {"type":"system","subtype":"init","cwd":"/path/to/project","session_id":"ses_844cdd08affeNGo8Caw4wSm6Gw","tools":["Bash","Edit","WebFetch","Glob","Grep","LS","lsp_diagnostics","lsp_hover","Read","Edit","Write","TodoWrite","TodoRead"],"mcp_servers":[],"model":"anthropic-claude-sonnet-4-20250514","permissionMode":"default","apiKeySource":"ANTHROPIC_OAUTH"} {"type":"assistant","message":{"id":"msg_7bb322f8c001n8Ehhz6liEBL9L","type":"message","role":"assistant","model":"anthropic-claude-sonnet-4-20250514","content":[{"type":"text","text":"4"}],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":4,"cache_creation_input_tokens":0,"cache_read_input_tokens":11329,"output_tokens":5,"service_tier":"standard"}},"parent_tool_use_id":null,"session_id":"ses_844cdd08affeNGo8Caw4wSm6Gw"} {"type":"result","subtype":"success","is_error":false,"duration_ms":2508,"duration_api_ms":2504,"num_turns":1,"result":"4","session_id":"ses_844cdd08affeNGo8Caw4wSm6Gw","total_cost_usd":0,"usage":{"input_tokens":4,"cache_creation_input_tokens":0,"cache_read_input_tokens":11329,"output_tokens":5,"server_tool_use":{"web_search_requests":0},"service_tier":"standard"}} ``` ## Validation & Error Handling ### Stream-JSON requires verbose flag ```bash $ opencode run -p --output-format stream-json "test" Error: When using --print, --output-format=stream-json requires --verbose ``` ### Empty message validation ```bash $ opencode run -p Error: No message provided ``` ## Features - **Multiple Output Formats**: - `text`: Simple text output (default) - `json`: Structured JSON with session metadata, timing, costs, and token usage - `stream-json`: Multiple JSON objects including system init, assistant message, and final result - **Dynamic Runtime Data**: System init output reflects actual configuration: - **Tools**: Shows available tools from `Provider.tools()` and active MCP tools - **MCP Servers**: Lists active MCP server names from `MCP.clients()` - **API Key Source**: Detects actual authentication method (API key, OAuth, env vars) - **Comprehensive Metadata**: JSON formats include: - Session ID and timing information - API call duration and total duration - Token usage (input, output, cache read/write) - Cost tracking - Error status and handling - **Validation**: - `stream-json` format requires `--verbose` flag - Empty message validation with appropriate error responses - Proper exit codes for error conditions - **Backward Compatibility**: Normal `run` command behavior is completely unchanged ## Implementation Details - Integrated into existing `RunCommand` rather than creating a separate command - Reuses existing session management and chat infrastructure - Maintains all existing `run` command functionality (continue, session, share, model options) - Clean separation between print mode and interactive mode logic - Dynamic data functions that query actual runtime state instead of using hard-coded values - Proper TypeScript interfaces for all output formats ## Testing All output formats and validation scenarios have been tested: - ✅ Text output format - ✅ JSON output format with full metadata - ✅ Stream-JSON output format with verbose mode - ✅ Dynamic tools, MCP servers, and API key source detection - ✅ Validation error for stream-json without verbose - ✅ Empty message validation - ✅ Normal run command behavior unchanged - ✅ Error handling and exit codes Closes #278 🤖 Generated with [opencode](https://opencode.ai) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 18:13:54 -05:00
yindo closed this issue 2026-02-16 18:13:54 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#9636