pub struct Database { /* private fields */ }Expand description
数据库连接封装
使用 Mutex 包装 Connection 以支持在多线程环境(如 Tauri State)中共享。 rusqlite::Connection 本身不是 Sync 的,因此需要这层包装。
Implementations§
Source§impl Database
impl Database
Source§impl Database
impl Database
Sourcepub fn get_failover_queue(
&self,
app_type: &str,
) -> Result<Vec<FailoverQueueItem>, AppError>
pub fn get_failover_queue( &self, app_type: &str, ) -> Result<Vec<FailoverQueueItem>, AppError>
获取故障转移队列(按 sort_index 排序)
Sourcepub fn get_failover_providers(
&self,
app_type: &str,
) -> Result<Vec<Provider>, AppError>
pub fn get_failover_providers( &self, app_type: &str, ) -> Result<Vec<Provider>, AppError>
获取故障转移队列中的供应商(完整 Provider 信息,按顺序)
Sourcepub fn add_to_failover_queue(
&self,
app_type: &str,
provider_id: &str,
) -> Result<(), AppError>
pub fn add_to_failover_queue( &self, app_type: &str, provider_id: &str, ) -> Result<(), AppError>
添加供应商到故障转移队列
Sourcepub fn remove_from_failover_queue(
&self,
app_type: &str,
provider_id: &str,
) -> Result<(), AppError>
pub fn remove_from_failover_queue( &self, app_type: &str, provider_id: &str, ) -> Result<(), AppError>
从故障转移队列中移除供应商
Source§impl Database
impl Database
Sourcepub fn get_all_providers(
&self,
app_type: &str,
) -> Result<IndexMap<String, Provider>, AppError>
pub fn get_all_providers( &self, app_type: &str, ) -> Result<IndexMap<String, Provider>, AppError>
获取指定应用类型的所有供应商
Sourcepub fn get_current_provider(
&self,
app_type: &str,
) -> Result<Option<String>, AppError>
pub fn get_current_provider( &self, app_type: &str, ) -> Result<Option<String>, AppError>
获取当前激活的供应商 ID
Sourcepub fn get_provider_by_id(
&self,
id: &str,
app_type: &str,
) -> Result<Option<Provider>, AppError>
pub fn get_provider_by_id( &self, id: &str, app_type: &str, ) -> Result<Option<Provider>, AppError>
根据 ID 获取单个供应商
Sourcepub fn get_provider_ids(
&self,
app_type: &str,
) -> Result<HashSet<String>, AppError>
pub fn get_provider_ids( &self, app_type: &str, ) -> Result<HashSet<String>, AppError>
仅获取指定 app 下所有 provider 的 id 集合。
Sourcepub fn has_non_official_seed_provider(
&self,
app_type: &str,
) -> Result<bool, AppError>
pub fn has_non_official_seed_provider( &self, app_type: &str, ) -> Result<bool, AppError>
判断指定 app 下是否存在非官方种子的供应商。
Sourcepub fn init_default_official_providers(&self) -> Result<usize, AppError>
pub fn init_default_official_providers(&self) -> Result<usize, AppError>
启动时补齐上游官方预设供应商(Claude / Codex / Gemini)。
Sourcepub fn save_provider(
&self,
app_type: &str,
provider: &Provider,
) -> Result<(), AppError>
pub fn save_provider( &self, app_type: &str, provider: &Provider, ) -> Result<(), AppError>
保存供应商(新增或更新)
注意:更新模式下不同步 endpoints,因为编辑模式下端点通过单独的 API 管理 (add_custom_endpoint / remove_custom_endpoint),避免覆盖用户的修改。
Sourcepub fn set_current_provider(
&self,
app_type: &str,
id: &str,
) -> Result<(), AppError>
pub fn set_current_provider( &self, app_type: &str, id: &str, ) -> Result<(), AppError>
设置当前供应商
Sourcepub fn update_provider_settings_config(
&self,
app_type: &str,
provider_id: &str,
settings_config: &Value,
) -> Result<(), AppError>
pub fn update_provider_settings_config( &self, app_type: &str, provider_id: &str, settings_config: &Value, ) -> Result<(), AppError>
更新供应商的 settings_config(仅更新配置,不改变其他字段)
Source§impl Database
impl Database
Sourcepub async fn get_global_proxy_config(
&self,
) -> Result<GlobalProxyConfig, AppError>
pub async fn get_global_proxy_config( &self, ) -> Result<GlobalProxyConfig, AppError>
获取全局代理配置(统一字段)
从 claude 行读取(三行镜像一致)
Sourcepub async fn update_global_proxy_config(
&self,
config: GlobalProxyConfig,
) -> Result<(), AppError>
pub async fn update_global_proxy_config( &self, config: GlobalProxyConfig, ) -> Result<(), AppError>
更新全局代理配置(镜像写三行)
Sourcepub async fn get_default_cost_multiplier(
&self,
app_type: &str,
) -> Result<String, AppError>
pub async fn get_default_cost_multiplier( &self, app_type: &str, ) -> Result<String, AppError>
获取默认成本倍率
Sourcepub async fn set_default_cost_multiplier(
&self,
app_type: &str,
value: &str,
) -> Result<(), AppError>
pub async fn set_default_cost_multiplier( &self, app_type: &str, value: &str, ) -> Result<(), AppError>
设置默认成本倍率
Sourcepub async fn get_pricing_model_source(
&self,
app_type: &str,
) -> Result<String, AppError>
pub async fn get_pricing_model_source( &self, app_type: &str, ) -> Result<String, AppError>
获取计费模式来源
Sourcepub async fn set_pricing_model_source(
&self,
app_type: &str,
value: &str,
) -> Result<(), AppError>
pub async fn set_pricing_model_source( &self, app_type: &str, value: &str, ) -> Result<(), AppError>
设置计费模式来源
Sourcepub async fn get_proxy_config_for_app(
&self,
app_type: &str,
) -> Result<AppProxyConfig, AppError>
pub async fn get_proxy_config_for_app( &self, app_type: &str, ) -> Result<AppProxyConfig, AppError>
获取应用级代理配置
Sourcepub async fn update_proxy_config_for_app(
&self,
config: AppProxyConfig,
) -> Result<(), AppError>
pub async fn update_proxy_config_for_app( &self, config: AppProxyConfig, ) -> Result<(), AppError>
更新应用级代理配置
Sourcepub async fn get_proxy_config(&self) -> Result<ProxyConfig, AppError>
pub async fn get_proxy_config(&self) -> Result<ProxyConfig, AppError>
获取代理配置(兼容旧接口,返回 claude 行的配置)
Sourcepub async fn update_proxy_config(
&self,
config: ProxyConfig,
) -> Result<(), AppError>
pub async fn update_proxy_config( &self, config: ProxyConfig, ) -> Result<(), AppError>
更新代理配置(兼容旧接口,更新所有三行的公共字段)
Sourcepub async fn set_live_takeover_active(
&self,
_active: bool,
) -> Result<(), AppError>
pub async fn set_live_takeover_active( &self, _active: bool, ) -> Result<(), AppError>
设置 Live 接管状态(兼容旧版本,更新 enabled 字段)
Sourcepub async fn is_live_takeover_active(&self) -> Result<bool, AppError>
pub async fn is_live_takeover_active(&self) -> Result<bool, AppError>
检查是否处于 Live 接管模式
检查是否有任一 app 的 enabled = true
Sourcepub async fn get_provider_health(
&self,
provider_id: &str,
app_type: &str,
) -> Result<ProviderHealth, AppError>
pub async fn get_provider_health( &self, provider_id: &str, app_type: &str, ) -> Result<ProviderHealth, AppError>
获取Provider健康状态
Sourcepub async fn update_provider_health(
&self,
provider_id: &str,
app_type: &str,
success: bool,
error_msg: Option<String>,
) -> Result<(), AppError>
pub async fn update_provider_health( &self, provider_id: &str, app_type: &str, success: bool, error_msg: Option<String>, ) -> Result<(), AppError>
更新Provider健康状态
使用默认阈值(5)判断是否健康,建议使用 update_provider_health_with_threshold 传入配置的阈值
Sourcepub async fn update_provider_health_with_threshold(
&self,
provider_id: &str,
app_type: &str,
success: bool,
error_msg: Option<String>,
failure_threshold: u32,
) -> Result<(), AppError>
pub async fn update_provider_health_with_threshold( &self, provider_id: &str, app_type: &str, success: bool, error_msg: Option<String>, failure_threshold: u32, ) -> Result<(), AppError>
Sourcepub async fn reset_provider_health(
&self,
provider_id: &str,
app_type: &str,
) -> Result<(), AppError>
pub async fn reset_provider_health( &self, provider_id: &str, app_type: &str, ) -> Result<(), AppError>
重置Provider健康状态
Sourcepub async fn clear_provider_health_for_app(
&self,
app_type: &str,
) -> Result<(), AppError>
pub async fn clear_provider_health_for_app( &self, app_type: &str, ) -> Result<(), AppError>
清空指定应用的健康状态(关闭单个代理时使用)
Sourcepub async fn clear_all_provider_health(&self) -> Result<(), AppError>
pub async fn clear_all_provider_health(&self) -> Result<(), AppError>
清空所有Provider健康状态(代理停止时调用)
Sourcepub async fn get_circuit_breaker_config(
&self,
) -> Result<CircuitBreakerConfig, AppError>
pub async fn get_circuit_breaker_config( &self, ) -> Result<CircuitBreakerConfig, AppError>
获取熔断器配置(兼容旧接口,从 claude 行读取)
熔断器配置已合并到 proxy_config 表,每 app 独立 此方法保留用于兼容旧代码,建议使用 get_proxy_config_for_app
Sourcepub async fn update_circuit_breaker_config(
&self,
config: &CircuitBreakerConfig,
) -> Result<(), AppError>
pub async fn update_circuit_breaker_config( &self, config: &CircuitBreakerConfig, ) -> Result<(), AppError>
更新熔断器配置(兼容旧接口,更新所有三行)
熔断器配置已合并到 proxy_config 表 此方法保留用于兼容旧代码,建议使用 update_proxy_config_for_app
Sourcepub async fn save_live_backup(
&self,
app_type: &str,
config_json: &str,
) -> Result<(), AppError>
pub async fn save_live_backup( &self, app_type: &str, config_json: &str, ) -> Result<(), AppError>
保存 Live 配置备份
Sourcepub async fn has_any_live_backup(&self) -> Result<bool, AppError>
pub async fn has_any_live_backup(&self) -> Result<bool, AppError>
检查是否存在任意 Live 配置备份
Sourcepub async fn get_live_backup(
&self,
app_type: &str,
) -> Result<Option<LiveBackup>, AppError>
pub async fn get_live_backup( &self, app_type: &str, ) -> Result<Option<LiveBackup>, AppError>
获取 Live 配置备份
Sourcepub async fn delete_all_live_backups(&self) -> Result<(), AppError>
pub async fn delete_all_live_backups(&self) -> Result<(), AppError>
删除所有 Live 配置备份
Sourcepub fn get_proxy_flags_sync(&self, app_type: &str) -> (bool, bool)
pub fn get_proxy_flags_sync(&self, app_type: &str) -> (bool, bool)
同步获取应用的 proxy 启用状态和自动故障转移状态
用于托盘菜单构建等同步场景 返回 (enabled, auto_failover_enabled)
Source§impl Database
impl Database
Sourcepub fn get_config_snippet(
&self,
app_type: &str,
) -> Result<Option<String>, AppError>
pub fn get_config_snippet( &self, app_type: &str, ) -> Result<Option<String>, AppError>
获取通用配置片段
Sourcepub fn set_config_snippet(
&self,
app_type: &str,
snippet: Option<String>,
) -> Result<(), AppError>
pub fn set_config_snippet( &self, app_type: &str, snippet: Option<String>, ) -> Result<(), AppError>
设置通用配置片段
Sourcepub fn get_global_proxy_url(&self) -> Result<Option<String>, AppError>
pub fn get_global_proxy_url(&self) -> Result<Option<String>, AppError>
获取全局出站代理 URL
返回 None 表示未配置或已清除代理(直连) 返回 Some(url) 表示已配置代理
Sourcepub fn set_global_proxy_url(&self, url: Option<&str>) -> Result<(), AppError>
pub fn set_global_proxy_url(&self, url: Option<&str>) -> Result<(), AppError>
设置全局出站代理 URL
- 传入非空字符串:启用代理
- 传入空字符串或 None:清除代理设置(直连)
Sourcepub fn get_proxy_takeover_enabled(
&self,
app_type: &str,
) -> Result<bool, AppError>
👎Deprecated since 3.9.0: 使用 get_proxy_config_for_app().enabled 替代
pub fn get_proxy_takeover_enabled( &self, app_type: &str, ) -> Result<bool, AppError>
使用 get_proxy_config_for_app().enabled 替代
获取指定应用的代理接管状态
已废弃: 请使用 proxy_config.enabled 字段替代
此方法仅用于数据库迁移时读取旧数据
Sourcepub fn set_proxy_takeover_enabled(
&self,
app_type: &str,
enabled: bool,
) -> Result<(), AppError>
👎Deprecated since 3.9.0: 使用 update_proxy_config_for_app() 修改 enabled 字段
pub fn set_proxy_takeover_enabled( &self, app_type: &str, enabled: bool, ) -> Result<(), AppError>
使用 update_proxy_config_for_app() 修改 enabled 字段
设置指定应用的代理接管状态
已废弃: 请使用 proxy_config.enabled 字段替代
Sourcepub fn has_any_proxy_takeover(&self) -> Result<bool, AppError>
👎Deprecated since 3.9.0: 使用 is_live_takeover_active() 替代
pub fn has_any_proxy_takeover(&self) -> Result<bool, AppError>
使用 is_live_takeover_active() 替代
检查是否有任一应用开启了代理接管
已废弃: 请使用 is_live_takeover_active() 替代
Sourcepub fn clear_all_proxy_takeover(&self) -> Result<(), AppError>
👎Deprecated since 3.9.0: 使用 update_proxy_config_for_app() 清除各应用的 enabled 字段
pub fn clear_all_proxy_takeover(&self) -> Result<(), AppError>
使用 update_proxy_config_for_app() 清除各应用的 enabled 字段
清除所有代理接管状态(将所有 proxy_takeover_* 设置为 false)
已废弃: settings 表不再用于存储代理状态
Sourcepub fn get_rectifier_config(&self) -> Result<RectifierConfig, AppError>
pub fn get_rectifier_config(&self) -> Result<RectifierConfig, AppError>
获取整流器配置
返回整流器配置,如果不存在则返回默认值(全部启用)
Sourcepub fn set_rectifier_config(
&self,
config: &RectifierConfig,
) -> Result<(), AppError>
pub fn set_rectifier_config( &self, config: &RectifierConfig, ) -> Result<(), AppError>
更新整流器配置
pub fn get_optimizer_config(&self) -> Result<OptimizerConfig, AppError>
pub fn set_optimizer_config( &self, config: &OptimizerConfig, ) -> Result<(), AppError>
Sourcepub fn get_log_config(&self) -> Result<LogConfig, AppError>
pub fn get_log_config(&self) -> Result<LogConfig, AppError>
获取日志配置
Sourcepub fn set_log_config(&self, config: &LogConfig) -> Result<(), AppError>
pub fn set_log_config(&self, config: &LogConfig) -> Result<(), AppError>
更新日志配置
Source§impl Database
impl Database
Sourcepub fn get_all_installed_skills(
&self,
) -> Result<IndexMap<String, InstalledSkill>, AppError>
pub fn get_all_installed_skills( &self, ) -> Result<IndexMap<String, InstalledSkill>, AppError>
获取所有已安装的 Skills
Sourcepub fn get_installed_skill(
&self,
id: &str,
) -> Result<Option<InstalledSkill>, AppError>
pub fn get_installed_skill( &self, id: &str, ) -> Result<Option<InstalledSkill>, AppError>
获取单个已安装的 Skill
Sourcepub fn save_skill(&self, skill: &InstalledSkill) -> Result<(), AppError>
pub fn save_skill(&self, skill: &InstalledSkill) -> Result<(), AppError>
保存 Skill(添加或更新)
Sourcepub fn clear_skills(&self) -> Result<(), AppError>
pub fn clear_skills(&self) -> Result<(), AppError>
清空所有 Skills(用于迁移)
Sourcepub fn update_skill_apps(
&self,
id: &str,
apps: &SkillApps,
) -> Result<bool, AppError>
pub fn update_skill_apps( &self, id: &str, apps: &SkillApps, ) -> Result<bool, AppError>
更新 Skill 的应用启用状态
Sourcepub fn get_skill_repos(&self) -> Result<Vec<SkillRepo>, AppError>
pub fn get_skill_repos(&self) -> Result<Vec<SkillRepo>, AppError>
获取所有 Skill 仓库
Sourcepub fn save_skill_repo(&self, repo: &SkillRepo) -> Result<(), AppError>
pub fn save_skill_repo(&self, repo: &SkillRepo) -> Result<(), AppError>
保存 Skill 仓库
Sourcepub fn init_default_skill_repos(&self) -> Result<usize, AppError>
pub fn init_default_skill_repos(&self) -> Result<usize, AppError>
初始化默认的 Skill 仓库(启动时调用,补充缺失的默认仓库)
Source§impl Database
impl Database
Sourcepub fn save_stream_check_log(
&self,
provider_id: &str,
provider_name: &str,
app_type: &str,
result: &StreamCheckResult,
) -> Result<i64, AppError>
pub fn save_stream_check_log( &self, provider_id: &str, provider_name: &str, app_type: &str, result: &StreamCheckResult, ) -> Result<i64, AppError>
保存流式检查日志
Sourcepub fn get_stream_check_config(&self) -> Result<StreamCheckConfig, AppError>
pub fn get_stream_check_config(&self) -> Result<StreamCheckConfig, AppError>
获取流式检查配置
Sourcepub fn save_stream_check_config(
&self,
config: &StreamCheckConfig,
) -> Result<(), AppError>
pub fn save_stream_check_config( &self, config: &StreamCheckConfig, ) -> Result<(), AppError>
保存流式检查配置
Source§impl Database
impl Database
Sourcepub fn migrate_from_json(&self, config: &MultiAppConfig) -> Result<(), AppError>
pub fn migrate_from_json(&self, config: &MultiAppConfig) -> Result<(), AppError>
从 MultiAppConfig 迁移数据到数据库
Sourcepub fn migrate_from_json_dry_run(
config: &MultiAppConfig,
) -> Result<(), AppError>
pub fn migrate_from_json_dry_run( config: &MultiAppConfig, ) -> Result<(), AppError>
运行迁移的 dry-run 模式(在内存数据库中验证,不写入磁盘)
用于部署前验证迁移逻辑是否正确。
Source§impl Database
impl Database
Sourcepub fn ensure_model_pricing_seeded(&self) -> Result<(), AppError>
pub fn ensure_model_pricing_seeded(&self) -> Result<(), AppError>
确保模型定价表具备默认数据
Auto Trait Implementations§
impl !Freeze for Database
impl RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl UnsafeUnpin for Database
impl UnwindSafe for Database
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more