cersei-provider 0.1.9

Provider trait and built-in LLM providers for the Cersei SDK
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
//! Static registry of known LLM providers.
//!
//! Each entry contains the provider's API base URL, env var names for auth,
//! API format (Anthropic or OpenAI-compatible), and known models with
//! context windows and capabilities.

use crate::ProviderCapabilities;

/// API format used by a provider.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ApiFormat {
    /// Anthropic's native API format (different SSE events, system prompt handling).
    Anthropic,
    /// OpenAI-compatible `/v1/chat/completions` format (used by most providers).
    OpenAiCompatible,
    /// Google Gemini native `generateContent` API format.
    Google,
}

/// A known LLM provider.
#[derive(Debug, Clone)]
pub struct ProviderEntry {
    pub id: &'static str,
    pub name: &'static str,
    pub api_base: &'static str,
    pub env_keys: &'static [&'static str],
    pub api_format: ApiFormat,
    pub default_model: &'static str,
    pub models: &'static [ModelEntry],
}

/// A known model within a provider.
#[derive(Debug, Clone)]
pub struct ModelEntry {
    pub id: &'static str,
    pub context_window: u64,
    pub capabilities: ProviderCapabilities,
}

impl ProviderEntry {
    /// Try to read an API key from the environment using this provider's env key list.
    pub fn api_key_from_env(&self) -> Option<String> {
        for key in self.env_keys {
            if let Ok(val) = std::env::var(key) {
                if !val.is_empty() {
                    return Some(val);
                }
            }
        }
        None
    }

    /// Whether this provider requires an API key (Ollama does not).
    pub fn requires_key(&self) -> bool {
        !self.env_keys.is_empty()
    }

    /// Whether a local provider (one with no `env_keys`, e.g. Ollama) is
    /// actually reachable right now. Does a 200ms TCP probe against the
    /// host:port parsed out of `api_base`. Returns `true` when the probe
    /// succeeds, `false` otherwise. Providers that *do* require a key
    /// return `true` unconditionally (their availability is gated on the
    /// env var, not connectivity).
    pub fn is_reachable(&self) -> bool {
        if self.requires_key() {
            return true;
        }
        let host_port = extract_host_port(self.api_base);
        let Some(host_port) = host_port else {
            return false;
        };
        use std::net::ToSocketAddrs;
        let addrs: Vec<std::net::SocketAddr> = match host_port.to_socket_addrs() {
            Ok(it) => it.collect(),
            Err(_) => return false,
        };
        addrs.into_iter().any(|addr| {
            std::net::TcpStream::connect_timeout(&addr, std::time::Duration::from_millis(200))
                .is_ok()
        })
    }

    /// Get the context window for a model, falling back to a default.
    pub fn context_window(&self, model: &str) -> u64 {
        self.models
            .iter()
            .find(|m| m.id == model)
            .map(|m| m.context_window)
            .unwrap_or(128_000)
    }
}

// ─── Capabilities shorthand ────────────────────────────────────────────────

const FULL: ProviderCapabilities = ProviderCapabilities {
    streaming: true,
    tool_use: true,
    vision: true,
    thinking: false,
    system_prompt: true,
    caching: false,
};

const FULL_THINKING: ProviderCapabilities = ProviderCapabilities {
    streaming: true,
    tool_use: true,
    vision: true,
    thinking: true,
    system_prompt: true,
    caching: true,
};

const BASIC: ProviderCapabilities = ProviderCapabilities {
    streaming: true,
    tool_use: true,
    vision: false,
    thinking: false,
    system_prompt: true,
    caching: false,
};

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

pub static REGISTRY: &[ProviderEntry] = &[
    ProviderEntry {
        id: "anthropic",
        name: "Anthropic",
        api_base: "https://api.anthropic.com",
        env_keys: &["ANTHROPIC_API_KEY", "ANTHROPIC_KEY"],
        api_format: ApiFormat::Anthropic,
        default_model: "claude-sonnet-4-6",
        models: &[
            ModelEntry {
                id: "claude-opus-4-6",
                context_window: 200_000,
                capabilities: FULL_THINKING,
            },
            ModelEntry {
                id: "claude-sonnet-4-6",
                context_window: 200_000,
                capabilities: FULL_THINKING,
            },
            ModelEntry {
                id: "claude-haiku-4-5",
                context_window: 200_000,
                capabilities: FULL,
            },
        ],
    },
    ProviderEntry {
        id: "openai",
        name: "OpenAI",
        api_base: "https://api.openai.com/v1",
        env_keys: &["OPENAI_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "gpt-5.4-2026-03-05",
        models: &[
            ModelEntry {
                id: "gpt-5.4-2026-03-05",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gpt-5.3-chat-latest",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gpt-5.3-chat",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gpt-5.3-codex",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gpt-5-chat",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gpt-4o",
                context_window: 128_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gpt-4-turbo",
                context_window: 128_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "o1",
                context_window: 200_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "o3",
                context_window: 200_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "o3-pro",
                context_window: 200_000,
                capabilities: FULL,
            },
        ],
    },
    ProviderEntry {
        id: "google",
        name: "Google",
        api_base: "https://generativelanguage.googleapis.com/v1beta",
        env_keys: &["GOOGLE_API_KEY", "GEMINI_API_KEY"],
        api_format: ApiFormat::Google,
        default_model: "gemini-3.1-pro-preview",
        models: &[
            ModelEntry {
                id: "gemini-3.1-pro-preview",
                context_window: 2_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gemini-3.0-flash",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gemini-2.0-flash",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gemini-2.0-pro",
                context_window: 1_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gemini-1.5-pro",
                context_window: 2_000_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "gemini-1.5-flash",
                context_window: 1_000_000,
                capabilities: FULL,
            },
        ],
    },
    ProviderEntry {
        id: "mistral",
        name: "Mistral",
        api_base: "https://api.mistral.ai/v1",
        env_keys: &["MISTRAL_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "mistral-large-latest",
        models: &[
            ModelEntry {
                id: "mistral-large-latest",
                context_window: 128_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "codestral-latest",
                context_window: 256_000,
                capabilities: BASIC,
            },
        ],
    },
    ProviderEntry {
        id: "groq",
        name: "Groq",
        api_base: "https://api.groq.com/openai/v1",
        env_keys: &["GROQ_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "llama-3.1-70b-versatile",
        models: &[
            ModelEntry {
                id: "llama-3.1-70b-versatile",
                context_window: 128_000,
                capabilities: BASIC,
            },
            ModelEntry {
                id: "llama-3.1-8b-instant",
                context_window: 128_000,
                capabilities: BASIC,
            },
            ModelEntry {
                id: "mixtral-8x7b-32768",
                context_window: 32_768,
                capabilities: BASIC,
            },
        ],
    },
    ProviderEntry {
        id: "deepseek",
        name: "DeepSeek",
        api_base: "https://api.deepseek.com/v1",
        env_keys: &["DEEPSEEK_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "deepseek-chat",
        models: &[
            ModelEntry {
                id: "deepseek-chat",
                context_window: 64_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "deepseek-coder",
                context_window: 64_000,
                capabilities: BASIC,
            },
        ],
    },
    ProviderEntry {
        id: "xai",
        name: "xAI",
        api_base: "https://api.x.ai/v1",
        env_keys: &["XAI_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "grok-2",
        models: &[ModelEntry {
            id: "grok-2",
            context_window: 128_000,
            capabilities: FULL,
        }],
    },
    ProviderEntry {
        id: "together",
        name: "Together",
        api_base: "https://api.together.xyz/v1",
        env_keys: &["TOGETHER_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
        models: &[ModelEntry {
            id: "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
            context_window: 128_000,
            capabilities: BASIC,
        }],
    },
    ProviderEntry {
        id: "fireworks",
        name: "Fireworks",
        api_base: "https://api.fireworks.ai/inference/v1",
        env_keys: &["FIREWORKS_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "accounts/fireworks/models/llama-v3p1-70b-instruct",
        models: &[ModelEntry {
            id: "accounts/fireworks/models/llama-v3p1-70b-instruct",
            context_window: 128_000,
            capabilities: BASIC,
        }],
    },
    ProviderEntry {
        id: "perplexity",
        name: "Perplexity",
        api_base: "https://api.perplexity.ai",
        env_keys: &["PERPLEXITY_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "llama-3.1-sonar-large-128k-online",
        models: &[ModelEntry {
            id: "llama-3.1-sonar-large-128k-online",
            context_window: 128_000,
            capabilities: BASIC,
        }],
    },
    ProviderEntry {
        id: "cerebras",
        name: "Cerebras",
        api_base: "https://api.cerebras.ai/v1",
        env_keys: &["CEREBRAS_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "llama3.1-70b",
        models: &[ModelEntry {
            id: "llama3.1-70b",
            context_window: 128_000,
            capabilities: BASIC,
        }],
    },
    ProviderEntry {
        id: "ollama",
        name: "Ollama",
        api_base: "http://localhost:11434/v1",
        env_keys: &[],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "llama3.1",
        models: &[],
    },
    ProviderEntry {
        id: "openrouter",
        name: "OpenRouter",
        api_base: "https://openrouter.ai/api/v1",
        env_keys: &["OPENROUTER_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "anthropic/claude-3.5-sonnet",
        models: &[],
    },
    ProviderEntry {
        id: "cohere",
        name: "Cohere",
        api_base: "https://api.cohere.com/compatibility/v1",
        env_keys: &["COHERE_API_KEY", "CO_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "command-r-plus",
        models: &[
            ModelEntry {
                id: "command-r-plus",
                context_window: 128_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "command-r",
                context_window: 128_000,
                capabilities: FULL,
            },
            ModelEntry {
                id: "command-a",
                context_window: 256_000,
                capabilities: FULL,
            },
        ],
    },
    ProviderEntry {
        id: "sambanova",
        name: "SambaNova",
        api_base: "https://api.sambanova.ai/v1",
        env_keys: &["SAMBANOVA_API_KEY"],
        api_format: ApiFormat::OpenAiCompatible,
        default_model: "Meta-Llama-3.1-70B-Instruct",
        models: &[
            ModelEntry {
                id: "Meta-Llama-3.1-70B-Instruct",
                context_window: 128_000,
                capabilities: BASIC,
            },
            ModelEntry {
                id: "Meta-Llama-3.1-405B-Instruct",
                context_window: 128_000,
                capabilities: BASIC,
            },
        ],
    },
];

/// Look up a provider by ID.
pub fn lookup(provider_id: &str) -> Option<&'static ProviderEntry> {
    REGISTRY.iter().find(|e| e.id == provider_id)
}

/// All registered providers.
pub fn all() -> &'static [ProviderEntry] {
    REGISTRY
}

/// Providers that have valid auth configured in the environment **and** — for
/// local providers without an API key (e.g. Ollama) — are actually reachable
/// via a quick TCP probe.
///
/// The probe prevents `from_model_string("auto")` from silently picking Ollama
/// when the daemon is not running, which was causing the CLI to default to
/// `llama3.1` on machines without any LLM installed.
pub fn available() -> Vec<&'static ProviderEntry> {
    REGISTRY
        .iter()
        .filter(|e| {
            if e.requires_key() {
                e.api_key_from_env().is_some()
            } else {
                e.is_reachable()
            }
        })
        .collect()
}

/// Extract a `host:port` string from an http(s) URL for TCP probing.
fn extract_host_port(api_base: &str) -> Option<String> {
    let trimmed = api_base
        .trim_start_matches("https://")
        .trim_start_matches("http://");
    let authority = trimmed.split('/').next()?;
    if authority.contains(':') {
        Some(authority.to_string())
    } else {
        // default ports based on scheme
        let port = if api_base.starts_with("https://") {
            443
        } else {
            80
        };
        Some(format!("{authority}:{port}"))
    }
}