Files
CC-Switch/src-tauri/src/database/dao/providers_seed.rs
T
Jason f733def452 feat(grokbuild): add Grok Official provider with official-state import
Add a "Grok Official" preset and seed (grokbuild-official) whose empty
config represents the official login state: no custom [model.*] tables
are written, so Grok CLI falls back to its built-in xAI OAuth login and
cc-switch never touches those credentials.

Backend:
- Seed entry in OFFICIAL_SEEDS plus ensure_grokbuild_official_provider
  command for on-demand repair (the one-shot master seeding flag is
  already set for existing databases).
- Split validation into syntax-only (empty allowed) for live reads,
  writes and official snapshots, keeping the full custom-model shape
  check for non-official provider writes and imports. Backup/restore
  can now round-trip an official-state live file.
- Manual import (command layer only) recognizes an official-state live
  config and imports it as the official entry set as current, matching
  the Codex official-login import outcome. Startup auto-import keeps
  rejecting official-state live so a deleted official entry is never
  resurrected on launch: startup import only captures real user data
  as "default" and never manufactures official entries.
- Manual import also ensures the official entry before importing
  (claude-desktop precedent) and after a successful custom import, so
  first-time users end up with default + official like other apps.
- Proxy takeover guards skip or reject official-state live configs in
  all three takeover paths, consistent with the official-provider
  takeover ban.

Frontend:
- Grok Official preset entry in the GrokBuild form: official category
  hides connection fields and passes the raw config through untouched.
- Filter managed-OAuth presets out of the GrokBuild preset list; they
  were never wired for this app and produced keyless broken configs.

Tests cover seed presence, official round-trip, ensure-after-deletion,
and the four import scenarios including startup non-resurrection.
2026-07-21 16:39:34 +08:00

121 lines
4.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"
//! - `src/components/providers/forms/GrokBuildProviderForm.tsx`"Grok Official"
use crate::app_config::AppType;
pub(crate) const CLAUDE_DESKTOP_OFFICIAL_PROVIDER_ID: &str = "claude-desktop-official";
pub(crate) const CODEX_OFFICIAL_PROVIDER_ID: &str = "codex-official";
pub(crate) const GROKBUILD_OFFICIAL_PROVIDER_ID: &str = "grokbuild-official";
/// 单条官方供应商种子定义。
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 / Claude Desktop / 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: CLAUDE_DESKTOP_OFFICIAL_PROVIDER_ID,
app_type: AppType::ClaudeDesktop,
name: "Claude Desktop Official",
website_url: "https://claude.ai/download",
icon: "anthropic",
icon_color: "#D4915D",
// 空 env 只是占位;切换该 provider 时会恢复 Claude Desktop 1P 模式
settings_config_json: r#"{"env":{}}"#,
},
OfficialProviderSeed {
id: CODEX_OFFICIAL_PROVIDER_ID,
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":{}}"#,
},
OfficialProviderSeed {
id: GROKBUILD_OFFICIAL_PROVIDER_ID,
app_type: AppType::GrokBuild,
name: "Grok Official",
website_url: "https://x.ai/grok",
icon: "grok",
icon_color: "currentColor",
// 空 config = 不写自定义模型表,Grok CLI 回落到自带的 xAI OAuth 登录
settings_config_json: r#"{"config":""}"#,
},
];
/// 判断给定的 provider id 是否属于内置官方种子。
///
/// 单一事实源:直接扫描 `OFFICIAL_SEEDS`,避免在多处重复维护 id 列表。
pub(crate) fn is_official_seed_id(id: &str) -> bool {
OFFICIAL_SEEDS.iter().any(|seed| seed.id == id)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn official_seeds_include_claude_desktop() {
let seed = OFFICIAL_SEEDS
.iter()
.find(|seed| seed.id == CLAUDE_DESKTOP_OFFICIAL_PROVIDER_ID)
.expect("claude desktop official seed");
assert_eq!(seed.app_type, AppType::ClaudeDesktop);
assert!(is_official_seed_id(CLAUDE_DESKTOP_OFFICIAL_PROVIDER_ID));
}
#[test]
fn official_seeds_include_grokbuild() {
let seed = OFFICIAL_SEEDS
.iter()
.find(|seed| seed.id == GROKBUILD_OFFICIAL_PROVIDER_ID)
.expect("grok build official seed");
assert_eq!(seed.app_type, AppType::GrokBuild);
assert!(is_official_seed_id(GROKBUILD_OFFICIAL_PROVIDER_ID));
// 空 config = 官方登录态:切换时不注入自定义模型表
assert_eq!(seed.settings_config_json, r#"{"config":""}"#);
}
}