mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-28 08:44:41 +08:00
8fe5c1041a
* feat: add Universal Provider feature - Add Universal Provider data structures and type definitions - Implement backend CRUD operations and sync functionality - Add frontend UI components (UniversalProviderPanel, Card, FormModal) - Add NewAPI icon and preset configuration - Support cross-app (Claude/Codex/Gemini) configuration sync - Add website URL field for providers - Implement real-time refresh via event notifications - Add i18n support (Chinese/English/Japanese) * feat: integrate universal provider presets into add provider dialog - Add universal provider presets (NewAPI, Custom Gateway) to preset selector - Show universal presets with Layers icon badge in preset selector - Open UniversalProviderFormModal when universal preset is clicked - Pass initialPreset to auto-fill form when opened from add dialog - Add i18n keys for addSuccess/addFailed messages - Keep separate universal provider panel for management * refactor: move universal provider management to add dialog - Remove Layers button from main navigation header - Add 'Manage' button next to universal provider presets - Open UniversalProviderPanel from within add provider dialog - Add i18n keys for 'manage' in all locales * style: display universal provider presets on separate line - Move universal provider section to a new row with border separator - Add label '统一供应商:' to clarify the section * style: unify universal provider label style with preset label - Use FormLabel component for consistent styling - Add background to 'Manage' button matching preset buttons - Update icon size and button padding for consistency * feat: add sync functionality and JSON preview for Universal Provider * fix: add missing in_failover_queue field to Provider structs After rebasing to main, the Provider struct gained a new `in_failover_queue` field. This fix adds the missing field to the three to_*_provider() methods in UniversalProvider. * refactor: redesign AddProviderDialog with tab-based layout - Add tabs to separate app-specific providers and universal providers - Move "Add Universal Provider" button from panel header to footer - Remove unused handleAdd callback and clean up imports - Update emptyHint i18n text to reference the footer button * fix: append /v1 suffix to Codex base_url in Universal Provider Codex uses OpenAI-compatible API which requires the /v1 endpoint suffix. The Universal Provider now automatically appends /v1 to base_url when generating Codex provider config if not already present. - Handle trailing slashes to avoid double slashes - Apply fix to both backend (to_codex_provider) and frontend preview * feat: auto-sync universal provider to apps on creation Previously, users had to manually click sync after adding a universal provider. Now it automatically syncs to Claude/Codex/Gemini on creation, providing a smoother user experience. --------- Co-authored-by: Jason <farion1231@gmail.com>
132 lines
3.0 KiB
TypeScript
132 lines
3.0 KiB
TypeScript
/**
|
|
* 统一供应商(Universal Provider)预设配置
|
|
*
|
|
* 统一供应商是跨应用共享的配置,修改后会自动同步到 Claude、Codex、Gemini 三个应用。
|
|
* 适用于 NewAPI 等支持多种协议的 API 网关。
|
|
*/
|
|
|
|
import type {
|
|
UniversalProvider,
|
|
UniversalProviderApps,
|
|
UniversalProviderModels,
|
|
} from "@/types";
|
|
|
|
/**
|
|
* 统一供应商预设接口
|
|
*/
|
|
export interface UniversalProviderPreset {
|
|
/** 预设名称 */
|
|
name: string;
|
|
/** 供应商类型标识 */
|
|
providerType: string;
|
|
/** 默认启用的应用 */
|
|
defaultApps: UniversalProviderApps;
|
|
/** 默认模型配置 */
|
|
defaultModels: UniversalProviderModels;
|
|
/** 网站链接 */
|
|
websiteUrl?: string;
|
|
/** 图标名称 */
|
|
icon?: string;
|
|
/** 图标颜色 */
|
|
iconColor?: string;
|
|
/** 描述 */
|
|
description?: string;
|
|
/** 是否为自定义模板(允许用户完全自定义) */
|
|
isCustomTemplate?: boolean;
|
|
}
|
|
|
|
/**
|
|
* NewAPI 默认模型配置
|
|
*/
|
|
const NEWAPI_DEFAULT_MODELS: UniversalProviderModels = {
|
|
claude: {
|
|
model: "claude-sonnet-4-20250514",
|
|
haikuModel: "claude-haiku-4-20250514",
|
|
sonnetModel: "claude-sonnet-4-20250514",
|
|
opusModel: "claude-sonnet-4-20250514",
|
|
},
|
|
codex: {
|
|
model: "gpt-4o",
|
|
reasoningEffort: "high",
|
|
},
|
|
gemini: {
|
|
model: "gemini-2.5-pro",
|
|
},
|
|
};
|
|
|
|
/**
|
|
* 统一供应商预设列表
|
|
*/
|
|
export const universalProviderPresets: UniversalProviderPreset[] = [
|
|
{
|
|
name: "NewAPI",
|
|
providerType: "newapi",
|
|
defaultApps: {
|
|
claude: true,
|
|
codex: true,
|
|
gemini: true,
|
|
},
|
|
defaultModels: NEWAPI_DEFAULT_MODELS,
|
|
websiteUrl: "https://www.newapi.pro",
|
|
icon: "newapi",
|
|
iconColor: "#00A67E",
|
|
description:
|
|
"NewAPI 是一个可自部署的 API 网关,支持 Anthropic、OpenAI、Gemini 等多种协议",
|
|
},
|
|
{
|
|
name: "自定义网关",
|
|
providerType: "custom_gateway",
|
|
defaultApps: {
|
|
claude: true,
|
|
codex: true,
|
|
gemini: true,
|
|
},
|
|
defaultModels: NEWAPI_DEFAULT_MODELS,
|
|
icon: "openai",
|
|
iconColor: "#6366F1",
|
|
description: "自定义配置的 API 网关",
|
|
isCustomTemplate: true,
|
|
},
|
|
];
|
|
|
|
/**
|
|
* 根据预设创建统一供应商
|
|
*/
|
|
export function createUniversalProviderFromPreset(
|
|
preset: UniversalProviderPreset,
|
|
id: string,
|
|
baseUrl: string,
|
|
apiKey: string,
|
|
customName?: string,
|
|
): UniversalProvider {
|
|
return {
|
|
id,
|
|
name: customName || preset.name,
|
|
providerType: preset.providerType,
|
|
apps: { ...preset.defaultApps },
|
|
baseUrl,
|
|
apiKey,
|
|
models: JSON.parse(JSON.stringify(preset.defaultModels)), // Deep copy
|
|
websiteUrl: preset.websiteUrl,
|
|
icon: preset.icon,
|
|
iconColor: preset.iconColor,
|
|
createdAt: Date.now(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取预设的显示名称(用于 UI)
|
|
*/
|
|
export function getPresetDisplayName(preset: UniversalProviderPreset): string {
|
|
return preset.name;
|
|
}
|
|
|
|
/**
|
|
* 根据类型查找预设
|
|
*/
|
|
export function findPresetByType(
|
|
providerType: string,
|
|
): UniversalProviderPreset | undefined {
|
|
return universalProviderPresets.find((p) => p.providerType === providerType);
|
|
}
|