diff --git a/crates/forge_main/src/cli.rs b/crates/forge_main/src/cli.rs index c3f93a1db0..94c9539571 100644 --- a/crates/forge_main/src/cli.rs +++ b/crates/forge_main/src/cli.rs @@ -79,6 +79,15 @@ pub struct Cli { #[arg(long)] pub provider: Option, + /// Additional context to include with the prompt. + /// + /// This allows passing contextual information (e.g., the last shell command + /// executed) to help the agent understand the user's intent. This is + /// particularly useful when using the ZSH plugin's `:` command after + /// running shell commands. + #[arg(long)] + pub context: Option, + /// Top-level subcommands. #[command(subcommand)] pub subcommands: Option, diff --git a/crates/forge_main/src/ui.rs b/crates/forge_main/src/ui.rs index ad3231cdf1..f4a3f2165a 100644 --- a/crates/forge_main/src/ui.rs +++ b/crates/forge_main/src/ui.rs @@ -2955,6 +2955,12 @@ impl A + Send + Sync> UI { event = event.additional_context(piped); } + // Add CLI --context as additional context if provided + // This allows the ZSH plugin to pass the last shell command as context + if let Some(ctx) = &self.cli.context { + event = event.additional_context(ctx.clone()); + } + // Create the chat request with the event let chat = ChatRequest::new(event, conversation_id); diff --git a/shell-plugin/lib/dispatcher.zsh b/shell-plugin/lib/dispatcher.zsh index c50609b430..1bb0f5ef03 100644 --- a/shell-plugin/lib/dispatcher.zsh +++ b/shell-plugin/lib/dispatcher.zsh @@ -78,8 +78,16 @@ function _forge_action_default() { _FORGE_ACTIVE_AGENT="$user_action" fi - # Execute the forge command directly with proper escaping - _forge_exec_interactive -p "$input_text" --cid "$_FORGE_CONVERSATION_ID" + # Capture last command from history for context + # This allows forge to understand what the user was doing before asking for help + local last_cmd=$(fc -ln -1 2>/dev/null || echo "") + + # Execute the forge command with last command context + if [[ -n "$last_cmd" ]]; then + _forge_exec_interactive -p "$input_text" --cid "$_FORGE_CONVERSATION_ID" --context "Last command: $last_cmd" + else + _forge_exec_interactive -p "$input_text" --cid "$_FORGE_CONVERSATION_ID" + fi # Start background sync job if enabled and not already running _forge_start_background_sync