diff --git a/README.md b/README.md index 5439391a..01cbb5fe 100644 --- a/README.md +++ b/README.md @@ -505,6 +505,23 @@ Interactive mode supports English and Chinese, language settings are automatical ### 🔧 Utilities +Import reference text-model prices from [models.dev](https://models.dev) explicitly: + +```bash +cc-switch pricing sync --provider openai --model gpt-6.1-sol --dry-run +cc-switch pricing sync --provider openai --model gpt-6.1-sol --backfill +cc-switch pricing sync --provider anthropic --overwrite --json +``` + +`--provider` is a **models.dev catalog provider ID**, not a configured CC Switch provider. +Omit `--model` to import all supported text models for that provider, or repeat it to select +exact catalog IDs. Existing prices and deleted models are preserved unless `--overwrite` +is supplied. `--dry-run` previews the selected source prices without opening the database. +`--backfill` fills missing costs for the selected models; it does not reprice already +calculated history. Prices are base USD per million tokens: context tiers, discounts, +and your relay's actual billing are not represented. There is no background network sync; +run the command again when you want to refresh the catalog. + Shell completions, environment management, and other utilities. ```bash diff --git a/README_ZH.md b/README_ZH.md index 825aab47..7b0a0c62 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -506,6 +506,21 @@ cc-switch env tools # 检查 Claude/Codex/Gemini/OpenCode/Herme ### 🔧 实用工具 +手动从 [models.dev](https://models.dev) 同步文本模型的参考定价: + +```bash +cc-switch pricing sync --provider openai --model gpt-6.1-sol --dry-run +cc-switch pricing sync --provider openai --model gpt-6.1-sol --backfill +cc-switch pricing sync --provider anthropic --overwrite --json +``` + +`--provider` 指 **models.dev 目录中的供应商 ID**,不是 CC Switch 中配置的供应商。 +省略 `--model` 会导入该供应商支持的全部文本模型,也可重复此参数选择精确的模型 ID。 +默认保留已有价格和已删除模型,只有 `--overwrite` 才会覆盖或恢复。 +`--dry-run` 预览源价格,不打开数据库;`--backfill` 补算所选模型缺失的费用, +不会重算已经计价的历史记录。单价单位为美元/百万 Token,只使用基础单价, +不包含长上下文分层、折扣或中转商实际账单。此功能不在后台联网,需再次执行命令刷新价格。 + Shell 补全、环境管理等实用功能。 ```bash diff --git a/src-tauri/src/cli/commands/mod.rs b/src-tauri/src/cli/commands/mod.rs index 27d351ba..05b5dca2 100644 --- a/src-tauri/src/cli/commands/mod.rs +++ b/src-tauri/src/cli/commands/mod.rs @@ -14,6 +14,7 @@ pub mod failover; pub mod hermes; pub mod internal; pub mod mcp; +pub mod pricing; pub mod prompts; pub mod provider; pub mod provider_input; diff --git a/src-tauri/src/cli/commands/pricing.rs b/src-tauri/src/cli/commands/pricing.rs new file mode 100644 index 00000000..08e57183 --- /dev/null +++ b/src-tauri/src/cli/commands/pricing.rs @@ -0,0 +1,217 @@ +use clap::Subcommand; +use serde_json::json; + +use crate::database::Database; +use crate::error::AppError; +use crate::services::{global_proxy, model_pricing_sync}; + +#[derive(Subcommand, Debug, Clone)] +pub enum PricingCommand { + /// Import base USD-per-million text model prices from models.dev (explicit network request) + Sync { + /// models.dev provider ID, e.g. openai or anthropic; unrelated to configured provider IDs + #[arg(long)] + provider: String, + /// Exact catalog model ID; repeat to select multiple models (default: all text models) + #[arg(long)] + model: Vec, + /// Preview selected prices without changing the price catalog or usage costs + #[arg(long)] + dry_run: bool, + /// Replace existing prices and restore explicitly deleted model prices + #[arg(long)] + overwrite: bool, + /// Fill missing costs for selected models; preserve costs already calculated + #[arg(long, conflicts_with = "dry_run")] + backfill: bool, + /// Print machine-readable JSON + #[arg(long)] + json: bool, + }, +} + +pub fn execute(command: PricingCommand) -> Result<(), AppError> { + execute_with_catalog_fetch(command, || { + let runtime = + tokio::runtime::Runtime::new().map_err(|e| AppError::Message(e.to_string()))?; + runtime.block_on(model_pricing_sync::fetch_catalog()) + }) +} + +fn execute_with_catalog_fetch( + command: PricingCommand, + fetch: impl FnOnce() -> Result, +) -> Result<(), AppError> { + let PricingCommand::Sync { + provider, + model, + dry_run, + overwrite, + backfill, + json: json_output, + } = command; + if !dry_run { + global_proxy::initialize_http_client_from_disk_best_effort(); + } + let catalog = fetch()?; + let selection = model_pricing_sync::select_catalog(&catalog, &provider, &model)?; + finish_sync( + selection, + &provider, + dry_run, + overwrite, + backfill, + json_output, + ) +} + +fn finish_sync( + selection: model_pricing_sync::CatalogSelection, + provider: &str, + dry_run: bool, + overwrite: bool, + backfill: bool, + json_output: bool, +) -> Result<(), AppError> { + let (changed, preserved, backfilled) = if dry_run { + (0, 0, 0) + } else { + let db = Database::init_for_pricing_sync()?; + let (changed, preserved) = db.import_model_prices(&selection.prices, overwrite)?; + let mut backfilled = 0; + if backfill { + for price in &selection.prices { + backfilled += db + .backfill_missing_usage_costs_for_model(&price.model_id) + .map_err(|e| { + AppError::Message(format!("Prices saved, but usage backfill failed: {e}")) + })?; + } + } + (changed, preserved, backfilled) + }; + let result = json!({ + "source":"https://models.dev/api.json", "provider":provider, "dryRun":dry_run, + "selected":selection.prices.len(), "changed":changed, "preserved":preserved, + "skippedUnsupported":selection.skipped, "basePriceOnlyModels":selection.tiered, + "backfilled":backfilled, + "prices":selection.prices.iter().map(|p| json!({"modelId":p.model_id,"input":p.input_cost_per_million,"output":p.output_cost_per_million,"cacheRead":p.cache_read_cost_per_million,"cacheWrite":p.cache_creation_cost_per_million})).collect::>() + }); + if json_output { + println!( + "{}", + serde_json::to_string_pretty(&result) + .map_err(|source| AppError::JsonSerialize { source })? + ); + } else { + if dry_run { + for price in &selection.prices { + println!( + "{}: input={}, output={}, cache read={}, cache write={} USD / 1M tokens", + price.model_id, + price.input_cost_per_million, + price.output_cost_per_million, + price.cache_read_cost_per_million, + price.cache_creation_cost_per_million + ); + } + } + println!( + "{} {}: selected {}, saved {}, preserved {}, unsupported {}, backfilled {}.", + crate::t!("Pricing sync", "定价同步"), + provider, + selection.prices.len(), + changed, + preserved, + selection.skipped, + backfilled + ); + println!("{}", crate::t!( + "Reference base prices only; provider billing, context tiers and discounts may differ. Existing prices are preserved unless --overwrite is used.", + "仅导入参考基础价格;供应商账单、长上下文分层及折扣可能不同。默认保留已有价格,使用 --overwrite 才会覆盖。" + )); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::database::ModelPricingUpdate; + + fn selection(input: &str) -> model_pricing_sync::CatalogSelection { + model_pricing_sync::CatalogSelection { + prices: vec![ + ModelPricingUpdate::new("Vendor/SyncModel", "test", input, "0", "0", "0").unwrap(), + ], + skipped: 0, + tiered: 0, + } + } + + #[test] + fn pricing_sync_preview_does_not_open_database() { + let home = tempfile::tempdir().unwrap(); + let _env = crate::test_support::TestEnvGuard::isolated(home.path()); + let dir = home.path().join(".cc-switch"); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("cc-switch.db"); + std::fs::write(&path, b"not a database").unwrap(); + execute_with_catalog_fetch( + PricingCommand::Sync { provider: "test".into(), model: vec![], dry_run: true, overwrite: false, backfill: false, json: true }, + || Ok(json!({"test":{"models":{"Vendor/SyncModel":{"modalities":{"output":["text"]},"cost":{"input":2,"output":0}}}}})), + ).unwrap(); + assert_eq!(std::fs::read(path).unwrap(), b"not a database"); + } + + #[test] + fn pricing_sync_disk_path_preserves_history_and_backfills_with_new_exact_price( + ) -> Result<(), AppError> { + let home = tempfile::tempdir().unwrap(); + let _env = crate::test_support::TestEnvGuard::isolated(home.path()); + { + let db = Database::init_for_pricing_sync()?; + db.import_model_prices(&selection("99").prices, true)?; + db.import_model_prices( + &[ + ModelPricingUpdate::new("syncmodel", "fallback", "77", "0", "0", "0")?, + ModelPricingUpdate::new("unrelated", "other", "88", "0", "0", "0")?, + ], + true, + )?; + let conn = crate::database::lock_conn!(db.conn); + conn.execute_batch("INSERT INTO proxy_request_logs + (request_id, provider_id, app_type, model, input_tokens, output_tokens, + total_cost_usd, latency_ms, status_code, created_at) + VALUES ('selected', '_codex_session', 'codex', 'Vendor/SyncModel', 1000000, 0, '0', 0, 200, 1), + ('unrelated', '_codex_session', 'codex', 'unrelated', 1000000, 0, '0', 0, 200, 1);")?; + } + finish_sync(selection("2"), "test", false, true, false, true)?; + { + let db = Database::init_for_pricing_sync()?; + let conn = crate::database::lock_conn!(db.conn); + let unpriced: i64 = conn.query_row( + "SELECT COUNT(*) FROM proxy_request_logs WHERE total_cost_usd='0'", + [], + |r| r.get(0), + )?; + assert_eq!(unpriced, 2); + } + finish_sync(selection("3"), "test", false, true, true, true)?; + let db = Database::init_for_pricing_sync()?; + let conn = crate::database::lock_conn!(db.conn); + let cost: String = conn.query_row( + "SELECT total_cost_usd FROM proxy_request_logs WHERE request_id='selected'", + [], + |r| r.get(0), + )?; + assert_eq!(cost, "3.000000"); + let unrelated: String = conn.query_row( + "SELECT total_cost_usd FROM proxy_request_logs WHERE request_id='unrelated'", + [], + |r| r.get(0), + )?; + assert_eq!(unrelated, "0"); + Ok(()) + } +} diff --git a/src-tauri/src/cli/mod.rs b/src-tauri/src/cli/mod.rs index 9972619b..989b5c17 100644 --- a/src-tauri/src/cli/mod.rs +++ b/src-tauri/src/cli/mod.rs @@ -42,6 +42,10 @@ pub struct Cli { #[derive(Subcommand)] pub enum Commands { + /// Import reference model prices from models.dev + #[command(subcommand)] + Pricing(commands::pricing::PricingCommand), + /// Manage ChatGPT Codex OAuth accounts #[command(subcommand)] Auth(commands::auth::AuthCommand), @@ -147,6 +151,37 @@ mod tests { CompletionLifecycleCommand, CompletionsAction, ManagedShellSelection, }; + #[test] + fn pricing_sync_requires_provider_and_rejects_backfill_preview() { + assert!(Cli::try_parse_from(["cc-switch", "pricing", "sync"]).is_err()); + assert!(Cli::try_parse_from([ + "cc-switch", + "pricing", + "sync", + "--provider", + "openai", + "--dry-run", + "--backfill" + ]) + .is_err()); + let cli = Cli::try_parse_from([ + "cc-switch", + "pricing", + "sync", + "--provider", + "openai", + "--model", + "one", + "--model", + "two", + "--json", + ]) + .unwrap(); + assert!( + matches!(cli.command, Some(Commands::Pricing(super::commands::pricing::PricingCommand::Sync { provider, model, json: true, .. })) if provider == "openai" && model == ["one", "two"]) + ); + } + #[test] fn long_help_mentions_prompts_and_proxy_routes() { let mut cmd = Cli::command(); diff --git a/src-tauri/src/database/dao/model_pricing.rs b/src-tauri/src/database/dao/model_pricing.rs index 5370b04f..d688bf70 100644 --- a/src-tauri/src/database/dao/model_pricing.rs +++ b/src-tauri/src/database/dao/model_pricing.rs @@ -61,6 +61,53 @@ impl ModelPricingUpdate { } impl Database { + /// Import one catalog selection atomically, preserving local overrides and deletions by default. + #[cfg(feature = "cli")] + pub(crate) fn import_model_prices( + &self, + prices: &[ModelPricingUpdate], + overwrite: bool, + ) -> Result<(usize, usize), AppError> { + let prices = prices + .iter() + .map(ModelPricingUpdate::normalized) + .collect::, _>>()?; + let mut conn = lock_conn!(self.conn); + let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?; + let deleted = Self::deleted_model_pricing_ids_on_conn(&tx)?; + let mut changed = 0; + let mut preserved = 0; + for price in prices { + let exists: bool = tx.query_row( + "SELECT EXISTS(SELECT 1 FROM model_pricing WHERE model_id = ?1)", + [&price.model_id], + |row| row.get(0), + )?; + if !overwrite && (exists || deleted.contains(&price.model_id)) { + preserved += 1; + continue; + } + tx.execute( + "INSERT INTO model_pricing (model_id, display_name, input_cost_per_million, + output_cost_per_million, cache_read_cost_per_million, cache_creation_cost_per_million) + VALUES (?1, ?2, ?3, ?4, ?5, ?6) + ON CONFLICT(model_id) DO UPDATE SET + display_name = excluded.display_name, + input_cost_per_million = excluded.input_cost_per_million, + output_cost_per_million = excluded.output_cost_per_million, + cache_read_cost_per_million = excluded.cache_read_cost_per_million, + cache_creation_cost_per_million = excluded.cache_creation_cost_per_million", + params![price.model_id, price.display_name, price.input_cost_per_million, + price.output_cost_per_million, price.cache_read_cost_per_million, + price.cache_creation_cost_per_million], + )?; + Self::forget_deleted_model_pricing_on_conn(&tx, &price.model_id)?; + changed += 1; + } + tx.commit()?; + Ok((changed, preserved)) + } + pub(crate) fn upsert_model_pricing( &self, pricing: &ModelPricingUpdate, @@ -215,3 +262,98 @@ fn pricing_decimal_field(field: &str, value: String) -> Result } Ok(trimmed.to_string()) } + +#[cfg(all(test, feature = "cli"))] +mod sync_tests { + use super::*; + + fn price(id: &str, input: &str) -> ModelPricingUpdate { + ModelPricingUpdate::new(id, id, input, "10", "0.1", "0").unwrap() + } + + #[test] + fn pricing_sync_preserves_local_prices_and_deletions_unless_overwritten() -> Result<(), AppError> + { + let db = Database::memory()?; + db.upsert_model_pricing(&price("sync-local", "99"))?; + db.upsert_model_pricing(&price("sync-deleted", "99"))?; + db.delete_model_pricing("sync-deleted")?; + let batch = vec![ + price("sync-local", "2"), + price("sync-deleted", "2"), + price("sync-new", "2"), + ]; + assert_eq!(db.import_model_prices(&batch, false)?, (1, 2)); + assert_eq!(db.import_model_prices(&batch, false)?, (0, 3)); + { + let conn = lock_conn!(db.conn); + let value: String = conn.query_row( + "SELECT input_cost_per_million FROM model_pricing WHERE model_id='sync-local'", + [], + |r| r.get(0), + )?; + assert_eq!(value, "99"); + } + assert_eq!(db.import_model_prices(&batch, true)?, (3, 0)); + let conn = lock_conn!(db.conn); + assert!(!Database::deleted_model_pricing_ids_on_conn(&conn)?.contains("sync-deleted")); + Ok(()) + } + + #[test] + fn pricing_sync_rolls_back_whole_batch_on_write_failure() -> Result<(), AppError> { + let db = Database::memory()?; + { + let conn = lock_conn!(db.conn); + conn.execute_batch( + "CREATE TRIGGER fail_sync BEFORE INSERT ON model_pricing + WHEN NEW.model_id = 'sync-fail' BEGIN SELECT RAISE(ABORT, 'test failure'); END;", + )?; + } + assert!(db + .import_model_prices(&[price("sync-first", "2"), price("sync-fail", "2")], false) + .is_err()); + let conn = lock_conn!(db.conn); + let count: i64 = conn.query_row( + "SELECT COUNT(*) FROM model_pricing WHERE model_id='sync-first'", + [], + |r| r.get(0), + )?; + assert_eq!(count, 0); + Ok(()) + } + + #[test] + fn pricing_sync_backfills_missing_costs_without_repricing_history() -> Result<(), AppError> { + let db = Database::memory()?; + { + let conn = lock_conn!(db.conn); + conn.execute_batch( + "INSERT INTO proxy_request_logs + (request_id, provider_id, app_type, model, input_tokens, output_tokens, + cache_read_tokens, total_cost_usd, latency_ms, status_code, created_at) + VALUES ('sync-unpriced', '_codex_session', 'codex', 'sync-model', 1000000, 100000, + 500000, '0', 0, 200, 1), + ('sync-priced', '_codex_session', 'codex', 'sync-model', 1000000, 100000, + 500000, '99', 0, 200, 1);", + )?; + } + db.import_model_prices(&[price("sync-model", "2")], false)?; + assert_eq!(db.backfill_missing_usage_costs_for_model("sync-model")?, 1); + assert_eq!(db.backfill_missing_usage_costs_for_model("sync-model")?, 0); + let conn = lock_conn!(db.conn); + let cost: String = conn.query_row( + "SELECT total_cost_usd FROM proxy_request_logs WHERE request_id='sync-unpriced'", + [], + |r| r.get(0), + )?; + assert_eq!(cost, "2.050000"); + let cost: String = conn.query_row( + "SELECT total_cost_usd FROM proxy_request_logs WHERE request_id='sync-priced'", + [], + |r| r.get(0), + )?; + assert_eq!(cost, "99"); + Ok(()) + } +} diff --git a/src-tauri/src/database/mod.rs b/src-tauri/src/database/mod.rs index 0663cfb3..4df6d505 100644 --- a/src-tauri/src/database/mod.rs +++ b/src-tauri/src/database/mod.rs @@ -481,7 +481,14 @@ impl Database { /// /// 数据库文件位于 `~/.cc-switch/cc-switch.db` pub fn init() -> Result { - Self::init_impl(true) + Self::init_impl(true, true) + } + + /// Initialize safely without globally backfilling or pruning usage before + /// the caller imports prices and optionally backfills selected models. + #[cfg(feature = "cli")] + pub(crate) fn init_for_pricing_sync() -> Result { + Self::init_impl(true, false) } /// Daemon startup already owns `daemon.pid`; the type-level lease proves @@ -490,12 +497,12 @@ impl Database { pub(crate) fn init_for_daemon( _pidfile: &crate::daemon::pidfile::PidFile, ) -> Result { - Self::init_impl(false) + Self::init_impl(false, true) } #[cfg_attr(not(unix), allow(unused_variables))] - fn init_impl(quiesce_daemon: bool) -> Result { - let result = Self::init_impl_inner(quiesce_daemon); + fn init_impl(quiesce_daemon: bool, maintain_usage: bool) -> Result { + let result = Self::init_impl_inner(quiesce_daemon, maintain_usage); match result { Ok(database) => Ok(database), Err(error) => { @@ -506,7 +513,7 @@ impl Database { } #[cfg_attr(not(unix), allow(unused_variables))] - fn init_impl_inner(quiesce_daemon: bool) -> Result { + fn init_impl_inner(quiesce_daemon: bool, maintain_usage: bool) -> Result { if let Err(err) = crate::config::validate_config_dir() { log::warn!("拒绝初始化数据库:配置目录校验失败: {err}"); return Err(err); @@ -677,7 +684,9 @@ impl Database { log::warn!("Failed to ensure incremental auto-vacuum: {err}"); } db.ensure_model_pricing_seeded()?; - db.run_usage_maintenance("startup"); + if maintain_usage { + db.run_usage_maintenance("startup"); + } drop(migration_sync_guard); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1493b28e..5d2953e6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -51,6 +51,7 @@ fn run(cli: Cli) -> Result<(), AppError> { // Default to interactive mode if no command is provided None | Some(Commands::Interactive) => cc_switch_lib::cli::interactive::run(cli.app), Some(Commands::Auth(cmd)) => cc_switch_lib::cli::commands::auth::execute(cmd), + Some(Commands::Pricing(cmd)) => cc_switch_lib::cli::commands::pricing::execute(cmd), Some(Commands::Provider(cmd)) => { cc_switch_lib::cli::commands::provider::execute(cmd, cli.app) } @@ -99,6 +100,7 @@ fn command_requires_startup_state(command: &Option) -> bool { | Some(Commands::Update(_)) | Some(Commands::Internal(_)) | Some(Commands::Sessions(_)) + | Some(Commands::Pricing(_)) | Some(Commands::Settings( cc_switch_lib::cli::commands::settings::SettingsCommand::CodexAuthPreservation(_), )) => false, @@ -207,6 +209,15 @@ mod tests { #[test] fn update_and_completions_skip_startup_state() { + let pricing = Cli::parse_from([ + "cc-switch", + "pricing", + "sync", + "--provider", + "openai", + "--dry-run", + ]); + assert!(!command_requires_startup_state(&pricing.command)); let update = Cli::parse_from(["cc-switch", "update"]); let completions_generate = Cli::parse_from(["cc-switch", "completions", "bash"]); let completions_install = Cli::parse_from(["cc-switch", "completions", "install"]); diff --git a/src-tauri/src/services/mod.rs b/src-tauri/src/services/mod.rs index b573ab62..0d2e44f2 100644 --- a/src-tauri/src/services/mod.rs +++ b/src-tauri/src/services/mod.rs @@ -16,6 +16,8 @@ pub mod global_proxy; pub mod local_env_check; pub mod mcp; pub mod model_fetch; +#[cfg(feature = "cli")] +pub(crate) mod model_pricing_sync; pub(crate) mod pi_prompt_files; pub(crate) mod pi_state; pub mod prompt; diff --git a/src-tauri/src/services/model_pricing_sync.rs b/src-tauri/src/services/model_pricing_sync.rs new file mode 100644 index 00000000..aea83135 --- /dev/null +++ b/src-tauri/src/services/model_pricing_sync.rs @@ -0,0 +1,244 @@ +//! Explicit, provider-scoped imports of base USD-per-million prices from models.dev. + +use crate::database::ModelPricingUpdate; +use crate::error::AppError; +use serde_json::Value; +use std::collections::BTreeSet; +use std::time::Duration; + +const CATALOG_URL: &str = "https://models.dev/api.json"; +const MAX_CATALOG_BYTES: usize = 32 * 1024 * 1024; + +pub(crate) struct CatalogSelection { + pub prices: Vec, + pub skipped: usize, + pub tiered: usize, +} + +pub(crate) async fn fetch_catalog() -> Result { + // Honor the existing outbound proxy configuration without sending provider credentials. + let client = crate::proxy::http_client::get(); + fetch_catalog_from(&client, CATALOG_URL).await +} + +async fn fetch_catalog_from(client: &reqwest::Client, url: &str) -> Result { + let mut response = client + .get(url) + .timeout(Duration::from_secs(30)) + .send() + .await + .and_then(reqwest::Response::error_for_status) + .map_err(|e| AppError::Message(format!("models.dev download failed: {e}")))?; + if response + .content_length() + .is_some_and(|n| n > MAX_CATALOG_BYTES as u64) + { + return Err(AppError::Message( + "models.dev catalog exceeds 32 MiB".into(), + )); + } + let mut bytes = Vec::new(); + while let Some(chunk) = response + .chunk() + .await + .map_err(|e| AppError::Message(format!("models.dev download failed: {e}")))? + { + if bytes.len().saturating_add(chunk.len()) > MAX_CATALOG_BYTES { + return Err(AppError::Message( + "models.dev catalog exceeds 32 MiB".into(), + )); + } + bytes.extend_from_slice(&chunk); + } + serde_json::from_slice(&bytes) + .map_err(|e| AppError::Message(format!("Invalid models.dev catalog: {e}"))) +} + +pub(crate) fn select_catalog( + catalog: &Value, + provider: &str, + models: &[String], +) -> Result { + let entries = catalog + .get(provider) + .and_then(|p| p.get("models")) + .and_then(Value::as_object) + .ok_or_else(|| { + AppError::InvalidInput(format!("Unknown models.dev provider: {provider}")) + })?; + let requested: BTreeSet<&str> = models.iter().map(String::as_str).collect(); + for id in &requested { + if !entries.contains_key(*id) { + return Err(AppError::InvalidInput(format!( + "Model {id} not found in models.dev provider {provider}" + ))); + } + } + let mut selection = CatalogSelection { + prices: Vec::new(), + skipped: 0, + tiered: 0, + }; + // Sorting gives deterministic output independent of the catalog's JSON object order. + let sorted: std::collections::BTreeMap<_, _> = entries.iter().collect(); + for (id, model) in sorted { + if !requested.is_empty() && !requested.contains(id.as_str()) { + continue; + } + match parse_price(id, model) { + Ok(price) => { + let cost = &model["cost"]; + if cost.get("tiers").is_some() || cost.get("context_over_200k").is_some() { + selection.tiered += 1; + } + selection.prices.push(price); + } + Err(error) if requested.contains(id.as_str()) => return Err(error), + Err(_) => selection.skipped += 1, + } + } + if selection.prices.is_empty() { + return Err(AppError::InvalidInput( + "No supported text model prices found in selection".into(), + )); + } + Ok(selection) +} + +fn parse_price(id: &str, model: &Value) -> Result { + let invalid = || { + AppError::InvalidInput(format!( + "Model {id} has unsupported or invalid text pricing" + )) + }; + if model.get("status").and_then(Value::as_str) == Some("deprecated") { + return Err(invalid()); + } + let output = model + .pointer("/modalities/output") + .and_then(Value::as_array) + .ok_or_else(invalid)?; + if output.is_empty() || output.iter().any(|v| v.as_str() != Some("text")) { + return Err(invalid()); + } + let cost = model + .get("cost") + .and_then(Value::as_object) + .ok_or_else(invalid)?; + let number = |field: &str, optional: bool| -> Result { + match cost.get(field) { + None | Some(Value::Null) if optional => Ok("0".into()), + Some(Value::Number(n)) => { + let raw = n.to_string(); + let value = raw + .parse::() + .or_else(|_| rust_decimal::Decimal::from_scientific(&raw)) + .map_err(|_| invalid())?; + if value < rust_decimal::Decimal::ZERO + || value >= rust_decimal::Decimal::from(1_000_000_000_000u64) + { + return Err(invalid()); + } + Ok(value.normalize().to_string()) + } + _ => Err(invalid()), + } + }; + // Keep exact catalog IDs: stripping a vendor prefix could overwrite an unrelated model. + ModelPricingUpdate::new( + id, + model + .get("name") + .and_then(Value::as_str) + .filter(|s| !s.trim().is_empty()) + .unwrap_or(id), + number("input", false)?, + number("output", false)?, + number("cache_read", true)?, + number("cache_write", true)?, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[tokio::test] + async fn pricing_sync_download_handles_http_json_and_size_errors() { + use std::io::{Read, Write}; + for (response, valid) in [ + ("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n{}", true), + ( + "HTTP/1.1 503 Unavailable\r\nContent-Length: 0\r\n\r\n", + false, + ), + ("HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nbad", false), + ("HTTP/1.1 200 OK\r\nContent-Length: 33554433\r\n\r\n", false), + ] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(5))) + .unwrap(); + let mut request = [0; 4096]; + let n = stream.read(&mut request).unwrap(); + assert!(!String::from_utf8_lossy(&request[..n]) + .to_lowercase() + .contains("authorization:")); + stream.write_all(response.as_bytes()).unwrap(); + }); + let client = reqwest::Client::builder().no_proxy().build().unwrap(); + assert_eq!(fetch_catalog_from(&client, &url).await.is_ok(), valid); + server.join().unwrap(); + } + } + + fn model(cost: Value) -> Value { + json!({"name":"Test", "modalities":{"output":["text"]}, "cost":cost}) + } + + #[test] + fn pricing_sync_preserves_exact_ids_and_provider_selection() { + let catalog = json!({"openai":{"models":{"vendor/test": model(json!({"input":2,"output":10,"cache_read":0.1,"tiers":[]}))}}, "other":{"models":{"vendor/test":model(json!({"input":99,"output":99}))}}}); + let selected = select_catalog(&catalog, "openai", &[]).unwrap(); + assert_eq!(selected.prices[0].model_id, "vendor/test"); + assert_eq!(selected.prices[0].input_cost_per_million, "2"); + assert_eq!(selected.prices[0].cache_read_cost_per_million, "0.1"); + assert_eq!(selected.prices[0].cache_creation_cost_per_million, "0"); + assert_eq!(selected.tiered, 1); + assert!(select_catalog(&catalog, "missing", &[]).is_err()); + assert!(select_catalog(&catalog, "openai", &["missing".into()]).is_err()); + } + + #[test] + fn pricing_sync_rejects_invalid_costs_but_accepts_free_models() { + for cost in [ + json!({"input":-1,"output":1}), + json!({"input":"2","output":1}), + json!({"input":2}), + json!({"input":2,"output":1,"cache_read":-1}), + ] { + assert!(parse_price("test", &model(cost)).is_err()); + } + assert_eq!( + parse_price("free", &model(json!({"input":0,"output":0}))) + .unwrap() + .input_cost_per_million, + "0" + ); + } + + #[test] + fn pricing_sync_skips_non_text_and_invalid_entries_unless_explicit() { + let mut audio = model(json!({"input":1,"output":2})); + audio["modalities"]["output"] = json!(["text", "audio"]); + let catalog = json!({"p":{"models":{"good":model(json!({"input":1,"output":2})),"audio":audio,"missing":{}}}}); + let selection = select_catalog(&catalog, "p", &[]).unwrap(); + assert_eq!(selection.prices.len(), 1); + assert_eq!(selection.skipped, 2); + assert!(select_catalog(&catalog, "p", &["audio".into()]).is_err()); + } +} diff --git a/src-tauri/src/services/usage_stats.rs b/src-tauri/src/services/usage_stats.rs index 568fdeea..aadffd14 100644 --- a/src-tauri/src/services/usage_stats.rs +++ b/src-tauri/src/services/usage_stats.rs @@ -1803,6 +1803,12 @@ fn find_model_pricing_raw( return Ok(None); } + // Imported catalog IDs can include case-sensitive namespaces and suffixes. + // Prefer the original ID before the compatibility normalization below. + if let Some(row) = query_model_pricing_exact(conn, model_id.trim())? { + return Ok(Some(row)); + } + for candidate in &candidates { if let Some(row) = query_model_pricing_exact(conn, candidate)? { return Ok(Some(row));