anyllm_translate 0.9.7

Pure translation layer between Anthropic Messages API and OpenAI Chat Completions
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
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
// Streaming state machine: OpenAI chunks -> Anthropic SSE events

use crate::anthropic;
use crate::openai;
use crate::util;

// Safety cap: prevents unbounded Vec growth if a backend sends a
// malformed chunk with an absurdly large tool_call index.
const MAX_TOOL_CALL_INDEX: usize = 128;

/// State machine that converts OpenAI ChatCompletion chunks into Anthropic SSE events.
///
/// Feed chunks via `process_chunk`, then call `finish` after the OpenAI `[DONE]` sentinel.
/// Each call returns zero or more Anthropic SSE events to forward to the client.
///
/// Anthropic: <https://docs.anthropic.com/en/api/messages-streaming>
/// OpenAI: <https://platform.openai.com/docs/api-reference/chat/streaming>
pub struct StreamingTranslator {
    model: String,
    message_id: String,
    started: bool,
    content_block_index: u32,
    content_block_open: bool,
    /// Tracks whether a thinking content block is open (for reasoning_content
    /// from DeepSeek/Qwen thinking models).
    thinking_block_open: bool,
    /// Tool calls arrive incrementally across multiple chunks, indexed by
    /// position in the OpenAI tool_calls array. We accumulate them here
    /// so we can emit Anthropic's strict Start -> Delta* -> Stop sequence
    /// per tool when finish_reason arrives.
    active_tool_calls: Vec<ToolCallAccumulator>,
    usage: anthropic::Usage,
    finished: bool,
    created: Option<u64>,
}

struct ToolCallAccumulator {
    block_index: u32,
    closed: bool,
}

impl StreamingTranslator {
    /// Create a new streaming translator for the given model.
    ///
    /// Anthropic: <https://docs.anthropic.com/en/api/messages-streaming>
    /// OpenAI: <https://platform.openai.com/docs/api-reference/chat/streaming>
    pub fn new(model: String) -> Self {
        Self {
            model,
            message_id: util::ids::generate_message_id(),
            started: false,
            content_block_index: 0,
            content_block_open: false,
            thinking_block_open: false,
            active_tool_calls: Vec::new(),
            usage: anthropic::Usage::default(),
            finished: false,
            created: None,
        }
    }

    /// Process one OpenAI chunk and return zero or more Anthropic SSE events.
    ///
    /// Anthropic: <https://docs.anthropic.com/en/api/messages-streaming>
    /// OpenAI: <https://platform.openai.com/docs/api-reference/chat/streaming>
    pub fn process_chunk(
        &mut self,
        chunk: &openai::ChatCompletionChunk,
    ) -> Vec<anthropic::StreamEvent> {
        let mut events = Vec::new();

        // Emit message_start on first chunk
        if !self.started {
            self.started = true;
            self.created = chunk.created;
            events.push(self.make_message_start());
        }

        // Capture usage from the final chunk (OpenAI sends it with stream_options.include_usage)
        if let Some(ref usage) = chunk.usage {
            self.usage.input_tokens = usage.prompt_tokens;
            self.usage.output_tokens = usage.completion_tokens;
            self.usage.cache_read_input_tokens = crate::mapping::usage_map::extract_cached_tokens(
                usage.prompt_tokens_details.as_ref(),
            );
        }

        for choice in &chunk.choices {
            // Handle reasoning_content (DeepSeek/Qwen thinking models).
            // Emitted as a separate Anthropic thinking content block before text.
            if let Some(ref reasoning) = choice.delta.reasoning_content {
                if !self.thinking_block_open {
                    events.push(anthropic::StreamEvent::ContentBlockStart {
                        index: self.content_block_index,
                        content_block: anthropic::ContentBlock::Thinking {
                            thinking: String::new(),
                            signature: None,
                        },
                    });
                    self.thinking_block_open = true;
                }
                events.push(anthropic::StreamEvent::ContentBlockDelta {
                    index: self.content_block_index,
                    delta: anthropic::Delta::ThinkingDelta {
                        thinking: reasoning.clone(),
                    },
                });
            }

            // Handle text content deltas
            if let Some(ref text) = choice.delta.content {
                // Close thinking block if transitioning from reasoning to content
                if self.thinking_block_open {
                    events.push(anthropic::StreamEvent::ContentBlockStop {
                        index: self.content_block_index,
                    });
                    self.thinking_block_open = false;
                    self.content_block_index += 1;
                }
                if !self.content_block_open {
                    events.push(anthropic::StreamEvent::ContentBlockStart {
                        index: self.content_block_index,
                        content_block: anthropic::ContentBlock::Text {
                            text: String::new(),
                        },
                    });
                    self.content_block_open = true;
                }
                events.push(anthropic::StreamEvent::ContentBlockDelta {
                    index: self.content_block_index,
                    delta: anthropic::Delta::TextDelta { text: text.clone() },
                });
            }

            // Handle refusals (safety filter triggered during streaming).
            // Anthropic has no refusal type; surface as text so the client sees it.
            if let Some(ref refusal) = choice.delta.refusal {
                if !self.content_block_open {
                    events.push(anthropic::StreamEvent::ContentBlockStart {
                        index: self.content_block_index,
                        content_block: anthropic::ContentBlock::Text {
                            text: String::new(),
                        },
                    });
                    self.content_block_open = true;
                }
                events.push(anthropic::StreamEvent::ContentBlockDelta {
                    index: self.content_block_index,
                    delta: anthropic::Delta::TextDelta {
                        text: super::format_refusal(refusal),
                    },
                });
            }

            // Handle tool call deltas
            if let Some(ref tool_calls) = choice.delta.tool_calls {
                for tc in tool_calls {
                    self.handle_tool_call_delta(tc, &mut events);
                }
            }

            // Handle finish_reason
            if let Some(ref finish_reason) = choice.finish_reason {
                // Close any open thinking block
                if self.thinking_block_open {
                    events.push(anthropic::StreamEvent::ContentBlockStop {
                        index: self.content_block_index,
                    });
                    self.thinking_block_open = false;
                    self.content_block_index += 1;
                }
                // Close any open text content block
                if self.content_block_open {
                    events.push(anthropic::StreamEvent::ContentBlockStop {
                        index: self.content_block_index,
                    });
                    self.content_block_open = false;
                    self.content_block_index += 1;
                }

                // Flush any accumulated tool calls
                self.flush_tool_calls(&mut events);

                // Map OpenAI finish_reason to Anthropic stop_reason
                let stop_reason = map_finish_reason(finish_reason);

                events.push(anthropic::StreamEvent::MessageDelta {
                    delta: anthropic::streaming::MessageDeltaData {
                        stop_reason: Some(stop_reason),
                        stop_sequence: None,
                        ..Default::default()
                    },
                    usage: Some(anthropic::streaming::DeltaUsage {
                        output_tokens: self.usage.output_tokens,
                    }),
                });
            }
        }

        events
    }

    /// Call after all chunks have been processed (when OpenAI sends `[DONE]`).
    ///
    /// Anthropic: <https://docs.anthropic.com/en/api/messages-streaming>
    /// OpenAI: <https://platform.openai.com/docs/api-reference/chat/streaming>
    pub fn finish(&mut self) -> Vec<anthropic::StreamEvent> {
        let mut events = Vec::new();
        if !self.finished {
            self.finished = true;
            events.push(anthropic::StreamEvent::MessageStop {});
        }
        events
    }

    /// Return accumulated usage if any tokens were counted, None otherwise.
    pub fn usage(&self) -> Option<&anthropic::Usage> {
        if self.usage.input_tokens > 0 || self.usage.output_tokens > 0 {
            Some(&self.usage)
        } else {
            None
        }
    }

    fn make_message_start(&self) -> anthropic::StreamEvent {
        anthropic::StreamEvent::MessageStart {
            message: anthropic::streaming::MessageStartData {
                id: self.message_id.clone(),
                msg_type: "message".to_string(),
                role: "assistant".to_string(),
                content: vec![],
                model: self.model.clone(),
                stop_reason: None,
                stop_sequence: None,
                usage: self.usage.clone(),
                created: self.created,
            },
        }
    }

    fn handle_tool_call_delta(
        &mut self,
        tc: &openai::ChunkToolCall,
        events: &mut Vec<anthropic::StreamEvent>,
    ) {
        let idx = tc.index as usize;
        if idx > MAX_TOOL_CALL_INDEX {
            tracing::warn!(
                index = idx,
                "tool call index exceeds maximum ({MAX_TOOL_CALL_INDEX}); skipping"
            );
            return;
        }

        // Determine if this chunk starts a new tool call. OpenAI-compliant backends
        // send `id` on the first chunk; local LLMs may omit `id` but include `name`.
        let has_id = tc.id.is_some();
        let has_name = tc.function.as_ref().and_then(|f| f.name.as_ref()).is_some();
        let is_new_tool = has_id || has_name;

        // Bug 4 guard: if the accumulator at this index is already open (not closed),
        // this is a continuation chunk (e.g., local LLM sending id:"" on every chunk),
        // not a genuinely new tool call.
        let already_active = self.active_tool_calls.get(idx).is_some_and(|tc| !tc.closed);

        if is_new_tool && !already_active {
            // Close any open text content block first
            if self.content_block_open {
                events.push(anthropic::StreamEvent::ContentBlockStop {
                    index: self.content_block_index,
                });
                self.content_block_open = false;
                self.content_block_index += 1;
            }

            // Close the previous tool call block before starting a new one.
            // Anthropic streaming protocol requires sequential: Start -> Delta -> Stop per block.
            if let Some(last_tc) = self.active_tool_calls.last_mut() {
                if !last_tc.closed {
                    events.push(anthropic::StreamEvent::ContentBlockStop {
                        index: last_tc.block_index,
                    });
                    last_tc.closed = true;
                }
            }

            let name = tc
                .function
                .as_ref()
                .and_then(|f| f.name.clone())
                .unwrap_or_default();
            // Skip tool calls with empty name (matches non-streaming behavior).
            if name.is_empty() {
                let id_str = tc.id.as_deref().unwrap_or("<none>");
                tracing::warn!(id = %id_str, "streaming tool call has empty function name; skipping");
                return;
            }

            // Local LLMs may send empty or missing tool call ID
            let tool_id = match tc.id.as_deref() {
                Some(id) if !id.is_empty() => id.to_string(),
                _ => {
                    let synthetic = crate::util::ids::generate_tool_use_id();
                    tracing::warn!(
                        synthetic_id = synthetic,
                        "streaming tool call had empty/missing ID; generated synthetic toolu_ ID"
                    );
                    synthetic
                }
            };

            // OpenAI indexes tool calls within a single chunk (0, 1, 2...);
            // Anthropic uses sequential content block indices across the
            // entire message. Merge the two index spaces by offsetting.
            let block_index = self.content_block_index + idx as u32;

            events.push(anthropic::StreamEvent::ContentBlockStart {
                index: block_index,
                content_block: anthropic::ContentBlock::ToolUse {
                    id: tool_id,
                    name: name.clone(),
                    input: serde_json::Value::Object(serde_json::Map::new()),
                },
            });

            // Grow the accumulator vec to fit this index. OpenAI chunks may
            // report tool calls out of order, so we pre-fill with defaults
            // to avoid index-out-of-bounds, then overwrite at [idx].
            while self.active_tool_calls.len() <= idx {
                self.active_tool_calls.push(ToolCallAccumulator {
                    block_index: 0,
                    closed: true, // Padding: never opened, so must not emit ContentBlockStop
                });
            }
            self.active_tool_calls[idx] = ToolCallAccumulator {
                block_index,
                closed: false,
            };
        }

        // Emit argument fragments as input_json_delta events
        if let Some(ref func) = tc.function {
            if let Some(ref args) = func.arguments {
                if idx < self.active_tool_calls.len() {
                    let block_index = self.active_tool_calls[idx].block_index;
                    events.push(anthropic::StreamEvent::ContentBlockDelta {
                        index: block_index,
                        delta: anthropic::Delta::InputJsonDelta {
                            partial_json: args.clone(),
                        },
                    });
                }
            }
        }
    }

    fn flush_tool_calls(&mut self, events: &mut Vec<anthropic::StreamEvent>) {
        for tc in self.active_tool_calls.drain(..) {
            if !tc.closed {
                events.push(anthropic::StreamEvent::ContentBlockStop {
                    index: tc.block_index,
                });
            }
        }
    }
}

/// Map OpenAI finish_reason to Anthropic stop_reason.
///
/// OpenAI: <https://platform.openai.com/docs/api-reference/chat/object>
/// Anthropic: <https://docs.anthropic.com/en/api/messages>
pub fn map_finish_reason(reason: &openai::FinishReason) -> anthropic::StopReason {
    match reason {
        openai::FinishReason::Stop => anthropic::StopReason::EndTurn,
        openai::FinishReason::Length => anthropic::StopReason::MaxTokens,
        openai::FinishReason::ToolCalls => anthropic::StopReason::ToolUse,
        // Anthropic has no content_filter stop reason; EndTurn is the
        // closest approximation. Refusal text is already surfaced via
        // the refusal handling path above.
        openai::FinishReason::ContentFilter => anthropic::StopReason::EndTurn,
        openai::FinishReason::FunctionCall => anthropic::StopReason::ToolUse,
        // Provider-specific reasons (e.g. DeepSeek "insufficient_system_resource").
        // Log so the unknown value is visible without breaking callers.
        openai::FinishReason::Unknown => {
            tracing::warn!("unknown OpenAI finish_reason received; treating as end_turn");
            anthropic::StopReason::EndTurn
        }
    }
}

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

    /// Helper: build a ChatCompletionChunk with text content.
    fn text_chunk(id: &str, model: &str, text: &str) -> ChatCompletionChunk {
        ChatCompletionChunk {
            id: id.into(),
            object: "chat.completion.chunk".into(),
            model: model.into(),
            choices: vec![ChunkChoice {
                index: 0,
                delta: ChunkDelta {
                    role: None,
                    content: Some(text.into()),
                    refusal: None,
                    tool_calls: None,
                    reasoning_content: None,
                },
                finish_reason: None,
                logprobs: None,
            }],
            usage: None,
            created: None,
            system_fingerprint: None,
        }
    }

    /// Helper: build a chunk with only a role delta (first chunk from OpenAI).
    fn role_chunk(id: &str, model: &str) -> ChatCompletionChunk {
        ChatCompletionChunk {
            id: id.into(),
            object: "chat.completion.chunk".into(),
            model: model.into(),
            choices: vec![ChunkChoice {
                index: 0,
                delta: ChunkDelta {
                    role: Some(crate::openai::ChatRole::Assistant),
                    content: None,
                    refusal: None,
                    tool_calls: None,
                    reasoning_content: None,
                },
                finish_reason: None,
                logprobs: None,
            }],
            usage: None,
            created: None,
            system_fingerprint: None,
        }
    }

    /// Helper: build a chunk with finish_reason.
    fn finish_chunk(
        id: &str,
        model: &str,
        reason: crate::openai::FinishReason,
    ) -> ChatCompletionChunk {
        ChatCompletionChunk {
            id: id.into(),
            object: "chat.completion.chunk".into(),
            model: model.into(),
            choices: vec![ChunkChoice {
                index: 0,
                delta: ChunkDelta::default(),
                finish_reason: Some(reason),
                logprobs: None,
            }],
            usage: None,
            created: None,
            system_fingerprint: None,
        }
    }

    /// Helper: build a chunk with usage info (no choices).
    fn usage_chunk(id: &str, model: &str, prompt: u32, completion: u32) -> ChatCompletionChunk {
        ChatCompletionChunk {
            id: id.into(),
            object: "chat.completion.chunk".into(),
            model: model.into(),
            choices: vec![],
            usage: Some(crate::openai::ChatUsage {
                prompt_tokens: prompt,
                completion_tokens: completion,
                total_tokens: prompt + completion,
                completion_tokens_details: None,
                prompt_tokens_details: None,
            }),
            created: None,
            system_fingerprint: None,
        }
    }

    /// Helper: build a chunk with a tool call delta.
    fn tool_call_chunk(
        id_str: &str,
        model: &str,
        tc_index: u32,
        tc_id: Option<&str>,
        name: Option<&str>,
        args: Option<&str>,
    ) -> ChatCompletionChunk {
        ChatCompletionChunk {
            id: id_str.into(),
            object: "chat.completion.chunk".into(),
            model: model.into(),
            choices: vec![ChunkChoice {
                index: 0,
                delta: ChunkDelta {
                    role: None,
                    content: None,
                    refusal: None,
                    tool_calls: Some(vec![ChunkToolCall {
                        index: tc_index,
                        id: tc_id.map(Into::into),
                        call_type: tc_id.map(|_| "function".into()),
                        function: Some(ChunkFunctionCall {
                            name: name.map(Into::into),
                            arguments: args.map(Into::into),
                        }),
                    }]),
                    reasoning_content: None,
                },
                finish_reason: None,
                logprobs: None,
            }],
            usage: None,
            created: None,
            system_fingerprint: None,
        }
    }

    #[test]
    fn first_chunk_emits_message_start() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        let chunk = role_chunk("chatcmpl-1", "gpt-4o");
        let events = translator.process_chunk(&chunk);

        assert_eq!(events.len(), 1);
        match &events[0] {
            anthropic::StreamEvent::MessageStart { message } => {
                assert!(message.id.starts_with("msg_"));
                assert_eq!(message.model, "gpt-4o");
                assert_eq!(message.role, "assistant");
                assert!(message.content.is_empty());
                assert!(message.stop_reason.is_none());
            }
            other => panic!("expected MessageStart, got {:?}", other),
        }
    }

    #[test]
    fn text_chunks_emit_block_start_and_deltas() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());

        // First text chunk: should emit message_start + content_block_start + delta
        let events = translator.process_chunk(&text_chunk("c1", "gpt-4o", "Hello"));
        assert_eq!(events.len(), 3);
        assert!(matches!(
            &events[0],
            anthropic::StreamEvent::MessageStart { .. }
        ));
        assert!(matches!(
            &events[1],
            anthropic::StreamEvent::ContentBlockStart { index: 0, .. }
        ));
        match &events[2] {
            anthropic::StreamEvent::ContentBlockDelta {
                index: 0,
                delta: anthropic::Delta::TextDelta { text },
            } => assert_eq!(text, "Hello"),
            other => panic!("expected TextDelta, got {:?}", other),
        }

        // Second text chunk: only delta (no message_start, no block_start)
        let events = translator.process_chunk(&text_chunk("c1", "gpt-4o", " world"));
        assert_eq!(events.len(), 1);
        match &events[0] {
            anthropic::StreamEvent::ContentBlockDelta {
                index: 0,
                delta: anthropic::Delta::TextDelta { text },
            } => assert_eq!(text, " world"),
            other => panic!("expected TextDelta, got {:?}", other),
        }
    }

    #[test]
    fn finish_reason_stop_emits_block_stop_and_message_delta() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&text_chunk("c1", "gpt-4o", "Hi"));

        let events =
            translator.process_chunk(&finish_chunk("c1", "gpt-4o", openai::FinishReason::Stop));

        // Should emit: ContentBlockStop, MessageDelta
        assert_eq!(events.len(), 2);
        assert!(matches!(
            &events[0],
            anthropic::StreamEvent::ContentBlockStop { index: 0 }
        ));
        match &events[1] {
            anthropic::StreamEvent::MessageDelta { delta, usage } => {
                assert_eq!(delta.stop_reason, Some(anthropic::StopReason::EndTurn));
                assert!(usage.is_some());
            }
            other => panic!("expected MessageDelta, got {:?}", other),
        }
    }

    #[test]
    fn finish_emits_message_stop() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&text_chunk("c1", "gpt-4o", "Hi"));

        let events = translator.finish();
        assert_eq!(events.len(), 1);
        assert!(matches!(&events[0], anthropic::StreamEvent::MessageStop {}));

        // Calling finish again should produce nothing
        let events = translator.finish();
        assert!(events.is_empty());
    }

    #[test]
    fn usage_chunk_updates_token_counts() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&text_chunk("c1", "gpt-4o", "Hi"));
        translator.process_chunk(&usage_chunk("c1", "gpt-4o", 10, 5));

        let events =
            translator.process_chunk(&finish_chunk("c1", "gpt-4o", openai::FinishReason::Stop));

        // The MessageDelta should carry the usage from the usage chunk
        let msg_delta = events
            .iter()
            .find(|e| matches!(e, anthropic::StreamEvent::MessageDelta { .. }));
        match msg_delta {
            Some(anthropic::StreamEvent::MessageDelta { usage, .. }) => {
                let u = usage.as_ref().unwrap();
                assert_eq!(u.output_tokens, 5);
            }
            other => panic!("expected MessageDelta with usage, got {:?}", other),
        }
    }

    #[test]
    fn tool_call_chunks_emit_tool_use_events() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&role_chunk("c1", "gpt-4o"));

        // First tool call chunk: has id + name + partial args
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "gpt-4o",
            0,
            Some("call_abc"),
            Some("get_weather"),
            Some("{\"loc"),
        ));

        // Should emit ContentBlockStart (tool_use) + ContentBlockDelta (input_json_delta)
        assert_eq!(events.len(), 2);
        match &events[0] {
            anthropic::StreamEvent::ContentBlockStart {
                index: 0,
                content_block,
            } => match content_block {
                anthropic::ContentBlock::ToolUse { id, name, .. } => {
                    assert_eq!(id, "call_abc");
                    assert_eq!(name, "get_weather");
                }
                other => panic!("expected ToolUse content block, got {:?}", other),
            },
            other => panic!("expected ContentBlockStart, got {:?}", other),
        }
        match &events[1] {
            anthropic::StreamEvent::ContentBlockDelta {
                index: 0,
                delta: anthropic::Delta::InputJsonDelta { partial_json },
            } => assert_eq!(partial_json, "{\"loc"),
            other => panic!("expected InputJsonDelta, got {:?}", other),
        }

        // Continuation chunk: more args
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "gpt-4o",
            0,
            None,
            None,
            Some("ation\": \"NYC\"}"),
        ));
        assert_eq!(events.len(), 1);
        match &events[0] {
            anthropic::StreamEvent::ContentBlockDelta {
                index: 0,
                delta: anthropic::Delta::InputJsonDelta { partial_json },
            } => assert_eq!(partial_json, "ation\": \"NYC\"}"),
            other => panic!("expected InputJsonDelta, got {:?}", other),
        }
    }

    #[test]
    fn tool_call_finish_flushes_and_emits_stop() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&role_chunk("c1", "gpt-4o"));
        translator.process_chunk(&tool_call_chunk(
            "c1",
            "gpt-4o",
            0,
            Some("call_abc"),
            Some("get_weather"),
            Some("{\"location\": \"NYC\"}"),
        ));

        let events = translator.process_chunk(&finish_chunk(
            "c1",
            "gpt-4o",
            openai::FinishReason::ToolCalls,
        ));

        // Should emit: ContentBlockStop (for tool call), MessageDelta
        assert_eq!(events.len(), 2);
        assert!(matches!(
            &events[0],
            anthropic::StreamEvent::ContentBlockStop { index: 0 }
        ));
        match &events[1] {
            anthropic::StreamEvent::MessageDelta { delta, .. } => {
                assert_eq!(delta.stop_reason, Some(anthropic::StopReason::ToolUse));
            }
            other => panic!("expected MessageDelta, got {:?}", other),
        }
    }

    #[test]
    fn text_then_tool_call_closes_text_block() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());

        // Text content first
        translator.process_chunk(&text_chunk("c1", "gpt-4o", "Let me check"));

        // Then a tool call arrives: should close text block first
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "gpt-4o",
            0,
            Some("call_xyz"),
            Some("search"),
            Some("{}"),
        ));

        // ContentBlockStop (text, index 0), ContentBlockStart (tool, index 1), ContentBlockDelta
        assert_eq!(events.len(), 3);
        assert!(matches!(
            &events[0],
            anthropic::StreamEvent::ContentBlockStop { index: 0 }
        ));
        match &events[1] {
            anthropic::StreamEvent::ContentBlockStart {
                index: 1,
                content_block: anthropic::ContentBlock::ToolUse { id, .. },
            } => assert_eq!(id, "call_xyz"),
            other => panic!(
                "expected ContentBlockStart for tool_use at index 1, got {:?}",
                other
            ),
        }
    }

    #[test]
    fn empty_choices_chunk_only_emits_message_start() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        let chunk = ChatCompletionChunk {
            id: "c1".into(),
            object: "chat.completion.chunk".into(),
            model: "gpt-4o".into(),
            choices: vec![],
            usage: None,
            created: None,
            system_fingerprint: None,
        };
        let events = translator.process_chunk(&chunk);
        // Only message_start on first call
        assert_eq!(events.len(), 1);
        assert!(matches!(
            &events[0],
            anthropic::StreamEvent::MessageStart { .. }
        ));

        // Subsequent empty chunk: no events
        let events = translator.process_chunk(&chunk);
        assert!(events.is_empty());
    }

    #[test]
    fn map_finish_reason_length() {
        assert_eq!(
            map_finish_reason(&openai::FinishReason::Length),
            anthropic::StopReason::MaxTokens
        );
    }

    #[test]
    fn map_finish_reason_content_filter() {
        // Content filter maps to EndTurn (best approximation)
        assert_eq!(
            map_finish_reason(&openai::FinishReason::ContentFilter),
            anthropic::StopReason::EndTurn
        );
    }

    #[test]
    fn full_text_stream_sequence() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());

        // Simulate a complete text streaming sequence
        let mut all_events = Vec::new();
        all_events.extend(translator.process_chunk(&role_chunk("c1", "gpt-4o")));
        all_events.extend(translator.process_chunk(&text_chunk("c1", "gpt-4o", "Hello")));
        all_events.extend(translator.process_chunk(&text_chunk("c1", "gpt-4o", " world")));
        all_events.extend(translator.process_chunk(&usage_chunk("c1", "gpt-4o", 10, 5)));
        all_events.extend(translator.process_chunk(&finish_chunk(
            "c1",
            "gpt-4o",
            openai::FinishReason::Stop,
        )));
        all_events.extend(translator.finish());

        // Verify event sequence: MessageStart, ContentBlockStart, TextDelta, TextDelta,
        //   ContentBlockStop, MessageDelta, MessageStop
        let types: Vec<&str> = all_events
            .iter()
            .map(|e| match e {
                anthropic::StreamEvent::MessageStart { .. } => "message_start",
                anthropic::StreamEvent::ContentBlockStart { .. } => "content_block_start",
                anthropic::StreamEvent::ContentBlockDelta { .. } => "content_block_delta",
                anthropic::StreamEvent::ContentBlockStop { .. } => "content_block_stop",
                anthropic::StreamEvent::MessageDelta { .. } => "message_delta",
                anthropic::StreamEvent::MessageStop {} => "message_stop",
                anthropic::StreamEvent::Ping {} => "ping",
                anthropic::StreamEvent::Error { .. } => "error",
                _ => "unknown",
            })
            .collect();

        assert_eq!(
            types,
            vec![
                "message_start",
                "content_block_start",
                "content_block_delta",
                "content_block_delta",
                "content_block_stop",
                "message_delta",
                "message_stop",
            ]
        );
    }

    // --- Local LLM robustness ---

    #[test]
    fn streaming_tool_call_empty_id_gets_synthetic() {
        let mut translator = StreamingTranslator::new("llama".into());
        translator.process_chunk(&role_chunk("c1", "llama"));

        // First tool call chunk with empty string ID
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "llama",
            0,
            Some(""), // empty ID from local LLM
            Some("Read"),
            Some("{\"file"),
        ));

        assert_eq!(events.len(), 2);
        match &events[0] {
            anthropic::StreamEvent::ContentBlockStart {
                content_block: anthropic::ContentBlock::ToolUse { id, name, .. },
                ..
            } => {
                assert!(
                    id.starts_with("toolu_"),
                    "expected synthetic toolu_ ID, got: {}",
                    id
                );
                assert_eq!(name, "Read");
            }
            other => panic!("expected ContentBlockStart with ToolUse, got {:?}", other),
        }
    }

    #[test]
    fn streaming_tool_call_empty_name_skipped() {
        let mut translator = StreamingTranslator::new("llama".into());
        translator.process_chunk(&role_chunk("c1", "llama"));

        // Tool call chunk with no name should be skipped (consistent with non-streaming).
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "llama",
            0,
            Some("call_1"),
            None, // no name from local LLM
            Some("{}"),
        ));

        // Empty name causes early return -- no ContentBlockStart emitted.
        assert!(
            events.iter().all(|e| !matches!(
                e,
                anthropic::StreamEvent::ContentBlockStart {
                    content_block: anthropic::ContentBlock::ToolUse { .. },
                    ..
                }
            )),
            "tool call with empty name should be skipped, got: {:?}",
            events
        );
    }

    #[test]
    fn streaming_tool_call_none_id_with_name_gets_synthetic() {
        // Bug 3: local LLMs may omit id entirely but provide name
        let mut translator = StreamingTranslator::new("llama".into());
        translator.process_chunk(&role_chunk("c1", "llama"));

        // First chunk: id is None, but name is present
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "llama",
            0,
            None, // no ID at all
            Some("get_weather"),
            Some("{\"loc"),
        ));

        assert_eq!(
            events.len(),
            2,
            "expected ContentBlockStart + ContentBlockDelta"
        );
        match &events[0] {
            anthropic::StreamEvent::ContentBlockStart {
                content_block: anthropic::ContentBlock::ToolUse { id, name, .. },
                ..
            } => {
                assert!(
                    id.starts_with("toolu_"),
                    "expected synthetic toolu_ ID, got: {}",
                    id
                );
                assert_eq!(name, "get_weather");
            }
            other => panic!("expected ContentBlockStart with ToolUse, got {:?}", other),
        }

        // Second chunk: continuation with more arguments (no id, no name)
        let events2 = translator.process_chunk(&tool_call_chunk(
            "c1",
            "llama",
            0,
            None,
            None,
            Some("ation\"}"),
        ));

        assert_eq!(
            events2.len(),
            1,
            "expected only ContentBlockDelta for continuation"
        );
        assert!(matches!(
            &events2[0],
            anthropic::StreamEvent::ContentBlockDelta {
                delta: anthropic::Delta::InputJsonDelta { partial_json },
                ..
            } if partial_json == "ation\"}"
        ));
    }

    #[test]
    fn streaming_tool_call_repeated_empty_id_not_corrupted() {
        // Bug 4: backend sends id:"" on every chunk of the same tool call;
        // only the first chunk should open a new block.
        let mut translator = StreamingTranslator::new("llama".into());
        translator.process_chunk(&role_chunk("c1", "llama"));

        // First chunk with empty id + name: opens a new tool block
        let events1 = translator.process_chunk(&tool_call_chunk(
            "c1",
            "llama",
            0,
            Some(""),
            Some("Read"),
            Some("{\"f"),
        ));
        assert_eq!(
            events1.len(),
            2,
            "expected ContentBlockStart + ContentBlockDelta"
        );
        let first_id = match &events1[0] {
            anthropic::StreamEvent::ContentBlockStart {
                content_block: anthropic::ContentBlock::ToolUse { id, .. },
                ..
            } => id.clone(),
            other => panic!("expected ContentBlockStart, got {:?}", other),
        };

        // Second chunk with empty id again: should NOT open a new block
        let events2 = translator.process_chunk(&tool_call_chunk(
            "c1",
            "llama",
            0,
            Some(""),
            None,
            Some("ile\"}"),
        ));

        // Should only have the argument delta, no new ContentBlockStart
        assert_eq!(
            events2.len(),
            1,
            "repeated empty id should not re-open block"
        );
        assert!(
            matches!(
                &events2[0],
                anthropic::StreamEvent::ContentBlockDelta { .. }
            ),
            "expected ContentBlockDelta, got {:?}",
            events2[0]
        );

        // Verify no second synthetic ID was generated (only one ContentBlockStart total)
        let all_starts: Vec<_> = events1
            .iter()
            .chain(events2.iter())
            .filter(|e| matches!(e, anthropic::StreamEvent::ContentBlockStart { .. }))
            .collect();
        assert_eq!(
            all_starts.len(),
            1,
            "should have exactly one ContentBlockStart, got {}",
            all_starts.len()
        );
        // The synthetic ID from the first chunk should be used
        assert!(first_id.starts_with("toolu_"));
    }

    #[test]
    fn streaming_refusal_emits_text_delta() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        let chunk = ChatCompletionChunk {
            id: "chatcmpl-1".into(),
            object: "chat.completion.chunk".into(),
            model: "gpt-4o".into(),
            choices: vec![ChunkChoice {
                index: 0,
                delta: ChunkDelta {
                    role: None,
                    content: None,
                    refusal: Some("content policy violation".into()),
                    tool_calls: None,
                    reasoning_content: None,
                },
                finish_reason: None,
                logprobs: None,
            }],
            usage: None,
            created: None,
            system_fingerprint: None,
        };
        let events = translator.process_chunk(&chunk);
        // message_start + content_block_start + content_block_delta
        assert!(
            events.len() >= 3,
            "expected at least 3 events, got {}",
            events.len()
        );
        match &events[events.len() - 1] {
            anthropic::StreamEvent::ContentBlockDelta {
                delta: anthropic::Delta::TextDelta { text },
                ..
            } => {
                assert!(
                    text.contains("Refusal"),
                    "expected refusal text, got: {}",
                    text
                );
                assert!(text.contains("content policy violation"));
            }
            other => panic!("expected TextDelta with refusal, got {:?}", other),
        }
    }

    /// Helper: build a chunk with reasoning_content (DeepSeek/Qwen thinking).
    fn reasoning_chunk(id: &str, model: &str, reasoning: &str) -> ChatCompletionChunk {
        ChatCompletionChunk {
            id: id.into(),
            object: "chat.completion.chunk".into(),
            model: model.into(),
            choices: vec![ChunkChoice {
                index: 0,
                delta: ChunkDelta {
                    role: None,
                    content: None,
                    refusal: None,
                    tool_calls: None,
                    reasoning_content: Some(reasoning.into()),
                },
                finish_reason: None,
                logprobs: None,
            }],
            usage: None,
            created: None,
            system_fingerprint: None,
        }
    }

    #[test]
    fn reasoning_content_emits_thinking_block() {
        let mut translator = StreamingTranslator::new("deepseek-reasoner".into());

        // First reasoning chunk should open a thinking block
        let events =
            translator.process_chunk(&reasoning_chunk("c1", "deepseek-reasoner", "Let me"));
        assert_eq!(events.len(), 3); // message_start + content_block_start + thinking_delta

        match &events[1] {
            anthropic::StreamEvent::ContentBlockStart {
                index,
                content_block,
            } => {
                assert_eq!(*index, 0);
                assert!(matches!(
                    content_block,
                    anthropic::ContentBlock::Thinking { .. }
                ));
            }
            other => panic!("expected ContentBlockStart, got {:?}", other),
        }
        match &events[2] {
            anthropic::StreamEvent::ContentBlockDelta { index, delta } => {
                assert_eq!(*index, 0);
                match delta {
                    anthropic::Delta::ThinkingDelta { thinking } => {
                        assert_eq!(thinking, "Let me");
                    }
                    other => panic!("expected ThinkingDelta, got {:?}", other),
                }
            }
            other => panic!("expected ContentBlockDelta, got {:?}", other),
        }

        // Second reasoning chunk continues the thinking block
        let events =
            translator.process_chunk(&reasoning_chunk("c1", "deepseek-reasoner", " think..."));
        assert_eq!(events.len(), 1); // just a thinking delta
        match &events[0] {
            anthropic::StreamEvent::ContentBlockDelta { delta, .. } => {
                assert!(
                    matches!(delta, anthropic::Delta::ThinkingDelta { thinking } if thinking == " think...")
                );
            }
            other => panic!("expected ThinkingDelta, got {:?}", other),
        }

        // Text chunk should close thinking block and open text block
        let events = translator.process_chunk(&text_chunk("c1", "deepseek-reasoner", "Answer: 4"));
        assert_eq!(events.len(), 3); // content_block_stop (thinking) + content_block_start (text) + text_delta

        assert!(
            matches!(&events[0], anthropic::StreamEvent::ContentBlockStop { index } if *index == 0)
        );
        assert!(matches!(
            &events[1],
            anthropic::StreamEvent::ContentBlockStart { index: 1, .. }
        ));
        match &events[2] {
            anthropic::StreamEvent::ContentBlockDelta { index, delta } => {
                assert_eq!(*index, 1);
                assert!(
                    matches!(delta, anthropic::Delta::TextDelta { text } if text == "Answer: 4")
                );
            }
            other => panic!("expected TextDelta, got {:?}", other),
        }

        // Finish
        let events = translator.process_chunk(&finish_chunk(
            "c1",
            "deepseek-reasoner",
            openai::FinishReason::Stop,
        ));
        // content_block_stop (text) + message_delta
        assert_eq!(events.len(), 2);
    }

    #[test]
    fn reasoning_only_without_text_content() {
        // Some thinking models may return only reasoning_content with no text content
        let mut translator = StreamingTranslator::new("deepseek-reasoner".into());
        translator.process_chunk(&reasoning_chunk("c1", "deepseek-reasoner", "Thinking..."));

        let events = translator.process_chunk(&finish_chunk(
            "c1",
            "deepseek-reasoner",
            openai::FinishReason::Stop,
        ));
        // Should close thinking block + message_delta
        assert_eq!(events.len(), 2);
        assert!(
            matches!(&events[0], anthropic::StreamEvent::ContentBlockStop { index } if *index == 0)
        );
    }

    #[test]
    fn usage_chunk_with_cached_tokens_maps_cache_read() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        let chunk = ChatCompletionChunk {
            id: "c1".into(),
            object: "chat.completion.chunk".into(),
            model: "gpt-4o".into(),
            choices: vec![],
            usage: Some(crate::openai::ChatUsage {
                prompt_tokens: 100,
                completion_tokens: 50,
                total_tokens: 150,
                completion_tokens_details: None,
                prompt_tokens_details: Some(serde_json::json!({"cached_tokens": 42})),
            }),
            created: None,
            system_fingerprint: None,
        };
        translator.process_chunk(&chunk);
        let usage = translator.usage().expect("usage should be present");
        assert_eq!(usage.input_tokens, 100);
        assert_eq!(usage.output_tokens, 50);
        assert_eq!(usage.cache_read_input_tokens, Some(42));
    }

    #[test]
    fn usage_chunk_without_cached_tokens_leaves_cache_read_none() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&usage_chunk("c1", "gpt-4o", 10, 5));
        let usage = translator.usage().expect("usage should be present");
        assert!(usage.cache_read_input_tokens.is_none());
    }

    // Boundary: idx == MAX_TOOL_CALL_INDEX (128) uses `>` not `>=`, so 128 is accepted.
    #[test]
    fn tool_call_at_max_index_is_accepted() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&role_chunk("c1", "gpt-4o"));

        // idx == 128: 128 > 128 is false, so the chunk is processed.
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "gpt-4o",
            MAX_TOOL_CALL_INDEX as u32,
            Some("call_at_max"),
            Some("my_tool"),
            Some("{}"),
        ));

        // Must emit at least a ContentBlockStart for the tool call.
        let has_tool_start = events.iter().any(|e| {
            matches!(
                e,
                anthropic::StreamEvent::ContentBlockStart {
                    content_block: anthropic::ContentBlock::ToolUse { .. },
                    ..
                }
            )
        });
        assert!(
            has_tool_start,
            "expected ContentBlockStart for tool_use at index {MAX_TOOL_CALL_INDEX}, got {events:?}"
        );
    }

    // Boundary: idx == MAX_TOOL_CALL_INDEX + 1 (129) satisfies `>`, so it is dropped.
    #[test]
    fn tool_call_above_max_index_is_dropped() {
        let mut translator = StreamingTranslator::new("gpt-4o".into());
        translator.process_chunk(&role_chunk("c1", "gpt-4o"));

        // idx == 129: 129 > 128 is true, so the chunk is silently skipped.
        let events = translator.process_chunk(&tool_call_chunk(
            "c1",
            "gpt-4o",
            (MAX_TOOL_CALL_INDEX + 1) as u32,
            Some("call_over_max"),
            Some("bad_tool"),
            Some("{}"),
        ));

        // No tool-related events should be emitted for this chunk.
        let has_tool_event = events.iter().any(|e| {
            matches!(
                e,
                anthropic::StreamEvent::ContentBlockStart {
                    content_block: anthropic::ContentBlock::ToolUse { .. },
                    ..
                } | anthropic::StreamEvent::ContentBlockDelta {
                    delta: anthropic::Delta::InputJsonDelta { .. },
                    ..
                }
            )
        });
        assert!(
            !has_tool_event,
            "expected no tool events for index > {MAX_TOOL_CALL_INDEX}, got {events:?}"
        );
    }
}