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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
//! OpenAI-compatible provider implementation
use async_trait::async_trait;
use bytes::Bytes;
use futures::{Stream, StreamExt};
use reqwest::Client;
use serde::Deserialize;
use serde_json::Value as JsonValue;
use std::pin::Pin;
use super::openai_responses_shared::parse_streaming_json;
use super::shared_client;
use crate::{
error::ProviderError, Api, AssistantMessage, ContentBlock, Context, Model, Provider,
ProviderEvent, StopReason, StreamOptions, TextContent, ThinkingContent, Usage,
};
/// Detect whether a model targets the ZAI provider.
fn is_zai(model: &Model) -> bool {
model.provider.eq_ignore_ascii_case("zai") || model.base_url.contains("api.z.ai")
}
/// OpenAI-compatible provider
#[derive(Clone)]
pub struct OpenAiProvider {
client: &'static Client,
api_key: Option<String>,
base_url: Option<String>,
}
impl OpenAiProvider {
/// Create a new OpenAI provider without an API key.
///
/// API keys are resolved at request time via auth.json or StreamOptions.
/// Use `with_api_key()` for explicit key injection.
pub fn new() -> Self {
Self {
client: shared_client(),
api_key: None,
base_url: None,
}
}
/// Create with explicit API key (public API for external consumers)
pub fn with_api_key(api_key: impl Into<String>) -> Self {
Self {
client: shared_client(),
api_key: Some(api_key.into()),
base_url: None,
}
}
/// Create with a custom base URL (API key resolved from auth storage).
///
/// Used for built-in OpenAI-compatible providers like ZAI.
pub fn with_base_url(base_url: &str) -> Self {
Self {
client: shared_client(),
api_key: None,
base_url: Some(base_url.to_string()),
}
}
/// Create with a custom base URL and optional API key.
///
/// Used for registering custom OpenAI-compatible providers (Minimax, ZAI, etc.).
pub fn with_base_url_and_key(base_url: &str, api_key: Option<String>) -> Self {
Self {
client: shared_client(),
api_key,
base_url: Some(base_url.to_string()),
}
}
}
impl Default for OpenAiProvider {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Provider for OpenAiProvider {
async fn stream(
&self,
model: &Model,
context: &Context,
options: Option<StreamOptions>,
) -> Result<Pin<Box<dyn Stream<Item = ProviderEvent> + Send>>, ProviderError> {
let options = options.unwrap_or_default();
// Build the request
let effective_base_url = self.base_url.as_deref().unwrap_or(&model.base_url);
let url = format!("{}/chat/completions", effective_base_url);
// Get API key
let api_key = options
.api_key
.as_ref()
.or(self.api_key.as_ref())
.ok_or_else(|| ProviderError::MissingApiKey)?;
// Build messages
let messages = build_messages(context)?;
// Build request body
let mut body = serde_json::json!({
"model": model.id,
"messages": messages,
"stream": true,
"stream_options": { "include_usage": true },
});
// Add optional parameters
if let Some(temp) = options.temperature {
body["temperature"] = serde_json::json!(temp);
}
if let Some(max) = options.max_tokens {
body["max_tokens"] = serde_json::json!(max);
}
// Add tools if present
if !context.tools.is_empty() {
body["tools"] = build_tools(&context.tools)?;
}
// ── ZAI-specific parameters ──────────────────────────────────
// Mirror pi's detectCompat: when provider is ZAI (or base_url contains
// api.z.ai), send enable_thinking and tool_stream.
if is_zai(model) {
if model.reasoning {
body["enable_thinking"] = serde_json::json!(true);
}
if !context.tools.is_empty() {
body["tool_stream"] = serde_json::json!(true);
}
}
tracing::info!(
"Sending request to {} model={} body_len={} enable_thinking={} tool_stream={}",
url,
model.id,
body.to_string().len(),
body.get("enable_thinking").is_some(),
body.get("tool_stream").is_some()
);
tracing::debug!("Request body: {}", body.to_string());
// Build headers
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", api_key)
.parse()
.expect("valid bearer header"),
);
headers.insert(
reqwest::header::CONTENT_TYPE,
"application/json".parse().expect("valid header value"),
);
for (k, v) in &options.headers {
if let (Ok(name), Ok(value)) = (
k.parse::<reqwest::header::HeaderName>(),
v.parse::<reqwest::header::HeaderValue>(),
) {
headers.insert(name, value);
}
}
// Make request
let response = self
.client
.post(&url)
.headers(headers)
.json(&body)
.send()
.await
.map_err(ProviderError::RequestFailed)?;
if !response.status().is_success() {
let status = response.status();
let body: String = response.text().await.unwrap_or_default();
return Err(ProviderError::HttpError(status.as_u16(), body));
}
// Create event stream
let provider_name = model.provider.clone();
let model_id = model.id.clone();
// Emit Start event once at the beginning of the stream (matches pi's behavior)
let start_event = ProviderEvent::Start {
partial: AssistantMessage::new(Api::OpenAiCompletions, &provider_name, &model_id),
};
// Stateful stream parser that accumulates tool calls across chunks.
// OpenAI sends tool calls as multiple deltas (id, name, arguments fragments)
// that must be reassembled before emitting ToolCallEnd.
//
// State:
// pending_bytes – incomplete UTF-8 bytes from the previous HTTP chunk
// pending_tc_index – accumulated tool calls keyed by streaming index
// pending_tc_id – secondary lookup by tool-call ID (ZAI et al. may
// omit the index on continuation deltas)
// thinking_started – whether ThinkingStart has been emitted
let stream = response
.bytes_stream()
.scan(
(
Vec::new(),
std::collections::HashMap::<usize, (String, String, String)>::new(),
std::collections::HashMap::<String, usize>::new(), // id → index
false,
AssistantMessage::new(Api::OpenAiCompletions, &provider_name, &model_id),
),
move |(
pending_bytes,
pending_tc,
tc_id_to_index,
thinking_started,
accumulated_output,
),
chunk: Result<Bytes, reqwest::Error>| {
let events = match chunk {
Ok(bytes) => {
// Prepend any incomplete bytes from previous chunk
let mut combined =
Vec::with_capacity(pending_bytes.len() + bytes.len());
combined.extend_from_slice(pending_bytes);
combined.extend_from_slice(&bytes);
// Split into complete lines (ending with \n) and trailing incomplete data.
// This prevents JSON parse failures from partial SSE lines
// that were split across HTTP chunks.
let (text, trailing) = split_complete_lines(&combined);
*pending_bytes = trailing;
tracing::debug!(
"parse_sse_events input: {} bytes, {} lines",
text.len(),
text.lines().count()
);
let raw_events = parse_sse_events(
&text,
&provider_name,
&model_id,
accumulated_output,
);
tracing::debug!("parse_sse_events output: {} events", raw_events.len());
// Post-process: accumulate tool call deltas, inject ThinkingStart once
let mut processed = Vec::new();
for event in raw_events {
match &event {
ProviderEvent::ThinkingDelta { content_index, .. } => {
// Inject ThinkingStart before the first ThinkingDelta
if !*thinking_started {
*thinking_started = true;
processed.push(ProviderEvent::ThinkingStart {
content_index: *content_index,
partial: AssistantMessage::new(
Api::OpenAiCompletions,
&provider_name,
&model_id,
),
});
}
processed.push(event);
}
ProviderEvent::ToolCallStart {
content_index,
tool_call_id,
tool_name,
..
} => {
let entry =
pending_tc.entry(*content_index).or_insert_with(|| {
(String::new(), String::new(), String::new())
});
if let Some(ref id) = tool_call_id {
if !id.is_empty() {
entry.0 = id.clone();
tc_id_to_index.insert(id.clone(), *content_index);
}
}
if let Some(ref name) = tool_name {
if !name.is_empty() {
entry.1 = name.clone();
}
}
processed.push(event);
}
ProviderEvent::ToolCallDelta {
content_index,
delta,
..
} => {
// Dual-map lookup: prefer index, fall back to ID
let idx = if pending_tc.contains_key(content_index) {
*content_index
} else {
// Scan id→index map for a match
tc_id_to_index
.values()
.copied()
.find(|i| *i == *content_index)
.unwrap_or(*content_index)
};
let entry = pending_tc.entry(idx).or_insert_with(|| {
(String::new(), String::new(), String::new())
});
tracing::debug!(
"[TC-DELTA] idx={}, delta_len={}, accumulated_len={}",
idx,
delta.len(),
entry.2.len() + delta.len()
);
entry.2.push_str(delta);
processed.push(event);
}
ProviderEvent::ToolCallEnd { .. } => {
// Already a ToolCallEnd from parse_sse_events
processed.push(event);
}
ProviderEvent::Done { reason, .. } => {
// Before Done, emit ToolCallEnd for all accumulated tool calls
if matches!(reason, StopReason::ToolUse) {
let mut indices: Vec<usize> =
pending_tc.keys().copied().collect();
indices.sort();
for idx in indices {
let (id, name, arguments) = &pending_tc[&idx];
tracing::debug!(
"[TC-END] idx={}, id={}, name={}, args_len={}",
idx,
id.len(),
name.len(),
arguments.len()
);
let args_value = parse_streaming_json(arguments);
processed.push(ProviderEvent::ToolCallEnd {
content_index: idx,
tool_call: crate::ToolCall {
content_type:
crate::messages::ToolCallType::ToolCall,
id: id.clone(),
name: name.clone(),
arguments: args_value,
thought_signature: None,
},
partial: AssistantMessage::new(
Api::OpenAiCompletions,
&provider_name,
&model_id,
),
});
}
}
// Clear pending_tc for the next stream/turn.
// Without this, tool call arguments from the previous
// turn leak into the next turn's accumulation.
pending_tc.clear();
tc_id_to_index.clear();
processed.push(event);
}
_ => {
processed.push(event);
}
}
}
processed
}
Err(e) => {
vec![ProviderEvent::Error {
reason: StopReason::Error,
error: create_error_message(
&e.to_string(),
&provider_name,
&model_id,
),
}]
}
};
// Return Some to continue, wrap events in an iterator
async move { Some(futures::stream::iter(events)) }
},
)
.flatten();
// Prepend Start event to the stream
let stream_with_start = futures::stream::once(async move { start_event }).chain(stream);
Ok(Box::pin(stream_with_start))
}
fn name(&self) -> &str {
"openai"
}
}
/// Build messages array from context
fn build_messages(context: &Context) -> Result<Vec<JsonValue>, ProviderError> {
let mut messages = Vec::new();
// System prompt
if let Some(ref prompt) = context.system_prompt {
messages.push(serde_json::json!({
"role": "system",
"content": prompt,
}));
}
// Conversation messages
for msg in &context.messages {
match msg {
crate::Message::User(u) => {
let content: String = match &u.content {
crate::MessageContent::Text(s) => s.clone(),
crate::MessageContent::Blocks(blocks) => blocks_to_content(blocks)?.to_string(),
};
messages.push(serde_json::json!({
"role": "user",
"content": content,
}));
}
crate::Message::Assistant(a) => {
// OpenAI format: separate content (text) and tool_calls
let mut text_parts = Vec::new();
let mut tool_calls = Vec::new();
for block in &a.content {
match block {
ContentBlock::Text(t) => {
text_parts.push(t.text.clone());
}
ContentBlock::Thinking(_) => {
// Skip thinking blocks in message history
}
ContentBlock::ToolCall(tc) => {
tool_calls.push(serde_json::json!({
"id": tc.id,
"type": "function",
"function": {
"name": tc.name,
"arguments": tc.arguments.to_string(),
},
}));
}
ContentBlock::Image(_) | ContentBlock::Unknown(_) => {}
}
}
let mut msg = serde_json::json!({
"role": "assistant",
"content": text_parts.join(""),
});
if !tool_calls.is_empty() {
msg["tool_calls"] = serde_json::json!(tool_calls);
}
messages.push(msg);
}
crate::Message::ToolResult(t) => {
let result_text: String = t
.content
.iter()
.filter_map(|b| b.as_text())
.collect::<Vec<_>>()
.join("");
messages.push(serde_json::json!({
"role": "tool",
"tool_call_id": t.tool_call_id,
"content": result_text,
}));
}
}
}
Ok(messages)
}
/// Convert content blocks to a string representation
fn blocks_to_content(blocks: &[ContentBlock]) -> Result<JsonValue, ProviderError> {
if blocks.len() == 1 {
if let Some(text) = blocks[0].as_text() {
return Ok(JsonValue::String(text.to_string()));
}
}
let items: Result<Vec<_>, _> = blocks
.iter()
.map(|block| match block {
ContentBlock::Text(t) => Ok(serde_json::json!({
"type": "text",
"text": t.text,
})),
ContentBlock::ToolCall(tc) => Ok(serde_json::json!({
"type": "function",
"id": tc.id,
"function": {
"name": tc.name,
"arguments": tc.arguments.to_string(),
},
})),
ContentBlock::Thinking(th) => Ok(serde_json::json!({
"type": "thinking",
"thinking": th.thinking,
})),
ContentBlock::Image(img) => Ok(serde_json::json!({
"type": "image_url",
"image_url": {
"url": format!("data:{};base64,{}", img.mime_type, img.data),
},
})),
ContentBlock::Unknown(_) => Err(ProviderError::InvalidResponse(
"Unknown content block type".into(),
)),
})
.collect();
Ok(serde_json::json!(items?))
}
/// Build tools array
fn build_tools(tools: &[crate::Tool]) -> Result<JsonValue, ProviderError> {
let items: Vec<_> = tools
.iter()
.map(|tool| {
serde_json::json!({
"type": "function",
"function": {
"name": tool.name,
"description": tool.description,
"parameters": tool.parameters,
},
})
})
.collect();
Ok(serde_json::json!(items))
}
/// Extract the longest valid UTF-8 prefix from a byte slice.
///
/// Returns the valid string and the trailing bytes that form an incomplete UTF-8
/// sequence. These trailing bytes should be prepended to the next chunk to
/// ensure no characters are lost at HTTP chunk boundaries.
fn find_valid_utf8_prefix(bytes: &[u8]) -> (String, Vec<u8>) {
match std::str::from_utf8(bytes) {
Ok(s) => (s.to_string(), Vec::new()),
Err(e) => {
let valid = &bytes[..e.valid_up_to()];
let trailing = bytes[e.valid_up_to()..].to_vec();
(String::from_utf8_lossy(valid).to_string(), trailing)
}
}
}
/// Split bytes into complete lines (ending with \n) and trailing incomplete data.
/// This ensures `parse_sse_events` only receives complete SSE `data:` lines,
/// preventing JSON parse failures from lines split across HTTP chunks.
pub fn split_complete_lines(bytes: &[u8]) -> (String, Vec<u8>) {
// Find the last newline — everything up to and including it is complete.
match bytes.iter().rposition(|&b| b == b'\n') {
Some(last_nl) => {
let split_at = last_nl + 1;
let complete = match std::str::from_utf8(&bytes[..split_at]) {
Ok(s) => s.to_string(),
Err(_) => {
let (s, _) = find_valid_utf8_prefix(&bytes[..split_at]);
s
}
};
let trailing = bytes[split_at..].to_vec();
(complete, trailing)
}
None => {
// No newline at all — the entire buffer is incomplete.
// Check if it's valid UTF-8; if not, save as pending.
(String::new(), bytes.to_vec())
}
}
}
/// Parse SSE event stream from a byte buffer.
///
/// Optimizations over a naïve implementation:
/// - **Fast-line splitting** – iterates over `\n` boundaries via `split`
/// instead of allocating an intermediate `String` per line.
/// - **Early `DONE` exit** – breaks immediately when `data: [DONE]` is
/// encountered.
/// - **Pre-allocated events** – reserves capacity based on data-line count.
/// - **Accumulated usage** – tracks usage separately, only cloning into
/// the Done message at stream end, not on every chunk.
fn parse_sse_events(
text: &str,
_provider: &str,
_model_id: &str,
output: &mut AssistantMessage,
) -> Vec<ProviderEvent> {
let mut events = Vec::new();
// Pre-estimate capacity: one event per data line is a reasonable upper bound.
let estimated_events = text.split('\n').filter(|l| l.starts_with("data: ")).count();
events.reserve(estimated_events);
let mut accumulated_usage = Usage::default();
for line in text.split('\n') {
let line = line.trim_end_matches('\r');
if line.is_empty() {
continue;
}
// Fast rejection for non-data lines (comments, event tags, etc.)
if !line.starts_with("data: ") {
continue;
}
let data = &line[6..]; // skip "data: "
// Early exit on stream end
if data == "[DONE]" {
break;
}
if data.is_empty() {
continue;
}
let chunk = match serde_json::from_str::<SSEChunk>(data) {
Ok(c) => c,
Err(_) => continue,
};
// ── Accumulate usage BEFORE processing choices ────────────────
// OpenAI with include_usage sends usage in a final chunk with
// empty choices. By accumulating before the choice loop, the
// Done event (triggered by finish_reason in an earlier chunk)
// and any subsequent rendering sees the latest usage.
if let Some(chunk_usage) = &chunk.usage {
accumulated_usage.input = chunk_usage.prompt_tokens;
accumulated_usage.output = chunk_usage.completion_tokens;
accumulated_usage.cache_read = chunk_usage
.prompt_tokens_details
.as_ref()
.map(|d| d.cached_tokens)
.unwrap_or(0);
accumulated_usage.total_tokens = chunk_usage.total_tokens;
}
for choice in &chunk.choices {
if let Some(delta) = &choice.delta {
if let Some(content) = &delta.content {
// pi-mono: append to the output's text block
let last_text_idx = output
.content
.iter()
.rposition(|b| matches!(b, ContentBlock::Text(_)));
if let Some(idx) = last_text_idx {
if let ContentBlock::Text(t) = &mut output.content[idx] {
t.text.push_str(content);
}
} else {
output
.content
.push(ContentBlock::Text(TextContent::new(content.clone())));
}
events.push(ProviderEvent::TextDelta {
content_index: choice.index,
delta: content.clone(),
partial: output.clone(),
});
}
// Handle GLM's reasoning_content field (thinking/thought chain)
if let Some(ref reasoning) = delta.reasoning_content {
if !reasoning.is_empty() {
// pi-mono: append to the output's thinking block
let last_think_idx = output
.content
.iter()
.rposition(|b| matches!(b, ContentBlock::Thinking(_)));
if let Some(idx) = last_think_idx {
if let ContentBlock::Thinking(t) = &mut output.content[idx] {
t.thinking.push_str(reasoning);
}
} else {
output
.content
.push(ContentBlock::Thinking(ThinkingContent::new(
reasoning.clone(),
)));
}
events.push(ProviderEvent::ThinkingDelta {
content_index: choice.index,
delta: reasoning.clone(),
partial: output.clone(),
});
}
}
if let Some(tool_calls) = &delta.tool_calls {
for tc in tool_calls {
let tc_index = tc.index.unwrap_or(choice.index);
// Emit ToolCallStart when id or name is present (first delta)
if tc.id.is_some()
|| tc.function.as_ref().and_then(|f| f.name.as_ref()).is_some()
{
events.push(ProviderEvent::ToolCallStart {
content_index: tc_index,
tool_call_id: tc.id.clone(),
tool_name: tc.function.as_ref().and_then(|f| f.name.clone()),
partial: output.clone(),
});
}
// Emit ToolCallDelta for arguments
if let Some(func) = &tc.function {
events.push(ProviderEvent::ToolCallDelta {
content_index: tc_index,
delta: func.arguments.clone().unwrap_or_default(),
partial: output.clone(),
});
}
}
}
}
if choice.finish_reason.is_some() {
let reason = match choice.finish_reason.as_deref() {
Some("stop") | Some("end") => StopReason::Stop,
Some("length") => StopReason::Length,
Some("tool_calls") | Some("function_call") => StopReason::ToolUse,
Some("content_filter") => StopReason::Error,
Some(unknown) => {
tracing::warn!("Unknown finish_reason: '{}', treating as Error", unknown);
StopReason::Error
}
None => StopReason::Stop,
};
tracing::info!("finish_reason={:?} → {:?}", choice.finish_reason, reason);
let mut done_msg = output.clone();
done_msg.stop_reason = reason;
done_msg.usage = accumulated_usage.clone();
events.push(ProviderEvent::Done {
reason,
message: done_msg,
});
}
}
}
events
}
/// Create error assistant message
fn create_error_message(msg: &str, provider: &str, model_id: &str) -> AssistantMessage {
let mut message = AssistantMessage::new(Api::OpenAiCompletions, provider, model_id);
message.stop_reason = StopReason::Error;
message.error_message = Some(msg.to_string());
message
}
// SSE chunk structure
#[derive(Debug, Deserialize)]
// serde deserialization structs
struct SSEChunk {
_id: Option<String>,
#[serde(rename = "model")]
_model: Option<String>,
choices: Vec<Choice>,
usage: Option<UsageInfo>,
}
#[derive(Debug, Deserialize)]
// serde deserialization structs
struct Choice {
index: usize,
delta: Option<Delta>,
finish_reason: Option<String>,
}
#[derive(Debug, Deserialize)]
struct Delta {
content: Option<String>,
reasoning_content: Option<String>,
tool_calls: Option<Vec<ToolCallDelta>>,
}
#[derive(Debug, Deserialize)]
// serde deserialization structs
struct ToolCallDelta {
index: Option<usize>,
id: Option<String>,
#[serde(rename = "type")]
_type_: Option<String>,
function: Option<FunctionDelta>,
}
#[derive(Debug, Deserialize)]
// serde deserialization structs
struct FunctionDelta {
name: Option<String>,
arguments: Option<String>,
}
#[derive(Debug, Deserialize, Clone)]
struct UsageInfo {
prompt_tokens: usize,
completion_tokens: usize,
total_tokens: usize,
#[serde(rename = "prompt_tokens_details")]
prompt_tokens_details: Option<PromptTokensDetails>,
}
#[derive(Debug, Deserialize, Clone)]
struct PromptTokensDetails {
#[serde(rename = "cached_tokens")]
cached_tokens: usize,
}
#[cfg(test)]
mod tests {
use super::*;
const PROVIDER: &str = "openai";
const MODEL: &str = "gpt-4o";
fn parse_sse(sse: &str) -> Vec<ProviderEvent> {
let mut output = AssistantMessage::new(Api::OpenAiCompletions, PROVIDER, MODEL);
parse_sse_events(sse, PROVIDER, MODEL, &mut output)
}
// ── SSE event parsing ──────────────────────────────────────────────
#[test]
fn parse_single_text_event() {
let sse = "data: {\"id\":\"chatcmpl-1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"}}]}\n\n";
let events = parse_sse(sse);
assert_eq!(events.len(), 1);
match &events[0] {
ProviderEvent::TextDelta {
delta,
content_index,
..
} => {
assert_eq!(delta, "Hello");
assert_eq!(*content_index, 0);
}
other => panic!("expected TextDelta, got {other:?}"),
}
}
#[test]
fn parse_multiple_text_events() {
let sse = concat!(
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hel\"}}]}\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo!\"}}]}\n",
"\n"
);
let events = parse_sse(sse);
assert_eq!(events.len(), 2);
let texts: Vec<&str> = events
.iter()
.filter_map(|e| match e {
ProviderEvent::TextDelta { delta, .. } => Some(delta.as_str()),
_ => None,
})
.collect();
assert_eq!(texts, vec!["Hel", "lo!"]);
}
#[test]
fn parse_done_terminator() {
let sse = concat!(
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"X\"}}]}\n",
"\n",
"data: [DONE]\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"NEVER\"}}]}\n"
);
let events = parse_sse(sse);
// Should stop at [DONE]; the final data line is never parsed
assert_eq!(events.len(), 1);
match &events[0] {
ProviderEvent::TextDelta { delta, .. } => assert_eq!(delta, "X"),
other => panic!("expected TextDelta, got {other:?}"),
}
}
// ── Content extraction ─────────────────────────────────────────────
#[test]
fn parse_finish_reason_stop() {
let sse = "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":null,\"finish_reason\":\"stop\"}]}\n\n";
let events = parse_sse(sse);
assert_eq!(events.len(), 1);
match &events[0] {
ProviderEvent::Done { reason, .. } => assert!(matches!(reason, StopReason::Stop)),
other => panic!("expected Done, got {other:?}"),
}
}
#[test]
fn parse_finish_reason_length() {
let sse = "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":null,\"finish_reason\":\"length\"}]}\n\n";
let events = parse_sse(sse);
match &events[0] {
ProviderEvent::Done { reason, .. } => assert!(matches!(reason, StopReason::Length)),
other => panic!("expected Done with Length, got {other:?}"),
}
}
#[test]
fn parse_finish_reason_tool_calls() {
let sse = "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":null,\"finish_reason\":\"tool_calls\"}]}\n\n";
let events = parse_sse(sse);
match &events[0] {
ProviderEvent::Done { reason, .. } => assert!(matches!(reason, StopReason::ToolUse)),
other => panic!("expected Done with ToolUse, got {other:?}"),
}
}
// ── Tool call delta accumulation ───────────────────────────────────
#[test]
fn parse_tool_call_deltas() {
let sse = concat!(
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"\"}}]}}]}\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"city\\\":\\\"SF\\\"}\"}}]}}]}\n",
"\n"
);
let events = parse_sse(sse);
// First chunk: ToolCallStart (id+name present) + ToolCallDelta (function present)
// Second chunk: ToolCallDelta only
assert_eq!(events.len(), 3);
let starts: Vec<&str> = events
.iter()
.filter_map(|e| match e {
ProviderEvent::ToolCallStart { tool_name, .. } => tool_name.as_deref(),
_ => None,
})
.collect();
assert_eq!(starts, vec!["get_weather"]);
let deltas: Vec<&str> = events
.iter()
.filter_map(|e| match e {
ProviderEvent::ToolCallDelta { delta, .. } => Some(delta.as_str()),
_ => None,
})
.collect();
assert_eq!(deltas, vec!["", "{\"city\":\"SF\"}"]);
}
#[test]
fn parse_tool_call_with_no_arguments_field() {
// function field present but arguments is null → emits ToolCallStart + ToolCallDelta
let sse = "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"run\"}}]}}]}\n\n";
let events = parse_sse(sse);
assert_eq!(events.len(), 2);
match &events[0] {
ProviderEvent::ToolCallStart { tool_name, .. } => {
assert_eq!(tool_name.as_deref(), Some("run"));
}
other => panic!("expected ToolCallStart, got {other:?}"),
}
match &events[1] {
ProviderEvent::ToolCallDelta { delta, .. } => assert_eq!(delta, ""),
other => panic!("expected ToolCallDelta, got {other:?}"),
}
}
// ── Usage accumulation ─────────────────────────────────────────────
#[test]
fn parse_usage_in_chunk() {
// Usage is accumulated from earlier chunks; the Done event captures
// usage that was accumulated *before* the finish_reason chunk.
let sse = concat!(
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hi\"}}],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":8,\"total_tokens\":18,\"prompt_tokens_details\":{\"cached_tokens\":3}}}\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":null,\"finish_reason\":\"stop\"}]}\n"
);
let events = parse_sse(sse);
// TextDelta + Done
assert_eq!(events.len(), 2);
match &events[1] {
ProviderEvent::Done { message, .. } => {
assert_eq!(message.usage.input, 10);
assert_eq!(message.usage.output, 8);
assert_eq!(message.usage.total_tokens, 18);
assert_eq!(message.usage.cache_read, 3);
}
other => panic!("expected Done, got {other:?}"),
}
}
#[test]
fn parse_usage_without_cache_details() {
// Usage from an earlier chunk; Done event on a separate chunk without usage.
let sse = concat!(
"data: {\"id\":\"c\",\"choices\":[],\"usage\":{\"prompt_tokens\":5,\"completion_tokens\":2,\"total_tokens\":7}}\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":null,\"finish_reason\":\"stop\"}]}\n"
);
let events = parse_sse(sse);
match &events[0] {
ProviderEvent::Done { message, .. } => {
assert_eq!(message.usage.input, 5);
assert_eq!(message.usage.output, 2);
assert_eq!(message.usage.cache_read, 0);
}
other => panic!("expected Done, got {other:?}"),
}
}
// ── Empty / malformed handling ─────────────────────────────────────
#[test]
fn parse_empty_input() {
let events = parse_sse("");
assert!(events.is_empty());
}
#[test]
fn parse_only_empty_lines() {
let events = parse_sse("\n\n\n");
assert!(events.is_empty());
}
#[test]
fn parse_malformed_json_after_data() {
let sse = "data: {not json at all}\ndata: also bad\ndata: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"}}]}\n";
let events = parse_sse(sse);
// Malformed lines are skipped, only the valid one emits
assert_eq!(events.len(), 1);
match &events[0] {
ProviderEvent::TextDelta { delta, .. } => assert_eq!(delta, "ok"),
other => panic!("expected TextDelta, got {other:?}"),
}
}
#[test]
fn parse_empty_data_line() {
let sse = "data: \ndata: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"X\"}}]}\n";
let events = parse_sse(sse);
assert_eq!(events.len(), 1);
}
#[test]
fn parse_non_data_lines_ignored() {
let sse = "event: ping\nid: 42\nretry: 5000\ndata: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Y\"}}]}\n";
let events = parse_sse(sse);
assert_eq!(events.len(), 1);
}
#[test]
fn parse_carriage_return_line_endings() {
let sse = "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"CR\"}}]}\r\n\r\n";
let events = parse_sse(sse);
assert_eq!(events.len(), 1);
match &events[0] {
ProviderEvent::TextDelta { delta, .. } => assert_eq!(delta, "CR"),
other => panic!("expected TextDelta, got {other:?}"),
}
}
// ── Mixed content + tool + done ────────────────────────────────────
#[test]
fn parse_full_stream_with_text_tool_and_done() {
let sse = concat!(
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Let me\"}}]}\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\" check\"}}]}\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"search\",\"arguments\":\"{\\\"q\\\":\\\"rust\\\"}\"}}]}}]}\n",
"\n",
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":null,\"finish_reason\":\"tool_calls\"}]}\n",
"\n",
"data: [DONE]\n"
);
let events = parse_sse(sse);
assert_eq!(events.len(), 5); // 2 TextDelta + ToolCallStart + ToolCallDelta + Done
let mut text_count = 0;
let mut tc_start_count = 0;
let mut tc_delta_count = 0;
let mut done_count = 0;
for e in &events {
match e {
ProviderEvent::TextDelta { .. } => text_count += 1,
ProviderEvent::ToolCallStart { .. } => tc_start_count += 1,
ProviderEvent::ToolCallDelta { .. } => tc_delta_count += 1,
ProviderEvent::Done { reason, .. } => {
done_count += 1;
assert!(matches!(reason, StopReason::ToolUse));
}
other => panic!("unexpected event: {other:?}"),
}
}
assert_eq!(text_count, 2);
assert_eq!(tc_start_count, 1);
assert_eq!(tc_delta_count, 1);
assert_eq!(done_count, 1);
}
}