Move tool executor to a separate module.

This commit is contained in:
Drew 2026-03-14 14:51:28 -07:00
parent 7420755800
commit 838a5995bd
17 changed files with 465 additions and 476 deletions

View file

@ -16,14 +16,13 @@ use crate::tui::tool_display;
/// arrives, the handler searches `state.messages` for an existing entry with the
/// same `tool_use_id` and replaces its content rather than appending a new row.
///
/// | Event | Effect |
/// |------------------------|------------------------------------------------------------|
/// | `StreamDelta(s)` | Append `s` to last message if it's `Assistant`; else push |
/// | `ToolApprovalRequest` | Push inline message with approval prompt, set pending |
/// | `ToolExecuting` | Replace approval message in-place (or push new) |
/// | `ToolResult` | Replace executing message in-place (or push new) |
/// | `TurnComplete` | No structural change; logged at debug level |
/// | `Error(msg)` | Push `(Assistant, "[error] {msg}")` |
/// | Event | Effect |
/// |-------------------|------------------------------------------------------------|
/// | `StreamDelta(s)` | Append `s` to last message if it's `Assistant`; else push |
/// | `ToolExecuting` | Push new executing message (or replace in-place) |
/// | `ToolResult` | Replace executing message in-place (or push new) |
/// | `TurnComplete` | No structural change; logged at debug level |
/// | `Error(msg)` | Push `(Assistant, "[error] {msg}")` |
pub(super) fn drain_ui_events(event_rx: &mut mpsc::Receiver<StampedEvent>, state: &mut AppState) {
while let Ok(stamped) = event_rx.try_recv() {
// Discard events from before the most recent :clear.
@ -56,21 +55,6 @@ pub(super) fn drain_ui_events(event_rx: &mut mpsc::Receiver<StampedEvent>, state
}
state.content_changed = true;
}
UIEvent::ToolApprovalRequest {
tool_use_id,
tool_name,
display,
} => {
let mut content = tool_display::format_executing(&tool_name, &display);
content.push_str("\n[y] approve [n] deny");
state.messages.push(DisplayMessage {
role: Role::Assistant,
content,
tool_use_id: Some(tool_use_id.clone()),
});
state.pending_approval = Some(PendingApproval { tool_use_id });
state.content_changed = true;
}
UIEvent::ToolExecuting {
tool_use_id,
tool_name,
@ -127,12 +111,6 @@ fn replace_or_push(state: &mut AppState, tool_use_id: &str, content: String) {
}
}
/// A pending tool approval request waiting for user input.
#[derive(Debug, Clone)]
pub struct PendingApproval {
pub tool_use_id: String,
}
#[cfg(test)]
mod tests {
use super::*;
@ -179,29 +157,6 @@ mod tests {
assert_eq!(state.messages[1].content, "hello");
}
#[tokio::test]
async fn drain_tool_approval_sets_pending_and_adds_message() {
let (tx, mut rx) = tokio::sync::mpsc::channel(8);
let mut state = AppState::new();
tx.send(stamp(UIEvent::ToolApprovalRequest {
tool_use_id: "t1".to_string(),
tool_name: "shell_exec".to_string(),
display: ToolDisplay::ShellExec {
command: "cargo test".to_string(),
},
}))
.await
.unwrap();
drop(tx);
drain_ui_events(&mut rx, &mut state);
assert!(state.pending_approval.is_some());
assert_eq!(state.pending_approval.as_ref().unwrap().tool_use_id, "t1");
// Message should be inline with approval prompt.
assert_eq!(state.messages.len(), 1);
assert!(state.messages[0].content.contains("[y] approve"));
assert_eq!(state.messages[0].tool_use_id.as_deref(), Some("t1"));
}
#[tokio::test]
async fn drain_tool_result_replaces_existing_message() {
let (tx, mut rx) = tokio::sync::mpsc::channel(8);