reson-agentic 0.5.1

Agents are just functions - production-grade LLM agent framework
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! OpenAI streaming response parser
//!
//! Handles OpenAI's delta-based streaming format with index-based tool call accumulation.
//! Key differences from Anthropic:
//! - Tool calls use index-based deltas (not content_block_* events)
//! - Multiple parallel tools supported via index changes
//! - Reasoning can come alongside content in same delta
//! - Usage arrives in separate final chunk

use crate::providers::StreamChunk;
use crate::utils::parse_json_value_strict_str;
use serde_json::Value;
use std::collections::HashMap;

/// Accumulates partial tool calls by index for OpenAI streaming
#[derive(Debug, Default)]
pub struct OpenAIToolAccumulator {
    current_tool_calls: HashMap<usize, PartialToolCall>,
}

#[derive(Debug, Clone)]
struct PartialToolCall {
    id: String,
    name: String,
    arguments: String,
}

impl OpenAIToolAccumulator {
    pub fn new() -> Self {
        Self {
            current_tool_calls: HashMap::new(),
        }
    }

    /// Start or update a tool call at given index
    pub fn update_tool(&mut self, index: usize, id: Option<&str>, name: Option<&str>, args: &str) {
        let entry = self
            .current_tool_calls
            .entry(index)
            .or_insert_with(|| PartialToolCall {
                id: String::new(),
                name: String::new(),
                arguments: String::new(),
            });

        if let Some(id) = id {
            entry.id = id.to_string();
        }
        if let Some(name) = name {
            entry.name = name.to_string();
        }
        entry.arguments.push_str(args);
    }

    /// Complete a tool call at given index, returning the JSON
    pub fn complete_tool(&mut self, index: usize) -> Option<Value> {
        self.current_tool_calls.remove(&index).and_then(|tool| {
            // Parse accumulated JSON
            parse_json_value_strict_str(&tool.arguments)
                .ok()
                .map(|args: Value| {
                    serde_json::json!({
                        "id": tool.id,
                        "type": "function",
                        "function": {
                            "name": tool.name,
                            "arguments": args
                        }
                    })
                })
        })
    }

    /// Return partial tool call data if name is known
    pub fn tool_partial(&self, index: usize) -> Option<Value> {
        self.current_tool_calls.get(&index).and_then(|tool| {
            if tool.name.is_empty() {
                return None;
            }
            let call_id = if tool.id.is_empty() {
                format!("call_{}", index)
            } else {
                tool.id.clone()
            };
            Some(serde_json::json!({
                "id": call_id,
                "type": "function",
                "function": {
                    "name": tool.name,
                    "arguments": tool.arguments
                }
            }))
        })
    }

    /// Check if tool at index is ready (has id and name)
    pub fn is_tool_ready(&self, index: usize) -> bool {
        self.current_tool_calls
            .get(&index)
            .map(|t| !t.id.is_empty() && !t.name.is_empty())
            .unwrap_or(false)
    }
}

/// Parse OpenAI streaming chunk into StreamChunks
///
/// Handles:
/// - delta.content → Content chunk
/// - delta.reasoning → Reasoning chunk
/// - delta.signature → Signature chunk
/// - delta.tool_calls → Index-based tool accumulation with parallel support
/// - usage → Usage chunk (final message)
pub fn parse_openai_chunk(
    chunk_json: &Value,
    accumulator: &mut OpenAIToolAccumulator,
    has_tools: bool,
) -> Vec<StreamChunk> {
    let mut chunks = Vec::new();

    // Extract delta from choices[0]
    let delta = match chunk_json
        .get("choices")
        .and_then(|c| c.get(0))
        .and_then(|c| c.get("delta"))
    {
        Some(d) => d,
        None => {
            // Check for usage in final chunk
            if let Some(usage) = chunk_json.get("usage") {
                chunks.push(StreamChunk::Usage {
                    input_tokens: usage["prompt_tokens"].as_u64().unwrap_or(0),
                    output_tokens: usage["completion_tokens"].as_u64().unwrap_or(0),
                    cached_tokens: usage
                        .get("prompt_tokens_details")
                        .and_then(|d| d.get("cached_tokens"))
                        .and_then(|v| v.as_u64())
                        .unwrap_or(0),
                });
            }
            return chunks;
        }
    };

    // Handle content
    if let Some(content) = delta.get("content").and_then(|c| c.as_str()) {
        if !content.is_empty() {
            chunks.push(StreamChunk::Content(content.to_string()));
        }
    }

    // Handle reasoning (o-series models)
    if let Some(reasoning) = delta.get("reasoning").and_then(|r| r.as_str()) {
        if !reasoning.is_empty() {
            chunks.push(StreamChunk::Reasoning(reasoning.to_string()));
        }
    }

    // Handle signature
    if let Some(signature) = delta.get("signature").and_then(|s| s.as_str()) {
        if !signature.is_empty() {
            chunks.push(StreamChunk::Signature(signature.to_string()));
        }
    }

    // Handle tool calls (only if tools were provided)
    if has_tools {
        if let Some(tool_calls) = delta.get("tool_calls").and_then(|tc| tc.as_array()) {
            for tool_call in tool_calls {
                let index = tool_call["index"].as_u64().unwrap_or(0) as usize;

                // Extract delta fields
                let id = tool_call.get("id").and_then(|i| i.as_str());
                let name = tool_call
                    .get("function")
                    .and_then(|f| f.get("name"))
                    .and_then(|n| n.as_str());
                let args = tool_call
                    .get("function")
                    .and_then(|f| f.get("arguments"))
                    .and_then(|a| a.as_str())
                    .unwrap_or("");

                // Check if this is a new tool (index changed from previous)
                let was_ready = accumulator.is_tool_ready(index);

                // Update accumulator
                accumulator.update_tool(index, id, name, args);

                // If we had a complete tool at this index before, emit completion
                // This happens when index changes (parallel tool support)
                if was_ready && (id.is_some() || name.is_some()) {
                    // New tool starting at same index = previous tool complete
                    if let Some(completed) = accumulator.complete_tool(index) {
                        chunks.push(StreamChunk::ToolCallComplete(completed));
                    }
                    // Re-add the new tool
                    accumulator.update_tool(index, id, name, args);
                }

                // Emit partial tool state for UI updates
                if let Some(partial) = accumulator.tool_partial(index) {
                    chunks.push(StreamChunk::ToolCallPartial(partial));
                }
            }
        }
    }

    // Check for finish_reason to complete any remaining tools
    if has_tools {
        if let Some(finish_reason) = chunk_json
            .get("choices")
            .and_then(|c| c.get(0))
            .and_then(|c| c.get("finish_reason"))
            .and_then(|fr| fr.as_str())
        {
            if finish_reason == "tool_calls" || finish_reason == "stop" {
                // Complete all remaining tools
                let indices: Vec<usize> = accumulator.current_tool_calls.keys().copied().collect();
                for index in indices {
                    if let Some(completed) = accumulator.complete_tool(index) {
                        chunks.push(StreamChunk::ToolCallComplete(completed));
                    }
                }
            }
        }
    }

    chunks
}

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

    #[test]
    fn test_accumulator_new() {
        let acc = OpenAIToolAccumulator::new();
        assert_eq!(acc.current_tool_calls.len(), 0);
    }

    #[test]
    fn test_accumulator_update_tool() {
        let mut acc = OpenAIToolAccumulator::new();
        acc.update_tool(0, Some("call_123"), Some("get_weather"), "{\"location\":");
        acc.update_tool(0, None, None, "\"SF\"}");

        let tool = acc.current_tool_calls.get(&0).unwrap();
        assert_eq!(tool.id, "call_123");
        assert_eq!(tool.name, "get_weather");
        assert_eq!(tool.arguments, "{\"location\":\"SF\"}");
    }

    #[test]
    fn test_accumulator_complete_tool() {
        let mut acc = OpenAIToolAccumulator::new();
        acc.update_tool(
            0,
            Some("call_123"),
            Some("get_weather"),
            "{\"location\":\"SF\"}",
        );

        let completed = acc.complete_tool(0).unwrap();
        assert_eq!(completed["id"], "call_123");
        assert_eq!(completed["function"]["name"], "get_weather");
        assert_eq!(completed["function"]["arguments"]["location"], "SF");

        // Should be removed after completion
        assert!(!acc.current_tool_calls.contains_key(&0));
    }

    #[test]
    fn test_accumulator_is_tool_ready() {
        let mut acc = OpenAIToolAccumulator::new();
        assert!(!acc.is_tool_ready(0));

        acc.update_tool(0, Some("call_123"), None, "");
        assert!(!acc.is_tool_ready(0)); // Need both id and name

        acc.update_tool(0, None, Some("get_weather"), "");
        assert!(acc.is_tool_ready(0)); // Now ready
    }

    #[test]
    fn test_parse_content_chunk() {
        let chunk = serde_json::json!({
            "choices": [{
                "delta": {
                    "content": "Hello"
                }
            }]
        });

        let mut acc = OpenAIToolAccumulator::new();
        let chunks = parse_openai_chunk(&chunk, &mut acc, false);

        assert_eq!(chunks.len(), 1);
        match &chunks[0] {
            StreamChunk::Content(s) => assert_eq!(s, "Hello"),
            _ => panic!("Expected Content chunk"),
        }
    }

    #[test]
    fn test_parse_reasoning_chunk() {
        let chunk = serde_json::json!({
            "choices": [{
                "delta": {
                    "reasoning": "Let me think..."
                }
            }]
        });

        let mut acc = OpenAIToolAccumulator::new();
        let chunks = parse_openai_chunk(&chunk, &mut acc, false);

        assert_eq!(chunks.len(), 1);
        match &chunks[0] {
            StreamChunk::Reasoning(s) => assert_eq!(s, "Let me think..."),
            _ => panic!("Expected Reasoning chunk"),
        }
    }

    #[test]
    fn test_parse_signature_chunk() {
        let chunk = serde_json::json!({
            "choices": [{
                "delta": {
                    "signature": "sig_123"
                }
            }]
        });

        let mut acc = OpenAIToolAccumulator::new();
        let chunks = parse_openai_chunk(&chunk, &mut acc, false);

        assert_eq!(chunks.len(), 1);
        match &chunks[0] {
            StreamChunk::Signature(s) => assert_eq!(s, "sig_123"),
            _ => panic!("Expected Signature chunk"),
        }
    }

    #[test]
    fn test_parse_tool_call_start() {
        let chunk = serde_json::json!({
            "choices": [{
                "delta": {
                    "tool_calls": [{
                        "index": 0,
                        "id": "call_123",
                        "function": {
                            "name": "get_weather",
                            "arguments": "{\"loc"
                        }
                    }]
                }
            }]
        });

        let mut acc = OpenAIToolAccumulator::new();
        let chunks = parse_openai_chunk(&chunk, &mut acc, true);

        assert_eq!(chunks.len(), 1);
        match &chunks[0] {
            StreamChunk::ToolCallPartial(tool) => {
                assert_eq!(tool["id"], "call_123");
                assert_eq!(tool["function"]["name"], "get_weather");
            }
            _ => panic!("Expected ToolCallPartial chunk"),
        }
        assert!(acc.is_tool_ready(0));
    }

    #[test]
    fn test_parse_tool_call_accumulation() {
        let mut acc = OpenAIToolAccumulator::new();

        // First chunk - start tool
        let chunk1 = serde_json::json!({
            "choices": [{
                "delta": {
                    "tool_calls": [{
                        "index": 0,
                        "id": "call_123",
                        "function": {
                            "name": "get_weather",
                            "arguments": "{\"location\":"
                        }
                    }]
                }
            }]
        });
        let chunks1 = parse_openai_chunk(&chunk1, &mut acc, true);
        assert!(matches!(
            chunks1.first(),
            Some(StreamChunk::ToolCallPartial(_))
        ));

        // Second chunk - continue arguments
        let chunk2 = serde_json::json!({
            "choices": [{
                "delta": {
                    "tool_calls": [{
                        "index": 0,
                        "function": {
                            "arguments": "\"SF\"}"
                        }
                    }]
                }
            }]
        });
        let chunks2 = parse_openai_chunk(&chunk2, &mut acc, true);
        assert!(matches!(
            chunks2.first(),
            Some(StreamChunk::ToolCallPartial(_))
        ));

        // Should have accumulated full JSON
        let tool = acc.current_tool_calls.get(&0).unwrap();
        assert_eq!(tool.arguments, "{\"location\":\"SF\"}");
    }

    #[test]
    fn test_parse_tool_call_completion_on_finish() {
        let mut acc = OpenAIToolAccumulator::new();

        // Start tool
        let chunk1 = serde_json::json!({
            "choices": [{
                "delta": {
                    "tool_calls": [{
                        "index": 0,
                        "id": "call_123",
                        "function": {
                            "name": "get_weather",
                            "arguments": "{\"location\":\"SF\"}"
                        }
                    }]
                }
            }]
        });
        parse_openai_chunk(&chunk1, &mut acc, true);

        // Finish chunk
        let chunk2 = serde_json::json!({
            "choices": [{
                "delta": {},
                "finish_reason": "tool_calls"
            }]
        });
        let chunks = parse_openai_chunk(&chunk2, &mut acc, true);

        assert_eq!(chunks.len(), 1);
        match &chunks[0] {
            StreamChunk::ToolCallComplete(tc) => {
                assert_eq!(tc["id"], "call_123");
                assert_eq!(tc["function"]["name"], "get_weather");
            }
            _ => panic!("Expected ToolCallComplete chunk"),
        }
    }

    #[test]
    fn test_parse_parallel_tools() {
        let mut acc = OpenAIToolAccumulator::new();

        // Start two tools
        let chunk = serde_json::json!({
            "choices": [{
                "delta": {
                    "tool_calls": [
                        {
                            "index": 0,
                            "id": "call_123",
                            "function": {
                                "name": "get_weather",
                                "arguments": "{\"location\":\"SF\"}"
                            }
                        },
                        {
                            "index": 1,
                            "id": "call_456",
                            "function": {
                                "name": "get_time",
                                "arguments": "{\"timezone\":\"UTC\"}"
                            }
                        }
                    ]
                }
            }]
        });
        parse_openai_chunk(&chunk, &mut acc, true);

        // Finish both
        let finish = serde_json::json!({
            "choices": [{
                "delta": {},
                "finish_reason": "tool_calls"
            }]
        });
        let chunks = parse_openai_chunk(&finish, &mut acc, true);

        assert_eq!(chunks.len(), 2);
        for chunk in &chunks {
            match chunk {
                StreamChunk::ToolCallComplete(_) => {} // Expected
                _ => panic!("Expected ToolCallComplete chunk"),
            }
        }
    }

    #[test]
    fn test_parse_usage_chunk() {
        let chunk = serde_json::json!({
            "usage": {
                "prompt_tokens": 100,
                "completion_tokens": 50,
                "prompt_tokens_details": {
                    "cached_tokens": 25
                }
            }
        });

        let mut acc = OpenAIToolAccumulator::new();
        let chunks = parse_openai_chunk(&chunk, &mut acc, false);

        assert_eq!(chunks.len(), 1);
        match &chunks[0] {
            StreamChunk::Usage {
                input_tokens,
                output_tokens,
                cached_tokens,
            } => {
                assert_eq!(*input_tokens, 100);
                assert_eq!(*output_tokens, 50);
                assert_eq!(*cached_tokens, 25);
            }
            _ => panic!("Expected Usage chunk"),
        }
    }

    #[test]
    fn test_parse_empty_content_ignored() {
        let chunk = serde_json::json!({
            "choices": [{
                "delta": {
                    "content": ""
                }
            }]
        });

        let mut acc = OpenAIToolAccumulator::new();
        let chunks = parse_openai_chunk(&chunk, &mut acc, false);

        assert_eq!(chunks.len(), 0);
    }

    #[test]
    fn test_parse_mixed_content_and_reasoning() {
        let chunk = serde_json::json!({
            "choices": [{
                "delta": {
                    "content": "Answer: ",
                    "reasoning": "First, I'll analyze..."
                }
            }]
        });

        let mut acc = OpenAIToolAccumulator::new();
        let chunks = parse_openai_chunk(&chunk, &mut acc, false);

        assert_eq!(chunks.len(), 2);
        match &chunks[0] {
            StreamChunk::Content(s) => assert_eq!(s, "Answer: "),
            _ => panic!("Expected Content chunk"),
        }
        match &chunks[1] {
            StreamChunk::Reasoning(s) => assert_eq!(s, "First, I'll analyze..."),
            _ => panic!("Expected Reasoning chunk"),
        }
    }
}