use rexis_llm::tools::{ToolCall as ToolExec, ToolRegistry};
use rexis_llm::{ChatMessage, ToolCall};
pub struct ToolExecutor {
registry: ToolRegistry,
}
impl ToolExecutor {
pub fn new(registry: ToolRegistry) -> Self {
Self { registry }
}
pub fn execute_tool_call(&self, tool_call: &ToolCall) -> ChatMessage {
let tool_exec = ToolExec::new(
&tool_call.id,
&tool_call.function.name,
tool_call.function.arguments.clone(),
);
let result = self.registry.execute(&tool_exec);
let result_content = if result.success {
serde_json::to_string(&result.content).unwrap_or_else(|_| "{}".to_string())
} else {
format!("Error: {}", result.error.unwrap_or_default())
};
ChatMessage::tool(&tool_call.id, result_content)
}
pub fn execute_tool_calls(&self, tool_calls: &[ToolCall]) -> Vec<ChatMessage> {
tool_calls
.iter()
.map(|call| self.execute_tool_call(call))
.collect()
}
pub fn registry(&self) -> &ToolRegistry {
&self.registry
}
}