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}
18
19impl Tool {
20    pub fn function<S: Into<String>>(name: S) -> Self {
21        Tool::Function {
22            name: name.into(),
23            description: None,
24            parameters: None,
25            strict: None,
26        }
27    }
28
29    pub fn with_description<S: Into<String>>(mut self, desc: S) -> Self {
30        let Tool::Function { description, .. } = &mut self;
31        *description = Some(desc.into());
32        self
33    }
34
35    pub fn with_parameters(mut self, params: serde_json::Value) -> Self {
36        let Tool::Function { parameters, .. } = &mut self;
37        *parameters = Some(params);
38        self
39    }
40
41    pub fn strict(mut self, strict: bool) -> Self {
42        let Tool::Function { strict: s, .. } = &mut self;
43        *s = Some(strict);
44        self
45    }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
49#[serde(untagged)]
50pub enum ToolChoiceParam {
51    Simple(ToolChoice),
52    Specific {
53        #[serde(rename = "type")]
54        tool_type: String,
55        name: String,
56    },
57    Allowed {
58        #[serde(rename = "type")]
59        allowed_type: String,
60        tools: Vec<SpecificTool>,
61        mode: ToolChoice,
62    },
63}
64
65impl Default for ToolChoiceParam {
66    fn default() -> Self {
67        ToolChoiceParam::Simple(ToolChoice::Auto)
68    }
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
72pub struct SpecificTool {
73    #[serde(rename = "type")]
74    pub tool_type: String,
75    pub name: String,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
79pub struct FunctionToolParam {
80    pub name: String,
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub description: Option<String>,
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub parameters: Option<serde_json::Value>,
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub strict: Option<bool>,
87    #[serde(rename = "type")]
88    pub tool_type: String,
89}