albatross-cli 2.2.0

Open source, terminal-first AI coding agent with fully transparent multi-model routing. Local (Ollama, LM Studio, MLX, llama.cpp) or cloud, your keys, one TUI.
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
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum BackendName {
    Ollama,
    LmStudio,
    Mlx,
    LlamaCpp,
    Openrouter,
    OpenAi,
    Anthropic,
    OpenAiCodex,
    Grok,
}

impl BackendName {
    pub fn as_str(&self) -> &'static str {
        match self {
            BackendName::Ollama => "ollama",
            BackendName::LmStudio => "lm-studio",
            BackendName::Mlx => "mlx",
            BackendName::LlamaCpp => "llamacpp",
            BackendName::Openrouter => "openrouter",
            BackendName::OpenAi => "openai",
            BackendName::Anthropic => "anthropic",
            BackendName::OpenAiCodex => "openai-codex",
            BackendName::Grok => "grok",
        }
    }
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "ollama" => Some(Self::Ollama),
            "lm-studio" => Some(Self::LmStudio),
            "mlx" => Some(Self::Mlx),
            "llamacpp" | "llama-cpp" | "llama.cpp" => Some(Self::LlamaCpp),
            "openrouter" => Some(Self::Openrouter),
            "openai" | "open-ai" => Some(Self::OpenAi),
            "anthropic" | "claude" => Some(Self::Anthropic),
            "openai-codex" | "open-ai-codex" | "codex" | "chatgpt" => Some(Self::OpenAiCodex),
            "grok" | "xai" | "xai-oauth" | "x-ai" | "supergrok" | "grok-oauth" => Some(Self::Grok),
            _ => None,
        }
    }
    pub fn all() -> &'static [BackendName] {
        &[
            Self::Ollama,
            Self::LmStudio,
            Self::Mlx,
            Self::LlamaCpp,
            Self::Openrouter,
            Self::OpenAi,
            Self::Anthropic,
            Self::OpenAiCodex,
            Self::Grok,
        ]
    }
    /// True for backends that talk to a process on the user's machine, false
    /// for hosted APIs. Used to gate cloud-aware behavior (handoff refusal,
    /// recommend filtering, capability scoring) so adding a new hosted
    /// backend doesn't require touching every cloud check.
    pub fn is_local(&self) -> bool {
        match self {
            Self::Ollama | Self::LmStudio | Self::Mlx | Self::LlamaCpp => true,
            Self::Openrouter | Self::OpenAi | Self::Anthropic | Self::OpenAiCodex | Self::Grok => {
                false
            }
        }
    }

    pub fn is_oauth_login(&self) -> bool {
        matches!(self, Self::OpenAiCodex | Self::Grok)
    }
}

#[derive(Debug, Clone)]
pub struct BackendDescriptor {
    pub name: BackendName,
    pub base_url: String,
    pub api_key: String,
    #[allow(dead_code)]
    pub is_local: bool,
    pub openrouter: OpenRouterConfig,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct OpenRouterConfig {
    #[serde(default)]
    pub fusion: OpenRouterFusionConfig,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct OpenRouterFusionConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default)]
    pub analysis_models: Vec<String>,
    #[serde(default)]
    pub judge_model: Option<String>,
    #[serde(default)]
    pub max_tool_calls: Option<u8>,
}

pub fn backend(name: BackendName) -> BackendDescriptor {
    match name {
        BackendName::Ollama => BackendDescriptor {
            name,
            base_url: std::env::var("OLLAMA_BASE_URL")
                .unwrap_or_else(|_| "http://localhost:11434/v1".into()),
            api_key: "ollama".into(),
            is_local: true,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::LmStudio => BackendDescriptor {
            name,
            base_url: std::env::var("LM_STUDIO_BASE_URL")
                .unwrap_or_else(|_| "http://localhost:1234/v1".into()),
            api_key: "lm-studio".into(),
            is_local: true,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::Mlx => BackendDescriptor {
            name,
            base_url: std::env::var("MLX_BASE_URL")
                .unwrap_or_else(|_| "http://localhost:8080/v1".into()),
            api_key: "mlx".into(),
            is_local: true,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::LlamaCpp => BackendDescriptor {
            name,
            base_url: std::env::var("LLAMACPP_BASE_URL")
                .unwrap_or_else(|_| "http://localhost:8080/v1".into()),
            api_key: std::env::var("LLAMACPP_API_KEY")
                .unwrap_or_else(|_| "sk-no-key-required".into()),
            is_local: true,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::Openrouter => BackendDescriptor {
            name,
            base_url: "https://openrouter.ai/api/v1".into(),
            api_key: std::env::var("OPENROUTER_API_KEY").unwrap_or_default(),
            is_local: false,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::OpenAi => BackendDescriptor {
            name,
            base_url: std::env::var("OPENAI_BASE_URL")
                .unwrap_or_else(|_| "https://api.openai.com/v1".into()),
            api_key: std::env::var("OPENAI_API_KEY").unwrap_or_default(),
            is_local: false,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::Anthropic => BackendDescriptor {
            name,
            base_url: std::env::var("ANTHROPIC_BASE_URL")
                .unwrap_or_else(|_| "https://api.anthropic.com/v1".into()),
            api_key: std::env::var("ANTHROPIC_API_KEY").unwrap_or_default(),
            is_local: false,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::OpenAiCodex => BackendDescriptor {
            name,
            base_url: std::env::var("OPENAI_CODEX_BASE_URL")
                .unwrap_or_else(|_| "https://chatgpt.com/backend-api".into()),
            // OAuth access tokens are loaded/refreshed lazily from auth.json by
            // the Codex Responses adapter.  Keep this empty so callers don't
            // accidentally treat ChatGPT subscription auth as an API key.
            api_key: String::new(),
            is_local: false,
            openrouter: OpenRouterConfig::default(),
        },
        BackendName::Grok => BackendDescriptor {
            name,
            // OAuth session tokens belong only on xAI's first-party CLI
            // proxy. Do not permit an environment override that could receive
            // and exfiltrate the bearer token.
            base_url: crate::xai_oauth::INFERENCE_BASE_URL.into(),
            api_key: String::new(),
            is_local: false,
            openrouter: OpenRouterConfig::default(),
        },
    }
}

/// The default model for a backend when the user hasn't set an override
/// (`modelOverride` / `AGENT_MODEL` / `/model`). One sensible default per
/// backend — local backends default to a 7B coder that runs on modest
/// hardware; bump it with `/model` if you have the headroom.
pub fn default_model(b: &BackendDescriptor, override_: Option<&str>) -> String {
    if let Some(m) = override_ {
        if matches!(b.name, BackendName::OpenAiCodex) {
            return crate::codex_responses::canonical_codex_model(m)
                .unwrap_or("gpt-5.5")
                .to_string();
        }
        if matches!(b.name, BackendName::Grok) {
            return crate::xai_oauth::canonical_grok_model(m)
                .unwrap_or("grok-4.5")
                .to_string();
        }
        return m.to_string();
    }
    match b.name {
        // qwen2.5:7b (base) is used rather than qwen2.5-coder:7b because the
        // coder variant does not use Ollama's structured tool_calls API —
        // it emits tool invocations as raw JSON text in the content field,
        // which Albatross cannot execute. The base model uses the same
        // <tool_call> template that Ollama translates to structured tool_calls.
        BackendName::Ollama => "qwen2.5:7b",
        BackendName::LmStudio => "qwen2.5-coder-7b-instruct",
        BackendName::Mlx => "mlx-community/Qwen2.5-Coder-7B-Instruct-4bit",
        BackendName::LlamaCpp => "gpt-3.5-turbo",
        BackendName::Openrouter => "qwen/qwen-2.5-coder-32b-instruct",
        BackendName::OpenAi => "gpt-4o-mini",
        BackendName::Anthropic => "claude-sonnet-5",
        BackendName::OpenAiCodex => "gpt-5.5",
        BackendName::Grok => "grok-4.5",
    }
    .to_string()
}

pub fn validate(b: &BackendDescriptor) -> Result<()> {
    if matches!(b.name, BackendName::Openrouter) && b.api_key.is_empty() {
        return Err(anyhow!(
            "OPENROUTER_API_KEY is required when BACKEND=openrouter."
        ));
    }
    if matches!(b.name, BackendName::OpenAi) && b.api_key.is_empty() {
        return Err(anyhow!("OPENAI_API_KEY is required when BACKEND=openai."));
    }
    if matches!(b.name, BackendName::Anthropic) && b.api_key.is_empty() {
        return Err(anyhow!(
            "ANTHROPIC_API_KEY is required when BACKEND=anthropic."
        ));
    }
    if matches!(b.name, BackendName::OpenAiCodex)
        && crate::auth::AuthStore::load()
            .get_oauth("openai-codex")
            .is_none()
    {
        return Err(anyhow!(
            "ChatGPT/Codex login is required when BACKEND=openai-codex. Run `/login openai-codex`."
        ));
    }
    if matches!(b.name, BackendName::Grok)
        && crate::auth::AuthStore::load()
            .get_oauth(crate::xai_oauth::PROVIDER)
            .is_none()
    {
        return Err(anyhow!(
            "Grok login is required when BACKEND=grok. Run `/login grok`."
        ));
    }
    Ok(())
}

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

    fn descriptor(name: BackendName) -> BackendDescriptor {
        BackendDescriptor {
            name,
            base_url: String::new(),
            api_key: String::new(),
            is_local: true,
            openrouter: OpenRouterConfig::default(),
        }
    }

    #[test]
    fn parses_llamacpp_aliases() {
        assert_eq!(BackendName::parse("llamacpp"), Some(BackendName::LlamaCpp));
        assert_eq!(BackendName::parse("llama-cpp"), Some(BackendName::LlamaCpp));
        assert_eq!(BackendName::parse("llama.cpp"), Some(BackendName::LlamaCpp));
    }

    #[test]
    fn lists_llamacpp_as_switchable_backend() {
        assert!(BackendName::all().contains(&BackendName::LlamaCpp));
    }

    #[test]
    fn defaults_llamacpp_to_openai_example_model() {
        let model = default_model(&descriptor(BackendName::LlamaCpp), None);
        assert_eq!(model, "gpt-3.5-turbo");
    }

    #[test]
    fn parses_openai_aliases() {
        assert_eq!(BackendName::parse("openai"), Some(BackendName::OpenAi));
        assert_eq!(BackendName::parse("open-ai"), Some(BackendName::OpenAi));
        assert_eq!(BackendName::OpenAi.as_str(), "openai");
    }

    #[test]
    fn lists_openai_as_switchable_backend() {
        assert!(BackendName::all().contains(&BackendName::OpenAi));
    }

    #[test]
    fn is_local_marks_hosted_backends_as_cloud() {
        assert!(BackendName::Ollama.is_local());
        assert!(BackendName::LmStudio.is_local());
        assert!(BackendName::Mlx.is_local());
        assert!(BackendName::LlamaCpp.is_local());
        assert!(!BackendName::Openrouter.is_local());
        assert!(!BackendName::OpenAi.is_local());
        assert!(!BackendName::Anthropic.is_local());
        assert!(!BackendName::OpenAiCodex.is_local());
        assert!(!BackendName::Grok.is_local());
        assert!(BackendName::Grok.is_oauth_login());
    }

    #[test]
    fn parses_grok_aliases() {
        assert_eq!(BackendName::parse("grok"), Some(BackendName::Grok));
        assert_eq!(BackendName::parse("xai"), Some(BackendName::Grok));
        assert_eq!(BackendName::parse("xai-oauth"), Some(BackendName::Grok));
        assert_eq!(BackendName::parse("supergrok"), Some(BackendName::Grok));
        assert_eq!(BackendName::Grok.as_str(), "grok");
    }

    #[test]
    fn lists_grok_as_switchable_backend() {
        assert!(BackendName::all().contains(&BackendName::Grok));
    }

    #[test]
    fn defaults_grok_to_grok_4_5() {
        let backend = backend(BackendName::Grok);
        let model = default_model(&backend, None);
        assert_eq!(model, "grok-4.5");
        assert_eq!(backend.base_url, crate::xai_oauth::INFERENCE_BASE_URL);
    }

    #[test]
    fn defaults_openai_to_gpt_4o_mini() {
        let model = default_model(&descriptor(BackendName::OpenAi), None);
        assert_eq!(model, "gpt-4o-mini");
    }

    #[test]
    fn parses_and_defaults_anthropic() {
        assert_eq!(
            BackendName::parse("anthropic"),
            Some(BackendName::Anthropic)
        );
        assert_eq!(BackendName::parse("claude"), Some(BackendName::Anthropic));
        assert!(BackendName::all().contains(&BackendName::Anthropic));
        assert_eq!(BackendName::Anthropic.as_str(), "anthropic");
        assert_eq!(
            default_model(&descriptor(BackendName::Anthropic), None),
            "claude-sonnet-5"
        );
    }

    #[test]
    fn defaults_ollama_to_base_qwen_not_coder_variant() {
        // qwen2.5:7b (base) must be the default, not qwen2.5-coder:7b.
        // The coder variant emits tool calls as raw JSON text rather than
        // via Ollama's structured tool_calls API, so Albatross cannot
        // execute them in interactive or one-shot mode.
        let model = default_model(&descriptor(BackendName::Ollama), None);
        assert_eq!(model, "qwen2.5:7b");
        assert_ne!(
            model, "qwen2.5-coder:7b",
            "coder variant does not support structured tool_calls"
        );
    }

    #[test]
    fn parses_openai_codex_aliases() {
        assert_eq!(
            BackendName::parse("openai-codex"),
            Some(BackendName::OpenAiCodex)
        );
        assert_eq!(BackendName::parse("codex"), Some(BackendName::OpenAiCodex));
        assert_eq!(
            BackendName::parse("chatgpt"),
            Some(BackendName::OpenAiCodex)
        );
        assert_eq!(BackendName::OpenAiCodex.as_str(), "openai-codex");
    }

    #[test]
    fn lists_openai_codex_as_switchable_backend() {
        assert!(BackendName::all().contains(&BackendName::OpenAiCodex));
    }

    #[test]
    fn defaults_openai_codex_to_codex_model() {
        let model = default_model(&descriptor(BackendName::OpenAiCodex), None);
        assert_eq!(model, "gpt-5.5");
    }

    #[test]
    fn ignores_non_codex_override_for_openai_codex() {
        let model = default_model(&descriptor(BackendName::OpenAiCodex), Some("gpt-5-codex"));
        assert_eq!(model, "gpt-5.5");
    }

    #[test]
    fn normalizes_openai_codex_model_aliases() {
        let model = default_model(&descriptor(BackendName::OpenAiCodex), Some("5.5"));
        assert_eq!(model, "gpt-5.5");
    }

    #[test]
    fn validate_requires_openai_api_key() {
        let mut desc = descriptor(BackendName::OpenAi);
        desc.is_local = false;
        assert!(validate(&desc).is_err());
        desc.api_key = "sk-test".into();
        assert!(validate(&desc).is_ok());
    }

    #[test]
    fn validate_requires_anthropic_api_key() {
        let mut desc = descriptor(BackendName::Anthropic);
        desc.is_local = false;
        assert!(validate(&desc).is_err());
        desc.api_key = "sk-ant-test".into();
        assert!(validate(&desc).is_ok());
    }
}