Mark any tool calls that lack a corresponding tool result as aborted. When the prompt loop is cancelled mid-execution, some tool calls in the assistant messages may not have received their results yet. This mirrors the TS abort handling in processor.ts that sets incomplete tool parts to error status with "Tool execution aborted".
(session: &mut Session)
| 1428 | /// the TS abort handling in processor.ts that sets incomplete tool parts to |
| 1429 | /// error status with "Tool execution aborted". |
| 1430 | fn abort_pending_tool_calls(session: &mut Session) { |
| 1431 | // Collect all tool call IDs that already have a result |
| 1432 | let mut resolved_call_ids: std::collections::HashSet<String> = |
| 1433 | std::collections::HashSet::new(); |
| 1434 | for msg in &session.messages { |
| 1435 | for part in &msg.parts { |
| 1436 | if let PartType::ToolResult { tool_call_id, .. } = &part.part_type { |
| 1437 | resolved_call_ids.insert(tool_call_id.clone()); |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | // Find unresolved tool calls and add error results |
| 1443 | let mut pending_calls: Vec<String> = Vec::new(); |
| 1444 | for msg in &session.messages { |
| 1445 | for part in &msg.parts { |
| 1446 | if let PartType::ToolCall { id, .. } = &part.part_type { |
| 1447 | if !resolved_call_ids.contains(id) { |
| 1448 | pending_calls.push(id.clone()); |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | if pending_calls.is_empty() { |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | tracing::info!( |
| 1459 | count = pending_calls.len(), |
| 1460 | "Marking pending tool calls as aborted" |
| 1461 | ); |
| 1462 | |
| 1463 | // Add error results for each pending tool call to the last assistant message |
| 1464 | if let Some(last_assistant) = session |
| 1465 | .messages |
| 1466 | .iter_mut() |
| 1467 | .rev() |
| 1468 | .find(|m| matches!(m.role, MessageRole::Assistant)) |
| 1469 | { |
| 1470 | for call_id in pending_calls { |
| 1471 | last_assistant.add_tool_result(&call_id, "Tool execution aborted", true); |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | pub async fn execute_tool_calls( |
| 1477 | session: &mut Session, |