1use 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
28const PROVIDER_STARTUP_TIMEOUT: Duration = Duration::from_secs(30);
30
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct ProviderConfig {
34 pub command: Vec<String>,
37 #[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#[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 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
84impl 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
96impl<'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
125pub 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
166fn 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
189pub 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 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_output_to_log(provider_id, "stdout", lines.into_inner());
238
239 Ok((RemoteModelProvider::new(socket_path.trim()), child))
240}
241
242pub 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#[derive(Clone)]
270pub struct CatalogModel {
271 pub provider_id: String,
272 pub entry: ModelEntry,
273}
274
275pub struct ModelCatalog {
277 providers: HashMap<String, Arc<dyn ModelProvider>>,
279 models: Vec<CatalogModel>,
281 default: ModelRef,
283}
284
285impl ModelCatalog {
286 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 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 pub fn models(&self) -> &[CatalogModel] {
333 &self.models
334 }
335
336 pub fn default_ref(&self) -> &ModelRef {
338 &self.default
339 }
340
341 pub fn default_entry(&self) -> &ModelEntry {
343 self.find(&self.default)
344 .expect("bug: default model missing from catalog")
345 }
346
347 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 pub fn provider(&self, provider_id: &str) -> Option<&Arc<dyn ModelProvider>> {
357 self.providers.get(provider_id)
358 }
359
360 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
375pub 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 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 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}