llmman 0.1.689

Run any agent on any model, models stored as OCI images
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
//! `llmman launch opencode`.

use std::path::{Path, PathBuf};

use anyhow::Context;

use super::common::write_json_merged;
use super::optimizer::Optimizer;
use super::{
    exec_with_env, find_on_path, server, thinking_choices, xdg_dir, Thinking, WINDOWS_PATH_EXTS,
};

/// opencode: a JSON config via OPENCODE_CONFIG_CONTENT pointing at our
/// /v1 endpoint, with the model's thinking variants, its window and, for
/// a vision model, image input. `--variant` becomes the model's default
/// options and its selection in opencode's state.
#[allow(clippy::too_many_arguments)]
pub(super) fn launch_opencode(
    model: &str,
    api_key: &str,
    thinking: Option<&Thinking>,
    variant: Option<&str>,
    vision: bool,
    context_window: Option<u64>,
    max_output: Option<u32>,
    extra_args: &[String],
    optimizer: Optimizer,
) -> anyhow::Result<()> {
    let bin = find_opencode().ok_or_else(|| anyhow::anyhow!("opencode is not installed"))?;
    let added = optimizer.addition("opencode")?;

    let effective_model = if model.is_empty() { "default" } else { model };
    let variants = opencode_variants(thinking);
    let options = variant.and_then(|v| variants.iter().find(|(name, _)| *name == v));
    let config = opencode_config(
        added.server.as_deref().unwrap_or(&server()),
        effective_model,
        api_key,
        &variants,
        options.map(|(_, options)| options),
        vision,
        context_window,
        max_output,
    );
    let config = with_added_config(&config, added.config.as_ref());
    if let Some(variant) = variant {
        write_opencode_variant(&opencode_state_dir()?, effective_model, variant)?;
    }

    if let Some(notice) = optimizer.notice("opencode") {
        eprintln!("{notice}");
    }
    exec_with_env(&bin, extra_args, &[("OPENCODE_CONFIG_CONTENT", &config)])
}

/// `config` (a JSON object) with the keys of `added` appended, so the order
/// the rest of it was written in (the variants') is kept.
fn with_added_config(config: &str, added: Option<&serde_json::Value>) -> String {
    let Some(added) = added.and_then(serde_json::Value::as_object) else {
        return config.to_string();
    };
    let mut out = config.trim_end().trim_end_matches('}').to_string();
    for (key, value) in added {
        out.push_str(&format!(
            ",{}:{value}",
            serde_json::Value::from(key.as_str())
        ));
    }
    out.push('}');
    out
}

/// Refuses an opencode before 2, which has not the plugins `tk` adds.
pub(super) fn ensure_plugins() -> anyhow::Result<()> {
    let bin = find_opencode().ok_or_else(|| anyhow::anyhow!("opencode is not installed"))?;
    let out = std::process::Command::new(&bin)
        .arg("--version")
        .output()
        .with_context(|| format!("run {} --version", bin.display()))?;
    let version = String::from_utf8_lossy(&out.stdout);
    anyhow::ensure!(
        major_version(&version).is_some_and(|major| major >= 2),
        "--optimizer tk needs opencode 2 or later, which has the plugin hook it uses; \
         this is {}.\nUpgrade opencode, or launch without --optimizer.",
        version.trim()
    );
    Ok(())
}

/// The major version in `opencode --version`'s output (`opencode v2.3.0`).
fn major_version(output: &str) -> Option<u64> {
    let version = output.trim().rsplit(' ').next()?;
    version
        .trim_start_matches('v')
        .split('.')
        .next()?
        .parse()
        .ok()
}

/// opencode's state directory, where its `xdg-basedir` puts it.
pub(super) fn opencode_state_dir() -> anyhow::Result<PathBuf> {
    let home = crate::config::home_dir().context("no home directory")?;
    Ok(xdg_dir(&home, "XDG_STATE_HOME", ".local/state").join("opencode"))
}

/// How opencode names `model` on llmman's provider.
fn opencode_model_ref(model: &str) -> String {
    format!("ollama/{model}")
}

/// Selects `variant` for `model` in opencode's `model.json`, as ctrl+t
/// does. Its UI sends that selection, which outranks the model's
/// `options`; only `opencode run` sends those alone.
fn write_opencode_variant(state_dir: &Path, model: &str, variant: &str) -> anyhow::Result<()> {
    write_json_merged(&state_dir.join("model.json"), "opencode", |existing| {
        let mut merged = existing.clone();
        if !merged["variant"].is_object() {
            merged["variant"] = serde_json::json!({});
        }
        merged["variant"][opencode_model_ref(model)] = variant.into();
        merged
    })
}

/// opencode's `variants` for the model, in cycle order (`variant_cycle`,
/// ctrl+t by default): [`thinking_choices`]. Each variant is the request
/// options `@ai-sdk/openai-compatible` sends: `reasoningEffort` as
/// `reasoning_effort`, other keys verbatim. opencode derives variants
/// only for models it knows from models.dev, so without these a model
/// has nothing to cycle.
/// A switch-only model's two variants set both keys, so either overrides
/// the other when `--variant` put it in the model's options.
pub(super) fn opencode_variants(thinking: Option<&Thinking>) -> Vec<(&str, serde_json::Value)> {
    let choices = thinking_choices(thinking);
    let switch = choices.contains(&"thinking");
    choices
        .into_iter()
        .map(|choice| {
            let options = match choice {
                "none" if switch => serde_json::json!({
                    "reasoningEffort": "none",
                    "chat_template_kwargs": { "enable_thinking": false },
                }),
                "thinking" => serde_json::json!({
                    "reasoningEffort": "medium",
                    "chat_template_kwargs": { "enable_thinking": true },
                }),
                level => serde_json::json!({ "reasoningEffort": level }),
            };
            (choice, options)
        })
        .collect()
}

/// Finds opencode on `PATH`, then where its installers put it. The second
/// check finds a fresh install that this process's `PATH` doesn't include
/// yet.
pub(super) fn find_opencode() -> Option<PathBuf> {
    find_on_path("opencode").or_else(|| opencode_fallback_paths().into_iter().find(|p| p.is_file()))
}

/// Where opencode's installers put it: `~/.opencode/bin` (install script)
/// and, on Windows, `%APPDATA%\npm` (`npm install -g`).
fn opencode_fallback_paths() -> Vec<PathBuf> {
    let home = dirs::home_dir();
    if cfg!(windows) {
        let mut paths: Vec<PathBuf> = home
            .iter()
            .map(|h| h.join(".opencode").join("bin").join("opencode.exe"))
            .collect();
        // Treat an empty APPDATA as unset.
        let roaming = std::env::var_os("APPDATA")
            .filter(|d| !d.is_empty())
            .map(PathBuf::from)
            .or_else(|| home.as_ref().map(|h| h.join("AppData").join("Roaming")));
        if let Some(npm) = roaming.map(|d| d.join("npm")) {
            paths.extend(
                WINDOWS_PATH_EXTS
                    .iter()
                    .map(|ext| npm.join(format!("opencode.{ext}"))),
            );
        }
        return paths;
    }
    home.iter()
        .map(|h| h.join(".opencode").join("bin").join("opencode"))
        .collect()
}

/// opencode's own `OUTPUT_TOKEN_MAX` (`provider/transform.ts`), the cap
/// it applies to `limit.output` and the value it substitutes for a 0.
const OPENCODE_OUTPUT_TOKEN_MAX: u64 = 32_000;

/// `limit.output` for a window of `context` when the catalog names no
/// real ceiling: a quarter, capped at [`OPENCODE_OUTPUT_TOKEN_MAX`],
/// never 0.
///
/// opencode spends this field twice — the `maxOutputTokens` it sends
/// and the headroom it keeps before compacting — capping both at its
/// own [`OPENCODE_OUTPUT_TOKEN_MAX`], which is what makes a catalog
/// ceiling of any size safe to pass. A quarter is the guess where
/// there is none: longer than one turn produces, short enough to leave
/// the window mostly usable.
fn opencode_output_reserve(context: u64) -> u64 {
    (context / 4).clamp(1, OPENCODE_OUTPUT_TOKEN_MAX)
}

/// The `OPENCODE_CONFIG_CONTENT` for `model` at `server`. Structs rather
/// than `json!`, whose map sorts keys: opencode cycles variants in the
/// order listed. No variants leaves the key out.
#[allow(clippy::too_many_arguments)]
fn opencode_config(
    server: &str,
    model: &str,
    api_key: &str,
    variants: &[(&str, serde_json::Value)],
    options: Option<&serde_json::Value>,
    vision: bool,
    context_window: Option<u64>,
    max_output: Option<u32>,
) -> String {
    use serde::ser::{SerializeMap, Serializer};

    /// An object with runtime keys, in the order given.
    fn entries<S: Serializer, V: serde::Serialize>(
        entries: &[(&str, V)],
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        let mut map = serializer.serialize_map(Some(entries.len()))?;
        for (key, value) in entries {
            map.serialize_entry(key, value)?;
        }
        map.end()
    }

    #[derive(serde::Serialize)]
    struct Config<'a> {
        #[serde(rename = "$schema")]
        schema: &'static str,
        provider: Providers<'a>,
        model: String,
    }
    #[derive(serde::Serialize)]
    struct Providers<'a> {
        ollama: Provider<'a>,
    }
    #[derive(serde::Serialize)]
    struct Provider<'a> {
        npm: &'static str,
        name: &'static str,
        options: Options<'a>,
        #[serde(serialize_with = "entries")]
        models: [(&'a str, Model<'a>); 1],
    }
    #[derive(serde::Serialize)]
    struct Options<'a> {
        #[serde(rename = "baseURL")]
        base_url: String,
        #[serde(rename = "apiKey")]
        api_key: &'a str,
    }
    #[derive(serde::Serialize)]
    struct Model<'a> {
        name: &'a str,
        #[serde(serialize_with = "entries", skip_serializing_if = "<[_]>::is_empty")]
        variants: &'a [(&'a str, serde_json::Value)],
        #[serde(skip_serializing_if = "Option::is_none")]
        options: Option<&'a serde_json::Value>,
        #[serde(skip_serializing_if = "Option::is_none")]
        modalities: Option<Modalities>,
        #[serde(skip_serializing_if = "Option::is_none")]
        attachment: Option<bool>,
        #[serde(skip_serializing_if = "Option::is_none")]
        limit: Option<Limit>,
    }
    #[derive(serde::Serialize)]
    struct Modalities {
        input: &'static [&'static str],
        output: &'static [&'static str],
    }
    /// opencode's schema requires both fields once `limit` is present.
    #[derive(serde::Serialize)]
    struct Limit {
        context: u64,
        output: u64,
    }

    // Declare image input for a vision model so opencode will attach
    // images; a text-only model gets neither key.
    let modalities = vision.then_some(Modalities {
        input: &["text", "image"],
        output: &["text"],
    });

    // Without `limit`, opencode normalizes a config-defined model to
    // `{context: 0, output: 0}` and then skips overflow detection
    // entirely for a 0 context (`session/overflow.ts`, `isOverflow`), so
    // a session never auto-compacts. Declared only when llmman knows the
    // window; otherwise the key stays out rather than assert a guess.
    let limit = context_window.map(|context| Limit {
        context,
        // The catalog's own ceiling wherever there is one; derived
        // only for a local model, which has no catalog to name one.
        output: max_output.map_or_else(|| opencode_output_reserve(context), u64::from),
    });

    let config = Config {
        schema: "https://opencode.ai/config.json",
        provider: Providers {
            // The id stays `ollama`: saved model selections are keyed by it
            // (`ollama/<model>`). Only the name opencode shows beside the
            // model changes, and the provider is llmman, not Ollama.
            ollama: Provider {
                npm: "@ai-sdk/openai-compatible",
                name: "llmman",
                options: Options {
                    base_url: format!("{server}/v1"),
                    api_key,
                },
                models: [(
                    model,
                    Model {
                        name: model,
                        variants,
                        options,
                        modalities,
                        attachment: vision.then_some(true),
                        limit,
                    },
                )],
            },
        },
        model: opencode_model_ref(model),
    };
    serde_json::to_string(&config).expect("opencode config serializes")
}

#[cfg(test)]
mod tests {
    use super::super::PORTABLE_THINKING_LEVELS;
    use super::*;
    use crate::chat_template::ThinkingControls;

    /// The config points at the daemon's `/v1` and lists the variants in
    /// the order given (a parsed `Value` would re-sort them).
    #[test]
    fn opencode_config_lists_the_variants_in_order() {
        let variants = opencode_variants(None);
        let text = opencode_config(
            "http://127.0.0.1:17434",
            "qwen3.5:0.8b",
            "k",
            &variants,
            None,
            false,
            None,
            None,
        );
        let config: serde_json::Value = serde_json::from_str(&text).expect("valid JSON");
        assert_eq!(config["$schema"], "https://opencode.ai/config.json");
        assert_eq!(config["model"], "ollama/qwen3.5:0.8b");
        let provider = &config["provider"]["ollama"];
        assert_eq!(provider["npm"], "@ai-sdk/openai-compatible");
        assert_eq!(provider["name"], "llmman");
        assert_eq!(provider["options"]["baseURL"], "http://127.0.0.1:17434/v1");
        assert_eq!(provider["options"]["apiKey"], "k");
        assert_eq!(provider["models"].as_object().map(|m| m.len()), Some(1));

        let model = &provider["models"]["qwen3.5:0.8b"];
        assert_eq!(model["name"], "qwen3.5:0.8b");
        let written = model["variants"].as_object().expect("variants object");
        assert_eq!(written.len(), variants.len());
        for (name, options) in &variants {
            assert_eq!(&written[*name], options, "variant {name}");
        }
        let positions: Vec<usize> = variants
            .iter()
            .map(|(name, _)| text.find(&format!("\"{name}\"")).expect(name))
            .collect();
        assert!(positions.windows(2).all(|w| w[0] < w[1]), "{text}");

        let bare = opencode_config("http://h", "m", "k", &[], None, false, None, None);
        assert!(!bare.contains("variants"), "{bare}");
    }

    /// `--variant` makes its options the model's own, which requests that
    /// name no variant (all of `opencode run`'s) carry.
    #[test]
    fn opencode_config_starts_the_model_at_the_variant() {
        let variants = opencode_variants(None);
        let high = variants.iter().find(|(name, _)| *name == "high").unwrap();
        let text = opencode_config(
            "http://h",
            "m",
            "k",
            &variants,
            Some(&high.1),
            false,
            None,
            None,
        );
        let config: serde_json::Value = serde_json::from_str(&text).expect("valid JSON");
        let model = &config["provider"]["ollama"]["models"]["m"];
        assert_eq!(
            model["options"],
            serde_json::json!({ "reasoningEffort": "high" })
        );

        let text = opencode_config("http://h", "m", "k", &variants, None, false, None, None);
        let config: serde_json::Value = serde_json::from_str(&text).expect("valid JSON");
        assert!(config["provider"]["ollama"]["models"]["m"]["options"].is_null());
    }

    #[test]
    fn write_opencode_variant_selects_the_model_and_keeps_the_rest() {
        let dir = std::env::temp_dir().join(format!(
            "llmman-opencode-state-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let path = dir.join("state").join("opencode").join("model.json");
        let state = path.parent().unwrap();
        let read = || -> serde_json::Value {
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap()
        };

        // No file yet.
        write_opencode_variant(state, "m", "xhigh").unwrap();
        assert_eq!(
            read(),
            serde_json::json!({ "variant": { "ollama/m": "xhigh" } })
        );

        // Another model's selection and the rest of the state stay.
        std::fs::write(
            &path,
            r#"{"recent":[{"modelID":"o"}],"variant":{"ollama/m":"medium","ollama/o":"low"}}"#,
        )
        .unwrap();
        write_opencode_variant(state, "m", "xhigh").unwrap();
        let written = read();
        assert_eq!(written["variant"]["ollama/m"], "xhigh");
        assert_eq!(written["variant"]["ollama/o"], "low");
        assert_eq!(written["recent"][0]["modelID"], "o");

        // A `variant` that is not an object is replaced; a file that is not
        // a JSON object is left alone, as opencode resets it itself.
        std::fs::write(&path, r#"{"variant":"high"}"#).unwrap();
        write_opencode_variant(state, "m", "low").unwrap();
        assert_eq!(read()["variant"], serde_json::json!({ "ollama/m": "low" }));
        std::fs::write(&path, "[1, 2").unwrap();
        write_opencode_variant(state, "m", "low").unwrap();
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "[1, 2");

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn opencode_config_declares_image_input_only_for_a_vision_model() {
        let text = opencode_config("http://h", "m", "k", &[], None, true, None, None);
        let config: serde_json::Value = serde_json::from_str(&text).expect("valid JSON");
        let model = &config["provider"]["ollama"]["models"]["m"];
        assert_eq!(
            model["modalities"],
            serde_json::json!({ "input": ["text", "image"], "output": ["text"] })
        );
        assert_eq!(model["attachment"], true);

        let text_only = opencode_config("http://h", "m", "k", &[], None, false, None, None);
        assert!(!text_only.contains("modalities"), "{text_only}");
        assert!(!text_only.contains("attachment"), "{text_only}");
    }

    /// The reserve scales with the window, so a small one keeps a
    /// usable budget.
    #[test]
    fn opencode_output_reserve_scales_with_the_window_and_is_never_zero() {
        let cases = [
            (4096, 1024),
            (32768, 8192),
            (131072, 32000),
            // Capped however large the window.
            (1 << 20, OPENCODE_OUTPUT_TOKEN_MAX),
            // Never 0, which opencode would replace with its own max.
            (1, 1),
            (3, 1),
        ];
        for (context, want) in cases {
            assert_eq!(opencode_output_reserve(context), want, "context={context}");
        }
    }

    /// Without `limit` opencode never auto-compacts, so the window has
    /// to travel.
    #[test]
    fn opencode_config_declares_the_window_only_when_it_is_known() {
        let text = opencode_config("http://h", "m", "k", &[], None, false, Some(8192), None);
        let config: serde_json::Value = serde_json::from_str(&text).expect("valid JSON");
        let limit = &config["provider"]["ollama"]["models"]["m"]["limit"];
        assert_eq!(limit["context"], 8192);
        // Both keys are required once `limit` is present.
        assert_eq!(limit["output"], 2048);

        // No window: the key stays out and opencode keeps its defaults.
        let unknown = opencode_config("http://h", "m", "k", &[], None, false, None, None);
        assert!(!unknown.contains("limit"), "{unknown}");
    }

    /// opencode sends `limit.output` as the request's max output, so a
    /// hosted model's own ceiling has to travel: deriving one from the
    /// window advertises more than the provider will accept.
    #[test]
    fn opencode_config_prefers_the_catalogs_output_ceiling_to_a_derived_one() {
        let hosted = opencode_config(
            "http://h",
            "m",
            "k",
            &[],
            None,
            false,
            Some(128_000),
            Some(8_192),
        );
        let config: serde_json::Value = serde_json::from_str(&hosted).expect("valid JSON");
        let limit = &config["provider"]["ollama"]["models"]["m"]["limit"];
        assert_eq!(limit["context"], 128_000);
        assert_eq!(
            limit["output"], 8_192,
            "the catalog's ceiling, not a quarter of the window"
        );

        // A local model has no catalog and no ceiling of its own, so the
        // derived reserve still stands.
        let local = opencode_config("http://h", "m", "k", &[], None, false, Some(128_000), None);
        let config: serde_json::Value = serde_json::from_str(&local).expect("valid JSON");
        assert_eq!(
            config["provider"]["ollama"]["models"]["m"]["limit"]["output"],
            OPENCODE_OUTPUT_TOKEN_MAX
        );
    }

    #[test]
    fn opencode_config_escapes_the_model_name() {
        let model = "we\"ird/mo\\del";
        let config: serde_json::Value = serde_json::from_str(&opencode_config(
            "http://h",
            model,
            "k",
            &[],
            None,
            false,
            None,
            None,
        ))
        .expect("valid JSON");
        assert_eq!(config["model"], format!("ollama/{model}"));
        assert_eq!(config["provider"]["ollama"]["models"][model]["name"], model);
    }

    /// Each choice becomes the options that select it; no template means
    /// the portable set, no thinking means no variants.
    #[test]
    fn opencode_variants_follow_the_templates_controls() {
        let gemma4 = ThinkingControls {
            thinks: true,
            enable_thinking: true,
            efforts: vec![],
            default_on: false,
            default_effort: None,
        };
        assert_eq!(
            opencode_variants(Some(&Thinking::Template(gemma4.clone()))),
            [
                (
                    "none",
                    serde_json::json!({
                        "reasoningEffort": "none",
                        "chat_template_kwargs": { "enable_thinking": false },
                    })
                ),
                (
                    "thinking",
                    serde_json::json!({
                        "reasoningEffort": "medium",
                        "chat_template_kwargs": { "enable_thinking": true },
                    })
                ),
            ]
        );
        let qwen3_8 = ThinkingControls {
            efforts: vec!["low", "medium", "xhigh"],
            ..gemma4
        };
        assert_eq!(
            opencode_variants(Some(&Thinking::Template(qwen3_8))),
            [
                ("none", serde_json::json!({ "reasoningEffort": "none" })),
                ("low", serde_json::json!({ "reasoningEffort": "low" })),
                ("medium", serde_json::json!({ "reasoningEffort": "medium" })),
                ("xhigh", serde_json::json!({ "reasoningEffort": "xhigh" })),
            ]
        );
        let plain = Thinking::Template(ThinkingControls::default());
        assert!(opencode_variants(Some(&plain)).is_empty());
        let fallback = opencode_variants(None);
        assert_eq!(fallback.len(), PORTABLE_THINKING_LEVELS.len());
        assert_eq!(fallback[0].0, "none");
    }

    /// A provider's model cycles exactly the catalog's levels, with no
    /// added `none`; an empty list means no variants, not the portable set.
    #[test]
    fn opencode_variants_follow_the_catalogs_levels() {
        let claude = Thinking::Listed(
            ["low", "medium", "high", "xhigh", "max"]
                .map(String::from)
                .to_vec(),
        );
        let variants = opencode_variants(Some(&claude));
        assert_eq!(
            variants.iter().map(|(name, _)| *name).collect::<Vec<_>>(),
            ["low", "medium", "high", "xhigh", "max"]
        );
        assert_eq!(
            variants[4].1,
            serde_json::json!({ "reasoningEffort": "max" })
        );
        assert!(opencode_variants(Some(&Thinking::Listed(Vec::new()))).is_empty());
        assert!(claude.template().is_none());
    }

    #[test]
    fn added_config_is_appended_and_keeps_the_order_of_the_rest() {
        let base = r#"{"variants":{"none":1,"low":2,"high":3},"model":"a"}"#;
        let added = serde_json::json!({ "instructions": ["y.md"], "plugins": ["/p"] });
        let merged = with_added_config(base, Some(&added));
        assert_eq!(
            merged,
            r#"{"variants":{"none":1,"low":2,"high":3},"model":"a","instructions":["y.md"],"plugins":["/p"]}"#
        );
        assert_eq!(with_added_config(base, None), base);
    }

    #[test]
    fn the_major_version_is_read_from_the_version_output() {
        assert_eq!(major_version("opencode v2.3.0\n"), Some(2));
        assert_eq!(major_version("1.18.29\n"), Some(1));
        assert_eq!(major_version("v10.0.1"), Some(10));
        assert_eq!(major_version(""), None);
        assert_eq!(major_version("opencode dev"), None);
    }
}