Skip to main content

vv_agent/tools/
public_tool.rs

1use serde_json::Value;
2
3use crate::tools::{ToolHandler, ToolSpec};
4use crate::types::ToolExecutionResult;
5
6pub trait Tool: Send + Sync {
7    fn name(&self) -> &str;
8    fn description(&self) -> &str;
9    fn parameters_schema(&self) -> &Value;
10
11    fn strict_schema(&self) -> bool {
12        true
13    }
14
15    fn as_tool_spec(&self) -> ToolSpec;
16}
17
18#[derive(Clone)]
19pub struct StaticTool {
20    name: String,
21    description: String,
22    parameters_schema: Value,
23    handler: ToolHandler,
24}
25
26impl StaticTool {
27    pub fn new(
28        name: impl Into<String>,
29        description: impl Into<String>,
30        parameters_schema: Value,
31        handler: ToolHandler,
32    ) -> Self {
33        Self {
34            name: name.into(),
35            description: description.into(),
36            parameters_schema,
37            handler,
38        }
39    }
40}
41
42impl Tool for StaticTool {
43    fn name(&self) -> &str {
44        &self.name
45    }
46
47    fn description(&self) -> &str {
48        &self.description
49    }
50
51    fn parameters_schema(&self) -> &Value {
52        &self.parameters_schema
53    }
54
55    fn as_tool_spec(&self) -> ToolSpec {
56        let mut spec = ToolSpec::new(
57            self.name.clone(),
58            self.description.clone(),
59            self.handler.clone(),
60        );
61        spec.schema = serde_json::json!({
62            "type": "function",
63            "function": {
64                "name": self.name,
65                "description": self.description,
66                "parameters": self.parameters_schema,
67            }
68        });
69        spec
70    }
71}
72
73impl<F> From<(String, String, Value, F)> for StaticTool
74where
75    F: Fn(&mut crate::tools::ToolContext, &crate::types::ToolArguments) -> ToolExecutionResult
76        + Send
77        + Sync
78        + 'static,
79{
80    fn from(value: (String, String, Value, F)) -> Self {
81        Self::new(value.0, value.1, value.2, std::sync::Arc::new(value.3))
82    }
83}