Skip to content

[AgentProtocol] Push client-initiated command routing down to the agent session #24998

@mbleigh

Description

@mbleigh

Parent tracking issue: #22702

Context

Currently, in the stable useGeminiStream flow, the UI explicitly parses slash commands (like /skill) and @-mentions, explicitly schedules the resulting tool calls, and manually injects synthetic turns into the LLM history.

To achieve a true "dumb terminal" TUI, the UI should simply send the user's raw text string to the AgentProtocol. The underlying agent implementation (e.g., LegacyAgentSession or its internal prompt router) should be responsible for intercepting command syntax, executing the necessary tools, managing its internal history, and emitting the appropriate tool_request / tool_response events back to the UI.

Note on Autocomplete: For now, the UI will continue to handle tab-completion and autocomplete suggestions for these commands as it does today. In the future, this capability should also be moved to the protocol level.

Tasks

  • Refactor command parsing: Move the logic that detects and resolves backend slash commands and @-mentions out of the UI hooks and into the core agent logic (e.g., intercepting the initial text in LegacyAgentProtocol._runLoop).
    • Note: Purely UI-local commands (like /help or /clear) should remain handled by the UI.
  • Implement synthetic tool execution in LegacyAgentSession: When a command string is intercepted, the session should execute the corresponding tool directly via the Scheduler.
  • Emit synthetic events: The session must emit tool_request and tool_response events for these intercepted commands. Ensure these events are flagged (e.g., isClientInitiated: true in the meta payload) so the UI can render them appropriately.
  • Manage history: Ensure the LegacyAgentSession automatically injects the synthetic "model" and "user" turns into the underlying LLM history so subsequent requests have the correct context.
  • Refactor useAgentStream.ts: Remove the explicit execution logic for backend commands, simply passing the raw text to agent.send({ message: ... }).
  • Maintain Backward Compatibility: When migrating existing command processors from packages/cli to packages/core, ensure the existing behavior and interfaces used by the stable useGeminiStream.ts are strictly preserved without regressions.
  • Add a TODO comment in the codebase tracking the future migration of command autocomplete/discovery to the AgentProtocol.

Relevant Files

  • `packages/core/src/agent/legacy-agent-session.ts`: Implement interception, execution, and history injection logic.
  • `packages/cli/src/ui/hooks/useAgentStream.ts`: Simplify input processing to pass raw text.
  • (Various existing command processors in `packages/cli/src/ui/hooks/` may need to be migrated to `packages/core`).

Acceptance Criteria

  • Sending a string like `/skill [name]` via `agent.send()` results in the agent emitting tool lifecycle events for the `activate_skill` tool.
  • The UI properly renders these synthetic events.
  • The LLM context correctly reflects the outcome of the command in subsequent turns.
  • Autocomplete continues to function in the TUI as it does today.
  • The stable `useGeminiStream` flow continues to function without changes to its user-facing behavior.

Metadata

Metadata

Assignees

Labels

area/agentIssues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Qualitystatus/need-triageIssues that need to be triaged by the triage automation.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions