mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-06-16 13:34:04 +08:00
* feat(db): add pricing config fields to proxy_config table - Add default_cost_multiplier field per app type - Add pricing_model_source field (request/response) - Add request_model field to proxy_request_logs table - Implement schema migration v5 * feat(api): add pricing config commands and provider meta fields - Add get/set commands for default cost multiplier - Add get/set commands for pricing model source - Extend ProviderMeta with cost_multiplier and pricing_model_source - Register new commands in Tauri invoke handler * fix(proxy): apply cost multiplier to total cost only - Move multiplier calculation from per-item to total cost - Add resolve_pricing_config for provider-level override - Include request_model and cost_multiplier in usage logs - Return new fields in get_request_logs API * feat(ui): add pricing config UI and usage log enhancements - Add pricing config section to provider advanced settings - Refactor PricingConfigPanel to compact table layout - Display all three apps (Claude/Codex/Gemini) in one view - Add multiplier column and request model display to logs - Add frontend API wrappers for pricing config * feat(i18n): add pricing config translations - Add zh/en/ja translations for pricing defaults config - Add translations for multiplier, requestModel, responseModel - Add provider pricing config translations * fix(pricing): align backfill cost calculation with real-time logic - Fix backfill to deduct cache_read_tokens from input (avoid double billing) - Apply multiplier only to total cost, not to each item - Add multiplier display in request detail panel with i18n support - Use AppError::localized for backend error messages - Fix init_proxy_config_rows to use per-app default values - Fix silent failure in set_default_cost_multiplier/set_pricing_model_source - Add clippy allow annotation for test mutex across await * style: format code with cargo fmt and prettier * fix(tests): correct error type assertions in proxy DAO tests The tests expected AppError::InvalidInput but the DAO functions use AppError::localized() which returns AppError::Localized variant. Updated assertions to match the correct error type with key validation. --------- Co-authored-by: Jason <farion1231@gmail.com>
141 lines
4.1 KiB
Rust
141 lines
4.1 KiB
Rust
//! 数据库模块 - SQLite 数据持久化
|
|
//!
|
|
//! 此模块提供应用的核心数据存储功能,包括:
|
|
//! - 供应商配置管理
|
|
//! - MCP 服务器配置
|
|
//! - 提示词管理
|
|
//! - Skills 管理
|
|
//! - 通用设置存储
|
|
//!
|
|
//! ## 架构设计
|
|
//!
|
|
//! ```text
|
|
//! database/
|
|
//! ├── mod.rs - Database 结构体 + 初始化
|
|
//! ├── schema.rs - 表结构定义 + Schema 迁移
|
|
//! ├── backup.rs - SQL 导入导出 + 快照备份
|
|
//! ├── migration.rs - JSON → SQLite 数据迁移
|
|
//! └── dao/ - 数据访问对象
|
|
//! ├── providers.rs
|
|
//! ├── mcp.rs
|
|
//! ├── prompts.rs
|
|
//! ├── skills.rs
|
|
//! └── settings.rs
|
|
//! ```
|
|
|
|
mod backup;
|
|
mod dao;
|
|
mod migration;
|
|
mod schema;
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
// DAO 类型导出供外部使用
|
|
pub use dao::FailoverQueueItem;
|
|
|
|
use crate::config::get_app_config_dir;
|
|
use crate::error::AppError;
|
|
use rusqlite::Connection;
|
|
use serde::Serialize;
|
|
use std::sync::Mutex;
|
|
|
|
// DAO 方法通过 impl Database 提供,无需额外导出
|
|
|
|
/// 数据库备份保留数量
|
|
const DB_BACKUP_RETAIN: usize = 10;
|
|
|
|
/// 当前 Schema 版本号
|
|
/// 每次修改表结构时递增,并在 schema.rs 中添加相应的迁移逻辑
|
|
pub(crate) const SCHEMA_VERSION: i32 = 5;
|
|
|
|
/// 安全地序列化 JSON,避免 unwrap panic
|
|
pub(crate) fn to_json_string<T: Serialize>(value: &T) -> Result<String, AppError> {
|
|
serde_json::to_string(value)
|
|
.map_err(|e| AppError::Config(format!("JSON serialization failed: {e}")))
|
|
}
|
|
|
|
/// 安全地获取 Mutex 锁,避免 unwrap panic
|
|
macro_rules! lock_conn {
|
|
($mutex:expr) => {
|
|
$mutex
|
|
.lock()
|
|
.map_err(|e| AppError::Database(format!("Mutex lock failed: {}", e)))?
|
|
};
|
|
}
|
|
|
|
// 导出宏供子模块使用
|
|
pub(crate) use lock_conn;
|
|
|
|
/// 数据库连接封装
|
|
///
|
|
/// 使用 Mutex 包装 Connection 以支持在多线程环境(如 Tauri State)中共享。
|
|
/// rusqlite::Connection 本身不是 Sync 的,因此需要这层包装。
|
|
pub struct Database {
|
|
pub(crate) conn: Mutex<Connection>,
|
|
}
|
|
|
|
impl Database {
|
|
/// 初始化数据库连接并创建表
|
|
///
|
|
/// 数据库文件位于 `~/.cc-switch/cc-switch.db`
|
|
pub fn init() -> Result<Self, AppError> {
|
|
let db_path = get_app_config_dir().join("cc-switch.db");
|
|
|
|
// 确保父目录存在
|
|
if let Some(parent) = db_path.parent() {
|
|
std::fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
|
}
|
|
|
|
let conn = Connection::open(&db_path).map_err(|e| AppError::Database(e.to_string()))?;
|
|
|
|
// 启用外键约束
|
|
conn.execute("PRAGMA foreign_keys = ON;", [])
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
|
|
let db = Self {
|
|
conn: Mutex::new(conn),
|
|
};
|
|
db.create_tables()?;
|
|
db.apply_schema_migrations()?;
|
|
db.ensure_model_pricing_seeded()?;
|
|
|
|
Ok(db)
|
|
}
|
|
|
|
/// 创建内存数据库(用于测试)
|
|
pub fn memory() -> Result<Self, AppError> {
|
|
let conn = Connection::open_in_memory().map_err(|e| AppError::Database(e.to_string()))?;
|
|
|
|
// 启用外键约束
|
|
conn.execute("PRAGMA foreign_keys = ON;", [])
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
|
|
let db = Self {
|
|
conn: Mutex::new(conn),
|
|
};
|
|
db.create_tables()?;
|
|
db.ensure_model_pricing_seeded()?;
|
|
|
|
Ok(db)
|
|
}
|
|
|
|
/// 检查 MCP 服务器表是否为空
|
|
pub fn is_mcp_table_empty(&self) -> Result<bool, AppError> {
|
|
let conn = lock_conn!(self.conn);
|
|
let count: i64 = conn
|
|
.query_row("SELECT COUNT(*) FROM mcp_servers", [], |row| row.get(0))
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
Ok(count == 0)
|
|
}
|
|
|
|
/// 检查提示词表是否为空
|
|
pub fn is_prompts_table_empty(&self) -> Result<bool, AppError> {
|
|
let conn = lock_conn!(self.conn);
|
|
let count: i64 = conn
|
|
.query_row("SELECT COUNT(*) FROM prompts", [], |row| row.get(0))
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
Ok(count == 0)
|
|
}
|
|
}
|