* 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>
286 lines
8.3 KiB
Rust
286 lines
8.3 KiB
Rust
use serde::{Deserialize, Serialize};
|
||
use serde_json::{Map, Value};
|
||
use std::env;
|
||
use std::fs;
|
||
use std::path::{Path, PathBuf};
|
||
|
||
use crate::config::{atomic_write, get_claude_mcp_path, get_default_claude_mcp_path};
|
||
use crate::error::AppError;
|
||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub struct McpStatus {
|
||
pub user_config_path: String,
|
||
pub user_config_exists: bool,
|
||
pub server_count: usize,
|
||
}
|
||
|
||
fn user_config_path() -> PathBuf {
|
||
ensure_mcp_override_migrated();
|
||
get_claude_mcp_path()
|
||
}
|
||
|
||
fn ensure_mcp_override_migrated() {
|
||
if crate::settings::get_claude_override_dir().is_none() {
|
||
return;
|
||
}
|
||
|
||
let new_path = get_claude_mcp_path();
|
||
if new_path.exists() {
|
||
return;
|
||
}
|
||
|
||
let legacy_path = get_default_claude_mcp_path();
|
||
if !legacy_path.exists() {
|
||
return;
|
||
}
|
||
|
||
if let Some(parent) = new_path.parent() {
|
||
if let Err(err) = fs::create_dir_all(parent) {
|
||
log::warn!("创建 MCP 目录失败: {err}");
|
||
return;
|
||
}
|
||
}
|
||
|
||
match fs::copy(&legacy_path, &new_path) {
|
||
Ok(_) => {
|
||
log::info!(
|
||
"已根据覆盖目录复制 MCP 配置: {} -> {}",
|
||
legacy_path.display(),
|
||
new_path.display()
|
||
);
|
||
}
|
||
Err(err) => {
|
||
log::warn!(
|
||
"复制 MCP 配置失败: {} -> {}: {}",
|
||
legacy_path.display(),
|
||
new_path.display(),
|
||
err
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
fn read_json_value(path: &Path) -> Result<Value, AppError> {
|
||
if !path.exists() {
|
||
return Ok(serde_json::json!({}));
|
||
}
|
||
let content = fs::read_to_string(path).map_err(|e| AppError::io(path, e))?;
|
||
let value: Value = serde_json::from_str(&content).map_err(|e| AppError::json(path, e))?;
|
||
Ok(value)
|
||
}
|
||
|
||
fn write_json_value(path: &Path, value: &Value) -> 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(value).map_err(|e| AppError::JsonSerialize { source: e })?;
|
||
atomic_write(path, json.as_bytes())
|
||
}
|
||
|
||
pub fn get_mcp_status() -> Result<McpStatus, AppError> {
|
||
let path = user_config_path();
|
||
let (exists, count) = if path.exists() {
|
||
let v = read_json_value(&path)?;
|
||
let servers = v.get("mcpServers").and_then(|x| x.as_object());
|
||
(true, servers.map(|m| m.len()).unwrap_or(0))
|
||
} else {
|
||
(false, 0)
|
||
};
|
||
|
||
Ok(McpStatus {
|
||
user_config_path: path.to_string_lossy().to_string(),
|
||
user_config_exists: exists,
|
||
server_count: count,
|
||
})
|
||
}
|
||
|
||
pub fn read_mcp_json() -> Result<Option<String>, AppError> {
|
||
let path = user_config_path();
|
||
if !path.exists() {
|
||
return Ok(None);
|
||
}
|
||
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
||
Ok(Some(content))
|
||
}
|
||
|
||
pub fn upsert_mcp_server(id: &str, spec: Value) -> Result<bool, AppError> {
|
||
if id.trim().is_empty() {
|
||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||
}
|
||
// 基础字段校验(尽量宽松)
|
||
if !spec.is_object() {
|
||
return Err(AppError::McpValidation(
|
||
"MCP 服务器定义必须为 JSON 对象".into(),
|
||
));
|
||
}
|
||
let t_opt = spec.get("type").and_then(|x| x.as_str());
|
||
let is_stdio = t_opt.map(|t| t == "stdio").unwrap_or(true); // 兼容缺省(按 stdio 处理)
|
||
let is_http = t_opt.map(|t| t == "http").unwrap_or(false);
|
||
if !(is_stdio || is_http) {
|
||
return Err(AppError::McpValidation(
|
||
"MCP 服务器 type 必须是 'stdio' 或 'http'(或省略表示 stdio)".into(),
|
||
));
|
||
}
|
||
|
||
// stdio 类型必须有 command
|
||
if is_stdio {
|
||
let cmd = spec.get("command").and_then(|x| x.as_str()).unwrap_or("");
|
||
if cmd.is_empty() {
|
||
return Err(AppError::McpValidation(
|
||
"stdio 类型的 MCP 服务器缺少 command 字段".into(),
|
||
));
|
||
}
|
||
}
|
||
|
||
// http 类型必须有 url
|
||
if is_http {
|
||
let url = spec.get("url").and_then(|x| x.as_str()).unwrap_or("");
|
||
if url.is_empty() {
|
||
return Err(AppError::McpValidation(
|
||
"http 类型的 MCP 服务器缺少 url 字段".into(),
|
||
));
|
||
}
|
||
}
|
||
|
||
let path = user_config_path();
|
||
let mut root = if path.exists() {
|
||
read_json_value(&path)?
|
||
} else {
|
||
serde_json::json!({})
|
||
};
|
||
|
||
// 确保 mcpServers 对象存在
|
||
{
|
||
let obj = root
|
||
.as_object_mut()
|
||
.ok_or_else(|| AppError::Config("mcp.json 根必须是对象".into()))?;
|
||
if !obj.contains_key("mcpServers") {
|
||
obj.insert("mcpServers".into(), serde_json::json!({}));
|
||
}
|
||
}
|
||
|
||
let before = root.clone();
|
||
if let Some(servers) = root.get_mut("mcpServers").and_then(|v| v.as_object_mut()) {
|
||
servers.insert(id.to_string(), spec);
|
||
}
|
||
|
||
if before == root && path.exists() {
|
||
return Ok(false);
|
||
}
|
||
|
||
write_json_value(&path, &root)?;
|
||
Ok(true)
|
||
}
|
||
|
||
pub fn delete_mcp_server(id: &str) -> Result<bool, AppError> {
|
||
if id.trim().is_empty() {
|
||
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
||
}
|
||
let path = user_config_path();
|
||
if !path.exists() {
|
||
return Ok(false);
|
||
}
|
||
let mut root = read_json_value(&path)?;
|
||
let Some(servers) = root.get_mut("mcpServers").and_then(|v| v.as_object_mut()) else {
|
||
return Ok(false);
|
||
};
|
||
let existed = servers.remove(id).is_some();
|
||
if !existed {
|
||
return Ok(false);
|
||
}
|
||
write_json_value(&path, &root)?;
|
||
Ok(true)
|
||
}
|
||
|
||
pub fn validate_command_in_path(cmd: &str) -> Result<bool, AppError> {
|
||
if cmd.trim().is_empty() {
|
||
return Ok(false);
|
||
}
|
||
// 如果包含路径分隔符,直接判断是否存在可执行文件
|
||
if cmd.contains('/') || cmd.contains('\\') {
|
||
return Ok(Path::new(cmd).exists());
|
||
}
|
||
|
||
let path_var = env::var_os("PATH").unwrap_or_default();
|
||
let paths = env::split_paths(&path_var);
|
||
|
||
#[cfg(windows)]
|
||
let exts: Vec<String> = env::var("PATHEXT")
|
||
.unwrap_or(".COM;.EXE;.BAT;.CMD".into())
|
||
.split(';')
|
||
.map(|s| s.trim().to_uppercase())
|
||
.collect();
|
||
|
||
for p in paths {
|
||
let candidate = p.join(cmd);
|
||
if candidate.is_file() {
|
||
return Ok(true);
|
||
}
|
||
#[cfg(windows)]
|
||
{
|
||
for ext in &exts {
|
||
let cand = p.join(format!("{}{}", cmd, ext));
|
||
if cand.is_file() {
|
||
return Ok(true);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
Ok(false)
|
||
}
|
||
|
||
/// 将给定的启用 MCP 服务器映射写入到用户级 ~/.claude.json 的 mcpServers 字段
|
||
/// 仅覆盖 mcpServers,其他字段保持不变
|
||
pub fn set_mcp_servers_map(
|
||
servers: &std::collections::HashMap<String, Value>,
|
||
) -> Result<(), AppError> {
|
||
let path = user_config_path();
|
||
let mut root = if path.exists() {
|
||
read_json_value(&path)?
|
||
} else {
|
||
serde_json::json!({})
|
||
};
|
||
|
||
// 构建 mcpServers 对象:移除 UI 辅助字段(enabled/source),仅保留实际 MCP 规范
|
||
let mut out: Map<String, Value> = Map::new();
|
||
for (id, spec) in servers.iter() {
|
||
let mut obj = if let Some(map) = spec.as_object() {
|
||
map.clone()
|
||
} else {
|
||
return Err(AppError::McpValidation(format!(
|
||
"MCP 服务器 '{id}' 不是对象"
|
||
)));
|
||
};
|
||
|
||
if let Some(server_val) = obj.remove("server") {
|
||
let server_obj = server_val.as_object().cloned().ok_or_else(|| {
|
||
AppError::McpValidation(format!("MCP 服务器 '{id}' server 字段不是对象"))
|
||
})?;
|
||
obj = server_obj;
|
||
}
|
||
|
||
obj.remove("enabled");
|
||
obj.remove("source");
|
||
obj.remove("id");
|
||
obj.remove("name");
|
||
obj.remove("description");
|
||
obj.remove("tags");
|
||
obj.remove("homepage");
|
||
obj.remove("docs");
|
||
|
||
out.insert(id.clone(), Value::Object(obj));
|
||
}
|
||
|
||
{
|
||
let obj = root
|
||
.as_object_mut()
|
||
.ok_or_else(|| AppError::Config("~/.claude.json 根必须是对象".into()))?;
|
||
obj.insert("mcpServers".into(), Value::Object(out));
|
||
}
|
||
|
||
write_json_value(&path, &root)?;
|
||
Ok(())
|
||
}
|