use infinity_agent_core::ThreadId;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use infinity_protocol::ModelRef;
use infinity_provider_protocol::remote::RemoteModelProvider;
use infinity_provider_protocol::{ModelEntry, ModelProvider};
use serde::{Deserialize, Serialize};
use tokio::io::AsyncBufReadExt;
type BoxError = Box<dyn std::error::Error + Send + Sync>;
const PROVIDER_STARTUP_TIMEOUT: Duration = Duration::from_secs(30);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProviderConfig {
pub command: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub crate_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub git: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ProvidersConfig {
pub providers: Vec<(String, ProviderConfig)>,
}
impl ProvidersConfig {
pub fn get(&self, id: &str) -> Option<&ProviderConfig> {
self.providers
.iter()
.find(|(pid, _)| pid == id)
.map(|(_, config)| config)
}
pub fn upsert(&mut self, id: String, config: ProviderConfig) {
if let Some((_, existing)) = self.providers.iter_mut().find(|(pid, _)| *pid == id) {
*existing = config;
} else {
self.providers.push((id, config));
}
}
}
impl Serialize for ProvidersConfig {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(self.providers.len()))?;
for (id, config) in &self.providers {
map.serialize_entry(id, config)?;
}
map.end()
}
}
impl<'de> Deserialize<'de> for ProvidersConfig {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct OrderedMapVisitor;
impl<'de> serde::de::Visitor<'de> for OrderedMapVisitor {
type Value = ProvidersConfig;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a map from provider id to provider config")
}
fn visit_map<A: serde::de::MapAccess<'de>>(
self,
mut access: A,
) -> Result<Self::Value, A::Error> {
let mut providers = Vec::new();
while let Some((id, config)) = access.next_entry::<String, ProviderConfig>()? {
providers.push((id, config));
}
Ok(ProvidersConfig { providers })
}
}
deserializer.deserialize_map(OrderedMapVisitor)
}
}
pub fn load_providers_config(path: &Path) -> Result<ProvidersConfig, BoxError> {
if !path.exists() {
return Err(format!(
"no model providers configured ({} not found); \
run `infinity provider install <id> --crate <name>` to install one \
(e.g. `infinity provider install bedrock --crate infinity-provider-bedrock`)",
path.display()
)
.into());
}
let json = std::fs::read_to_string(path)
.map_err(|e| format!("failed to read {}: {e}", path.display()))?;
let config: ProvidersConfig = serde_json::from_str(&json)
.map_err(|e| format!("failed to parse {}: {e}", path.display()))?;
if config.providers.is_empty() {
return Err(format!("no providers configured in {}", path.display()).into());
}
let mut seen = std::collections::HashSet::new();
for (provider_id, provider) in &config.providers {
if provider_id.is_empty() {
return Err(format!("empty provider id in {}", path.display()).into());
}
if !seen.insert(provider_id) {
return Err(
format!("duplicate provider id {provider_id} in {}", path.display()).into(),
);
}
if provider.command.is_empty() {
return Err(format!(
"provider {provider_id} in {} has an empty command",
path.display()
)
.into());
}
}
Ok(config)
}
fn forward_output_to_log(
provider_id: &str,
stream_name: &'static str,
stream: impl tokio::io::AsyncRead + Unpin + Send + 'static,
) {
let id = provider_id.to_owned();
tokio::spawn(async move {
let mut lines = tokio::io::BufReader::new(stream).lines();
loop {
match lines.next_line().await {
Ok(Some(line)) => tracing::info!("provider {id} {stream_name}: {line}"),
Ok(None) => break,
Err(e) => {
tracing::warn!("failed reading {stream_name} of provider {id}: {e}");
break;
}
}
}
});
}
pub async fn spawn_provider(
provider_id: &str,
command: &[String],
) -> Result<(RemoteModelProvider, tokio::process::Child), BoxError> {
let mut child = tokio::process::Command::new(&command[0])
.args(&command[1..])
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.kill_on_drop(true)
.spawn()
.map_err(|e| {
format!(
"failed to spawn provider {provider_id} ({}): {e}",
command[0]
)
})?;
let stderr = child
.stderr
.take()
.expect("bug: piped child stderr missing");
forward_output_to_log(provider_id, "stderr", stderr);
let stdout = child
.stdout
.take()
.expect("bug: piped child stdout missing");
let mut lines = tokio::io::BufReader::new(stdout).lines();
let socket_path = tokio::time::timeout(PROVIDER_STARTUP_TIMEOUT, lines.next_line())
.await
.map_err(|_| format!("provider {provider_id} did not print a socket path in time"))?
.map_err(|e| format!("failed reading socket path from provider {provider_id}: {e}"))?
.ok_or_else(|| format!("provider {provider_id} exited without printing a socket path"))?;
forward_output_to_log(provider_id, "stdout", lines.into_inner());
Ok((RemoteModelProvider::new(socket_path.trim()), child))
}
pub async fn spawn_configured_providers(
config: &ProvidersConfig,
) -> Result<
(
Vec<(String, Arc<dyn ModelProvider>)>,
Vec<tokio::process::Child>,
),
BoxError,
> {
let mut providers: Vec<(String, Arc<dyn ModelProvider>)> = Vec::new();
let mut children = Vec::new();
for (provider_id, provider_config) in &config.providers {
let (provider, child) = spawn_provider(provider_id, &provider_config.command).await?;
tracing::info!(
"provider {provider_id} serving on {}",
provider.socket_path().display()
);
providers.push((provider_id.clone(), Arc::new(provider) as _));
children.push(child);
}
Ok((providers, children))
}
#[derive(Clone)]
pub struct CatalogModel {
pub provider_id: String,
pub entry: ModelEntry,
}
pub struct ModelCatalog {
providers: HashMap<String, Arc<dyn ModelProvider>>,
models: Vec<CatalogModel>,
default: ModelRef,
}
impl ModelCatalog {
pub async fn new(providers: Vec<(String, Arc<dyn ModelProvider>)>) -> Result<Self, BoxError> {
let mut provider_map: HashMap<String, Arc<dyn ModelProvider>> = HashMap::new();
let mut models = Vec::new();
for (provider_id, provider) in providers {
if provider_id.is_empty() {
return Err("model provider id must not be empty".into());
}
if provider_map.contains_key(&provider_id) {
return Err(format!("duplicate model provider id: {provider_id}").into());
}
for entry in provider
.list_models()
.await
.map_err(|e| format!("failed to list models from provider {provider_id}: {e}"))?
{
models.push(CatalogModel {
provider_id: provider_id.clone(),
entry,
});
}
provider_map.insert(provider_id, provider);
}
let default = models
.first()
.map(|m| ModelRef {
provider_id: m.provider_id.clone(),
model_id: m.entry.model_id.clone(),
})
.ok_or("no models available from any provider")?;
Ok(Self {
providers: provider_map,
models,
default,
})
}
pub fn models(&self) -> &[CatalogModel] {
&self.models
}
pub fn default_ref(&self) -> &ModelRef {
&self.default
}
pub fn default_entry(&self) -> &ModelEntry {
self.find(&self.default)
.expect("bug: default model missing from catalog")
}
pub fn find(&self, model: &ModelRef) -> Option<&ModelEntry> {
self.models
.iter()
.find(|m| m.provider_id == model.provider_id && m.entry.model_id == model.model_id)
.map(|m| &m.entry)
}
pub fn provider(&self, provider_id: &str) -> Option<&Arc<dyn ModelProvider>> {
self.providers.get(provider_id)
}
pub fn resolve(&self, selected: &ModelRef) -> (ModelRef, &ModelEntry, bool) {
if let Some(entry) = self.find(selected) {
return (selected.clone(), entry, false);
}
let entry = self
.find(&self.default)
.expect("bug: default model missing from catalog");
(self.default.clone(), entry, true)
}
}
pub struct CatalogModelSource {
pub catalog: Arc<ModelCatalog>,
pub conversation_store: crate::memory_store::PersistentConversationStore,
}
#[async_trait::async_trait(?Send)]
impl infinity_agent_core::system::ModelSource for CatalogModelSource {
async fn resolve(
&self,
thread_id: &ThreadId<str>,
) -> Result<infinity_agent_core::system::ResolvedModel, BoxError> {
let selected = self.conversation_store.get_thread_model(thread_id);
let (model_ref, entry, fell_back) = self.catalog.resolve(&selected);
if fell_back {
tracing::warn!(
"model {}/{} is not available for thread {}; using default {}/{}",
selected.provider_id,
selected.model_id,
thread_id,
model_ref.provider_id,
model_ref.model_id
);
}
let provider = self
.catalog
.provider(&model_ref.provider_id)
.expect("bug: cataloged model's provider missing from catalog")
.clone();
Ok(infinity_agent_core::system::ResolvedModel {
provider,
model_id: model_ref.model_id,
context_window: entry.context_window,
supports_image_input: entry.supports_image_input,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn missing_providers_config_is_an_error() {
let dir = tempfile::tempdir().expect("create temp dir");
let Err(err) = load_providers_config(&dir.path().join("providers.json")) else {
panic!("missing config should be an error");
};
assert!(
err.to_string().contains("infinity provider install"),
"error should point at `infinity provider install`: {err}"
);
}
#[test]
fn providers_config_parses_entries_and_preserves_order() {
let dir = tempfile::tempdir().expect("create temp dir");
let path = dir.path().join("providers.json");
std::fs::write(
&path,
r#"{
"zeta": { "command": ["zeta-provider"] },
"bedrock": {
"command": ["infinity-provider-bedrock"],
"crate_name": "infinity-provider-bedrock"
},
"custom": {
"command": ["/usr/local/bin/my-provider", "--flag", "value"],
"crate_name": "my-provider",
"git": "https://example.com/my-provider.git"
}
}"#,
)
.expect("write config");
let config = load_providers_config(&path).expect("load config");
let ids: Vec<&str> = config.providers.iter().map(|(id, _)| id.as_str()).collect();
assert_eq!(ids, ["zeta", "bedrock", "custom"]);
let custom = config.get("custom").expect("custom provider");
assert_eq!(
custom.command,
vec![
"/usr/local/bin/my-provider".to_owned(),
"--flag".to_owned(),
"value".to_owned()
]
);
assert_eq!(custom.crate_name.as_deref(), Some("my-provider"));
assert_eq!(
custom.git.as_deref(),
Some("https://example.com/my-provider.git")
);
assert_eq!(custom.path, None);
}
#[test]
fn providers_config_round_trips_in_order() {
let mut config = ProvidersConfig::default();
config.upsert(
"zeta".to_owned(),
ProviderConfig {
command: vec!["zeta-provider".to_owned()],
crate_name: None,
git: None,
path: None,
},
);
config.upsert(
"alpha".to_owned(),
ProviderConfig {
command: vec!["alpha-provider".to_owned()],
crate_name: Some("alpha-provider".to_owned()),
git: None,
path: Some("/src/alpha".to_owned()),
},
);
let json = serde_json::to_string_pretty(&config).expect("serialize");
assert!(json.find("zeta").expect("zeta") < json.find("alpha").expect("alpha"));
let parsed: ProvidersConfig = serde_json::from_str(&json).expect("parse");
assert_eq!(parsed, config);
}
#[test]
fn providers_config_upsert_replaces_in_place() {
let mut config = ProvidersConfig::default();
let entry = |cmd: &str| ProviderConfig {
command: vec![cmd.to_owned()],
crate_name: None,
git: None,
path: None,
};
config.upsert("first".to_owned(), entry("first-v1"));
config.upsert("second".to_owned(), entry("second-v1"));
config.upsert("first".to_owned(), entry("first-v2"));
let ids: Vec<&str> = config.providers.iter().map(|(id, _)| id.as_str()).collect();
assert_eq!(ids, ["first", "second"]);
assert_eq!(
config.get("first").map(|p| p.command.clone()),
Some(vec!["first-v2".to_owned()])
);
}
#[test]
fn providers_config_rejects_invalid_entries() {
let dir = tempfile::tempdir().expect("create temp dir");
let path = dir.path().join("providers.json");
std::fs::write(&path, "{}").expect("write config");
assert!(load_providers_config(&path).is_err(), "empty config");
std::fs::write(&path, r#"{"bedrock": {"command": []}}"#).expect("write config");
assert!(load_providers_config(&path).is_err(), "empty command");
std::fs::write(
&path,
r#"{"a": {"command": ["x"]}, "a": {"command": ["y"]}}"#,
)
.expect("write config");
assert!(load_providers_config(&path).is_err(), "duplicate ids");
std::fs::write(&path, "not json").expect("write config");
assert!(load_providers_config(&path).is_err(), "invalid json");
}
}