73 lines
1.6 KiB
Rust
73 lines
1.6 KiB
Rust
//! Tool system for agent capabilities
|
|
|
|
use async_trait::async_trait;
|
|
use serde_json::Value;
|
|
use zclaw_types::{AgentId, Result};
|
|
|
|
use crate::driver::ToolDefinition;
|
|
|
|
/// Tool trait for implementing agent tools
|
|
#[async_trait]
|
|
pub trait Tool: Send + Sync {
|
|
/// Get the tool name
|
|
fn name(&self) -> &str;
|
|
|
|
/// Get the tool description
|
|
fn description(&self) -> &str;
|
|
|
|
/// Get the JSON schema for input parameters
|
|
fn input_schema(&self) -> Value;
|
|
|
|
/// Execute the tool
|
|
async fn execute(&self, input: Value, context: &ToolContext) -> Result<Value>;
|
|
}
|
|
|
|
/// Context provided to tool execution
|
|
#[derive(Debug, Clone)]
|
|
pub struct ToolContext {
|
|
pub agent_id: AgentId,
|
|
pub working_directory: Option<String>,
|
|
}
|
|
|
|
/// Tool registry for managing available tools
|
|
pub struct ToolRegistry {
|
|
tools: Vec<Box<dyn Tool>>,
|
|
}
|
|
|
|
impl ToolRegistry {
|
|
pub fn new() -> Self {
|
|
Self { tools: Vec::new() }
|
|
}
|
|
|
|
pub fn register(&mut self, tool: Box<dyn Tool>) {
|
|
self.tools.push(tool);
|
|
}
|
|
|
|
pub fn get(&self, name: &str) -> Option<&dyn Tool> {
|
|
self.tools.iter().find(|t| t.name() == name).map(|t| t.as_ref())
|
|
}
|
|
|
|
pub fn list(&self) -> Vec<&dyn Tool> {
|
|
self.tools.iter().map(|t| t.as_ref()).collect()
|
|
}
|
|
|
|
pub fn definitions(&self) -> Vec<ToolDefinition> {
|
|
self.tools.iter().map(|t| {
|
|
ToolDefinition::new(
|
|
t.name(),
|
|
t.description(),
|
|
t.input_schema(),
|
|
)
|
|
}).collect()
|
|
}
|
|
}
|
|
|
|
impl Default for ToolRegistry {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
// Built-in tools module
|
|
pub mod builtin;
|