agent-air-runtime 0.7.0

Core runtime for agent-air - LLM orchestration, tools, and permissions (no TUI dependencies)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Server-Sent Events (SSE) parser for Gemini streaming responses.

use crate::client::error::LlmError;
use crate::client::models::{ContentBlockType, StreamEvent, Usage};

// =============================================================================
// Constants
// =============================================================================

/// SSE data line prefix.
const SSE_DATA_PREFIX: &str = "data: ";

/// Error code for SSE parsing errors.
const ERROR_SSE_PARSE: &str = "SSE_PARSE_ERROR";

/// Prefix for Gemini API error codes.
const ERROR_PREFIX_GEMINI: &str = "GEMINI_ERROR_";

/// Default error message when error details are unavailable.
const MSG_UNKNOWN_ERROR: &str = "Unknown error";

/// Gemini finish reason: normal completion.
const FINISH_REASON_STOP: &str = "STOP";

/// Gemini finish reason: max tokens reached.
const FINISH_REASON_MAX_TOKENS: &str = "MAX_TOKENS";

/// Gemini finish reason: safety filter triggered.
const FINISH_REASON_SAFETY: &str = "SAFETY";

/// Gemini finish reason: recitation detected.
const FINISH_REASON_RECITATION: &str = "RECITATION";

/// Internal stop reason: end of turn.
const STOP_REASON_END_TURN: &str = "end_turn";

/// Internal stop reason: max tokens.
const STOP_REASON_MAX_TOKENS: &str = "max_tokens";

/// Internal stop reason: safety.
const STOP_REASON_SAFETY: &str = "safety";

/// Internal stop reason: recitation.
const STOP_REASON_RECITATION: &str = "recitation";

// =============================================================================
// Types
// =============================================================================

/// Parsed SSE event with optional event type and data payload.
///
/// Matches Anthropic's SSE structure for consistency across providers.
#[derive(Debug)]
pub struct SseEvent {
    /// Event type - included for cross-provider consistency even though
    /// Gemini doesn't typically use event types in their SSE stream.
    #[allow(dead_code)]
    pub event: Option<String>,
    /// JSON data payload.
    pub data: String,
}

/// State tracker for streaming to handle indices across chunks.
#[derive(Debug, Default, Clone)]
pub struct StreamState {
    /// Number of content blocks seen so far.
    pub block_count: usize,
    /// Whether we've seen at least one content block.
    pub has_content: bool,
    /// Pending function call being accumulated (for partial args).
    /// Item 15 fix: accumulate function call args across chunks.
    pub pending_function_call: Option<PendingFunctionCall>,
}

/// Tracks a function call being accumulated across streaming chunks.
#[derive(Debug, Clone)]
pub struct PendingFunctionCall {
    /// Index of this content block.
    pub index: usize,
    /// Generated ID for this function call - stored for debugging and potential
    /// future use in tool call correlation.
    #[allow(dead_code)]
    pub id: String,
    /// Function name.
    pub name: String,
    /// Accumulated JSON arguments buffer - written during streaming, available
    /// for debugging partial argument accumulation.
    #[allow(dead_code)]
    pub args_buffer: String,
    /// Tracks whether the ToolUseStart event has been emitted - stored for
    /// debugging streaming state.
    #[allow(dead_code)]
    pub started: bool,
}

// =============================================================================
// Public API
// =============================================================================

/// Parse SSE lines from a buffer, returning events and remaining buffer.
///
/// Gemini streams use SSE format with `data:` lines. Empty lines mark event boundaries.
pub fn parse_sse_chunk(buffer: &str) -> (Vec<SseEvent>, String) {
    let mut events = Vec::new();
    let mut current_event: Option<String> = None;
    let mut current_data: Option<String> = None;

    let lines: Vec<&str> = buffer.split('\n').collect();

    for line in &lines {
        if line.is_empty() {
            // Empty line marks end of event
            if let Some(data) = current_data.take() {
                events.push(SseEvent {
                    event: current_event.take(),
                    data,
                });
            }
            current_event = None;
        } else if let Some(event_type) = line.strip_prefix("event: ") {
            current_event = Some(event_type.to_string());
        } else if let Some(data) = line.strip_prefix(SSE_DATA_PREFIX) {
            current_data = Some(data.to_string());
        }
        // Ignore other lines (comments starting with :, etc.)
    }

    // Build remaining buffer only if there's incomplete data
    let remaining = if current_data.is_some() || current_event.is_some() {
        let mut rem = String::new();
        if let Some(evt) = current_event {
            rem.push_str("event: ");
            rem.push_str(&evt);
            rem.push('\n');
        }
        if let Some(data) = current_data {
            rem.push_str(SSE_DATA_PREFIX);
            rem.push_str(&data);
        }
        rem
    } else {
        String::new()
    };

    (events, remaining)
}

/// Parse a Gemini SSE event into StreamEvents.
///
/// Gemini sends complete candidate objects in each SSE event. This function
/// uses the provided state to track content block indices across events and
/// accumulate partial function call arguments (Item 15 fix).
pub fn parse_stream_event(
    sse: &SseEvent,
    state: &mut StreamState,
) -> Result<Vec<StreamEvent>, LlmError> {
    let json: serde_json::Value = serde_json::from_str(&sse.data)
        .map_err(|e| LlmError::new(ERROR_SSE_PARSE, format!("Invalid JSON: {}", e)))?;

    // Check for error response
    if let Some(error) = json.get("error") {
        let error_code = error["code"].as_i64().unwrap_or(0);
        let error_msg = error["message"].as_str().unwrap_or(MSG_UNKNOWN_ERROR);
        return Err(LlmError::new(
            format!("{}{}", ERROR_PREFIX_GEMINI, error_code),
            error_msg,
        ));
    }

    let mut events = Vec::new();

    // Gemini streaming format:
    // {"candidates":[{"content":{"parts":[...],"role":"model"},...}],"usageMetadata":{...}}
    let candidates = &json["candidates"];

    if let Some(candidates_array) = candidates.as_array()
        && let Some(candidate) = candidates_array.first()
    {
        let content = &candidate["content"];
        let parts = &content["parts"];

        if let Some(parts_array) = parts.as_array() {
            for part in parts_array {
                let index = state.block_count;

                // Text part
                if let Some(text) = part["text"].as_str() {
                    if !text.is_empty() {
                        // Finalize any pending function call first
                        if let Some(pending) = state.pending_function_call.take() {
                            events.extend(finalize_function_call(&pending));
                        }

                        // Only emit start if this is new content
                        if !state.has_content || index >= state.block_count {
                            events.push(StreamEvent::ContentBlockStart {
                                index,
                                block_type: ContentBlockType::Text,
                            });
                        }

                        events.push(StreamEvent::TextDelta {
                            index,
                            text: text.to_string(),
                        });

                        state.has_content = true;
                        state.block_count = index + 1;
                    }
                }
                // Function call part - Item 15 fix: accumulate partial args
                else if let Some(function_call) = part.get("functionCall") {
                    let name = function_call["name"].as_str().unwrap_or("");
                    let args_json = &function_call["args"];

                    // Check if this is a continuation of a pending function call
                    if let Some(ref mut pending) = state.pending_function_call {
                        if pending.name == name {
                            // Accumulate args - try to merge JSON
                            let new_args = args_json.to_string();
                            if is_complete_json(&new_args) {
                                // Complete args received, use them
                                pending.args_buffer = new_args;
                            } else {
                                // Partial args, append to buffer
                                pending.args_buffer.push_str(&new_args);
                            }
                            // Emit delta for the new args
                            events.push(StreamEvent::InputJsonDelta {
                                index: pending.index,
                                json: args_json.to_string(),
                            });
                            continue;
                        } else {
                            // Different function, finalize the previous one
                            let pending_owned = state.pending_function_call.take().unwrap();
                            events.extend(finalize_function_call(&pending_owned));
                        }
                    }

                    // Start a new function call
                    // NOTE: Gemini matches function responses by NAME, not by unique ID.
                    // We use the function name as the ID so tool results flow back correctly.
                    let id = name.to_string();
                    let args_str = args_json.to_string();

                    events.push(StreamEvent::ContentBlockStart {
                        index,
                        block_type: ContentBlockType::ToolUse {
                            id: id.clone(),
                            name: name.to_string(),
                        },
                    });

                    events.push(StreamEvent::InputJsonDelta {
                        index,
                        json: args_str.clone(),
                    });

                    // Check if args are complete
                    if is_complete_json(&args_str) {
                        // Complete function call, emit stop immediately
                        events.push(StreamEvent::ContentBlockStop { index });
                        state.has_content = true;
                        state.block_count = index + 1;
                    } else {
                        // Partial args, store for accumulation
                        state.pending_function_call = Some(PendingFunctionCall {
                            index,
                            id,
                            name: name.to_string(),
                            args_buffer: args_str,
                            started: true,
                        });
                        state.has_content = true;
                        state.block_count = index + 1;
                    }
                }
            }
        }

        // Check for finish reason
        if let Some(finish_reason) = candidate["finishReason"].as_str() {
            // Finalize any pending function call
            if let Some(pending) = state.pending_function_call.take() {
                events.extend(finalize_function_call(&pending));
            }

            // Emit content block stop for text blocks on completion
            if state.has_content && state.block_count > 0 {
                let last_index = state.block_count - 1;
                // Only emit if not already stopped (e.g., not a function call)
                let already_stopped = events.iter().any(|e| {
                        matches!(e, StreamEvent::ContentBlockStop { index } if *index == last_index)
                    });
                if !already_stopped {
                    events.push(StreamEvent::ContentBlockStop { index: last_index });
                }
            }

            let stop_reason = map_finish_reason(finish_reason);

            // Extract usage if available
            let usage = extract_usage(&json);

            events.push(StreamEvent::MessageDelta { stop_reason, usage });
        }
    }

    // Check for usage metadata at the top level (final message)
    if let Some(usage_meta) = json.get("usageMetadata") {
        let has_candidates = json
            .get("candidates")
            .and_then(|c| c.as_array())
            .is_some_and(|a| !a.is_empty());

        if !has_candidates {
            // This is a usage-only message (final stats)
            events.push(StreamEvent::MessageDelta {
                stop_reason: None,
                usage: Some(Usage {
                    input_tokens: usage_meta["promptTokenCount"].as_u64().unwrap_or(0) as u32,
                    output_tokens: usage_meta["candidatesTokenCount"].as_u64().unwrap_or(0) as u32,
                }),
            });
        }
    }

    Ok(events)
}

/// Finalize a pending function call by emitting ContentBlockStop.
fn finalize_function_call(pending: &PendingFunctionCall) -> Vec<StreamEvent> {
    vec![StreamEvent::ContentBlockStop {
        index: pending.index,
    }]
}

/// Check if a JSON string appears to be complete (balanced braces/brackets).
fn is_complete_json(s: &str) -> bool {
    let s = s.trim();
    if s.is_empty() {
        return false;
    }

    // Quick check: try to parse it
    serde_json::from_str::<serde_json::Value>(s).is_ok()
}

// =============================================================================
// Private helpers
// =============================================================================

/// Map Gemini finish reason to internal stop reason.
fn map_finish_reason(finish_reason: &str) -> Option<String> {
    Some(match finish_reason {
        FINISH_REASON_STOP => STOP_REASON_END_TURN.to_string(),
        FINISH_REASON_MAX_TOKENS => STOP_REASON_MAX_TOKENS.to_string(),
        FINISH_REASON_SAFETY => STOP_REASON_SAFETY.to_string(),
        FINISH_REASON_RECITATION => STOP_REASON_RECITATION.to_string(),
        other => other.to_lowercase(),
    })
}

/// Extract usage metadata from JSON response.
fn extract_usage(json: &serde_json::Value) -> Option<Usage> {
    json.get("usageMetadata").map(|usage_meta| Usage {
        input_tokens: usage_meta["promptTokenCount"].as_u64().unwrap_or(0) as u32,
        output_tokens: usage_meta["candidatesTokenCount"].as_u64().unwrap_or(0) as u32,
    })
}

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

    #[test]
    fn test_parse_sse_chunk() {
        let chunk = "data: {\"test\":true}\n\ndata: {\"test\":false}\n\n";
        let (events, remaining) = parse_sse_chunk(chunk);

        assert_eq!(events.len(), 2);
        assert_eq!(events[0].data, "{\"test\":true}");
        assert_eq!(events[1].data, "{\"test\":false}");
        assert!(remaining.is_empty());
    }

    #[test]
    fn test_parse_sse_chunk_with_event_type() {
        let chunk = "event: message\ndata: {\"test\":true}\n\n";
        let (events, remaining) = parse_sse_chunk(chunk);

        assert_eq!(events.len(), 1);
        assert_eq!(events[0].event, Some("message".to_string()));
        assert_eq!(events[0].data, "{\"test\":true}");
        assert!(remaining.is_empty());
    }

    #[test]
    fn test_parse_incomplete_chunk() {
        let chunk = "data: {\"test\":true}";
        let (events, remaining) = parse_sse_chunk(chunk);

        assert_eq!(events.len(), 0);
        assert!(remaining.contains("{\"test\":true}"));
        assert!(remaining.starts_with(SSE_DATA_PREFIX));
    }

    #[test]
    fn test_parse_text_response() {
        let data = r#"{"candidates":[{"content":{"parts":[{"text":"Hello"}],"role":"model"},"finishReason":"STOP"}]}"#;
        let sse = SseEvent {
            event: None,
            data: data.to_string(),
        };

        let mut state = StreamState::default();
        let events = parse_stream_event(&sse, &mut state).unwrap();

        // Should have: ContentBlockStart, TextDelta, ContentBlockStop, MessageDelta
        assert!(events.len() >= 3);

        let has_text_delta = events
            .iter()
            .any(|e| matches!(e, StreamEvent::TextDelta { text, .. } if text == "Hello"));
        assert!(has_text_delta);

        let has_stop = events.iter().any(|e| {
            matches!(e, StreamEvent::MessageDelta { stop_reason: Some(reason), .. } if reason == STOP_REASON_END_TURN)
        });
        assert!(has_stop);
    }

    #[test]
    fn test_parse_function_call_response() {
        let data = r#"{"candidates":[{"content":{"parts":[{"functionCall":{"name":"get_weather","args":{"location":"SF"}}}],"role":"model"},"finishReason":"STOP"}]}"#;
        let sse = SseEvent {
            event: None,
            data: data.to_string(),
        };

        let mut state = StreamState::default();
        let events = parse_stream_event(&sse, &mut state).unwrap();

        let has_tool_use = events.iter().any(|e| {
            matches!(e, StreamEvent::ContentBlockStart { block_type: ContentBlockType::ToolUse { name, .. }, .. } if name == "get_weather")
        });
        assert!(has_tool_use);
    }

    #[test]
    fn test_parse_error_response() {
        let data = r#"{"error":{"code":400,"message":"Invalid request"}}"#;
        let sse = SseEvent {
            event: None,
            data: data.to_string(),
        };

        let mut state = StreamState::default();
        let result = parse_stream_event(&sse, &mut state);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.error_code.starts_with(ERROR_PREFIX_GEMINI));
    }

    #[test]
    fn test_parse_usage_metadata() {
        let data = r#"{"candidates":[{"content":{"parts":[{"text":"Hi"}],"role":"model"},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":5}}"#;
        let sse = SseEvent {
            event: None,
            data: data.to_string(),
        };

        let mut state = StreamState::default();
        let events = parse_stream_event(&sse, &mut state).unwrap();

        let has_usage = events.iter().any(|e| {
            matches!(e, StreamEvent::MessageDelta { usage: Some(u), .. } if u.input_tokens == 10 && u.output_tokens == 5)
        });
        assert!(has_usage);
    }

    #[test]
    fn test_stream_state_tracking() {
        let mut state = StreamState::default();
        assert_eq!(state.block_count, 0);
        assert!(!state.has_content);

        // First chunk
        let data1 = r#"{"candidates":[{"content":{"parts":[{"text":"Hello"}],"role":"model"}}]}"#;
        let sse1 = SseEvent {
            event: None,
            data: data1.to_string(),
        };
        let _ = parse_stream_event(&sse1, &mut state).unwrap();

        assert_eq!(state.block_count, 1);
        assert!(state.has_content);
    }

    #[test]
    fn test_map_finish_reason() {
        assert_eq!(
            map_finish_reason(FINISH_REASON_STOP),
            Some(STOP_REASON_END_TURN.to_string())
        );
        assert_eq!(
            map_finish_reason(FINISH_REASON_MAX_TOKENS),
            Some(STOP_REASON_MAX_TOKENS.to_string())
        );
        assert_eq!(
            map_finish_reason(FINISH_REASON_SAFETY),
            Some(STOP_REASON_SAFETY.to_string())
        );
        assert_eq!(map_finish_reason("UNKNOWN"), Some("unknown".to_string()));
    }

    #[test]
    fn test_is_complete_json() {
        assert!(is_complete_json(r#"{"key": "value"}"#));
        assert!(is_complete_json(r#"{"nested": {"key": "value"}}"#));
        assert!(is_complete_json(r#"[1, 2, 3]"#));
        assert!(is_complete_json(r#""string""#));
        assert!(is_complete_json(r#"123"#));
        assert!(is_complete_json(r#"null"#));

        // Incomplete JSON
        assert!(!is_complete_json(r#"{"key": "val"#));
        assert!(!is_complete_json(r#"{"nested": {"#));
        assert!(!is_complete_json(r#"[1, 2,"#));
        assert!(!is_complete_json(r#""#));
    }

    #[test]
    fn test_partial_function_call_accumulation() {
        let mut state = StreamState::default();

        // First chunk with function call start
        let data1 = r#"{"candidates":[{"content":{"parts":[{"functionCall":{"name":"get_weather","args":{"location":"San Francisco"}}}],"role":"model"},"finishReason":"STOP"}]}"#;
        let sse1 = SseEvent {
            event: None,
            data: data1.to_string(),
        };

        let events = parse_stream_event(&sse1, &mut state).unwrap();

        // Should have start, delta, stop, and message delta
        let has_start = events.iter().any(|e| {
            matches!(e, StreamEvent::ContentBlockStart { block_type: ContentBlockType::ToolUse { name, .. }, .. } if name == "get_weather")
        });
        assert!(has_start);

        let has_delta = events.iter().any(|e| {
            matches!(e, StreamEvent::InputJsonDelta { json, .. } if json.contains("San Francisco"))
        });
        assert!(has_delta);

        let has_stop = events
            .iter()
            .any(|e| matches!(e, StreamEvent::ContentBlockStop { .. }));
        assert!(has_stop);
    }
}