plans/2025-09-13-dynamic-agent-slash-commands-v3.md
Implement dynamic registration of agent-specific slash commands that automatically creates commands like /agent-foo and /agent-bar based on the agents returned by the API. This feature will provide direct shortcuts for agent switching without requiring the interactive selection interface of the existing /agent command. The implementation follows the existing command registration pattern used by workflow commands.
Task 1. Add AgentSwitch Command Variant
Rationale: Extend the existing Command enum with a new variant for direct agent switching, following the pattern of other command variants like Custom(PartialEvent).
AgentSwitch(String) variant to Command enum in crates/forge_main/src/model.rs:221crates/forge_main/src/model.rs:325Task 2. Extend ForgeCommandManager with Agent Command Registration
Rationale: Follow the existing pattern used by register_all() method for workflow commands, but create a separate method for agent-based dynamic commands to maintain separation of concerns.
register_agent_commands(&self, agents: Vec<Agent>) method to ForgeCommandManager at crates/forge_main/src/model.rs:63ForgeCommand objects with pattern /agent-{sanitized_id} following workflow command formatvalue field for later retrieval during parsingTask 3. Update ForgeCommandManager Command Parsing
Rationale: Extend the existing parsing logic in the parse() method to detect and handle agent-specific command patterns, similar to how workflow custom commands are handled.
parse() method fallback logic at crates/forge_main/src/model.rs:192 to check for agent commands/agent-* pattern before falling back to custom commands/agent- prefixCommand::AgentSwitch(agent_id) for valid agent commandsTask 4. Integrate Agent Command Registration into UI Initialization
Rationale: Agent commands must be registered during UI initialization right after workflow commands, ensuring they are available throughout the session.
init_state() method in crates/forge_main/src/ui.rs:700 to register agent commandsself.api.get_agents().await? after line 720self.command.register_agent_commands(agents) after workflow registrationinit_state() callsTask 5. Implement Agent Switch Command Handler
Rationale: Create a streamlined handler for direct agent switching in the main UI command processing loop, reusing existing agent switching logic for consistency.
Command::AgentSwitch(agent_id) in the UI command processing loop (around crates/forge_main/src/ui.rs)on_agent_change() logic for the actual switch operationTask 6. Implement Agent ID Sanitization Logic
Rationale: Ensure generated command names are valid shell command identifiers and don't conflict with existing system commands.
/agent, /forge, /muse, etc.)Task 7. Update register_all Method to Handle Combined Registration
Rationale: Ensure the existing workflow command registration doesn't interfere with agent commands and that both can coexist in the command registry.
register_all() method at crates/forge_main/src/model.rs:78 to preserve existing agent commandsTask 8. Add Command Validation and Conflict Resolution
Rationale: Prevent agent command names from conflicting with built-in commands or workflow commands, ensuring system stability.
register_agent_commands() to check for name conflicts/agent-{id} commands in the command registryCommand::AgentSwitch variant/agent commandImpact: Agent IDs might conflict with existing built-in commands like /agent, /forge, /muse
Mitigation:
/agent-{id} prefixing to avoid conflicts with built-in commandsImpact: Loading agents during UI initialization could slow startup Mitigation:
Impact: Agent commands might become stale if agents are reloaded but commands aren't refreshed Mitigation:
init_state() call, which handles session refreshImpact: Unusual agent IDs (empty, special chars, very long) could break command generation Mitigation:
/agent foo instead of creating separate commandsTrade-offs: Simpler implementation but doesn't provide individual completion entries and requires parameter parsing
/switch-{agent} or /to-{agent} instead of /agent-{agent}Trade-offs: Different naming might be clearer but /agent- prefix clearly indicates relationship to the /agent command
Trade-offs: Could reuse more existing logic but would blur the distinction between user-defined and system-generated commands