Skip to main content

distri_types/
tool.rs

1use anyhow::Result;
2use std::{collections::HashMap, sync::Arc};
3use tokio::sync::mpsc;
4
5use crate::Part;
6use crate::{
7    ToolCall, ToolDefinition, auth::AuthMetadata, events::AgentEvent, stores::SessionStore,
8};
9
10/// Tool execution context - lighter weight than ExecutorContext
11#[derive(Debug, Clone)]
12pub struct ToolContext {
13    /// Agent ID executing the tool
14    pub agent_id: String,
15    /// Session ID for the current conversation
16    pub session_id: String,
17    /// Task ID for the current task
18    pub task_id: String,
19    /// Run ID for the current execution
20    pub run_id: String,
21    /// Thread ID for conversation grouping
22    pub thread_id: String,
23    /// User ID if available
24    pub user_id: String,
25    /// Session store for persistent state across tool calls
26    pub session_store: Arc<dyn SessionStore>,
27    /// Event sender for emitting events during tool execution
28    pub event_tx: Option<Arc<mpsc::Sender<AgentEvent>>>,
29
30    /// Additional metadata for the tool. Useful in direct inline agent invocation.
31    pub metadata: Option<HashMap<String, serde_json::Value>>,
32}
33
34/// Tool trait for implementing tools that can be called by agents
35#[async_trait::async_trait]
36pub trait Tool: Send + Sync + std::fmt::Debug + std::any::Any {
37    fn get_name(&self) -> String;
38
39    /// Get the tool definition for the LLM
40    fn get_tool_definition(&self) -> ToolDefinition {
41        ToolDefinition {
42            name: self.get_name(),
43            description: self.get_description(),
44            parameters: self.get_parameters(),
45            output_schema: None,
46            examples: self.get_tool_examples(),
47        }
48    }
49
50    fn get_parameters(&self) -> serde_json::Value;
51    fn get_description(&self) -> String;
52
53    fn get_tool_examples(&self) -> Option<String> {
54        None
55    }
56
57    /// Check if this tool is external (handled by frontend)
58    fn is_external(&self) -> bool {
59        false // Default to false for built-in tools
60    }
61
62    /// Check if this tool is an MCP tool
63    fn is_mcp(&self) -> bool {
64        false // Default to false for built-in tools
65    }
66
67    fn is_sync(&self) -> bool {
68        false // Default to false for built-in tools
69    }
70
71    fn is_final(&self) -> bool {
72        false // Default to false for built-in tools
73    }
74
75    /// Check if this tool needs ExecutorContext instead of ToolContext
76    fn needs_executor_context(&self) -> bool {
77        false // Default to false - most tools use ToolContext
78    }
79
80    /// Get authentication metadata for this tool
81    fn get_auth_metadata(&self) -> Option<Box<dyn AuthMetadata>> {
82        None // Default to no authentication required
83    }
84
85    /// Get the plugin name this tool belongs to (nullable)
86    /// If this returns Some, the tool is part of a plugin
87    /// If None, the tool is standalone
88    fn get_plugin_name(&self) -> Option<String> {
89        None // Default to standalone tool
90    }
91
92    /// Execute the tool with given arguments, returning content parts
93    async fn execute(
94        &self,
95        tool_call: ToolCall,
96        context: Arc<ToolContext>,
97    ) -> Result<Vec<Part>, anyhow::Error>;
98
99    /// Synchronous execution of the tool, returning content parts (default unsupported)
100    fn execute_sync(
101        &self,
102        _tool_call: ToolCall,
103        _context: Arc<ToolContext>,
104    ) -> Result<Vec<Part>, anyhow::Error> {
105        Err(anyhow::anyhow!("Sync execution not supported"))
106    }
107}