mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
c0ea566bb5
## TL;DR When you press Esc or Ctrl+C after sending a prompt but before any output was rendering, it restores the last composer and the message. ## Summary Cancelling a prompt immediately after submission should behave like returning to edit that prompt, not like discarding the user's draft. Today, pressing `Esc` or `Ctrl+C` before Codex responds leaves the submitted prompt in the transcript and returns an empty composer, forcing the user to recall or retype it. When an interrupted turn has not produced substantive visible output, restore its submitted prompt directly into the composer and roll back that latest turn. This also covers the first prompt in a fresh thread, before the TUI has retained a local user-history cell. The restored draft keeps its text, image attachments, and active collaboration mode so it can be edited and resubmitted in place. Restoration is intentionally suppressed once the turn has produced user-visible activity such as assistant output, tool work, hooks, or patches. A transient thinking status does not make the prompt ineligible. Rollback also rebuilds terminal scrollback from the retained transcript cells so repeated cancellations and terminal resizes do not duplicate history. ## How to Test 1. Start the TUI with `cargo run -p codex-cli --bin codex`. 2. In a fresh thread, submit the first prompt and press `Esc` before Codex emits substantive output. Confirm that the prompt returns to the composer for editing and its submitted transcript row is removed. 3. Repeat with `Ctrl+C`, then repeat after at least one completed turn. Confirm the same behavior. 4. Submit a prompt, wait for assistant output or tool activity, then cancel. Confirm that the transcript remains intact and the prompt is not restored into the composer. 5. Cancel several output-free prompts and resize the terminal between attempts. Confirm that the startup banner, tip, and transcript history do not duplicate in scrollback. Targeted tests: - `just test -p codex-tui cancelled_turn_edit_restores_prompt` - `just test -p codex-tui output_free_interrupted_turn_requests_prompt_restore` - `just test -p codex-tui visible_output_prevents_cancelled_turn_prompt_restore` - `just test -p codex-tui thinking_status_keeps_cancelled_turn_prompt_restore_eligible` - `just test -p codex-tui patch_activity_prevents_cancelled_turn_prompt_restore` The full `just test -p codex-tui` run completed with `2746` passing tests and two unrelated existing guardian feature-flag failures. `just argument-comment-lint` remains blocked locally by the existing Bazel LLVM `compiler-rt` sanitizer-header glob failure; the touched Rust diff was manually audited for positional literal comments.
271 lines
8.8 KiB
Rust
271 lines
8.8 KiB
Rust
//! Non-command tool lifecycle rendering for `ChatWidget`.
|
|
//!
|
|
//! This module handles patch, MCP, web search, image, and collaborator tool
|
|
//! events as transcript cells.
|
|
|
|
use super::*;
|
|
|
|
impl ChatWidget {
|
|
pub(super) fn on_patch_apply_begin(&mut self, changes: HashMap<PathBuf, FileChange>) {
|
|
self.record_visible_turn_activity();
|
|
self.add_to_history(history_cell::new_patch_event(changes, &self.config.cwd));
|
|
}
|
|
|
|
pub(super) fn on_view_image_tool_call(&mut self, path: AbsolutePathBuf) {
|
|
self.record_visible_turn_activity();
|
|
self.flush_answer_stream_with_separator();
|
|
self.add_to_history(history_cell::new_view_image_tool_call(
|
|
path,
|
|
&self.config.cwd,
|
|
));
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(super) fn on_image_generation_begin(&mut self) {
|
|
self.record_visible_turn_activity();
|
|
self.flush_answer_stream_with_separator();
|
|
}
|
|
|
|
pub(super) fn on_image_generation_end(
|
|
&mut self,
|
|
call_id: String,
|
|
revised_prompt: Option<String>,
|
|
saved_path: Option<AbsolutePathBuf>,
|
|
) {
|
|
self.flush_answer_stream_with_separator();
|
|
self.add_to_history(history_cell::new_image_generation_call(
|
|
call_id,
|
|
revised_prompt,
|
|
saved_path,
|
|
));
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(super) fn on_file_change_completed(&mut self, item: ThreadItem) {
|
|
let item2 = item.clone();
|
|
self.defer_or_handle(
|
|
|q| q.push_item_completed(item),
|
|
|s| s.handle_file_change_completed_now(item2),
|
|
);
|
|
}
|
|
|
|
pub(super) fn on_mcp_tool_call_started(&mut self, item: ThreadItem) {
|
|
let item2 = item.clone();
|
|
self.defer_or_handle(
|
|
|q| q.push_item_started(item),
|
|
|s| s.handle_mcp_tool_call_started_now(item2),
|
|
);
|
|
}
|
|
|
|
pub(super) fn on_mcp_tool_call_completed(&mut self, item: ThreadItem) {
|
|
let item2 = item.clone();
|
|
self.defer_or_handle(
|
|
|q| q.push_item_completed(item),
|
|
|s| s.handle_mcp_tool_call_completed_now(item2),
|
|
);
|
|
}
|
|
|
|
pub(super) fn on_web_search_begin(&mut self, call_id: String) {
|
|
self.record_visible_turn_activity();
|
|
self.flush_answer_stream_with_separator();
|
|
self.flush_active_cell();
|
|
self.transcript.active_cell = Some(Box::new(history_cell::new_active_web_search_call(
|
|
call_id,
|
|
String::new(),
|
|
self.config.animations,
|
|
)));
|
|
self.bump_active_cell_revision();
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(super) fn on_web_search_end(
|
|
&mut self,
|
|
call_id: String,
|
|
query: String,
|
|
action: codex_app_server_protocol::WebSearchAction,
|
|
) {
|
|
self.flush_answer_stream_with_separator();
|
|
let mut handled = false;
|
|
if let Some(cell) = self
|
|
.transcript
|
|
.active_cell
|
|
.as_mut()
|
|
.and_then(|cell| cell.as_any_mut().downcast_mut::<WebSearchCell>())
|
|
&& cell.call_id() == call_id
|
|
{
|
|
cell.update(action.clone(), query.clone());
|
|
cell.complete();
|
|
self.bump_active_cell_revision();
|
|
self.flush_active_cell();
|
|
handled = true;
|
|
}
|
|
|
|
if !handled {
|
|
self.add_to_history(history_cell::new_web_search_call(call_id, query, action));
|
|
}
|
|
self.transcript.had_work_activity = true;
|
|
}
|
|
|
|
pub(super) fn on_collab_event(&mut self, cell: PlainHistoryCell) {
|
|
self.flush_answer_stream_with_separator();
|
|
self.add_to_history(cell);
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(super) fn on_collab_agent_tool_call(&mut self, item: ThreadItem) {
|
|
self.record_visible_turn_activity();
|
|
let ThreadItem::CollabAgentToolCall {
|
|
id, tool, status, ..
|
|
} = &item
|
|
else {
|
|
return;
|
|
};
|
|
if matches!(tool, CollabAgentTool::SpawnAgent)
|
|
&& let Some(spawn_request) = multi_agents::spawn_request_summary(&item)
|
|
{
|
|
self.pending_collab_spawn_requests
|
|
.insert(id.clone(), spawn_request);
|
|
}
|
|
|
|
let cached_spawn_request = if matches!(tool, CollabAgentTool::SpawnAgent)
|
|
&& !matches!(status, CollabAgentToolCallStatus::InProgress)
|
|
{
|
|
self.pending_collab_spawn_requests.remove(id)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
if let Some(cell) = multi_agents::tool_call_history_cell(
|
|
&item,
|
|
cached_spawn_request.as_ref(),
|
|
|thread_id| self.collab_agent_metadata(thread_id),
|
|
) {
|
|
self.on_collab_event(cell);
|
|
}
|
|
}
|
|
|
|
pub(crate) fn handle_file_change_completed_now(&mut self, item: ThreadItem) {
|
|
let ThreadItem::FileChange { status, .. } = item else {
|
|
return;
|
|
};
|
|
// If the patch was successful, just let the "Edited" block stand.
|
|
// Otherwise, add a failure block.
|
|
if matches!(status, codex_app_server_protocol::PatchApplyStatus::Failed) {
|
|
self.add_to_history(history_cell::new_patch_apply_failure(String::new()));
|
|
}
|
|
// Mark that actual work was done (patch applied)
|
|
self.transcript.had_work_activity = true;
|
|
}
|
|
|
|
pub(crate) fn handle_mcp_tool_call_started_now(&mut self, item: ThreadItem) {
|
|
self.record_visible_turn_activity();
|
|
let ThreadItem::McpToolCall {
|
|
id,
|
|
server,
|
|
tool,
|
|
arguments,
|
|
..
|
|
} = item
|
|
else {
|
|
return;
|
|
};
|
|
self.flush_answer_stream_with_separator();
|
|
self.flush_active_cell();
|
|
self.transcript.active_cell = Some(Box::new(history_cell::new_active_mcp_tool_call(
|
|
id,
|
|
McpInvocation {
|
|
server,
|
|
tool,
|
|
arguments: Some(arguments),
|
|
},
|
|
self.config.animations,
|
|
)));
|
|
self.bump_active_cell_revision();
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(crate) fn handle_mcp_tool_call_completed_now(&mut self, item: ThreadItem) {
|
|
self.flush_answer_stream_with_separator();
|
|
|
|
let ThreadItem::McpToolCall {
|
|
id,
|
|
server,
|
|
tool,
|
|
arguments,
|
|
result,
|
|
error,
|
|
duration_ms,
|
|
..
|
|
} = item
|
|
else {
|
|
return;
|
|
};
|
|
let invocation = McpInvocation {
|
|
server,
|
|
tool,
|
|
arguments: Some(arguments),
|
|
};
|
|
let duration = Duration::from_millis(duration_ms.unwrap_or_default().max(0) as u64);
|
|
let result = match (result, error) {
|
|
(_, Some(error)) => Err(error.message),
|
|
(Some(result), None) => {
|
|
let result = *result;
|
|
Ok(codex_protocol::mcp::CallToolResult {
|
|
content: result.content,
|
|
structured_content: result.structured_content,
|
|
is_error: Some(false),
|
|
meta: None,
|
|
})
|
|
}
|
|
(None, None) => Err("MCP tool call completed without a result".to_string()),
|
|
};
|
|
|
|
let extra_cell = match self
|
|
.transcript
|
|
.active_cell
|
|
.as_mut()
|
|
.and_then(|cell| cell.as_any_mut().downcast_mut::<McpToolCallCell>())
|
|
{
|
|
Some(cell) if cell.call_id() == id => cell.complete(duration, result),
|
|
_ => {
|
|
self.flush_active_cell();
|
|
let mut cell =
|
|
history_cell::new_active_mcp_tool_call(id, invocation, self.config.animations);
|
|
let extra_cell = cell.complete(duration, result);
|
|
self.transcript.active_cell = Some(Box::new(cell));
|
|
extra_cell
|
|
}
|
|
};
|
|
|
|
self.flush_active_cell();
|
|
if let Some(extra) = extra_cell {
|
|
self.add_boxed_history(extra);
|
|
}
|
|
// Mark that actual work was done (MCP tool call)
|
|
self.transcript.had_work_activity = true;
|
|
}
|
|
|
|
pub(crate) fn handle_queued_item_started_now(&mut self, item: ThreadItem) {
|
|
match item {
|
|
item @ ThreadItem::CommandExecution { .. } => {
|
|
self.handle_command_execution_started_now(item);
|
|
}
|
|
item @ ThreadItem::McpToolCall { .. } => {
|
|
self.handle_mcp_tool_call_started_now(item);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn handle_queued_item_completed_now(&mut self, item: ThreadItem) {
|
|
match item {
|
|
item @ ThreadItem::CommandExecution { .. } => {
|
|
self.handle_command_execution_completed_now(item);
|
|
}
|
|
item @ ThreadItem::FileChange { .. } => self.handle_file_change_completed_now(item),
|
|
item @ ThreadItem::McpToolCall { .. } => self.handle_mcp_tool_call_completed_now(item),
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|