From f74d641f869f17c7e8474fabaad9251cdb061559 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 1 Oct 2025 21:23:55 +0800 Subject: [PATCH] Add Claude plugin sync alongside VS Code integration --- src-tauri/src/claude_plugin.rs | 103 ++++++++++++++++++++++++++++++++ src-tauri/src/commands.rs | 35 +++++++++++ src-tauri/src/lib.rs | 5 ++ src/App.tsx | 34 +++++++++++ src/components/ProviderList.tsx | 86 ++++++++++++++++++++++++-- src/i18n/locales/en.json | 9 ++- src/i18n/locales/zh.json | 9 ++- src/lib/tauri-api.ts | 40 +++++++++++++ src/vite-env.d.ts | 7 +++ 9 files changed, 319 insertions(+), 9 deletions(-) create mode 100644 src-tauri/src/claude_plugin.rs diff --git a/src-tauri/src/claude_plugin.rs b/src-tauri/src/claude_plugin.rs new file mode 100644 index 0000000..21483db --- /dev/null +++ b/src-tauri/src/claude_plugin.rs @@ -0,0 +1,103 @@ +use std::fs; +use std::path::PathBuf; + +const CLAUDE_DIR: &str = ".claude"; +const CLAUDE_CONFIG_FILE: &str = "config.json"; +const CLAUDE_CONFIG_PAYLOAD: &str = "{\n \"primaryApiKey\": \"any\"\n}\n"; + +fn claude_dir() -> Result { + let home = dirs::home_dir().ok_or_else(|| "无法获取用户主目录".to_string())?; + Ok(home.join(CLAUDE_DIR)) +} + +pub fn claude_config_path() -> Result { + Ok(claude_dir()?.join(CLAUDE_CONFIG_FILE)) +} + +pub fn ensure_claude_dir_exists() -> Result { + let dir = claude_dir()?; + if !dir.exists() { + fs::create_dir_all(&dir).map_err(|e| format!("创建 Claude 配置目录失败: {}", e))?; + } + Ok(dir) +} + +pub fn read_claude_config() -> Result, String> { + let path = claude_config_path()?; + if path.exists() { + let content = + fs::read_to_string(&path).map_err(|e| format!("读取 Claude 配置失败: {}", e))?; + Ok(Some(content)) + } else { + Ok(None) + } +} + +fn is_managed_config(content: &str) -> bool { + match serde_json::from_str::(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 { + let path = claude_config_path()?; + ensure_claude_dir_exists()?; + let need_write = match read_claude_config()? { + Some(existing) => existing != CLAUDE_CONFIG_PAYLOAD, + None => true, + }; + if need_write { + fs::write(&path, CLAUDE_CONFIG_PAYLOAD) + .map_err(|e| format!("写入 Claude 配置失败: {}", e))?; + } + Ok(need_write) +} + +pub fn clear_claude_config() -> Result { + 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::(&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| format!("序列化 Claude 配置失败: {}", e))?; + fs::write(&path, format!("{}\n", serialized)) + .map_err(|e| format!("写入 Claude 配置失败: {}", e))?; + Ok(true) +} + +pub fn claude_config_status() -> Result<(bool, PathBuf), String> { + let path = claude_config_path()?; + Ok((path.exists(), path)) +} + +pub fn is_claude_config_applied() -> Result { + match read_claude_config()? { + Some(content) => Ok(is_managed_config(&content)), + None => Ok(false), + } +} diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 631fb6f..c8098e6 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -6,6 +6,7 @@ use tauri_plugin_dialog::DialogExt; use tauri_plugin_opener::OpenerExt; use crate::app_config::AppType; +use crate::claude_plugin; use crate::codex_config; use crate::config::{self, get_claude_settings_path, ConfigStatus}; use crate::provider::Provider; @@ -730,3 +731,37 @@ pub async fn write_vscode_settings(content: String) -> Result { Err("未找到 VS Code 用户设置文件".to_string()) } } + +/// Claude 插件:获取 ~/.claude/config.json 状态 +#[tauri::command] +pub async fn get_claude_plugin_status() -> Result { + match claude_plugin::claude_config_status() { + Ok((exists, path)) => Ok(ConfigStatus { + exists, + path: path.to_string_lossy().to_string(), + }), + Err(err) => Err(err), + } +} + +/// Claude 插件:读取配置内容(若不存在返回 Ok(None)) +#[tauri::command] +pub async fn read_claude_plugin_config() -> Result, String> { + claude_plugin::read_claude_config() +} + +/// Claude 插件:写入/清除固定配置 +#[tauri::command] +pub async fn apply_claude_plugin_config(official: bool) -> Result { + if official { + claude_plugin::clear_claude_config() + } else { + claude_plugin::write_claude_config() + } +} + +/// Claude 插件:检测是否已写入目标配置 +#[tauri::command] +pub async fn is_claude_plugin_applied() -> Result { + claude_plugin::is_claude_config_applied() +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index eae18aa..f7ecb86 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,4 +1,5 @@ mod app_config; +mod claude_plugin; mod codex_config; mod commands; mod config; @@ -418,6 +419,10 @@ pub fn run() { commands::get_vscode_settings_status, commands::read_vscode_settings, commands::write_vscode_settings, + commands::get_claude_plugin_status, + commands::read_claude_plugin_config, + commands::apply_claude_plugin_config, + commands::is_claude_plugin_applied, update_tray_menu, ]); diff --git a/src/App.tsx b/src/App.tsx index b405166..c7dfea2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -102,6 +102,10 @@ function App() { if (data.appType === "codex" && isAutoSyncEnabled) { await syncCodexToVSCode(data.providerId, true); } + + if (data.appType === "claude") { + await syncClaudePlugin(data.providerId, true); + } }); } catch (error) { console.error(t("console.setupListenerFailed"), error); @@ -240,6 +244,32 @@ function App() { } }; + // 同步 Claude 插件配置(写入/移除固定 JSON) + const syncClaudePlugin = async (providerId: string, silent = false) => { + try { + const provider = providers[providerId]; + if (!provider) return; + const isOfficial = provider.category === "official"; + await window.api.applyClaudePluginConfig({ official: isOfficial }); + if (!silent) { + showNotification( + isOfficial + ? t("notifications.removedFromClaudePlugin") + : t("notifications.appliedToClaudePlugin"), + "success", + 2000, + ); + } + } catch (error: any) { + console.error("同步 Claude 插件失败:", error); + if (!silent) { + const message = + error?.message || t("notifications.syncClaudePluginFailed"); + showNotification(message, "error", 5000); + } + } + }; + const handleSwitchProvider = async (id: string) => { const success = await window.api.switchProvider(id, activeApp); if (success) { @@ -258,6 +288,10 @@ function App() { if (activeApp === "codex" && isAutoSyncEnabled) { await syncCodexToVSCode(id, true); // silent模式,不显示通知 } + + if (activeApp === "claude") { + await syncClaudePlugin(id, true); + } } else { showNotification(t("notifications.switchFailed"), "error"); } diff --git a/src/components/ProviderList.tsx b/src/components/ProviderList.tsx index dfe94ae..3eebdec 100644 --- a/src/components/ProviderList.tsx +++ b/src/components/ProviderList.tsx @@ -70,6 +70,7 @@ const ProviderList: React.FC = ({ // VS Code 按钮:仅在 Codex + 当前供应商显示;按钮文案根据是否"已应用"变化 const [vscodeAppliedFor, setVscodeAppliedFor] = useState(null); const { enableAutoSync, disableAutoSync } = useVSCodeAutoSync(); + const [claudeApplied, setClaudeApplied] = useState(false); // 当当前供应商或 appType 变化时,尝试读取 VS Code settings 并检测状态 useEffect(() => { @@ -104,6 +105,24 @@ const ProviderList: React.FC = ({ check(); }, [appType, currentProviderId, providers]); + // 检查 Claude 插件配置是否已应用 + useEffect(() => { + const checkClaude = async () => { + if (appType !== "claude" || !currentProviderId) { + setClaudeApplied(false); + return; + } + try { + const applied = await window.api.isClaudePluginApplied(); + setClaudeApplied(applied); + } catch (error) { + console.error("检测 Claude 插件配置失败:", error); + setClaudeApplied(false); + } + }; + checkClaude(); + }, [appType, currentProviderId, providers]); + const handleApplyToVSCode = async (provider: Provider) => { try { const status = await window.api.getVSCodeSettingsStatus(); @@ -181,6 +200,36 @@ const ProviderList: React.FC = ({ } }; + const handleApplyToClaudePlugin = async () => { + try { + await window.api.applyClaudePluginConfig({ official: false }); + onNotify?.(t("notifications.appliedToClaudePlugin"), "success", 3000); + setClaudeApplied(true); + } catch (error: any) { + console.error(error); + const msg = + error && error.message + ? error.message + : t("notifications.syncClaudePluginFailed"); + onNotify?.(msg, "error", 5000); + } + }; + + const handleRemoveFromClaudePlugin = async () => { + try { + await window.api.applyClaudePluginConfig({ official: true }); + onNotify?.(t("notifications.removedFromClaudePlugin"), "success", 3000); + setClaudeApplied(false); + } catch (error: any) { + console.error(error); + const msg = + error && error.message + ? error.message + : t("notifications.syncClaudePluginFailed"); + onNotify?.(msg, "error", 5000); + } + }; + // 对供应商列表进行排序 const sortedProviders = Object.values(providers).sort((a, b) => { // 按添加时间排序 @@ -272,9 +321,10 @@ const ProviderList: React.FC = ({
{/* VS Code 按钮占位容器 - 始终保持空间,避免布局跳动 */} - {appType === "codex" ? ( -
- {provider.category !== "official" && isCurrent && ( +
+ {appType === "codex" && + provider.category !== "official" && + isCurrent && ( )} -
- ) : null} + + {appType === "claude" && + provider.category !== "official" && + isCurrent && ( + + )} +