mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-27 16:26:16 +08:00
feat(providers): auto-import OpenCode/OpenClaw live providers on startup
Drops the friction of clicking the manual "Import current config" button for OpenCode and OpenClaw — they now match the auto-import behavior the previous commit added for Claude/Codex/Gemini. - New "1.6." startup block in lib.rs runs both import_opencode_providers_from_live and import_openclaw_providers_from_live on every launch. The functions are id-keyed and idempotent, so re-running just picks up new providers added externally to the live JSON files. - Both functions now use a new Database::get_provider_ids() helper (HashSet<String> from a single SELECT id-only query) instead of get_all_providers(), avoiding the N+1 endpoint sub-queries that would otherwise hit the startup hot path on every launch.
This commit is contained in:
@@ -3,7 +3,7 @@ use crate::error::AppError;
|
||||
use crate::provider::{Provider, ProviderMeta};
|
||||
use indexmap::IndexMap;
|
||||
use rusqlite::params;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
type OmoProviderRow = (
|
||||
String,
|
||||
@@ -502,6 +502,25 @@ impl Database {
|
||||
}))
|
||||
}
|
||||
|
||||
/// 仅获取指定 app 下所有 provider 的 id 集合。
|
||||
///
|
||||
/// 比 `get_all_providers` 轻量得多:只读 id 列、无 endpoint 子查询。
|
||||
/// 用于只需要做存在性检查的场景(如 additive 模式的 live 同步去重)。
|
||||
pub fn get_provider_ids(&self, app_type: &str) -> Result<HashSet<String>, AppError> {
|
||||
let conn = lock_conn!(self.conn);
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT id FROM providers WHERE app_type = ?1")
|
||||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||||
let rows = stmt
|
||||
.query_map(params![app_type], |row| row.get::<_, String>(0))
|
||||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||||
let mut ids = HashSet::new();
|
||||
for row in rows {
|
||||
ids.insert(row.map_err(|e| AppError::Database(e.to_string()))?);
|
||||
}
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
/// 判断指定 app 下是否存在非官方种子的供应商。
|
||||
///
|
||||
/// 比 `get_all_providers` 轻量得多:只读 id 列、无 endpoint 子查询、首条命中即返回。
|
||||
|
||||
Reference in New Issue
Block a user