rexis_rag/agent/
executor.rs1use rexis_llm::tools::{ToolCall as ToolExec, ToolRegistry};
4use rexis_llm::{ChatMessage, ToolCall};
5
6pub struct ToolExecutor {
8 registry: ToolRegistry,
9}
10
11impl ToolExecutor {
12 pub fn new(registry: ToolRegistry) -> Self {
14 Self { registry }
15 }
16
17 pub fn execute_tool_call(&self, tool_call: &ToolCall) -> ChatMessage {
19 let tool_exec = ToolExec::new(
21 &tool_call.id,
22 &tool_call.function.name,
23 tool_call.function.arguments.clone(),
24 );
25
26 let result = self.registry.execute(&tool_exec);
28
29 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 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 pub fn registry(&self) -> &ToolRegistry {
49 &self.registry
50 }
51}