mirror of
https://github.com/openai/codex.git
synced 2026-11-01 19:33:35 +00:00
feature: Add "!cmd" user shell execution
- protocol: add Op::RunUserShellCommand to model a user-initiated one-off command - core: handle new Op by spawning a cancellable task that runs the command using the user’s default shell; stream output via ExecCommand* events; send TaskStarted/TaskComplete; track as current_task so Interrupt works - tui: detect leading '!' in composer submission and dispatch Op::RunUserShellCommand instead of sending a user message No changes to sandbox env var behavior; uses existing exec pipeline and event types.
This commit is contained in:
@@ -114,10 +114,14 @@ use codex_git_tooling::restore_ghost_commit;
|
||||
|
||||
const MAX_TRACKED_GHOST_COMMITS: usize = 20;
|
||||
|
||||
const BANG_COMMAND_HELP_TITLE: &str = "Prefix a command with ! to run it locally";
|
||||
const BANG_COMMAND_HELP_HINT: &str = "Example: !ls";
|
||||
|
||||
// Track information about an in-flight exec command.
|
||||
struct RunningCommand {
|
||||
command: Vec<String>,
|
||||
parsed_cmd: Vec<ParsedCommand>,
|
||||
is_user_shell_command: bool,
|
||||
}
|
||||
|
||||
const RATE_LIMIT_WARNING_THRESHOLDS: [f64; 3] = [75.0, 90.0, 95.0];
|
||||
@@ -617,9 +621,9 @@ impl ChatWidget {
|
||||
|
||||
pub(crate) fn handle_exec_end_now(&mut self, ev: ExecCommandEndEvent) {
|
||||
let running = self.running_commands.remove(&ev.call_id);
|
||||
let (command, parsed) = match running {
|
||||
Some(rc) => (rc.command, rc.parsed_cmd),
|
||||
None => (vec![ev.call_id.clone()], Vec::new()),
|
||||
let (command, parsed, is_user_shell_command) = match running {
|
||||
Some(rc) => (rc.command, rc.parsed_cmd, rc.is_user_shell_command),
|
||||
None => (vec![ev.call_id.clone()], Vec::new(), false),
|
||||
};
|
||||
|
||||
let needs_new = self
|
||||
@@ -633,6 +637,7 @@ impl ChatWidget {
|
||||
ev.call_id.clone(),
|
||||
command,
|
||||
parsed,
|
||||
is_user_shell_command,
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -717,6 +722,7 @@ impl ChatWidget {
|
||||
RunningCommand {
|
||||
command: ev.command.clone(),
|
||||
parsed_cmd: ev.parsed_cmd.clone(),
|
||||
is_user_shell_command: ev.is_user_shell_command,
|
||||
},
|
||||
);
|
||||
if let Some(cell) = self
|
||||
@@ -727,6 +733,7 @@ impl ChatWidget {
|
||||
ev.call_id.clone(),
|
||||
ev.command.clone(),
|
||||
ev.parsed_cmd.clone(),
|
||||
ev.is_user_shell_command,
|
||||
)
|
||||
{
|
||||
*cell = new_exec;
|
||||
@@ -737,6 +744,7 @@ impl ChatWidget {
|
||||
ev.call_id.clone(),
|
||||
ev.command.clone(),
|
||||
ev.parsed_cmd,
|
||||
ev.is_user_shell_command,
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -1171,6 +1179,24 @@ impl ChatWidget {
|
||||
|
||||
let mut items: Vec<InputItem> = Vec::new();
|
||||
|
||||
// Special-case: "!cmd" executes a local shell command instead of sending to the model.
|
||||
if let Some(stripped) = text.strip_prefix('!') {
|
||||
let cmd = stripped.trim();
|
||||
if cmd.is_empty() {
|
||||
self.app_event_tx.send(AppEvent::InsertHistoryCell(Box::new(
|
||||
history_cell::new_info_event(
|
||||
BANG_COMMAND_HELP_TITLE.to_string(),
|
||||
Some(BANG_COMMAND_HELP_HINT.to_string()),
|
||||
),
|
||||
)));
|
||||
return;
|
||||
}
|
||||
self.submit_op(Op::RunUserShellCommand {
|
||||
command: cmd.to_string(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if !text.is_empty() {
|
||||
items.push(InputItem::Text { text: text.clone() });
|
||||
}
|
||||
|
||||
@@ -525,6 +525,7 @@ fn begin_exec(chat: &mut ChatWidget, call_id: &str, raw_cmd: &str) {
|
||||
command,
|
||||
cwd: std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
|
||||
parsed_cmd,
|
||||
is_user_shell_command: false,
|
||||
}),
|
||||
});
|
||||
}
|
||||
@@ -1039,6 +1040,7 @@ async fn binary_size_transcript_snapshot() {
|
||||
.into_iter()
|
||||
.map(std::convert::Into::into)
|
||||
.collect(),
|
||||
is_user_shell_command: false,
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -2026,6 +2028,7 @@ fn chatwidget_exec_and_status_layout_vt100_snapshot() {
|
||||
}
|
||||
.into(),
|
||||
],
|
||||
is_user_shell_command: false,
|
||||
}),
|
||||
});
|
||||
chat.handle_codex_event(Event {
|
||||
|
||||
@@ -281,6 +281,7 @@ pub(crate) struct ExecCall {
|
||||
pub(crate) command: Vec<String>,
|
||||
pub(crate) parsed: Vec<ParsedCommand>,
|
||||
pub(crate) output: Option<CommandOutput>,
|
||||
pub(crate) is_user_shell_command: bool,
|
||||
start_time: Option<Instant>,
|
||||
duration: Option<Duration>,
|
||||
}
|
||||
@@ -493,12 +494,19 @@ impl ExecCell {
|
||||
body_lines.extend(wrapped_borrowed.iter().map(|l| line_to_static(l)));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(output) = call.output.as_ref()
|
||||
&& output.exit_code != 0
|
||||
&& (call.is_user_shell_command || output.exit_code != 0)
|
||||
{
|
||||
let line_limit = if call.is_user_shell_command {
|
||||
USER_SHELL_TOOL_CALL_MAX_LINES
|
||||
} else {
|
||||
TOOL_CALL_MAX_LINES
|
||||
};
|
||||
let out = output_lines(
|
||||
Some(output),
|
||||
OutputLinesParams {
|
||||
line_limit,
|
||||
only_err: false,
|
||||
include_angle_pipe: false,
|
||||
include_prefix: false,
|
||||
@@ -590,11 +598,13 @@ impl ExecCell {
|
||||
call_id: String,
|
||||
command: Vec<String>,
|
||||
parsed: Vec<ParsedCommand>,
|
||||
is_user_shell_command: bool,
|
||||
) -> Option<Self> {
|
||||
let call = ExecCall {
|
||||
call_id,
|
||||
command,
|
||||
parsed,
|
||||
is_user_shell_command,
|
||||
output: None,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
@@ -638,6 +648,7 @@ impl HistoryCell for CompletedMcpToolCallWithImageOutput {
|
||||
|
||||
const TOOL_CALL_MAX_LINES: usize = 5;
|
||||
const SESSION_HEADER_MAX_INNER_WIDTH: usize = 56; // Just an eyeballed value
|
||||
const USER_SHELL_TOOL_CALL_MAX_LINES: usize = 50;
|
||||
|
||||
fn card_inner_width(width: u16, max_inner_width: usize) -> Option<usize> {
|
||||
if width < 4 {
|
||||
@@ -793,11 +804,13 @@ pub(crate) fn new_active_exec_command(
|
||||
call_id: String,
|
||||
command: Vec<String>,
|
||||
parsed: Vec<ParsedCommand>,
|
||||
is_user_shell_command: bool,
|
||||
) -> ExecCell {
|
||||
ExecCell::new(ExecCall {
|
||||
call_id,
|
||||
command,
|
||||
parsed,
|
||||
is_user_shell_command,
|
||||
output: None,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
@@ -1590,6 +1603,7 @@ pub(crate) fn new_patch_apply_failure(stderr: String) -> PlainHistoryCell {
|
||||
formatted_output: String::new(),
|
||||
}),
|
||||
OutputLinesParams {
|
||||
line_limit: TOOL_CALL_MAX_LINES,
|
||||
only_err: true,
|
||||
include_angle_pipe: true,
|
||||
include_prefix: true,
|
||||
@@ -1667,6 +1681,7 @@ pub(crate) fn new_reasoning_summary_block(
|
||||
}
|
||||
|
||||
struct OutputLinesParams {
|
||||
line_limit: usize,
|
||||
only_err: bool,
|
||||
include_angle_pipe: bool,
|
||||
include_prefix: bool,
|
||||
@@ -1674,6 +1689,7 @@ struct OutputLinesParams {
|
||||
|
||||
fn output_lines(output: Option<&CommandOutput>, params: OutputLinesParams) -> Vec<Line<'static>> {
|
||||
let OutputLinesParams {
|
||||
line_limit,
|
||||
only_err,
|
||||
include_angle_pipe,
|
||||
include_prefix,
|
||||
@@ -1692,11 +1708,9 @@ fn output_lines(output: Option<&CommandOutput>, params: OutputLinesParams) -> Ve
|
||||
let src = if *exit_code == 0 { stdout } else { stderr };
|
||||
let lines: Vec<&str> = src.lines().collect();
|
||||
let total = lines.len();
|
||||
let limit = TOOL_CALL_MAX_LINES;
|
||||
|
||||
let mut out = Vec::new();
|
||||
|
||||
let head_end = total.min(limit);
|
||||
let head_end = total.min(line_limit);
|
||||
for (i, raw) in lines[..head_end].iter().enumerate() {
|
||||
let mut line = ansi_escape_line(raw);
|
||||
let prefix = if !include_prefix {
|
||||
@@ -1714,14 +1728,14 @@ fn output_lines(output: Option<&CommandOutput>, params: OutputLinesParams) -> Ve
|
||||
}
|
||||
|
||||
// If we will ellipsize less than the limit, just show it.
|
||||
let show_ellipsis = total > 2 * limit;
|
||||
let show_ellipsis = total > 2 * line_limit;
|
||||
if show_ellipsis {
|
||||
let omitted = total - 2 * limit;
|
||||
let omitted = total - 2 * line_limit;
|
||||
out.push(format!("… +{omitted} lines").into());
|
||||
}
|
||||
|
||||
let tail_start = if show_ellipsis {
|
||||
total - limit
|
||||
total - line_limit
|
||||
} else {
|
||||
head_end
|
||||
};
|
||||
@@ -2127,6 +2141,7 @@ mod tests {
|
||||
},
|
||||
],
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2158,6 +2173,7 @@ mod tests {
|
||||
cmd: "rg shimmer_spans".into(),
|
||||
}],
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2181,6 +2197,7 @@ mod tests {
|
||||
name: "shimmer.rs".into(),
|
||||
cmd: "cat shimmer.rs".into(),
|
||||
}],
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
cell.complete_call(
|
||||
@@ -2202,6 +2219,7 @@ mod tests {
|
||||
name: "status_indicator_widget.rs".into(),
|
||||
cmd: "cat status_indicator_widget.rs".into(),
|
||||
}],
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
cell.complete_call(
|
||||
@@ -2240,6 +2258,7 @@ mod tests {
|
||||
},
|
||||
],
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2268,6 +2287,7 @@ mod tests {
|
||||
command: vec!["bash".into(), "-lc".into(), cmd],
|
||||
parsed: Vec::new(),
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2298,6 +2318,7 @@ mod tests {
|
||||
command: vec!["echo".into(), "ok".into()],
|
||||
parsed: Vec::new(),
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2326,6 +2347,7 @@ mod tests {
|
||||
command: vec!["bash".into(), "-lc".into(), long],
|
||||
parsed: Vec::new(),
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2353,6 +2375,7 @@ mod tests {
|
||||
command: vec!["bash".into(), "-lc".into(), cmd],
|
||||
parsed: Vec::new(),
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2381,6 +2404,7 @@ mod tests {
|
||||
command: vec!["bash".into(), "-lc".into(), cmd],
|
||||
parsed: Vec::new(),
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2409,6 +2433,7 @@ mod tests {
|
||||
command: vec!["bash".into(), "-lc".into(), "seq 1 10 1>&2 && false".into()],
|
||||
parsed: Vec::new(),
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
@@ -2455,6 +2480,7 @@ mod tests {
|
||||
command: vec!["bash".into(), "-lc".into(), long_cmd.to_string()],
|
||||
parsed: Vec::new(),
|
||||
output: None,
|
||||
is_user_shell_command: false,
|
||||
start_time: Some(Instant::now()),
|
||||
duration: None,
|
||||
});
|
||||
|
||||
@@ -691,6 +691,7 @@ mod tests {
|
||||
"exec-1".into(),
|
||||
vec!["bash".into(), "-lc".into(), "ls".into()],
|
||||
vec![ParsedCommand::Unknown { cmd: "ls".into() }],
|
||||
false,
|
||||
);
|
||||
exec_cell.complete_call(
|
||||
"exec-1",
|
||||
|
||||
Reference in New Issue
Block a user