Skip to main content

infinity_daemon/
models.rs

1//! Registry of model providers and the models they offer.
2//!
3//! Providers are stored in a map keyed by their stable unique id (e.g.
4//! `"bedrock"`). Models are identified globally by a [`ModelRef`]
5//! (provider id + provider-scoped model id).
6//!
7//! Providers run as separate processes configured in
8//! `~/.infinity/providers.json` (see [`ProvidersConfig`]): each entry maps a
9//! provider id to the command that serves that provider over a Unix socket.
10//! The daemon spawns each command, reads the socket path the process prints
11//! on stdout, and talks to it through a
12//! [`RemoteModelProvider`](infinity_provider_protocol::remote::RemoteModelProvider).
13
14use infinity_agent_core::ThreadId;
15use std::collections::HashMap;
16use std::path::Path;
17use std::sync::Arc;
18use std::time::Duration;
19
20use infinity_protocol::ModelRef;
21use infinity_provider_protocol::remote::RemoteModelProvider;
22use infinity_provider_protocol::{ModelEntry, ModelProvider};
23use serde::{Deserialize, Serialize};
24use tokio::io::AsyncBufReadExt;
25
26type BoxError = Box<dyn std::error::Error + Send + Sync>;
27
28/// How long to wait for a spawned provider process to print its socket path.
29const PROVIDER_STARTUP_TIMEOUT: Duration = Duration::from_secs(30);
30
31/// Configuration for a single model provider process in `providers.json`.
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct ProviderConfig {
34    /// Command (argv) that serves this provider over a Unix socket and
35    /// prints the socket path on stdout.
36    pub command: Vec<String>,
37    /// Installation source info (mirroring rap.json toolset commands) so the
38    /// provider can be re-installed by `infinity provider update`.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub crate_name: Option<String>,
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub git: Option<String>,
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    pub path: Option<String>,
45}
46
47/// The providers configured in `~/.infinity/providers.json`: a JSON object
48/// mapping provider id to a [`ProviderConfig`], e.g.:
49///
50/// ```json
51/// {
52///   "bedrock": { "command": ["infinity-provider-bedrock"] },
53///   "custom": { "command": ["my-provider", "--flag"], "crate_name": "my-provider" }
54/// }
55/// ```
56///
57/// Stored as a `Vec` of `(id, config)` pairs because the JSON object's
58/// insertion order is significant — providers are registered in config
59/// order, and the first model of the first provider is the global default.
60#[derive(Debug, Clone, PartialEq, Eq, Default)]
61pub struct ProvidersConfig {
62    pub providers: Vec<(String, ProviderConfig)>,
63}
64
65impl ProvidersConfig {
66    pub fn get(&self, id: &str) -> Option<&ProviderConfig> {
67        self.providers
68            .iter()
69            .find(|(pid, _)| pid == id)
70            .map(|(_, config)| config)
71    }
72
73    /// Insert or replace the config for `id`, preserving its position when
74    /// it already exists.
75    pub fn upsert(&mut self, id: String, config: ProviderConfig) {
76        if let Some((_, existing)) = self.providers.iter_mut().find(|(pid, _)| *pid == id) {
77            *existing = config;
78        } else {
79            self.providers.push((id, config));
80        }
81    }
82}
83
84// Serialize as a JSON object (entries in `providers` order).
85impl Serialize for ProvidersConfig {
86    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
87        use serde::ser::SerializeMap;
88        let mut map = serializer.serialize_map(Some(self.providers.len()))?;
89        for (id, config) in &self.providers {
90            map.serialize_entry(id, config)?;
91        }
92        map.end()
93    }
94}
95
96// Deserialize from a JSON object, preserving the document's entry order
97// (serde visits map entries in the order they appear in the input).
98impl<'de> Deserialize<'de> for ProvidersConfig {
99    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
100        struct OrderedMapVisitor;
101
102        impl<'de> serde::de::Visitor<'de> for OrderedMapVisitor {
103            type Value = ProvidersConfig;
104
105            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
106                f.write_str("a map from provider id to provider config")
107            }
108
109            fn visit_map<A: serde::de::MapAccess<'de>>(
110                self,
111                mut access: A,
112            ) -> Result<Self::Value, A::Error> {
113                let mut providers = Vec::new();
114                while let Some((id, config)) = access.next_entry::<String, ProviderConfig>()? {
115                    providers.push((id, config));
116                }
117                Ok(ProvidersConfig { providers })
118            }
119        }
120
121        deserializer.deserialize_map(OrderedMapVisitor)
122    }
123}
124
125/// Load the providers config from `path`. There is no implicit default: at
126/// least one provider must be configured (e.g. via
127/// `infinity provider install`) for the daemon to start.
128pub fn load_providers_config(path: &Path) -> Result<ProvidersConfig, BoxError> {
129    if !path.exists() {
130        return Err(format!(
131            "no model providers configured ({} not found); \
132             run `infinity provider install <id> --crate <name>` to install one \
133             (e.g. `infinity provider install bedrock --crate infinity-provider-bedrock`)",
134            path.display()
135        )
136        .into());
137    }
138    let json = std::fs::read_to_string(path)
139        .map_err(|e| format!("failed to read {}: {e}", path.display()))?;
140    let config: ProvidersConfig = serde_json::from_str(&json)
141        .map_err(|e| format!("failed to parse {}: {e}", path.display()))?;
142    if config.providers.is_empty() {
143        return Err(format!("no providers configured in {}", path.display()).into());
144    }
145    let mut seen = std::collections::HashSet::new();
146    for (provider_id, provider) in &config.providers {
147        if provider_id.is_empty() {
148            return Err(format!("empty provider id in {}", path.display()).into());
149        }
150        if !seen.insert(provider_id) {
151            return Err(
152                format!("duplicate provider id {provider_id} in {}", path.display()).into(),
153            );
154        }
155        if provider.command.is_empty() {
156            return Err(format!(
157                "provider {provider_id} in {} has an empty command",
158                path.display()
159            )
160            .into());
161        }
162    }
163    Ok(config)
164}
165
166/// Forward a child output stream to the daemon's tracing log, line by line.
167/// Also keeps the pipe drained so the child never blocks on a full pipe.
168fn forward_output_to_log(
169    provider_id: &str,
170    stream_name: &'static str,
171    stream: impl tokio::io::AsyncRead + Unpin + Send + 'static,
172) {
173    let id = provider_id.to_owned();
174    tokio::spawn(async move {
175        let mut lines = tokio::io::BufReader::new(stream).lines();
176        loop {
177            match lines.next_line().await {
178                Ok(Some(line)) => tracing::info!("provider {id} {stream_name}: {line}"),
179                Ok(None) => break,
180                Err(e) => {
181                    tracing::warn!("failed reading {stream_name} of provider {id}: {e}");
182                    break;
183                }
184            }
185        }
186    });
187}
188
189/// Spawn a provider process and wait for it to print its socket path on
190/// stdout. The returned [`Child`](tokio::process::Child) kills the process
191/// when dropped; callers must keep it alive for as long as the provider is
192/// in use.
193///
194/// The child gets a null stdin and piped stdout/stderr — never the daemon's
195/// own handles, which can be closed pipes when the daemon was auto-launched
196/// in the background. Everything the provider prints (beyond the initial
197/// socket path line) is forwarded to the daemon's log.
198pub async fn spawn_provider(
199    provider_id: &str,
200    command: &[String],
201) -> Result<(RemoteModelProvider, tokio::process::Child), BoxError> {
202    let mut child = tokio::process::Command::new(&command[0])
203        .args(&command[1..])
204        .stdin(std::process::Stdio::null())
205        .stdout(std::process::Stdio::piped())
206        .stderr(std::process::Stdio::piped())
207        .kill_on_drop(true)
208        .spawn()
209        .map_err(|e| {
210            format!(
211                "failed to spawn provider {provider_id} ({}): {e}",
212                command[0]
213            )
214        })?;
215
216    // Forward stderr to the log right away so startup logs are captured and
217    // a chatty provider can't fill the pipe while we wait for the socket
218    // path below.
219    let stderr = child
220        .stderr
221        .take()
222        .expect("bug: piped child stderr missing");
223    forward_output_to_log(provider_id, "stderr", stderr);
224
225    let stdout = child
226        .stdout
227        .take()
228        .expect("bug: piped child stdout missing");
229    let mut lines = tokio::io::BufReader::new(stdout).lines();
230    let socket_path = tokio::time::timeout(PROVIDER_STARTUP_TIMEOUT, lines.next_line())
231        .await
232        .map_err(|_| format!("provider {provider_id} did not print a socket path in time"))?
233        .map_err(|e| format!("failed reading socket path from provider {provider_id}: {e}"))?
234        .ok_or_else(|| format!("provider {provider_id} exited without printing a socket path"))?;
235
236    // Forward any further stdout output to the log as well.
237    forward_output_to_log(provider_id, "stdout", lines.into_inner());
238
239    Ok((RemoteModelProvider::new(socket_path.trim()), child))
240}
241
242/// Spawn every provider in the config. Returns the `(provider_id, provider)`
243/// pairs for [`ModelCatalog::new`] plus the child process handles, which
244/// must be kept alive for as long as the providers are in use.
245pub async fn spawn_configured_providers(
246    config: &ProvidersConfig,
247) -> Result<
248    (
249        Vec<(String, Arc<dyn ModelProvider>)>,
250        Vec<tokio::process::Child>,
251    ),
252    BoxError,
253> {
254    let mut providers: Vec<(String, Arc<dyn ModelProvider>)> = Vec::new();
255    let mut children = Vec::new();
256    for (provider_id, provider_config) in &config.providers {
257        let (provider, child) = spawn_provider(provider_id, &provider_config.command).await?;
258        tracing::info!(
259            "provider {provider_id} serving on {}",
260            provider.socket_path().display()
261        );
262        providers.push((provider_id.clone(), Arc::new(provider) as _));
263        children.push(child);
264    }
265    Ok((providers, children))
266}
267
268/// A model in the catalog, tagged with the provider that offers it.
269#[derive(Clone)]
270pub struct CatalogModel {
271    pub provider_id: String,
272    pub entry: ModelEntry,
273}
274
275/// All registered providers and their available models.
276pub struct ModelCatalog {
277    /// Providers keyed by their stable unique id.
278    providers: HashMap<String, Arc<dyn ModelProvider>>,
279    /// Flattened list of all available models, in registration order.
280    models: Vec<CatalogModel>,
281    /// The global default model, used for any thread without a selected model.
282    default: ModelRef,
283}
284
285impl ModelCatalog {
286    /// Build a catalog from `(provider_id, provider)` pairs (in registration
287    /// order). Provider ids must be stable, unique, and non-empty. The first
288    /// model of the first provider becomes the global default.
289    pub async fn new(providers: Vec<(String, Arc<dyn ModelProvider>)>) -> Result<Self, BoxError> {
290        let mut provider_map: HashMap<String, Arc<dyn ModelProvider>> = HashMap::new();
291        let mut models = Vec::new();
292
293        for (provider_id, provider) in providers {
294            // The empty string is reserved: the conversation store uses an
295            // empty provider id as the serde sentinel for thread metadata
296            // that predates per-thread model tracking.
297            if provider_id.is_empty() {
298                return Err("model provider id must not be empty".into());
299            }
300            if provider_map.contains_key(&provider_id) {
301                return Err(format!("duplicate model provider id: {provider_id}").into());
302            }
303            for entry in provider
304                .list_models()
305                .await
306                .map_err(|e| format!("failed to list models from provider {provider_id}: {e}"))?
307            {
308                models.push(CatalogModel {
309                    provider_id: provider_id.clone(),
310                    entry,
311                });
312            }
313            provider_map.insert(provider_id, provider);
314        }
315
316        let default = models
317            .first()
318            .map(|m| ModelRef {
319                provider_id: m.provider_id.clone(),
320                model_id: m.entry.model_id.clone(),
321            })
322            .ok_or("no models available from any provider")?;
323
324        Ok(Self {
325            providers: provider_map,
326            models,
327            default,
328        })
329    }
330
331    /// All available models, in registration order.
332    pub fn models(&self) -> &[CatalogModel] {
333        &self.models
334    }
335
336    /// The global default model.
337    pub fn default_ref(&self) -> &ModelRef {
338        &self.default
339    }
340
341    /// The entry for the global default model.
342    pub fn default_entry(&self) -> &ModelEntry {
343        self.find(&self.default)
344            .expect("bug: default model missing from catalog")
345    }
346
347    /// Look up a model's entry by reference.
348    pub fn find(&self, model: &ModelRef) -> Option<&ModelEntry> {
349        self.models
350            .iter()
351            .find(|m| m.provider_id == model.provider_id && m.entry.model_id == model.model_id)
352            .map(|m| &m.entry)
353    }
354
355    /// Look up a provider by its id.
356    pub fn provider(&self, provider_id: &str) -> Option<&Arc<dyn ModelProvider>> {
357        self.providers.get(provider_id)
358    }
359
360    /// Resolve a selected model to a concrete `(ModelRef, entry)`, falling
361    /// back to the global default when the selection is no longer available.
362    /// Returns `(model, entry, fell_back)` where `fell_back` is true when a
363    /// stale selection was replaced by the default.
364    pub fn resolve(&self, selected: &ModelRef) -> (ModelRef, &ModelEntry, bool) {
365        if let Some(entry) = self.find(selected) {
366            return (selected.clone(), entry, false);
367        }
368        let entry = self
369            .find(&self.default)
370            .expect("bug: default model missing from catalog");
371        (self.default.clone(), entry, true)
372    }
373}
374
375/// [`ModelSource`] over the daemon's catalog: each completion round resolves
376/// the model persisted for its thread in the conversation store, falling back
377/// to the global default when the selection is no longer available. Model
378/// switching is just persisting a new selection — the next round picks it up,
379/// and an in-flight completion finishes on the model it started with.
380pub struct CatalogModelSource {
381    pub catalog: Arc<ModelCatalog>,
382    pub conversation_store: crate::memory_store::PersistentConversationStore,
383}
384
385#[async_trait::async_trait(?Send)]
386impl infinity_agent_core::system::ModelSource for CatalogModelSource {
387    async fn resolve(
388        &self,
389        thread_id: &ThreadId<str>,
390    ) -> Result<infinity_agent_core::system::ResolvedModel, BoxError> {
391        let selected = self.conversation_store.get_thread_model(thread_id);
392        let (model_ref, entry, fell_back) = self.catalog.resolve(&selected);
393        if fell_back {
394            tracing::warn!(
395                "model {}/{} is not available for thread {}; using default {}/{}",
396                selected.provider_id,
397                selected.model_id,
398                thread_id,
399                model_ref.provider_id,
400                model_ref.model_id
401            );
402        }
403        let provider = self
404            .catalog
405            .provider(&model_ref.provider_id)
406            .expect("bug: cataloged model's provider missing from catalog")
407            .clone();
408        Ok(infinity_agent_core::system::ResolvedModel {
409            provider,
410            model_id: model_ref.model_id,
411            context_window: entry.context_window,
412            supports_image_input: entry.supports_image_input,
413        })
414    }
415}
416
417#[cfg(test)]
418mod tests {
419    use super::*;
420
421    #[test]
422    fn missing_providers_config_is_an_error() {
423        let dir = tempfile::tempdir().expect("create temp dir");
424        let Err(err) = load_providers_config(&dir.path().join("providers.json")) else {
425            panic!("missing config should be an error");
426        };
427        assert!(
428            err.to_string().contains("infinity provider install"),
429            "error should point at `infinity provider install`: {err}"
430        );
431    }
432
433    #[test]
434    fn providers_config_parses_entries_and_preserves_order() {
435        let dir = tempfile::tempdir().expect("create temp dir");
436        let path = dir.path().join("providers.json");
437        // Note: ids deliberately not in alphabetical order — the document
438        // order must be preserved (it determines the default provider).
439        std::fs::write(
440            &path,
441            r#"{
442                "zeta": { "command": ["zeta-provider"] },
443                "bedrock": {
444                    "command": ["infinity-provider-bedrock"],
445                    "crate_name": "infinity-provider-bedrock"
446                },
447                "custom": {
448                    "command": ["/usr/local/bin/my-provider", "--flag", "value"],
449                    "crate_name": "my-provider",
450                    "git": "https://example.com/my-provider.git"
451                }
452            }"#,
453        )
454        .expect("write config");
455        let config = load_providers_config(&path).expect("load config");
456        let ids: Vec<&str> = config.providers.iter().map(|(id, _)| id.as_str()).collect();
457        assert_eq!(ids, ["zeta", "bedrock", "custom"]);
458        let custom = config.get("custom").expect("custom provider");
459        assert_eq!(
460            custom.command,
461            vec![
462                "/usr/local/bin/my-provider".to_owned(),
463                "--flag".to_owned(),
464                "value".to_owned()
465            ]
466        );
467        assert_eq!(custom.crate_name.as_deref(), Some("my-provider"));
468        assert_eq!(
469            custom.git.as_deref(),
470            Some("https://example.com/my-provider.git")
471        );
472        assert_eq!(custom.path, None);
473    }
474
475    #[test]
476    fn providers_config_round_trips_in_order() {
477        let mut config = ProvidersConfig::default();
478        config.upsert(
479            "zeta".to_owned(),
480            ProviderConfig {
481                command: vec!["zeta-provider".to_owned()],
482                crate_name: None,
483                git: None,
484                path: None,
485            },
486        );
487        config.upsert(
488            "alpha".to_owned(),
489            ProviderConfig {
490                command: vec!["alpha-provider".to_owned()],
491                crate_name: Some("alpha-provider".to_owned()),
492                git: None,
493                path: Some("/src/alpha".to_owned()),
494            },
495        );
496        let json = serde_json::to_string_pretty(&config).expect("serialize");
497        // "zeta" must come before "alpha" in the serialized object.
498        assert!(json.find("zeta").expect("zeta") < json.find("alpha").expect("alpha"));
499        let parsed: ProvidersConfig = serde_json::from_str(&json).expect("parse");
500        assert_eq!(parsed, config);
501    }
502
503    #[test]
504    fn providers_config_upsert_replaces_in_place() {
505        let mut config = ProvidersConfig::default();
506        let entry = |cmd: &str| ProviderConfig {
507            command: vec![cmd.to_owned()],
508            crate_name: None,
509            git: None,
510            path: None,
511        };
512        config.upsert("first".to_owned(), entry("first-v1"));
513        config.upsert("second".to_owned(), entry("second-v1"));
514        config.upsert("first".to_owned(), entry("first-v2"));
515        let ids: Vec<&str> = config.providers.iter().map(|(id, _)| id.as_str()).collect();
516        assert_eq!(ids, ["first", "second"]);
517        assert_eq!(
518            config.get("first").map(|p| p.command.clone()),
519            Some(vec!["first-v2".to_owned()])
520        );
521    }
522
523    #[test]
524    fn providers_config_rejects_invalid_entries() {
525        let dir = tempfile::tempdir().expect("create temp dir");
526        let path = dir.path().join("providers.json");
527
528        std::fs::write(&path, "{}").expect("write config");
529        assert!(load_providers_config(&path).is_err(), "empty config");
530
531        std::fs::write(&path, r#"{"bedrock": {"command": []}}"#).expect("write config");
532        assert!(load_providers_config(&path).is_err(), "empty command");
533
534        std::fs::write(
535            &path,
536            r#"{"a": {"command": ["x"]}, "a": {"command": ["y"]}}"#,
537        )
538        .expect("write config");
539        assert!(load_providers_config(&path).is_err(), "duplicate ids");
540
541        std::fs::write(&path, "not json").expect("write config");
542        assert!(load_providers_config(&path).is_err(), "invalid json");
543    }
544}