appam 0.1.1

High-throughput, traceable, reliable Rust agent framework for long-horizon AI sessions and easy extensibility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Anthropic Messages API streaming event types.
//!
//! Handles Server-Sent Events (SSE) from the Anthropic streaming API.
//!
//! # Event Flow
//!
//! 1. `message_start`: Response begins, empty content
//! 2. `content_block_start`: New content block (text/tool_use/thinking)
//! 3. `content_block_delta`: Incremental updates (text_delta/input_json_delta/thinking_delta)
//! 4. `content_block_stop`: Block finalized
//! 5. `message_delta`: Top-level updates (stop_reason, usage)
//! 6. `message_stop`: Response complete
//!
//! # Special Events
//!
//! - `ping`: Keepalive (no action needed)
//! - `error`: API error (stop processing)
//!
//! # Content Block Types
//!
//! - **text**: Plain text with `text_delta` events
//! - **tool_use**: Tool call with `input_json_delta` events (partial JSON)
//! - **thinking**: Reasoning with `thinking_delta` and `signature_delta` events
//! - **redacted_thinking**: Encrypted thinking (no deltas)

use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

use super::types::{ContentBlock, Usage};

/// Streaming event from Anthropic SSE.
///
/// Each SSE event has a `type` field that determines its structure.
/// Events are sent as:
/// ```text
/// event: message_start
/// data: {"type": "message_start", "message": {...}}
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamEvent {
    /// Message started.
    MessageStart {
        /// Initial message with empty content
        message: MessageStart,
    },

    /// Content block started.
    ContentBlockStart {
        /// Block index in content array
        index: usize,
        /// Content block details
        content_block: ContentBlockStart,
    },

    /// Content block delta.
    ContentBlockDelta {
        /// Block index
        index: usize,
        /// Delta update
        delta: Delta,
    },

    /// Content block stopped.
    ContentBlockStop {
        /// Block index
        index: usize,
    },

    /// Message delta (top-level updates).
    MessageDelta {
        /// Delta updates
        delta: MessageDeltaData,
        /// Usage updates (cumulative)
        usage: Usage,
    },

    /// Message stopped.
    MessageStop,

    /// Ping (keepalive).
    Ping,

    /// Error.
    Error {
        /// Error details
        error: ErrorData,
    },
}

/// Message start data.
#[derive(Debug, Clone, Deserialize)]
pub struct MessageStart {
    /// Unique identifier for the message
    pub id: String,
    /// Object type discriminator
    #[serde(rename = "type")]
    pub object_type: String,
    /// Role of the message sender (e.g., "assistant")
    pub role: String,
    /// Initial content blocks (may be empty at start)
    pub content: Vec<JsonValue>,
    /// Model used for generating the response
    pub model: String,
    /// Reason the model stopped generating (if known at start)
    pub stop_reason: Option<String>,
    /// Stop sequence that triggered completion (if any)
    pub stop_sequence: Option<String>,
    /// Token usage statistics
    pub usage: Usage,
}

/// Content block start data.
///
/// Indicates the type of block that's starting (text, tool_use, thinking).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlockStart {
    /// Text block starting.
    Text {
        /// Initial text (empty)
        text: String,
    },

    /// Tool use block starting.
    ToolUse {
        /// Tool call ID
        id: String,
        /// Tool name
        name: String,
        /// Initial input (empty object)
        input: JsonValue,
    },

    /// Thinking block starting.
    Thinking {
        /// Initial thinking (empty)
        thinking: String,
    },
}

/// Delta update for content blocks.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Delta {
    /// Text content delta.
    TextDelta {
        /// Text chunk
        text: String,
    },

    /// Tool input JSON delta.
    ///
    /// Provides PARTIAL JSON string. Must accumulate until `content_block_stop`
    /// and then parse as complete JSON.
    InputJsonDelta {
        /// Partial JSON string
        partial_json: String,
    },

    /// Thinking content delta.
    ThinkingDelta {
        /// Thinking chunk
        thinking: String,
    },

    /// Signature delta (for thinking blocks).
    ///
    /// Sent just before `content_block_stop` for thinking blocks.
    /// Used to verify thinking authenticity.
    SignatureDelta {
        /// Cryptographic signature
        signature: String,
    },
}

/// Message-level delta.
#[derive(Debug, Clone, Deserialize)]
pub struct MessageDeltaData {
    /// Stop reason (when final)
    pub stop_reason: Option<String>,
    /// Stop sequence (if applicable)
    pub stop_sequence: Option<String>,
}

/// Error data in error events.
#[derive(Debug, Clone, Deserialize)]
pub struct ErrorData {
    /// Error type
    #[serde(rename = "type")]
    pub error_type: String,
    /// Error message
    pub message: String,
}

impl ErrorData {
    /// Check if this error is retryable.
    ///
    /// Returns true for transient errors that may succeed on retry:
    /// - `rate_limit_error` (429)
    /// - `overloaded_error` (529)
    /// - `api_error` (500) - Internal server errors
    ///
    /// All other errors are considered non-retryable.
    ///
    /// Note: HTTP status codes 502, 503, and 504 are also retried even if they
    /// don't have a structured error response. See `AnthropicClient::is_status_code_retryable`.
    pub fn is_retryable(&self) -> bool {
        matches!(
            self.error_type.as_str(),
            "rate_limit_error" | "overloaded_error" | "api_error"
        )
    }
}

/// Accumulated content block during streaming.
///
/// Used to track the state of a content block as deltas arrive.
#[derive(Debug, Clone, Default)]
pub struct AccumulatedBlock {
    /// Block type
    pub block_type: Option<String>,
    /// Accumulated text (for text blocks)
    pub text: String,
    /// Tool call ID (for tool_use blocks)
    pub tool_id: Option<String>,
    /// Tool name (for tool_use blocks)
    pub tool_name: Option<String>,
    /// Accumulated partial JSON (for tool_use blocks)
    pub partial_json: String,
    /// Initial tool input provided at block start (for parameterless tools)
    pub initial_input: Option<JsonValue>,
    /// Accumulated thinking (for thinking blocks)
    pub thinking: String,
    /// Signature (for thinking blocks)
    pub signature: Option<String>,
}

impl AccumulatedBlock {
    /// Create a new block from content_block_start event.
    pub fn from_start(start: &ContentBlockStart) -> Self {
        let mut block = Self::default();

        match start {
            ContentBlockStart::Text { .. } => {
                block.block_type = Some("text".to_string());
            }
            ContentBlockStart::ToolUse { id, name, input } => {
                block.block_type = Some("tool_use".to_string());
                block.tool_id = Some(id.clone());
                block.tool_name = Some(name.clone());
                if !input.is_null() {
                    block.initial_input = Some(input.clone());
                }
            }
            ContentBlockStart::Thinking { .. } => {
                block.block_type = Some("thinking".to_string());
            }
        }

        block
    }

    /// Apply a delta update to this block.
    pub fn apply_delta(&mut self, delta: &Delta) {
        match delta {
            Delta::TextDelta { text } => {
                self.text.push_str(text);
            }
            Delta::InputJsonDelta { partial_json } => {
                self.partial_json.push_str(partial_json);
            }
            Delta::ThinkingDelta { thinking } => {
                self.thinking.push_str(thinking);
            }
            Delta::SignatureDelta { signature } => {
                self.signature = Some(signature.clone());
            }
        }
    }

    /// Convert to a finalized ContentBlock.
    ///
    /// # Errors
    ///
    /// Returns an error if the accumulated data is incomplete or invalid
    /// (e.g., tool_use block with unparseable JSON).
    pub fn to_content_block(&self) -> Result<ContentBlock, serde_json::Error> {
        match self.block_type.as_deref() {
            Some("text") => Ok(ContentBlock::Text {
                text: self.text.clone(),
                cache_control: None,
            }),
            Some("tool_use") => {
                let input: JsonValue = if self.partial_json.trim().is_empty() {
                    self.initial_input.clone()
                } else {
                    serde_json::from_str(&self.partial_json).ok()
                }
                .or_else(|| self.initial_input.clone())
                .unwrap_or_else(|| JsonValue::Object(Default::default()));
                Ok(ContentBlock::ToolUse {
                    id: self.tool_id.clone().unwrap_or_default(),
                    name: self.tool_name.clone().unwrap_or_default(),
                    input,
                    cache_control: None,
                })
            }
            Some("thinking") => Ok(ContentBlock::Thinking {
                thinking: self.thinking.clone(),
                signature: self.signature.clone().unwrap_or_default(),
            }),
            _ => Ok(ContentBlock::Text {
                text: String::new(),
                cache_control: None,
            }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_accumulated_block_text() {
        let start = ContentBlockStart::Text {
            text: String::new(),
        };
        let mut block = AccumulatedBlock::from_start(&start);

        block.apply_delta(&Delta::TextDelta {
            text: "Hello".to_string(),
        });
        block.apply_delta(&Delta::TextDelta {
            text: " world".to_string(),
        });

        assert_eq!(block.text, "Hello world");
        let content = block.to_content_block().unwrap();
        match content {
            ContentBlock::Text { text, .. } => assert_eq!(text, "Hello world"),
            _ => panic!("Expected text block"),
        }
    }

    #[test]
    fn test_accumulated_block_tool_use() {
        let start = ContentBlockStart::ToolUse {
            id: "call_123".to_string(),
            name: "get_weather".to_string(),
            input: json!({}),
        };
        let mut block = AccumulatedBlock::from_start(&start);

        block.apply_delta(&Delta::InputJsonDelta {
            partial_json: r#"{"location":"#.to_string(),
        });
        block.apply_delta(&Delta::InputJsonDelta {
            partial_json: r#" "Paris"}"#.to_string(),
        });

        let content = block.to_content_block().unwrap();
        match content {
            ContentBlock::ToolUse {
                id, name, input, ..
            } => {
                assert_eq!(id, "call_123");
                assert_eq!(name, "get_weather");
                assert_eq!(input["location"], "Paris");
            }
            _ => panic!("Expected tool_use block"),
        }
    }

    #[test]
    fn test_accumulated_block_tool_use_empty_input() {
        let start = ContentBlockStart::ToolUse {
            id: "call_456".to_string(),
            name: "read_notes".to_string(),
            input: json!({}),
        };
        let block = AccumulatedBlock::from_start(&start);

        let content = block.to_content_block().unwrap();
        match content {
            ContentBlock::ToolUse { input, .. } => {
                assert_eq!(input, json!({}));
            }
            _ => panic!("Expected tool_use block"),
        }
    }

    #[test]
    fn test_accumulated_block_tool_use_truncated_partial_fallback() {
        let start = ContentBlockStart::ToolUse {
            id: "call_789".to_string(),
            name: "read_todo_list".to_string(),
            input: json!({}),
        };
        let mut block = AccumulatedBlock::from_start(&start);

        // Simulate truncated partial JSON that would previously trigger EOF errors.
        block.partial_json = r#"{"note": "unfinished"#.to_string();

        let content = block.to_content_block().unwrap();
        match content {
            ContentBlock::ToolUse { input, .. } => {
                // We should gracefully fall back to the initial empty input.
                assert_eq!(input, json!({}));
            }
            _ => panic!("Expected tool_use block"),
        }
    }

    #[test]
    fn test_accumulated_block_thinking() {
        let start = ContentBlockStart::Thinking {
            thinking: String::new(),
        };
        let mut block = AccumulatedBlock::from_start(&start);

        block.apply_delta(&Delta::ThinkingDelta {
            thinking: "Let me think...".to_string(),
        });
        block.apply_delta(&Delta::SignatureDelta {
            signature: "sig123".to_string(),
        });

        let content = block.to_content_block().unwrap();
        match content {
            ContentBlock::Thinking {
                thinking,
                signature,
            } => {
                assert_eq!(thinking, "Let me think...");
                assert_eq!(signature, "sig123");
            }
            _ => panic!("Expected thinking block"),
        }
    }
}