openai_agents_rust/tools/
traits.rs

1use crate::agent::traits::AgentContext;
2use crate::error::AgentError;
3use async_trait::async_trait;
4use serde_json::Value;
5
6/// Basic tool trait comparable to Python FunctionTool.
7#[async_trait]
8pub trait Tool: Send + Sync {
9    fn name(&self) -> &str;
10    fn description(&self) -> &str {
11        ""
12    }
13    /// Optional OpenAI Chat Completions tool spec for this tool.
14    /// Return None if the tool shouldn't be exposed to the model.
15    fn openai_tool_spec(&self) -> Option<Value> {
16        None
17    }
18    /// Whether the tool is enabled in the current context.
19    async fn is_enabled(&self, _ctx: &AgentContext) -> bool {
20        true
21    }
22    /// Execute the tool with a string input and return a string output.
23    async fn call(&self, input: &str) -> Result<String, AgentError>;
24
25    /// Optional context-aware call. Default delegates to `call`.
26    async fn call_with_context(
27        &self,
28        ctx: &AgentContext,
29        _tool_call_id: Option<&str>,
30        input: &str,
31    ) -> Result<String, AgentError> {
32        let _ = ctx; // unused by default
33        self.call(input).await
34    }
35}