1use super::*;
4
5#[derive(Debug, Clone, Default, PartialEq)]
8#[non_exhaustive]
9pub struct ProgressUpdate {
10 pub message: String,
11 pub progress: Option<f64>,
12 pub total: Option<f64>,
13}
14
15impl ProgressUpdate {
16 pub fn text(message: impl Into<String>) -> Self {
17 Self {
18 message: message.into(),
19 ..Self::default()
20 }
21 }
22 pub fn with_progress(mut self, progress: f64) -> Self {
23 self.progress = Some(progress);
24 self
25 }
26 pub fn with_total(mut self, total: f64) -> Self {
27 self.total = Some(total);
28 self
29 }
30}
31
32#[derive(Clone)]
36pub struct ProgressSender {
37 tool_use_id: String,
38 callback: Option<Arc<dyn ProgressCallback>>,
39}
40impl std::fmt::Debug for ProgressSender {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 f.debug_struct("ProgressSender")
43 .field("tool_use_id", &self.tool_use_id)
44 .finish_non_exhaustive()
45 }
46}
47pub trait ProgressCallback: Send + Sync {
48 fn on_progress(&self, tool_use_id: &str, data: &str);
49 fn on_update(&self, tool_use_id: &str, update: ProgressUpdate) {
50 self.on_progress(tool_use_id, &update.message);
51 }
52}
53impl ProgressSender {
54 pub fn noop(id: impl Into<String>) -> Self {
55 Self {
56 tool_use_id: id.into(),
57 callback: None,
58 }
59 }
60 pub fn new(id: impl Into<String>) -> Self {
61 Self {
62 tool_use_id: id.into(),
63 callback: None,
64 }
65 }
66 pub fn with_callback(id: impl Into<String>, cb: Arc<dyn ProgressCallback>) -> Self {
67 Self {
68 tool_use_id: id.into(),
69 callback: Some(cb),
70 }
71 }
72 pub fn send(&self, data: &str) {
73 if let Some(ref cb) = self.callback {
74 cb.on_progress(&self.tool_use_id, data);
75 }
76 }
77 pub fn update(&self, update: ProgressUpdate) {
78 if let Some(ref cb) = self.callback {
79 cb.on_update(&self.tool_use_id, update);
80 }
81 }
82 pub fn tool_use_id(&self) -> &str {
83 &self.tool_use_id
84 }
85}
86
87#[derive(Debug, Clone)]
88pub enum ValidationResult {
89 Ok,
90 Err(String, i32),
91}
92impl ValidationResult {
93 pub fn is_ok(&self) -> bool {
94 matches!(self, Self::Ok)
95 }
96 pub fn is_err(&self) -> bool {
97 matches!(self, Self::Err(..))
98 }
99 pub fn err(m: impl Into<String>, c: i32) -> Self {
100 Self::Err(m.into(), c)
101 }
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize, Default)]
105#[non_exhaustive]
106pub struct ToolResult {
107 pub content: ToolResultContent,
108 pub is_error: bool,
109 #[serde(skip_serializing_if = "Option::is_none")]
110 pub structured_content: Option<Value>,
111 #[serde(skip_serializing_if = "Option::is_none")]
112 pub mcp_meta: Option<McpMeta>,
113 #[serde(skip_serializing_if = "Option::is_none")]
114 pub new_messages: Option<Vec<Value>>,
115 #[serde(default, skip_serializing_if = "Vec::is_empty")]
117 pub file_changes: Vec<FileChange>,
118 #[serde(default, skip_serializing_if = "Option::is_none")]
121 pub ends_turn: Option<Option<String>>,
122}
123
124#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
126#[non_exhaustive]
127pub struct FileChange {
128 pub path: String,
130 pub op: FileOp,
131 pub before_hash: Option<String>,
134 pub after_hash: Option<String>,
135 pub before_bytes: Option<u64>,
136 pub after_bytes: u64,
137 pub added: u64,
138 pub removed: u64,
139 pub unified_diff: Option<String>,
141 pub truncated: bool,
143 pub omitted: Option<DiffOmitted>,
145}
146
147#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
148#[serde(rename_all = "snake_case")]
149#[non_exhaustive]
150pub enum FileOp {
151 Create,
152 Modify,
153}
154
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
156#[serde(rename_all = "snake_case")]
157#[non_exhaustive]
158pub enum DiffOmitted {
159 TooLarge,
160 Binary,
161 Redacted,
164}
165
166impl FileChange {
167 pub fn new(path: impl Into<String>, op: FileOp, after_bytes: u64) -> Self {
168 Self {
169 path: path.into(),
170 op,
171 before_hash: None,
172 after_hash: None,
173 before_bytes: None,
174 after_bytes,
175 added: 0,
176 removed: 0,
177 unified_diff: None,
178 truncated: false,
179 omitted: None,
180 }
181 }
182
183 pub fn without_content(mut self) -> Self {
185 if self.unified_diff.take().is_some() || self.omitted.is_none() {
186 self.omitted = Some(DiffOmitted::Redacted);
187 }
188 self.truncated = false;
189 self
190 }
191}
192#[derive(Debug, Clone, Serialize, Deserialize)]
193pub struct McpMeta {
194 #[serde(default)]
195 pub server_name: String,
196}
197#[derive(Debug, Clone, Serialize, Deserialize)]
198#[serde(untagged)]
199pub enum ToolResultContent {
200 Text(String),
201 Blocks(Vec<ToolResultBlock>),
202}
203impl Default for ToolResultContent {
204 fn default() -> Self {
205 Self::Text(String::new())
206 }
207}
208#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct ToolResultBlock {
210 #[serde(rename = "type")]
211 pub block_type: String,
212 pub text: Option<String>,
213 pub source: Option<Value>,
214}
215
216pub const MAX_IMAGE_BYTES: u64 = 3_500_000;
221
222impl ToolResultBlock {
230 pub fn text(s: impl Into<String>) -> Self {
231 Self {
232 block_type: "text".into(),
233 text: Some(s.into()),
234 source: None,
235 }
236 }
237 pub fn image(media_type: impl Into<String>, data: impl Into<String>) -> Self {
238 Self {
239 block_type: "image".into(),
240 text: None,
241 source: Some(serde_json::json!({
242 "type": "base64",
243 "media_type": media_type.into(),
244 "data": data.into(),
245 })),
246 }
247 }
248 pub fn as_image(&self) -> Option<(&str, &str)> {
250 if self.block_type != "image" {
251 return None;
252 }
253 let source = self.source.as_ref()?;
254 Some((
255 source.get("media_type")?.as_str()?,
256 source.get("data")?.as_str()?,
257 ))
258 }
259}
260
261impl ToolResult {
262 pub fn text(s: impl Into<String>) -> Self {
263 Self {
264 content: ToolResultContent::Text(s.into()),
265 is_error: false,
266 structured_content: None,
267 mcp_meta: None,
268 new_messages: None,
269 file_changes: Vec::new(),
270 ends_turn: None,
271 }
272 }
273 pub fn error_text(s: impl Into<String>) -> Self {
274 Self {
275 content: ToolResultContent::Text(s.into()),
276 is_error: true,
277 structured_content: None,
278 mcp_meta: None,
279 new_messages: None,
280 file_changes: Vec::new(),
281 ends_turn: None,
282 }
283 }
284 pub fn blocks(blocks: Vec<ToolResultBlock>) -> Self {
285 Self {
286 content: ToolResultContent::Blocks(blocks),
287 ..Self::default()
288 }
289 }
290 pub fn ending_turn(mut self, message: Option<String>) -> Self {
291 self.ends_turn = Some(message);
292 self
293 }
294 pub fn with_error(mut self, is_error: bool) -> Self {
295 self.is_error = is_error;
296 self
297 }
298 pub fn with_structured(mut self, v: Value) -> Self {
299 self.structured_content = Some(v);
300 self
301 }
302 pub fn with_mcp_meta(mut self, m: McpMeta) -> Self {
303 self.mcp_meta = Some(m);
304 self
305 }
306 pub fn with_new_messages(mut self, m: Vec<Value>) -> Self {
307 self.new_messages = Some(m);
308 self
309 }
310 pub fn with_file_change(mut self, change: FileChange) -> Self {
311 self.file_changes.push(change);
312 self
313 }
314}