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