Skip to main content

troupekit_core/
tool.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4/// Name of the hosted web search tool, on the wire and in tool registries.
5/// Cost meters count its hosted calls.
6/// `HostedTool::WebSearch` returns it from `name`.
7pub const WEB_SEARCH: &str = "web_search";
8
9/// A server-side tool a vendor can run itself instead of the local tool
10/// of the same name. `max_uses` caps calls per agent run; fetch also caps
11/// the page tokens it reads. Serializes with a `kind` tag in snake case.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(tag = "kind", rename_all = "snake_case")]
14pub enum HostedTool {
15    WebSearch { max_uses: u32 },
16    WebFetch { max_uses: u32, max_content_tokens: u32 },
17}
18
19impl HostedTool {
20    /// The tool name vendors use for this server-side tool and the local
21    /// tool that stands in for it must carry: `web_search` or `web_fetch`.
22    /// Returns the name.
23    pub fn name(&self) -> &'static str {
24        match self {
25            HostedTool::WebSearch { .. } => WEB_SEARCH,
26            HostedTool::WebFetch { .. } => "web_fetch",
27        }
28    }
29
30    /// Returns the calls per run this tool allows.
31    /// The agent sends what is left and drops the tool at zero.
32    /// Same for every kind.
33    pub fn max_uses(&self) -> u32 {
34        match self {
35            HostedTool::WebSearch { max_uses } | HostedTool::WebFetch { max_uses, .. } => *max_uses,
36        }
37    }
38
39    /// Returns a copy allowing `uses` calls, for the calls left in a run.
40    /// Every other setting stays.
41    /// Pair it with `max_uses`.
42    pub fn with_max_uses(self, uses: u32) -> Self {
43        match self {
44            HostedTool::WebSearch { .. } => HostedTool::WebSearch { max_uses: uses },
45            HostedTool::WebFetch { max_content_tokens, .. } => HostedTool::WebFetch { max_uses: uses, max_content_tokens },
46        }
47    }
48}
49
50/// A tool as a model sees it: its name, what it does and a JSON Schema
51/// for its arguments. `hosted` marks a tool a vendor may run server-side;
52/// wires without that send it as a plain tool and the local one runs.
53#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
54pub struct ToolSpec {
55    pub name: String,
56    pub description: String,
57    pub schema: Value,
58    #[serde(default, skip_serializing_if = "Option::is_none")]
59    pub hosted: Option<HostedTool>,
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use serde_json::json;
66
67    #[test]
68    fn round_trips_as_plain_fields() {
69        let t = ToolSpec { name: "search".into(), description: "Web search".into(), schema: json!({"type":"object"}), hosted: None };
70        let v = serde_json::to_value(&t).unwrap();
71        assert_eq!(v, json!({"name":"search","description":"Web search","schema":{"type":"object"}}));
72        assert_eq!(serde_json::from_value::<ToolSpec>(v).unwrap(), t);
73    }
74
75    #[test]
76    fn hosted_tools_know_their_names_and_uses() {
77        let s = HostedTool::WebSearch { max_uses: 5 };
78        assert_eq!((s.name(), s.max_uses()), ("web_search", 5));
79        let f = HostedTool::WebFetch { max_uses: 5, max_content_tokens: 8000 };
80        assert_eq!(f.with_max_uses(2), HostedTool::WebFetch { max_uses: 2, max_content_tokens: 8000 });
81        assert_eq!(f.name(), "web_fetch");
82    }
83}