base/provider/routing.rs
1//! What a session's code asks of the router its model calls go through, and
2//! what the router tells it back. The router itself — the providers of a
3//! pair, their health and quota, the per-session order — is the model
4//! crate's (`model::routing`); runtime and the modules see only this.
5
6use std::sync::Arc;
7
8use crate::interface::model::Model;
9
10/// Where a session's model calls go: the model each kind of call goes
11/// through, and what a model or tier name stands for at the provider calls
12/// go to first.
13pub trait TaskRouting: Send + Sync {
14 /// The model `task`'s calls go through. A task with no tier of its own
15 /// asks for `strong`.
16 fn model_for(&self, task: &str) -> Arc<dyn Model>;
17
18 /// The model `task` goes to at the head of the order; `None` for a task
19 /// that uses no model.
20 fn model_name_for(&self, task: &str) -> Option<String>;
21
22 /// The model `name` stands for at the head: its model of that tier when
23 /// `name` is a tier, else `name`.
24 fn resolve_model(&self, name: &str) -> String;
25
26 /// Report this session's provider switches to `observer`. The first
27 /// observer stays.
28 fn observe(&self, observer: Arc<dyn RouteObserver>);
29
30 /// A route of its own over the same providers: `provider` first, the
31 /// rest in this route's order, with its own record of where its calls
32 /// went and its own observer. `Err` when `provider` is not defined.
33 fn starting_at(&self, provider: &str) -> Result<Arc<dyn TaskRouting>, String>;
34
35 /// Whether `provider` is in this route's order.
36 fn in_order(&self, provider: &str) -> bool;
37
38 /// The first provider in the order that lists `model` or names it as a
39 /// tier. One that only accepts any model is no home.
40 fn home_of(&self, model: &str) -> Option<String>;
41
42 /// Where `task`'s call asking for `model` goes while the head can take
43 /// it; an empty `model` asks for the task's tier.
44 fn destination(&self, task: &str, model: &str) -> Destination;
45}
46
47/// A provider and the model a call is sent to it with.
48#[derive(
49 Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
50)]
51pub struct Destination {
52 pub provider: String,
53 pub model: String,
54}
55
56/// A call that went to a different provider than the call before it.
57#[derive(
58 Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
59)]
60pub struct ProviderSwitch {
61 pub from: String,
62 pub to: String,
63 /// Why each provider before `to` did not take the call; when none was
64 /// passed over, that `to` can take calls again.
65 pub reason: String,
66 /// The kind of call: `main`, `subagent`, `compact`, ….
67 pub task: String,
68}
69
70/// Told about every [`ProviderSwitch`] of a session's calls.
71pub trait RouteObserver: Send + Sync {
72 fn switched(&self, switch: &ProviderSwitch);
73}