Skip to main content

pi_core/
tool.rs

1use ts_rs::TS;
2use serde::{Deserialize, Serialize};
3
4use crate::types::{JsonSchema, ToolDetails, ToolName};
5
6/// Metadata describing a tool available to the agent.
7/// Hosts implement the actual execution; core only holds the schema.
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
9#[ts(export)]
10pub struct ToolDefinition {
11    pub name: ToolName,
12    pub label: String,
13    pub description: String,
14    #[ts(type = "object")]
15    pub parameters: JsonSchema,
16    #[serde(rename = "execution_mode", default)]
17    pub execution_mode: ExecutionMode,
18}
19
20/// Whether a tool can run concurrently with other tools.
21#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, TS, Default)]
22#[serde(rename_all = "snake_case")]
23pub enum ExecutionMode {
24    #[default]
25    Parallel,
26    Sequential,
27}
28
29/// Controls how multiple tool calls from a single assistant message are handled.
30#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, TS, Default)]
31#[serde(rename_all = "snake_case")]
32pub enum ToolExecutionMode {
33    #[default]
34    Parallel,
35    Sequential,
36}
37
38/// Result of a tool execution, returned by the host via `on_tool_done`.
39#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
40pub struct ToolResult {
41    pub content: Vec<crate::message::Content>,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    #[ts(type = "object | undefined")]
44    pub details: Option<ToolDetails>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub terminate: Option<bool>,
47}
48
49impl ToolResult {
50    pub fn text(text: impl Into<String>) -> Self {
51        Self {
52            content: vec![crate::message::Content::Text(crate::message::TextContent {
53                text: text.into(),
54            })],
55            details: None,
56            terminate: None,
57        }
58    }
59
60    pub fn partial_text(text: impl Into<String>) -> Self {
61        Self::text(text)
62    }
63}
64
65/// Error produced during tool execution.
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS, thiserror::Error)]
67#[error("tool error: {message}")]
68pub struct ToolError {
69    pub code: String,
70    pub message: String,
71    #[serde(skip_serializing_if = "Option::is_none")]
72    #[ts(type = "object | undefined")]
73    pub details: Option<ToolDetails>,
74}
75
76impl ToolError {
77    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
78        Self {
79            code: code.into(),
80            message: message.into(),
81            details: None,
82        }
83    }
84}