1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use tokio_util::sync::CancellationToken;
5
6use crate::types::ToolResult;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum ToolError {
10 InvalidParameters(String),
11 ExecutionFailed(String),
12 Timeout,
13 Cancelled,
14}
15
16impl std::fmt::Display for ToolError {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 match self {
19 ToolError::InvalidParameters(msg) => write!(f, "Invalid parameters: {}", msg),
20 ToolError::ExecutionFailed(msg) => write!(f, "Execution failed: {}", msg),
21 ToolError::Timeout => write!(f, "Tool execution timed out"),
22 ToolError::Cancelled => write!(f, "Tool execution was cancelled"),
23 }
24 }
25}
26
27impl std::error::Error for ToolError {}
28
29#[async_trait]
30pub trait AgentTool: Send + Sync {
31 fn name(&self) -> &str;
32 fn label(&self) -> String;
33 fn description(&self) -> String;
34 fn parameters_schema(&self) -> Value;
35
36 async fn execute(&self, tool_call_id: &str, params: Value, cancel: CancellationToken) -> Result<ToolResult, ToolError>;
37}