2025-10-16 10:49:56 +08:00
|
|
|
import { useCallback } from "react";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
2025-10-18 23:31:14 +08:00
|
|
|
import { Plus } from "lucide-react";
|
2025-10-16 10:49:56 +08:00
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
2025-10-18 23:28:33 +08:00
|
|
|
DialogFooter,
|
2025-10-16 10:49:56 +08:00
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
} from "@/components/ui/dialog";
|
2025-10-18 23:28:33 +08:00
|
|
|
import { Button } from "@/components/ui/button";
|
2025-10-16 17:40:25 +08:00
|
|
|
import type { Provider, CustomEndpoint } from "@/types";
|
2025-10-30 14:59:15 +08:00
|
|
|
import type { AppId } from "@/lib/api";
|
2025-10-16 10:49:56 +08:00
|
|
|
import {
|
|
|
|
|
ProviderForm,
|
|
|
|
|
type ProviderFormValues,
|
|
|
|
|
} from "@/components/providers/forms/ProviderForm";
|
2025-11-02 21:05:48 +08:00
|
|
|
import { providerPresets } from "@/config/claudeProviderPresets";
|
2025-10-16 17:40:25 +08:00
|
|
|
import { codexProviderPresets } from "@/config/codexProviderPresets";
|
2025-11-12 10:47:34 +08:00
|
|
|
import { geminiProviderPresets } from "@/config/geminiProviderPresets";
|
2025-10-16 10:49:56 +08:00
|
|
|
|
|
|
|
|
interface AddProviderDialogProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
2025-10-30 14:59:15 +08:00
|
|
|
appId: AppId;
|
2025-10-16 10:49:56 +08:00
|
|
|
onSubmit: (provider: Omit<Provider, "id">) => Promise<void> | void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function AddProviderDialog({
|
|
|
|
|
open,
|
|
|
|
|
onOpenChange,
|
2025-10-30 14:59:15 +08:00
|
|
|
appId,
|
2025-10-16 10:49:56 +08:00
|
|
|
onSubmit,
|
|
|
|
|
}: AddProviderDialogProps) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const handleSubmit = useCallback(
|
|
|
|
|
async (values: ProviderFormValues) => {
|
|
|
|
|
const parsedConfig = JSON.parse(values.settingsConfig) as Record<
|
|
|
|
|
string,
|
|
|
|
|
unknown
|
|
|
|
|
>;
|
|
|
|
|
|
2025-10-16 17:40:25 +08:00
|
|
|
// 构造基础提交数据
|
2025-10-16 10:49:56 +08:00
|
|
|
const providerData: Omit<Provider, "id"> = {
|
|
|
|
|
name: values.name.trim(),
|
feat: add model configuration support and fix Gemini deeplink bug (#251)
* 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(-)
2025-11-19 09:03:18 +08:00
|
|
|
notes: values.notes?.trim() || undefined,
|
2025-10-16 10:49:56 +08:00
|
|
|
websiteUrl: values.websiteUrl?.trim() || undefined,
|
|
|
|
|
settingsConfig: parsedConfig,
|
2025-10-16 13:02:38 +08:00
|
|
|
...(values.presetCategory ? { category: values.presetCategory } : {}),
|
2025-10-28 20:28:11 +08:00
|
|
|
...(values.meta ? { meta: values.meta } : {}),
|
2025-10-16 10:49:56 +08:00
|
|
|
};
|
|
|
|
|
|
2025-10-28 20:28:11 +08:00
|
|
|
const hasCustomEndpoints =
|
|
|
|
|
providerData.meta?.custom_endpoints &&
|
|
|
|
|
Object.keys(providerData.meta.custom_endpoints).length > 0;
|
2025-10-16 17:40:25 +08:00
|
|
|
|
2025-10-28 20:28:11 +08:00
|
|
|
if (!hasCustomEndpoints) {
|
|
|
|
|
// 收集端点候选(仅在缺少自定义端点时兜底)
|
|
|
|
|
// 1. 从预设配置中获取 endpointCandidates
|
|
|
|
|
// 2. 从当前配置中提取 baseUrl (ANTHROPIC_BASE_URL 或 Codex base_url)
|
|
|
|
|
const urlSet = new Set<string>();
|
|
|
|
|
|
|
|
|
|
const addUrl = (rawUrl?: string) => {
|
|
|
|
|
const url = (rawUrl || "").trim().replace(/\/+$/, "");
|
|
|
|
|
if (url && url.startsWith("http")) {
|
|
|
|
|
urlSet.add(url);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (values.presetId) {
|
2025-10-30 14:59:15 +08:00
|
|
|
if (appId === "claude") {
|
2025-10-28 20:28:11 +08:00
|
|
|
const presets = providerPresets;
|
|
|
|
|
const presetIndex = parseInt(
|
|
|
|
|
values.presetId.replace("claude-", ""),
|
|
|
|
|
);
|
|
|
|
|
if (
|
|
|
|
|
!isNaN(presetIndex) &&
|
|
|
|
|
presetIndex >= 0 &&
|
|
|
|
|
presetIndex < presets.length
|
|
|
|
|
) {
|
|
|
|
|
const preset = presets[presetIndex];
|
|
|
|
|
if (preset?.endpointCandidates) {
|
|
|
|
|
preset.endpointCandidates.forEach(addUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-30 14:59:15 +08:00
|
|
|
} else if (appId === "codex") {
|
2025-10-28 20:28:11 +08:00
|
|
|
const presets = codexProviderPresets;
|
2025-10-30 15:31:08 +08:00
|
|
|
const presetIndex = parseInt(values.presetId.replace("codex-", ""));
|
2025-10-28 20:28:11 +08:00
|
|
|
if (
|
|
|
|
|
!isNaN(presetIndex) &&
|
|
|
|
|
presetIndex >= 0 &&
|
|
|
|
|
presetIndex < presets.length
|
|
|
|
|
) {
|
|
|
|
|
const preset = presets[presetIndex];
|
|
|
|
|
if (Array.isArray(preset.endpointCandidates)) {
|
|
|
|
|
preset.endpointCandidates.forEach(addUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-12 10:47:34 +08:00
|
|
|
} else if (appId === "gemini") {
|
|
|
|
|
const presets = geminiProviderPresets;
|
|
|
|
|
const presetIndex = parseInt(
|
|
|
|
|
values.presetId.replace("gemini-", ""),
|
|
|
|
|
);
|
|
|
|
|
if (
|
|
|
|
|
!isNaN(presetIndex) &&
|
|
|
|
|
presetIndex >= 0 &&
|
|
|
|
|
presetIndex < presets.length
|
|
|
|
|
) {
|
|
|
|
|
const preset = presets[presetIndex];
|
|
|
|
|
if (Array.isArray(preset.endpointCandidates)) {
|
|
|
|
|
preset.endpointCandidates.forEach(addUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-28 20:28:11 +08:00
|
|
|
}
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-30 14:59:15 +08:00
|
|
|
if (appId === "claude") {
|
2025-10-28 20:28:11 +08:00
|
|
|
const env = parsedConfig.env as Record<string, any> | undefined;
|
|
|
|
|
if (env?.ANTHROPIC_BASE_URL) {
|
|
|
|
|
addUrl(env.ANTHROPIC_BASE_URL);
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
2025-10-30 14:59:15 +08:00
|
|
|
} else if (appId === "codex") {
|
2025-10-28 20:28:11 +08:00
|
|
|
const config = parsedConfig.config as string | undefined;
|
|
|
|
|
if (config) {
|
2025-10-30 15:31:08 +08:00
|
|
|
const baseUrlMatch = config.match(
|
|
|
|
|
/base_url\s*=\s*["']([^"']+)["']/,
|
|
|
|
|
);
|
2025-10-28 20:28:11 +08:00
|
|
|
if (baseUrlMatch?.[1]) {
|
|
|
|
|
addUrl(baseUrlMatch[1]);
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-12 10:47:34 +08:00
|
|
|
} else if (appId === "gemini") {
|
|
|
|
|
const env = parsedConfig.env as Record<string, any> | undefined;
|
|
|
|
|
if (env?.GOOGLE_GEMINI_BASE_URL) {
|
|
|
|
|
addUrl(env.GOOGLE_GEMINI_BASE_URL);
|
|
|
|
|
}
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-28 20:28:11 +08:00
|
|
|
const urls = Array.from(urlSet);
|
|
|
|
|
if (urls.length > 0) {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const customEndpoints: Record<string, CustomEndpoint> = {};
|
|
|
|
|
urls.forEach((url) => {
|
|
|
|
|
customEndpoints[url] = {
|
|
|
|
|
url,
|
|
|
|
|
addedAt: now,
|
|
|
|
|
lastUsed: undefined,
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-10-16 17:40:25 +08:00
|
|
|
|
2025-10-28 20:28:11 +08:00
|
|
|
providerData.meta = {
|
|
|
|
|
...(providerData.meta ?? {}),
|
|
|
|
|
custom_endpoints: customEndpoints,
|
2025-10-16 17:40:25 +08:00
|
|
|
};
|
2025-10-28 20:28:11 +08:00
|
|
|
}
|
2025-10-16 17:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:49:56 +08:00
|
|
|
await onSubmit(providerData);
|
|
|
|
|
onOpenChange(false);
|
|
|
|
|
},
|
2025-10-30 14:59:15 +08:00
|
|
|
[appId, onSubmit, onOpenChange],
|
2025-10-16 10:49:56 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const submitLabel =
|
2025-10-30 14:59:15 +08:00
|
|
|
appId === "claude"
|
2025-10-19 11:55:46 +08:00
|
|
|
? t("provider.addClaudeProvider")
|
2025-11-12 10:47:34 +08:00
|
|
|
: appId === "codex"
|
|
|
|
|
? t("provider.addCodexProvider")
|
|
|
|
|
: t("provider.addGeminiProvider");
|
2025-10-16 10:49:56 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
2025-10-19 12:02:22 +08:00
|
|
|
<DialogContent className="max-w-3xl max-h-[85vh] min-h-[600px] flex flex-col">
|
2025-10-16 10:49:56 +08:00
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>{submitLabel}</DialogTitle>
|
2025-10-24 13:02:35 +08:00
|
|
|
<DialogDescription>{t("provider.addProviderHint")}</DialogDescription>
|
2025-10-16 10:49:56 +08:00
|
|
|
</DialogHeader>
|
|
|
|
|
|
2025-10-18 16:52:02 +08:00
|
|
|
<div className="flex-1 overflow-y-auto px-6 py-4">
|
2025-10-16 16:39:03 +08:00
|
|
|
<ProviderForm
|
2025-10-30 14:59:15 +08:00
|
|
|
appId={appId}
|
2025-10-19 11:55:46 +08:00
|
|
|
submitLabel={t("common.add")}
|
2025-10-16 16:39:03 +08:00
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
onCancel={() => onOpenChange(false)}
|
2025-10-18 23:28:33 +08:00
|
|
|
showButtons={false}
|
2025-10-16 16:39:03 +08:00
|
|
|
/>
|
|
|
|
|
</div>
|
2025-10-18 23:28:33 +08:00
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
2025-10-19 11:55:46 +08:00
|
|
|
{t("common.cancel")}
|
2025-10-18 23:28:33 +08:00
|
|
|
</Button>
|
|
|
|
|
<Button type="submit" form="provider-form">
|
2025-10-18 23:31:14 +08:00
|
|
|
<Plus className="h-4 w-4" />
|
2025-10-19 11:55:46 +08:00
|
|
|
{t("common.add")}
|
2025-10-18 23:28:33 +08:00
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
2025-10-16 10:49:56 +08:00
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|