opencrabs 0.3.24

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
//! Provider Factory
//!
//! Creates providers based on config.toml settings.
//!
//! ## Registry Pattern
//!
//! All built-in providers are registered in the `REGISTRATIONS` array below.
//! Adding a new provider requires only:
//! 1. Adding a `ProviderRegistration` entry to `REGISTRATIONS`
//! 2. Adding the corresponding field to `ProviderConfigs` in `config/types.rs`
//! 3. Writing a `try_create_*` function
//!
//! The factory functions (`provider_enabled`,
//! `create_provider_by_name`, `create_fallback`, `active_provider_vision`)
//! all iterate the registry automatically.

use super::qwen::{qwen_body_transform, qwen_extra_headers};
use super::{
    Provider,
    anthropic::AnthropicProvider,
    claude_cli::ClaudeCliProvider,
    codex_cli::CodexCliProvider,
    codex_oauth::CodexOAuthProvider,
    custom_openai_compatible::{BodyTransformFn, OpenAIProvider},
    gemini::GeminiProvider,
    opencode_cli::OpenCodeCliProvider,
};
use crate::config::{Config, ProviderConfig};
use anyhow::Result;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, LazyLock};

// ── Provider Registry ───────────────────────────────────────────

/// Type alias for async factory functions stored in the registry.
type ProviderFactoryFn = Box<
    dyn Fn(&Config) -> Pin<Box<dyn Future<Output = Result<Option<Arc<dyn Provider>>>> + Send + '_>>
        + Send
        + Sync,
>;

/// Type alias for sync factory functions (used by `sync_factory`).
type SyncProviderFactoryFn = fn(&Config) -> Result<Option<Arc<dyn Provider>>>;

/// Wrap a synchronous factory function into the async registry type.
fn sync_factory(f: SyncProviderFactoryFn) -> ProviderFactoryFn {
    Box::new(move |config| Box::pin(async move { f(config) }))
}

/// Registration entry for a built-in provider.
struct ProviderRegistration {
    /// Display name shown in pickers and logs (e.g. "Anthropic").
    display_name: &'static str,
    /// Session ID used for restoration (e.g. "anthropic").
    session_id: &'static str,
    /// Alternative session IDs (e.g. ["claude-cli", "claude_cli"]).
    aliases: &'static [&'static str],
    /// Check if this provider is enabled in config.
    is_enabled: fn(&Config) -> bool,
    /// Try to create the provider instance.
    factory: ProviderFactoryFn,
    /// Extract the provider config for vision/model lookups.
    config_field: fn(&Config) -> Option<&ProviderConfig>,
}

/// All built-in providers in priority order.
///
/// **IMPORTANT:** This array must stay in sync with `PROVIDER_NAMES`.
/// The index is used by `provider_enabled()`.
static REGISTRATIONS: LazyLock<Vec<ProviderRegistration>> = LazyLock::new(|| {
    vec![
        ProviderRegistration {
            display_name: "Claude CLI",
            session_id: "claude-cli",
            aliases: &["claude_cli"],
            is_enabled: |c| c.providers.claude_cli.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_claude_cli),
            config_field: |c| c.providers.claude_cli.as_ref(),
        },
        ProviderRegistration {
            display_name: "OpenCode CLI",
            session_id: "opencode-cli",
            aliases: &["opencode_cli"],
            is_enabled: |c| c.providers.opencode_cli.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_opencode_cli),
            config_field: |c| c.providers.opencode_cli.as_ref(),
        },
        ProviderRegistration {
            display_name: "Codex CLI",
            session_id: "codex-cli",
            aliases: &["codex_cli"],
            is_enabled: |c| c.providers.codex_cli.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_codex_cli),
            config_field: |c| c.providers.codex_cli.as_ref(),
        },
        ProviderRegistration {
            display_name: "Codex",
            session_id: "codex",
            aliases: &["codex_oauth"],
            is_enabled: |c| c.providers.codex.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_codex_oauth),
            config_field: |c| c.providers.codex.as_ref(),
        },
        ProviderRegistration {
            display_name: "OpenCode",
            session_id: "opencode",
            aliases: &["opencode_api"],
            is_enabled: |c| c.providers.opencode.as_ref().is_some_and(|p| p.enabled),
            factory: Box::new(|config| Box::pin(try_create_opencode(config))),
            config_field: |c| c.providers.opencode.as_ref(),
        },
        ProviderRegistration {
            display_name: "Qwen",
            session_id: "qwen",
            aliases: &[],
            is_enabled: |c| c.providers.qwen.as_ref().is_some_and(|p| p.enabled),
            factory: Box::new(|config| Box::pin(try_create_qwen(config))),
            config_field: |c| c.providers.qwen.as_ref(),
        },
        ProviderRegistration {
            display_name: "Anthropic",
            session_id: "anthropic",
            aliases: &[],
            is_enabled: |c| c.providers.anthropic.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_anthropic),
            config_field: |c| c.providers.anthropic.as_ref(),
        },
        ProviderRegistration {
            display_name: "OpenAI",
            session_id: "openai",
            aliases: &[],
            is_enabled: |c| c.providers.openai.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_openai),
            config_field: |c| c.providers.openai.as_ref(),
        },
        ProviderRegistration {
            display_name: "GitHub Copilot",
            session_id: "github",
            aliases: &[],
            is_enabled: |c| c.providers.github.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_github),
            config_field: |c| c.providers.github.as_ref(),
        },
        ProviderRegistration {
            display_name: "Google Gemini",
            session_id: "gemini",
            aliases: &[],
            is_enabled: |c| c.providers.gemini.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_gemini),
            config_field: |c| c.providers.gemini.as_ref(),
        },
        ProviderRegistration {
            display_name: "OpenRouter",
            session_id: "openrouter",
            aliases: &[],
            is_enabled: |c| c.providers.openrouter.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_openrouter),
            config_field: |c| c.providers.openrouter.as_ref(),
        },
        ProviderRegistration {
            display_name: "Minimax",
            session_id: "minimax",
            aliases: &[],
            is_enabled: |c| c.providers.minimax.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_minimax),
            config_field: |c| c.providers.minimax.as_ref(),
        },
        ProviderRegistration {
            display_name: "z.ai GLM",
            session_id: "zhipu",
            aliases: &[],
            is_enabled: |c| c.providers.zhipu.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_zhipu),
            config_field: |c| c.providers.zhipu.as_ref(),
        },
        ProviderRegistration {
            display_name: "Ollama",
            session_id: "ollama",
            aliases: &[],
            is_enabled: |c| c.providers.ollama.as_ref().is_some_and(|p| p.enabled),
            factory: sync_factory(try_create_ollama),
            config_field: |c| c.providers.ollama.as_ref(),
        },
        ProviderRegistration {
            display_name: "Custom",
            session_id: "custom",
            aliases: &[],
            is_enabled: |c| c.providers.active_custom().is_some(),
            factory: sync_factory(try_create_custom),
            config_field: |_| None, // Custom uses active_custom() instead
        },
    ]
});

/// Provider names in priority order, derived from REGISTRATIONS.
pub const PROVIDER_NAMES: &[&str] = &[
    "Claude CLI",
    "OpenCode CLI",
    "Codex CLI",
    "Codex",
    "OpenCode",
    "Qwen",
    "Anthropic",
    "OpenAI",
    "GitHub Copilot",
    "Google Gemini",
    "OpenRouter",
    "Minimax",
    "z.ai GLM",
    "Ollama",
    "Custom",
];

/// Whether a provider is enabled in config, by index matching PROVIDER_NAMES.
fn provider_enabled(config: &Config, idx: usize) -> bool {
    REGISTRATIONS
        .get(idx)
        .is_some_and(|reg| (reg.is_enabled)(config))
}

/// All built-in provider session_ids in priority order.
/// Used for cross-checking TUI provider ids against the factory registry.
pub fn provider_session_ids() -> Vec<&'static str> {
    REGISTRATIONS.iter().map(|r| r.session_id).collect()
}

// ── Local URL detection & thinking transform ────────────────────

/// Detect whether a base URL points to a local inference server
/// (llama.cpp, MLX, LM Studio, Ollama, etc.). Used to gate behaviours
/// that only make sense for self-hosted backends — specifically the
/// `chat_template_kwargs` injection for local Qwen, which cloud Qwen
/// (DashScope) rejects because it sets the reasoning flag server-side.
///
/// Matches loopback (`localhost`, `127.0.0.1`, `::1`, `0.0.0.0`),
/// mDNS (`*.local`), and the three RFC1918 private-network ranges
/// (`10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`). Public hosts
/// always return false so production endpoints keep their existing
/// request shape.
pub(crate) fn is_local_base_url(url: &str) -> bool {
    let after_scheme = url
        .strip_prefix("http://")
        .or_else(|| url.strip_prefix("https://"))
        .unwrap_or(url);
    let host_and_port = after_scheme.split(['/', '?']).next().unwrap_or("");
    // IPv6 addresses are bracketed: `[::1]:1234`. Strip brackets first so
    // the port-split logic doesn't mangle the address.
    let bare = if let Some(rest) = host_and_port.strip_prefix('[') {
        rest.split_once(']').map(|(h, _)| h).unwrap_or(rest)
    } else {
        host_and_port
            .rsplit_once(':')
            .map(|(h, _)| h)
            .unwrap_or(host_and_port)
    };
    let bare = bare.to_ascii_lowercase();
    if bare == "localhost"
        || bare == "127.0.0.1"
        || bare == "0.0.0.0"
        || bare == "::1"
        || bare.ends_with(".local")
        || bare.starts_with("192.168.")
        || bare.starts_with("10.")
    {
        return true;
    }
    // 172.16.0.0 – 172.31.255.255
    let mut parts = bare.split('.');
    if parts.next() == Some("172")
        && let Some(second) = parts.next()
        && let Ok(n) = second.parse::<u8>()
        && (16..=31).contains(&n)
    {
        return true;
    }
    false
}

/// Build a body transform that injects
/// `chat_template_kwargs: {"enable_thinking": X}` into every request sent
/// to a local llama.cpp / MLX / LM Studio / Ollama server. Mirrors what
/// `llama-server --jinja --chat-template-kwargs '{"enable_thinking":true}'`
/// does — the flags Unsloth Studio launches with — so embedded GGUF/MLX
/// chat templates render `<tool_call>` tags and reasoning blocks the way
/// the model was trained on.
///
/// Generic across thinking-capable models (Qwen3, Kimi-K2, DeepSeek-R1
/// variants, etc.) — we inject when opted in, regardless of the model
/// name, because the mechanism is a llama.cpp jinja feature, not a
/// model-specific one. Callers only install this transform when the
/// user set `enable_thinking` in config (opt-in), so local models whose
/// template doesn't accept the variable stay untouched by default.
fn local_thinking_body_transform(enable: bool) -> BodyTransformFn {
    Arc::new(move |mut body: serde_json::Value| {
        if let Some(obj) = body.as_object_mut()
            && !obj.contains_key("chat_template_kwargs")
        {
            obj.insert(
                "chat_template_kwargs".to_string(),
                serde_json::json!({ "enable_thinking": enable }),
            );
        }
        body
    })
}

// ── Public factory functions ────────────────────────────────────

/// Create a provider based on config.toml
/// No hardcoded priority - providers are enabled/disabled in config
pub async fn create_provider(config: &Config) -> Result<Arc<dyn Provider>> {
    let (provider, warning) = create_provider_with_warning(config).await?;
    if let Some(msg) = &warning {
        tracing::warn!("{}", msg);
    }
    Ok(provider)
}

/// Like `create_provider` but returns a warning message when a fallback was used.
/// The caller (TUI) should surface this to the user instead of printing to stderr.
pub async fn create_provider_with_warning(
    config: &Config,
) -> Result<(Arc<dyn Provider>, Option<String>)> {
    let mut primary: Option<Arc<dyn Provider>> = None;
    let mut failed_name: Option<&str> = None;
    let mut warning: Option<String> = None;

    // Try enabled providers in priority order
    for (i, reg) in REGISTRATIONS.iter().enumerate() {
        if !provider_enabled(config, i) {
            continue;
        }

        match (reg.factory)(config).await {
            Ok(Some(provider)) => {
                if let Some(failed) = failed_name {
                    let msg = format!(
                        "{} failed to initialize — fell back to {}. Run /onboard:provider to reconfigure.",
                        failed, reg.display_name
                    );
                    tracing::warn!("{}", msg);
                    warning = Some(msg);
                }
                tracing::info!("Using enabled provider: {}", reg.display_name);
                primary = Some(provider);
                break;
            }
            Ok(None) => {
                tracing::warn!(
                    "{} enabled but could not be created (missing API key?)",
                    reg.display_name
                );
                if failed_name.is_none() {
                    failed_name = Some(reg.display_name);
                }
            }
            Err(e) => {
                tracing::error!("{} provider error: {}", reg.display_name, e);
                if failed_name.is_none() {
                    failed_name = Some(reg.display_name);
                }
            }
        }
    }

    // Build fallback chain if configured (user-defined in config.toml)
    let fallback_providers = if let Some(fallback) = &config.providers.fallback
        && fallback.enabled
    {
        let chain = fallback_chain(fallback);
        let mut providers = Vec::new();
        for name in &chain {
            match create_fallback(config, name).await {
                Ok(p) => {
                    tracing::info!("Fallback provider '{}' ready", name);
                    providers.push(p);
                }
                Err(e) => {
                    tracing::warn!("Fallback provider '{}' skipped: {}", name, e);
                }
            }
        }
        providers
    } else {
        Vec::new()
    };

    match primary {
        Some(provider) => {
            if fallback_providers.is_empty() {
                Ok((provider, warning))
            } else {
                tracing::info!(
                    "Wrapping primary provider with {} fallback(s)",
                    fallback_providers.len()
                );
                Ok((
                    Arc::new(super::FallbackProvider::new_with_health(
                        provider,
                        fallback_providers,
                    )),
                    warning,
                ))
            }
        }
        None => {
            // No primary — try fallbacks as primary candidates
            if let Some(first) = fallback_providers.into_iter().next() {
                tracing::warn!("No primary provider enabled, using first fallback");
                Ok((first, warning))
            } else {
                tracing::info!("No provider configured, using placeholder provider");
                Ok((Arc::new(super::PlaceholderProvider), warning))
            }
        }
    }
}

/// Create a provider by name, ignoring the `enabled` flag.
/// Used for per-session provider restoration without toggling disk config.
/// Accepts names like "anthropic", "openai", "minimax", "openrouter", or "custom:<name>".
pub async fn create_provider_by_name(config: &Config, name: &str) -> Result<Arc<dyn Provider>> {
    // Custom entries take precedence over built-in names. If the user
    // created a custom provider literally named "opencode" / "anthropic"
    // / anything that collides with a built-in id, the custom entry wins.
    if !name.starts_with("custom:")
        && config
            .providers
            .custom
            .as_ref()
            .is_some_and(|m| m.contains_key(name))
        && let Some(p) = try_create_custom_by_name(config, name)?
    {
        return Ok(p);
    }

    // Try built-in registry by session_id or alias
    for reg in REGISTRATIONS.iter() {
        if reg.session_id == name || reg.aliases.contains(&name) {
            let provider = (reg.factory)(config).await?.ok_or_else(|| {
                anyhow::anyhow!("{} not configured (missing API key)", reg.display_name)
            })?;
            return Ok(provider);
        }
    }

    // Try custom: prefix
    if let Some(custom_name) = name.strip_prefix("custom:") {
        return try_create_custom_by_name(config, custom_name)?
            .ok_or_else(|| anyhow::anyhow!("Custom provider '{}' not configured", custom_name));
    }

    // Try as a custom provider name directly (legacy sessions)
    try_create_custom_by_name(config, name)?
        .ok_or_else(|| anyhow::anyhow!("Unknown provider: {}", name))
}

/// Try to create a specific named custom provider (ignores enabled flag).
fn try_create_custom_by_name(config: &Config, name: &str) -> Result<Option<Arc<dyn Provider>>> {
    let customs = match &config.providers.custom {
        Some(map) => map,
        None => return Ok(None),
    };

    let custom_config = match customs.get(name) {
        Some(cfg) => cfg.clone(),
        None => {
            tracing::warn!("Custom provider '{}' not found in config", name);
            return Ok(None);
        }
    };

    // API key is optional for local providers (LM Studio, Ollama, etc.)
    let api_key = custom_config.api_key.clone().unwrap_or_default();

    // base_url is REQUIRED. Silently defaulting to localhost:1234 (LM Studio)
    // produced channel-side errors like "failed to connect to localhost:1234"
    // whenever a session had a stale/missing custom provider entry — the user
    // saw the bot trying to reach a server they weren't running. Refuse to
    // construct the provider without an explicit base_url so the caller can
    // fall through to the next option (e.g. the global active provider).
    let Some(mut base_url) = custom_config.base_url.clone() else {
        tracing::warn!(
            "Custom provider '{}' has no base_url configured — skipping (run /onboard:provider)",
            name
        );
        return Ok(None);
    };

    if !base_url.contains("/chat/completions") {
        base_url = format!("{}/chat/completions", base_url.trim_end_matches('/'));
    }

    // Redact the key for logs but confirm merger ran. An empty key here
    // means keys.toml wasn't merged into this custom entry — the request
    // will then go out with `Bearer ` (empty) and always 401/403. Surface
    // that loudly instead of silently constructing a provider that can't
    // authenticate.
    let key_status = if api_key.is_empty() {
        "MISSING".to_string()
    } else if api_key == "__EXISTING_KEY__" {
        "SENTINEL(__EXISTING_KEY__ never merged)".to_string()
    } else {
        format!("present (len={})", api_key.len())
    };
    if api_key.is_empty() || api_key == "__EXISTING_KEY__" {
        tracing::warn!(
            "Custom provider '{}' being constructed without a real api_key ({}). \
             Requests will fail auth. Check keys.toml has [providers.custom.{}] api_key = \"...\" \
             and that the key isn't the literal sentinel string.",
            name,
            key_status,
            name,
        );
    }
    tracing::info!(
        "Creating custom provider '{}' at: {} (api_key: {})",
        name,
        base_url,
        key_status,
    );
    let mut builder =
        OpenAIProvider::with_base_url(api_key.clone(), base_url.clone()).with_name(name);
    if is_local_base_url(&base_url) {
        let enable = custom_config.enable_thinking.unwrap_or(true);
        builder = builder.with_body_transform(local_thinking_body_transform(enable));
    }
    let provider = configure_openai_compatible(builder, &custom_config);
    Ok(Some(Arc::new(provider)))
}

/// Build ordered fallback chain: `providers` array first, legacy `provider` as last resort.
pub(crate) fn fallback_chain(fallback: &crate::config::FallbackProviderConfig) -> Vec<String> {
    let mut chain: Vec<String> = fallback.providers.clone();
    // Append legacy single `provider` if set and not already in the list
    if let Some(ref legacy) = fallback.provider
        && !chain.iter().any(|p| p == legacy)
    {
        chain.push(legacy.clone());
    }
    chain
}

/// Create fallback provider
async fn create_fallback(config: &Config, fallback_type: &str) -> Result<Arc<dyn Provider>> {
    // Custom entries take precedence over built-in names. If the user
    // created a custom provider literally named "opencode" / "ollama" /
    // anything that collides with a built-in id, the custom entry wins.
    // (Same priority as create_provider_by_name.)
    if !fallback_type.starts_with("custom:")
        && config
            .providers
            .custom
            .as_ref()
            .is_some_and(|m| m.contains_key(fallback_type))
    {
        tracing::info!("Using fallback: Custom '{}'", fallback_type);
        return try_create_custom_by_name(config, fallback_type)?
            .ok_or_else(|| anyhow::anyhow!("Custom provider '{}' not configured", fallback_type));
    }

    // Try custom: prefix
    if let Some(custom_name) = fallback_type.strip_prefix("custom:") {
        tracing::info!("Using fallback: Custom '{}'", custom_name);
        return try_create_custom_by_name(config, custom_name)?
            .ok_or_else(|| anyhow::anyhow!("Custom provider '{}' not configured", custom_name));
    }

    // Try built-in registry
    for reg in REGISTRATIONS.iter() {
        if reg.session_id == fallback_type || reg.aliases.contains(&fallback_type) {
            tracing::info!("Using fallback: {}", reg.display_name);
            return (reg.factory)(config)
                .await?
                .ok_or_else(|| anyhow::anyhow!("{} not configured", reg.display_name));
        }
    }

    Err(anyhow::anyhow!(
        "Unknown fallback provider: {}",
        fallback_type
    ))
}

/// Returns `(api_key, base_url, vision_model)` for the first active provider
/// that has a `vision_model` configured. Used to register the provider-native
/// `analyze_image` tool when Gemini vision isn't set up.
pub fn active_provider_vision(config: &Config) -> Option<(String, String, String)> {
    // Get the ACTUAL active provider — don't iterate all providers,
    // otherwise MiniMax wins just because it's listed before OpenRouter.
    let (name, _) = config.providers.active_provider_and_model();

    // Custom entries take precedence over built-in names. If the user
    // created a custom provider literally named "opencode" / "ollama" /
    // anything that collides with a built-in id, the custom entry wins.
    let custom_cfg = if !name.starts_with("custom:")
        && config
            .providers
            .custom
            .as_ref()
            .is_some_and(|m| m.contains_key(name.as_str()))
    {
        config
            .providers
            .custom
            .as_ref()
            .and_then(|m| m.get(&name))
            .cloned()
    } else if let Some(custom_name) = name.strip_prefix("custom:") {
        config
            .providers
            .custom
            .as_ref()
            .and_then(|m| m.get(custom_name))
            .cloned()
    } else {
        None
    };

    // Try built-in registry
    let native_cfg: Option<&ProviderConfig> = REGISTRATIONS
        .iter()
        .find(|reg| reg.session_id == name || reg.aliases.contains(&name.as_str()))
        .and_then(|reg| (reg.config_field)(config));

    // Custom wins if it exists (same priority as create_provider_by_name)
    let active_cfg = custom_cfg.as_ref().or(native_cfg);

    if let Some(cfg) = active_cfg
        && let (Some(api_key), Some(vision_model)) = (&cfg.api_key, &cfg.vision_model)
    {
        let base_url = cfg
            .base_url
            .clone()
            .unwrap_or_else(|| "https://api.openai.com/v1/chat/completions".to_string());
        let base_url = if base_url.contains("/chat/completions") {
            base_url
        } else {
            format!("{}/chat/completions", base_url.trim_end_matches('/'))
        };
        return Some((api_key.clone(), base_url, vision_model.clone()));
    }
    // No provider-native vision — let cli/ui.rs fall back to Gemini if configured
    None
}

/// Returns `(api_key, base_url, generation_model)` for the active provider
/// when it has `generation_model` set in its config. Used by cli/ui.rs to
/// register `generate_image` against a non-Gemini, OpenAI-compatible images
/// endpoint (`/v1/images/generations`) instead of the global Gemini default.
///
/// `base_url` is normalized so the caller can append the trailing
/// `/images/generations` segment without worrying whether the user typed
/// `https://openrouter.ai/api/v1` or `.../v1/chat/completions`.
pub fn active_provider_generation(config: &Config) -> Option<(String, String, String)> {
    let (name, _) = config.providers.active_provider_and_model();

    let custom_cfg = if !name.starts_with("custom:")
        && config
            .providers
            .custom
            .as_ref()
            .is_some_and(|m| m.contains_key(name.as_str()))
    {
        config
            .providers
            .custom
            .as_ref()
            .and_then(|m| m.get(&name))
            .cloned()
    } else if let Some(custom_name) = name.strip_prefix("custom:") {
        config
            .providers
            .custom
            .as_ref()
            .and_then(|m| m.get(custom_name))
            .cloned()
    } else {
        None
    };

    let native_cfg: Option<&ProviderConfig> = REGISTRATIONS
        .iter()
        .find(|reg| reg.session_id == name || reg.aliases.contains(&name.as_str()))
        .and_then(|reg| (reg.config_field)(config));

    let active_cfg = custom_cfg.as_ref().or(native_cfg);

    if let Some(cfg) = active_cfg
        && let (Some(api_key), Some(generation_model)) = (&cfg.api_key, &cfg.generation_model)
    {
        // Strip any chat-specific trailing segment so the caller can append
        // `/images/generations` cleanly. Accepts the three URL shapes users
        // typically paste into custom-provider config: `…/v1`,
        // `…/v1/chat/completions`, or the bare host root.
        let raw = cfg
            .base_url
            .clone()
            .unwrap_or_else(|| "https://api.openai.com/v1".to_string());
        let base_url = raw
            .trim_end_matches("/chat/completions")
            .trim_end_matches('/')
            .to_string();
        return Some((api_key.clone(), base_url, generation_model.clone()));
    }
    None
}

/// Final model name to hand to `GenerateImageTool` — active provider's
/// `generation_model` override wins, otherwise fall back to the global
/// `image.generation.model`. Keeps the resolution decision next to its
/// vision counterpart so call sites stay plain wiring.
pub fn effective_generation_model(config: &Config) -> String {
    active_provider_generation(config)
        .map(|(_, _, m)| m)
        .unwrap_or_else(|| config.image.generation.model.clone())
}

// ── Individual provider factory functions ───────────────────────

/// Try to create GitHub Copilot provider if configured.
/// Uses OAuth token to exchange for short-lived Copilot API tokens.
fn try_create_github(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    use super::copilot::{COPILOT_CHAT_URL, CopilotTokenManager, copilot_extra_headers};

    let github_config = match &config.providers.github {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    let oauth_token = github_config.api_key.clone().filter(|k| !k.is_empty());

    let Some(oauth_token) = oauth_token else {
        tracing::warn!(
            "GitHub Copilot enabled but no OAuth token found. \
             Run /onboard:provider to authenticate."
        );
        return Ok(None);
    };

    // Create the token manager — background task does the initial + recurring refresh
    let manager = Arc::new(CopilotTokenManager::new(oauth_token));
    manager.clone().start_background_refresh();

    // Build a token_fn closure that reads the cached Copilot token
    let mgr_clone = manager.clone();
    let token_fn: super::custom_openai_compatible::TokenFn =
        Arc::new(move || mgr_clone.get_cached_token());

    let base_url = github_config
        .base_url
        .clone()
        .unwrap_or_else(|| COPILOT_CHAT_URL.to_string());

    tracing::info!("Using GitHub Copilot at: {}", base_url);

    let provider = configure_openai_compatible(
        OpenAIProvider::with_base_url("copilot-managed".to_string(), base_url)
            .with_name("GitHub Copilot")
            .with_token_fn(token_fn)
            .with_extra_headers(copilot_extra_headers()),
        github_config,
    );
    Ok(Some(Arc::new(provider)))
}

/// Default DashScope OpenAI-compatible chat-completions URL (China region).
/// Users can override via `[providers.qwen].base_url` for Singapore
/// (`dashscope-intl`), US (`dashscope-us`), or Coding Plan
/// (`coding.dashscope.aliyuncs.com/v1`).
const QWEN_DEFAULT_DASHSCOPE_URL: &str =
    "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

/// Try to create the **Qwen** provider backed by a DashScope API key.
///
/// OAuth and multi-account rotation were removed after Alibaba discontinued
/// Qwen OAuth. The provider now behaves like any other OpenAI-compatible
/// key-based provider (zhipu, openai, …): `api_key` + `base_url` +
/// `default_model` from `[providers.qwen]`.
async fn try_create_qwen(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let qwen_config = match &config.providers.qwen {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    let Some(api_key) = qwen_config.api_key.as_ref().filter(|k| !k.is_empty()) else {
        tracing::warn!("Qwen enabled but no API key configured — run /onboard:provider");
        return Ok(None);
    };

    let base_url = qwen_config
        .base_url
        .clone()
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| QWEN_DEFAULT_DASHSCOPE_URL.to_string());
    let base_url = if base_url.contains("/chat/completions") {
        base_url
    } else {
        format!("{}/chat/completions", base_url.trim_end_matches('/'))
    };

    tracing::info!("Using Qwen (DashScope) at: {}", base_url);

    let qwen_limiter = Arc::clone(&super::rate_limiter::QWEN_OAUTH_LIMITER);

    let enable_thinking = qwen_config.enable_thinking;
    let builder = OpenAIProvider::with_base_url(api_key.clone(), base_url)
        .with_name("qwen")
        .with_extra_headers(qwen_extra_headers())
        .with_body_transform(Arc::new(move |body| {
            let mut body = qwen_body_transform(body);
            if let Some(enable) = enable_thinking
                && let Some(obj) = body.as_object_mut()
            {
                obj.insert(
                    "enable_thinking".to_string(),
                    serde_json::Value::Bool(enable),
                );
            }
            body
        }))
        .with_rate_limiter(qwen_limiter);

    let provider = configure_openai_compatible(builder, qwen_config);
    Ok(Some(Arc::new(provider)))
}

/// Try to create OpenRouter provider if configured
fn try_create_openrouter(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let openrouter_config = match &config.providers.openrouter {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    let Some(api_key) = &openrouter_config.api_key else {
        tracing::warn!("OpenRouter enabled but API key missing — check keys.toml");
        return Ok(None);
    };

    let base_url = openrouter_config
        .base_url
        .clone()
        .unwrap_or_else(|| "https://openrouter.ai/api/v1/chat/completions".to_string());

    let model = openrouter_config
        .default_model
        .clone()
        .unwrap_or_else(|| "openai/gpt-4o".to_string());

    let is_free = model.ends_with(":free");

    tracing::info!(
        "Using OpenRouter at: {} (model={}, free={})",
        base_url,
        model,
        is_free
    );
    let mut provider = configure_openai_compatible(
        OpenAIProvider::with_base_url(api_key.clone(), base_url)
            .with_name("openrouter")
            .with_default_model(model.clone())
            .with_extra_headers(vec![
                ("X-Title".to_string(), "Open Crabs".to_string()),
                (
                    "HTTP-Referer".to_string(),
                    "https://opencrabs.com".to_string(),
                ),
            ]),
        openrouter_config,
    );

    // Proactive pacing for :free models — per-model rate limiter shared across
    // all provider instances. Enforces 4s between requests per model (~15 req/min,
    // safely under OpenRouter's 20 req/min window with 25% headroom).
    if is_free {
        let limiter = super::rate_limiter::OPENROUTER_FREE_LIMITERS.get(&model);
        provider = provider.with_rate_limiter(limiter);
        tracing::info!("Rate limiter attached for :free model: {}", model);
    }

    Ok(Some(Arc::new(provider)))
}

/// Try to create Minimax provider if configured
fn try_create_minimax(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let minimax_config = match &config.providers.minimax {
        Some(cfg) => {
            tracing::debug!(
                "Minimax config: enabled={}, has_key={}",
                cfg.enabled,
                cfg.api_key.is_some()
            );
            cfg
        }
        None => return Ok(None),
    };

    let Some(api_key) = &minimax_config.api_key else {
        tracing::warn!("Minimax enabled but API key missing — check keys.toml");
        return Ok(None);
    };

    // MiniMax requires specific endpoint path, not just /v1
    let base_url = minimax_config
        .base_url
        .clone()
        .unwrap_or_else(|| "https://api.minimax.io/v1".to_string());

    // Append correct path if not already present
    let full_url = if base_url.contains("minimax.io") && !base_url.contains("/text/") {
        format!("{}/text/chatcompletion_v2", base_url.trim_end_matches('/'))
    } else {
        base_url
    };

    tracing::info!("Using Minimax at: {}", full_url);
    let mut provider = configure_openai_compatible(
        OpenAIProvider::with_base_url(api_key.clone(), full_url).with_name("minimax"),
        minimax_config,
    );

    // MiniMax M2.7/M2.5 doesn't support vision — default to MiniMax-Text-01 in-memory.
    // Do NOT write to config here — this runs inside ConfigWatcher callbacks and
    // writing triggers another reload → infinite loop → crash.
    if minimax_config.vision_model.is_none() {
        provider = provider.with_vision_model("MiniMax-Text-01".to_string());
    }

    Ok(Some(Arc::new(provider)))
}

/// Try to create z.ai GLM provider if configured
/// Supports two endpoint types: "api" (general) or "coding" (coding-specific)
fn try_create_zhipu(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let zhipu_config = match &config.providers.zhipu {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    let Some(api_key) = &zhipu_config.api_key else {
        tracing::warn!("z.ai GLM enabled but API key missing — check keys.toml");
        return Ok(None);
    };

    // Determine base URL based on endpoint_type
    // API endpoint: https://api.z.ai/api/paas/v4
    // Coding endpoint: https://api.z.ai/api/coding/paas/v4
    let base_url = match zhipu_config.endpoint_type.as_deref() {
        Some("coding") => "https://api.z.ai/api/coding/paas/v4/chat/completions",
        _ => "https://api.z.ai/api/paas/v4/chat/completions",
    };

    tracing::info!(
        "Using z.ai GLM at: {} (endpoint_type: {:?})",
        base_url,
        zhipu_config.endpoint_type
    );
    let provider = configure_openai_compatible(
        OpenAIProvider::with_base_url(api_key.clone(), base_url.to_string()).with_name("zhipu"),
        zhipu_config,
    );
    Ok(Some(Arc::new(provider)))
}

/// Try to create Ollama provider if configured.
/// Supports both local (localhost:11434, no API key) and cloud (api.ollama.com, optional key).
fn try_create_ollama(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let ollama_config = match &config.providers.ollama {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    // Default to local Ollama if no base_url specified
    let base_url = ollama_config
        .base_url
        .clone()
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| "http://localhost:11434/v1/chat/completions".to_string());

    let base_url = if base_url.contains("/chat/completions") {
        base_url
    } else {
        format!("{}/chat/completions", base_url.trim_end_matches('/'))
    };

    // API key is optional for local Ollama
    let api_key = ollama_config.api_key.clone().unwrap_or_default();

    tracing::info!(
        "Using Ollama at: {} (has_key={})",
        base_url,
        !api_key.is_empty()
    );

    let mut builder = OpenAIProvider::with_base_url(api_key, base_url.clone()).with_name("ollama");
    if is_local_base_url(&base_url) {
        let enable = ollama_config.enable_thinking.unwrap_or(true);
        builder = builder.with_body_transform(local_thinking_body_transform(enable));
    }
    let provider = configure_openai_compatible(builder, ollama_config);
    Ok(Some(Arc::new(provider)))
}

/// Try to create Custom OpenAI-compatible provider if configured.
/// Picks the first enabled named custom provider from the map.
fn try_create_custom(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let (name, custom_config) = match config.providers.active_custom() {
        Some((n, c)) => (n.to_string(), c.clone()),
        None => {
            tracing::warn!("Custom provider requested but no active custom provider found");
            return Ok(None);
        }
    };

    // API key is optional for local providers (LM Studio, Ollama, etc.)
    let api_key = custom_config.api_key.clone().unwrap_or_default();

    // Same rationale as try_create_custom_by_name: refuse to silently default
    // to localhost:1234 so Discord / other channels don't hit a dead
    // LM Studio URL the user never configured.
    let Some(mut base_url) = custom_config.base_url.clone() else {
        tracing::warn!(
            "Custom provider '{}' has no base_url configured — skipping (run /onboard:provider)",
            name
        );
        return Ok(None);
    };

    // Auto-append /chat/completions if missing — all OpenAI-compatible APIs need it
    if !base_url.contains("/chat/completions") {
        base_url = format!("{}/chat/completions", base_url.trim_end_matches('/'));
    }

    tracing::info!(
        "Using Custom OpenAI-compatible '{}' at: {} (has_key={})",
        name,
        base_url,
        !api_key.is_empty()
    );
    let mut builder = OpenAIProvider::with_base_url(api_key, base_url.clone()).with_name(&name);
    if is_local_base_url(&base_url) {
        let enable = custom_config.enable_thinking.unwrap_or(true);
        builder = builder.with_body_transform(local_thinking_body_transform(enable));
    }
    let provider = configure_openai_compatible(builder, &custom_config);
    Ok(Some(Arc::new(provider)))
}

/// Configure OpenAI-compatible provider with custom model
fn configure_openai_compatible(
    mut provider: OpenAIProvider,
    config: &ProviderConfig,
) -> OpenAIProvider {
    tracing::debug!(
        "configure_openai_compatible: default_model = {:?}",
        config.default_model
    );
    if let Some(model) = &config.default_model {
        tracing::info!("Using custom default model: {}", model);
        provider = provider.with_default_model(model.clone());
    }
    if let Some(vm) = &config.vision_model {
        tracing::info!("Vision model configured: {}", vm);
        provider = provider.with_vision_model(vm.clone());
    }
    if let Some(cw) = config.context_window {
        tracing::info!("Context window configured: {} tokens", cw);
        provider = provider.with_context_window(cw);
    }
    if !config.models.is_empty() {
        tracing::debug!(
            "Loaded {} configured models for provider",
            config.models.len()
        );
        provider = provider.with_models(config.models.clone());
    }
    // OpenRouter response caching
    if let Some(cache) = config.cache_enabled {
        provider = provider.with_cache_enabled(cache);
        if cache {
            tracing::info!("OpenRouter response caching enabled");
        }
    }
    if let Some(ttl) = config.cache_ttl {
        provider = provider.with_cache_ttl(ttl);
        tracing::info!("OpenRouter cache TTL: {}s", ttl);
    }
    provider
}

/// Try to create OpenAI provider if configured
fn try_create_openai(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let openai_config = match &config.providers.openai {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    // Local LLM (LM Studio, Ollama, etc.) - has base_url but NO api_key
    if let Some(base_url) = &openai_config.base_url
        && openai_config.api_key.is_none()
    {
        tracing::info!("Using local LLM at: {}", base_url);
        let mut builder = OpenAIProvider::local(base_url.clone()).with_name("openai");
        if is_local_base_url(base_url) {
            let enable = openai_config.enable_thinking.unwrap_or(true);
            builder = builder.with_body_transform(local_thinking_body_transform(enable));
        }
        let provider = configure_openai_compatible(builder, openai_config);
        return Ok(Some(Arc::new(provider)));
    }

    // Official OpenAI API - has api_key
    if let Some(api_key) = &openai_config.api_key {
        tracing::info!("Using OpenAI provider");
        let provider = configure_openai_compatible(
            OpenAIProvider::new(api_key.clone()).with_name("openai"),
            openai_config,
        );
        return Ok(Some(Arc::new(provider)));
    }

    tracing::warn!("OpenAI enabled but no API key and no base_url — check keys.toml");
    Ok(None)
}

/// Try to create Gemini provider if configured
fn try_create_gemini(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let gemini_config = match &config.providers.gemini {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    let api_key = match &gemini_config.api_key {
        Some(key) if !key.is_empty() => key.clone(),
        _ => {
            tracing::warn!("Gemini enabled but API key missing — check keys.toml");
            return Ok(None);
        }
    };

    let model = gemini_config
        .default_model
        .clone()
        .unwrap_or_else(|| "gemini-2.0-flash".to_string());

    tracing::info!("Using Gemini provider with model: {}", model);
    Ok(Some(Arc::new(
        GeminiProvider::new(api_key).with_model(model),
    )))
}

/// Try to create Claude CLI provider if configured and binary is available.
fn try_create_claude_cli(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let cli_config = match &config.providers.claude_cli {
        Some(cfg) if cfg.enabled => cfg,
        _ => return Ok(None),
    };

    match ClaudeCliProvider::new() {
        Ok(mut provider) => {
            if let Some(model) = &cli_config.default_model {
                provider = provider.with_default_model(model.clone());
            }
            tracing::info!("Using Claude CLI provider (Max subscription, no API key needed)");
            Ok(Some(Arc::new(provider)))
        }
        Err(e) => {
            tracing::warn!("Claude CLI enabled but binary not found: {}", e);
            Ok(None)
        }
    }
}

/// Try to create OpenCode CLI provider if configured and binary is available.
fn try_create_opencode_cli(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let cli_config = match &config.providers.opencode_cli {
        Some(cfg) if cfg.enabled => cfg,
        _ => return Ok(None),
    };

    match OpenCodeCliProvider::new() {
        Ok(mut provider) => {
            if let Some(model) = &cli_config.default_model {
                provider = provider.with_default_model(model.clone());
            }
            tracing::info!("Using OpenCode CLI provider (free models, no API key needed)");
            Ok(Some(Arc::new(provider)))
        }
        Err(e) => {
            tracing::warn!("OpenCode CLI enabled but binary not found: {}", e);
            Ok(None)
        }
    }
}

/// Try to create Codex CLI provider if configured and binary is available.
fn try_create_codex_cli(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let cli_config = match &config.providers.codex_cli {
        Some(cfg) if cfg.enabled => cfg,
        _ => return Ok(None),
    };

    match CodexCliProvider::new() {
        Ok(mut provider) => {
            if let Some(model) = &cli_config.default_model {
                provider = provider.with_default_model(model.clone());
            }
            tracing::info!(
                "Using Codex CLI provider (ChatGPT/Codex subscription, no API key needed)"
            );
            Ok(Some(Arc::new(provider)))
        }
        Err(e) => {
            tracing::warn!("Codex CLI enabled but binary not found: {}", e);
            Ok(None)
        }
    }
}

/// Try to create Codex OAuth provider if configured and tokens are available.
fn try_create_codex_oauth(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let codex_config = match &config.providers.codex {
        Some(cfg) if cfg.enabled => cfg,
        _ => return Ok(None),
    };

    match CodexOAuthProvider::new() {
        Ok(mut provider) => {
            if let Some(model) = &codex_config.default_model {
                provider = provider.with_default_model(model.clone());
            }
            tracing::info!("Using Codex provider (OAuth authenticated, no API key needed)");
            Ok(Some(Arc::new(provider)))
        }
        Err(e) => {
            tracing::warn!("Codex OAuth enabled but not authenticated: {}", e);
            Ok(None)
        }
    }
}

/// Try to create OpenCode API provider if configured (Go/Zen plans)
async fn try_create_opencode(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let opencode_config = match &config.providers.opencode {
        Some(cfg) if cfg.enabled => cfg,
        _ => return Ok(None),
    };

    let Some(api_key) = &opencode_config.api_key else {
        tracing::warn!("OpenCode enabled but API key missing — check keys.toml");
        return Ok(None);
    };

    let base_url = opencode_config
        .base_url
        .clone()
        .unwrap_or_else(|| "https://opencode.ai/zen/go/v1/chat/completions".to_string());

    let model = opencode_config
        .default_model
        .clone()
        .unwrap_or_else(|| "qwen3.6-plus".to_string());

    tracing::info!("Using OpenCode API at: {} (model={})", base_url, model);

    let provider = configure_openai_compatible(
        OpenAIProvider::with_base_url(api_key.clone(), base_url)
            .with_name("opencode")
            .with_default_model(model.clone()),
        opencode_config,
    );

    Ok(Some(Arc::new(provider)))
}

/// Try to create Anthropic provider if configured
fn try_create_anthropic(config: &Config) -> Result<Option<Arc<dyn Provider>>> {
    let anthropic_config = match &config.providers.anthropic {
        Some(cfg) => cfg,
        None => return Ok(None),
    };

    let api_key = match &anthropic_config.api_key {
        Some(key) => key.clone(),
        None => {
            tracing::warn!("Anthropic enabled but API key missing — check keys.toml");
            return Ok(None);
        }
    };

    let mut provider = AnthropicProvider::new(api_key);

    if let Some(model) = &anthropic_config.default_model {
        tracing::info!("Using custom default model: {}", model);
        provider = provider.with_default_model(model.clone());
    }

    tracing::info!("Using Anthropic provider");

    Ok(Some(Arc::new(provider)))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Config, ProviderConfig, ProviderConfigs};

    #[tokio::test]
    async fn test_create_provider_with_anthropic() {
        let config = Config {
            providers: ProviderConfigs {
                anthropic: Some(ProviderConfig {
                    enabled: true,
                    api_key: Some("test-key".to_string()),
                    base_url: None,
                    default_model: None,
                    models: vec![],
                    vision_model: None,
                    ..Default::default()
                }),
                ..Default::default()
            },
            ..Default::default()
        };

        let result = create_provider(&config).await;
        assert!(result.is_ok());
        let provider = result.unwrap();
        assert_eq!(provider.name(), "anthropic");
    }

    #[tokio::test]
    async fn test_create_provider_with_minimax() {
        let config = Config {
            providers: ProviderConfigs {
                minimax: Some(ProviderConfig {
                    enabled: true,
                    api_key: Some("test-key".to_string()),
                    base_url: Some("https://api.minimax.io/v1".to_string()),
                    default_model: Some("MiniMax-M2.7".to_string()),
                    models: vec![],
                    vision_model: None,
                    ..Default::default()
                }),
                ..Default::default()
            },
            ..Default::default()
        };

        let result = create_provider(&config).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_minimax_takes_priority() {
        let config = Config {
            providers: ProviderConfigs {
                openai: Some(ProviderConfig {
                    enabled: true,
                    api_key: Some("openai-key".to_string()),
                    base_url: None,
                    default_model: None,
                    models: vec![],
                    vision_model: None,
                    ..Default::default()
                }),
                minimax: Some(ProviderConfig {
                    enabled: true,
                    api_key: Some("minimax-key".to_string()),
                    base_url: Some("https://api.minimax.io/v1".to_string()),
                    default_model: None,
                    models: vec![],
                    vision_model: None,
                    ..Default::default()
                }),
                ..Default::default()
            },
            ..Default::default()
        };

        let result = create_provider(&config).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_create_provider_no_credentials() {
        let config = Config {
            providers: ProviderConfigs::default(),
            ..Default::default()
        };

        // No credentials → PlaceholderProvider (app starts, shows onboarding)
        let result = create_provider(&config).await;
        assert!(result.is_ok());
    }
}