Skip to main content

walrus_model/
config.rs

1//! Model configuration.
2//!
3//! `ProviderDef` and `ApiStandard` are defined in wcore and re-exported here.
4
5use anyhow::{Result, bail};
6use serde::{Deserialize, Serialize};
7use std::collections::{BTreeMap, HashSet};
8
9pub use wcore::config::provider::{ApiStandard, ProviderDef};
10
11/// Model configuration for the daemon.
12///
13/// Providers are configured as `[provider.<name>]` sections, each owning
14/// a list of model names. The active model name lives in `[system.walrus].model`.
15#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16pub struct ModelConfig {
17    /// Optional embedding model.
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub embedding: Option<String>,
20}
21
22impl ModelConfig {
23    /// Validate provider definitions and reject duplicate model names.
24    pub fn validate(providers: &BTreeMap<String, ProviderDef>) -> Result<()> {
25        let mut seen = HashSet::new();
26        for (name, def) in providers {
27            def.validate(name).map_err(|e| anyhow::anyhow!(e))?;
28            for model in &def.models {
29                if !seen.insert(model.clone()) {
30                    bail!("duplicate model name '{model}' across providers");
31                }
32            }
33        }
34        Ok(())
35    }
36}