Skip to main content

agent_io/tools/
tool.rs

1//! Core tool types and trait
2
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5
6use crate::Result;
7use crate::llm::ToolDefinition;
8
9/// Tool execution result
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ToolResult {
12    /// Tool call ID
13    pub tool_call_id: String,
14    /// Result content
15    pub content: String,
16    /// Whether this result should be ephemeral (removed after use)
17    #[serde(default)]
18    pub ephemeral: bool,
19}
20
21impl ToolResult {
22    pub fn new(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
23        Self {
24            tool_call_id: tool_call_id.into(),
25            content: content.into(),
26            ephemeral: false,
27        }
28    }
29
30    pub fn with_ephemeral(mut self, ephemeral: bool) -> Self {
31        self.ephemeral = ephemeral;
32        self
33    }
34}
35
36/// Trait for defining tools that can be called by an LLM
37#[async_trait]
38pub trait Tool: Send + Sync {
39    /// Get the tool name
40    fn name(&self) -> &str;
41
42    /// Get the tool description
43    fn description(&self) -> &str;
44
45    /// Get the tool definition (JSON Schema)
46    fn definition(&self) -> ToolDefinition;
47
48    /// Execute the tool with given arguments
49    async fn execute(&self, args: serde_json::Value) -> Result<ToolResult>;
50
51    /// Whether tool outputs should be ephemeral (removed from context after use)
52    fn ephemeral(&self) -> EphemeralConfig {
53        EphemeralConfig::None
54    }
55}
56
57/// Configuration for ephemeral tool outputs
58#[derive(Debug, Clone, Copy, PartialEq, Default)]
59pub enum EphemeralConfig {
60    /// Not ephemeral
61    #[default]
62    None,
63    /// Ephemeral, removed after one use
64    Single,
65    /// Keep last N outputs in context
66    Count(usize),
67}