swiftide_core/chat_completion/
tools.rs1use derive_builder::Builder;
2
3#[derive(Debug, Clone, PartialEq)]
5#[non_exhaustive]
6pub enum ToolOutput {
7 Text(String),
9
10 Fail(String),
12 Stop,
14}
15
16impl ToolOutput {
17 pub fn content(&self) -> Option<&str> {
18 match self {
19 ToolOutput::Fail(s) | ToolOutput::Text(s) => Some(s),
20 _ => None,
21 }
22 }
23}
24
25impl<T: AsRef<str>> From<T> for ToolOutput {
26 fn from(s: T) -> Self {
27 ToolOutput::Text(s.as_ref().to_string())
28 }
29}
30
31impl std::fmt::Display for ToolOutput {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 ToolOutput::Text(value) => write!(f, "{value}"),
35 ToolOutput::Fail(value) => write!(f, "Tool call failed: {value}"),
36 ToolOutput::Stop => write!(f, "Stop"),
37 }
38 }
39}
40
41#[derive(Clone, Debug, Builder, PartialEq)]
43#[builder(setter(into, strip_option))]
44pub struct ToolCall {
45 id: String,
46 name: String,
47 #[builder(default)]
48 args: Option<String>,
49}
50
51impl std::hash::Hash for &ToolCall {
53 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
54 self.name.hash(state);
55 self.args.hash(state);
56 }
57}
58
59impl std::fmt::Display for ToolCall {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(
62 f,
63 "{id}#{name} {args}",
64 id = self.id,
65 name = self.name,
66 args = self.args.as_deref().unwrap_or("")
67 )
68 }
69}
70
71impl ToolCall {
72 pub fn builder() -> ToolCallBuilder {
73 ToolCallBuilder::default()
74 }
75
76 pub fn id(&self) -> &str {
77 &self.id
78 }
79
80 pub fn name(&self) -> &str {
81 &self.name
82 }
83
84 pub fn args(&self) -> Option<&str> {
85 self.args.as_deref()
86 }
87}
88
89#[derive(Clone, Debug, Hash, Eq, PartialEq, Default, Builder)]
93#[builder(setter(into))]
94pub struct ToolSpec {
95 pub name: String,
97 pub description: String,
99
100 #[builder(default)]
101 pub parameters: Vec<ParamSpec>,
103}
104
105impl ToolSpec {
106 pub fn builder() -> ToolSpecBuilder {
107 ToolSpecBuilder::default()
108 }
109}
110
111#[derive(Clone, Debug, Hash, Eq, PartialEq, Default, strum_macros::AsRefStr)]
112#[strum(serialize_all = "camelCase")]
113pub enum ParamType {
114 #[default]
115 String,
116 Number,
117 Boolean,
118 Array,
119 }
123
124#[derive(Clone, Debug, Hash, Eq, PartialEq, Builder)]
126#[builder(setter(into))]
127pub struct ParamSpec {
128 pub name: String,
130 pub description: String,
132 #[builder(default)]
134 pub ty: ParamType,
135 #[builder(default = true)]
139 pub required: bool,
140}
141
142impl ParamSpec {
143 pub fn builder() -> ParamSpecBuilder {
144 ParamSpecBuilder::default()
145 }
146}