Skip to main content

openresponses_rust/types/
tools.rs

1use super::enums::ToolChoice;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5#[serde(tag = "type")]
6pub enum Tool {
7    #[serde(rename = "function")]
8    Function {
9        name: String,
10        #[serde(skip_serializing_if = "Option::is_none")]
11        description: Option<String>,
12        #[serde(skip_serializing_if = "Option::is_none")]
13        parameters: Option<serde_json::Value>,
14        #[serde(skip_serializing_if = "Option::is_none")]
15        strict: Option<bool>,
16    },
17    #[serde(rename = "mcp")]
18    Mcp {
19        server_label: String,
20        server_url: String,
21        #[serde(skip_serializing_if = "Option::is_none")]
22        allowed_tools: Option<Vec<String>>,
23    },
24}
25
26impl Tool {
27    pub fn function<S: Into<String>>(name: S) -> Self {
28        Tool::Function {
29            name: name.into(),
30            description: None,
31            parameters: None,
32            strict: None,
33        }
34    }
35
36    pub fn mcp<S1: Into<String>, S2: Into<String>>(label: S1, url: S2) -> Self {
37        Tool::Mcp {
38            server_label: label.into(),
39            server_url: url.into(),
40            allowed_tools: None,
41        }
42    }
43
44    pub fn with_allowed_tools(mut self, tools: Vec<String>) -> Self {
45        if let Tool::Mcp { allowed_tools, .. } = &mut self {
46            *allowed_tools = Some(tools);
47        }
48        self
49    }
50
51    pub fn with_description<S: Into<String>>(mut self, desc: S) -> Self {
52        if let Tool::Function { description, .. } = &mut self {
53            *description = Some(desc.into());
54        }
55        self
56    }
57
58    pub fn with_parameters(mut self, params: serde_json::Value) -> Self {
59        if let Tool::Function { parameters, .. } = &mut self {
60            *parameters = Some(params);
61        }
62        self
63    }
64
65    pub fn strict(mut self, strict: bool) -> Self {
66        if let Tool::Function { strict: s, .. } = &mut self {
67            *s = Some(strict);
68        }
69        self
70    }
71}
72
73#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
74#[serde(untagged)]
75pub enum ToolChoiceParam {
76    Simple(ToolChoice),
77    Specific {
78        #[serde(rename = "type")]
79        tool_type: String,
80        name: String,
81    },
82    Allowed {
83        #[serde(rename = "type")]
84        allowed_type: String,
85        tools: Vec<SpecificTool>,
86        mode: ToolChoice,
87    },
88}
89
90impl Default for ToolChoiceParam {
91    fn default() -> Self {
92        ToolChoiceParam::Simple(ToolChoice::Auto)
93    }
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
97pub struct SpecificTool {
98    #[serde(rename = "type")]
99    pub tool_type: String,
100    pub name: String,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
104pub struct FunctionToolParam {
105    pub name: String,
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub description: Option<String>,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub parameters: Option<serde_json::Value>,
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub strict: Option<bool>,
112    #[serde(rename = "type")]
113    pub tool_type: String,
114}