tandem-providers 0.6.2

Provider integrations for Tandem
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
#[cfg(test)]
mod tests {
    use super::*;
    use futures::StreamExt;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;
    use tokio::sync::oneshot;

    fn cfg(
        provider_ids: &[&str],
        default_provider: Option<&str>,
        include_openai_key: bool,
    ) -> AppConfig {
        let mut providers = HashMap::new();
        for id in provider_ids {
            let api_key = if *id == "openai" && include_openai_key {
                Some("sk-test".to_string())
            } else {
                None
            };
            providers.insert(
                (*id).to_string(),
                ProviderConfig {
                    api_key,
                    url: None,
                    default_model: Some(format!("{id}-model")),
                },
            );
        }
        AppConfig {
            providers,
            default_provider: default_provider.map(|s| s.to_string()),
        }
    }

    #[tokio::test]
    async fn explicit_provider_wins_over_default_provider() {
        let registry = ProviderRegistry::new(cfg(&["openai", "openrouter"], Some("openai"), true));
        let provider = registry
            .select_provider(Some("openrouter"))
            .await
            .expect("provider");
        assert_eq!(provider.info().id, "openrouter");
    }

    #[tokio::test]
    async fn uses_default_provider_when_explicit_provider_missing() {
        let registry =
            ProviderRegistry::new(cfg(&["openai", "openrouter"], Some("openrouter"), true));
        let provider = registry.select_provider(None).await.expect("provider");
        assert_eq!(provider.info().id, "openrouter");
    }

    #[tokio::test]
    async fn falls_back_to_first_provider_when_default_provider_missing() {
        let registry = ProviderRegistry::new(cfg(&["openai"], Some("anthropic"), true));
        let provider = registry.select_provider(None).await.expect("provider");
        assert_eq!(provider.info().id, "openai");
    }

    #[tokio::test]
    async fn explicit_unknown_provider_errors() {
        let registry = ProviderRegistry::new(cfg(&["openai"], None, true));
        let err = registry
            .select_provider(Some("openruter"))
            .await
            .err()
            .expect("expected error");
        assert!(err
            .to_string()
            .contains("provider `openruter` is not configured"));
    }

    #[tokio::test]
    async fn custom_provider_id_is_supported_from_config() {
        let registry = ProviderRegistry::new(cfg(&["custom"], Some("custom"), false));
        let provider = registry
            .select_provider(Some("custom"))
            .await
            .expect("provider");
        assert_eq!(provider.info().id, "custom");
    }

    #[test]
    fn normalize_base_handles_common_openai_compatible_inputs() {
        assert_eq!(
            normalize_base("http://localhost:8080"),
            "http://localhost:8080/v1"
        );
        assert_eq!(
            normalize_base("http://localhost:8080/v1"),
            "http://localhost:8080/v1"
        );
        assert_eq!(
            normalize_base("http://localhost:8080/v1/"),
            "http://localhost:8080/v1"
        );
        assert_eq!(
            normalize_base("http://localhost:8080/v1/chat/completions"),
            "http://localhost:8080/v1"
        );
        assert_eq!(
            normalize_base("http://localhost:8080/v1/models"),
            "http://localhost:8080/v1"
        );
        assert_eq!(
            normalize_base("http://localhost:8080/v1/v1"),
            "http://localhost:8080/v1"
        );
    }

    #[test]
    fn normalize_openai_messages_merges_system_messages_to_front() {
        let normalized = normalize_openai_messages(vec![
            ChatMessage {
                role: "system".to_string(),
                content: "base instructions".to_string(),
                attachments: Vec::new(),
            },
            ChatMessage {
                role: "user".to_string(),
                content: "hi".to_string(),
                attachments: Vec::new(),
            },
            ChatMessage {
                role: "system".to_string(),
                content: "memory scope".to_string(),
                attachments: Vec::new(),
            },
            ChatMessage {
                role: "assistant".to_string(),
                content: "hello".to_string(),
                attachments: Vec::new(),
            },
        ]);

        assert_eq!(normalized.len(), 3);
        assert_eq!(normalized[0].role, "system");
        assert_eq!(normalized[0].content, "base instructions\n\nmemory scope");
        assert_eq!(normalized[1].role, "user");
        assert_eq!(normalized[2].role, "assistant");
    }

    #[test]
    fn normalize_openai_messages_leaves_non_system_order_unchanged() {
        let normalized = normalize_openai_messages(vec![
            ChatMessage {
                role: "user".to_string(),
                content: "hi".to_string(),
                attachments: Vec::new(),
            },
            ChatMessage {
                role: "assistant".to_string(),
                content: "hello".to_string(),
                attachments: Vec::new(),
            },
        ]);

        assert_eq!(normalized.len(), 2);
        assert_eq!(normalized[0].role, "user");
        assert_eq!(normalized[1].role, "assistant");
    }

    #[tokio::test]
    async fn complete_cheapest_picks_ollama_first() {
        // Test priority parsing logic
        let registry = ProviderRegistry::new(cfg(&["openai", "groq", "ollama"], None, true));
        let cheapest = registry.select_cheapest_provider_id().await;
        assert_eq!(cheapest, Some("ollama"));

        let registry = ProviderRegistry::new(cfg(&["openai", "openai", "openrouter"], None, true));
        let cheapest = registry.select_cheapest_provider_id().await;
        assert_eq!(cheapest, Some("openrouter"));

        let registry = ProviderRegistry::new(cfg(&["unknown_provider"], None, true));
        let cheapest = registry.select_cheapest_provider_id().await;
        assert_eq!(cheapest, None);
    }

    #[test]
    fn sanitize_openai_function_name_rewrites_invalid_chars() {
        assert_eq!(
            sanitize_openai_function_name("mcp.arcade.gmail_sendemail"),
            "mcp_arcade_gmail_sendemail"
        );
        assert_eq!(sanitize_openai_function_name("  "), "tool");
        assert_eq!(
            sanitize_openai_function_name("clickup-getSpaces"),
            "clickup-getSpaces"
        );
    }

    #[test]
    fn build_openai_tool_aliases_preserves_roundtrip_and_uniqueness() {
        let tools = vec![
            ToolSchema::new("mcp.arcade.gmail.send", "a", json!({"type":"object"})),
            ToolSchema::new("mcp_arcade_gmail_send", "b", json!({"type":"object"})),
        ];
        let (forward, reverse) = build_openai_tool_aliases(&tools);
        let alias_a = forward
            .get("mcp.arcade.gmail.send")
            .expect("alias for dotted name");
        let alias_b = forward
            .get("mcp_arcade_gmail_send")
            .expect("alias for underscore name");
        assert_ne!(alias_a, alias_b, "aliases must be unique");
        assert_eq!(
            reverse.get(alias_a).map(String::as_str),
            Some("mcp.arcade.gmail.send")
        );
        assert_eq!(
            reverse.get(alias_b).map(String::as_str),
            Some("mcp_arcade_gmail_send")
        );
    }

    fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
        if needle.is_empty() {
            return Some(0);
        }
        haystack
            .windows(needle.len())
            .position(|window| window == needle)
    }

    async fn read_single_http_request(
        socket: &mut tokio::net::TcpStream,
    ) -> (String, String, String) {
        let mut buffer = Vec::new();
        let header_end = loop {
            let mut chunk = [0u8; 1024];
            let read = socket.read(&mut chunk).await.expect("read request");
            assert!(
                read > 0,
                "connection closed before request headers were read"
            );
            buffer.extend_from_slice(&chunk[..read]);
            if let Some(pos) = find_subsequence(&buffer, b"\r\n\r\n") {
                break pos + 4;
            }
        };

        let headers = String::from_utf8(buffer[..header_end].to_vec()).expect("utf8 headers");
        let content_length = headers
            .lines()
            .find_map(|line| {
                line.strip_prefix("Content-Length: ")
                    .or_else(|| line.strip_prefix("content-length: "))
            })
            .and_then(|value| value.trim().parse::<usize>().ok())
            .unwrap_or(0);

        let mut body = buffer[header_end..].to_vec();
        while body.len() < content_length {
            let mut chunk = [0u8; 1024];
            let read = socket.read(&mut chunk).await.expect("read request body");
            if read == 0 {
                break;
            }
            body.extend_from_slice(&chunk[..read]);
        }

        let request_line = headers.lines().next().unwrap_or("").to_string();
        let body = String::from_utf8(body).expect("utf8 body");
        (request_line, headers, body)
    }

    #[tokio::test]
    async fn openai_codex_stream_uses_responses_transport() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener address");
        let (tx, rx) = oneshot::channel();

        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept connection");
            let request = read_single_http_request(&mut socket).await;
            let response_body = concat!(
                "data: {\"type\":\"response.output_text.delta\",\"item_id\":\"msg_1\",\"delta\":\"Hello\"}\n\n",
                "data: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":5,\"output_tokens\":7,\"total_tokens\":12}}}\n\n",
                "data: [DONE]\n\n"
            );
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                response_body.as_bytes().len(),
                response_body
            );
            socket
                .write_all(response.as_bytes())
                .await
                .expect("write response");
            socket.shutdown().await.expect("shutdown socket");
            tx.send(request).expect("send request");
        });

        let provider = OpenAIResponsesProvider {
            id: "openai-codex".to_string(),
            name: "OpenAI Codex".to_string(),
            base_url: format!("http://{}/codex", addr),
            api_key: Some("codex-test-token".to_string()),
            default_model: "gpt-5.5".to_string(),
            models: codex_supported_models(272_000),
            client: Client::new(),
        };

        let messages = vec![
            ChatMessage {
                role: "system".to_string(),
                content: "Be concise.".to_string(),
                attachments: Vec::new(),
            },
            ChatMessage {
                role: "user".to_string(),
                content: "hi".to_string(),
                attachments: Vec::new(),
            },
        ];
        let tools = vec![ToolSchema::new(
            "browser_wait",
            "Wait for a selector.",
            json!({
                "type": "object",
                "properties": {
                    "session_id": { "type": "string" },
                    "selector": { "type": "string" }
                },
                "required": ["session_id"],
                "anyOf": [
                    { "required": ["selector"] }
                ]
            }),
        )];
        let cancel = CancellationToken::new();
        let stream = provider
            .stream(messages, None, ToolMode::Auto, Some(tools), SamplingParams::default(), cancel)
            .await
            .expect("stream");

        let mut chunks = Vec::new();
        futures::pin_mut!(stream);
        while let Some(chunk) = stream.next().await {
            let chunk = chunk.expect("stream chunk");
            let is_done = matches!(chunk, StreamChunk::Done { .. });
            chunks.push(chunk);
            if is_done {
                break;
            }
        }

        let (request_line, headers, body) = rx.await.expect("request");
        server.await.expect("server task");

        assert_eq!(request_line, "POST /codex/responses HTTP/1.1");
        assert!(headers
            .to_ascii_lowercase()
            .contains("authorization: bearer codex-test-token"));
        assert!(body.contains("\"input\""));
        assert!(body.contains("\"store\":false"));
        assert!(body.contains("\"tools\":["));
        assert!(body.contains("\"tool_choice\":\"auto\""));
        assert!(body.contains("\"parallel_tool_calls\":false"));
        assert!(body.contains("\"instructions\":\"Be concise.\""));
        assert!(body.contains("\"gpt-5.5\""));
        assert!(body.contains("\"browser_wait\""));
        assert!(!body.contains("\"anyOf\""));
        assert!(!body.contains("\"role\":\"developer\""));
        assert!(!body.contains("\"max_output_tokens\""));

        let text_deltas = chunks
            .iter()
            .filter_map(|chunk| match chunk {
                StreamChunk::TextDelta(text) => Some(text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(text_deltas, vec!["Hello"]);

        let done_chunks = chunks
            .iter()
            .filter(|chunk| matches!(chunk, StreamChunk::Done { .. }))
            .count();
        assert_eq!(done_chunks, 1);

        let done = chunks
            .iter()
            .find_map(|chunk| match chunk {
                StreamChunk::Done {
                    finish_reason,
                    usage,
                } => Some((
                    finish_reason.as_str(),
                    usage.as_ref().map(|usage| usage.total_tokens),
                )),
                _ => None,
            })
            .expect("done chunk");
        assert_eq!(done.0, "stop");
        assert_eq!(done.1, Some(12));
    }

    #[tokio::test]
    async fn openai_codex_stream_recovers_function_call_args_without_deltas() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener address");
        let (tx, rx) = oneshot::channel();

        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept connection");
            let request = read_single_http_request(&mut socket).await;
            let response_body = concat!(
                "data: {\"type\":\"response.output_item.added\",\"output_index\":0,\"item\":{\"type\":\"function_call\",\"id\":\"fc_1\",\"call_id\":\"call_abc\",\"name\":\"write\"}}\n\n",
                "data: {\"type\":\"response.output_item.done\",\"output_index\":0,\"item\":{\"type\":\"function_call\",\"id\":\"fc_1\",\"call_id\":\"call_abc\",\"name\":\"write\",\"arguments\":\"{\\\"path\\\":\\\"assess.json\\\",\\\"content\\\":\\\"{}\\\"}\"}}\n\n",
                "data: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"output\":[{\"type\":\"function_call\",\"id\":\"fc_1\",\"call_id\":\"call_abc\",\"name\":\"write\",\"arguments\":\"{\\\"path\\\":\\\"assess.json\\\",\\\"content\\\":\\\"{}\\\"}\"}],\"usage\":{\"input_tokens\":10,\"output_tokens\":20,\"total_tokens\":30}}}\n\n",
                "data: [DONE]\n\n"
            );
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                response_body.as_bytes().len(),
                response_body
            );
            socket
                .write_all(response.as_bytes())
                .await
                .expect("write response");
            socket.shutdown().await.expect("shutdown socket");
            tx.send(request).expect("send request");
        });

        let provider = OpenAIResponsesProvider {
            id: "openai-codex".to_string(),
            name: "OpenAI Codex".to_string(),
            base_url: format!("http://{}/codex", addr),
            api_key: Some("codex-test-token".to_string()),
            default_model: "gpt-5.4-mini".to_string(),
            models: codex_supported_models(272_000),
            client: Client::new(),
        };

        let messages = vec![ChatMessage {
            role: "user".to_string(),
            content: "write a file".to_string(),
            attachments: Vec::new(),
        }];
        let tools = vec![ToolSchema::new(
            "write",
            "Write a file.",
            json!({
                "type": "object",
                "properties": {
                    "path": { "type": "string" },
                    "content": { "type": "string" }
                },
                "required": ["path", "content"]
            }),
        )];
        let cancel = CancellationToken::new();
        let stream = provider
            .stream(messages, None, ToolMode::Auto, Some(tools), SamplingParams::default(), cancel)
            .await
            .expect("stream");

        let mut chunks = Vec::new();
        futures::pin_mut!(stream);
        while let Some(chunk) = stream.next().await {
            let chunk = chunk.expect("stream chunk");
            let is_done = matches!(chunk, StreamChunk::Done { .. });
            chunks.push(chunk);
            if is_done {
                break;
            }
        }

        let _ = rx.await.expect("request");
        server.await.expect("server task");

        let tool_start_count = chunks
            .iter()
            .filter(|chunk| matches!(chunk, StreamChunk::ToolCallStart { .. }))
            .count();
        assert_eq!(tool_start_count, 1, "expected exactly one ToolCallStart");

        let tool_end_count = chunks
            .iter()
            .filter(|chunk| matches!(chunk, StreamChunk::ToolCallEnd { .. }))
            .count();
        assert_eq!(tool_end_count, 1, "expected exactly one ToolCallEnd");

        let accumulated_args = chunks
            .iter()
            .filter_map(|chunk| match chunk {
                StreamChunk::ToolCallDelta { args_delta, .. } => Some(args_delta.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .concat();
        assert!(
            accumulated_args.contains("\"path\":\"assess.json\""),
            "recovered args missing path: {accumulated_args}"
        );
        assert!(
            accumulated_args.contains("\"content\""),
            "recovered args missing content key: {accumulated_args}"
        );

        let done = chunks
            .iter()
            .find_map(|chunk| match chunk {
                StreamChunk::Done { finish_reason, .. } => Some(finish_reason.as_str()),
                _ => None,
            })
            .expect("done chunk");
        assert_eq!(done, "toolUse");
    }

    #[tokio::test]
    async fn openai_codex_stream_accepts_sse_event_headers_without_json_type() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener address");
        let (tx, rx) = oneshot::channel();

        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept connection");
            let request = read_single_http_request(&mut socket).await;
            let response_body = concat!(
                "event: response.output_text.delta\n",
                "data: {\"item_id\":\"msg_1\",\"delta\":\"Hello\"}\n\n",
                "event: response.completed\n",
                "data: {\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":5,\"output_tokens\":7,\"total_tokens\":12}}}\n\n",
                "data: [DONE]\n\n"
            );
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                response_body.as_bytes().len(),
                response_body
            );
            socket
                .write_all(response.as_bytes())
                .await
                .expect("write response");
            socket.shutdown().await.expect("shutdown socket");
            tx.send(request).expect("send request");
        });

        let provider = OpenAIResponsesProvider {
            id: "openai-codex".to_string(),
            name: "OpenAI Codex".to_string(),
            base_url: format!("http://{}/codex", addr),
            api_key: Some("codex-test-token".to_string()),
            default_model: "gpt-5.5".to_string(),
            models: codex_supported_models(272_000),
            client: Client::new(),
        };

        let messages = vec![ChatMessage {
            role: "user".to_string(),
            content: "hi".to_string(),
            attachments: Vec::new(),
        }];
        let cancel = CancellationToken::new();
        let stream = provider
            .stream(messages, None, ToolMode::Auto, None, SamplingParams::default(), cancel)
            .await
            .expect("stream");

        let mut chunks = Vec::new();
        futures::pin_mut!(stream);
        while let Some(chunk) = stream.next().await {
            let chunk = chunk.expect("stream chunk");
            let is_done = matches!(chunk, StreamChunk::Done { .. });
            chunks.push(chunk);
            if is_done {
                break;
            }
        }

        let _ = rx.await.expect("request");
        server.await.expect("server task");

        let text_deltas = chunks
            .iter()
            .filter_map(|chunk| match chunk {
                StreamChunk::TextDelta(text) => Some(text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(text_deltas, vec!["Hello"]);

        let done = chunks
            .iter()
            .find_map(|chunk| match chunk {
                StreamChunk::Done {
                    finish_reason,
                    usage,
                } => Some((
                    finish_reason.as_str(),
                    usage.as_ref().map(|usage| usage.total_tokens),
                )),
                _ => None,
            })
            .expect("done chunk");
        assert_eq!(done.0, "stop");
        assert_eq!(done.1, Some(12));
    }

    #[tokio::test]
    async fn openai_codex_complete_recovers_when_responses_requires_streaming() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener address");
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();

        let server = tokio::spawn(async move {
            let mut request_count = 0usize;
            while request_count < 2 {
                let (mut socket, _) = listener.accept().await.expect("accept connection");
                let request = read_single_http_request(&mut socket).await;
                request_count += 1;
                if request_count == 1 {
                    let response_body = "{\"detail\":\"Stream must be set to true\"}";
                    let response = format!(
                        "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                        response_body.as_bytes().len(),
                        response_body
                    );
                    socket
                        .write_all(response.as_bytes())
                        .await
                        .expect("write first response");
                } else {
                    let response_body = concat!(
                        "data: {\"type\":\"response.output_text.delta\",\"delta\":\"Recovered\"}\n\n",
                        "data: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"output_text\":\"Recovered\"}}\n\n",
                        "data: [DONE]\n\n"
                    );
                    let response = format!(
                        "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                        response_body.as_bytes().len(),
                        response_body
                    );
                    socket
                        .write_all(response.as_bytes())
                        .await
                        .expect("write second response");
                }
                socket.shutdown().await.expect("shutdown socket");
                tx.send(request).expect("send request");
            }
        });

        let provider = OpenAIResponsesProvider {
            id: "openai-codex".to_string(),
            name: "OpenAI Codex".to_string(),
            base_url: format!("http://{}/codex", addr),
            api_key: Some("codex-test-token".to_string()),
            default_model: "gpt-5.5".to_string(),
            models: codex_supported_models(272_000),
            client: Client::new(),
        };

        let text = provider
            .complete("recover completion", None)
            .await
            .expect("completion");
        assert_eq!(text, "Recovered");

        let first = rx.recv().await.expect("first request");
        let second = rx.recv().await.expect("second request");
        server.await.expect("server task");

        assert_eq!(first.0, "POST /codex/responses HTTP/1.1");
        assert!(first.2.contains("\"stream\":false"));
        assert_eq!(second.0, "POST /codex/responses HTTP/1.1");
        assert!(second.2.contains("\"stream\":true"));
    }

    #[test]
    fn codex_supported_models_include_extended_catalog() {
        let models = codex_supported_models(272_000);
        let ids = models
            .iter()
            .map(|model| model.id.as_str())
            .collect::<Vec<_>>();
        assert!(ids.contains(&"gpt-5.5"));
        assert!(ids.contains(&"gpt-5.4"));
        assert!(ids.contains(&"gpt-5.2-codex"));
        assert!(ids.contains(&"gpt-5.1-codex-max"));
        assert!(ids.contains(&"gpt-5.4-mini"));
        assert!(ids.contains(&"gpt-5.3-codex"));
        assert!(ids.contains(&"gpt-5.3-codex-spark"));
        assert!(ids.contains(&"gpt-5.1-codex-mini"));
    }

    #[test]
    fn extract_openai_tool_call_chunks_supports_content_array_tool_calls() {
        let mut alias_to_original = HashMap::new();
        alias_to_original.insert("write_alias".to_string(), "write".to_string());
        let choice = json!({
            "message": {
                "content": [
                    {
                        "type": "tool_call",
                        "id": "call-1",
                        "function": {
                            "name": "write_alias",
                            "arguments": "{\"path\":\"README.md\",\"content\":\"hi\"}"
                        }
                    }
                ]
            }
        });
        let calls = extract_openai_tool_call_chunks(&choice, &alias_to_original);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].id, "call-1");
        assert_eq!(calls[0].name, "write");
        assert!(calls[0].args_delta.contains("\"README.md\""));
    }

    #[test]
    fn resolve_openai_tool_call_stream_id_keeps_multichunk_write_args_on_same_id() {
        let mut alias_to_original = HashMap::new();
        alias_to_original.insert("write_alias".to_string(), "write".to_string());

        let first_choice = json!({
            "delta": {
                "tool_calls": [
                    {
                        "index": 2,
                        "id": "call_ghi",
                        "function": {
                            "name": "write_alias",
                            "arguments": ""
                        }
                    }
                ]
            }
        });
        let continuation_choice = json!({
            "delta": {
                "tool_calls": [
                    {
                        "index": 2,
                        "function": {
                            "arguments": "{\"path\":\"game.html\",\"content\":\"hi\"}"
                        }
                    }
                ]
            }
        });

        let first_calls = extract_openai_tool_call_chunks(&first_choice, &alias_to_original);
        let continuation_calls =
            extract_openai_tool_call_chunks(&continuation_choice, &alias_to_original);

        assert_eq!(first_calls.len(), 1);
        assert_eq!(first_calls[0].id, "call_ghi");
        assert_eq!(first_calls[0].name, "write");
        assert_eq!(first_calls[0].index, 2);

        assert_eq!(continuation_calls.len(), 1);
        assert_eq!(continuation_calls[0].id, "tool_call_2");
        assert_eq!(continuation_calls[0].name, "");
        assert_eq!(continuation_calls[0].index, 2);

        let mut real_ids_by_index = HashMap::new();
        let mut args_by_id = HashMap::<String, String>::new();
        for call in first_calls.into_iter().chain(continuation_calls) {
            let effective_id = resolve_openai_tool_call_stream_id(&call, &mut real_ids_by_index);
            args_by_id
                .entry(effective_id)
                .or_default()
                .push_str(&call.args_delta);
        }

        assert_eq!(
            real_ids_by_index.get(&2).map(String::as_str),
            Some("call_ghi")
        );
        assert_eq!(
            args_by_id.get("call_ghi").map(String::as_str),
            Some("{\"path\":\"game.html\",\"content\":\"hi\"}")
        );
        assert!(!args_by_id.contains_key("tool_call_2"));
    }

    #[test]
    fn push_openai_text_fragments_reads_nested_text_parts() {
        let value = json!([
            {"type":"text","text":"first"},
            {"type":"output_text","text":{"value":"second"}},
            {"type":"text","content":"third"}
        ]);
        let mut fragments = Vec::new();
        push_openai_text_fragments(&value, &mut fragments);
        assert_eq!(fragments, vec!["first", "second", "third"]);
    }

    #[test]
    fn normalize_openai_function_parameters_adds_missing_properties() {
        let normalized = normalize_openai_function_parameters(json!({"type":"object"}));
        assert_eq!(
            normalized
                .get("type")
                .and_then(|v| v.as_str())
                .unwrap_or_default(),
            "object"
        );
        assert!(
            normalized
                .get("properties")
                .and_then(|v| v.as_object())
                .is_some(),
            "properties object should exist"
        );
    }

    #[test]
    fn normalize_openai_function_parameters_recovers_non_object_schema() {
        let normalized = normalize_openai_function_parameters(json!("bad"));
        assert_eq!(
            normalized
                .get("type")
                .and_then(|v| v.as_str())
                .unwrap_or_default(),
            "object"
        );
        assert!(
            normalized
                .get("properties")
                .and_then(|v| v.as_object())
                .is_some(),
            "properties object should exist"
        );
    }

    #[test]
    fn normalize_openai_function_parameters_rewrites_tuple_array_items() {
        let normalized = normalize_openai_function_parameters(json!({
            "type": "object",
            "properties": {
                "fieldIds": {
                    "type": "array",
                    "items": [
                        { "$ref": "#/properties/fieldIds/items" }
                    ]
                }
            }
        }));
        assert!(
            normalized["properties"]["fieldIds"]["items"].is_object(),
            "array items should be object/bool for OpenAI-compatible tools"
        );
    }

    #[test]
    fn normalize_openai_function_parameters_adds_nested_object_properties() {
        let normalized = normalize_openai_function_parameters(json!({
            "type": "object",
            "properties": {
                "filters": {
                    "type": "object"
                }
            }
        }));
        assert!(
            normalized["properties"]["filters"]["properties"].is_object(),
            "nested object schemas should include properties for OpenAI validation"
        );
    }

    #[test]
    fn normalize_codex_function_parameters_strips_root_combinators() {
        let normalized = normalize_codex_function_parameters(json!({
            "type": "object",
            "properties": {
                "session_id": { "type": "string" },
                "selector": { "type": "string" }
            },
            "required": ["session_id"],
            "anyOf": [
                { "required": ["selector"] }
            ],
            "not": {
                "required": ["forbidden"]
            }
        }));

        assert_eq!(
            normalized.get("type").and_then(|value| value.as_str()),
            Some("object")
        );
        assert!(normalized
            .get("properties")
            .and_then(|value| value.as_object())
            .is_some());
        assert!(normalized.get("anyOf").is_none());
        assert!(normalized.get("not").is_none());
    }

    #[test]
    fn openrouter_affordability_retry_uses_affordable_cap() {
        let detail = r#"{"error":{"message":"This request requires more credits, or fewer max_tokens. You requested up to 16384 tokens, but can only afford 14605."}}"#;
        assert_eq!(
            openrouter_affordability_retry_max_tokens(
                "openrouter",
                reqwest::StatusCode::PAYMENT_REQUIRED,
                detail,
                16_384,
            ),
            Some(14_605)
        );
    }

    #[test]
    fn openrouter_tool_choice_retry_detects_unsupported_required_mode() {
        assert!(openrouter_tool_choice_retry_supported(
            "openrouter",
            &ToolMode::Required,
            "No endpoints found that support the provided 'tool_choice' value."
        ));
        assert!(!openrouter_tool_choice_retry_supported(
            "openrouter",
            &ToolMode::Auto,
            "No endpoints found that support the provided 'tool_choice' value."
        ));
        assert!(!openrouter_tool_choice_retry_supported(
            "openai",
            &ToolMode::Required,
            "No endpoints found that support the provided 'tool_choice' value."
        ));
    }

    #[test]
    fn provider_specific_max_tokens_override_is_respected() {
        std::env::remove_var("TANDEM_PROVIDER_MAX_TOKENS");
        std::env::set_var("TANDEM_PROVIDER_MAX_TOKENS_OPENROUTER", "24576");
        assert_eq!(provider_max_tokens_for("openrouter"), 24_576);
        std::env::remove_var("TANDEM_PROVIDER_MAX_TOKENS_OPENROUTER");
        assert_eq!(provider_max_tokens_for("openrouter"), 16_384);
    }

    // OpenAI sends usage in a separate trailing chunk (choices:[]) when
    // stream_options.include_usage is set.  The Done event must carry that
    // usage even though it arrives after the finish_reason chunk.
    #[tokio::test]
    async fn chat_completions_trailing_usage_chunk_reaches_done_event() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener address");
        let (tx, rx) = oneshot::channel::<(String, String, String)>();

        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept connection");
            let request = read_single_http_request(&mut socket).await;
            // finish_reason chunk arrives first, usage chunk arrives separately
            let response_body = concat!(
                "data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"},\"finish_reason\":null}]}\n\n",
                "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n",
                "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15}}\n\n",
                "data: [DONE]\n\n",
            );
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                response_body.as_bytes().len(),
                response_body
            );
            socket.write_all(response.as_bytes()).await.expect("write");
            socket.shutdown().await.expect("shutdown");
            tx.send(request).expect("send");
        });

        let provider = OpenAICompatibleProvider {
            id: "openai".to_string(),
            name: "OpenAI".to_string(),
            base_url: format!("http://{}/v1", addr),
            api_key: Some("sk-test".to_string()),
            default_model: "gpt-4o-mini".to_string(),
            client: Client::new(),
        };

        let messages = vec![ChatMessage {
            role: "user".to_string(),
            content: "hi".to_string(),
            attachments: Vec::new(),
        }];
        let cancel = CancellationToken::new();
        let stream = provider
            .stream(messages, None, ToolMode::Auto, None, SamplingParams::default(), cancel)
            .await
            .expect("stream");

        let mut chunks = Vec::new();
        futures::pin_mut!(stream);
        while let Some(chunk) = stream.next().await {
            let chunk = chunk.expect("chunk");
            let is_done = matches!(chunk, StreamChunk::Done { .. });
            chunks.push(chunk);
            if is_done {
                break;
            }
        }

        let (_, _, body) = rx.await.expect("request");
        server.await.expect("server");

        assert!(
            body.contains("\"include_usage\":true"),
            "request body must include stream_options.include_usage: {body}"
        );

        let done = chunks
            .iter()
            .find_map(|c| match c {
                StreamChunk::Done {
                    finish_reason,
                    usage,
                } => Some((
                    finish_reason.as_str(),
                    usage.as_ref().map(|u| u.total_tokens),
                )),
                _ => None,
            })
            .expect("done chunk");
        assert_eq!(done.0, "stop");
        assert_eq!(
            done.1,
            Some(15),
            "trailing usage chunk must reach the Done event"
        );
    }

    // ── Per-role sampling parameter mapping & clamping ───────────────────────

    #[test]
    fn openai_chat_sampling_maps_all_fields() {
        let mut body = json!({ "model": "gpt-4o", "max_tokens": 16384 });
        apply_openai_chat_sampling(
            &mut body,
            "openai",
            "gpt-4o",
            SamplingParams {
                temperature: Some(0.1),
                top_p: Some(0.9),
                max_tokens: Some(2048),
            },
        );
        assert_eq!(body["temperature"], json!(0.1_f32));
        assert_eq!(body["top_p"], json!(0.9_f32));
        // Explicit max_tokens overrides the engine default budget.
        assert_eq!(body["max_tokens"], json!(2048));
    }

    #[test]
    fn empty_sampling_leaves_request_body_untouched() {
        // Omitting sampling must produce a byte-identical request to today.
        let original = json!({
            "model": "gpt-4o",
            "messages": [],
            "stream": true,
            "max_tokens": 16384,
        });
        let mut body = original.clone();
        apply_openai_chat_sampling(&mut body, "openai", "gpt-4o", SamplingParams::default());
        assert_eq!(body, original);
    }

    #[test]
    fn openai_chat_sampling_clamps_out_of_range_values() {
        let mut body = json!({ "model": "gpt-4o" });
        apply_openai_chat_sampling(
            &mut body,
            "openai",
            "gpt-4o",
            SamplingParams {
                temperature: Some(5.0),
                top_p: Some(2.0),
                max_tokens: Some(0),
            },
        );
        // OpenAI temperature caps at 2.0, top_p at 1.0, max_tokens at >= 1.
        assert_eq!(body["temperature"], json!(2.0_f32));
        assert_eq!(body["top_p"], json!(1.0_f32));
        assert_eq!(body["max_tokens"], json!(1));
    }

    #[test]
    fn reasoning_model_drops_temperature_without_failing() {
        let mut body = json!({ "model": "o3-mini" });
        apply_openai_chat_sampling(
            &mut body,
            "openai",
            "o3-mini",
            SamplingParams {
                temperature: Some(0.2),
                top_p: None,
                max_tokens: Some(1000),
            },
        );
        // Model rejects temperature → dropped (with a warning), run continues.
        assert!(body.get("temperature").is_none());
        assert_eq!(body["max_tokens"], json!(1000));
    }

    #[test]
    fn openai_responses_sampling_uses_max_output_tokens() {
        let mut body = json!({ "model": "gpt-4o" });
        apply_openai_responses_sampling(
            &mut body,
            "openai-codex",
            "gpt-4o",
            SamplingParams {
                temperature: Some(0.3),
                top_p: None,
                max_tokens: Some(4096),
            },
        );
        assert_eq!(body["temperature"], json!(0.3_f32));
        assert_eq!(body["max_output_tokens"], json!(4096));
        assert!(body.get("max_tokens").is_none());
    }

    #[test]
    fn anthropic_sampling_clamps_temperature_to_one() {
        let mut body = json!({ "model": "claude-sonnet-4-6", "max_tokens": 1024 });
        apply_anthropic_sampling(
            &mut body,
            "claude-sonnet-4-6",
            SamplingParams {
                temperature: Some(1.8),
                top_p: Some(0.5),
                max_tokens: Some(8192),
            },
        );
        // Anthropic caps temperature at 1.0.
        assert_eq!(body["temperature"], json!(1.0_f32));
        assert_eq!(body["top_p"], json!(0.5_f32));
        assert_eq!(body["max_tokens"], json!(8192));
    }

    #[test]
    fn model_rejects_temperature_matches_reasoning_families() {
        assert!(model_rejects_temperature("o1"));
        assert!(model_rejects_temperature("o3-mini"));
        assert!(model_rejects_temperature("o4-mini"));
        assert!(model_rejects_temperature("gpt-5-thinking"));
        assert!(!model_rejects_temperature("gpt-4o"));
        assert!(!model_rejects_temperature("claude-sonnet-4-6"));
    }

    #[test]
    fn openrouter_affordability_retry_respects_max_tokens_override() {
        let status = reqwest::StatusCode::PAYMENT_REQUIRED;
        let detail = "can only afford 32000";
        // With a max_tokens override of 64k, an affordable cap of 32k is below
        // the requested value and must trigger a retry (regression: previously
        // compared against the 16k default and bailed).
        assert_eq!(
            openrouter_affordability_retry_max_tokens("openrouter", status, detail, 64_000),
            Some(32_000)
        );
        // Affordable cap at/above the requested value does not retry.
        assert_eq!(
            openrouter_affordability_retry_max_tokens("openrouter", status, detail, 16_000),
            None
        );
        // Only OpenRouter 402s qualify.
        assert_eq!(
            openrouter_affordability_retry_max_tokens("openai", status, detail, 64_000),
            None
        );
    }
}