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)]
93pub struct ToolSpec {
94 pub name: &'static str,
96 pub description: &'static str,
98
99 #[builder(default)]
100 pub parameters: Vec<ParamSpec>,
102}
103
104impl ToolSpec {
105 pub fn builder() -> ToolSpecBuilder {
106 ToolSpecBuilder::default()
107 }
108}
109
110#[derive(Clone, Debug, Hash, Eq, PartialEq, Default, strum_macros::AsRefStr)]
111#[strum(serialize_all = "camelCase")]
112pub enum ParamType {
113 #[default]
114 String,
115 Number,
116 Boolean,
117 Array,
118 }
122
123#[derive(Clone, Debug, Hash, Eq, PartialEq, Builder)]
125pub struct ParamSpec {
126 pub name: &'static str,
128 pub description: &'static str,
130 #[builder(default)]
132 pub ty: ParamType,
133 #[builder(default = true)]
137 pub required: bool,
138}
139
140impl ParamSpec {
141 pub fn builder() -> ParamSpecBuilder {
142 ParamSpecBuilder::default()
143 }
144}