Skip to main content

llm/
tools.rs

1use serde::{Deserialize, Serialize};
2
3#[doc = include_str!("docs/tools.md")]
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
5pub struct ToolDefinition {
6    pub name: String,
7    pub description: String,
8    pub parameters: String,
9    pub server: Option<String>,
10    #[serde(default, skip_serializing_if = "Option::is_none")]
11    pub annotations: Option<ToolAnnotations>,
12}
13
14#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16pub struct ToolAnnotations {
17    pub title: Option<String>,
18    pub read_only_hint: Option<bool>,
19    pub destructive_hint: Option<bool>,
20    pub idempotent_hint: Option<bool>,
21    pub open_world_hint: Option<bool>,
22}
23
24impl ToolDefinition {
25    pub fn new(name: impl Into<String>, description: impl Into<String>, parameters: impl Into<String>) -> Self {
26        Self {
27            name: name.into(),
28            description: description.into(),
29            parameters: parameters.into(),
30            server: None,
31            annotations: None,
32        }
33    }
34
35    pub fn with_server(mut self, server: impl Into<String>) -> Self {
36        self.server = Some(server.into());
37        self
38    }
39
40    pub fn with_annotations(mut self, annotations: impl Into<Option<ToolAnnotations>>) -> Self {
41        self.annotations = annotations.into();
42        self
43    }
44}
45
46impl ToolAnnotations {
47    pub fn read_only() -> Self {
48        Self { read_only_hint: Some(true), ..Self::default() }
49    }
50}
51
52/// Tool call request from the LLM
53#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
54pub struct ToolCallRequest {
55    pub id: String,
56    pub name: String,
57    pub arguments: String,
58}
59
60/// Successful result of a tool call
61#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
62pub struct ToolCallResult {
63    pub id: String,
64    pub name: String,
65    pub arguments: String,
66    pub result: String,
67}
68
69/// Error result of a tool call
70#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
71pub struct ToolCallError {
72    pub id: String,
73    pub name: String,
74    pub arguments: Option<String>,
75    pub error: String,
76}
77
78impl ToolCallError {
79    pub fn from_request(request: &ToolCallRequest, error: impl Into<String>) -> Self {
80        Self {
81            id: request.id.clone(),
82            name: request.name.clone(),
83            arguments: Some(request.arguments.clone()),
84            error: error.into(),
85        }
86    }
87}