mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-25 13:45:03 +08:00
3d69da5b66
* feat(providers): add notes field for provider management - Add notes field to Provider model (backend and frontend) - Display notes with higher priority than URL in provider card - Style notes as non-clickable text to differentiate from URLs - Add notes input field in provider form - Add i18n support (zh/en) for notes field * chore: format code and clean up unused props - Run cargo fmt on Rust backend code - Format TypeScript imports and code style - Remove unused appId prop from ProviderPresetSelector - Clean up unused variables in tests - Integrate notes field handling in provider dialogs * feat(deeplink): implement ccswitch:// protocol for provider import Add deep link support to enable one-click provider configuration import via ccswitch:// URLs. Backend: - Implement URL parsing and validation (src-tauri/src/deeplink.rs) - Add Tauri commands for parse and import (src-tauri/src/commands/deeplink.rs) - Register ccswitch:// protocol in macOS Info.plist - Add comprehensive unit tests (src-tauri/tests/deeplink_import.rs) Frontend: - Create confirmation dialog with security review UI (src/components/DeepLinkImportDialog.tsx) - Add API wrapper (src/lib/api/deeplink.ts) - Integrate event listeners in App.tsx Configuration: - Update Tauri config for deep link handling - Add i18n support for Chinese and English - Include test page for deep link validation (deeplink-test.html) Files: 15 changed, 1312 insertions(+) * chore(deeplink): integrate deep link handling into app lifecycle Wire up deep link infrastructure with app initialization and event handling. Backend Integration: - Register deep link module and commands in mod.rs - Add URL handling in app setup (src-tauri/src/lib.rs:handle_deeplink_url) - Handle deep links from single instance callback (Windows/Linux CLI) - Handle deep links from macOS system events - Add tauri-plugin-deep-link dependency (Cargo.toml) Frontend Integration: - Listen for deeplink-import/deeplink-error events in App.tsx - Update DeepLinkImportDialog component imports Configuration: - Enable deep link plugin in tauri.conf.json - Update Cargo.lock for new dependencies Localization: - Add Chinese translations for deep link UI (zh.json) - Add English translations for deep link UI (en.json) Files: 9 changed, 359 insertions(+), 18 deletions(-) * refactor(deeplink): enhance Codex provider template generation Align deep link import with UI preset generation logic by: - Adding complete config.toml template matching frontend defaults - Generating safe provider name from sanitized input - Including model_provider, reasoning_effort, and wire_api settings - Removing minimal template that only contained base_url - Cleaning up deprecated test file deeplink-test.html * style: fix clippy uninlined_format_args warnings Apply clippy --fix to use inline format arguments in: - src/mcp.rs (8 fixes) - src/services/env_manager.rs (10 fixes) * style: apply code formatting and cleanup - Format TypeScript files with Prettier (App.tsx, EnvWarningBanner.tsx, formatters.ts) - Organize Rust imports and module order alphabetically - Add newline at end of JSON files (en.json, zh.json) - Update Cargo.lock for dependency changes * feat: add model name configuration support for Codex and fix Gemini model handling - Add visual model name input field for Codex providers - Add model name extraction and update utilities in providerConfigUtils - Implement model name state management in useCodexConfigState hook - Add conditional model field rendering in CodexFormFields (non-official only) - Integrate model name sync with TOML config in ProviderForm - Fix Gemini deeplink model injection bug - Correct environment variable name from GOOGLE_GEMINI_MODEL to GEMINI_MODEL - Add test cases for Gemini model injection (with/without model) - All tests passing (9/9) - Fix Gemini model field binding in edit mode - Add geminiModel state to useGeminiConfigState hook - Extract model value during initialization and reset - Sync model field with geminiEnv state to prevent data loss on submit - Fix missing model value display when editing Gemini providers Changes: - 6 files changed, 245 insertions(+), 13 deletions(-)
241 lines
7.3 KiB
Rust
241 lines
7.3 KiB
Rust
#![allow(non_snake_case)]
|
||
|
||
use tauri::AppHandle;
|
||
use tauri_plugin_dialog::DialogExt;
|
||
use tauri_plugin_opener::OpenerExt;
|
||
|
||
use crate::app_config::AppType;
|
||
use crate::codex_config;
|
||
use crate::config::{self, get_claude_settings_path, ConfigStatus};
|
||
|
||
/// 获取 Claude Code 配置状态
|
||
#[tauri::command]
|
||
pub async fn get_claude_config_status() -> Result<ConfigStatus, String> {
|
||
Ok(config::get_claude_config_status())
|
||
}
|
||
|
||
use std::str::FromStr;
|
||
|
||
#[tauri::command]
|
||
pub async fn get_config_status(app: String) -> Result<ConfigStatus, String> {
|
||
match AppType::from_str(&app).map_err(|e| e.to_string())? {
|
||
AppType::Claude => Ok(config::get_claude_config_status()),
|
||
AppType::Codex => {
|
||
let auth_path = codex_config::get_codex_auth_path();
|
||
let exists = auth_path.exists();
|
||
let path = codex_config::get_codex_config_dir()
|
||
.to_string_lossy()
|
||
.to_string();
|
||
|
||
Ok(ConfigStatus { exists, path })
|
||
}
|
||
AppType::Gemini => {
|
||
let env_path = crate::gemini_config::get_gemini_env_path();
|
||
let exists = env_path.exists();
|
||
let path = crate::gemini_config::get_gemini_dir()
|
||
.to_string_lossy()
|
||
.to_string();
|
||
|
||
Ok(ConfigStatus { exists, path })
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 获取 Claude Code 配置文件路径
|
||
#[tauri::command]
|
||
pub async fn get_claude_code_config_path() -> Result<String, String> {
|
||
Ok(get_claude_settings_path().to_string_lossy().to_string())
|
||
}
|
||
|
||
/// 获取当前生效的配置目录
|
||
#[tauri::command]
|
||
pub async fn get_config_dir(app: String) -> Result<String, String> {
|
||
let dir = match AppType::from_str(&app).map_err(|e| e.to_string())? {
|
||
AppType::Claude => config::get_claude_config_dir(),
|
||
AppType::Codex => codex_config::get_codex_config_dir(),
|
||
AppType::Gemini => crate::gemini_config::get_gemini_dir(),
|
||
};
|
||
|
||
Ok(dir.to_string_lossy().to_string())
|
||
}
|
||
|
||
/// 打开配置文件夹
|
||
#[tauri::command]
|
||
pub async fn open_config_folder(handle: AppHandle, app: String) -> Result<bool, String> {
|
||
let config_dir = match AppType::from_str(&app).map_err(|e| e.to_string())? {
|
||
AppType::Claude => config::get_claude_config_dir(),
|
||
AppType::Codex => codex_config::get_codex_config_dir(),
|
||
AppType::Gemini => crate::gemini_config::get_gemini_dir(),
|
||
};
|
||
|
||
if !config_dir.exists() {
|
||
std::fs::create_dir_all(&config_dir).map_err(|e| format!("创建目录失败: {e}"))?;
|
||
}
|
||
|
||
handle
|
||
.opener()
|
||
.open_path(config_dir.to_string_lossy().to_string(), None::<String>)
|
||
.map_err(|e| format!("打开文件夹失败: {e}"))?;
|
||
|
||
Ok(true)
|
||
}
|
||
|
||
/// 弹出系统目录选择器并返回用户选择的路径
|
||
#[tauri::command]
|
||
pub async fn pick_directory(
|
||
app: AppHandle,
|
||
#[allow(non_snake_case)] defaultPath: Option<String>,
|
||
) -> Result<Option<String>, String> {
|
||
let initial = defaultPath
|
||
.map(|p| p.trim().to_string())
|
||
.filter(|p| !p.is_empty());
|
||
|
||
let result = tauri::async_runtime::spawn_blocking(move || {
|
||
let mut builder = app.dialog().file();
|
||
if let Some(path) = initial {
|
||
builder = builder.set_directory(path);
|
||
}
|
||
builder.blocking_pick_folder()
|
||
})
|
||
.await
|
||
.map_err(|e| format!("弹出目录选择器失败: {e}"))?;
|
||
|
||
match result {
|
||
Some(file_path) => {
|
||
let resolved = file_path
|
||
.simplified()
|
||
.into_path()
|
||
.map_err(|e| format!("解析选择的目录失败: {e}"))?;
|
||
Ok(Some(resolved.to_string_lossy().to_string()))
|
||
}
|
||
None => Ok(None),
|
||
}
|
||
}
|
||
|
||
/// 获取应用配置文件路径
|
||
#[tauri::command]
|
||
pub async fn get_app_config_path() -> Result<String, String> {
|
||
let config_path = config::get_app_config_path();
|
||
Ok(config_path.to_string_lossy().to_string())
|
||
}
|
||
|
||
/// 打开应用配置文件夹
|
||
#[tauri::command]
|
||
pub async fn open_app_config_folder(handle: AppHandle) -> Result<bool, String> {
|
||
let config_dir = config::get_app_config_dir();
|
||
|
||
if !config_dir.exists() {
|
||
std::fs::create_dir_all(&config_dir).map_err(|e| format!("创建目录失败: {e}"))?;
|
||
}
|
||
|
||
handle
|
||
.opener()
|
||
.open_path(config_dir.to_string_lossy().to_string(), None::<String>)
|
||
.map_err(|e| format!("打开文件夹失败: {e}"))?;
|
||
|
||
Ok(true)
|
||
}
|
||
|
||
/// 获取 Claude 通用配置片段(已废弃,使用 get_common_config_snippet)
|
||
#[tauri::command]
|
||
pub async fn get_claude_common_config_snippet(
|
||
state: tauri::State<'_, crate::store::AppState>,
|
||
) -> Result<Option<String>, String> {
|
||
let guard = state
|
||
.config
|
||
.read()
|
||
.map_err(|e| format!("读取配置锁失败: {e}"))?;
|
||
Ok(guard.common_config_snippets.claude.clone())
|
||
}
|
||
|
||
/// 设置 Claude 通用配置片段(已废弃,使用 set_common_config_snippet)
|
||
#[tauri::command]
|
||
pub async fn set_claude_common_config_snippet(
|
||
snippet: String,
|
||
state: tauri::State<'_, crate::store::AppState>,
|
||
) -> Result<(), String> {
|
||
let mut guard = state
|
||
.config
|
||
.write()
|
||
.map_err(|e| format!("写入配置锁失败: {e}"))?;
|
||
|
||
// 验证是否为有效的 JSON(如果不为空)
|
||
if !snippet.trim().is_empty() {
|
||
serde_json::from_str::<serde_json::Value>(&snippet)
|
||
.map_err(|e| format!("无效的 JSON 格式: {e}"))?;
|
||
}
|
||
|
||
guard.common_config_snippets.claude = if snippet.trim().is_empty() {
|
||
None
|
||
} else {
|
||
Some(snippet)
|
||
};
|
||
|
||
guard.save().map_err(|e| e.to_string())?;
|
||
Ok(())
|
||
}
|
||
|
||
/// 获取通用配置片段(统一接口)
|
||
#[tauri::command]
|
||
pub async fn get_common_config_snippet(
|
||
app_type: String,
|
||
state: tauri::State<'_, crate::store::AppState>,
|
||
) -> Result<Option<String>, String> {
|
||
use crate::app_config::AppType;
|
||
use std::str::FromStr;
|
||
|
||
let app = AppType::from_str(&app_type).map_err(|e| format!("无效的应用类型: {e}"))?;
|
||
|
||
let guard = state
|
||
.config
|
||
.read()
|
||
.map_err(|e| format!("读取配置锁失败: {e}"))?;
|
||
|
||
Ok(guard.common_config_snippets.get(&app).cloned())
|
||
}
|
||
|
||
/// 设置通用配置片段(统一接口)
|
||
#[tauri::command]
|
||
pub async fn set_common_config_snippet(
|
||
app_type: String,
|
||
snippet: String,
|
||
state: tauri::State<'_, crate::store::AppState>,
|
||
) -> Result<(), String> {
|
||
use crate::app_config::AppType;
|
||
use std::str::FromStr;
|
||
|
||
let app = AppType::from_str(&app_type).map_err(|e| format!("无效的应用类型: {e}"))?;
|
||
|
||
let mut guard = state
|
||
.config
|
||
.write()
|
||
.map_err(|e| format!("写入配置锁失败: {e}"))?;
|
||
|
||
// 验证格式(根据应用类型)
|
||
if !snippet.trim().is_empty() {
|
||
match app {
|
||
AppType::Claude | AppType::Gemini => {
|
||
// 验证 JSON 格式
|
||
serde_json::from_str::<serde_json::Value>(&snippet)
|
||
.map_err(|e| format!("无效的 JSON 格式: {e}"))?;
|
||
}
|
||
AppType::Codex => {
|
||
// TOML 格式暂不验证(或可使用 toml crate)
|
||
// 注意:TOML 验证较为复杂,暂时跳过
|
||
}
|
||
}
|
||
}
|
||
|
||
guard.common_config_snippets.set(
|
||
&app,
|
||
if snippet.trim().is_empty() {
|
||
None
|
||
} else {
|
||
Some(snippet)
|
||
},
|
||
);
|
||
|
||
guard.save().map_err(|e| e.to_string())?;
|
||
Ok(())
|
||
}
|