mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
89591e4246
feature: Add "!cmd" user shell execution
This change lets users run local shell commands directly from the TUI by
prefixing their input with ! (e.g. !ls). Output is truncated to keep the
exec cell usable, and Ctrl-C cleanly
interrupts long-running commands (e.g. !sleep 10000).
**Summary of changes**
- Route Op::RunUserShellCommand through a dedicated UserShellCommandTask
(core/src/tasks/user_shell.rs), keeping the task logic out of codex.rs.
- Reuse the existing tool router: the task constructs a ToolCall for the
local_shell tool and relies on ShellHandler, so no manual MCP tool
lookup is required.
- Emit exec lifecycle events (ExecCommandBegin/ExecCommandEnd) so the
TUI can show command metadata, live output, and exit status.
**End-to-end flow**
**TUI handling**
1. ChatWidget::submit_user_message (TUI) intercepts messages starting
with !.
2. Non-empty commands dispatch Op::RunUserShellCommand { command };
empty commands surface a help hint.
3. No UserInput items are created, so nothing is enqueued for the model.
**Core submission loop**
4. The submission loop routes the op to handlers::run_user_shell_command
(core/src/codex.rs).
5. A fresh TurnContext is created and Session::spawn_user_shell_command
enqueues UserShellCommandTask.
**Task execution**
6. UserShellCommandTask::run emits TaskStartedEvent, formats the
command, and prepares a ToolCall targeting local_shell.
7. ToolCallRuntime::handle_tool_call dispatches to ShellHandler.
**Shell tool runtime**
8. ShellHandler::run_exec_like launches the process via the unified exec
runtime, honoring sandbox and shell policies, and emits
ExecCommandBegin/End.
9. Stdout/stderr are captured for the UI, but the task does not turn the
resulting ToolOutput into a model response.
**Completion**
10. After ExecCommandEnd, the task finishes without an assistant
message; the session marks it complete and the exec cell displays the
final output.
**Conversation context**
- The command and its output never enter the conversation history or the
model prompt; the flow is local-only.
- Only exec/task events are emitted for UI rendering.
**Demo video**
https://github.com/user-attachments/assets/fcd114b0-4304-4448-a367-a04c43e0b996
269 lines
8.2 KiB
Rust
269 lines
8.2 KiB
Rust
use crate::codex::Session;
|
|
use crate::codex::TurnContext;
|
|
use crate::tools::TELEMETRY_PREVIEW_MAX_BYTES;
|
|
use crate::tools::TELEMETRY_PREVIEW_MAX_LINES;
|
|
use crate::tools::TELEMETRY_PREVIEW_TRUNCATION_NOTICE;
|
|
use crate::turn_diff_tracker::TurnDiffTracker;
|
|
use codex_otel::otel_event_manager::OtelEventManager;
|
|
use codex_protocol::models::FunctionCallOutputContentItem;
|
|
use codex_protocol::models::FunctionCallOutputPayload;
|
|
use codex_protocol::models::ResponseInputItem;
|
|
use codex_protocol::models::ShellToolCallParams;
|
|
use codex_protocol::protocol::FileChange;
|
|
use codex_utils_string::take_bytes_at_char_boundary;
|
|
use mcp_types::CallToolResult;
|
|
use std::borrow::Cow;
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
use tokio::sync::Mutex;
|
|
|
|
pub type SharedTurnDiffTracker = Arc<Mutex<TurnDiffTracker>>;
|
|
|
|
#[derive(Clone)]
|
|
pub struct ToolInvocation {
|
|
pub session: Arc<Session>,
|
|
pub turn: Arc<TurnContext>,
|
|
pub tracker: SharedTurnDiffTracker,
|
|
pub call_id: String,
|
|
pub tool_name: String,
|
|
pub payload: ToolPayload,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub enum ToolPayload {
|
|
Function {
|
|
arguments: String,
|
|
},
|
|
Custom {
|
|
input: String,
|
|
},
|
|
LocalShell {
|
|
params: ShellToolCallParams,
|
|
},
|
|
UnifiedExec {
|
|
arguments: String,
|
|
},
|
|
Mcp {
|
|
server: String,
|
|
tool: String,
|
|
raw_arguments: String,
|
|
},
|
|
}
|
|
|
|
impl ToolPayload {
|
|
pub fn log_payload(&self) -> Cow<'_, str> {
|
|
match self {
|
|
ToolPayload::Function { arguments } => Cow::Borrowed(arguments),
|
|
ToolPayload::Custom { input } => Cow::Borrowed(input),
|
|
ToolPayload::LocalShell { params } => Cow::Owned(params.command.join(" ")),
|
|
ToolPayload::UnifiedExec { arguments } => Cow::Borrowed(arguments),
|
|
ToolPayload::Mcp { raw_arguments, .. } => Cow::Borrowed(raw_arguments),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub enum ToolOutput {
|
|
Function {
|
|
// Plain text representation of the tool output.
|
|
content: String,
|
|
// Some tool calls such as MCP calls may return structured content that can get parsed into an array of polymorphic content items.
|
|
content_items: Option<Vec<FunctionCallOutputContentItem>>,
|
|
success: Option<bool>,
|
|
},
|
|
Mcp {
|
|
result: Result<CallToolResult, String>,
|
|
},
|
|
}
|
|
|
|
impl ToolOutput {
|
|
pub fn log_preview(&self) -> String {
|
|
match self {
|
|
ToolOutput::Function { content, .. } => telemetry_preview(content),
|
|
ToolOutput::Mcp { result } => format!("{result:?}"),
|
|
}
|
|
}
|
|
|
|
pub fn success_for_logging(&self) -> bool {
|
|
match self {
|
|
ToolOutput::Function { success, .. } => success.unwrap_or(true),
|
|
ToolOutput::Mcp { result } => result.is_ok(),
|
|
}
|
|
}
|
|
|
|
pub fn into_response(self, call_id: &str, payload: &ToolPayload) -> ResponseInputItem {
|
|
match self {
|
|
ToolOutput::Function {
|
|
content,
|
|
content_items,
|
|
success,
|
|
} => {
|
|
if matches!(payload, ToolPayload::Custom { .. }) {
|
|
ResponseInputItem::CustomToolCallOutput {
|
|
call_id: call_id.to_string(),
|
|
output: content,
|
|
}
|
|
} else {
|
|
ResponseInputItem::FunctionCallOutput {
|
|
call_id: call_id.to_string(),
|
|
output: FunctionCallOutputPayload {
|
|
content,
|
|
content_items,
|
|
success,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
ToolOutput::Mcp { result } => ResponseInputItem::McpToolCallOutput {
|
|
call_id: call_id.to_string(),
|
|
result,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
fn telemetry_preview(content: &str) -> String {
|
|
let truncated_slice = take_bytes_at_char_boundary(content, TELEMETRY_PREVIEW_MAX_BYTES);
|
|
let truncated_by_bytes = truncated_slice.len() < content.len();
|
|
|
|
let mut preview = String::new();
|
|
let mut lines_iter = truncated_slice.lines();
|
|
for idx in 0..TELEMETRY_PREVIEW_MAX_LINES {
|
|
match lines_iter.next() {
|
|
Some(line) => {
|
|
if idx > 0 {
|
|
preview.push('\n');
|
|
}
|
|
preview.push_str(line);
|
|
}
|
|
None => break,
|
|
}
|
|
}
|
|
let truncated_by_lines = lines_iter.next().is_some();
|
|
|
|
if !truncated_by_bytes && !truncated_by_lines {
|
|
return content.to_string();
|
|
}
|
|
|
|
if preview.len() < truncated_slice.len()
|
|
&& truncated_slice
|
|
.as_bytes()
|
|
.get(preview.len())
|
|
.is_some_and(|byte| *byte == b'\n')
|
|
{
|
|
preview.push('\n');
|
|
}
|
|
|
|
if !preview.is_empty() && !preview.ends_with('\n') {
|
|
preview.push('\n');
|
|
}
|
|
preview.push_str(TELEMETRY_PREVIEW_TRUNCATION_NOTICE);
|
|
|
|
preview
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[test]
|
|
fn custom_tool_calls_should_roundtrip_as_custom_outputs() {
|
|
let payload = ToolPayload::Custom {
|
|
input: "patch".to_string(),
|
|
};
|
|
let response = ToolOutput::Function {
|
|
content: "patched".to_string(),
|
|
content_items: None,
|
|
success: Some(true),
|
|
}
|
|
.into_response("call-42", &payload);
|
|
|
|
match response {
|
|
ResponseInputItem::CustomToolCallOutput { call_id, output } => {
|
|
assert_eq!(call_id, "call-42");
|
|
assert_eq!(output, "patched");
|
|
}
|
|
other => panic!("expected CustomToolCallOutput, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn function_payloads_remain_function_outputs() {
|
|
let payload = ToolPayload::Function {
|
|
arguments: "{}".to_string(),
|
|
};
|
|
let response = ToolOutput::Function {
|
|
content: "ok".to_string(),
|
|
content_items: None,
|
|
success: Some(true),
|
|
}
|
|
.into_response("fn-1", &payload);
|
|
|
|
match response {
|
|
ResponseInputItem::FunctionCallOutput { call_id, output } => {
|
|
assert_eq!(call_id, "fn-1");
|
|
assert_eq!(output.content, "ok");
|
|
assert!(output.content_items.is_none());
|
|
assert_eq!(output.success, Some(true));
|
|
}
|
|
other => panic!("expected FunctionCallOutput, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn telemetry_preview_returns_original_within_limits() {
|
|
let content = "short output";
|
|
assert_eq!(telemetry_preview(content), content);
|
|
}
|
|
|
|
#[test]
|
|
fn telemetry_preview_truncates_by_bytes() {
|
|
let content = "x".repeat(TELEMETRY_PREVIEW_MAX_BYTES + 8);
|
|
let preview = telemetry_preview(&content);
|
|
|
|
assert!(preview.contains(TELEMETRY_PREVIEW_TRUNCATION_NOTICE));
|
|
assert!(
|
|
preview.len()
|
|
<= TELEMETRY_PREVIEW_MAX_BYTES + TELEMETRY_PREVIEW_TRUNCATION_NOTICE.len() + 1
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn telemetry_preview_truncates_by_lines() {
|
|
let content = (0..(TELEMETRY_PREVIEW_MAX_LINES + 5))
|
|
.map(|idx| format!("line {idx}"))
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
|
|
let preview = telemetry_preview(&content);
|
|
let lines: Vec<&str> = preview.lines().collect();
|
|
|
|
assert!(lines.len() <= TELEMETRY_PREVIEW_MAX_LINES + 1);
|
|
assert_eq!(lines.last(), Some(&TELEMETRY_PREVIEW_TRUNCATION_NOTICE));
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
#[allow(dead_code)]
|
|
pub(crate) struct ExecCommandContext {
|
|
pub(crate) turn: Arc<TurnContext>,
|
|
pub(crate) call_id: String,
|
|
pub(crate) command_for_display: Vec<String>,
|
|
pub(crate) cwd: PathBuf,
|
|
pub(crate) apply_patch: Option<ApplyPatchCommandContext>,
|
|
pub(crate) tool_name: String,
|
|
pub(crate) otel_event_manager: OtelEventManager,
|
|
// TODO(abhisek-oai): Find a better way to track this.
|
|
// https://github.com/openai/codex/pull/2471/files#r2470352242
|
|
pub(crate) is_user_shell_command: bool,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
#[allow(dead_code)]
|
|
pub(crate) struct ApplyPatchCommandContext {
|
|
pub(crate) user_explicitly_approved_this_action: bool,
|
|
pub(crate) changes: HashMap<PathBuf, FileChange>,
|
|
}
|