* 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>
132 lines
3.7 KiB
Rust
132 lines
3.7 KiB
Rust
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
use crate::error::AppError;
|
|
|
|
const CLAUDE_DIR: &str = ".claude";
|
|
const CLAUDE_CONFIG_FILE: &str = "config.json";
|
|
|
|
fn claude_dir() -> Result<PathBuf, AppError> {
|
|
// 优先使用设置中的覆盖目录
|
|
if let Some(dir) = crate::settings::get_claude_override_dir() {
|
|
return Ok(dir);
|
|
}
|
|
let home = dirs::home_dir().ok_or_else(|| AppError::Config("无法获取用户主目录".into()))?;
|
|
Ok(home.join(CLAUDE_DIR))
|
|
}
|
|
|
|
pub fn claude_config_path() -> Result<PathBuf, AppError> {
|
|
Ok(claude_dir()?.join(CLAUDE_CONFIG_FILE))
|
|
}
|
|
|
|
pub fn ensure_claude_dir_exists() -> Result<PathBuf, AppError> {
|
|
let dir = claude_dir()?;
|
|
if !dir.exists() {
|
|
fs::create_dir_all(&dir).map_err(|e| AppError::io(&dir, e))?;
|
|
}
|
|
Ok(dir)
|
|
}
|
|
|
|
pub fn read_claude_config() -> Result<Option<String>, AppError> {
|
|
let path = claude_config_path()?;
|
|
if path.exists() {
|
|
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
|
Ok(Some(content))
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
fn is_managed_config(content: &str) -> bool {
|
|
match serde_json::from_str::<serde_json::Value>(content) {
|
|
Ok(value) => value
|
|
.get("primaryApiKey")
|
|
.and_then(|v| v.as_str())
|
|
.map(|val| val == "any")
|
|
.unwrap_or(false),
|
|
Err(_) => false,
|
|
}
|
|
}
|
|
|
|
pub fn write_claude_config() -> Result<bool, AppError> {
|
|
// 增量写入:仅设置 primaryApiKey = "any",保留其它字段
|
|
let path = claude_config_path()?;
|
|
ensure_claude_dir_exists()?;
|
|
|
|
// 尝试读取并解析为对象
|
|
let mut obj = match read_claude_config()? {
|
|
Some(existing) => match serde_json::from_str::<serde_json::Value>(&existing) {
|
|
Ok(serde_json::Value::Object(map)) => serde_json::Value::Object(map),
|
|
_ => serde_json::json!({}),
|
|
},
|
|
None => serde_json::json!({}),
|
|
};
|
|
|
|
let mut changed = false;
|
|
if let Some(map) = obj.as_object_mut() {
|
|
let cur = map
|
|
.get("primaryApiKey")
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or("");
|
|
if cur != "any" {
|
|
map.insert(
|
|
"primaryApiKey".to_string(),
|
|
serde_json::Value::String("any".to_string()),
|
|
);
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
if changed || !path.exists() {
|
|
let serialized = serde_json::to_string_pretty(&obj)
|
|
.map_err(|e| AppError::JsonSerialize { source: e })?;
|
|
fs::write(&path, format!("{serialized}\n")).map_err(|e| AppError::io(&path, e))?;
|
|
Ok(true)
|
|
} else {
|
|
Ok(false)
|
|
}
|
|
}
|
|
|
|
pub fn clear_claude_config() -> Result<bool, AppError> {
|
|
let path = claude_config_path()?;
|
|
if !path.exists() {
|
|
return Ok(false);
|
|
}
|
|
|
|
let content = match read_claude_config()? {
|
|
Some(content) => content,
|
|
None => return Ok(false),
|
|
};
|
|
|
|
let mut value = match serde_json::from_str::<serde_json::Value>(&content) {
|
|
Ok(value) => value,
|
|
Err(_) => return Ok(false),
|
|
};
|
|
|
|
let obj = match value.as_object_mut() {
|
|
Some(obj) => obj,
|
|
None => return Ok(false),
|
|
};
|
|
|
|
if obj.remove("primaryApiKey").is_none() {
|
|
return Ok(false);
|
|
}
|
|
|
|
let serialized =
|
|
serde_json::to_string_pretty(&value).map_err(|e| AppError::JsonSerialize { source: e })?;
|
|
fs::write(&path, format!("{serialized}\n")).map_err(|e| AppError::io(&path, e))?;
|
|
Ok(true)
|
|
}
|
|
|
|
pub fn claude_config_status() -> Result<(bool, PathBuf), AppError> {
|
|
let path = claude_config_path()?;
|
|
Ok((path.exists(), path))
|
|
}
|
|
|
|
pub fn is_claude_config_applied() -> Result<bool, AppError> {
|
|
match read_claude_config()? {
|
|
Some(content) => Ok(is_managed_config(&content)),
|
|
None => Ok(false),
|
|
}
|
|
}
|