Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
217 changes: 217 additions & 0 deletions src-tauri/src/cli/commands/pricing.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
/// 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<serde_json::Value, AppError>,
) -> 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::<Vec<_>>()
});
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(())
}
}
35 changes: 35 additions & 0 deletions src-tauri/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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();
Expand Down
Loading