rexis_rag/agent/
executor.rs

1//! Tool execution for agents
2
3use rexis_llm::tools::{ToolCall as ToolExec, ToolRegistry};
4use rexis_llm::{ChatMessage, ToolCall};
5
6/// Handles tool execution for the agent
7pub struct ToolExecutor {
8    registry: ToolRegistry,
9}
10
11impl ToolExecutor {
12    /// Create a new tool executor
13    pub fn new(registry: ToolRegistry) -> Self {
14        Self { registry }
15    }
16
17    /// Execute a tool call and return the result message
18    pub fn execute_tool_call(&self, tool_call: &ToolCall) -> ChatMessage {
19        // Convert to ToolExec format
20        let tool_exec = ToolExec::new(
21            &tool_call.id,
22            &tool_call.function.name,
23            tool_call.function.arguments.clone(),
24        );
25
26        // Execute the tool
27        let result = self.registry.execute(&tool_exec);
28
29        // Convert result to chat message
30        let result_content = if result.success {
31            serde_json::to_string(&result.content).unwrap_or_else(|_| "{}".to_string())
32        } else {
33            format!("Error: {}", result.error.unwrap_or_default())
34        };
35
36        ChatMessage::tool(&tool_call.id, result_content)
37    }
38
39    /// Execute multiple tool calls
40    pub fn execute_tool_calls(&self, tool_calls: &[ToolCall]) -> Vec<ChatMessage> {
41        tool_calls
42            .iter()
43            .map(|call| self.execute_tool_call(call))
44            .collect()
45    }
46
47    /// Get the tool registry
48    pub fn registry(&self) -> &ToolRegistry {
49        &self.registry
50    }
51}