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
444
445
446
447
448
449
450
451
452
453
//! Streaming support for OpenAI Responses API.
//!
//! Handles Server-Sent Events (SSE) parsing and stream accumulation for
//! incremental response processing.

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

use super::types::*;

/// SSE stream event types from OpenAI Responses API.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamEvent {
    // Response lifecycle events
    /// Response created
    #[serde(rename = "response.created")]
    ResponseCreated {
        /// Response snapshot
        response: Response,
        /// Sequence number
        sequence_number: i32,
    },

    /// Response in progress
    #[serde(rename = "response.in_progress")]
    ResponseInProgress {
        /// Sequence number
        sequence_number: i32,
    },

    /// Response completed
    #[serde(rename = "response.completed")]
    ResponseCompleted {
        /// Final response
        response: Response,
        /// Sequence number
        sequence_number: i32,
    },

    /// Response failed
    #[serde(rename = "response.failed")]
    ResponseFailed {
        /// Error details
        error: ResponseError,
        /// Sequence number
        sequence_number: i32,
    },

    /// Response incomplete
    #[serde(rename = "response.incomplete")]
    ResponseIncomplete {
        /// Sequence number
        sequence_number: i32,
    },

    // Output item events
    /// Output item added
    #[serde(rename = "response.output_item.added")]
    ResponseOutputItemAdded {
        /// Output item
        item: OutputItem,
        /// Output index
        output_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    /// Output item done
    #[serde(rename = "response.output_item.done")]
    ResponseOutputItemDone {
        /// Output index
        output_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    // Content part events
    /// Content part added
    #[serde(rename = "response.content_part.added")]
    ResponseContentPartAdded {
        /// Content part
        part: ContentPart,
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    /// Content part done
    #[serde(rename = "response.content_part.done")]
    ResponseContentPartDone {
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    // Text delta events
    /// Text delta
    #[serde(rename = "response.output_text.delta")]
    ResponseTextDelta {
        /// Text delta
        delta: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
        /// Logprobs
        #[serde(skip_serializing_if = "Option::is_none")]
        logprobs: Option<Vec<Logprob>>,
    },

    /// Text done
    #[serde(rename = "response.output_text.done")]
    ResponseTextDone {
        /// Complete text
        text: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
        /// Annotations
        #[serde(default)]
        annotations: Vec<Annotation>,
        /// Logprobs
        #[serde(skip_serializing_if = "Option::is_none")]
        logprobs: Option<Vec<Logprob>>,
    },

    // Function call events
    /// Function call arguments delta
    #[serde(rename = "response.function_call_arguments.delta")]
    ResponseFunctionCallArgumentsDelta {
        /// Arguments delta
        delta: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    /// Function call arguments done
    #[serde(rename = "response.function_call_arguments.done")]
    ResponseFunctionCallArgumentsDone {
        /// Complete arguments
        arguments: String,
        /// Item ID
        item_id: String,
        /// Call ID
        #[serde(default)]
        call_id: Option<String>,
        /// Function name
        #[serde(default)]
        name: Option<String>,
        /// Output index
        output_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    // Reasoning events
    /// Reasoning summary part added
    #[serde(rename = "response.reasoning_summary_part.added")]
    ResponseReasoningSummaryPartAdded {
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Summary index
        summary_index: i32,
        /// Summary part payload
        part: Value,
        /// Sequence number
        sequence_number: i32,
    },

    /// Reasoning summary part done
    #[serde(rename = "response.reasoning_summary_part.done")]
    ResponseReasoningSummaryPartDone {
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Summary index
        summary_index: i32,
        /// Summary part payload
        part: Value,
        /// Sequence number
        sequence_number: i32,
    },

    /// Reasoning summary text delta
    #[serde(rename = "response.reasoning_summary_text.delta")]
    ResponseReasoningSummaryTextDelta {
        /// Text delta
        delta: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Summary index
        summary_index: i32,
        /// Sequence number
        sequence_number: i32,
        /// Optional obfuscation token
        #[serde(skip_serializing_if = "Option::is_none")]
        obfuscation: Option<String>,
    },

    /// Reasoning summary text done
    #[serde(rename = "response.reasoning_summary_text.done")]
    ResponseReasoningSummaryTextDone {
        /// Full summary text
        text: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Summary index
        summary_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    /// Reasoning text delta
    #[serde(rename = "response.reasoning_text.delta")]
    ResponseReasoningTextDelta {
        /// Reasoning delta
        delta: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    /// Reasoning text done
    #[serde(rename = "response.reasoning_text.done")]
    ResponseReasoningTextDone {
        /// Complete reasoning text
        text: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    // Refusal events
    /// Refusal delta
    #[serde(rename = "response.refusal.delta")]
    ResponseRefusalDelta {
        /// Refusal delta
        delta: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    /// Refusal done
    #[serde(rename = "response.refusal.done")]
    ResponseRefusalDone {
        /// Complete refusal
        refusal: String,
        /// Item ID
        item_id: String,
        /// Output index
        output_index: i32,
        /// Content index
        content_index: i32,
        /// Sequence number
        sequence_number: i32,
    },

    // Error event
    /// Response error
    #[serde(rename = "response.error")]
    ResponseError {
        /// Error details
        error: ResponseError,
        /// Sequence number
        sequence_number: i32,
    },
}

/// Stream accumulator for managing partial streaming state.
///
/// Accumulates deltas and maintains snapshots of the response as it streams.
pub struct StreamAccumulator {
    /// Current response snapshot
    pub response_snapshot: Option<Response>,

    /// Text buffers by (output_index, content_index)
    text_buffers: HashMap<(usize, usize), String>,

    /// Function argument buffers by output_index
    function_arg_buffers: HashMap<usize, String>,

    /// Reasoning buffers by (output_index, content_index)
    reasoning_buffers: HashMap<(usize, usize), String>,
}

impl StreamAccumulator {
    /// Create a new stream accumulator.
    pub fn new() -> Self {
        Self {
            response_snapshot: None,
            text_buffers: HashMap::new(),
            function_arg_buffers: HashMap::new(),
            reasoning_buffers: HashMap::new(),
        }
    }

    /// Handle a stream event and update internal state.
    ///
    /// Returns the accumulated text for the event, if applicable.
    pub fn handle_event(&mut self, event: &StreamEvent) -> Option<String> {
        match event {
            StreamEvent::ResponseCreated { response, .. } => {
                self.response_snapshot = Some(response.clone());
                None
            }
            StreamEvent::ResponseTextDelta {
                delta,
                output_index,
                content_index,
                ..
            } => {
                let key = (*output_index as usize, *content_index as usize);
                let buffer = self.text_buffers.entry(key).or_default();
                buffer.push_str(delta);
                Some(delta.clone())
            }
            StreamEvent::ResponseFunctionCallArgumentsDelta {
                delta,
                output_index,
                ..
            } => {
                let buffer = self
                    .function_arg_buffers
                    .entry(*output_index as usize)
                    .or_default();
                buffer.push_str(delta);
                None
            }
            StreamEvent::ResponseReasoningTextDelta {
                delta,
                output_index,
                content_index,
                ..
            } => {
                let key = (*output_index as usize, *content_index as usize);
                let buffer = self.reasoning_buffers.entry(key).or_default();
                buffer.push_str(delta);
                Some(delta.clone())
            }
            StreamEvent::ResponseCompleted { response, .. } => {
                self.response_snapshot = Some(response.clone());
                None
            }
            _ => None,
        }
    }

    /// Get the final response snapshot.
    pub fn get_final_response(&self) -> Option<&Response> {
        self.response_snapshot.as_ref()
    }

    /// Get accumulated text for a specific output/content index.
    pub fn get_text(&self, output_index: usize, content_index: usize) -> Option<&String> {
        self.text_buffers.get(&(output_index, content_index))
    }

    /// Get accumulated function arguments for a specific output index.
    pub fn get_function_args(&self, output_index: usize) -> Option<&String> {
        self.function_arg_buffers.get(&output_index)
    }

    /// Get accumulated reasoning for a specific output/content index.
    pub fn get_reasoning(&self, output_index: usize, content_index: usize) -> Option<&String> {
        self.reasoning_buffers.get(&(output_index, content_index))
    }
}

impl Default for StreamAccumulator {
    fn default() -> Self {
        Self::new()
    }
}

/// Check if a stream chunk reading error is recoverable.
///
/// Returns true for transient network errors during stream reading:
/// - EOF errors (connection closed unexpectedly)
/// - Connection resets
/// - Incomplete chunk reads
/// - DNS resolution failures
///
/// These errors indicate the connection was interrupted during streaming.
/// When recoverable, we return partial content rather than failing completely.
///
/// # Arguments
///
/// * `error` - The error that occurred while reading a stream chunk
///
/// # Returns
///
/// True if the error is recoverable and partial content should be returned.
pub fn is_chunk_error_recoverable(error: &anyhow::Error) -> bool {
    let error_str = format!("{:#}", error);
    let error_str_lower = error_str.to_lowercase();

    // Check for known recoverable patterns in reqwest/hyper errors
    error_str_lower.contains("unexpected eof")
        || error_str_lower.contains("connection reset")
        || error_str_lower.contains("broken pipe")
        || error_str_lower.contains("connection closed")
        || error_str_lower.contains("incomplete")
        || error_str_lower.contains("chunk size")
        || error_str_lower.contains("dns error")
        || error_str_lower.contains("failed to lookup address")
        || error_str_lower.contains("nodename nor servname provided")
        || error_str_lower.contains("decoding response body")
        || error_str_lower.contains("reading a body from connection")
}