Skip to main content

pixie_pi/ai/
mod.rs

1//! LLM abstraction layer (`packages/ai`). Types + the Anthropic Messages API
2//! streaming provider.
3
4pub mod anthropic;
5pub mod stream;
6pub mod types;
7
8pub use types::{
9    Api, Message, Model, ThinkingLevel, Usage, UserMessage,
10};
11
12use std::time::{SystemTime, UNIX_EPOCH};
13
14/// Current wall-clock time in milliseconds since the Unix epoch.
15pub fn now_ms() -> i64 {
16    SystemTime::now()
17        .duration_since(UNIX_EPOCH)
18        .map(|d| d.as_millis() as i64)
19        .unwrap_or(0)
20}
21
22/// The default base URL for the Anthropic API.
23pub const DEFAULT_ANTHROPIC_BASE_URL: &str = "https://api.anthropic.com";
24
25/// A curated model registry. A handful of Anthropic models with sensible
26/// defaults; resolved/overridden at runtime via env vars and CLI flags.
27pub fn builtin_models() -> Vec<Model> {
28    let base = std::env::var("ANTHROPIC_BASE_URL").unwrap_or_else(|_| DEFAULT_ANTHROPIC_BASE_URL.to_string());
29    vec![
30        Model {
31            id: "claude-sonnet-4-6".into(),
32            provider: "anthropic".into(),
33            api: Api::AnthropicMessages,
34            max_tokens: 64_000,
35            context_window: 1_000_000,
36            base_url: base.clone(),
37            reasoning: true,
38            force_adaptive_thinking: true,
39            supports_temperature: false,
40            input_cost_per_mtok: 3.0,
41            output_cost_per_mtok: 15.0,
42            cache_read_cost_per_mtok: 0.30,
43            cache_write_cost_per_mtok: 3.75,
44        },
45        Model {
46            id: "claude-opus-4-8".into(),
47            provider: "anthropic".into(),
48            api: Api::AnthropicMessages,
49            max_tokens: 64_000,
50            context_window: 1_000_000,
51            base_url: base.clone(),
52            reasoning: true,
53            force_adaptive_thinking: true,
54            supports_temperature: false,
55            input_cost_per_mtok: 15.0,
56            output_cost_per_mtok: 75.0,
57            cache_read_cost_per_mtok: 1.50,
58            cache_write_cost_per_mtok: 18.75,
59        },
60        Model {
61            id: "claude-haiku-4-5".into(),
62            provider: "anthropic".into(),
63            api: Api::AnthropicMessages,
64            max_tokens: 64_000,
65            context_window: 1_000_000,
66            base_url: base.clone(),
67            reasoning: true,
68            force_adaptive_thinking: true,
69            supports_temperature: false,
70            input_cost_per_mtok: 1.0,
71            output_cost_per_mtok: 5.0,
72            cache_read_cost_per_mtok: 0.10,
73            cache_write_cost_per_mtok: 1.25,
74        },
75    ]
76}
77
78/// Resolve a model by `provider/id` pattern (supports just `id`). An optional
79/// `:thinking` suffix is tolerated and ignored here (thinking is set elsewhere).
80pub fn resolve_model(registry: &[Model], pattern: &str) -> Option<Model> {
81    let (provider, id) = match pattern.split_once('/') {
82        Some((p, id)) => (Some(p), id),
83        None => (None, pattern),
84    };
85    let needle = id.split(':').next().unwrap_or(id).to_ascii_lowercase();
86    let provider = provider.map(str::to_ascii_lowercase);
87    registry
88        .iter()
89        .filter(|m| provider.as_deref().is_none_or(|p| m.provider == p))
90        .find(|m| {
91            let id = m.id.to_ascii_lowercase();
92            id == needle || id.ends_with(&format!("-{needle}")) || id.contains(&needle)
93        })
94        .cloned()
95}