1use rskit_ai::ToolResultBlock;
4use serde::{Deserialize, Serialize};
5
6use crate::io::{ToolMetadata, ToolOutput};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct ToolResult {
11 #[serde(skip_serializing_if = "Option::is_none")]
13 pub output: Option<ToolOutput>,
14 #[serde(default)]
16 pub content: String,
17 #[serde(default)]
19 pub is_error: bool,
20 #[serde(skip_serializing_if = "ToolMetadata::is_empty", default)]
22 pub metadata: ToolMetadata,
23}
24
25impl ToolResult {
26 pub fn text(&self) -> &str {
28 &self.content
29 }
30
31 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 pub fn set_meta(&mut self, key: &str, value: ToolOutput) {
42 self.metadata.insert(key.to_string(), value);
43 }
44}
45
46pub 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
56pub 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
66pub 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}