//! Which model writes which kind of analysis, and the models OpenRouter //! offers (with prices) for the admin page. //! //! The choice is in `model_settings`; without a row the configured default //! is used (`SUMMARY_MODEL` for the short summary, `ANALYSIS_MODEL` else, //! both Opus 5.5 by default). When the chosen model fails, the worker falls //! back to the default. The model is never shown to users (a business secret). use std::sync::Arc; use std::time::{Duration, Instant}; use analysis_prompt::Kind; use serde::Serialize; use serde_json::Value; use tokio::sync::Mutex; use crate::AppState; /// The OpenRouter list is fetched at most this often. const CATALOG_TTL: Duration = Duration::from_secs(3600); /// The model settings of the admin page: one model writes all analyses and /// forecasts, another the answers to questions, a third the short summary. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Function { Summary, Analysis, Question, } impl Function { pub const ALL: [Function; 3] = [Function::Summary, Function::Analysis, Function::Question]; pub fn key(self) -> &'static str { match self { Function::Summary => "summary", Function::Analysis => "analysis", Function::Question => "question", } } pub fn parse(key: &str) -> Option { Function::ALL.into_iter().find(|f| f.key() == key) } pub fn of(kind: Kind) -> Function { match kind { Kind::Summary => Function::Summary, Kind::Question => Function::Question, Kind::Natal | Kind::Synastry | Kind::Forecast => Function::Analysis, } } } /// The configured default model of a function; also the fallback when the /// chosen model fails. pub fn default_model(state: &AppState, function: Function) -> &str { match function { Function::Summary => &state.config.summary_model, Function::Analysis | Function::Question => &state.config.analysis_model, } } /// The model new texts of this kind are written with, and the fallback model /// (`None` when the chosen model is the default). pub async fn models_for(state: &AppState, kind: Kind) -> Result<(String, Option), sqlx::Error> { let function = Function::of(kind); let chosen: Option = sqlx::query_scalar("SELECT model FROM model_settings WHERE kind = $1") .bind(function.key()) .fetch_optional(&state.db) .await?; let default = default_model(state, function); Ok(match chosen { Some(model) if model != default => (model, Some(default.to_owned())), _ => (default.to_owned(), None), }) } /// The model new texts of this kind are written with. pub async fn model_for(state: &AppState, kind: Kind) -> Result { Ok(models_for(state, kind).await?.0) } /// One model OpenRouter offers, with its price in USD per million tokens. #[derive(Debug, Clone, Serialize)] pub struct ModelInfo { pub id: String, pub name: String, pub input_per_million: f64, pub output_per_million: f64, pub context_length: Option, } pub struct Catalog { url: String, http: reqwest::Client, cache: Mutex>)>>, } impl Catalog { pub fn new(url: &str) -> Self { let http = reqwest::Client::builder().timeout(Duration::from_secs(15)).build().expect("static configuration"); Self { url: url.to_owned(), http, cache: Mutex::default() } } /// The models that can write an analysis (they support the structured /// output the worker asks for), by name; a stale list when OpenRouter is down. pub async fn models(&self) -> Result>, String> { let mut cache = self.cache.lock().await; if let Some((at, list)) = cache.as_ref() && at.elapsed() < CATALOG_TTL { return Ok(list.clone()); } match self.fetch().await { Ok(list) => { let list = Arc::new(list); *cache = Some((Instant::now(), list.clone())); Ok(list) } Err(e) => match cache.as_ref() { Some((_, list)) => { tracing::warn!(error = %e, "OpenRouter model list unavailable, using the cached one"); Ok(list.clone()) } None => Err(e), }, } } async fn fetch(&self) -> Result, String> { let body: Value = self .http .get(&self.url) .send() .await .and_then(reqwest::Response::error_for_status) .map_err(|e| e.to_string())? .json() .await .map_err(|e| e.to_string())?; let price = |v: &Value| v.as_str().and_then(|s| s.parse::().ok()).map(|p| p * 1e6); let mut list: Vec = body["data"] .as_array() .ok_or("unexpected OpenRouter answer")? .iter() .filter(|m| { m["supported_parameters"].as_array().is_some_and(|p| p.iter().any(|x| x == "structured_outputs")) }) .filter_map(|m| { Some(ModelInfo { id: m["id"].as_str()?.to_owned(), name: m["name"].as_str().unwrap_or_default().to_owned(), input_per_million: price(&m["pricing"]["prompt"])?, output_per_million: price(&m["pricing"]["completion"])?, context_length: m["context_length"].as_u64(), }) }) .collect(); list.sort_by(|a, b| a.name.cmp(&b.name)); Ok(list) } }