|
1 | 1 | use std::sync::Arc; |
| 2 | +use std::time::Duration; |
2 | 3 |
|
3 | 4 | use async_trait::async_trait; |
4 | | -use codex_protocol::models::ShellToolCallParams; |
| 5 | +use codex_async_utils::CancelErr; |
| 6 | +use codex_async_utils::OrCancelExt; |
5 | 7 | use codex_protocol::user_input::UserInput; |
6 | | -use tokio::sync::Mutex; |
7 | 8 | use tokio_util::sync::CancellationToken; |
8 | 9 | use tracing::error; |
9 | 10 | use uuid::Uuid; |
10 | 11 |
|
11 | 12 | use crate::codex::TurnContext; |
| 13 | +use crate::exec::ExecToolCallOutput; |
| 14 | +use crate::exec::SandboxType; |
| 15 | +use crate::exec::StdoutStream; |
| 16 | +use crate::exec::StreamOutput; |
| 17 | +use crate::exec::execute_exec_env; |
| 18 | +use crate::exec_env::create_env; |
| 19 | +use crate::parse_command::parse_command; |
12 | 20 | use crate::protocol::EventMsg; |
| 21 | +use crate::protocol::ExecCommandBeginEvent; |
| 22 | +use crate::protocol::ExecCommandEndEvent; |
| 23 | +use crate::protocol::SandboxPolicy; |
13 | 24 | use crate::protocol::TaskStartedEvent; |
| 25 | +use crate::sandboxing::ExecEnv; |
14 | 26 | use crate::state::TaskKind; |
15 | | -use crate::tools::context::ToolPayload; |
16 | | -use crate::tools::parallel::ToolCallRuntime; |
17 | | -use crate::tools::router::ToolCall; |
18 | | -use crate::tools::router::ToolRouter; |
19 | | -use crate::turn_diff_tracker::TurnDiffTracker; |
| 27 | +use crate::tools::format_exec_output_str; |
| 28 | +use crate::user_shell_command::user_shell_command_record_item; |
20 | 29 |
|
21 | 30 | use super::SessionTask; |
22 | 31 | use super::SessionTaskContext; |
23 | 32 |
|
24 | | -const USER_SHELL_TOOL_NAME: &str = "local_shell"; |
25 | | - |
26 | 33 | #[derive(Clone)] |
27 | 34 | pub(crate) struct UserShellCommandTask { |
28 | 35 | command: String, |
@@ -78,34 +85,126 @@ impl SessionTask for UserShellCommandTask { |
78 | 85 | } |
79 | 86 | }; |
80 | 87 |
|
81 | | - let params = ShellToolCallParams { |
| 88 | + let call_id = Uuid::new_v4().to_string(); |
| 89 | + let raw_command = self.command.clone(); |
| 90 | + |
| 91 | + let parsed_cmd = parse_command(&shell_invocation); |
| 92 | + session |
| 93 | + .send_event( |
| 94 | + turn_context.as_ref(), |
| 95 | + EventMsg::ExecCommandBegin(ExecCommandBeginEvent { |
| 96 | + call_id: call_id.clone(), |
| 97 | + command: shell_invocation.clone(), |
| 98 | + cwd: turn_context.cwd.clone(), |
| 99 | + parsed_cmd, |
| 100 | + is_user_shell_command: true, |
| 101 | + }), |
| 102 | + ) |
| 103 | + .await; |
| 104 | + |
| 105 | + let exec_env = ExecEnv { |
82 | 106 | command: shell_invocation, |
83 | | - workdir: None, |
| 107 | + cwd: turn_context.cwd.clone(), |
| 108 | + env: create_env(&turn_context.shell_environment_policy), |
84 | 109 | timeout_ms: None, |
| 110 | + sandbox: SandboxType::None, |
85 | 111 | with_escalated_permissions: None, |
86 | 112 | justification: None, |
| 113 | + arg0: None, |
87 | 114 | }; |
88 | 115 |
|
89 | | - let tool_call = ToolCall { |
90 | | - tool_name: USER_SHELL_TOOL_NAME.to_string(), |
91 | | - call_id: Uuid::new_v4().to_string(), |
92 | | - payload: ToolPayload::LocalShell { params }, |
93 | | - }; |
| 116 | + let stdout_stream = Some(StdoutStream { |
| 117 | + sub_id: turn_context.sub_id.clone(), |
| 118 | + call_id: call_id.clone(), |
| 119 | + tx_event: session.get_tx_event(), |
| 120 | + }); |
94 | 121 |
|
95 | | - let router = Arc::new(ToolRouter::from_config(&turn_context.tools_config, None)); |
96 | | - let tracker = Arc::new(Mutex::new(TurnDiffTracker::new())); |
97 | | - let runtime = ToolCallRuntime::new( |
98 | | - Arc::clone(&router), |
99 | | - Arc::clone(&session), |
100 | | - Arc::clone(&turn_context), |
101 | | - Arc::clone(&tracker), |
102 | | - ); |
| 122 | + let sandbox_policy = SandboxPolicy::DangerFullAccess; |
| 123 | + let exec_result = execute_exec_env(exec_env, &sandbox_policy, stdout_stream) |
| 124 | + .or_cancel(&cancellation_token) |
| 125 | + .await; |
103 | 126 |
|
104 | | - if let Err(err) = runtime |
105 | | - .handle_tool_call(tool_call, cancellation_token) |
106 | | - .await |
107 | | - { |
108 | | - error!("user shell command failed: {err:?}"); |
| 127 | + match exec_result { |
| 128 | + Err(CancelErr::Cancelled) => { |
| 129 | + let aborted_message = "command aborted by user".to_string(); |
| 130 | + let exec_output = ExecToolCallOutput { |
| 131 | + exit_code: -1, |
| 132 | + stdout: StreamOutput::new(String::new()), |
| 133 | + stderr: StreamOutput::new(aborted_message.clone()), |
| 134 | + aggregated_output: StreamOutput::new(aborted_message.clone()), |
| 135 | + duration: Duration::ZERO, |
| 136 | + timed_out: false, |
| 137 | + }; |
| 138 | + let output_items = [user_shell_command_record_item(&raw_command, &exec_output)]; |
| 139 | + session |
| 140 | + .record_conversation_items(turn_context.as_ref(), &output_items) |
| 141 | + .await; |
| 142 | + session |
| 143 | + .send_event( |
| 144 | + turn_context.as_ref(), |
| 145 | + EventMsg::ExecCommandEnd(ExecCommandEndEvent { |
| 146 | + call_id, |
| 147 | + stdout: String::new(), |
| 148 | + stderr: aborted_message.clone(), |
| 149 | + aggregated_output: aborted_message.clone(), |
| 150 | + exit_code: -1, |
| 151 | + duration: Duration::ZERO, |
| 152 | + formatted_output: aborted_message, |
| 153 | + }), |
| 154 | + ) |
| 155 | + .await; |
| 156 | + } |
| 157 | + Ok(Ok(output)) => { |
| 158 | + session |
| 159 | + .send_event( |
| 160 | + turn_context.as_ref(), |
| 161 | + EventMsg::ExecCommandEnd(ExecCommandEndEvent { |
| 162 | + call_id: call_id.clone(), |
| 163 | + stdout: output.stdout.text.clone(), |
| 164 | + stderr: output.stderr.text.clone(), |
| 165 | + aggregated_output: output.aggregated_output.text.clone(), |
| 166 | + exit_code: output.exit_code, |
| 167 | + duration: output.duration, |
| 168 | + formatted_output: format_exec_output_str(&output), |
| 169 | + }), |
| 170 | + ) |
| 171 | + .await; |
| 172 | + |
| 173 | + let output_items = [user_shell_command_record_item(&raw_command, &output)]; |
| 174 | + session |
| 175 | + .record_conversation_items(turn_context.as_ref(), &output_items) |
| 176 | + .await; |
| 177 | + } |
| 178 | + Ok(Err(err)) => { |
| 179 | + error!("user shell command failed: {err:?}"); |
| 180 | + let message = format!("execution error: {err:?}"); |
| 181 | + let exec_output = ExecToolCallOutput { |
| 182 | + exit_code: -1, |
| 183 | + stdout: StreamOutput::new(String::new()), |
| 184 | + stderr: StreamOutput::new(message.clone()), |
| 185 | + aggregated_output: StreamOutput::new(message.clone()), |
| 186 | + duration: Duration::ZERO, |
| 187 | + timed_out: false, |
| 188 | + }; |
| 189 | + session |
| 190 | + .send_event( |
| 191 | + turn_context.as_ref(), |
| 192 | + EventMsg::ExecCommandEnd(ExecCommandEndEvent { |
| 193 | + call_id, |
| 194 | + stdout: exec_output.stdout.text.clone(), |
| 195 | + stderr: exec_output.stderr.text.clone(), |
| 196 | + aggregated_output: exec_output.aggregated_output.text.clone(), |
| 197 | + exit_code: exec_output.exit_code, |
| 198 | + duration: exec_output.duration, |
| 199 | + formatted_output: format_exec_output_str(&exec_output), |
| 200 | + }), |
| 201 | + ) |
| 202 | + .await; |
| 203 | + let output_items = [user_shell_command_record_item(&raw_command, &exec_output)]; |
| 204 | + session |
| 205 | + .record_conversation_items(turn_context.as_ref(), &output_items) |
| 206 | + .await; |
| 207 | + } |
109 | 208 | } |
110 | 209 | None |
111 | 210 | } |
|
0 commit comments