* feat(gemini): add Gemini provider integration - Add gemini_config.rs module for .env file parsing - Extend AppType enum to support Gemini - Implement GeminiConfigEditor and GeminiFormFields components - Add GeminiIcon with standardized 1024x1024 viewBox - Add Gemini provider presets configuration - Update i18n translations for Gemini support - Extend ProviderService and McpService for Gemini * fix(gemini): resolve TypeScript errors, add i18n support, and fix MCP logic **Critical Fixes:** - Fix TS2741 errors in tests/msw/state.ts by adding missing Gemini type definitions - Fix ProviderCard.extractApiUrl to support GOOGLE_GEMINI_BASE_URL display - Add missing apps.gemini i18n keys (zh/en) for proper app name display - Fix MCP service Gemini cross-app duplication logic to prevent self-copy **Technical Details:** - tests/msw/state.ts: Add gemini default providers, current ID, and MCP config - ProviderCard.tsx: Check both ANTHROPIC_BASE_URL and GOOGLE_GEMINI_BASE_URL - services/mcp.rs: Skip Gemini in sync_other_side logic with unreachable!() guards - Run pnpm format to auto-fix code style issues **Verification:** - ✅ pnpm typecheck passes - ✅ pnpm format completed * feat(gemini): enhance authentication and config parsing - Add strict and lenient .env parsing modes - Implement PackyCode partner authentication detection - Support Google OAuth official authentication - Auto-configure security.auth.selectedType for PackyCode - Add comprehensive test coverage for all auth types - Update i18n for OAuth hints and Gemini config --------- Co-authored-by: Jason <farion1231@gmail.com>
258 lines
7.9 KiB
Rust
258 lines
7.9 KiB
Rust
use serde::{Deserialize, Serialize};
|
||
use std::fs;
|
||
use std::io::Write;
|
||
use std::path::{Path, PathBuf};
|
||
|
||
use crate::error::AppError;
|
||
|
||
/// 获取 Claude Code 配置目录路径
|
||
pub fn get_claude_config_dir() -> PathBuf {
|
||
if let Some(custom) = crate::settings::get_claude_override_dir() {
|
||
return custom;
|
||
}
|
||
|
||
dirs::home_dir()
|
||
.expect("无法获取用户主目录")
|
||
.join(".claude")
|
||
}
|
||
|
||
/// 默认 Claude MCP 配置文件路径 (~/.claude.json)
|
||
pub fn get_default_claude_mcp_path() -> PathBuf {
|
||
dirs::home_dir()
|
||
.expect("无法获取用户主目录")
|
||
.join(".claude.json")
|
||
}
|
||
|
||
fn derive_mcp_path_from_override(dir: &Path) -> Option<PathBuf> {
|
||
let file_name = dir
|
||
.file_name()
|
||
.map(|name| name.to_string_lossy().to_string())?
|
||
.trim()
|
||
.to_string();
|
||
if file_name.is_empty() {
|
||
return None;
|
||
}
|
||
let parent = dir.parent().unwrap_or_else(|| Path::new(""));
|
||
Some(parent.join(format!("{file_name}.json")))
|
||
}
|
||
|
||
/// 获取 Claude MCP 配置文件路径,若设置了目录覆盖则与覆盖目录同级
|
||
pub fn get_claude_mcp_path() -> PathBuf {
|
||
if let Some(custom_dir) = crate::settings::get_claude_override_dir() {
|
||
if let Some(path) = derive_mcp_path_from_override(&custom_dir) {
|
||
return path;
|
||
}
|
||
}
|
||
get_default_claude_mcp_path()
|
||
}
|
||
|
||
/// 获取 Claude Code 主配置文件路径
|
||
pub fn get_claude_settings_path() -> PathBuf {
|
||
let dir = get_claude_config_dir();
|
||
let settings = dir.join("settings.json");
|
||
if settings.exists() {
|
||
return settings;
|
||
}
|
||
// 兼容旧版命名:若存在旧文件则继续使用
|
||
let legacy = dir.join("claude.json");
|
||
if legacy.exists() {
|
||
return legacy;
|
||
}
|
||
// 默认新建:回落到标准文件名 settings.json(不再生成 claude.json)
|
||
settings
|
||
}
|
||
|
||
/// 获取应用配置目录路径 (~/.cc-switch)
|
||
pub fn get_app_config_dir() -> PathBuf {
|
||
if let Some(custom) = crate::app_store::get_app_config_dir_override() {
|
||
return custom;
|
||
}
|
||
|
||
dirs::home_dir()
|
||
.expect("无法获取用户主目录")
|
||
.join(".cc-switch")
|
||
}
|
||
|
||
/// 获取应用配置文件路径
|
||
pub fn get_app_config_path() -> PathBuf {
|
||
get_app_config_dir().join("config.json")
|
||
}
|
||
|
||
/// 清理供应商名称,确保文件名安全
|
||
pub fn sanitize_provider_name(name: &str) -> String {
|
||
name.chars()
|
||
.map(|c| match c {
|
||
'<' | '>' | ':' | '"' | '/' | '\\' | '|' | '?' | '*' => '-',
|
||
_ => c,
|
||
})
|
||
.collect::<String>()
|
||
.to_lowercase()
|
||
}
|
||
|
||
/// 获取供应商配置文件路径
|
||
pub fn get_provider_config_path(provider_id: &str, provider_name: Option<&str>) -> PathBuf {
|
||
let base_name = provider_name
|
||
.map(sanitize_provider_name)
|
||
.unwrap_or_else(|| sanitize_provider_name(provider_id));
|
||
|
||
get_claude_config_dir().join(format!("settings-{base_name}.json"))
|
||
}
|
||
|
||
/// 读取 JSON 配置文件
|
||
pub fn read_json_file<T: for<'a> Deserialize<'a>>(path: &Path) -> Result<T, AppError> {
|
||
if !path.exists() {
|
||
return Err(AppError::Config(format!("文件不存在: {}", path.display())));
|
||
}
|
||
|
||
let content = fs::read_to_string(path).map_err(|e| AppError::io(path, e))?;
|
||
|
||
serde_json::from_str(&content).map_err(|e| AppError::json(path, e))
|
||
}
|
||
|
||
/// 写入 JSON 配置文件
|
||
pub fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<(), AppError> {
|
||
// 确保目录存在
|
||
if let Some(parent) = path.parent() {
|
||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||
}
|
||
|
||
let json =
|
||
serde_json::to_string_pretty(data).map_err(|e| AppError::JsonSerialize { source: e })?;
|
||
|
||
atomic_write(path, json.as_bytes())
|
||
}
|
||
|
||
/// 原子写入文本文件(用于 TOML/纯文本)
|
||
pub fn write_text_file(path: &Path, data: &str) -> Result<(), AppError> {
|
||
if let Some(parent) = path.parent() {
|
||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||
}
|
||
atomic_write(path, data.as_bytes())
|
||
}
|
||
|
||
/// 原子写入:写入临时文件后 rename 替换,避免半写状态
|
||
pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), AppError> {
|
||
if let Some(parent) = path.parent() {
|
||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||
}
|
||
|
||
let parent = path
|
||
.parent()
|
||
.ok_or_else(|| AppError::Config("无效的路径".to_string()))?;
|
||
let mut tmp = parent.to_path_buf();
|
||
let file_name = path
|
||
.file_name()
|
||
.ok_or_else(|| AppError::Config("无效的文件名".to_string()))?
|
||
.to_string_lossy()
|
||
.to_string();
|
||
let ts = std::time::SystemTime::now()
|
||
.duration_since(std::time::UNIX_EPOCH)
|
||
.unwrap_or_default()
|
||
.as_nanos();
|
||
tmp.push(format!("{file_name}.tmp.{ts}"));
|
||
|
||
{
|
||
let mut f = fs::File::create(&tmp).map_err(|e| AppError::io(&tmp, e))?;
|
||
f.write_all(data).map_err(|e| AppError::io(&tmp, e))?;
|
||
f.flush().map_err(|e| AppError::io(&tmp, e))?;
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
{
|
||
use std::os::unix::fs::PermissionsExt;
|
||
if let Ok(meta) = fs::metadata(path) {
|
||
let perm = meta.permissions().mode();
|
||
let _ = fs::set_permissions(&tmp, fs::Permissions::from_mode(perm));
|
||
}
|
||
}
|
||
|
||
#[cfg(windows)]
|
||
{
|
||
// Windows 上 rename 目标存在会失败,先移除再重命名(尽量接近原子性)
|
||
if path.exists() {
|
||
let _ = fs::remove_file(path);
|
||
}
|
||
fs::rename(&tmp, path).map_err(|e| AppError::IoContext {
|
||
context: format!("原子替换失败: {} -> {}", tmp.display(), path.display()),
|
||
source: e,
|
||
})?;
|
||
}
|
||
|
||
#[cfg(not(windows))]
|
||
{
|
||
fs::rename(&tmp, path).map_err(|e| AppError::IoContext {
|
||
context: format!("原子替换失败: {} -> {}", tmp.display(), path.display()),
|
||
source: e,
|
||
})?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn derive_mcp_path_from_override_preserves_folder_name() {
|
||
let override_dir = PathBuf::from("/tmp/profile/.claude");
|
||
let derived = derive_mcp_path_from_override(&override_dir)
|
||
.expect("should derive path for nested dir");
|
||
assert_eq!(derived, PathBuf::from("/tmp/profile/.claude.json"));
|
||
}
|
||
|
||
#[test]
|
||
fn derive_mcp_path_from_override_handles_non_hidden_folder() {
|
||
let override_dir = PathBuf::from("/data/claude-config");
|
||
let derived = derive_mcp_path_from_override(&override_dir)
|
||
.expect("should derive path for standard dir");
|
||
assert_eq!(derived, PathBuf::from("/data/claude-config.json"));
|
||
}
|
||
|
||
#[test]
|
||
fn derive_mcp_path_from_override_supports_relative_rootless_dir() {
|
||
let override_dir = PathBuf::from("claude");
|
||
let derived = derive_mcp_path_from_override(&override_dir)
|
||
.expect("should derive path for single segment");
|
||
assert_eq!(derived, PathBuf::from("claude.json"));
|
||
}
|
||
|
||
#[test]
|
||
fn derive_mcp_path_from_root_like_dir_returns_none() {
|
||
let override_dir = PathBuf::from("/");
|
||
assert!(derive_mcp_path_from_override(&override_dir).is_none());
|
||
}
|
||
}
|
||
|
||
/// 复制文件
|
||
pub fn copy_file(from: &Path, to: &Path) -> Result<(), AppError> {
|
||
fs::copy(from, to).map_err(|e| AppError::IoContext {
|
||
context: format!("复制文件失败 ({} -> {})", from.display(), to.display()),
|
||
source: e,
|
||
})?;
|
||
Ok(())
|
||
}
|
||
|
||
/// 删除文件
|
||
pub fn delete_file(path: &Path) -> Result<(), AppError> {
|
||
if path.exists() {
|
||
fs::remove_file(path).map_err(|e| AppError::io(path, e))?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// 检查 Claude Code 配置状态
|
||
#[derive(Serialize, Deserialize)]
|
||
pub struct ConfigStatus {
|
||
pub exists: bool,
|
||
pub path: String,
|
||
}
|
||
|
||
/// 获取 Claude Code 配置状态
|
||
pub fn get_claude_config_status() -> ConfigStatus {
|
||
let path = get_claude_settings_path();
|
||
ConfigStatus {
|
||
exists: path.exists(),
|
||
path: path.to_string_lossy().to_string(),
|
||
}
|
||
}
|