Add shell command support with ! syntax to API prompt endpoint #2318

Closed
opened 2026-02-16 17:35:06 -05:00 by yindo · 0 comments
Owner

Originally created by @shuv1337 on GitHub (Oct 28, 2025).

Summary

Add support for executing shell commands using the ! syntax in API message prompts, similar to the TUI functionality where !command executes shell commands and returns results.

Problem/Context

Currently, the opencode TUI supports executing shell commands directly in chat using the ! syntax (e.g., !opencode -v). However, the API /session/{id}/message endpoint does not parse or execute shell commands from text parts. Users must make separate calls to the /session/{id}/shell endpoint to run shell commands, which is less convenient and doesn't match the TUI experience.

Acceptance Criteria

  • API message endpoint parses !command syntax in text parts
  • Shell commands are executed automatically when detected in prompts
  • Command output is included in the response message
  • Error handling for failed shell commands
  • Security considerations for command execution
  • Backward compatibility with existing API behavior

Implementation Details

Technical Approach

The shell command parsing logic already exists in the codebase and can be leveraged:

  1. Parser Location: packages/opencode/src/config/markdown.ts exports SHELL_REGEX = /!`([^`]+)`/g
  2. Shell Execution: packages/opencode/src/session/prompt.ts contains the shell() function used by the /session/{id}/shell endpoint
  3. Command Processing: The command() function in prompt.ts already processes shell commands using ConfigMarkdown.shell(template)

Code References

  • Shell Regex: packages/opencode/src/config/markdown.ts:3 - SHELL_REGEX
  • Shell Function: packages/opencode/src/session/prompt.ts:1295-1400 - SessionPrompt.shell()
  • Command Processing: packages/opencode/src/session/prompt.ts:1401-1550 - SessionPrompt.command()
  • API Endpoint: packages/opencode/src/server/server.ts:870-890 - /session/:id/shell route
  • Message Creation: packages/opencode/src/session/prompt.ts:250-400 - createUserMessage()

Integration Points

  1. Modify createUserMessage() in prompt.ts to detect and process shell commands
  2. Use existing SessionPrompt.shell() function for execution
  3. Include shell command results as synthetic text parts in the message
  4. Follow the existing pattern used by file references (@filename)

Security Considerations

  • Use existing permission system from SessionPrompt.shell()
  • Respect agent permissions for bash tool access
  • Maintain current shell execution environment and constraints
  • Apply same abort signal handling as existing shell endpoint

Tasks

  • Analyze current createUserMessage() function for shell command integration
  • Implement shell command detection using ConfigMarkdown.shell()
  • Add shell command execution logic for detected commands
  • Create synthetic text parts for command output
  • Add error handling for failed command execution
  • Update API documentation to reflect new functionality
  • Add tests for shell command parsing and execution
  • Verify backward compatibility with existing API clients

External References

  • Current shell endpoint: POST /session/{id}/shell
  • TUI shell command implementation in CLI
  • OpenAPI specification for message endpoint

Testing Strategy

  • Test basic shell commands: !pwd, !ls -la
  • Test command with output: !opencode -v
  • Test error handling: !false, !nonexistent-command
  • Test mixed content: text with shell commands
  • Test security: permission validation
  • Test backward compatibility: messages without shell commands
Originally created by @shuv1337 on GitHub (Oct 28, 2025). ## Summary Add support for executing shell commands using the ! syntax in API message prompts, similar to the TUI functionality where !command executes shell commands and returns results. ## Problem/Context Currently, the opencode TUI supports executing shell commands directly in chat using the ! syntax (e.g., !opencode -v). However, the API /session/{id}/message endpoint does not parse or execute shell commands from text parts. Users must make separate calls to the /session/{id}/shell endpoint to run shell commands, which is less convenient and doesn't match the TUI experience. ## Acceptance Criteria - [ ] API message endpoint parses !command syntax in text parts - [ ] Shell commands are executed automatically when detected in prompts - [ ] Command output is included in the response message - [ ] Error handling for failed shell commands - [ ] Security considerations for command execution - [ ] Backward compatibility with existing API behavior ## Implementation Details ### Technical Approach The shell command parsing logic already exists in the codebase and can be leveraged: 1. **Parser Location**: packages/opencode/src/config/markdown.ts exports SHELL_REGEX = /!\`([^\`]+)\`/g 2. **Shell Execution**: packages/opencode/src/session/prompt.ts contains the shell() function used by the /session/{id}/shell endpoint 3. **Command Processing**: The command() function in prompt.ts already processes shell commands using ConfigMarkdown.shell(template) ### Code References - **Shell Regex**: packages/opencode/src/config/markdown.ts:3 - SHELL_REGEX - **Shell Function**: packages/opencode/src/session/prompt.ts:1295-1400 - SessionPrompt.shell() - **Command Processing**: packages/opencode/src/session/prompt.ts:1401-1550 - SessionPrompt.command() - **API Endpoint**: packages/opencode/src/server/server.ts:870-890 - /session/:id/shell route - **Message Creation**: packages/opencode/src/session/prompt.ts:250-400 - createUserMessage() ### Integration Points 1. Modify createUserMessage() in prompt.ts to detect and process shell commands 2. Use existing SessionPrompt.shell() function for execution 3. Include shell command results as synthetic text parts in the message 4. Follow the existing pattern used by file references (@filename) ### Security Considerations - Use existing permission system from SessionPrompt.shell() - Respect agent permissions for bash tool access - Maintain current shell execution environment and constraints - Apply same abort signal handling as existing shell endpoint ## Tasks - [ ] Analyze current createUserMessage() function for shell command integration - [ ] Implement shell command detection using ConfigMarkdown.shell() - [ ] Add shell command execution logic for detected commands - [ ] Create synthetic text parts for command output - [ ] Add error handling for failed command execution - [ ] Update API documentation to reflect new functionality - [ ] Add tests for shell command parsing and execution - [ ] Verify backward compatibility with existing API clients ## External References - Current shell endpoint: POST /session/{id}/shell - TUI shell command implementation in CLI - OpenAPI specification for message endpoint ## Testing Strategy - Test basic shell commands: !pwd, !ls -la - Test command with output: !opencode -v - Test error handling: !false, !nonexistent-command - Test mixed content: text with shell commands - Test security: permission validation - Test backward compatibility: messages without shell commands
yindo closed this issue 2026-02-16 17:35:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#2318