oy-cli 0.10.1

Local AI coding CLI for inspecting, editing, running commands, and auditing repositories
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
//! OpenCode model metadata queries: token limits, reasoning capability,
//! and supported effort levels.
//!
//! This is the only source of OpenCode verbose model metadata; do not
//! add local provider/model registries. See [`AdapterModels`] for the
//! parsed listing shape and the free functions for cached lookups.

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

pub(crate) const MODELS_SOURCE: &str = "opencode models --verbose";

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "snake_case")]
pub(crate) enum AdapterModels {
    Available {
        adapter: String,
        source: String,
        count: usize,
        models: Vec<String>,
    },
    Failed {
        adapter: String,
        source: String,
        error: String,
    },
}

impl AdapterModels {
    pub fn models(&self) -> &[String] {
        match self {
            Self::Available { models, .. } => models,
            Self::Failed { .. } => &[],
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct OpenCodeModelListing {
    models: Vec<OpenCodeModel>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct OpenCodeModel {
    #[serde(default)]
    spec: String,
    #[serde(default)]
    id: String,
    #[serde(default, rename = "providerID")]
    provider_id: String,
    #[serde(default)]
    api: OpenCodeModelApi,
    #[serde(default)]
    capabilities: OpenCodeCapabilities,
    #[serde(default)]
    variants: std::collections::HashMap<String, OpenCodeVariant>,
    #[serde(default)]
    limit: OpenCodeModelLimit,
}

#[derive(Debug, Default, Clone, Deserialize)]
struct OpenCodeModelApi {
    id: Option<String>,
    url: Option<String>,
    npm: Option<String>,
}

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct OpenCodeCapabilities {
    #[serde(default)]
    pub reasoning: bool,
}

/// Token limits reported by OpenCode for a model.
#[derive(Debug, Default, Clone, Copy, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct OpenCodeModelLimit {
    /// Total context window in tokens.
    #[serde(default)]
    pub context: usize,
    /// Max input tokens (may be less than context).
    #[serde(default)]
    pub input: Option<usize>,
    /// Max output tokens.
    #[serde(default)]
    pub output: usize,
}

// Value type is unused; we only inspect variant keys.
type OpenCodeVariant = serde_json::Value;

use std::sync::{LazyLock, RwLock};

static MODELS_CACHE: LazyLock<RwLock<Option<OpenCodeModelListing>>> =
    LazyLock::new(|| RwLock::new(None));

pub(crate) fn populate_cache(listing: OpenCodeModelListing) {
    let mut cache = MODELS_CACHE.write().unwrap();
    *cache = Some(listing);
}

impl OpenCodeModelListing {
    pub(crate) fn load() -> Result<Self> {
        if let Some(cached) = MODELS_CACHE.read().unwrap().as_ref() {
            return Ok(cached.clone());
        }

        let output = std::process::Command::new("opencode")
            .arg("models")
            .arg("--verbose")
            .output()
            .context("failed to run `opencode models --verbose`")?;
        if !output.status.success() {
            bail!(
                "`opencode models --verbose` failed: {}",
                String::from_utf8_lossy(&output.stderr).trim()
            );
        }
        let listing = parse_verbose(&String::from_utf8_lossy(&output.stdout))?;
        populate_cache(listing.clone());
        Ok(listing)
    }

    pub(crate) fn find(&self, provider: &str, model: &str) -> Option<&OpenCodeModel> {
        self.models
            .iter()
            .find(|item| item.provider_id == provider && item.id == model)
            .or_else(|| {
                let spec = format!("{provider}/{model}");
                self.models.iter().find(|item| item.spec == spec)
            })
    }

    pub(crate) fn into_adapter_models(self) -> Vec<AdapterModels> {
        let mut groups = std::collections::BTreeMap::<String, Vec<String>>::new();
        for model in self.models {
            if !model.is_supported_by_native_openai() {
                continue;
            }
            groups
                .entry(model.provider_id)
                .or_default()
                .push(model.spec);
        }
        groups
            .into_iter()
            .map(|(adapter, mut models)| {
                models.sort();
                models.dedup();
                AdapterModels::Available {
                    adapter,
                    source: MODELS_SOURCE.to_string(),
                    count: models.len(),
                    models,
                }
            })
            .collect()
    }
}

impl OpenCodeModel {
    pub(crate) fn api_id(&self) -> &str {
        self.api.id.as_deref().unwrap_or(&self.id)
    }

    pub(crate) fn api_url(&self) -> Option<&str> {
        self.api
            .url
            .as_deref()
            .filter(|value| !value.trim().is_empty())
    }

    /// Whether OpenCode reports this model as reasoning-capable.
    pub(crate) fn supports_reasoning(&self) -> bool {
        self.capabilities.reasoning
    }

    /// Supported reasoning effort levels, derived from the model's variants.
    /// Returns an empty slice when the model has no reasoning variants
    /// (capabilities.reasoning may still be true).
    pub(crate) fn reasoning_efforts(&self) -> Vec<&str> {
        let mut efforts: Vec<&str> = self
            .variants
            .keys()
            .map(|s| s.as_str())
            .filter(|k| {
                // Only include keys that map to known reasoning effort values
                matches!(*k, "none" | "minimal" | "low" | "medium" | "high")
            })
            .collect();
        efforts.sort();
        efforts
    }

    /// Default reasoning effort from OpenCode metadata.
    /// Returns `Some("high")` when the model is reasoning-capable and
    /// "high" is a supported variant, otherwise the first available variant,
    /// otherwise `None`.
    pub(crate) fn default_reasoning_effort(&self) -> Option<&str> {
        if !self.supports_reasoning() {
            return None;
        }
        let efforts = self.reasoning_efforts();
        if efforts.is_empty() {
            // Model says it supports reasoning but has no explicit variants;
            // "high" is the safe default.
            Some("high")
        } else if efforts.contains(&"high") {
            Some("high")
        } else {
            efforts.first().copied()
        }
    }

    pub(crate) fn is_openai_compatible_api(&self) -> bool {
        self.api.npm.as_deref().is_some_and(|api| {
            matches!(
                api,
                "@ai-sdk/openai"
                    | "@ai-sdk/openai-compatible"
                    | "@ai-sdk/github-copilot"
                    | "@ai-sdk/google"
                    | "@ai-sdk/anthropic"
            )
        })
    }

    pub(crate) fn is_bedrock_api(&self) -> bool {
        self.api.npm.as_deref() == Some("@ai-sdk/amazon-bedrock")
            || matches!(self.provider_id.as_str(), "bedrock" | "amazon-bedrock")
    }

    fn is_supported_by_native_openai(&self) -> bool {
        if self.provider_id == "vertexai" {
            return false;
        }
        self.is_openai_compatible_api() || self.is_bedrock_api()
    }
}

pub(crate) fn parse_verbose(text: &str) -> Result<OpenCodeModelListing> {
    let mut lines = text.lines().peekable();
    let mut models = Vec::new();
    while let Some(line) = lines.next() {
        let spec = line.trim();
        if spec.is_empty() || spec.starts_with('{') || !spec.contains('/') {
            continue;
        }
        let mut json_lines = Vec::new();
        let mut depth = 0isize;
        while let Some(line) = lines.peek().copied() {
            if json_lines.is_empty() && !line.trim_start().starts_with('{') {
                break;
            }
            let line = lines.next().unwrap_or_default();
            depth += line.matches('{').count() as isize;
            depth -= line.matches('}').count() as isize;
            json_lines.push(line);
            if depth == 0 && !json_lines.is_empty() {
                break;
            }
        }
        if json_lines.is_empty() {
            continue;
        }
        let mut model: OpenCodeModel = serde_json::from_str(&json_lines.join("\n"))
            .with_context(|| format!("failed parsing OpenCode model metadata for {spec}"))?;
        model.spec = spec.to_string();
        if model.id.trim().is_empty() {
            model.id = spec
                .rsplit_once('/')
                .map(|(_, id)| id)
                .unwrap_or(spec)
                .to_string();
        }
        if model.provider_id.trim().is_empty() {
            model.provider_id = spec
                .split_once('/')
                .map(|(provider, _)| provider)
                .unwrap_or("")
                .to_string();
        }
        models.push(model);
    }
    Ok(OpenCodeModelListing { models })
}

pub(crate) fn inspect() -> Vec<AdapterModels> {
    match OpenCodeModelListing::load() {
        Ok(listing) => listing.into_adapter_models(),
        Err(err) => vec![AdapterModels::Failed {
            adapter: "opencode".to_string(),
            source: MODELS_SOURCE.to_string(),
            error: err.to_string(),
        }],
    }
}

pub(crate) fn find(provider: &str, model: &str) -> Option<OpenCodeModel> {
    OpenCodeModelListing::load()
        .ok()?
        .find(provider, model)
        .cloned()
}

/// Look up token limits for a model from OpenCode.
/// Returns `None` when the listing can't be loaded or the model isn't found,
/// or when the model reports no context limit.
pub(crate) fn lookup_limit(provider: &str, model: &str) -> Option<OpenCodeModelLimit> {
    let limit = OpenCodeModelListing::load()
        .ok()?
        .find(provider, model)?
        .limit;
    if limit.context == 0 {
        None
    } else {
        Some(limit)
    }
}

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

    #[test]
    fn parses_opencode_verbose_models() {
        let text = r#"github-copilot/gpt-5.5
{
  "id": "gpt-5.5",
  "providerID": "github-copilot",
  "api": { "id": "gpt-5.5", "url": "https://api.githubcopilot.com", "npm": "@ai-sdk/github-copilot" }
}
opencode/claude-test
{
  "id": "claude-test",
  "providerID": "opencode",
  "api": { "id": "claude-test", "url": "https://opencode.ai/zen/v1", "npm": "@ai-sdk/anthropic" }
}
"#;
        let listing = parse_verbose(text).unwrap();
        let model = listing.find("github-copilot", "gpt-5.5").unwrap();
        assert_eq!(model.api_id(), "gpt-5.5");
        assert_eq!(model.api_url(), Some("https://api.githubcopilot.com"));
        let groups = listing.into_adapter_models();
        // Both models are OpenAI-compatible (github-copilot + opencode proxying anthropic)
        assert_eq!(groups.len(), 2);
        // groups are sorted by adapter name
        assert_eq!(groups[0].models(), &["github-copilot/gpt-5.5".to_string()]);
        assert_eq!(groups[1].models(), &["opencode/claude-test".to_string()]);
    }

    #[test]
    fn falls_back_to_spec_for_missing_ids() {
        let text = r#"anthropic/anthropic.test
{
  "api": { "id": "anthropic.test" }
}
"#;
        let listing = parse_verbose(text).unwrap();
        assert!(listing.find("anthropic", "anthropic.test").is_some());
    }

    #[test]
    fn includes_bedrock_but_filters_vertexai_models() {
        let text = r#"bedrock/anthropic.claude-sonnet-4
{
  "id": "anthropic.claude-sonnet-4",
  "providerID": "bedrock",
  "api": { "id": "anthropic.claude-sonnet-4", "npm": "@ai-sdk/amazon-bedrock" }
}
amazon-bedrock/anthropic.claude-opus-4
{
  "id": "anthropic.claude-opus-4",
  "providerID": "amazon-bedrock",
  "api": { "id": "anthropic.claude-opus-4", "npm": "@ai-sdk/amazon-bedrock" }
}
vertexai/gemini-3.1-pro
{
  "id": "gemini-3.1-pro",
  "providerID": "vertexai",
  "api": { "id": "gemini-3.1-pro", "npm": "@ai-sdk/vertexai" }
}
github-copilot/gpt-5.5
{
  "id": "gpt-5.5",
  "providerID": "github-copilot",
  "api": { "id": "gpt-5.5", "npm": "@ai-sdk/github-copilot" }
}
"#;
        let listing = parse_verbose(text).unwrap();
        let groups = listing.into_adapter_models();
        assert_eq!(groups.len(), 3);
        assert_eq!(
            groups[0].models(),
            &["amazon-bedrock/anthropic.claude-opus-4".to_string()]
        );
        assert_eq!(
            groups[1].models(),
            &["bedrock/anthropic.claude-sonnet-4".to_string()]
        );
        assert_eq!(groups[2].models(), &["github-copilot/gpt-5.5".to_string()]);
    }
}