Skip to main content

crabtalk_core/model/
tool.rs

1//! Tool abstractions for the unified LLM Interfaces
2
3use schemars::Schema;
4use serde::{Deserialize, Serialize};
5
6/// A tool for the LLM
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct Tool {
9    /// The name of the tool
10    pub name: String,
11
12    /// The description of the tool
13    pub description: String,
14
15    /// The parameters of the tool
16    pub parameters: Schema,
17
18    /// Whether to strictly validate the parameters
19    pub strict: bool,
20}
21
22/// A tool call made by the model
23#[derive(Debug, Clone, Default, Deserialize, Serialize)]
24pub struct ToolCall {
25    /// The ID of the tool call
26    #[serde(default, skip_serializing_if = "String::is_empty")]
27    pub id: String,
28
29    /// The index of the tool call (used in streaming)
30    #[serde(default, skip_serializing)]
31    pub index: u32,
32
33    /// The type of tool (currently only "function")
34    #[serde(default, rename = "type")]
35    pub call_type: String,
36
37    /// The function to call
38    pub function: FunctionCall,
39}
40
41impl ToolCall {
42    /// Merge two tool calls into one
43    pub fn merge(&mut self, call: &Self) {
44        if !call.id.is_empty() {
45            self.id = call.id.clone();
46        }
47        if !call.call_type.is_empty() {
48            self.call_type = call.call_type.clone();
49        }
50        if !call.function.name.is_empty() {
51            self.function.name = call.function.name.clone();
52        }
53        self.function.arguments.push_str(&call.function.arguments);
54    }
55}
56
57/// A function call within a tool call
58#[derive(Debug, Clone, Default, Deserialize, Serialize)]
59pub struct FunctionCall {
60    /// The name of the function to call
61    #[serde(default, skip_serializing_if = "String::is_empty")]
62    pub name: String,
63
64    /// The arguments to pass to the function (JSON string)
65    #[serde(skip_serializing_if = "String::is_empty")]
66    pub arguments: String,
67}
68
69pub use crabllm_core::ToolChoice;