Files
CC-Switch/src-tauri/src/database/dao/providers_seed.rs
T
Jason 5dbea70eb1 feat(providers): seed an official preset on startup for Claude/Codex/Gemini
New and existing users now see a built-in "Claude Official" / "OpenAI
Official" / "Google Official" entry in their provider list, so switching
back to the official endpoint is one click away instead of buried in the
README.

- New providers_seed.rs holds the three seeds (id, name, settings_config,
  icon) keyed by AppType, with a single is_official_seed_id() helper that
  scans OFFICIAL_SEEDS so the id list has one source of truth.
- Database::init_default_official_providers() runs once per database
  (gated by an official_providers_seeded setting flag), appends each seed
  to the end of the sort order, and never touches is_current.
- Startup also auto-imports the live config (settings.json / auth.json /
  .env) as a "default" provider before seeding, so users with an existing
  manual config don't lose it when they click the official preset.
- Database::has_non_official_seed_provider() replaces the get_all_providers
  call in import_default_config's gating check with an id-only scan,
  dropping the N+1 endpoint sub-queries from every startup.
2026-04-09 16:49:14 +08:00

67 lines
2.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 官方供应商种子数据
//!
//! 启动时调用 `Database::init_default_official_providers` 把这些条目
//! 写入 `providers` 表,让所有用户都能看到一个"一键切回官方"的入口。
//!
//! 字段与前端预设保持一致,参见:
//! - `src/config/claudeProviderPresets.ts`"Claude Official"
//! - `src/config/codexProviderPresets.ts`"OpenAI Official"
//! - `src/config/geminiProviderPresets.ts`"Google Official"
use crate::app_config::AppType;
/// 单条官方供应商种子定义。
pub(crate) struct OfficialProviderSeed {
pub id: &'static str,
pub app_type: AppType,
pub name: &'static str,
pub website_url: &'static str,
pub icon: &'static str,
pub icon_color: &'static str,
/// settings_config 的 JSON 字符串,每个 app 结构不同。
pub settings_config_json: &'static str,
}
/// Claude / Codex / Gemini 三个应用的官方预设。
///
/// id 固定,便于幂等检查;name 直接用英文原名(与前端预设一致),不做 i18n。
pub(crate) const OFFICIAL_SEEDS: &[OfficialProviderSeed] = &[
OfficialProviderSeed {
id: "claude-official",
app_type: AppType::Claude,
name: "Claude Official",
website_url: "https://www.anthropic.com/claude-code",
icon: "anthropic",
icon_color: "#D4915D",
// 空 env 让用户走 Claude CLI 默认认证流程
settings_config_json: r#"{"env":{}}"#,
},
OfficialProviderSeed {
id: "codex-official",
app_type: AppType::Codex,
name: "OpenAI Official",
website_url: "https://chatgpt.com/codex",
icon: "openai",
icon_color: "#00A67E",
// 空 auth + 空 config 让用户走 ChatGPT Plus/Pro OAuth
settings_config_json: r#"{"auth":{},"config":""}"#,
},
OfficialProviderSeed {
id: "gemini-official",
app_type: AppType::Gemini,
name: "Google Official",
website_url: "https://ai.google.dev/",
icon: "gemini",
icon_color: "#4285F4",
// 空 env + 空 config 让用户走 Google OAuth
settings_config_json: r#"{"env":{},"config":{}}"#,
},
];
/// 判断给定的 provider id 是否属于内置官方种子。
///
/// 单一事实源:直接扫描 `OFFICIAL_SEEDS`,避免在多处重复维护 id 列表。
pub(crate) fn is_official_seed_id(id: &str) -> bool {
OFFICIAL_SEEDS.iter().any(|seed| seed.id == id)
}