1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct Tool {
9 pub name: String,
11 #[serde(default)]
13 pub description: String,
14 pub input_schema: Value,
16}
17
18impl Tool {
19 pub fn new(
21 name: impl Into<String>,
22 description: impl Into<String>,
23 input_schema: Value,
24 ) -> Self {
25 Self { name: name.into(), description: description.into(), input_schema }
26 }
27
28 pub fn from_schema<T: ToolSchema>() -> Self {
31 Self {
32 name: T::tool_name().to_string(),
33 description: T::tool_description().to_string(),
34 input_schema: T::input_schema(),
35 }
36 }
37}
38
39#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
41#[serde(rename_all = "snake_case")]
42pub enum ToolChoice {
43 #[default]
45 Auto,
46 Any,
48 None,
50 Tool(String),
52}
53
54#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56pub struct ToolCall {
57 pub id: String,
59 pub name: String,
61 pub input: Value,
63}
64
65impl ToolCall {
66 pub fn new(id: impl Into<String>, name: impl Into<String>, input: Value) -> Self {
68 Self { id: id.into(), name: name.into(), input }
69 }
70}
71
72#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
74pub struct ToolResult {
75 pub tool_use_id: String,
77 pub content: String,
79 #[serde(default)]
81 pub is_error: bool,
82}
83
84impl ToolResult {
85 pub fn ok(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
87 Self { tool_use_id: tool_use_id.into(), content: content.into(), is_error: false }
88 }
89
90 pub fn error(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
92 Self { tool_use_id: tool_use_id.into(), content: content.into(), is_error: true }
93 }
94}
95
96pub trait ToolSchema {
100 fn tool_name() -> &'static str;
102 fn tool_description() -> &'static str;
104 fn input_schema() -> Value;
106}