Skip to main content

rskit_tool/
result.rs

1//! Tool execution result types.
2
3use rskit_ai::ToolResultBlock;
4use serde::{Deserialize, Serialize};
5
6use crate::io::{ToolMetadata, ToolOutput};
7
8/// The outcome of executing a tool.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct ToolResult {
11    /// Structured JSON output (for programmatic consumption).
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub output: Option<ToolOutput>,
14    /// Human-readable text content.
15    #[serde(default)]
16    pub content: String,
17    /// Whether the tool execution failed.
18    #[serde(default)]
19    pub is_error: bool,
20    /// Arbitrary metadata attached to the result.
21    #[serde(skip_serializing_if = "ToolMetadata::is_empty", default)]
22    pub metadata: ToolMetadata,
23}
24
25impl ToolResult {
26    /// Returns the text content of this result.
27    pub fn text(&self) -> &str {
28        &self.content
29    }
30
31    /// Build the `GenAI` tool-result block for sending to the model.
32    pub fn to_block(&self, id: &str) -> ToolResultBlock {
33        ToolResultBlock {
34            id: id.to_string(),
35            content: self.content.clone(),
36            is_error: self.is_error,
37        }
38    }
39
40    /// Insert a metadata key-value pair.
41    pub fn set_meta(&mut self, key: &str, value: ToolOutput) {
42        self.metadata.insert(key.to_string(), value);
43    }
44}
45
46/// Create a success result with text content.
47pub fn text_result(content: &str) -> ToolResult {
48    ToolResult {
49        output: None,
50        content: content.to_string(),
51        is_error: false,
52        metadata: ToolMetadata::new(),
53    }
54}
55
56/// Create an error result with text content.
57pub fn error_result(content: &str) -> ToolResult {
58    ToolResult {
59        output: None,
60        content: content.to_string(),
61        is_error: true,
62        metadata: ToolMetadata::new(),
63    }
64}
65
66/// Create a success result by serializing a value to JSON.
67pub fn json_result<T: Serialize>(value: &T) -> std::result::Result<ToolResult, serde_json::Error> {
68    let content = serde_json::to_string(value)?;
69    let json = serde_json::to_value(value)?;
70    Ok(ToolResult {
71        output: Some(ToolOutput::from(json)),
72        content,
73        is_error: false,
74        metadata: ToolMetadata::new(),
75    })
76}