steer_core/app/
context_util.rs

1use std::sync::Arc;
2use tokio_util::sync::CancellationToken;
3use tracing::{debug, warn};
4
5use crate::api::ToolCall;
6use crate::app::ToolExecutor;
7use steer_tools::result::ToolResult;
8
9// Removed the old execute_tool_with_context function.
10
11/// Executes the core logic of a tool call, handling cancellation.
12/// Returns the raw string output of the tool or an error.
13/// Does NOT interact with Conversation or OpContext directly.
14pub async fn execute_tool_task_logic(
15    tool_call: ToolCall,
16    tool_executor: Arc<ToolExecutor>,
17    // Remove Conversation and event_sender - they are handled in the main loop
18    // conversation: Arc<Mutex<Conversation>>,
19    // event_sender: Option<Sender<AppEvent>>,
20    token: CancellationToken,
21) -> std::result::Result<ToolResult, steer_tools::ToolError> {
22    // Return Result<String, ToolError> instead of TaskResult
23    let tool_id = tool_call.id.clone();
24    let tool_name = tool_call.name.clone();
25
26    debug!(
27        target: "app.context_util.execute_tool_task_logic",
28        "Executing tool {} (ID: {}) logic", tool_name, tool_id,
29    );
30
31    // Check for cancellation before starting
32    if token.is_cancelled() {
33        // Return ToolError::Cancelled
34        return Err(steer_tools::ToolError::Cancelled(format!(
35            "{tool_name} ({tool_id})"
36        )));
37    }
38
39    // Use the cancellation-aware tool execution method
40    let result = tool_executor
41        .execute_tool_with_cancellation(&tool_call, token)
42        .await;
43
44    // Log outcome
45    match &result {
46        Ok(output) => debug!(
47            target:"app.context_util.execute_tool_task_logic",
48            "Tool {} ({}) completed successfully. Output type: {:?}",
49                tool_name,
50                tool_id,
51                std::mem::discriminant(output),
52        ),
53        Err(e) => warn!(
54            target:"app.context_util.execute_tool_task_logic",
55            "Tool {} ({}) failed: {}", tool_name, tool_id, e,
56        ),
57    }
58
59    // Return the raw Result<ToolResult, ToolError>
60    result
61}