From a01f6180ecbf6dae16ba0b3d72825003f0ad1f56 Mon Sep 17 00:00:00 2001 From: HuiNeng6 <3650306360@qq.com> Date: Tue, 24 Mar 2026 13:03:59 +0800 Subject: [PATCH] fix: pass last shell command as context to forge via --context flag (#2639) This PR resolves #2639 by enabling the ZSH plugin to pass the last executed shell command as context to the Forge agent. This allows the agent to understand what 'fix it' refers to after a failed command (e.g., a failed 'rm' or 'cargo build'). Changes: 1. Added --context CLI flag to accept additional context string 2. Modified UI to pass context as additional_context in the Event 3. Updated ZSH dispatcher to capture and pass the last shell command The solution: - Uses 'fc -ln -1' to get the last command from shell history - Passes it via the new --context flag - The context is added as additional_context to the Event This enables users to run commands like: $ rm test rm: cannot remove 'test': Is a directory $ : fix it And Forge will now know that the user was trying to run 'rm test' before asking for help. --- crates/forge_main/src/cli.rs | 9 +++++++++ crates/forge_main/src/ui.rs | 6 ++++++ shell-plugin/lib/dispatcher.zsh | 12 ++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) 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