oxi-ai 0.23.0

Unified LLM API — multi-provider streaming interface for AI coding assistants
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
//! Model auto-routing for oxi.
//!
//! Provides intelligent model selection based on conversation structure
//! and behavioral signals. This is an **opt-in** feature — the user must
//! explicitly select `router/auto` or another profile in model selection.

#![allow(missing_docs)]

pub mod classifier;
pub mod fallback;
pub mod profiles;
pub mod scoring;
pub mod signals;
pub mod types;

use crate::context::Context;
use crate::error::ProviderError;
use crate::messages::Message;
use crate::providers::ProviderRegistry;
use crate::providers::StreamOptions;
use crate::types::Model;
use crate::{register_model, register_provider, Api, Provider, ProviderEvent, ThinkingLevel};

/// Global router state snapshot — updated after each routing decision.
static ROUTER_SNAPSHOT: parking_lot::RwLock<Option<RouterSnapshot>> =
    parking_lot::RwLock::new(None);

/// Snapshot of the current router state for UI display.
#[derive(Debug, Clone, Default)]
pub struct RouterSnapshot {
    pub last_tier: Option<RouterTier>,
    pub last_score: f64,
    pub last_model: Option<String>,
    pub last_provider: Option<String>,
    pub accumulated_cost: f64,
    pub turn_count: usize,
    pub profile: Option<String>,
}
use futures::Stream;
use parking_lot::RwLock;
use std::pin::Pin;
use std::sync::Arc;

pub use fallback::FallbackChain;
pub use profiles::{parse_tier_model, ProviderModel, RouterProfiles};
pub use scoring::compute_score;
pub use signals::{BehavioralSignal, ContextBudgetSignal, StructuralSignal, VisionSignal};
pub use types::{
    DecisionMethod, RoutedTierConfig, RouterConfig, RouterPhase, RouterProfile, RouterState,
    RouterTier, RoutingDecision, RoutingScore, ScoringWeights,
};

// ── Helpers ──────────────────────────────────────────────────────────────────

fn message_chars(msg: &Message) -> usize {
    match msg {
        Message::User(u) => u.content.as_str().map(|s| s.len()).unwrap_or(0),
        Message::Assistant(a) => a
            .content
            .iter()
            .map(|b| b.as_text().map(|t| t.len()).unwrap_or(0))
            .sum(),
        Message::ToolResult(t) => t
            .content
            .iter()
            .map(|b| b.as_text().map(|t| t.len()).unwrap_or(0))
            .sum(),
    }
}

fn build_target_model(pm: &ProviderModel, reasoning: bool) -> Model {
    let mut m = Model::new(
        &pm.model_id,
        &pm.model_id,
        Api::AnthropicMessages,
        &pm.provider,
        "",
    );
    m.reasoning = reasoning;
    m
}

// ── Pipeline ─────────────────────────────────────────────────────────────────

/// Routing pipeline state for a session.
#[derive(Debug)]
pub struct RouterPipeline {
    pub weights: ScoringWeights,
    decision_history: Vec<RoutingDecision>,
    accumulated_cost: f64,
    budget_limit: Option<f64>,
    context_upgrade_threshold: Option<usize>,
    last_score: f64,
}

impl RouterPipeline {
    pub fn new() -> Self {
        Self {
            weights: ScoringWeights::default(),
            decision_history: Vec::new(),
            accumulated_cost: 0.0,
            budget_limit: None,
            context_upgrade_threshold: None,
            last_score: 0.5,
        }
    }

    pub fn from_config(config: &RouterConfig) -> Self {
        Self {
            weights: config.weights.clone(),
            decision_history: Vec::new(),
            accumulated_cost: 0.0,
            budget_limit: config.max_session_budget,
            context_upgrade_threshold: config.context_upgrade_threshold,
            last_score: 0.5,
        }
    }

    /// Route a context to (score, tier, phase).
    pub fn route(&mut self, context: &Context) -> (f64, RouterTier, RouterPhase) {
        let structural = StructuralSignal::extract(&context.messages);
        let behavioral = BehavioralSignal::extract(&context.messages, &self.decision_history);
        let budget = ContextBudgetSignal::extract(
            structural.estimated_tokens,
            self.accumulated_cost,
            self.budget_limit,
            self.context_upgrade_threshold,
        );

        let raw_score = compute_score(&structural, &behavioral, &budget, None, &self.weights);
        self.last_score = raw_score;

        let score = RoutingScore(raw_score);
        let mut tier = score.to_tier(0.65, 0.35);

        if budget.should_upgrade_context() && tier != RouterTier::High {
            tier = RouterTier::High;
        }
        if budget.is_over_budget() && tier == RouterTier::High {
            tier = RouterTier::Medium;
        }

        (raw_score, tier, behavioral.phase)
    }

    pub fn record_decision(&mut self, decision: RoutingDecision) {
        self.decision_history.push(decision);
        if self.decision_history.len() > 20 {
            self.decision_history.remove(0);
        }
    }

    pub fn record_turn_cost(&mut self, cost: f64) {
        self.accumulated_cost += cost;
    }

    pub fn accumulated_cost(&self) -> f64 {
        self.accumulated_cost
    }
    pub fn last_score(&self) -> f64 {
        self.last_score
    }
    pub fn history(&self) -> &[RoutingDecision] {
        &self.decision_history
    }
}

impl Default for RouterPipeline {
    fn default() -> Self {
        Self::new()
    }
}

// ── Provider ───────────────────────────────────────────────────────────────────

/// Router provider implementing the [`Provider`] trait.
///
/// Registers as provider `"router"` and accepts profile names as model IDs
/// (e.g. `"auto"`, `"budget"`). Delegation uses global provider resolution
/// (`get_provider_arc`) when `use_global_resolution` is true.
pub struct RouterProvider {
    pipeline: RwLock<RouterPipeline>,
    profiles: RwLock<RouterProfiles>,
    provider_registry: Arc<ProviderRegistry>,
    use_global_resolution: bool,
}

impl RouterProvider {
    /// Create with an instance-based ProviderRegistry.
    pub fn new(config: &RouterConfig, registry: Arc<ProviderRegistry>) -> Self {
        Self {
            pipeline: RwLock::new(RouterPipeline::from_config(config)),
            profiles: RwLock::new(RouterProfiles::from_config(config)),
            provider_registry: registry,
            use_global_resolution: false,
        }
    }

    /// Create with global provider resolution (for CLI use).
    pub fn new_global(config: &RouterConfig) -> Self {
        Self {
            pipeline: RwLock::new(RouterPipeline::from_config(config)),
            profiles: RwLock::new(RouterProfiles::from_config(config)),
            provider_registry: Arc::new(ProviderRegistry::new()),
            use_global_resolution: true,
        }
    }

    /// Hot-reload configuration at runtime.
    pub fn reload_config(&self, config: &RouterConfig) {
        let mut p = self.pipeline.write();
        p.weights = config.weights.clone();
        p.budget_limit = config.max_session_budget;
        p.context_upgrade_threshold = config.context_upgrade_threshold;
        drop(p);
        *self.profiles.write() = RouterProfiles::from_config(config);
    }

    /// Resolve a target provider (instance or global fallback).
    fn resolve_provider(&self, name: &str) -> Option<Arc<dyn Provider>> {
        if self.use_global_resolution {
            crate::get_provider_arc(name)
        } else {
            self.provider_registry.get(name)
        }
    }

    /// Resolve model from registry (instance or global fallback).
    #[allow(dead_code)]
    fn resolve_model(&self, provider: &str, model_id: &str) -> Option<Model> {
        crate::lookup_model(provider, model_id)
    }

    /// Update the global snapshot after a routing decision.
    fn update_snapshot(&self) {
        let p = self.pipeline.read();
        let last = p.history().last();
        let mut snap = ROUTER_SNAPSHOT.write();
        *snap = Some(RouterSnapshot {
            last_tier: last.map(|d| d.tier),
            last_score: p.last_score(),
            last_model: last.map(|d| d.target_label.clone()),
            last_provider: last.map(|d| d.target_provider.clone()),
            accumulated_cost: p.accumulated_cost(),
            turn_count: p.history().len(),
            profile: last.map(|d| d.profile.clone()),
        });
    }

    /// Get the current router state snapshot (global).
    pub fn get_snapshot() -> Option<RouterSnapshot> {
        ROUTER_SNAPSHOT.read().clone()
    }

    /// Route with vision awareness — extracts VisionSignal and adjusts tier/model.
    ///
    /// Returns `(vision_signal, adjusted_tier_config)`.
    pub fn route_with_vision(
        &self,
        context: &Context,
        profile_name: &str,
        tier: RouterTier,
    ) -> (VisionSignal, RoutedTierConfig) {
        // Extract vision signal from recent messages
        let vision = VisionSignal::extract(&context.messages, 10);

        // Get base tier config
        let tier_config = self
            .profiles
            .read()
            .tier_config(profile_name, tier)
            .cloned()
            .unwrap_or_else(|| RoutedTierConfig {
                model: String::new(),
                thinking: None,
                fallbacks: vec![],
            });

        // If vision is required, ensure we have a vision-capable model
        if vision.requires_vision() {
            let adjusted = self.ensure_vision_model(tier_config, tier, profile_name);
            (vision, adjusted)
        } else {
            (vision, tier_config)
        }
    }

    /// Ensure the selected model supports vision.
    ///
    /// Fallback priority:
    /// 1. Current model already supports vision → keep it
    /// 2. Fallback list contains a vision model → swap
    /// 3. Higher tier has a vision model → upgrade
    /// 4. No vision model found → warn and keep original
    fn ensure_vision_model(
        &self,
        tier_config: RoutedTierConfig,
        tier: RouterTier,
        profile_name: &str,
    ) -> RoutedTierConfig {
        // 1. Current model already supports vision?
        if let Some(pm) = parse_tier_model(&tier_config) {
            if let Some(model) = crate::lookup_model(&pm.provider, &pm.model_id) {
                if model.supports_vision() {
                    return tier_config;
                }
            }
        }

        let original_model = tier_config.model.clone();
        let thinking = tier_config.thinking;
        let fallbacks = tier_config.fallbacks.clone();

        // 2. Fallback list contains a vision model?
        for fb in &fallbacks {
            if let Some(pm) = ProviderModel::parse(fb) {
                if let Some(model) = crate::lookup_model(&pm.provider, &pm.model_id) {
                    if model.supports_vision() {
                        tracing::info!(
                            "Vision override: {} → {} (vision-capable fallback)",
                            original_model,
                            fb
                        );
                        return RoutedTierConfig {
                            model: fb.clone(),
                            thinking,
                            fallbacks,
                        };
                    }
                }
            }
        }

        // 3. Higher tier has a vision model?
        let profiles = self.profiles.read();
        if let Some(profile) = profiles.get_with_fallback(profile_name) {
            for higher_tier in [RouterTier::High, RouterTier::Medium] {
                if higher_tier.rank() > tier.rank() {
                    let tc = profile.tier_config(higher_tier);
                    if let Some(pm) = parse_tier_model(tc) {
                        if let Some(model) = crate::lookup_model(&pm.provider, &pm.model_id) {
                            if model.supports_vision() {
                                tracing::info!(
                                    "Vision upgrade: tier {:?} → {:?}, model {} → {}",
                                    tier,
                                    higher_tier,
                                    original_model,
                                    tc.model
                                );
                                return tc.clone();
                            }
                        }
                    }
                }
            }
        }

        // 4. No vision model found — warn and keep original
        tracing::warn!(
            "Vision required but no vision-capable model found for tier {:?}. \
             Model {} may fail with image content.",
            tier,
            original_model
        );
        tier_config
    }
}

#[async_trait::async_trait]
impl Provider for RouterProvider {
    fn name(&self) -> &str {
        "router"
    }

    async fn stream(
        &self,
        model: &Model,
        context: &Context,
        options: Option<StreamOptions>,
    ) -> Result<Pin<Box<dyn Stream<Item = ProviderEvent> + Send>>, ProviderError> {
        let profile_name = &model.id;

        // 1. Route through pipeline.
        let (score, tier, phase) = self.pipeline.write().route(context);

        // 2. Resolve tier config.
        let tier_config = match self
            .profiles
            .read()
            .tier_config(profile_name, tier)
            .cloned()
        {
            Some(tc) => tc,
            None => {
                return Err(ProviderError::StreamError(format!(
                    "Router profile '{}' is not configured. \
                     Run /router setup or edit ~/.oxi/settings.toml",
                    profile_name
                )));
            }
        };

        let pm = parse_tier_model(&tier_config).unwrap_or_else(|| ProviderModel {
            provider: model.provider.clone(),
            model_id: model.id.clone(),
        });

        // 3. Record decision.
        let decision = RoutingDecision {
            profile: profile_name.clone(),
            tier,
            phase,
            target_provider: pm.provider.clone(),
            target_model_id: pm.model_id.clone(),
            target_label: tier_config.model.clone(),
            reasoning: format!(
                "tier={tier:?}, score={score:.2}, provider={}, model={}",
                pm.provider, pm.model_id
            ),
            thinking: tier_config.thinking.unwrap_or(ThinkingLevel::Off),
            timestamp: chrono::Utc::now().timestamp_millis(),
            score,
            is_fallback: false,
            is_context_triggered: false,
            is_budget_forced: false,
            is_vision_triggered: false,
            vision_images: 0,
            decision_method: DecisionMethod::Heuristic,
        };
        self.pipeline.write().record_decision(decision);
        self.update_snapshot();

        // 4. Resolve target provider.
        let target_provider = self
            .resolve_provider(&pm.provider)
            .ok_or_else(|| ProviderError::UnknownProvider(pm.provider.clone()))?;

        // 5. Build target model.
        let target_model = build_target_model(&pm, tier_config.thinking.is_some());

        // 6. Inject thinking.
        let mut opts = options.unwrap_or_default();
        if let Some(thinking) = tier_config.thinking {
            opts.thinking_level = Some(thinking);
        }

        // 7. Truncate context if needed (preserve first message — system prompt).
        let estimated_chars: usize = context.messages.iter().map(message_chars).sum();
        let estimated_tokens = estimated_chars / 4;
        let adjusted_context = if estimated_tokens > target_model.context_window {
            let mut ctx = context.clone();
            let max_chars = target_model.context_window * 3;
            let mut total: usize = ctx.messages.iter().map(message_chars).sum();
            while total > max_chars && ctx.messages.len() > 2 {
                let removed = ctx.messages.remove(1);
                total -= message_chars(&removed);
            }
            ctx
        } else {
            context.clone()
        };

        // 8. Try primary model.
        match target_provider
            .stream(&target_model, &adjusted_context, Some(opts.clone()))
            .await
        {
            Ok(stream) => Ok(stream),
            Err(primary_err) => {
                if tier_config.fallbacks.is_empty() {
                    return Err(primary_err);
                }
                let chain = FallbackChain::new(tier_config.fallbacks.clone());
                chain
                    .try_models_with_resolver(
                        |name| self.resolve_provider(name),
                        &adjusted_context,
                        Some(opts),
                    )
                    .await
            }
        }
    }
}

// ── Registration ───────────────────────────────────────────────────────────────

/// Register the router provider and its profile models.
///
/// This is **opt-in** — the router is only active when the user selects
/// `router/auto` or another profile in model selection.
pub fn register_router(config: &RouterConfig) {
    let provider = RouterProvider::new_global(config);
    register_provider("router", provider);
    tracing::info!("Model router registered (opt-in: select router/auto)");
    for name in config.profiles.keys() {
        let model = Model::new(
            name,
            format!("Router ({name})"),
            Api::AnthropicMessages,
            "router",
            "router://local",
        );
        register_model(model);
        tracing::debug!("Registered router model: router/{}", name);
    }
}