mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-24 12:44:18 +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(-)
233 lines
6.8 KiB
TypeScript
233 lines
6.8 KiB
TypeScript
import { useState, useCallback, useEffect } from "react";
|
|
|
|
interface UseGeminiConfigStateProps {
|
|
initialData?: {
|
|
settingsConfig?: Record<string, unknown>;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 管理 Gemini 配置状态
|
|
* Gemini 配置包含两部分:env (环境变量) 和 config (扩展配置 JSON)
|
|
*/
|
|
export function useGeminiConfigState({
|
|
initialData,
|
|
}: UseGeminiConfigStateProps) {
|
|
const [geminiEnv, setGeminiEnvState] = useState("");
|
|
const [geminiConfig, setGeminiConfigState] = useState("");
|
|
const [geminiApiKey, setGeminiApiKey] = useState("");
|
|
const [geminiBaseUrl, setGeminiBaseUrl] = useState("");
|
|
const [geminiModel, setGeminiModel] = useState("");
|
|
const [envError, setEnvError] = useState("");
|
|
const [configError, setConfigError] = useState("");
|
|
|
|
// 将 JSON env 对象转换为 .env 格式字符串
|
|
const envObjToString = useCallback(
|
|
(envObj: Record<string, unknown>): string => {
|
|
const lines: string[] = [];
|
|
if (typeof envObj.GOOGLE_GEMINI_BASE_URL === "string") {
|
|
lines.push(`GOOGLE_GEMINI_BASE_URL=${envObj.GOOGLE_GEMINI_BASE_URL}`);
|
|
}
|
|
if (typeof envObj.GEMINI_API_KEY === "string") {
|
|
lines.push(`GEMINI_API_KEY=${envObj.GEMINI_API_KEY}`);
|
|
}
|
|
if (typeof envObj.GEMINI_MODEL === "string") {
|
|
lines.push(`GEMINI_MODEL=${envObj.GEMINI_MODEL}`);
|
|
}
|
|
return lines.join("\n");
|
|
},
|
|
[],
|
|
);
|
|
|
|
// 将 .env 格式字符串转换为 JSON env 对象
|
|
const envStringToObj = useCallback(
|
|
(envString: string): Record<string, string> => {
|
|
const env: Record<string, string> = {};
|
|
const lines = envString.split("\n");
|
|
lines.forEach((line) => {
|
|
const trimmed = line.trim();
|
|
if (!trimmed || trimmed.startsWith("#")) return;
|
|
const equalIndex = trimmed.indexOf("=");
|
|
if (equalIndex > 0) {
|
|
const key = trimmed.substring(0, equalIndex).trim();
|
|
const value = trimmed.substring(equalIndex + 1).trim();
|
|
env[key] = value;
|
|
}
|
|
});
|
|
return env;
|
|
},
|
|
[],
|
|
);
|
|
|
|
// 初始化 Gemini 配置(编辑模式)
|
|
useEffect(() => {
|
|
if (!initialData) return;
|
|
|
|
const config = initialData.settingsConfig;
|
|
if (typeof config === "object" && config !== null) {
|
|
// 设置 env
|
|
const env = (config as any).env || {};
|
|
setGeminiEnvState(envObjToString(env));
|
|
|
|
// 设置 config
|
|
const configObj = (config as any).config || {};
|
|
setGeminiConfigState(JSON.stringify(configObj, null, 2));
|
|
|
|
// 提取 API Key、Base URL 和 Model
|
|
if (typeof env.GEMINI_API_KEY === "string") {
|
|
setGeminiApiKey(env.GEMINI_API_KEY);
|
|
}
|
|
if (typeof env.GOOGLE_GEMINI_BASE_URL === "string") {
|
|
setGeminiBaseUrl(env.GOOGLE_GEMINI_BASE_URL);
|
|
}
|
|
if (typeof env.GEMINI_MODEL === "string") {
|
|
setGeminiModel(env.GEMINI_MODEL);
|
|
}
|
|
}
|
|
}, [initialData, envObjToString]);
|
|
|
|
// 从 geminiEnv 中提取并同步 API Key、Base URL 和 Model
|
|
useEffect(() => {
|
|
const envObj = envStringToObj(geminiEnv);
|
|
const extractedKey = envObj.GEMINI_API_KEY || "";
|
|
const extractedBaseUrl = envObj.GOOGLE_GEMINI_BASE_URL || "";
|
|
const extractedModel = envObj.GEMINI_MODEL || "";
|
|
|
|
if (extractedKey !== geminiApiKey) {
|
|
setGeminiApiKey(extractedKey);
|
|
}
|
|
if (extractedBaseUrl !== geminiBaseUrl) {
|
|
setGeminiBaseUrl(extractedBaseUrl);
|
|
}
|
|
if (extractedModel !== geminiModel) {
|
|
setGeminiModel(extractedModel);
|
|
}
|
|
}, [geminiEnv, envStringToObj, geminiApiKey, geminiBaseUrl, geminiModel]);
|
|
|
|
// 验证 Gemini Config JSON
|
|
const validateGeminiConfig = useCallback((value: string): string => {
|
|
if (!value.trim()) return ""; // 空值允许
|
|
try {
|
|
const parsed = JSON.parse(value);
|
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
return "";
|
|
}
|
|
return "Config must be a JSON object";
|
|
} catch {
|
|
return "Invalid JSON format";
|
|
}
|
|
}, []);
|
|
|
|
// 设置 env
|
|
const setGeminiEnv = useCallback((value: string) => {
|
|
setGeminiEnvState(value);
|
|
// .env 格式较宽松,不做严格校验
|
|
setEnvError("");
|
|
}, []);
|
|
|
|
// 设置 config (支持函数更新)
|
|
const setGeminiConfig = useCallback(
|
|
(value: string | ((prev: string) => string)) => {
|
|
const newValue =
|
|
typeof value === "function" ? value(geminiConfig) : value;
|
|
setGeminiConfigState(newValue);
|
|
setConfigError(validateGeminiConfig(newValue));
|
|
},
|
|
[geminiConfig, validateGeminiConfig],
|
|
);
|
|
|
|
// 处理 Gemini API Key 输入并写回 env
|
|
const handleGeminiApiKeyChange = useCallback(
|
|
(key: string) => {
|
|
const trimmed = key.trim();
|
|
setGeminiApiKey(trimmed);
|
|
|
|
const envObj = envStringToObj(geminiEnv);
|
|
envObj.GEMINI_API_KEY = trimmed;
|
|
const newEnv = envObjToString(envObj);
|
|
setGeminiEnv(newEnv);
|
|
},
|
|
[geminiEnv, envStringToObj, envObjToString, setGeminiEnv],
|
|
);
|
|
|
|
// 处理 Gemini Base URL 变化
|
|
const handleGeminiBaseUrlChange = useCallback(
|
|
(url: string) => {
|
|
const sanitized = url.trim().replace(/\/+$/, "");
|
|
setGeminiBaseUrl(sanitized);
|
|
|
|
const envObj = envStringToObj(geminiEnv);
|
|
envObj.GOOGLE_GEMINI_BASE_URL = sanitized;
|
|
const newEnv = envObjToString(envObj);
|
|
setGeminiEnv(newEnv);
|
|
},
|
|
[geminiEnv, envStringToObj, envObjToString, setGeminiEnv],
|
|
);
|
|
|
|
// 处理 env 变化
|
|
const handleGeminiEnvChange = useCallback(
|
|
(value: string) => {
|
|
setGeminiEnv(value);
|
|
},
|
|
[setGeminiEnv],
|
|
);
|
|
|
|
// 处理 config 变化
|
|
const handleGeminiConfigChange = useCallback(
|
|
(value: string) => {
|
|
setGeminiConfig(value);
|
|
},
|
|
[setGeminiConfig],
|
|
);
|
|
|
|
// 重置配置(用于预设切换)
|
|
const resetGeminiConfig = useCallback(
|
|
(env: Record<string, unknown>, config: Record<string, unknown>) => {
|
|
const envString = envObjToString(env);
|
|
const configString = JSON.stringify(config, null, 2);
|
|
|
|
setGeminiEnv(envString);
|
|
setGeminiConfig(configString);
|
|
|
|
// 提取 API Key、Base URL 和 Model
|
|
if (typeof env.GEMINI_API_KEY === "string") {
|
|
setGeminiApiKey(env.GEMINI_API_KEY);
|
|
} else {
|
|
setGeminiApiKey("");
|
|
}
|
|
|
|
if (typeof env.GOOGLE_GEMINI_BASE_URL === "string") {
|
|
setGeminiBaseUrl(env.GOOGLE_GEMINI_BASE_URL);
|
|
} else {
|
|
setGeminiBaseUrl("");
|
|
}
|
|
|
|
if (typeof env.GEMINI_MODEL === "string") {
|
|
setGeminiModel(env.GEMINI_MODEL);
|
|
} else {
|
|
setGeminiModel("");
|
|
}
|
|
},
|
|
[envObjToString, setGeminiEnv, setGeminiConfig],
|
|
);
|
|
|
|
return {
|
|
geminiEnv,
|
|
geminiConfig,
|
|
geminiApiKey,
|
|
geminiBaseUrl,
|
|
geminiModel,
|
|
envError,
|
|
configError,
|
|
setGeminiEnv,
|
|
setGeminiConfig,
|
|
handleGeminiApiKeyChange,
|
|
handleGeminiBaseUrlChange,
|
|
handleGeminiEnvChange,
|
|
handleGeminiConfigChange,
|
|
resetGeminiConfig,
|
|
envStringToObj,
|
|
envObjToString,
|
|
};
|
|
}
|