tool_useful/
types.rs

1//! Common types used throughout the framework.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6/// A tool call from an LLM
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct ToolCall {
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub id: Option<String>,
11    pub name: String,
12    pub arguments: Value,
13}
14
15impl ToolCall {
16    pub fn new(name: impl Into<String>, arguments: Value) -> Self {
17        Self {
18            id: None,
19            name: name.into(),
20            arguments,
21        }
22    }
23
24    pub fn with_id(id: impl Into<String>, name: impl Into<String>, arguments: Value) -> Self {
25        Self {
26            id: Some(id.into()),
27            name: name.into(),
28            arguments,
29        }
30    }
31}
32
33/// Output from a tool execution
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct ToolOutput {
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub tool_call_id: Option<String>,
38    pub tool_name: String,
39    pub content: Value,
40    pub success: bool,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub error: Option<String>,
43}
44
45impl ToolOutput {
46    pub fn success(tool_name: impl Into<String>, content: Value) -> Self {
47        Self {
48            tool_call_id: None,
49            tool_name: tool_name.into(),
50            content,
51            success: true,
52            error: None,
53        }
54    }
55
56    pub fn failure(tool_name: impl Into<String>, error: impl Into<String>) -> Self {
57        Self {
58            tool_call_id: None,
59            tool_name: tool_name.into(),
60            content: Value::Null,
61            success: false,
62            error: Some(error.into()),
63        }
64    }
65}