21 Commits

Author SHA1 Message Date
Jason
32a2ba5ef6 chore(release): prepare for v3.6.2 release 2025-11-11 23:57:21 +08:00
Jason
4502b2f973 feat(presets): add Kimi For Coding and BaiLing provider presets
Add two new Chinese official provider presets:
- Kimi For Coding: AI coding assistant from Kimi
- BaiLing: Claude-compatible API from AliPay TBox
2025-11-11 23:50:52 +08:00
Jason
6cb930b4ec docs: add TypeScript Trending badge and improve contributing tone
- Add 🔥 TypeScript Trending badge celebrating daily/weekly/monthly rankings
- Refine contributing guidelines with friendlier, more welcoming language
- Replace directive tone with collaborative suggestions for feature PRs
2025-11-11 11:03:26 +08:00
Jason
9a5c8c0e57 chore(release): prepare for v3.6.1 release
- Bump version to 3.6.1 across all config files
  - package.json, Cargo.toml, tauri.conf.json
  - README.md and README_ZH.md version badges
  - Auto-updated Cargo.lock
- Add release notes documentation
  - docs/release-note-v3.6.0-en.md (archive)
  - docs/release-note-v3.6.1-zh.md (cumulative format)
  - docs/release-note-v3.6.1-en.md (cumulative format)
- Enlarge PackyCode sponsor logo by 50% (100px → 150px)
- Update roadmap checklist (homebrew support marked as done)
2025-11-10 21:21:27 +08:00
Jason
b1abdf95aa feat(sponsor): add PackyCode as official partner
- Add PackyCode to sponsor section in README (both EN/ZH)
- Add PackyCode logo to assets/partners/logos/
- Mark PackyCode as partner in provider presets (Claude & Codex)
- Add promotion message to i18n files with 10% discount info
2025-11-10 18:44:19 +08:00
Jason
be155c857e refactor(usage-script): replace native checkbox with Switch component
Upgrade the enable toggle from native checkbox to shadcn/ui Switch component
for better UX and UI consistency with settings page.

**Improvements**:
1. Use modern toggle UI (Switch) instead of traditional checkbox
2. Adopt the same layout pattern as settings page (ToggleRow style)
3. Add bordered container with proper spacing for better visual hierarchy
4. Maintain full accessibility support (aria-label)

**Layout changes**:
- Before: Simple label + checkbox horizontal layout
- After: Bordered container, label on left, Switch on right, vertically centered
2025-11-10 16:01:28 +08:00
Jason
9d6f101006 fix(usage-script): replace FormLabel with Label to fix white screen crash
FormLabel component requires FormField context and throws error when used
standalone, causing the entire component to crash with a white screen.

Root cause:
- FormLabel internally calls useFormField() hook
- useFormField() requires FormFieldContext (must be within <FormField>)
- Without context, it throws: "useFormField should be used within <FormField>"
- Uncaught error crashes React rendering tree

Solution:
- Replace FormLabel with standalone Label component
- Label component from @/components/ui/label doesn't depend on form context
- Maintains same styling (text-sm font-medium) without requiring context

This fixes the white screen issue when clicking the usage panel.
2025-11-10 15:51:18 +08:00
Jason
2a56a0d889 style(usage-script): unify form input styles with shadcn/ui components
Replace native HTML input elements with shadcn/ui Input and FormLabel
components to ensure consistent styling across the application.

Changes:
- Import Input, FormLabel, Eye, and EyeOff components
- Replace all credential input fields with Input component
- Add show/hide toggle buttons for password fields (API Key, Access Token)
- Replace label/span elements with FormLabel component
- Update timeout and auto-query interval inputs to use Input component
- Improve spacing consistency (space-y-4 for credential config)
- Add proper id attributes for accessibility
- Use muted-foreground for hint text

The form now matches the styling of provider configuration forms
throughout the application.
2025-11-10 15:42:36 +08:00
Jason
7096957b40 feat(usage-query): decouple credentials from provider config
Add independent credential fields for usage query to support different
query endpoints and authentication methods.

Changes:
- Add `apiKey` and `baseUrl` fields to UsageScript struct
- Remove dependency on provider config credentials in query_usage
- Update test_usage_script to accept independent credential parameters
- Add credential input fields in UsageScriptModal based on template:
  * General: apiKey + baseUrl
  * NewAPI: baseUrl + accessToken + userId
  * Custom: no additional fields (full freedom)
- Auto-clear irrelevant fields when switching templates
- Add i18n text for "credentialsConfig"

Benefits:
- Query API can use different endpoint/key than provider config
- Better separation of concerns
- More flexible for various usage query scenarios
2025-11-10 15:28:09 +08:00
Jason
23d06515ad fix(toml): normalize CJK quotes to prevent parsing errors
Add quote normalization to handle Chinese/fullwidth quotes automatically
converted by IME. This fixes TOML parsing failures when users input
configuration with non-ASCII quotes (" " ' ' etc.).

Changes:
- Add textNormalization utility for quote normalization
- Apply normalization in TOML input handlers (MCP form, Codex config)
- Disable browser auto-correction in Textarea component
- Add defensive normalization in TOML parsing layer
2025-11-10 14:35:55 +08:00
Jason
3210202132 fix(mcp): preserve custom fields in Codex TOML config editor
Fixed an issue where custom/extension fields (e.g., timeout_ms, retry_count)
were silently dropped when editing Codex MCP server configurations in TOML format.

Root cause: The TOML parser functions only extracted known fields (type, command,
args, env, cwd, url, headers), discarding any additional fields during normalization.

Changes:
- mcpServerToToml: Now uses spread operator to copy all fields before stringification
- normalizeServerConfig: Added logic to preserve unknown fields after processing known ones
- Both stdio and http server types now retain custom configuration fields

This fix enables forward compatibility with future MCP protocol extensions and
allows users to add custom configurations without code changes.
2025-11-10 12:03:15 +08:00
Jason
7b52c44a9d feat(schema): add common JSON/TOML validators and enforce MCP conditional fields
- Add src/lib/schemas/common.ts with jsonConfigSchema and tomlConfigSchema
- Enhance src/lib/schemas/mcp.ts to require command for stdio and url for http via superRefine
- Keep ProviderForm as-is; future steps will wire new schemas into RHF flows
- Verified: pnpm typecheck passes
2025-11-09 20:42:43 +08:00
Jason
772081312e feat(ui): enhance provider switch error notification with copy action
- Split error message into title and description for better UX
- Add one-click copy button to easily share error details
- Extend toast duration to 6s for improved readability
- Use extractErrorMessage utility for consistent error handling
- Add i18n keys: common.copy, notifications.switchFailedTitle

This improves debugging experience when provider switching fails,
allowing users to quickly copy and share error messages.
2025-11-09 17:56:02 +08:00
Jason
cfcd7b892a fix(tray): replace unwrap with safe pattern matching in menu handler
Replace unwrap() calls with safe pattern matching to prevent panics
when handling invalid tray menu item IDs. Now logs errors and returns
gracefully instead of crashing the application.
2025-11-08 22:55:53 +08:00
Jason
3da787b9af fix(provider): set category to custom for imported default config
Ensure that when importing default configuration from live files,
the created provider is properly marked with category "custom" for
correct UI display and filtering.
2025-11-08 22:45:53 +08:00
Jason
9370054911 fix(error-handling): isolate tray menu update failures from main operations
Previously, if updateTrayMenu() failed after a successful main operation
(like sorting, adding, or updating providers), the entire operation would
appear to fail with a misleading error message, even though the core
functionality had already succeeded.

This resulted in false negative feedback where:
- Backend data was successfully updated
- Frontend UI was successfully refreshed
- Tray menu failed to update
- User saw "operation failed" message (incorrect)

Changes:
- Wrap updateTrayMenu() calls in nested try-catch blocks
- Log tray menu failures separately with descriptive messages
- Ensure main operation success is reported accurately
- Prevent tray menu failures from triggering main operation error handlers

Files modified:
- src/hooks/useDragSort.ts (drag-and-drop sorting)
- src/lib/query/mutations.ts (add/delete/switch mutations)
- src/hooks/useProviderActions.ts (update provider)

This fixes the bug introduced in PR #179 and prevents similar issues
across all provider operations.
2025-11-08 22:07:12 +08:00
ZyphrZero
5b3b211c9a fix(ui): sync tray menu order after drag-and-drop sorting (#179)
The drag-and-drop sorting feature introduced in PR #126 (9eb991d) was
updating the provider sort order in the backend and frontend, but failed
to update the tray menu to reflect the new order.

Changes:
- Add updateTrayMenu() call after successful sort order update
- Ensures tray menu items are immediately reordered to match the UI

This fixes the issue where dragging providers in the main window would
not update their order in the system tray menu.

Fixes: 9eb991d (feat(ui): add drag-and-drop sorting for provider list)
2025-11-08 21:45:43 +08:00
Jason
fb02881684 fix(forms): populate base URL for all non-official provider categories
The base URL field was not populating when editing providers with
categories like cn_official or aggregator. The issue was caused by
inconsistent conditional logic: the input field was shown for all
non-official categories, but the value extraction only worked for
third_party and custom categories.

Changed the category check from allowlist (third_party, custom) to
denylist (official) to match the UI display logic. Now ANTHROPIC_BASE_URL
correctly populates for all provider categories except official.
2025-11-08 21:25:41 +08:00
Jason
34b8aa1008 fix(ui): remove misleading model placeholders from input fields
Clear placeholder values for model input fields to avoid suggesting specific model names that may not be applicable to all providers. This prevents user confusion when configuring different Claude providers.
2025-11-08 08:50:15 +08:00
Jason
52a7f9d313 feat(usage): enable background query for usage polling
- Add `refetchIntervalInBackground: true` to usage query configuration
- Allows usage queries to continue running when app window is minimized or unfocused
- Existing safety mechanisms remain in place (timeout limits, minimum interval, no retry)
- Users have full control through autoQueryInterval setting
2025-11-08 00:02:28 +08:00
Jason
b617879035 docs: add v3.6.0 release notes and update READMEs with missing features
**Release Notes**
- Create Chinese v3.6.0 release notes in docs/ folder

**README Updates**
Add documentation for three missing v3.6.0 features:

1. Claude Configuration Data Structure Enhancements
   - Granular model configuration: migrated from dual-key to quad-key system
   - New fields: ANTHROPIC_DEFAULT_HAIKU_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL,
     ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_MODEL
   - Replaces legacy ANTHROPIC_SMALL_FAST_MODEL with automatic migration
   - Backend normalizes old configs with smart fallback chain
   - UI expanded from 2 to 4 model input fields

2. Updated Provider Models
   - Kimi: updated to latest kimi-k2-thinking model (from k1 series)
   - Removed legacy KimiModelSelector component

3. Custom Configuration Directory (Cloud Sync Support)
   - Customize CC Switch's configuration storage location
   - Point to cloud sync folders (Dropbox, OneDrive, iCloud, etc.)
     to enable automatic config synchronization across devices
   - Managed via Tauri Store for better isolation

**Files Changed**
- README.md & README_ZH.md: added feature documentation
- docs/release-note-v3.6.0-zh.md: comprehensive Chinese release notes
2025-11-07 22:05:33 +08:00
39 changed files with 2237 additions and 823 deletions

427
README.md
View File

@@ -1,8 +1,9 @@
# Claude Code & Codex Provider Switcher
<div align="center">
[![Version](https://img.shields.io/badge/version-3.6.0-blue.svg)](https://github.com/farion1231/cc-switch/releases)
# Claude Code & Codex Provider Switcher
[![Version](https://img.shields.io/badge/version-3.6.1-blue.svg)](https://github.com/farion1231/cc-switch/releases)
[![Trending](https://img.shields.io/badge/🔥_TypeScript_Trending-Daily%20%7C%20Weekly%20%7C%20Monthly-ff6b6b.svg)](https://github.com/trending/typescript)
[![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20macOS%20%7C%20Linux-lightgrey.svg)](https://github.com/farion1231/cc-switch/releases)
[![Built with Tauri](https://img.shields.io/badge/built%20with-Tauri%202-orange.svg)](https://tauri.app/)
@@ -12,7 +13,7 @@ A desktop application for managing and switching between different provider conf
</div>
## ❤️ Sponsor
## ❤Sponsor
![Zhipu GLM](assets/partners/banners/glm-en.jpg)
@@ -22,78 +23,49 @@ GLM CODING PLAN is a subscription service designed for AI coding, starting at ju
Get 10% OFF the GLM CODING PLAN with [this link](https://z.ai/subscribe?ic=8JVLJQFSKB)!
## Release Notes
---
> **v3.6.0**: Added edit mode (provider duplication, manual sorting), custom endpoint management, usage query features. Optimized config directory switching experience (perfect WSL environment support). Added multiple provider presets (DMXAPI, Azure Codex, AnyRouter, AiHubMix, MiniMax). Completed full-stack architecture refactoring and testing infrastructure.
> v3.5.0: Added MCP management, config import/export, endpoint speed testing. Complete i18n coverage. Added Longcat and kat-coder presets. Standardized release file naming conventions.
> v3.4.0: Added i18next internationalization, support for new models (qwen-3-max, GLM-4.6, DeepSeek-V3.2-Exp), Claude plugin, single-instance daemon, tray minimize, and installer optimizations.
> v3.3.0: One-click VS Code Codex plugin configuration/removal (auto-sync by default), Codex common config snippets, enhanced custom wizard, WSL environment support, cross-platform tray and UI optimizations. (VS Code write feature deprecated in v3.4.x)
> v3.2.0: Brand new UI, macOS system tray, built-in updater, atomic write with rollback, improved dark mode, Single Source of Truth (SSOT) with one-time migration/archival.
> v3.1.0: Added Codex provider management with one-click switching. Import current Codex config as default provider. Auto-backup before internal config v1 → v2 migration (see "Migration & Archival" below).
> v3.0.0 Major Update: Complete migration from Electron to Tauri 2.0. Significantly reduced app size and greatly improved startup performance.
## Features (v3.6.0)
### Core Features
- **MCP (Model Context Protocol) Management**: Complete MCP server configuration management system
- Support for stdio and http server types with command validation
- Built-in templates for popular MCP servers (e.g., mcp-fetch)
- Real-time enable/disable MCP servers with atomic file writes to prevent configuration corruption
- **Config Import/Export**: Backup and restore your provider configurations
- One-click export all configurations to JSON file
- Import configs with automatic validation and backup, auto-rotate backups (keep 10 most recent)
- Auto-sync to live config files after import to ensure immediate effect
- **Endpoint Speed Testing**: Test API endpoint response times
- Measure latency to different provider endpoints with visual connection quality indicators
- Help users choose the fastest provider
- **Internationalization & Language Switching**: Complete i18next i18n coverage (including error messages, tray menu, all UI components)
- **Claude Plugin Sync**: Built-in button to apply or restore Claude plugin configurations with one click. Takes effect immediately after switching providers.
### v3.6 New Features
- **Provider Duplication**: Quickly duplicate existing provider configs to easily create variants
- **Manual Sorting**: Drag and drop to manually reorder providers
- **Custom Endpoint Management**: Support multi-endpoint configuration for aggregator providers
- **Usage Query Features**
- Auto-refresh interval: Supports periodic automatic usage queries
- Test Script API: Validate JavaScript scripts before execution
- Template system expansion: Custom blank templates, support for access token and user ID parameters
- **Config Editor Improvements**
- Added JSON format button
- Real-time TOML syntax validation (for Codex configs)
- **Auto-sync on Directory Change**: When switching Claude/Codex config directories (e.g., switching to WSL environment), automatically sync current provider to new directory to avoid config file conflicts
- **Load Live Config When Editing Active Provider**: When editing the currently active provider, prioritize displaying the actual effective configuration to protect user manual modifications
- **New Provider Presets**: DMXAPI, Azure Codex, AnyRouter, AiHubMix, MiniMax
- **Partner Promotion Mechanism**: Support ecosystem partner promotion (e.g., Zhipu GLM Z.ai)
### v3.6 Architecture Improvements
- **Backend Refactoring**: Completed 5-phase refactoring (unified error handling → command layer split → integration tests → Service layer extraction → concurrency optimization)
- **Frontend Refactoring**: Completed 4-stage refactoring (test infrastructure → Hooks extraction → component splitting → code cleanup)
- **Testing System**: 100% Hooks unit test coverage, integration tests covering critical flows (vitest + MSW + @testing-library/react)
### System Features
- **System Tray & Window Behavior**: Window can minimize to tray, macOS supports hide/show Dock in tray mode, tray switching syncs Claude/Codex/plugin status.
- **Single Instance**: Ensures only one instance runs at a time to avoid multi-instance conflicts.
- **Standardized Release Naming**: All platform release files use consistent version-tagged naming (macOS: `.tar.gz` / `.zip`, Windows: `.msi` / `-Portable.zip`, Linux: `.AppImage` / `.deb`).
<table>
<tr>
<td width="180"><img src="assets/partners/logos/packycode.png" alt="PackyCode" width="150"></td>
<td>Thanks to PackyCode for sponsoring this project! PackyCode is a reliable and efficient API relay service provider, offering relay services for Claude Code, Codex, Gemini, and more. PackyCode provides special discounts for our software users: register using <a href="https://www.packyapi.com/register?aff=cc-switch">this link</a> and enter the "cc-switch" promo code during recharge to get 10% off.</td>
</tr>
</table>
## Screenshots
### Main Interface
| Main Interface | Add Provider |
| :-----------------------------------------------: | :--------------------------------------------: |
| ![Main Interface](assets/screenshots/main-en.png) | ![Add Provider](assets/screenshots/add-en.png) |
![Main Interface](assets/screenshots/main-en.png)
## Features
### Add Provider
### Current Version: v3.6.1 | [Full Changelog](CHANGELOG.md)
![Add Provider](assets/screenshots/add-en.png)
**Core Capabilities**
- **Provider Management**: One-click switching between Claude Code & Codex API configurations
- **MCP Integration**: Centralized MCP server management with stdio/http support and real-time sync
- **Speed Testing**: Measure API endpoint latency with visual quality indicators
- **Import/Export**: Backup and restore configs with auto-rotation (keep 10 most recent)
- **i18n Support**: Complete Chinese/English localization (UI, errors, tray)
- **Claude Plugin Sync**: One-click apply/restore Claude plugin configurations
**v3.6 Highlights**
- Provider duplication & drag-and-drop sorting
- Multi-endpoint management & custom config directory (cloud sync ready)
- Granular model configuration (4-tier: Haiku/Sonnet/Opus/Custom)
- WSL environment support with auto-sync on directory change
- 100% hooks test coverage & complete architecture refactoring
- New presets: DMXAPI, Azure Codex, AnyRouter, AiHubMix, MiniMax
**System Features**
- System tray with quick switching
- Single instance daemon
- Built-in auto-updater
- Atomic writes with rollback protection
## Download & Installation
@@ -132,148 +104,95 @@ Download `CC-Switch-v{version}-macOS.zip` from the [Releases](../../releases) pa
Download the latest `CC-Switch-v{version}-Linux.deb` package or `CC-Switch-v{version}-Linux.AppImage` from the [Releases](../../releases) page.
## Usage Guide
## Quick Start
1. Click "Add Provider" to add your API configuration
2. Switching methods:
- Select a provider on the main interface and click switch
- Or directly select target provider from "System Tray (Menu Bar)" for immediate effect
3. Switching will write to the corresponding app's "live config file" (Claude: `settings.json`; Codex: `auth.json` + `config.toml`)
4. Restart or open new terminal to ensure it takes effect
5. To switch back to official login, select "Official Login" from presets and switch; after restarting terminal, follow the official login process
### Basic Usage
### MCP Configuration Guide (v3.5.x)
1. **Add Provider**: Click "Add Provider" → Choose preset or create custom configuration
2. **Switch Provider**:
- Main UI: Select provider → Click "Enable"
- System Tray: Click provider name directly (instant effect)
3. **Takes Effect**: Restart terminal or Claude Code/Codex to apply changes
4. **Back to Official**: Select "Official Login" preset, restart terminal, then use `/login` (Claude) or official login flow (Codex)
- Management Location: All MCP server definitions are centrally saved in `~/.cc-switch/config.json` (categorized by client `claude` / `codex`)
- Sync Mechanism:
- Enabled Claude MCP servers are projected to `~/.claude.json` (path may vary with override directory)
- Enabled Codex MCP servers are projected to `~/.codex/config.toml`
- Validation & Normalization: Auto-validate field legality (stdio/http) when adding/importing, and auto-fix/populate keys like `id`
- Import Sources: Support importing from `~/.claude.json` and `~/.codex/config.toml`; existing entries only force `enabled=true`, don't override other fields
### MCP Management
### Check for Updates
- **Location**: Click "MCP" button in top-right corner
- **Add Server**: Use built-in templates (mcp-fetch, mcp-filesystem) or custom config
- **Enable/Disable**: Toggle switches to control which servers sync to live config
- **Sync**: Enabled servers auto-sync to `~/.claude.json` (Claude) or `~/.codex/config.toml` (Codex)
- Click "Check for Updates" in Settings. If built-in Updater config is available, it will detect and download directly; otherwise, it will fall back to opening the Releases page
### Configuration Files
### Codex Guide (SSOT)
**Claude Code**
- Config Directory: `~/.codex/`
- Live main config: `auth.json` (required), `config.toml` (can be empty)
- API Key Field: Uses `OPENAI_API_KEY` in `auth.json`
- Switching Behavior (no longer writes "copy files"):
- Provider configs are uniformly saved in `~/.cc-switch/config.json`
- When switching, writes target provider back to live files (`auth.json` + `config.toml`)
- Uses "atomic write + rollback on failure" to avoid half-written state; `config.toml` can be empty
- Import Default: When the app has no providers, creates a default entry from existing live main config and sets it as current
- Official Login: Can switch to preset "Codex Official Login", restart terminal and follow official login process
- Live config: `~/.claude/settings.json` (or `claude.json`)
- API key field: `env.ANTHROPIC_AUTH_TOKEN` or `env.ANTHROPIC_API_KEY`
- MCP servers: `~/.claude.json` `mcpServers`
### Claude Code Guide (SSOT)
**Codex**
- Config Directory: `~/.claude/`
- Live main config: `settings.json` (preferred) or legacy-compatible `claude.json`
- API Key Field: `env.ANTHROPIC_AUTH_TOKEN`
- Switching Behavior (no longer writes "copy files"):
- Provider configs are uniformly saved in `~/.cc-switch/config.json`
- When switching, writes target provider JSON directly to live file (preferring `settings.json`)
- When editing current provider, writes live first successfully, then updates app main config to ensure consistency
- Import Default: When the app has no providers, creates a default entry from existing live main config and sets it as current
- Official Login: Can switch to preset "Claude Official Login", restart terminal and use `/login` to complete login
- Live config: `~/.codex/auth.json` (required) + `config.toml` (optional)
- API key field: `OPENAI_API_KEY` in `auth.json`
- MCP servers: `~/.codex/config.toml``[mcp.servers]`
### Migration & Archival
**CC Switch Storage**
#### v3.6 Technical Improvements
- Main config (SSOT): `~/.cc-switch/config.json`
- Settings: `~/.cc-switch/settings.json`
- Backups: `~/.cc-switch/backups/` (auto-rotate, keep 10)
**Internal Optimizations (User Transparent)**:
### Cloud Sync Setup
- **Removed Legacy Migration Logic**: v3.6 removed v1 config auto-migration and copy file scanning logic
-**Impact**: Improved startup performance, cleaner code
-**Compatibility**: v2 format configs are fully compatible, no action required
- ⚠️ **Note**: Users upgrading from v3.1.0 or earlier should first upgrade to v3.2.x or v3.5.x for one-time migration, then upgrade to v3.6
1. Go to Settings → "Custom Configuration Directory"
2. Choose your cloud sync folder (Dropbox, OneDrive, iCloud, etc.)
3. Restart app to apply
4. Repeat on other devices to enable cross-device sync
- **Command Parameter Standardization**: Backend unified to use `app` parameter (values: `claude` or `codex`)
-**Impact**: More standardized code, friendlier error messages
-**Compatibility**: Frontend fully adapted, users don't need to care about this change
> **Note**: First launch auto-imports existing Claude/Codex configs as default provider.
#### Startup Failure & Recovery
## Architecture Overview
- Trigger Conditions: Triggered when `~/.cc-switch/config.json` doesn't exist, is corrupted, or fails to parse.
- User Action: Check JSON syntax according to popup prompt, or restore from backup files.
- Backup Location & Rotation: `~/.cc-switch/backups/backup_YYYYMMDD_HHMMSS.json` (keep up to 10, see `src-tauri/src/services/config.rs`).
- Exit Strategy: To protect data safety, the app will show a popup and force exit when the above errors occur; restart after fixing.
### Design Principles
#### Migration Mechanism (v3.2.0+)
```
┌─────────────────────────────────────────────────────────────┐
│ Frontend (React + TS) │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Components │ │ Hooks │ │ TanStack Query │ │
│ │ (UI) │──│ (Bus. Logic) │──│ (Cache/Sync) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
└────────────────────────┬────────────────────────────────────┘
│ Tauri IPC
┌────────────────────────▼────────────────────────────────────┐
│ Backend (Tauri + Rust) │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Commands │ │ Services │ │ Models/Config │ │
│ │ (API Layer) │──│ (Bus. Layer) │──│ (Data) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────┘
```
- One-time Migration: First launch of v3.2.0+ will scan old "copy files" and merge into `~/.cc-switch/config.json`
- Claude: `~/.claude/settings-*.json` (excluding `settings.json` / legacy `claude.json`)
- Codex: `~/.codex/auth-*.json` and `config-*.toml` (merged in pairs by name)
- Deduplication & Current Item: Deduplicate by "name (case-insensitive) + API Key"; if current is empty, set live merged item as current
- Archival & Cleanup:
- Archive directory: `~/.cc-switch/archive/<timestamp>/<category>/...`
- Delete original copies after successful archival; keep original files on failure (conservative strategy)
- v1 → v2 Structure Upgrade: Additionally generates `~/.cc-switch/config.v1.backup.<timestamp>.json` for rollback
- Note: After migration, daily switch/edit operations are no longer archived; prepare your own backup solution if long-term auditing is needed
**Core Design Patterns**
## Architecture Overview (v3.6)
- **SSOT** (Single Source of Truth): All provider configs stored in `~/.cc-switch/config.json`
- **Dual-way Sync**: Write to live files on switch, backfill from live when editing active provider
- **Atomic Writes**: Temp file + rename pattern prevents config corruption
- **Concurrency Safe**: RwLock with scoped guards avoids deadlocks
- **Layered Architecture**: Clear separation (Commands → Services → Models)
### Architecture Refactoring Highlights (v3.6)
**Key Components**
**Backend Refactoring (Rust)**: Completed 5-phase refactoring
- **ProviderService**: Provider CRUD, switching, backfill, sorting
- **McpService**: MCP server management, import/export, live file sync
- **ConfigService**: Config import/export, backup rotation
- **SpeedtestService**: API endpoint latency measurement
- **Phase 1**: Unified error handling (`AppError` + i18n error messages)
- **Phase 2**: Command layer split by domain (`commands/{provider,mcp,config,settings,plugin,misc}.rs`)
- **Phase 3**: Introduced integration tests and transaction mechanism (config snapshot + failure rollback)
- **Phase 4**: Extracted Service layer (`services/{provider,mcp,config,speedtest}.rs`)
- **Phase 5**: Concurrency optimization (`RwLock` instead of `Mutex`, scoped guard to avoid deadlock)
**v3.6 Refactoring**
**Frontend Refactoring (React + TypeScript)**: Completed 4-stage refactoring
- **Stage 1**: Established test infrastructure (vitest + MSW + @testing-library/react)
- **Stage 2**: Extracted custom hooks (`useProviderActions`, `useMcpActions`, `useSettings`, `useImportExport`, etc.)
- **Stage 3**: Component splitting and business logic extraction
- **Stage 4**: Code cleanup and formatting unification
**Test Coverage**:
- 100% Hooks unit test coverage
- Integration tests covering critical flows (App, SettingsDialog, MCP Panel)
- MSW mocking backend API to ensure test independence
### Layered Architecture
- **Frontend (Renderer)**
- Tech Stack: TypeScript + React 18 + Vite + TailwindCSS 4
- Data Layer: TanStack React Query unified queries and mutations (`@/lib/query`), Tauri API unified wrapper (`@/lib/api`)
- Business Logic Layer: Custom Hooks (`@/hooks`) carry domain logic, components stay simple
- Event Flow: Listen to backend `provider-switched` events, drive UI refresh and tray state consistency
- Organization: Components split by domain (`providers/settings/mcp/ui`)
- **Backend (Tauri + Rust)**
- **Commands Layer** (Interface Layer): `src-tauri/src/commands/*` split by domain, only responsible for parameter parsing and permission validation
- **Services Layer** (Business Layer): `src-tauri/src/services/*` carry core logic, reusable and testable
- `ProviderService`: Provider CRUD, switch, backfill, sorting
- `McpService`: MCP server management, import/export, sync
- `ConfigService`: Config file import/export, backup/restore
- `SpeedtestService`: API endpoint latency testing
- **Models & State**:
- `provider.rs`: Domain models (`Provider`, `ProviderManager`, `ProviderMeta`)
- `app_config.rs`: Multi-app config (`MultiAppConfig`, `AppId`, `McpRoot`)
- `store.rs`: Global state (`AppState` + `RwLock<MultiAppConfig>`)
- **Reliability**:
- Unified error type `AppError` (with localized messages)
- Transactional changes (config snapshot + failure rollback)
- Atomic writes (temp file + rename, avoid half-writes)
- Tray menu & events: Rebuild menu after switch and emit `provider-switched` event to frontend
- **Design Points (SSOT + Dual-way Sync)**
- **Single Source of Truth**: Provider configs centrally stored in `~/.cc-switch/config.json`
- **Write on Switch**: Write target provider config to live files (Claude: `settings.json`; Codex: `auth.json` + `config.toml`)
- **Backfill Mechanism**: Immediately read back live files after switch, update SSOT to protect user manual modifications
- **Directory Switch Sync**: Auto-sync current provider to new directory when changing config directories (perfect WSL environment support)
- **Prioritize Live When Editing**: When editing current provider, prioritize loading live config to ensure display of actually effective configuration
- **Compatibility & Changes**
- Command Parameters Unified: Tauri commands only accept `app` (values: `claude` / `codex`)
- Frontend Types Unified: Use `AppId` to express app identifiers (replacing legacy `AppType` export)
- Backend: 5-phase refactoring (error handling → command split → tests → services → concurrency)
- Frontend: 4-stage refactoring (test infra → hooks → components → cleanup)
- Testing: 100% hooks coverage + integration tests (vitest + MSW)
## Development
@@ -346,12 +265,12 @@ cargo test --features test-hooks
**Test Coverage**:
- Hooks unit tests (100% coverage)
- Hooks unit tests (100% coverage)
- `useProviderActions` - Provider operations
- `useMcpActions` - MCP management
- `useSettings` series - Settings management
- `useImportExport` - Import/export
- Integration tests
- Integration tests
- App main application flow
- SettingsDialog complete interaction
- MCP panel functionality
@@ -371,120 +290,36 @@ pnpm test:unit --coverage
## Tech Stack
### Frontend
**Frontend**: React 18 · TypeScript · Vite · TailwindCSS 4 · TanStack Query v5 · react-i18next · react-hook-form · zod · shadcn/ui · @dnd-kit
- **[React 18](https://react.dev/)** - User interface library
- **[TypeScript](https://www.typescriptlang.org/)** - Type-safe JavaScript
- **[Vite](https://vitejs.dev/)** - Lightning fast frontend build tool
- **[TailwindCSS 4](https://tailwindcss.com/)** - Utility-first CSS framework
- **[TanStack Query v5](https://tanstack.com/query/latest)** - Powerful data fetching and caching
- **[react-i18next](https://react.i18next.com/)** - React internationalization framework
- **[react-hook-form](https://react-hook-form.com/)** - High-performance forms library
- **[zod](https://zod.dev/)** - TypeScript-first schema validation
- **[shadcn/ui](https://ui.shadcn.com/)** - Reusable React components
- **[@dnd-kit](https://dndkit.com/)** - Modern drag and drop toolkit
**Backend**: Tauri 2.8 · Rust · serde · tokio · thiserror · tauri-plugin-updater/process/dialog/store/log
### Backend
- **[Tauri 2.8](https://tauri.app/)** - Cross-platform desktop app framework
- tauri-plugin-updater - Auto update
- tauri-plugin-process - Process management
- tauri-plugin-dialog - File dialogs
- tauri-plugin-store - Persistent storage
- tauri-plugin-log - Logging
- **[Rust](https://www.rust-lang.org/)** - Systems programming language
- **[serde](https://serde.rs/)** - Serialization/deserialization framework
- **[tokio](https://tokio.rs/)** - Async runtime
- **[thiserror](https://github.com/dtolnay/thiserror)** - Error handling derive macro
### Testing Tools
- **[vitest](https://vitest.dev/)** - Fast unit testing framework
- **[MSW](https://mswjs.io/)** - API mocking tool
- **[@testing-library/react](https://testing-library.com/react)** - React testing utilities
**Testing**: vitest · MSW · @testing-library/react
## Project Structure
```
├── src/ # Frontend code (React + TypeScript)
│ ├── components/ # React components
│ ├── providers/ # Provider management components
│ │ │ ├── forms/ # Form sub-components (Claude/Codex fields)
│ │ │ ├── ProviderList.tsx
│ │ │ ├── ProviderForm.tsx
│ │ │ ├── AddProviderDialog.tsx
│ │ │ └── EditProviderDialog.tsx
│ │ ├── settings/ # Settings related components
│ │ │ ├── SettingsDialog.tsx
│ │ │ ├── DirectorySettings.tsx
│ │ │ └── ImportExportSection.tsx
│ │ ├── mcp/ # MCP management components
│ │ │ ├── McpPanel.tsx
│ │ │ ├── McpFormModal.tsx
│ │ │ └── McpWizard.tsx
│ │ └── ui/ # shadcn/ui base components
│ ├── hooks/ # Custom Hooks (business logic layer)
│ │ ├── useProviderActions.ts # Provider operations
│ │ ├── useMcpActions.ts # MCP operations
│ │ ├── useSettings.ts # Settings management
│ │ ├── useImportExport.ts # Import/export
│ │ └── useDirectorySettings.ts # Directory config
├── src/ # Frontend (React + TypeScript)
│ ├── components/ # UI components (providers/settings/mcp/ui)
│ ├── hooks/ # Custom hooks (business logic)
│ ├── lib/
│ │ ├── api/ # Tauri API wrapper (type-safe)
│ │ │ ├── providers.ts # Provider API
│ │ │ ├── settings.ts # Settings API
│ │ │ ├── mcp.ts # MCP API
│ │ │ └── usage.ts # Usage query API
│ │ └── query/ # TanStack Query config
│ ├── queries.ts # Query definitions
│ ├── mutations.ts # Mutation definitions
│ └── queryClient.ts
│ ├── i18n/ # Internationalization resources
│ └── locales/
│ ├── zh/ # Chinese translations
── en/ # English translations
├── config/ # Config & presets
├── claudeProviderPresets.ts # Claude provider presets
├── codexProviderPresets.ts # Codex provider presets
└── mcpPresets.ts # MCP server templates
│ ├── utils/ # Utility functions
│ ├── postChangeSync.ts # Config sync utility
│ └── ...
│ └── types/ # TypeScript type definitions
├── src-tauri/ # Backend code (Rust)
│ ├── src/
│ │ ├── commands/ # Tauri command layer (split by domain)
│ │ │ ├── provider.rs # Provider commands
│ │ │ ├── mcp.rs # MCP commands
│ │ │ ├── config.rs # Config query commands
│ │ │ ├── settings.rs # Settings commands
│ │ │ ├── plugin.rs # Plugin commands
│ │ │ ├── import_export.rs # Import/export commands
│ │ │ └── misc.rs # Misc commands
│ │ ├── services/ # Service layer (business logic)
│ │ │ ├── provider.rs # ProviderService
│ │ │ ├── mcp.rs # McpService
│ │ │ ├── config.rs # ConfigService
│ │ │ └── speedtest.rs # SpeedtestService
│ │ ├── app_config.rs # Config data models
│ │ ├── provider.rs # Provider domain models
│ │ ├── store.rs # Global state management
│ │ ├── mcp.rs # MCP sync & validation
│ │ ├── error.rs # Unified error type
│ │ ├── usage_script.rs # Usage script execution
│ │ ├── claude_plugin.rs # Claude plugin management
│ │ └── lib.rs # App entry point
│ ├── capabilities/ # Tauri permission config
│ └── icons/ # App icons
├── tests/ # Frontend tests (v3.6 new)
│ ├── hooks/ # Hooks unit tests
│ ├── components/ # Component integration tests
│ └── setup.ts # Test config
└── assets/ # Static resources
├── screenshots/ # Interface screenshots
└── partners/ # Partner resources
├── logos/ # Partner logos
└── banners/ # Partner banners/promotional images
├── i18n/locales/ # Translations (zh/en)
├── config/ # Presets (providers/mcp)
└── types/ # TypeScript definitions
├── src-tauri/ # Backend (Rust)
│ └── src/
│ ├── commands/ # Tauri command layer (by domain)
── services/ # Business logic layer
├── app_config.rs # Config data models
├── provider.rs # Provider domain models
├── mcp.rs # MCP sync & validation
└── lib.rs # App entry & tray menu
├── tests/ # Frontend tests
│ ├── hooks/ # Unit tests
│ └── components/ # Integration tests
└── assets/ # Screenshots & partner resources
```
## Changelog
@@ -506,7 +341,7 @@ Before submitting PRs, please ensure:
- Pass type check: `pnpm typecheck`
- Pass format check: `pnpm format:check`
- Pass unit tests: `pnpm test:unit`
- Functional PRs should be discussed in the issue area first
- 💡 For new features, please open an issue for discussion before submitting a PR
## Star History

View File

@@ -1,8 +1,9 @@
# Claude Code & Codex 供应商管理器
<div align="center">
[![Version](https://img.shields.io/badge/version-3.6.0-blue.svg)](https://github.com/farion1231/cc-switch/releases)
# Claude Code & Codex 供应商管理器
[![Version](https://img.shields.io/badge/version-3.6.1-blue.svg)](https://github.com/farion1231/cc-switch/releases)
[![Trending](https://img.shields.io/badge/🔥_TypeScript_Trending-Daily%20%7C%20Weekly%20%7C%20Monthly-ff6b6b.svg)](https://github.com/trending/typescript)
[![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20macOS%20%7C%20Linux-lightgrey.svg)](https://github.com/farion1231/cc-switch/releases)
[![Built with Tauri](https://img.shields.io/badge/built%20with-Tauri%202-orange.svg)](https://tauri.app/)
@@ -12,88 +13,59 @@
</div>
## ❤️ 赞助商
## ❤️赞助商
![智谱 GLM](assets/partners/banners/glm-zh.jpg)
感谢智谱AI的 GLM CODING PLAN 赞助了本项目!
GLM CODING PLAN 是专为AI编码打造的订阅套餐每月最低仅需20元即可在十余款主流AI编码工具如 Claude Code、Cline 中畅享智谱旗舰模型 GLM-4.6,为开发者提供顶尖、高速、稳定的编码体验。
GLM CODING PLAN 是专为AI编码打造的订阅套餐,每月最低仅需20元即可在十余款主流AI编码工具如 Claude Code、Cline 中畅享智谱旗舰模型 GLM-4.6,为开发者提供顶尖、高速、稳定的编码体验。
CC Switch 已经预设了智谱GLM只需要填写 key 即可一键导入编程工具。智谱AI为本软件的用户提供了特别优惠使用[此链接](https://www.bigmodel.cn/claude-code?ic=RRVJPB5SII)购买可以享受九折优惠。
## 更新记录
---
> **v3.6.0** 新增编辑模式供应商复制、手动排序、自定义端点管理、使用量查询等功能优化配置目录切换体验WSL 环境完美支持新增多个供应商预设DMXAPI、Azure Codex、AnyRouter、AiHubMix、MiniMax完成全栈架构重构和测试体系建设。
> v3.5.0 :新增 MCP 管理、配置导入/导出、端点速度测试功能,完善国际化覆盖,新增 Longcat、kat-coder 预设,标准化发布文件命名规范。
> v3.4.0 :新增 i18next 国际化、对新模型qwen-3-max, GLM-4.6, DeepSeek-V3.2-Exp的支持、Claude 插件、单实例守护、托盘最小化及安装器优化等。
> v3.3.0 VS Code Codex 插件一键配置/移除默认自动同步、Codex 通用配置片段与自定义向导增强、WSL 环境支持、跨平台托盘与 UI 优化。(该 VS Code 写入功能已在 v3.4.x 停用)
> v3.2.0 :全新 UI、macOS系统托盘、内置更新器、原子写入与回滚、改进暗色样式、单一事实源SSOT与一次性迁移/归档。
> v3.1.0 :新增 Codex 供应商管理与一键切换,支持导入当前 Codex 配置为默认供应商,并在内部配置从 v1 → v2 迁移前自动备份(详见下文“迁移与归档”)。
> v3.0.0 重大更新:从 Electron 完全迁移到 Tauri 2.0,应用体积显著降低、启动性能大幅提升。
## 功能特性v3.6.0
### 核心功能
- **MCP (Model Context Protocol) 管理**:完整的 MCP 服务器配置管理系统
- 支持 stdio 和 http 服务器类型,并提供命令校验
- 内置常用 MCP 服务器模板(如 mcp-fetch 等)
- 实时启用/禁用 MCP 服务器,原子文件写入防止配置损坏
- **配置导入/导出**:备份和恢复你的供应商配置
- 一键导出所有配置到 JSON 文件
- 导入配置时自动验证并备份,自动轮换备份(保留最近 10 个)
- 导入后自动同步到 live 配置文件,确保立即生效
- **端点速度测试**:测试 API 端点响应时间
- 测量不同供应商端点的延迟,可视化连接质量指示器
- 帮助用户选择最快的供应商
- **国际化与语言切换**:完整的 i18next 国际化覆盖(包含错误消息、托盘菜单、所有 UI 组件)
- **Claude 插件同步**:内置按钮可一键应用或恢复 Claude 插件配置,切换供应商后立即生效。
### v3.6 新增功能
- **供应商复制功能**:快速复制现有供应商配置,轻松创建变体配置
- **手动排序功能**:通过拖拽来对供应商进行手动排序
- **自定义端点管理**:支持聚合类供应商的多端点配置
- **使用量查询功能**
- 自动刷新间隔:支持定时自动查询使用量
- 测试脚本 API测试 JavaScript 脚本是否正确
- 模板系统扩展:自定义空白模板、支持 access token 和 user ID 参数
- **配置编辑器改进**
- 新增 JSON 格式化按钮
- 实时 TOML 语法验证Codex 配置)
- **配置目录切换自动同步**:切换 Claude/Codex 配置目录(如切换到 WSL 环境)时,自动同步当前供应商到新目录,避免冲突导致配置文件混乱
- **编辑当前供应商时加载 live 配置**:编辑正在使用的供应商时,优先显示实际生效的配置,保护用户手动修改
- **新增供应商预设**DMXAPI、Azure Codex、AnyRouter、AiHubMix、MiniMax
- **合作伙伴推广机制**:支持生态合作伙伴推广(如智谱 GLM Z.ai
### v3.6 架构改进
- **后端重构**:完成 5 阶段重构(统一错误处理 → 命令层拆分 → 集成测试 → Service 层提取 → 并发优化)
- **前端重构**:完成 4 阶段重构(测试基础设施 → Hooks 提取 → 组件拆分 → 代码清理)
- **测试体系**Hooks 单元测试 100% 覆盖集成测试覆盖关键流程vitest + MSW + @testing-library/react
### 系统功能
- **系统托盘与窗口行为**窗口关闭可最小化到托盘macOS 支持托盘模式下隐藏/显示 Dock托盘切换时同步 Claude/Codex/插件状态。
- **单实例**:保证同一时间仅运行一个实例,避免多开冲突。
- **标准化发布命名**所有平台发布文件使用一致的版本标签命名macOS: `.tar.gz` / `.zip`Windows: `.msi` / `-Portable.zip`Linux: `.AppImage` / `.deb`)。
<table>
<tr>
<td width="180"><img src="assets/partners/logos/packycode.png" alt="PackyCode" width="150"></td>
<td>感谢 PackyCode 赞助了本项目PackyCode 是一家稳定、高效的API中转服务商提供 Claude Code、Codex、Gemini 等多种中转服务。PackyCode 为本软件的用户提供了特别优惠,使用<a href="https://www.packyapi.com/register?aff=cc-switch">此链接</a>注册并在充值时填写"cc-switch"优惠码可以享受9折优惠。</td>
</tr>
</table>
## 界面预览
### 主界面
| 主界面 | 添加供应商 |
| :---------------------------------------: | :------------------------------------------: |
| ![主界面](assets/screenshots/main-zh.png) | ![添加供应商](assets/screenshots/add-zh.png) |
![主界面](assets/screenshots/main-zh.png)
## 功能特性
### 添加供应商
### 当前版本v3.6.1 | [完整更新日志](CHANGELOG.md)
![添加供应商](assets/screenshots/add-zh.png)
**核心功能**
- **供应商管理**:一键切换 Claude Code 与 Codex 的 API 配置
- **MCP 集成**:集中管理 MCP 服务器,支持 stdio/http 类型和实时同步
- **速度测试**:测量 API 端点延迟,可视化连接质量指示器
- **导入导出**:备份和恢复配置,自动轮换(保留最近 10 个)
- **国际化支持**完整的中英文本地化UI、错误、托盘
- **Claude 插件同步**:一键应用或恢复 Claude 插件配置
**v3.6 亮点**
- 供应商复制 & 拖拽排序
- 多端点管理 & 自定义配置目录(支持云同步)
- 细粒度模型配置四层Haiku/Sonnet/Opus/自定义)
- WSL 环境支持,配置目录切换自动同步
- 100% hooks 测试覆盖 & 完整架构重构
- 新增预设DMXAPI、Azure Codex、AnyRouter、AiHubMix、MiniMax
**系统功能**
- 系统托盘快速切换
- 单实例守护
- 内置自动更新器
- 原子写入与回滚保护
## 下载安装
@@ -132,148 +104,95 @@ brew upgrade --cask cc-switch
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch-v{版本号}-Linux.deb` 包或者 `CC-Switch-v{版本号}-Linux.AppImage` 安装包。
## 使用说明
## 快速开始
1. 点击"添加供应商"添加你的 API 配置
2. 切换方式:
- 在主界面选择供应商后点击切换
- 或通过“系统托盘(菜单栏)”直接选择目标供应商,立即生效
3. 切换会写入对应应用的“live 配置文件”Claude`settings.json`Codex`auth.json` + `config.toml`
4. 重启或新开终端以确保生效
5. 若需切回官方登录,在预设中选择“官方登录”并切换即可;重启终端后按官方流程登录
### 基本使用
### MCP 配置说明v3.5.x
1. **添加供应商**:点击"添加供应商" → 选择预设或创建自定义配置
2. **切换供应商**
- 主界面:选择供应商 → 点击"启用"
- 系统托盘:直接点击供应商名称(立即生效)
3. **生效方式**:重启终端或 Claude Code/Codex 以应用更改
4. **恢复官方登录**:选择"官方登录"预设,重启终端后使用 `/login`Claude或官方登录流程Codex
- 管理位置:所有 MCP 服务器定义集中保存在 `~/.cc-switch/config.json`(按客户端 `claude` / `codex` 分类)
- 同步机制:
- 启用的 Claude MCP 会投影到 `~/.claude.json`(路径可随覆盖目录而变化)
- 启用的 Codex MCP 会投影到 `~/.codex/config.toml`
- 校验与归一化:新增/导入时自动校验字段合法性stdio/http并自动修复/填充 `id` 等键名
- 导入来源:支持从 `~/.claude.json``~/.codex/config.toml` 导入;已存在条目只强制 `enabled=true`,不覆盖其他字段
### MCP 管理
### 检查更新
- **位置**:点击右上角"MCP"按钮
- **添加服务器**使用内置模板mcp-fetch、mcp-filesystem或自定义配置
- **启用/禁用**:切换开关以控制哪些服务器同步到 live 配置
- **同步**:启用的服务器自动同步到 `~/.claude.json`Claude`~/.codex/config.toml`Codex
- 在“设置”中点击“检查更新”,若内置 Updater 配置可用将直接检测与下载;否则会回退打开 Releases 页面
### 配置文件
### Codex 说明SSOT
**Claude Code**
- 配置目录:`~/.codex/`
- live 主配置:`auth.json`(必需)、`config.toml`(可为空)
- API Key 字段:`auth.json` 中使用 `OPENAI_API_KEY`
- 切换行为(不再写“副本文件”):
- 供应商配置统一保存在 `~/.cc-switch/config.json`
- 切换时将目标供应商写回 live 文件(`auth.json` + `config.toml`
- 采用“原子写入 + 失败回滚”,避免半写状态;`config.toml` 可为空
- 导入默认:当该应用无任何供应商时,从现有 live 主配置创建一条默认项并设为当前
- 官方登录可切换到预设“Codex 官方登录”,重启终端后按官方流程登录
- Live 配置:`~/.claude/settings.json`(或 `claude.json`
- API key 字段:`env.ANTHROPIC_AUTH_TOKEN``env.ANTHROPIC_API_KEY`
- MCP 服务器:`~/.claude.json``mcpServers`
### Claude Code 说明SSOT
**Codex**
- 配置目录:`~/.claude/`
- live 主配置:`settings.json`(优先)或历史兼容 `claude.json`
- API Key 字段:`env.ANTHROPIC_AUTH_TOKEN`
- 切换行为(不再写“副本文件”):
- 供应商配置统一保存在 `~/.cc-switch/config.json`
- 切换时将目标供应商 JSON 直接写入 live 文件(优先 `settings.json`
- 编辑当前供应商时,先写 live 成功,再更新应用主配置,保证一致性
- 导入默认:当该应用无任何供应商时,从现有 live 主配置创建一条默认项并设为当前
- 官方登录可切换到预设“Claude 官方登录”,重启终端后可使用 `/login` 完成登录
- Live 配置:`~/.codex/auth.json`(必需)+ `config.toml`(可选)
- API key 字段:`auth.json` 中的 `OPENAI_API_KEY`
- MCP 服务器:`~/.codex/config.toml``[mcp.servers]`
### 迁移与归档
**CC Switch 存储**
#### v3.6 技术改进
- 主配置SSOT`~/.cc-switch/config.json`
- 设置:`~/.cc-switch/settings.json`
- 备份:`~/.cc-switch/backups/`(自动轮换,保留 10 个)
**内部优化(用户无感知)**
### 云同步设置
- **移除遗留迁移逻辑**v3.6 移除了 v1 配置自动迁移和副本文件扫描逻辑
-**影响**:启动性能提升,代码更简洁
-**兼容性**v2 格式配置完全兼容,无需任何操作
- ⚠️ **注意**:从 v3.1.0 或更早版本升级的用户,请先升级到 v3.2.x 或 v3.5.x 完成一次性迁移,再升级到 v3.6
1. 前往设置 → "自定义配置目录"
2. 选择您的云同步文件夹Dropbox、OneDrive、iCloud、坚果云等
3. 重启应用以应用
4. 在其他设备上重复操作以启用跨设备同步
- **命令参数标准化**:后端统一使用 `app` 参数(取值:`claude``codex`
-**影响**:代码更规范,错误提示更友好
-**兼容性**:前端已完全适配,用户无需关心此变更
> **注意**:首次启动会自动导入现有 Claude/Codex 配置作为默认供应商。
#### 启动失败与恢复
## 架构总览
- 触发条件:`~/.cc-switch/config.json` 不存在、损坏或解析失败时触发。
- 用户动作:根据弹窗提示检查 JSON 语法,或从备份文件恢复。
- 备份位置与轮换:`~/.cc-switch/backups/backup_YYYYMMDD_HHMMSS.json`(最多保留 10 个,参见 `src-tauri/src/services/config.rs`)。
- 退出策略:为保护数据安全,出现上述错误时应用会弹窗提示并强制退出;修复后重新启动即可。
### 设计原则
#### v3.2.0 起的迁移机制
```
┌─────────────────────────────────────────────────────────────┐
│ 前端 (React + TS) │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Components │ │ Hooks │ │ TanStack Query │ │
│ │ (UI) │──│ (业务逻辑) │──│ (缓存/同步) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
└────────────────────────┬────────────────────────────────────┘
│ Tauri IPC
┌────────────────────────▼────────────────────────────────────┐
│ 后端 (Tauri + Rust) │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Commands │ │ Services │ │ Models/Config │ │
│ │ (API 层) │──│ (业务层) │──│ (数据) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────┘
```
- 一次性迁移:首次启动 3.2.0 及以上版本会扫描旧的"副本文件"并合并到 `~/.cc-switch/config.json`
- Claude`~/.claude/settings-*.json`(排除 `settings.json` / 历史 `claude.json`
- Codex`~/.codex/auth-*.json``config-*.toml`(按名称成对合并)
- 去重与当前项:按"名称(忽略大小写)+ API Key"去重;若当前为空,将 live 合并项设为当前
- 归档与清理:
- 归档目录:`~/.cc-switch/archive/<timestamp>/<category>/...`
- 归档成功后删除原副本;失败则保留原文件(保守策略)
- v1 → v2 结构升级:会额外生成 `~/.cc-switch/config.v1.backup.<timestamp>.json` 以便回滚
- 注意:迁移后不再持续归档日常切换/编辑操作,如需长期审计请自备备份方案
**核心设计模式**
## 架构总览v3.6
- **SSOT**(单一事实源):所有供应商配置存储在 `~/.cc-switch/config.json`
- **双向同步**:切换时写入 live 文件,编辑当前供应商时从 live 回填
- **原子写入**:临时文件 + 重命名模式防止配置损坏
- **并发安全**RwLock 与作用域守卫避免死锁
- **分层架构**清晰分离Commands → Services → Models
### 架构重构亮点v3.6
**核心组件**
**后端重构Rust**:完成 5 阶段重构
- **ProviderService**:供应商增删改查、切换、回填、排序
- **McpService**MCP 服务器管理、导入导出、live 文件同步
- **ConfigService**:配置导入导出、备份轮换
- **SpeedtestService**API 端点延迟测量
- **Phase 1**:统一错误处理(`AppError` + 国际化错误消息)
- **Phase 2**:命令层按领域拆分(`commands/{provider,mcp,config,settings,plugin,misc}.rs`
- **Phase 3**:引入集成测试和事务机制(配置快照 + 失败回滚)
- **Phase 4**:提取 Service 层(`services/{provider,mcp,config,speedtest}.rs`
- **Phase 5**:并发优化(`RwLock` 替代 `Mutex`,作用域 guard 避免死锁)
**v3.6 重构**
**前端重构React + TypeScript**:完成 4 阶段重构
- **Stage 1**建立测试基础设施vitest + MSW + @testing-library/react
- **Stage 2**:提取自定义 hooks`useProviderActions`, `useMcpActions`, `useSettings`, `useImportExport` 等)
- **Stage 3**:组件拆分和业务逻辑提取
- **Stage 4**:代码清理和格式化统一
**测试覆盖**
- Hooks 单元测试 100% 覆盖
- 集成测试覆盖关键流程App、SettingsDialog、MCP 面板)
- MSW 模拟后端 API确保测试独立性
### 分层架构
- **前端Renderer**
- 技术栈TypeScript + React 18 + Vite + TailwindCSS 4
- 数据层TanStack React Query 统一查询与变更(`@/lib/query`Tauri API 统一封装(`@/lib/api`
- 业务逻辑层:自定义 Hooks`@/hooks`)承载领域逻辑,组件保持简洁
- 事件流:监听后端 `provider-switched` 事件,驱动 UI 刷新与托盘状态一致
- 组织结构:按领域拆分组件(`providers/settings/mcp/ui`
- **后端Tauri + Rust**
- **Commands 层**(接口层):`src-tauri/src/commands/*` 按领域拆分,仅负责参数解析和权限校验
- **Services 层**(业务层):`src-tauri/src/services/*` 承载核心逻辑,可复用和测试
- `ProviderService`:供应商增删改查、切换、回填、排序
- `McpService`MCP 服务器管理、导入导出、同步
- `ConfigService`:配置文件导入导出、备份恢复
- `SpeedtestService`API 端点延迟测试
- **模型与状态**
- `provider.rs`:领域模型(`Provider`, `ProviderManager`, `ProviderMeta`
- `app_config.rs`:多应用配置(`MultiAppConfig`, `AppId`, `McpRoot`
- `store.rs`:全局状态(`AppState` + `RwLock<MultiAppConfig>`
- **可靠性**
- 统一错误类型 `AppError`(包含本地化消息)
- 事务式变更(配置快照 + 失败回滚)
- 原子写入(临时文件 + 重命名,避免半写入)
- 托盘菜单与事件:切换后重建菜单并向前端发射 `provider-switched` 事件
- **设计要点SSOT + 双向同步)**
- **单一事实源**:供应商配置集中存放于 `~/.cc-switch/config.json`
- **切换时写入**:将目标供应商配置写入 live 文件Claude: `settings.json`Codex: `auth.json` + `config.toml`
- **回填机制**:切换后立即读回 live 文件,更新 SSOT保护用户手动修改
- **目录切换同步**修改配置目录时自动同步当前供应商到新目录WSL 环境完美支持)
- **编辑时优先 live**:编辑当前供应商时,优先加载 live 配置,确保显示实际生效的配置
- **兼容性与变更**
- 命令参数统一Tauri 命令仅接受 `app`(值为 `claude` / `codex`
- 前端类型统一:使用 `AppId` 表达应用标识(替代历史 `AppType` 导出)
- 后端5 阶段重构(错误处理 → 命令拆分 → 测试 → 服务 → 并发)
- 前端4 阶段重构(测试基础 → hooks → 组件 → 清理)
- 测试100% hooks 覆盖 + 集成测试vitest + MSW
## 开发
@@ -346,12 +265,12 @@ cargo test --features test-hooks
**测试覆盖**
- Hooks 单元测试100% 覆盖)
- Hooks 单元测试100% 覆盖)
- `useProviderActions` - 供应商操作
- `useMcpActions` - MCP 管理
- `useSettings` 系列 - 设置管理
- `useImportExport` - 导入导出
- 集成测试
- 集成测试
- App 主应用流程
- SettingsDialog 完整交互
- MCP 面板功能
@@ -371,120 +290,36 @@ pnpm test:unit --coverage
## 技术栈
### 前端
**前端**React 18 · TypeScript · Vite · TailwindCSS 4 · TanStack Query v5 · react-i18next · react-hook-form · zod · shadcn/ui · @dnd-kit
- **[React 18](https://react.dev/)** - 用户界面库
- **[TypeScript](https://www.typescriptlang.org/)** - 类型安全的 JavaScript
- **[Vite](https://vitejs.dev/)** - 极速的前端构建工具
- **[TailwindCSS 4](https://tailwindcss.com/)** - 实用优先的 CSS 框架
- **[TanStack Query v5](https://tanstack.com/query/latest)** - 强大的数据获取与缓存
- **[react-i18next](https://react.i18next.com/)** - React 国际化框架
- **[react-hook-form](https://react-hook-form.com/)** - 高性能表单库
- **[zod](https://zod.dev/)** - TypeScript 优先的模式验证
- **[shadcn/ui](https://ui.shadcn.com/)** - 可复用的 React 组件
- **[@dnd-kit](https://dndkit.com/)** - 现代拖拽工具包
**后端**Tauri 2.8 · Rust · serde · tokio · thiserror · tauri-plugin-updater/process/dialog/store/log
### 后端
- **[Tauri 2.8](https://tauri.app/)** - 跨平台桌面应用框架
- tauri-plugin-updater - 自动更新
- tauri-plugin-process - 进程管理
- tauri-plugin-dialog - 文件对话框
- tauri-plugin-store - 持久化存储
- tauri-plugin-log - 日志记录
- **[Rust](https://www.rust-lang.org/)** - 系统级编程语言
- **[serde](https://serde.rs/)** - 序列化/反序列化框架
- **[tokio](https://tokio.rs/)** - 异步运行时
- **[thiserror](https://github.com/dtolnay/thiserror)** - 错误处理派生宏
### 测试工具
- **[vitest](https://vitest.dev/)** - 快速的单元测试框架
- **[MSW](https://mswjs.io/)** - API mock 工具
- **[@testing-library/react](https://testing-library.com/react)** - React 测试工具
**测试**vitest · MSW · @testing-library/react
## 项目结构
```
├── src/ # 前端代码 (React + TypeScript)
│ ├── components/ # React 组件
│ ├── providers/ # 供应商管理组件
│ │ │ ├── forms/ # 表单子组件Claude/Codex 字段)
│ │ │ ├── ProviderList.tsx
│ │ │ ├── ProviderForm.tsx
│ │ │ ├── AddProviderDialog.tsx
│ │ │ └── EditProviderDialog.tsx
│ │ ├── settings/ # 设置相关组件
│ │ │ ├── SettingsDialog.tsx
│ │ │ ├── DirectorySettings.tsx
│ │ │ └── ImportExportSection.tsx
│ │ ├── mcp/ # MCP 管理组件
│ │ │ ├── McpPanel.tsx
│ │ │ ├── McpFormModal.tsx
│ │ │ └── McpWizard.tsx
│ │ └── ui/ # shadcn/ui 基础组件
│ ├── hooks/ # 自定义 Hooks业务逻辑层
│ │ ├── useProviderActions.ts # 供应商操作
│ │ ├── useMcpActions.ts # MCP 操作
│ │ ├── useSettings.ts # 设置管理
│ │ ├── useImportExport.ts # 导入导出
│ │ └── useDirectorySettings.ts # 目录配置
├── src/ # 前端 (React + TypeScript)
│ ├── components/ # UI 组件 (providers/settings/mcp/ui)
│ ├── hooks/ # 自定义 hooks (业务逻辑)
│ ├── lib/
│ │ ├── api/ # Tauri API 封装(类型安全)
│ │ │ ├── providers.ts # 供应商 API
│ │ │ ├── settings.ts # 设置 API
│ │ │ ├── mcp.ts # MCP API
│ │ │ └── usage.ts # 用量查询 API
│ │ └── query/ # TanStack Query 配置
│ ├── queries.ts # 查询定义
│ ├── mutations.ts # 变更定义
│ │ └── queryClient.ts
│ ├── i18n/ # 国际化资源
│ │ └── locales/
│ │ ├── zh/ # 中文翻译
│ │ └── en/ # 英文翻译
│ ├── config/ # 配置与预设
│ │ ├── claudeProviderPresets.ts # Claude 供应商预设
│ │ ├── codexProviderPresets.ts # Codex 供应商预设
│ │ └── mcpPresets.ts # MCP 服务器模板
│ ├── utils/ # 工具函数
│ │ ├── postChangeSync.ts # 配置同步工具
│ │ └── ...
├── i18n/locales/ # 翻译 (zh/en)
├── config/ # 预设 (providers/mcp)
│ └── types/ # TypeScript 类型定义
├── src-tauri/ # 后端代码 (Rust)
── src/
├── commands/ # Tauri 命令层(按领域拆分
│ │ ├── provider.rs # 供应商命令
│ │ ├── mcp.rs # MCP 命令
│ │ ├── config.rs # 配置查询命令
│ │ ├── settings.rs # 设置命令
│ ├── plugin.rs # 插件命令
├── import_export.rs # 导入导出命令
└── misc.rs # 杂项命令
│ ├── services/ # Service 层(业务逻辑)
├── provider.rs # ProviderService
│ │ │ ├── mcp.rs # McpService
│ │ │ ├── config.rs # ConfigService
│ │ │ └── speedtest.rs # SpeedtestService
│ │ ├── app_config.rs # 配置数据模型
│ │ ├── provider.rs # 供应商领域模型
│ │ ├── store.rs # 全局状态管理
│ │ ├── mcp.rs # MCP 同步与校验
│ │ ├── error.rs # 统一错误类型
│ │ ├── usage_script.rs # 用量脚本执行
│ │ ├── claude_plugin.rs # Claude 插件管理
│ │ └── lib.rs # 应用入口
│ ├── capabilities/ # Tauri 权限配置
│ └── icons/ # 应用图标
├── tests/ # 前端测试v3.6 新增)
│ ├── hooks/ # Hooks 单元测试
│ ├── components/ # 组件集成测试
│ └── setup.ts # 测试配置
└── assets/ # 静态资源
├── screenshots/ # 界面截图
└── partners/ # 合作商资源
├── logos/ # 合作商 Logo
└── banners/ # 合作商横幅/宣传图
├── src-tauri/ # 后端 (Rust)
── src/
├── commands/ # Tauri 命令层(按领域)
├── services/ # 业务逻辑层
├── app_config.rs # 配置数据模型
├── provider.rs # 供应商领域模型
├── mcp.rs # MCP 同步与校验
└── lib.rs # 应用入口 & 托盘菜单
├── tests/ # 前端测试
├── hooks/ # 单元测试
└── components/ # 集成测试
└── assets/ # 截图 & 合作商资源
```
## 更新日志
@@ -506,7 +341,7 @@ pnpm test:unit --coverage
- 通过类型检查:`pnpm typecheck`
- 通过格式检查:`pnpm format:check`
- 通过单元测试:`pnpm test:unit`
- 功能性 PR 请先经过 issue 讨论
- 💡 新功能开发前,欢迎先开 issue 讨论实现方案
## Star History

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

@@ -0,0 +1,249 @@
## Major architecture refactoring with enhanced config sync and data protection
**[中文更新说明 Chinese Documentation →](https://github.com/farion1231/cc-switch/blob/main/docs/release-note-v3.6.0-zh.md)**
---
## What's New
### Edit Mode & Provider Management
- **Provider Duplication** - Quickly duplicate existing provider configurations to create variants with one click
- **Manual Sorting** - Drag and drop to reorder providers, with visual push effect animations. Thanks to @ZyphrZero
- **Edit Mode Toggle** - Show/hide drag handles to optimize editing experience
### Custom Endpoint Management
- **Multi-Endpoint Configuration** - Support for aggregator providers with multiple API endpoints
- **Endpoint Input Visibility** - Shows endpoint field for all non-official providers automatically
### Usage Query Enhancements
- **Auto-Refresh Interval** - Configure periodic automatic usage queries with customizable intervals
- **Test Script API** - Validate JavaScript usage query scripts before execution
- **Enhanced Templates** - Custom blank templates with access token and user ID parameter support
Thanks to @Sirhexs
### Custom Configuration Directory (Cloud Sync)
- **Customizable Storage Location** - Customize CC Switch's configuration storage directory
- **Cloud Sync Support** - Point to cloud sync folders (Dropbox, OneDrive, iCloud Drive, etc.) to enable automatic config synchronization across devices
- **Independent Management** - Managed via Tauri Store for better isolation and reliability
Thanks to @ZyphrZero
### Configuration Directory Switching (WSL Support)
- **Auto-Sync on Directory Change** - When switching Claude/Codex config directories (e.g., WSL environment), automatically sync current provider to the new directory without manual operation
- **Post-Change Sync Utility** - Unified `postChangeSync.ts` utility for graceful error handling without blocking main flow
- **Import Config Auto-Sync** - Automatically sync after config import to ensure immediate effectiveness
- **Smart Conflict Resolution** - Distinguishes "fully successful" and "partially successful" states for precise user feedback
### Configuration Editor Improvements
- **JSON Format Button** - One-click JSON formatting in configuration editors
- **Real-Time TOML Validation** - Live syntax validation for Codex configuration with error highlighting
### Load Live Config When Editing
- **Protect Manual Modifications** - When editing the currently active provider, prioritize displaying the actual effective configuration from live files
- **Dual-Source Strategy** - Automatically loads from live config for active provider, SSOT for inactive ones
### Claude Configuration Data Structure Enhancements
- **Granular Model Configuration** - Migrated from dual-key to quad-key system for better model tier differentiation
- New fields: `ANTHROPIC_DEFAULT_HAIKU_MODEL`, `ANTHROPIC_DEFAULT_SONNET_MODEL`, `ANTHROPIC_DEFAULT_OPUS_MODEL`, `ANTHROPIC_MODEL`
- Replaces legacy `ANTHROPIC_SMALL_FAST_MODEL` with automatic migration
- Backend normalizes old configs on first read/write with smart fallback chain
- UI expanded from 2 to 4 model input fields with intelligent defaults
- **ANTHROPIC_API_KEY Support** - Providers can now use `ANTHROPIC_API_KEY` field in addition to `ANTHROPIC_AUTH_TOKEN`
- **Template Variable System** - Support for dynamic configuration replacement (e.g., KAT-Coder's `ENDPOINT_ID` parameter)
- **Endpoint Candidates** - Predefined endpoint list for speed testing and endpoint management
- **Visual Theme Configuration** - Custom icons and colors for provider cards
### Updated Provider Models
- **Kimi k2** - Updated to latest `kimi-k2-thinking` model
### New Provider Presets
Added 5 new provider presets:
- **DMXAPI** - Multi-model aggregation service
- **Azure Codex** - Microsoft Azure OpenAI endpoint
- **AnyRouter** - None-profit routing service
- **AiHubMix** - Multi-model aggregation service
- **MiniMax** - Open source AI model provider
### Partner Promotion Mechanism
- Support for ecosystem partner promotion (Zhipu GLM Z.ai)
- Sponsored banner integration in README
---
## Improvements
### Configuration & Sync
- **Unified Error Handling** - AppError with internationalized error messages throughout backend
- **Fixed apiKeyUrl Priority** - Correct priority order for API key URL resolution
- **Fixed MCP Sync Issues** - Resolved sync-to-other-side functionality failures
- **Import Config Sync** - Fixed sync issues after configuration import
- **Config Error Handling** - Force exit on config error to prevent silent fallback and data loss
### UI/UX Enhancements
- **Unique Provider Icons** - Each provider card now has unique icons and color identification
- **Unified Border System** - Consistent border design across all components
- **Drag Interaction** - Push effect animation and improved drag handle icons
- **Enhanced Visual Feedback** - Better current provider visual indication
- **Dialog Standardization** - Unified dialog sizes and layout consistency
- **Form Improvements** - Optimized model placeholders, simplified provider hints, category-specific hints
- **Usage Display Inline** - Usage info moved next to enable button for better space utilization
### Complete Internationalization
- **Error Messages i18n** - All backend error messages support Chinese/English
- **Tray Menu i18n** - System tray menu fully internationalized
- **UI Components i18n** - 100% coverage across all user-facing components
---
## Bug Fixes
### Configuration Management
- Fixed `apiKeyUrl` priority issue
- Fixed MCP sync-to-other-side functionality failure
- Fixed sync issues after config import
- Fixed Codex API Key auto-sync
- Fixed endpoint speed test functionality
- Fixed provider duplicate insertion position (now inserts next to original)
- Fixed custom endpoint preservation in edit mode
- Prevent silent fallback and data loss on config error
### Usage Query
- Fixed auto-query interval timing issue
- Ensured refresh button shows loading animation on click
### UI Issues
- Fixed name collision error (`get_init_error` command)
- Fixed language setting rollback after successful save
- Fixed language switch state reset (dependency cycle)
- Fixed edit mode button alignment
### Startup Issues
- Force exit on config error (no silent fallback)
- Eliminated code duplication causing initialization errors
---
## Architecture Refactoring
### Backend (Rust) - 5 Phase Refactoring
1. **Phase 1**: Unified error handling (`AppError` + i18n error messages)
2. **Phase 2**: Command layer split by domain (`commands/{provider,mcp,config,settings,plugin,misc}.rs`)
3. **Phase 3**: Integration tests and transaction mechanism (config snapshot + failure rollback)
4. **Phase 4**: Extracted Service layer (`services/{provider,mcp,config,speedtest}.rs`)
5. **Phase 5**: Concurrency optimization (`RwLock` instead of `Mutex`, scoped guard to avoid deadlock)
### Frontend (React + TypeScript) - 4 Stage Refactoring
1. **Stage 1**: Test infrastructure (vitest + MSW + @testing-library/react)
2. **Stage 2**: Extracted custom hooks (`useProviderActions`, `useMcpActions`, `useSettings`, `useImportExport`, etc.)
3. **Stage 3**: Component splitting and business logic extraction
4. **Stage 4**: Code cleanup and formatting unification
### Testing System
- **Hooks Unit Tests** - 100% coverage for all custom hooks
- **Integration Tests** - Coverage for key processes (App, SettingsDialog, MCP Panel)
- **MSW Mocking** - Backend API mocking to ensure test independence
- **Test Infrastructure** - vitest + MSW + @testing-library/react
### Code Quality
- **Unified Parameter Format** - All Tauri commands migrated to camelCase (Tauri 2 specification)
- **Semantic Clarity** - `AppType` renamed to `AppId` for better semantics
- **Centralized Parsing** - Unified `app` parameter parsing with `FromStr` trait
- **DRY Violations Cleanup** - Eliminated code duplication throughout codebase
- **Dead Code Removal** - Removed unused `missing_param` helper, deprecated `tauri-api.ts`, redundant `KimiModelSelector`
---
## Internal Optimizations (User Transparent)
### Removed Legacy Migration Logic
v3.6.0 removed v1 config auto-migration and copy file scanning logic:
- **Impact**: Improved startup performance, cleaner codebase
- **Compatibility**: v2 format configs fully compatible, no action required
- **Note**: Users upgrading from v3.1.0 or earlier should first upgrade to v3.2.x or v3.5.x for one-time migration, then upgrade to v3.6.0
### Command Parameter Standardization
Backend unified to use `app` parameter (values: `claude` or `codex`):
- **Impact**: More standardized code, friendlier error prompts
- **Compatibility**: Frontend fully adapted, users don't need to care about this change
---
## Dependencies
- Updated to **Tauri 2.8.x**
- Updated to **TailwindCSS 4.x**
- Updated to **TanStack Query v5.90.x**
- Maintained **React 18.2.x** and **TypeScript 5.3.x**
---
## Installation
### macOS
**Via Homebrew (Recommended):**
```bash
brew tap farion1231/ccswitch
brew install --cask cc-switch
```
**Manual Download:**
- Download `CC-Switch-v3.6.0-macOS.zip` from [Assets](#assets) below
> **Note**: Due to lack of Apple Developer account, you may see "unidentified developer" warning. Go to System Settings → Privacy & Security → Click "Open Anyway"
### Windows
- **Installer**: `CC-Switch-v3.6.0-Windows.msi`
- **Portable**: `CC-Switch-v3.6.0-Windows-Portable.zip`
### Linux
- **AppImage**: `CC-Switch-v3.6.0-Linux.AppImage`
- **Debian**: `CC-Switch-v3.6.0-Linux.deb`
---
## Documentation
- [中文文档 (Chinese)](https://github.com/farion1231/cc-switch/blob/main/README_ZH.md)
- [English Documentation](https://github.com/farion1231/cc-switch/blob/main/README.md)
- [完整更新日志 (Full Changelog)](https://github.com/farion1231/cc-switch/blob/main/CHANGELOG.md)
---
## Acknowledgments
Special thanks to **Zhipu AI** for sponsoring this project with their GLM CODING PLAN!
---
**Full Changelog**: https://github.com/farion1231/cc-switch/compare/v3.5.1...v3.6.0

View File

@@ -0,0 +1,249 @@
# CC Switch v3.6.0
> 全栈架构重构,增强配置同步与数据保护
**[English Version →](../release-note-v3.6.0.md)**
---
## 新增功能
### 编辑模式与供应商管理
- **供应商复制功能** - 一键快速复制现有供应商配置,轻松创建变体配置
- **手动排序功能** - 通过拖拽对供应商进行重新排序,带有视觉推送效果动画
- **编辑模式切换** - 显示/隐藏拖拽手柄,优化编辑体验
### 自定义端点管理
- **多端点配置** - 支持聚合类供应商的多 API 端点配置
- **端点输入可见性** - 为所有非官方供应商自动显示端点字段
### 自定义配置目录(云同步)
- **自定义存储位置** - 自定义 CC Switch 的配置存储目录
- **云同步支持** - 指定到云同步文件夹Dropbox、OneDrive、iCloud Drive、坚果云等即可实现跨设备配置自动同步
- **独立管理** - 通过 Tauri Store 管理,更好的隔离性和可靠性
### 使用量查询增强
- **自动刷新间隔** - 配置定时自动使用量查询,支持自定义间隔时间
- **测试脚本 API** - 在执行前验证 JavaScript 使用量查询脚本
- **增强模板系统** - 自定义空白模板,支持 access token 和 user ID 参数
### 配置目录切换WSL 支持)
- **目录变更自动同步** - 切换 Claude/Codex 配置目录(如 WSL 环境)时,自动同步当前供应商到新目录,无需手动操作
- **后置同步工具** - 统一的 `postChangeSync.ts` 工具,优雅处理错误而不阻塞主流程
- **导入配置自动同步** - 配置导入后自动同步,确保立即生效
- **智能冲突解决** - 区分"完全成功"和"部分成功"状态,提供精确的用户反馈
### 配置编辑器改进
- **JSON 格式化按钮** - 配置编辑器中一键 JSON 格式化
- **实时 TOML 验证** - Codex 配置的实时语法验证,带有错误高亮
### 编辑时加载 Live 配置
- **保护手动修改** - 编辑当前激活的供应商时,优先显示来自 live 文件的实际生效配置
- **双源策略** - 活动供应商自动从 live 配置加载,非活动供应商从 SSOT 加载
### Claude 配置数据结构增强
- **细粒度模型配置** - 从双键系统升级到四键系统,以匹配官方最新数据结构
- 新增字段:`ANTHROPIC_DEFAULT_HAIKU_MODEL``ANTHROPIC_DEFAULT_SONNET_MODEL``ANTHROPIC_DEFAULT_OPUS_MODEL``ANTHROPIC_MODEL`
- 替换旧版 `ANTHROPIC_SMALL_FAST_MODEL`,支持自动迁移
- 后端在首次读写时自动规范化旧配置,带有智能回退链
- UI 从 2 个模型输入字段扩展到 4 个,具有智能默认值
- **ANTHROPIC_API_KEY 支持** - 供应商现可使用 `ANTHROPIC_API_KEY` 字段(除 `ANTHROPIC_AUTH_TOKEN` 外)
- **模板变量系统** - 支持动态配置替换(如 KAT-Coder 的 `ENDPOINT_ID` 参数)
- **端点候选列表** - 预定义端点列表,用于速度测试和端点管理
- **视觉主题配置** - 供应商卡片自定义图标和颜色
### 供应商模型更新
- **Kimi k2** - 更新到最新的 `kimi-k2-thinking` 模型
### 新增供应商预设
新增 5 个供应商预设:
- **DMXAPI** - 多模型聚合服务
- **Azure Codex** - 微软 Azure OpenAI 端点
- **AnyRouter** - API 路由服务
- **AiHubMix** - AI 模型集合
- **MiniMax** - 国产 AI 模型提供商
### 合作伙伴推广机制
- 支持生态合作伙伴推广(智谱 GLM Z.ai
- README 中集成赞助商横幅
---
## 改进优化
### 配置与同步
- **统一错误处理** - 后端全面使用 AppError 与国际化错误消息
- **修复 apiKeyUrl 优先级** - 修正 API key URL 解析的优先级顺序
- **修复 MCP 同步问题** - 解决同步到另一端功能失效的问题
- **导入配置同步** - 修复配置导入后的同步问题
- **配置错误处理** - 配置错误时强制退出,防止静默回退和数据丢失
### UI/UX 增强
- **独特的供应商图标** - 每个供应商卡片现在都有独特的图标和颜色识别
- **统一边框系统** - 所有组件采用一致的边框设计
- **拖拽交互** - 推送效果动画和改进的拖拽手柄图标
- **增强视觉反馈** - 更好的当前供应商视觉指示
- **对话框标准化** - 统一的对话框尺寸和布局一致性
- **表单改进** - 优化模型占位符,简化供应商提示,分类特定提示
- **使用量内联显示** - 使用量信息移至启用按钮旁边,更好地利用空间
### 完整国际化
- **错误消息国际化** - 所有后端错误消息支持中英文
- **托盘菜单国际化** - 系统托盘菜单完全国际化
- **UI 组件国际化** - 所有面向用户的组件 100% 覆盖
---
## Bug 修复
### 配置管理
- 修复 `apiKeyUrl` 优先级问题
- 修复 MCP 同步到另一端功能失效
- 修复配置导入后的同步问题
- 修复 Codex API Key 自动同步
- 修复端点速度测试功能
- 修复供应商复制插入位置(现在插入到原供应商旁边)
- 修复编辑模式下自定义端点保留问题
- 防止配置错误时的静默回退和数据丢失
### 使用量查询
- 修复自动查询间隔时间问题
- 确保刷新按钮点击时显示加载动画
### UI 问题
- 修复名称冲突错误(`get_init_error` 命令)
- 修复保存成功后语言设置回滚
- 修复语言切换状态重置(依赖循环)
- 修复编辑模式按钮对齐
### 启动问题
- 配置错误时强制退出(不再静默回退)
- 消除导致初始化错误的代码重复
---
## 架构重构
### 后端Rust- 5 阶段重构
1. **阶段 1**:统一错误处理(`AppError` + 国际化错误消息)
2. **阶段 2**:命令层按领域拆分(`commands/{provider,mcp,config,settings,plugin,misc}.rs`
3. **阶段 3**:集成测试和事务机制(配置快照 + 失败回滚)
4. **阶段 4**:提取 Service 层(`services/{provider,mcp,config,speedtest}.rs`
5. **阶段 5**:并发优化(`RwLock` 替代 `Mutex`,作用域 guard 避免死锁)
### 前端React + TypeScript- 4 阶段重构
1. **阶段 1**测试基础设施vitest + MSW + @testing-library/react
2. **阶段 2**:提取自定义 hooks`useProviderActions``useMcpActions``useSettings``useImportExport` 等)
3. **阶段 3**:组件拆分和业务逻辑提取
4. **阶段 4**:代码清理和格式化统一
### 测试体系
- **Hooks 单元测试** - 所有自定义 hooks 100% 覆盖
- **集成测试** - 关键流程覆盖App、SettingsDialog、MCP 面板)
- **MSW 模拟** - 后端 API 模拟确保测试独立性
- **测试基础设施** - vitest + MSW + @testing-library/react
### 代码质量
- **统一参数格式** - 所有 Tauri 命令迁移到 camelCaseTauri 2 规范)
- **语义清晰** - `AppType` 重命名为 `AppId` 以获得更好的语义
- **集中解析** - 使用 `FromStr` trait 统一 `app` 参数解析
- **DRY 违规清理** - 消除整个代码库中的代码重复
- **死代码移除** - 移除未使用的 `missing_param` 辅助函数、废弃的 `tauri-api.ts`、冗余的 `KimiModelSelector`
---
## 内部优化(用户无感知)
### 移除遗留迁移逻辑
v3.6.0 移除了 v1 配置自动迁移和副本文件扫描逻辑:
- **影响**:提升启动性能,代码更简洁
- **兼容性**v2 格式配置完全兼容,无需任何操作
- **注意**:从 v3.1.0 或更早版本升级的用户,请先升级到 v3.2.x 或 v3.5.x 进行一次性迁移,然后再升级到 v3.6.0
### 命令参数标准化
后端统一使用 `app` 参数(取值:`claude``codex`
- **影响**:代码更规范,错误提示更友好
- **兼容性**:前端已完全适配,用户无需关心此变更
---
## 依赖更新
- 更新到 **Tauri 2.8.x**
- 更新到 **TailwindCSS 4.x**
- 更新到 **TanStack Query v5.90.x**
- 保持 **React 18.2.x****TypeScript 5.3.x**
---
## 安装方式
### macOS
**通过 Homebrew 安装(推荐):**
```bash
brew tap farion1231/ccswitch
brew install --cask cc-switch
```
**手动下载:**
- 从下方 [Assets](#assets) 下载 `CC-Switch-v3.6.0-macOS.zip`
> **注意**:由于作者没有苹果开发者账号,首次打开可能出现"未知开发者"警告。请前往"系统设置" → "隐私与安全性" → 点击"仍要打开"
### Windows
- **安装包**`CC-Switch-v3.6.0-Windows.msi`
- **便携版**`CC-Switch-v3.6.0-Windows-Portable.zip`
### Linux
- **AppImage**`CC-Switch-v3.6.0-Linux.AppImage`
- **Debian**`CC-Switch-v3.6.0-Linux.deb`
---
## 文档
- [中文文档](https://github.com/farion1231/cc-switch/blob/main/README_ZH.md)
- [English Documentation](https://github.com/farion1231/cc-switch/blob/main/README.md)
- [完整更新日志](https://github.com/farion1231/cc-switch/blob/main/CHANGELOG.md)
---
## 致谢
特别感谢**智谱 AI** 通过 GLM CODING PLAN 赞助本项目!
---
**完整变更记录**: https://github.com/farion1231/cc-switch/compare/v3.5.1...v3.6.0

View File

@@ -0,0 +1,391 @@
# CC Switch v3.6.1
> Stability improvements and user experience optimization (based on v3.6.0)
**[中文更新说明 Chinese Documentation →](https://github.com/farion1231/cc-switch/blob/main/docs/release-note-v3.6.1-zh.md)**
---
## 📦 What's New in v3.6.1 (2025-11-10)
This release focuses on **user experience optimization** and **configuration parsing robustness**, fixing several critical bugs and enhancing the usage query system.
### ✨ New Features
#### Usage Query System Enhancements
- **Credential Decoupling** - Usage queries can now use independent API Key and Base URL, no longer dependent on provider configuration
- Support for different query endpoints and authentication methods
- Automatically displays credential input fields based on template type
- General template: API Key + Base URL
- NewAPI template: Base URL + Access Token + User ID
- Custom template: Fully customizable
- **UI Component Upgrade** - Replaced native checkbox with shadcn/ui Switch component for modern experience
- **Form Unification** - Unified use of shadcn/ui Input components, consistent styling with the application
- **Password Visibility Toggle** - Added show/hide password functionality (API Key, Access Token)
#### Form Validation Infrastructure
- **Common Schema Library** - New JSON/TOML generic validators to reduce code duplication
- `jsonConfigSchema`: Generic JSON object validator
- `tomlConfigSchema`: Generic TOML format validator
- `mcpJsonConfigSchema`: MCP-specific JSON validator
- **MCP Conditional Field Validation** - Strict type checking
- stdio type requires `command` field
- http type requires `url` field
#### Partner Integration
- **PackyCode** - New official partner
- Added to Claude and Codex provider presets
- 10% discount promotion support
- New logo and partner identification
---
### 🔧 Improvements
#### User Experience
- **Drag Sort Sync** - Tray menu order now syncs with drag-and-drop sorting in real-time
- **Enhanced Error Notifications** - Provider switch failures now display copyable error messages
- **Removed Misleading Placeholders** - Deleted example text from model input fields to avoid user confusion
- **Auto-fill Base URL** - All non-official provider categories automatically populate the Base URL input field
#### Configuration Parsing
- **CJK Quote Normalization** - Automatically handles IME-input fullwidth quotes to prevent TOML parsing errors
- Supports automatic conversion of Chinese quotes (" " ' ') to ASCII quotes
- Applied in TOML input handlers
- Disabled browser auto-correction in Textarea component
- **Preserve Custom Fields** - Editing Codex MCP TOML configuration now preserves unknown fields
- Supports extension fields like timeout_ms, retry_count
- Forward compatibility with future MCP protocol extensions
---
### 🐛 Bug Fixes
#### Critical Fixes
- **Fixed usage script panel white screen crash** - FormLabel component missing FormField context caused entire app to crash
- Replaced with standalone Label component
- Root cause: FormLabel internally calls useFormField() hook which requires FormFieldContext
- **Fixed CJK input quote parsing failure** - IME-input fullwidth quotes caused TOML parsing errors
- Added textNormalization utility function
- Automatically normalizes quotes before parsing
- **Fixed drag sort tray desync** (#179) - Tray menu order not updated after drag-and-drop sorting
- Automatically calls updateTrayMenu after sorting completes
- Ensures UI and tray menu stay consistent
- **Fixed MCP custom field loss** - Custom fields silently dropped when editing Codex MCP configuration
- Uses spread operator to retain all fields
- Preserves unknown fields in normalizeServerConfig
#### Stability Improvements
- **Error Isolation** - Tray menu update failures no longer affect main operations
- Decoupled tray update errors from main operations
- Provides warning when main operation succeeds but tray update fails
- **Safe Pattern Matching** - Replaced `unwrap()` with safe pattern matching
- Avoids panic-induced app crashes
- Tray menu event handling uses match patterns
- **Import Config Classification** - Importing from default config now automatically sets category to `custom`
- Avoids imported configs being mistaken for official presets
- Provides clearer configuration source identification
---
### 📊 Technical Statistics
```
Commits: 17 commits
Code Changes: 31 files
- Additions: 1,163 lines
- Deletions: 811 lines
- Net Growth: +352 lines
Contributors: Jason (16), ZyphrZero (1)
```
**By Module**:
- UI/User Interface: 3 commits
- Usage Query System: 3 commits
- Configuration Parsing: 2 commits
- Form Validation: 1 commit
- Other Improvements: 8 commits
---
### 📥 Installation
#### macOS
**Via Homebrew (Recommended):**
```bash
brew tap farion1231/ccswitch
brew install --cask cc-switch
```
**Manual Download:**
- Download `CC-Switch-v3.6.1-macOS.zip` from [Assets](#assets) below
> **Note**: Due to lack of Apple Developer account, you may see "unidentified developer" warning. Go to System Settings → Privacy & Security → Click "Open Anyway"
#### Windows
- **Installer**: `CC-Switch-v3.6.1-Windows.msi`
- **Portable**: `CC-Switch-v3.6.1-Windows-Portable.zip`
#### Linux
- **AppImage**: `CC-Switch-v3.6.1-Linux.AppImage`
- **Debian**: `CC-Switch-v3.6.1-Linux.deb`
---
### 📚 Documentation
- [中文文档 (Chinese)](https://github.com/farion1231/cc-switch/blob/main/README_ZH.md)
- [English Documentation](https://github.com/farion1231/cc-switch/blob/main/README.md)
- [完整更新日志 (Full Changelog)](https://github.com/farion1231/cc-switch/blob/main/CHANGELOG.md)
---
### 🙏 Acknowledgments
Special thanks to:
- **Zhipu AI** - For sponsoring this project with GLM CODING PLAN
- **PackyCode** - New official partner
- **ZyphrZero** - For contributing tray menu sync fix (#179)
---
**Full Changelog**: https://github.com/farion1231/cc-switch/compare/v3.6.0...v3.6.1
---
---
## 📜 v3.6.0 Complete Feature Review
> Content below is from v3.6.0 (2025-11-07), helping you understand the complete feature set
<details>
<summary><b>Click to expand v3.6.0 detailed content →</b></summary>
## What's New
### Edit Mode & Provider Management
- **Provider Duplication** - Quickly duplicate existing provider configurations to create variants with one click
- **Manual Sorting** - Drag and drop to reorder providers, with visual push effect animations. Thanks to @ZyphrZero
- **Edit Mode Toggle** - Show/hide drag handles to optimize editing experience
### Custom Endpoint Management
- **Multi-Endpoint Configuration** - Support for aggregator providers with multiple API endpoints
- **Endpoint Input Visibility** - Shows endpoint field for all non-official providers automatically
### Usage Query Enhancements
- **Auto-Refresh Interval** - Configure periodic automatic usage queries with customizable intervals
- **Test Script API** - Validate JavaScript usage query scripts before execution
- **Enhanced Templates** - Custom blank templates with access token and user ID parameter support
Thanks to @Sirhexs
### Custom Configuration Directory (Cloud Sync)
- **Customizable Storage Location** - Customize CC Switch's configuration storage directory
- **Cloud Sync Support** - Point to cloud sync folders (Dropbox, OneDrive, iCloud Drive, etc.) to enable automatic config synchronization across devices
- **Independent Management** - Managed via Tauri Store for better isolation and reliability
Thanks to @ZyphrZero
### Configuration Directory Switching (WSL Support)
- **Auto-Sync on Directory Change** - When switching Claude/Codex config directories (e.g., WSL environment), automatically sync current provider to the new directory without manual operation
- **Post-Change Sync Utility** - Unified `postChangeSync.ts` utility for graceful error handling without blocking main flow
- **Import Config Auto-Sync** - Automatically sync after config import to ensure immediate effectiveness
- **Smart Conflict Resolution** - Distinguishes "fully successful" and "partially successful" states for precise user feedback
### Configuration Editor Improvements
- **JSON Format Button** - One-click JSON formatting in configuration editors
- **Real-Time TOML Validation** - Live syntax validation for Codex configuration with error highlighting
### Load Live Config When Editing
- **Protect Manual Modifications** - When editing the currently active provider, prioritize displaying the actual effective configuration from live files
- **Dual-Source Strategy** - Automatically loads from live config for active provider, SSOT for inactive ones
### Claude Configuration Data Structure Enhancements
- **Granular Model Configuration** - Migrated from dual-key to quad-key system for better model tier differentiation
- New fields: `ANTHROPIC_DEFAULT_HAIKU_MODEL`, `ANTHROPIC_DEFAULT_SONNET_MODEL`, `ANTHROPIC_DEFAULT_OPUS_MODEL`, `ANTHROPIC_MODEL`
- Replaces legacy `ANTHROPIC_SMALL_FAST_MODEL` with automatic migration
- Backend normalizes old configs on first read/write with smart fallback chain
- UI expanded from 2 to 4 model input fields with intelligent defaults
- **ANTHROPIC_API_KEY Support** - Providers can now use `ANTHROPIC_API_KEY` field in addition to `ANTHROPIC_AUTH_TOKEN`
- **Template Variable System** - Support for dynamic configuration replacement (e.g., KAT-Coder's `ENDPOINT_ID` parameter)
- **Endpoint Candidates** - Predefined endpoint list for speed testing and endpoint management
- **Visual Theme Configuration** - Custom icons and colors for provider cards
### Updated Provider Models
- **Kimi k2** - Updated to latest `kimi-k2-thinking` model
### New Provider Presets
Added 5 new provider presets:
- **DMXAPI** - Multi-model aggregation service
- **Azure Codex** - Microsoft Azure OpenAI endpoint
- **AnyRouter** - None-profit routing service
- **AiHubMix** - Multi-model aggregation service
- **MiniMax** - Open source AI model provider
### Partner Promotion Mechanism
- Support for ecosystem partner promotion (Zhipu GLM Z.ai)
- Sponsored banner integration in README
---
## Improvements
### Configuration & Sync
- **Unified Error Handling** - AppError with internationalized error messages throughout backend
- **Fixed apiKeyUrl Priority** - Correct priority order for API key URL resolution
- **Fixed MCP Sync Issues** - Resolved sync-to-other-side functionality failures
- **Import Config Sync** - Fixed sync issues after configuration import
- **Config Error Handling** - Force exit on config error to prevent silent fallback and data loss
### UI/UX Enhancements
- **Unique Provider Icons** - Each provider card now has unique icons and color identification
- **Unified Border System** - Consistent border design across all components
- **Drag Interaction** - Push effect animation and improved drag handle icons
- **Enhanced Visual Feedback** - Better current provider visual indication
- **Dialog Standardization** - Unified dialog sizes and layout consistency
- **Form Improvements** - Optimized model placeholders, simplified provider hints, category-specific hints
- **Usage Display Inline** - Usage info moved next to enable button for better space utilization
### Complete Internationalization
- **Error Messages i18n** - All backend error messages support Chinese/English
- **Tray Menu i18n** - System tray menu fully internationalized
- **UI Components i18n** - 100% coverage across all user-facing components
---
## Bug Fixes
### Configuration Management
- Fixed `apiKeyUrl` priority issue
- Fixed MCP sync-to-other-side functionality failure
- Fixed sync issues after config import
- Fixed Codex API Key auto-sync
- Fixed endpoint speed test functionality
- Fixed provider duplicate insertion position (now inserts next to original)
- Fixed custom endpoint preservation in edit mode
- Prevent silent fallback and data loss on config error
### Usage Query
- Fixed auto-query interval timing issue
- Ensured refresh button shows loading animation on click
### UI Issues
- Fixed name collision error (`get_init_error` command)
- Fixed language setting rollback after successful save
- Fixed language switch state reset (dependency cycle)
- Fixed edit mode button alignment
### Startup Issues
- Force exit on config error (no silent fallback)
- Eliminated code duplication causing initialization errors
---
## Architecture Refactoring
### Backend (Rust) - 5 Phase Refactoring
1. **Phase 1**: Unified error handling (`AppError` + i18n error messages)
2. **Phase 2**: Command layer split by domain (`commands/{provider,mcp,config,settings,plugin,misc}.rs`)
3. **Phase 3**: Integration tests and transaction mechanism (config snapshot + failure rollback)
4. **Phase 4**: Extracted Service layer (`services/{provider,mcp,config,speedtest}.rs`)
5. **Phase 5**: Concurrency optimization (`RwLock` instead of `Mutex`, scoped guard to avoid deadlock)
### Frontend (React + TypeScript) - 4 Stage Refactoring
1. **Stage 1**: Test infrastructure (vitest + MSW + @testing-library/react)
2. **Stage 2**: Extracted custom hooks (`useProviderActions`, `useMcpActions`, `useSettings`, `useImportExport`, etc.)
3. **Stage 3**: Component splitting and business logic extraction
4. **Stage 4**: Code cleanup and formatting unification
### Testing System
- **Hooks Unit Tests** - 100% coverage for all custom hooks
- **Integration Tests** - Coverage for key processes (App, SettingsDialog, MCP Panel)
- **MSW Mocking** - Backend API mocking to ensure test independence
- **Test Infrastructure** - vitest + MSW + @testing-library/react
### Code Quality
- **Unified Parameter Format** - All Tauri commands migrated to camelCase (Tauri 2 specification)
- **Semantic Clarity** - `AppType` renamed to `AppId` for better semantics
- **Centralized Parsing** - Unified `app` parameter parsing with `FromStr` trait
- **DRY Violations Cleanup** - Eliminated code duplication throughout codebase
- **Dead Code Removal** - Removed unused `missing_param` helper, deprecated `tauri-api.ts`, redundant `KimiModelSelector`
---
## Internal Optimizations (User Transparent)
### Removed Legacy Migration Logic
v3.6.0 removed v1 config auto-migration and copy file scanning logic:
- **Impact**: Improved startup performance, cleaner codebase
- **Compatibility**: v2 format configs fully compatible, no action required
- **Note**: Users upgrading from v3.1.0 or earlier should first upgrade to v3.2.x or v3.5.x for one-time migration, then upgrade to v3.6.0
### Command Parameter Standardization
Backend unified to use `app` parameter (values: `claude` or `codex`):
- **Impact**: More standardized code, friendlier error prompts
- **Compatibility**: Frontend fully adapted, users don't need to care about this change
---
## Dependencies
- Updated to **Tauri 2.8.x**
- Updated to **TailwindCSS 4.x**
- Updated to **TanStack Query v5.90.x**
- Maintained **React 18.2.x** and **TypeScript 5.3.x**
</details>
---
## 🌟 About CC Switch
CC Switch is a cross-platform desktop application for managing and switching between different provider configurations for Claude Code and Codex. Built with Tauri 2.0 + React 18 + TypeScript, supporting Windows, macOS, and Linux.
**Core Features**:
- 🔄 One-click switching between multiple AI providers
- 📦 Support for both Claude Code and Codex applications
- 🎨 Modern UI with complete Chinese/English internationalization
- 🔐 Local storage, secure and reliable data
- ☁️ Support for cloud sync configurations
- 🧩 Unified MCP server management
---
**Project Repository**: https://github.com/farion1231/cc-switch

View File

@@ -0,0 +1,389 @@
# CC Switch v3.6.1
> 稳定性提升与用户体验优化(基于 v3.6.0
**[English Version →](../release-note-v3.6.1.md)**
---
## 📦 v3.6.1 新增内容 (2025-11-10)
本次更新主要聚焦于**用户体验优化**和**配置解析健壮性**,修复了多个关键 Bug并增强了用量查询系统。
### ✨ 新增功能
#### 用量查询系统增强
- **凭证解耦** - 用量查询可使用独立的 API Key 和 Base URL不再依赖供应商配置
- 支持不同的查询端点和认证方式
- 根据模板类型自动显示对应的凭证输入框
- General 模板API Key + Base URL
- NewAPI 模板Base URL + Access Token + User ID
- Custom 模板:完全自定义
- **UI 组件升级** - 使用 shadcn/ui Switch 替代原生 checkbox体验更现代
- **表单统一化** - 统一使用 shadcn/ui 输入组件,样式与应用保持一致
- **密码显示切换** - 添加查看/隐藏密码功能API Key、Access Token
#### 表单验证基础设施
- **通用 Schema 库** - 新增 JSON/TOML 通用验证器,减少重复代码
- `jsonConfigSchema`:通用 JSON 对象验证器
- `tomlConfigSchema`:通用 TOML 格式验证器
- `mcpJsonConfigSchema`MCP 专用 JSON 验证器
- **MCP 条件字段验证** - 严格的类型检查
- stdio 类型强制要求 `command` 字段
- http 类型强制要求 `url` 字段
#### 合作伙伴集成
- **PackyCode** - 新增官方合作伙伴
- 添加到 Claude 和 Codex 供应商预设
- 支持 10% 折扣优惠(促销信息集成)
- 新增 Logo 和合作伙伴标识
---
### 🔧 改进优化
#### 用户体验
- **拖拽排序同步** - 托盘菜单顺序实时同步拖拽排序结果
- **错误通知增强** - 切换供应商失败时显示可复制的错误信息
- **移除误导性占位符** - 删除模型输入框的示例文本,避免用户混淆
- **Base URL 自动填充** - 所有非官方供应商类别自动填充 Base URL 输入框
#### 配置解析
- **中文引号规范化** - 自动处理 IME 输入的全角引号,防止 TOML 解析错误
- 支持中文引号(" " ' ')自动转换为 ASCII 引号
- 在 TOML 输入处理器中应用
- Textarea 组件禁用浏览器自动纠正
- **自定义字段保留** - 编辑 Codex MCP TOML 配置时保留未知字段
- 支持 timeout_ms、retry_count 等扩展字段
- 向前兼容未来的 MCP 协议扩展
---
### 🐛 Bug 修复
#### 关键修复
- **修复用量脚本面板白屏崩溃** - FormLabel 组件缺少 FormField context 导致整个应用崩溃
- 替换为独立的 Label 组件
- 根本原因FormLabel 内部调用 useFormField() hook 需要 FormFieldContext
- **修复中文输入法引号解析失败** - IME 输入的全角引号导致 TOML 解析错误
- 新增 textNormalization 工具函数
- 在解析前自动规范化引号
- **修复拖拽排序托盘不同步** (#179) - 拖拽排序后托盘菜单顺序未更新
- 在排序完成后自动调用 updateTrayMenu
- 确保 UI 和托盘菜单保持一致
- **修复 MCP 自定义字段丢失** - 编辑 Codex MCP 配置时自定义字段被静默丢弃
- 使用 spread 操作符保留所有字段
- normalizeServerConfig 中保留未知字段
#### 稳定性改进
- **错误隔离** - 托盘菜单更新失败不再影响主操作流程
- 将托盘更新错误与主操作解耦
- 主操作成功但托盘更新失败时给出警告
- **安全模式匹配** - 替换 `unwrap()` 为安全的 pattern matching
- 避免 panic 导致应用崩溃
- 托盘菜单事件处理使用 match 模式
- **导入配置分类** - 从默认配置导入时自动设置 category 为 `custom`
- 避免导入的配置被误认为官方预设
- 提供更清晰的配置来源标识
---
### 📊 技术统计
```
提交数: 17 commits
代码变更: 31 个文件
- 新增: 1,163 行
- 删除: 811 行
- 净增长: +352 行
贡献者: Jason (16), ZyphrZero (1)
```
**按模块分类**
- UI/用户界面3 commits
- 用量查询系统3 commits
- 配置解析2 commits
- 表单验证1 commit
- 其他改进8 commits
---
### 📥 安装方式
#### macOS
**通过 Homebrew 安装(推荐):**
```bash
brew tap farion1231/ccswitch
brew install --cask cc-switch
```
**手动下载:**
- 从下方 [Assets](#assets) 下载 `CC-Switch-v3.6.1-macOS.zip`
> **注意**:由于作者没有苹果开发者账号,首次打开可能出现"未知开发者"警告。请前往"系统设置" → "隐私与安全性" → 点击"仍要打开"
#### Windows
- **安装包**`CC-Switch-v3.6.1-Windows.msi`
- **便携版**`CC-Switch-v3.6.1-Windows-Portable.zip`
#### Linux
- **AppImage**`CC-Switch-v3.6.1-Linux.AppImage`
- **Debian**`CC-Switch-v3.6.1-Linux.deb`
---
### 📚 文档
- [中文文档](https://github.com/farion1231/cc-switch/blob/main/README_ZH.md)
- [English Documentation](https://github.com/farion1231/cc-switch/blob/main/README.md)
- [完整更新日志](https://github.com/farion1231/cc-switch/blob/main/CHANGELOG.md)
---
### 🙏 致谢
特别感谢:
- **智谱 AI** - 通过 GLM CODING PLAN 赞助本项目
- **PackyCode** - 新加入的官方合作伙伴
- **ZyphrZero** - 贡献托盘菜单同步修复 (#179)
---
**完整变更记录**: https://github.com/farion1231/cc-switch/compare/v3.6.0...v3.6.1
---
---
## 📜 v3.6.0 完整功能回顾
> 以下内容来自 v3.6.0 (2025-11-07),帮助您了解完整的功能集
<details>
<summary><b>点击展开 v3.6.0 的详细内容 →</b></summary>
## 新增功能
### 编辑模式与供应商管理
- **供应商复制功能** - 一键快速复制现有供应商配置,轻松创建变体配置
- **手动排序功能** - 通过拖拽对供应商进行重新排序,带有视觉推送效果动画
- **编辑模式切换** - 显示/隐藏拖拽手柄,优化编辑体验
### 自定义端点管理
- **多端点配置** - 支持聚合类供应商的多 API 端点配置
- **端点输入可见性** - 为所有非官方供应商自动显示端点字段
### 自定义配置目录(云同步)
- **自定义存储位置** - 自定义 CC Switch 的配置存储目录
- **云同步支持** - 指定到云同步文件夹Dropbox、OneDrive、iCloud Drive、坚果云等即可实现跨设备配置自动同步
- **独立管理** - 通过 Tauri Store 管理,更好的隔离性和可靠性
### 使用量查询增强
- **自动刷新间隔** - 配置定时自动使用量查询,支持自定义间隔时间
- **测试脚本 API** - 在执行前验证 JavaScript 使用量查询脚本
- **增强模板系统** - 自定义空白模板,支持 access token 和 user ID 参数
### 配置目录切换WSL 支持)
- **目录变更自动同步** - 切换 Claude/Codex 配置目录(如 WSL 环境)时,自动同步当前供应商到新目录,无需手动操作
- **后置同步工具** - 统一的 `postChangeSync.ts` 工具,优雅处理错误而不阻塞主流程
- **导入配置自动同步** - 配置导入后自动同步,确保立即生效
- **智能冲突解决** - 区分"完全成功"和"部分成功"状态,提供精确的用户反馈
### 配置编辑器改进
- **JSON 格式化按钮** - 配置编辑器中一键 JSON 格式化
- **实时 TOML 验证** - Codex 配置的实时语法验证,带有错误高亮
### 编辑时加载 Live 配置
- **保护手动修改** - 编辑当前激活的供应商时,优先显示来自 live 文件的实际生效配置
- **双源策略** - 活动供应商自动从 live 配置加载,非活动供应商从 SSOT 加载
### Claude 配置数据结构增强
- **细粒度模型配置** - 从双键系统升级到四键系统,以匹配官方最新数据结构
- 新增字段:`ANTHROPIC_DEFAULT_HAIKU_MODEL``ANTHROPIC_DEFAULT_SONNET_MODEL``ANTHROPIC_DEFAULT_OPUS_MODEL``ANTHROPIC_MODEL`
- 替换旧版 `ANTHROPIC_SMALL_FAST_MODEL`,支持自动迁移
- 后端在首次读写时自动规范化旧配置,带有智能回退链
- UI 从 2 个模型输入字段扩展到 4 个,具有智能默认值
- **ANTHROPIC_API_KEY 支持** - 供应商现可使用 `ANTHROPIC_API_KEY` 字段(除 `ANTHROPIC_AUTH_TOKEN` 外)
- **模板变量系统** - 支持动态配置替换(如 KAT-Coder 的 `ENDPOINT_ID` 参数)
- **端点候选列表** - 预定义端点列表,用于速度测试和端点管理
- **视觉主题配置** - 供应商卡片自定义图标和颜色
### 供应商模型更新
- **Kimi k2** - 更新到最新的 `kimi-k2-thinking` 模型
### 新增供应商预设
新增 5 个供应商预设:
- **DMXAPI** - 多模型聚合服务
- **Azure Codex** - 微软 Azure OpenAI 端点
- **AnyRouter** - API 路由服务
- **AiHubMix** - AI 模型集合
- **MiniMax** - 国产 AI 模型提供商
### 合作伙伴推广机制
- 支持生态合作伙伴推广(智谱 GLM Z.ai
- README 中集成赞助商横幅
---
## 改进优化
### 配置与同步
- **统一错误处理** - 后端全面使用 AppError 与国际化错误消息
- **修复 apiKeyUrl 优先级** - 修正 API key URL 解析的优先级顺序
- **修复 MCP 同步问题** - 解决同步到另一端功能失效的问题
- **导入配置同步** - 修复配置导入后的同步问题
- **配置错误处理** - 配置错误时强制退出,防止静默回退和数据丢失
### UI/UX 增强
- **独特的供应商图标** - 每个供应商卡片现在都有独特的图标和颜色识别
- **统一边框系统** - 所有组件采用一致的边框设计
- **拖拽交互** - 推送效果动画和改进的拖拽手柄图标
- **增强视觉反馈** - 更好的当前供应商视觉指示
- **对话框标准化** - 统一的对话框尺寸和布局一致性
- **表单改进** - 优化模型占位符,简化供应商提示,分类特定提示
- **使用量内联显示** - 使用量信息移至启用按钮旁边,更好地利用空间
### 完整国际化
- **错误消息国际化** - 所有后端错误消息支持中英文
- **托盘菜单国际化** - 系统托盘菜单完全国际化
- **UI 组件国际化** - 所有面向用户的组件 100% 覆盖
---
## Bug 修复
### 配置管理
- 修复 `apiKeyUrl` 优先级问题
- 修复 MCP 同步到另一端功能失效
- 修复配置导入后的同步问题
- 修复 Codex API Key 自动同步
- 修复端点速度测试功能
- 修复供应商复制插入位置(现在插入到原供应商旁边)
- 修复编辑模式下自定义端点保留问题
- 防止配置错误时的静默回退和数据丢失
### 使用量查询
- 修复自动查询间隔时间问题
- 确保刷新按钮点击时显示加载动画
### UI 问题
- 修复名称冲突错误(`get_init_error` 命令)
- 修复保存成功后语言设置回滚
- 修复语言切换状态重置(依赖循环)
- 修复编辑模式按钮对齐
### 启动问题
- 配置错误时强制退出(不再静默回退)
- 消除导致初始化错误的代码重复
---
## 架构重构
### 后端Rust- 5 阶段重构
1. **阶段 1**:统一错误处理(`AppError` + 国际化错误消息)
2. **阶段 2**:命令层按领域拆分(`commands/{provider,mcp,config,settings,plugin,misc}.rs`
3. **阶段 3**:集成测试和事务机制(配置快照 + 失败回滚)
4. **阶段 4**:提取 Service 层(`services/{provider,mcp,config,speedtest}.rs`
5. **阶段 5**:并发优化(`RwLock` 替代 `Mutex`,作用域 guard 避免死锁)
### 前端React + TypeScript- 4 阶段重构
1. **阶段 1**测试基础设施vitest + MSW + @testing-library/react
2. **阶段 2**:提取自定义 hooks`useProviderActions``useMcpActions``useSettings``useImportExport` 等)
3. **阶段 3**:组件拆分和业务逻辑提取
4. **阶段 4**:代码清理和格式化统一
### 测试体系
- **Hooks 单元测试** - 所有自定义 hooks 100% 覆盖
- **集成测试** - 关键流程覆盖App、SettingsDialog、MCP 面板)
- **MSW 模拟** - 后端 API 模拟确保测试独立性
- **测试基础设施** - vitest + MSW + @testing-library/react
### 代码质量
- **统一参数格式** - 所有 Tauri 命令迁移到 camelCaseTauri 2 规范)
- **语义清晰** - `AppType` 重命名为 `AppId` 以获得更好的语义
- **集中解析** - 使用 `FromStr` trait 统一 `app` 参数解析
- **DRY 违规清理** - 消除整个代码库中的代码重复
- **死代码移除** - 移除未使用的 `missing_param` 辅助函数、废弃的 `tauri-api.ts`、冗余的 `KimiModelSelector`
---
## 内部优化(用户无感知)
### 移除遗留迁移逻辑
v3.6.0 移除了 v1 配置自动迁移和副本文件扫描逻辑:
- **影响**:提升启动性能,代码更简洁
- **兼容性**v2 格式配置完全兼容,无需任何操作
- **注意**:从 v3.1.0 或更早版本升级的用户,请先升级到 v3.2.x 或 v3.5.x 进行一次性迁移,然后再升级到 v3.6.0
### 命令参数标准化
后端统一使用 `app` 参数(取值:`claude``codex`
- **影响**:代码更规范,错误提示更友好
- **兼容性**:前端已完全适配,用户无需关心此变更
---
## 依赖更新
- 更新到 **Tauri 2.8.x**
- 更新到 **TailwindCSS 4.x**
- 更新到 **TanStack Query v5.90.x**
- 保持 **React 18.2.x****TypeScript 5.3.x**
</details>
---
## 🌟 关于 CC Switch
CC Switch 是一个跨平台桌面应用,用于管理和切换 Claude Code 与 Codex 的不同供应商配置。基于 Tauri 2.0 + React 18 + TypeScript 构建,支持 Windows、macOS、Linux。
**核心特性**
- 🔄 一键切换多个 AI 供应商
- 📦 支持 Claude Code 和 Codex 双应用
- 🎨 现代化 UI完整的中英文国际化
- 🔐 本地存储,数据安全可靠
- ☁️ 支持云同步配置
- 🧩 MCP 服务器统一管理
---
**项目地址**: https://github.com/farion1231/cc-switch

View File

@@ -3,7 +3,8 @@
- mcp 管理器 ✅
- i18n ✅
- gemini cli
- homebrew 支持
- homebrew 支持
- memory 管理
- codex 更多预设供应商
- 云同步
- 本地代理

View File

@@ -1,6 +1,6 @@
{
"name": "cc-switch",
"version": "3.6.0",
"version": "3.6.2",
"description": "Claude Code & Codex 供应商切换工具",
"scripts": {
"dev": "pnpm tauri dev",

2
src-tauri/Cargo.lock generated
View File

@@ -563,7 +563,7 @@ dependencies = [
[[package]]
name = "cc-switch"
version = "3.6.0"
version = "3.6.2"
dependencies = [
"chrono",
"dirs 5.0.1",

View File

@@ -1,6 +1,6 @@
[package]
name = "cc-switch"
version = "3.6.0"
version = "3.6.2"
description = "Claude Code & Codex 供应商配置管理工具"
authors = ["Jason Young"]
license = "MIT"

View File

@@ -123,6 +123,7 @@ pub async fn queryProviderUsage(
/// 测试用量脚本(使用当前编辑器中的脚本,不保存)
#[allow(non_snake_case)]
#[allow(clippy::too_many_arguments)]
#[tauri::command]
pub async fn testUsageScript(
state: State<'_, AppState>,
@@ -130,6 +131,8 @@ pub async fn testUsageScript(
app: String,
#[allow(non_snake_case)] scriptCode: String,
timeout: Option<u64>,
#[allow(non_snake_case)] apiKey: Option<String>,
#[allow(non_snake_case)] baseUrl: Option<String>,
#[allow(non_snake_case)] accessToken: Option<String>,
#[allow(non_snake_case)] userId: Option<String>,
) -> Result<crate::provider::UsageResult, String> {
@@ -140,6 +143,8 @@ pub async fn testUsageScript(
&providerId,
&scriptCode,
timeout.unwrap_or(10),
apiKey.as_deref(),
baseUrl.as_deref(),
accessToken.as_deref(),
userId.as_deref(),
)

View File

@@ -243,7 +243,10 @@ fn handle_tray_menu_event(app: &tauri::AppHandle, event_id: &str) {
app.exit(0);
}
id if id.starts_with("claude_") => {
let provider_id = id.strip_prefix("claude_").unwrap();
let Some(provider_id) = id.strip_prefix("claude_") else {
log::error!("无效的 Claude 菜单项 ID: {}", id);
return;
};
log::info!("切换到Claude供应商: {}", provider_id);
// 执行切换
@@ -260,7 +263,10 @@ fn handle_tray_menu_event(app: &tauri::AppHandle, event_id: &str) {
});
}
id if id.starts_with("codex_") => {
let provider_id = id.strip_prefix("codex_").unwrap();
let Some(provider_id) = id.strip_prefix("codex_") else {
log::error!("无效的 Codex 菜单项 ID: {}", id);
return;
};
log::info!("切换到Codex供应商: {}", provider_id);
// 执行切换

View File

@@ -63,11 +63,19 @@ pub struct UsageScript {
pub code: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u64>,
/// 访问令牌(用于需要登录的接口
/// 用量查询专用的 API Key通用模板使用
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "apiKey")]
pub api_key: Option<String>,
/// 用量查询专用的 Base URL通用和 NewAPI 模板使用)
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "baseUrl")]
pub base_url: Option<String>,
/// 访问令牌用于需要登录的接口NewAPI 模板使用)
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "accessToken")]
pub access_token: Option<String>,
/// 用户ID用于需要用户标识的接口
/// 用户ID用于需要用户标识的接口NewAPI 模板使用
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "userId")]
pub user_id: Option<String>,

View File

@@ -532,12 +532,13 @@ impl ProviderService {
}
};
let provider = Provider::with_id(
let mut provider = Provider::with_id(
"default".to_string(),
"default".to_string(),
settings_config,
None,
);
provider.category = Some("custom".to_string());
{
let mut config = state.config.write().map_err(AppError::from)?;
@@ -801,7 +802,7 @@ impl ProviderService {
app_type: AppType,
provider_id: &str,
) -> Result<UsageResult, AppError> {
let (provider, script_code, timeout, access_token, user_id) = {
let (script_code, timeout, api_key, base_url, access_token, user_id) = {
let config = state.config.read().map_err(AppError::from)?;
let manager = config
.get_manager(&app_type)
@@ -813,38 +814,37 @@ impl ProviderService {
format!("Provider not found: {}", provider_id),
)
})?;
let (script_code, timeout, access_token, user_id) = {
let usage_script = provider
.meta
.as_ref()
.and_then(|m| m.usage_script.as_ref())
.ok_or_else(|| {
AppError::localized(
"provider.usage.script.missing",
"未配置用量查询脚本",
"Usage script is not configured",
)
})?;
if !usage_script.enabled {
return Err(AppError::localized(
"provider.usage.disabled",
"用量查询未启用",
"Usage query is disabled",
));
}
(
usage_script.code.clone(),
usage_script.timeout.unwrap_or(10),
usage_script.access_token.clone(),
usage_script.user_id.clone(),
)
};
(provider, script_code, timeout, access_token, user_id)
let usage_script = provider
.meta
.as_ref()
.and_then(|m| m.usage_script.as_ref())
.ok_or_else(|| {
AppError::localized(
"provider.usage.script.missing",
"未配置用量查询脚本",
"Usage script is not configured",
)
})?;
if !usage_script.enabled {
return Err(AppError::localized(
"provider.usage.disabled",
"用量查询未启用",
"Usage query is disabled",
));
}
// 直接从 UsageScript 中获取凭证,不再从供应商配置提取
(
usage_script.code.clone(),
usage_script.timeout.unwrap_or(10),
usage_script.api_key.clone().unwrap_or_default(),
usage_script.base_url.clone().unwrap_or_default(),
usage_script.access_token.clone(),
usage_script.user_id.clone(),
)
};
let (api_key, base_url) = Self::extract_credentials(&provider, &app_type)?;
Self::execute_and_format_usage_result(
&script_code,
&api_key,
@@ -857,36 +857,23 @@ impl ProviderService {
}
/// 测试用量脚本(使用临时脚本内容,不保存)
#[allow(clippy::too_many_arguments)]
pub async fn test_usage_script(
state: &AppState,
app_type: AppType,
provider_id: &str,
_state: &AppState,
_app_type: AppType,
_provider_id: &str,
script_code: &str,
timeout: u64,
api_key: Option<&str>,
base_url: Option<&str>,
access_token: Option<&str>,
user_id: Option<&str>,
) -> Result<UsageResult, AppError> {
// 获取 provider 的 API 凭证
let provider = {
let config = state.config.read().map_err(AppError::from)?;
let manager = config
.get_manager(&app_type)
.ok_or_else(|| Self::app_not_found(&app_type))?;
manager.providers.get(provider_id).cloned().ok_or_else(|| {
AppError::localized(
"provider.not_found",
format!("供应商不存在: {}", provider_id),
format!("Provider not found: {}", provider_id),
)
})?
};
let (api_key, base_url) = Self::extract_credentials(&provider, &app_type)?;
// 直接使用传入的凭证参数进行测试
Self::execute_and_format_usage_result(
script_code,
&api_key,
&base_url,
api_key.unwrap_or(""),
base_url.unwrap_or(""),
timeout,
access_token,
user_id,
@@ -1136,6 +1123,7 @@ impl ProviderService {
Ok(())
}
#[allow(dead_code)]
fn extract_credentials(
provider: &Provider,
app_type: &AppType,

View File

@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "CC Switch",
"version": "3.6.0",
"version": "3.6.2",
"identifier": "com.ccswitch.desktop",
"build": {
"frontendDist": "../dist",

View File

@@ -1,5 +1,5 @@
import React, { useState } from "react";
import { Play, Wand2 } from "lucide-react";
import { Play, Wand2, Eye, EyeOff } from "lucide-react";
import { toast } from "sonner";
import { useTranslation } from "react-i18next";
import { Provider, UsageScript } from "@/types";
@@ -16,6 +16,9 @@ import {
DialogFooter,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
interface UsageScriptModalProps {
provider: Provider;
@@ -140,6 +143,10 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
},
);
// 控制 API Key 的显示/隐藏
const [showApiKey, setShowApiKey] = useState(false);
const [showAccessToken, setShowAccessToken] = useState(false);
const handleSave = () => {
// 验证脚本格式
if (script.enabled && !script.code.trim()) {
@@ -166,6 +173,8 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
appId,
script.code,
script.timeout,
script.apiKey,
script.baseUrl,
script.accessToken,
script.userId,
);
@@ -225,23 +234,40 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
const handleUsePreset = (presetName: string) => {
const preset = PRESET_TEMPLATES[presetName];
if (preset) {
// 如果选择的不是 NewAPI 模板,清空高级配置字段
if (presetName !== TEMPLATE_KEYS.NEW_API) {
// 根据模板类型清空不同的字段
if (presetName === TEMPLATE_KEYS.CUSTOM) {
// 自定义:清空所有凭证字段
setScript({
...script,
code: preset,
apiKey: undefined,
baseUrl: undefined,
accessToken: undefined,
userId: undefined,
});
} else if (presetName === TEMPLATE_KEYS.GENERAL) {
// 通用:保留 apiKey 和 baseUrl清空 NewAPI 字段
setScript({
...script,
code: preset,
accessToken: undefined,
userId: undefined,
});
} else {
setScript({ ...script, code: preset });
} else if (presetName === TEMPLATE_KEYS.NEW_API) {
// NewAPI清空 apiKeyNewAPI 不使用通用的 apiKey
setScript({
...script,
code: preset,
apiKey: undefined,
});
}
setSelectedTemplate(presetName); // 记录选择的模板
}
};
// 判断是否应该显示高级配置(仅 NewAPI 模板需要)
const shouldShowAdvancedConfig = selectedTemplate === TEMPLATE_KEYS.NEW_API;
// 判断是否应该显示凭证配置区域
const shouldShowCredentialsConfig =
selectedTemplate === TEMPLATE_KEYS.GENERAL || selectedTemplate === TEMPLATE_KEYS.NEW_API;
return (
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
@@ -255,27 +281,28 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
{/* Content - Scrollable */}
<div className="flex-1 overflow-y-auto px-6 py-4 space-y-4">
{/* 启用开关 */}
<label className="flex items-center gap-2 cursor-pointer">
<input
type="checkbox"
<div className="flex items-center justify-between gap-4 rounded-lg border border-border-default p-4">
<div className="space-y-1">
<p className="text-sm font-medium leading-none">
{t("usageScript.enableUsageQuery")}
</p>
</div>
<Switch
checked={script.enabled}
onChange={(e) =>
setScript({ ...script, enabled: e.target.checked })
onCheckedChange={(checked) =>
setScript({ ...script, enabled: checked })
}
className="w-4 h-4"
aria-label={t("usageScript.enableUsageQuery")}
/>
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
{t("usageScript.enableUsageQuery")}
</span>
</label>
</div>
{script.enabled && (
<>
{/* 预设模板选择 */}
<div>
<label className="block text-sm font-medium mb-2 text-gray-900 dark:text-gray-100">
<Label className="mb-2">
{t("usageScript.presetTemplate")}
</label>
</Label>
<div className="flex gap-2">
{Object.keys(PRESET_TEMPLATES).map((name) => {
const isSelected = selectedTemplate === name;
@@ -296,46 +323,134 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
</div>
</div>
{/* 高级配置Access Token 和 User ID NewAPI 模板显示 */}
{shouldShowAdvancedConfig && (
<div className="space-y-3 p-4 bg-gray-50 dark:bg-gray-800/50 rounded-lg">
<label className="block">
<span className="text-xs text-gray-600 dark:text-gray-400">
{t("usageScript.accessToken")}
</span>
<input
type="text"
value={script.accessToken || ""}
onChange={(e) =>
setScript({ ...script, accessToken: e.target.value })
}
placeholder={t("usageScript.accessTokenPlaceholder")}
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 text-sm"
/>
</label>
{/* 凭证配置区域:通用和 NewAPI 模板显示 */}
{shouldShowCredentialsConfig && (
<div className="space-y-4 p-4 bg-gray-50 dark:bg-gray-800/50 rounded-lg">
<h4 className="text-sm font-medium text-gray-900 dark:text-gray-100">
{t("usageScript.credentialsConfig")}
</h4>
<label className="block">
<span className="text-xs text-gray-600 dark:text-gray-400">
{t("usageScript.userId")}
</span>
<input
type="text"
value={script.userId || ""}
onChange={(e) =>
setScript({ ...script, userId: e.target.value })
}
placeholder={t("usageScript.userIdPlaceholder")}
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 text-sm"
/>
</label>
{/* 通用模板:显示 apiKey + baseUrl */}
{selectedTemplate === TEMPLATE_KEYS.GENERAL && (
<>
<div className="space-y-2">
<Label htmlFor="usage-api-key">
API Key
</Label>
<div className="relative">
<Input
id="usage-api-key"
type={showApiKey ? "text" : "password"}
value={script.apiKey || ""}
onChange={(e) =>
setScript({ ...script, apiKey: e.target.value })
}
placeholder="sk-xxxxx"
autoComplete="off"
/>
{script.apiKey && (
<button
type="button"
onClick={() => setShowApiKey(!showApiKey)}
className="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
aria-label={showApiKey ? t("apiKeyInput.hide") : t("apiKeyInput.show")}
>
{showApiKey ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
)}
</div>
</div>
<div className="space-y-2">
<Label htmlFor="usage-base-url">
Base URL
</Label>
<Input
id="usage-base-url"
type="text"
value={script.baseUrl || ""}
onChange={(e) =>
setScript({ ...script, baseUrl: e.target.value })
}
placeholder="https://api.example.com"
autoComplete="off"
/>
</div>
</>
)}
{/* NewAPI 模板:显示 baseUrl + accessToken + userId */}
{selectedTemplate === TEMPLATE_KEYS.NEW_API && (
<>
<div className="space-y-2">
<Label htmlFor="usage-newapi-base-url">
Base URL
</Label>
<Input
id="usage-newapi-base-url"
type="text"
value={script.baseUrl || ""}
onChange={(e) =>
setScript({ ...script, baseUrl: e.target.value })
}
placeholder="https://api.newapi.com"
autoComplete="off"
/>
</div>
<div className="space-y-2">
<Label htmlFor="usage-access-token">
{t("usageScript.accessToken")}
</Label>
<div className="relative">
<Input
id="usage-access-token"
type={showAccessToken ? "text" : "password"}
value={script.accessToken || ""}
onChange={(e) =>
setScript({ ...script, accessToken: e.target.value })
}
placeholder={t("usageScript.accessTokenPlaceholder")}
autoComplete="off"
/>
{script.accessToken && (
<button
type="button"
onClick={() => setShowAccessToken(!showAccessToken)}
className="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
aria-label={showAccessToken ? t("apiKeyInput.hide") : t("apiKeyInput.show")}
>
{showAccessToken ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
)}
</div>
</div>
<div className="space-y-2">
<Label htmlFor="usage-user-id">
{t("usageScript.userId")}
</Label>
<Input
id="usage-user-id"
type="text"
value={script.userId || ""}
onChange={(e) =>
setScript({ ...script, userId: e.target.value })
}
placeholder={t("usageScript.userIdPlaceholder")}
autoComplete="off"
/>
</div>
</>
)}
</div>
)}
{/* 脚本编辑器 */}
<div>
<label className="block text-sm font-medium mb-2 text-gray-900 dark:text-gray-100">
<Label className="mb-2">
{t("usageScript.queryScript")}
</label>
</Label>
<JsonEditor
value={script.code}
onChange={(code) => setScript({ ...script, code })}
@@ -352,14 +467,15 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
{/* 配置选项 */}
<div className="grid grid-cols-2 gap-4">
<label className="block">
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
<div className="space-y-2">
<Label htmlFor="usage-timeout">
{t("usageScript.timeoutSeconds")}
</span>
<input
</Label>
<Input
id="usage-timeout"
type="number"
min="2"
max="30"
min={2}
max={30}
value={script.timeout || 10}
onChange={(e) =>
setScript({
@@ -367,20 +483,20 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
timeout: parseInt(e.target.value),
})
}
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100"
/>
</label>
</div>
{/* 🆕 自动查询间隔 */}
<label className="block">
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
<div className="space-y-2">
<Label htmlFor="usage-auto-interval">
{t("usageScript.autoQueryInterval")}
</span>
<input
</Label>
<Input
id="usage-auto-interval"
type="number"
min="0"
max="1440"
step="1"
min={0}
max={1440}
step={1}
value={script.autoQueryInterval || 0}
onChange={(e) =>
setScript({
@@ -388,12 +504,11 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
autoQueryInterval: parseInt(e.target.value) || 0,
})
}
className="mt-1 w-full px-3 py-2 border border-border-default dark:border-border-default rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100"
/>
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
<p className="text-xs text-muted-foreground">
{t("usageScript.autoQueryIntervalHint")}
</p>
</label>
</div>
</div>
{/* 脚本说明 */}

View File

@@ -32,6 +32,7 @@ import {
extractIdFromToml,
mcpServerToToml,
} from "@/utils/tomlUtils";
import { normalizeTomlText } from "@/utils/textNormalization";
import { useMcpValidation } from "./useMcpValidation";
interface McpFormModalProps {
@@ -228,19 +229,21 @@ const McpFormModal: React.FC<McpFormModalProps> = ({
};
const handleConfigChange = (value: string) => {
setFormConfig(value);
// 若为 TOML 模式,先做引号归一化,避免中文输入法导致的格式错误
const nextValue = useToml ? normalizeTomlText(value) : value;
setFormConfig(nextValue);
if (useToml) {
// TOML validation (use hook's complete validation)
const err = validateTomlConfig(value);
const err = validateTomlConfig(nextValue);
if (err) {
setConfigError(err);
return;
}
// Try to extract ID (if user hasn't filled it yet)
if (value.trim() && !formId.trim()) {
const extractedId = extractIdFromToml(value);
if (nextValue.trim() && !formId.trim()) {
const extractedId = extractIdFromToml(nextValue);
if (extractedId) {
setFormId(extractedId);
}

View File

@@ -179,7 +179,7 @@ export function ClaudeFormFields({
onModelChange("ANTHROPIC_MODEL", e.target.value)
}
placeholder={t("providerForm.modelPlaceholder", {
defaultValue: "claude-3-7-sonnet-20250219",
defaultValue: "",
})}
autoComplete="off"
/>
@@ -200,7 +200,7 @@ export function ClaudeFormFields({
onModelChange("ANTHROPIC_DEFAULT_HAIKU_MODEL", e.target.value)
}
placeholder={t("providerForm.haikuModelPlaceholder", {
defaultValue: "GLM-4.5-Air",
defaultValue: "",
})}
autoComplete="off"
/>
@@ -224,7 +224,7 @@ export function ClaudeFormFields({
)
}
placeholder={t("providerForm.modelPlaceholder", {
defaultValue: "claude-3-7-sonnet-20250219",
defaultValue: "",
})}
autoComplete="off"
/>
@@ -245,7 +245,7 @@ export function ClaudeFormFields({
onModelChange("ANTHROPIC_DEFAULT_OPUS_MODEL", e.target.value)
}
placeholder={t("providerForm.modelPlaceholder", {
defaultValue: "claude-3-7-opus-20250219",
defaultValue: "",
})}
autoComplete="off"
/>

View File

@@ -3,7 +3,7 @@ import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/button";
import { Form } from "@/components/ui/form";
import { Form, FormField, FormItem, FormMessage } from "@/components/ui/form";
import { providerSchema, type ProviderFormData } from "@/lib/schemas/provider";
import type { AppId } from "@/lib/api";
import type { ProviderCategory, ProviderMeta } from "@/types";
@@ -575,36 +575,60 @@ export function ProviderForm({
{/* 配置编辑器Claude 使用通用配置编辑器Codex 使用专用编辑器 */}
{appId === "codex" ? (
<CodexConfigEditor
authValue={codexAuth}
configValue={codexConfig}
onAuthChange={setCodexAuth}
onConfigChange={handleCodexConfigChange}
useCommonConfig={useCodexCommonConfigFlag}
onCommonConfigToggle={handleCodexCommonConfigToggle}
commonConfigSnippet={codexCommonConfigSnippet}
onCommonConfigSnippetChange={handleCodexCommonConfigSnippetChange}
commonConfigError={codexCommonConfigError}
authError={codexAuthError}
configError={codexConfigError}
onWebsiteUrlChange={(url) => form.setValue("websiteUrl", url)}
onNameChange={(name) => form.setValue("name", name)}
isTemplateModalOpen={isCodexTemplateModalOpen}
setIsTemplateModalOpen={setIsCodexTemplateModalOpen}
/>
<>
<CodexConfigEditor
authValue={codexAuth}
configValue={codexConfig}
onAuthChange={setCodexAuth}
onConfigChange={handleCodexConfigChange}
useCommonConfig={useCodexCommonConfigFlag}
onCommonConfigToggle={handleCodexCommonConfigToggle}
commonConfigSnippet={codexCommonConfigSnippet}
onCommonConfigSnippetChange={handleCodexCommonConfigSnippetChange}
commonConfigError={codexCommonConfigError}
authError={codexAuthError}
configError={codexConfigError}
onWebsiteUrlChange={(url) => form.setValue("websiteUrl", url)}
onNameChange={(name) => form.setValue("name", name)}
isTemplateModalOpen={isCodexTemplateModalOpen}
setIsTemplateModalOpen={setIsCodexTemplateModalOpen}
/>
{/* 配置验证错误显示 */}
<FormField
control={form.control}
name="settingsConfig"
render={() => (
<FormItem className="space-y-0">
<FormMessage />
</FormItem>
)}
/>
</>
) : (
<CommonConfigEditor
value={form.watch("settingsConfig")}
onChange={(value) => form.setValue("settingsConfig", value)}
useCommonConfig={useCommonConfig}
onCommonConfigToggle={handleCommonConfigToggle}
commonConfigSnippet={commonConfigSnippet}
onCommonConfigSnippetChange={handleCommonConfigSnippetChange}
commonConfigError={commonConfigError}
onEditClick={() => setIsCommonConfigModalOpen(true)}
isModalOpen={isCommonConfigModalOpen}
onModalClose={() => setIsCommonConfigModalOpen(false)}
/>
<>
<CommonConfigEditor
value={form.watch("settingsConfig")}
onChange={(value) => form.setValue("settingsConfig", value)}
useCommonConfig={useCommonConfig}
onCommonConfigToggle={handleCommonConfigToggle}
commonConfigSnippet={commonConfigSnippet}
onCommonConfigSnippetChange={handleCommonConfigSnippetChange}
commonConfigError={commonConfigError}
onEditClick={() => setIsCommonConfigModalOpen(true)}
isModalOpen={isCommonConfigModalOpen}
onModalClose={() => setIsCommonConfigModalOpen(false)}
/>
{/* 配置验证错误显示 */}
<FormField
control={form.control}
name="settingsConfig"
render={() => (
<FormItem className="space-y-0">
<FormMessage />
</FormItem>
)}
/>
</>
)}
{showButtons && (

View File

@@ -33,7 +33,8 @@ export function useBaseUrlState({
// 从配置同步到 stateClaude
useEffect(() => {
if (appType !== "claude") return;
if (category !== "third_party" && category !== "custom") return;
// 只有 official 类别不显示 Base URL 输入框,其他类别都需要回填
if (category === "official") return;
if (isUpdatingRef.current) return;
try {
@@ -50,7 +51,8 @@ export function useBaseUrlState({
// 从配置同步到 stateCodex
useEffect(() => {
if (appType !== "codex") return;
if (category !== "third_party" && category !== "custom") return;
// 只有 official 类别不显示 Base URL 输入框,其他类别都需要回填
if (category === "official") return;
if (isUpdatingRef.current) return;
if (!codexConfig) return;

View File

@@ -3,6 +3,7 @@ import {
extractCodexBaseUrl,
setCodexBaseUrl as setCodexBaseUrlInConfig,
} from "@/utils/providerConfigUtils";
import { normalizeTomlText } from "@/utils/textNormalization";
interface UseCodexConfigStateProps {
initialData?: {
@@ -159,10 +160,12 @@ export function useCodexConfigState({ initialData }: UseCodexConfigStateProps) {
// 处理 config 变化(同步 Base URL
const handleCodexConfigChange = useCallback(
(value: string) => {
setCodexConfig(value);
// 归一化中文/全角/弯引号,避免 TOML 解析报错
const normalized = normalizeTomlText(value);
setCodexConfig(normalized);
if (!isUpdatingCodexBaseUrlRef.current) {
const extracted = extractCodexBaseUrl(value) || "";
const extracted = extractCodexBaseUrl(normalized) || "";
if (extracted !== codexBaseUrl) {
setCodexBaseUrl(extracted);
}

View File

@@ -11,6 +11,10 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
"flex min-h-[80px] w-full rounded-md border border-border-default bg-background px-3 py-2 text-sm shadow-sm placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-blue-500/20 dark:focus:ring-blue-400/20 disabled:cursor-not-allowed disabled:opacity-50",
className,
)}
autoComplete="off"
autoCorrect="off"
autoCapitalize="none"
spellCheck={false}
ref={ref}
{...props}
/>

View File

@@ -139,6 +139,21 @@ export const providerPresets: ProviderPreset[] = [
},
category: "cn_official",
},
{
name: "Kimi For Coding",
websiteUrl: "https://www.kimi.com/coding/docs/",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://api.kimi.com/coding/",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "kimi-for-coding",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "kimi-for-coding",
ANTHROPIC_DEFAULT_SONNET_MODEL: "kimi-for-coding",
ANTHROPIC_DEFAULT_OPUS_MODEL: "kimi-for-coding",
},
},
category: "cn_official",
},
{
name: "ModelScope",
websiteUrl: "https://modelscope.cn",
@@ -215,6 +230,21 @@ export const providerPresets: ProviderPreset[] = [
},
category: "cn_official",
},
{
name: "BaiLing",
websiteUrl: "https://alipaytbox.yuque.com/sxs0ba/ling/get_started",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://api.tbox.cn/api/anthropic",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "Ling-1T",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "Ling-1T",
ANTHROPIC_DEFAULT_SONNET_MODEL: "Ling-1T",
ANTHROPIC_DEFAULT_OPUS_MODEL: "Ling-1T",
},
},
category: "cn_official",
},
{
name: "AiHubMix",
websiteUrl: "https://aihubmix.com",
@@ -248,7 +278,7 @@ export const providerPresets: ProviderPreset[] = [
{
name: "PackyCode",
websiteUrl: "https://www.packyapi.com",
apiKeyUrl: "https://www.packyapi.com",
apiKeyUrl: "https://www.packyapi.com/register?aff=cc-switch",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://www.packyapi.com",
@@ -261,6 +291,8 @@ export const providerPresets: ProviderPreset[] = [
"https://api-slb.packyapi.com",
],
category: "third_party",
isPartner: true, // 合作伙伴
partnerPromotionKey: "packycode", // 促销信息 i18n key
},
{
name: "AnyRouter",

View File

@@ -128,6 +128,7 @@ requires_openai_auth = true`,
{
name: "PackyCode",
websiteUrl: "https://www.packyapi.com",
apiKeyUrl: "https://www.packyapi.com/register?aff=cc-switch",
category: "third_party",
auth: generateThirdPartyAuth(""),
config: generateThirdPartyConfig(
@@ -139,6 +140,8 @@ requires_openai_auth = true`,
"https://www.packyapi.com/v1",
"https://api-slb.packyapi.com/v1",
],
isPartner: true, // 合作伙伴
partnerPromotionKey: "packycode", // 促销信息 i18n key
},
{
name: "AnyRouter",

View File

@@ -74,6 +74,15 @@ export function useDragSort(providers: Record<string, Provider>, appId: AppId) {
await queryClient.invalidateQueries({
queryKey: ["providers", appId],
});
// 更新托盘菜单以反映新的排序(失败不影响主操作)
try {
await providersApi.updateTrayMenu();
} catch (trayError) {
console.error("Failed to update tray menu after sort", trayError);
// 托盘菜单更新失败不影响排序成功
}
toast.success(
t("provider.sortUpdated", {
defaultValue: "排序已更新",

View File

@@ -64,7 +64,16 @@ export function useProviderActions(activeApp: AppId) {
const updateProvider = useCallback(
async (provider: Provider) => {
await updateProviderMutation.mutateAsync(provider);
await providersApi.updateTrayMenu();
// 更新托盘菜单(失败不影响主操作)
try {
await providersApi.updateTrayMenu();
} catch (trayError) {
console.error(
"Failed to update tray menu after updating provider",
trayError,
);
}
},
[updateProviderMutation],
);

View File

@@ -25,7 +25,8 @@
"toggleTheme": "Toggle theme",
"format": "Format",
"formatSuccess": "Formatted successfully",
"formatError": "Format failed: {{error}}"
"formatError": "Format failed: {{error}}",
"copy": "Copy"
},
"apiKeyInput": {
"placeholder": "Enter API Key",
@@ -97,7 +98,8 @@
"providerSaved": "Provider configuration saved",
"providerDeleted": "Provider deleted successfully",
"switchSuccess": "Switch successful! Please restart {{appName}} terminal to take effect",
"switchFailed": "Switch failed, please check configuration",
"switchFailedTitle": "Switch failed",
"switchFailed": "Switch failed: {{error}}",
"autoImported": "Default provider created from existing configuration",
"addFailed": "Failed to add provider: {{error}}",
"saveFailed": "Save failed: {{error}}",
@@ -239,7 +241,8 @@
"officialHint": "💡 Official provider uses browser login, no API Key needed",
"getApiKey": "Get API Key",
"partnerPromotion": {
"zhipu": "Zhipu GLM is an official partner of CC Switch. Use this link to top up and get a 10% discount"
"zhipu": "Zhipu GLM is an official partner of CC Switch. Use this link to top up and get a 10% discount",
"packycode": "PackyCode is an official partner of CC Switch. Register using this link and enter \"cc-switch\" promo code during recharge to get 10% off"
},
"parameterConfig": "Parameter Config - {{name}} *",
"mainModel": "Main Model (optional)",
@@ -264,9 +267,9 @@
"anthropicDefaultHaikuModel": "Default Haiku Model",
"anthropicDefaultSonnetModel": "Default Sonnet Model",
"anthropicDefaultOpusModel": "Default Opus Model",
"modelPlaceholder": "GLM-4.6",
"smallModelPlaceholder": "GLM-4.5-Air",
"haikuModelPlaceholder": "GLM-4.5-Air",
"modelPlaceholder": "",
"smallModelPlaceholder": "",
"haikuModelPlaceholder": "",
"modelHelper": "Optional: Specify default Claude model to use, leave blank to use system default.",
"categoryOfficial": "Official",
"categoryCnOfficial": "Opensource Official",
@@ -354,6 +357,7 @@
"templateCustom": "Custom",
"templateGeneral": "General",
"templateNewAPI": "NewAPI",
"credentialsConfig": "Credentials",
"accessToken": "Access Token",
"accessTokenPlaceholder": "Generate in 'Security Settings'",
"userId": "User ID",

View File

@@ -25,7 +25,8 @@
"toggleTheme": "切换主题",
"format": "格式化",
"formatSuccess": "格式化成功",
"formatError": "格式化失败:{{error}}"
"formatError": "格式化失败:{{error}}",
"copy": "复制"
},
"apiKeyInput": {
"placeholder": "请输入API Key",
@@ -97,7 +98,8 @@
"providerSaved": "供应商配置已保存",
"providerDeleted": "供应商删除成功",
"switchSuccess": "切换成功!请重启 {{appName}} 终端以生效",
"switchFailed": "切换失败,请检查配置",
"switchFailedTitle": "切换失败",
"switchFailed": "切换失败:{{error}}",
"autoImported": "已从现有配置创建默认供应商",
"addFailed": "添加供应商失败:{{error}}",
"saveFailed": "保存失败:{{error}}",
@@ -239,7 +241,8 @@
"officialHint": "💡 官方供应商使用浏览器登录,无需配置 API Key",
"getApiKey": "获取 API Key",
"partnerPromotion": {
"zhipu": "智谱 GLM 是 CC Switch 的官方合作伙伴使用此链接充值可以获得9折优惠"
"zhipu": "智谱 GLM 是 CC Switch 的官方合作伙伴使用此链接充值可以获得9折优惠",
"packycode": "PackyCode 是 CC Switch 的官方合作伙伴,使用此链接注册并在充值时填写 \"cc-switch\" 优惠码可以享受9折优惠"
},
"parameterConfig": "参数配置 - {{name}} *",
"mainModel": "主模型 (可选)",
@@ -264,9 +267,9 @@
"anthropicDefaultHaikuModel": "Haiku 默认模型",
"anthropicDefaultSonnetModel": "Sonnet 默认模型",
"anthropicDefaultOpusModel": "Opus 默认模型",
"modelPlaceholder": "GLM-4.6",
"smallModelPlaceholder": "GLM-4.5-Air",
"haikuModelPlaceholder": "GLM-4.5-Air",
"modelPlaceholder": "",
"smallModelPlaceholder": "",
"haikuModelPlaceholder": "",
"modelHelper": "可选:指定默认使用的 Claude 模型,留空则使用系统默认。",
"categoryOfficial": "官方",
"categoryCnOfficial": "开源官方",
@@ -354,6 +357,7 @@
"templateCustom": "自定义",
"templateGeneral": "通用模板",
"templateNewAPI": "NewAPI",
"credentialsConfig": "凭证配置",
"accessToken": "访问令牌",
"accessTokenPlaceholder": "在'安全设置'里生成",
"userId": "用户 ID",

View File

@@ -32,6 +32,8 @@ export const usageApi = {
appId: AppId,
scriptCode: string,
timeout?: number,
apiKey?: string,
baseUrl?: string,
accessToken?: string,
userId?: string,
): Promise<UsageResult> {
@@ -41,6 +43,8 @@ export const usageApi = {
app: appId,
scriptCode: scriptCode,
timeout: timeout,
apiKey: apiKey,
baseUrl: baseUrl,
accessToken: accessToken,
userId: userId,
});

View File

@@ -3,6 +3,7 @@ import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import { providersApi, settingsApi, type AppId } from "@/lib/api";
import type { Provider, Settings } from "@/types";
import { extractErrorMessage } from "@/utils/errorUtils";
export const useAddProviderMutation = (appId: AppId) => {
const queryClient = useQueryClient();
@@ -20,7 +21,17 @@ export const useAddProviderMutation = (appId: AppId) => {
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
await providersApi.updateTrayMenu();
// 更新托盘菜单(失败不影响主操作)
try {
await providersApi.updateTrayMenu();
} catch (trayError) {
console.error(
"Failed to update tray menu after adding provider",
trayError,
);
}
toast.success(
t("notifications.providerAdded", {
defaultValue: "供应商已添加",
@@ -76,7 +87,17 @@ export const useDeleteProviderMutation = (appId: AppId) => {
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
await providersApi.updateTrayMenu();
// 更新托盘菜单(失败不影响主操作)
try {
await providersApi.updateTrayMenu();
} catch (trayError) {
console.error(
"Failed to update tray menu after deleting provider",
trayError,
);
}
toast.success(
t("notifications.deleteSuccess", {
defaultValue: "供应商已删除",
@@ -104,7 +125,17 @@ export const useSwitchProviderMutation = (appId: AppId) => {
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["providers", appId] });
await providersApi.updateTrayMenu();
// 更新托盘菜单(失败不影响主操作)
try {
await providersApi.updateTrayMenu();
} catch (trayError) {
console.error(
"Failed to update tray menu after switching provider",
trayError,
);
}
toast.success(
t("notifications.switchSuccess", {
defaultValue: "切换供应商成功",
@@ -113,11 +144,24 @@ export const useSwitchProviderMutation = (appId: AppId) => {
);
},
onError: (error: Error) => {
const detail = extractErrorMessage(error) || t("common.unknown");
// 标题与详情分离,便于扫描 + 一键复制
toast.error(
t("notifications.switchFailed", {
defaultValue: "切换供应商失败: {{error}}",
error: error.message,
}),
t("notifications.switchFailedTitle", { defaultValue: "切换失败" }),
{
description: t("notifications.switchFailed", {
defaultValue: "切换失败:{{error}}",
error: detail,
}),
duration: 6000,
action: {
label: t("common.copy", { defaultValue: "复制" }),
onClick: () => {
navigator.clipboard?.writeText(detail).catch(() => undefined);
},
},
},
);
},
});

View File

@@ -103,6 +103,7 @@ export const useUsageQuery = (
autoQueryInterval > 0
? Math.max(autoQueryInterval, 1) * 60 * 1000 // 最小1分钟
: false,
refetchIntervalInBackground: true, // 后台也继续定时查询
refetchOnWindowFocus: false,
retry: false,
staleTime: 0, // 不使用缓存策略,确保 refetchInterval 准确执行

95
src/lib/schemas/common.ts Normal file
View File

@@ -0,0 +1,95 @@
import { z } from "zod";
import { validateToml, tomlToMcpServer } from "@/utils/tomlUtils";
/**
* 解析 JSON 语法错误,返回更友好的位置信息。
*/
function parseJsonError(error: unknown): string {
if (!(error instanceof SyntaxError)) {
return "JSON 格式错误";
}
const message = error.message || "JSON 解析失败";
// Chrome/V8: "Unexpected token ... in JSON at position 123"
const positionMatch = message.match(/at position (\d+)/i);
if (positionMatch) {
const position = parseInt(positionMatch[1], 10);
return `JSON 格式错误(位置:${position}`;
}
// Firefox: "JSON.parse: unexpected character at line 1 column 23"
const lineColumnMatch = message.match(/line (\d+) column (\d+)/i);
if (lineColumnMatch) {
const line = lineColumnMatch[1];
const column = lineColumnMatch[2];
return `JSON 格式错误:第 ${line} 行,第 ${column}`;
}
return `JSON 格式错误:${message}`;
}
/**
* 通用的 JSON 配置文本校验:
* - 非空
* - 可解析且为对象(非数组)
*/
export const jsonConfigSchema = z
.string()
.min(1, "配置不能为空")
.superRefine((value, ctx) => {
try {
const obj = JSON.parse(value);
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "需为单个对象配置",
});
}
} catch (e) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: parseJsonError(e),
});
}
});
/**
* 通用的 TOML 配置文本校验:
* - 允许为空(由上层业务决定是否必填)
* - 语法与结构有效
* - 针对 stdio/http 的必填字段command/url进行提示
*/
export const tomlConfigSchema = z.string().superRefine((value, ctx) => {
const err = validateToml(value);
if (err) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `TOML 无效:${err}`,
});
return;
}
if (!value.trim()) return;
try {
const server = tomlToMcpServer(value);
if (server.type === "stdio" && !server.command?.trim()) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "stdio 类型需填写 command",
});
}
if (server.type === "http" && !server.url?.trim()) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "http 类型需填写 url",
});
}
} catch (e: any) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: e?.message || "TOML 解析失败",
});
}
});

View File

@@ -1,14 +1,32 @@
import { z } from "zod";
const mcpServerSpecSchema = z.object({
type: z.enum(["stdio", "http"]).optional(),
command: z.string().trim().min(1, "请输入可执行命令").optional(),
args: z.array(z.string()).optional(),
env: z.record(z.string(), z.string()).optional(),
cwd: z.string().optional(),
url: z.string().url("请输入有效的 URL").optional(),
headers: z.record(z.string(), z.string()).optional(),
});
const mcpServerSpecSchema = z
.object({
type: z.enum(["stdio", "http"]).optional(),
command: z.string().trim().optional(),
args: z.array(z.string()).optional(),
env: z.record(z.string(), z.string()).optional(),
cwd: z.string().optional(),
url: z.string().trim().url("请输入有效的 URL").optional(),
headers: z.record(z.string(), z.string()).optional(),
})
.superRefine((server, ctx) => {
const type = server.type ?? "stdio";
if (type === "stdio" && !server.command?.trim()) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "stdio 类型需填写 command",
path: ["command"],
});
}
if (type === "http" && !server.url?.trim()) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "http 类型需填写 url",
path: ["url"],
});
}
});
export const mcpServerSchema = z.object({
id: z.string().min(1, "请输入服务器 ID"),

View File

@@ -1,19 +1,56 @@
import { z } from "zod";
/**
* 解析 JSON 语法错误,提取位置信息
*/
function parseJsonError(error: unknown): string {
if (!(error instanceof SyntaxError)) {
return "配置 JSON 格式错误";
}
const message = error.message;
// 提取位置信息Chrome/V8: "Unexpected token ... in JSON at position 123"
const positionMatch = message.match(/at position (\d+)/i);
if (positionMatch) {
const position = parseInt(positionMatch[1], 10);
return `JSON 格式错误:${message.split(" in JSON")[0]}(位置:${position}`;
}
// Firefox: "JSON.parse: unexpected character at line 1 column 23"
const lineColumnMatch = message.match(/line (\d+) column (\d+)/i);
if (lineColumnMatch) {
const line = lineColumnMatch[1];
const column = lineColumnMatch[2];
return `JSON 格式错误:第 ${line} 行,第 ${column}`;
}
// 通用情况:提取关键错误信息
const cleanMessage = message
.replace(/^JSON\.parse:\s*/i, "")
.replace(/^Unexpected\s+/i, "意外的 ")
.replace(/token/gi, "符号")
.replace(/Expected/gi, "预期");
return `JSON 格式错误:${cleanMessage}`;
}
export const providerSchema = z.object({
name: z.string().min(1, "请填写供应商名称"),
websiteUrl: z.string().url("请输入有效的网址").optional().or(z.literal("")),
settingsConfig: z
.string()
.min(1, "请填写配置内容")
.refine((value) => {
.superRefine((value, ctx) => {
try {
JSON.parse(value);
return true;
} catch {
return false;
} catch (error) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: parseJsonError(error),
});
}
}, "配置 JSON 格式错误"),
}),
});
export type ProviderFormData = z.infer<typeof providerSchema>;

View File

@@ -45,8 +45,10 @@ export interface UsageScript {
language: "javascript"; // 脚本语言
code: string; // 脚本代码JSON 格式配置)
timeout?: number; // 超时时间(秒,默认 10
accessToken?: string; // 访问令牌(用于需要登录的接口
userId?: string; // 用户ID用于需要用户标识的接口
apiKey?: string; // 用量查询专用的 API Key通用模板使用
baseUrl?: string; // 用量查询专用的 Base URL通用和 NewAPI 模板使用
accessToken?: string; // 访问令牌NewAPI 模板使用)
userId?: string; // 用户IDNewAPI 模板使用)
autoQueryInterval?: number; // 自动查询间隔单位分钟0 表示禁用)
}

View File

@@ -1,6 +1,7 @@
// 供应商配置处理工具函数
import type { TemplateValueConfig } from "../config/claudeProviderPresets";
import { normalizeQuotes } from "@/utils/textNormalization";
const isPlainObject = (value: unknown): value is Record<string, any> => {
return Object.prototype.toString.call(value) === "[object Object]";
@@ -357,7 +358,9 @@ export const extractCodexBaseUrl = (
configText: string | undefined | null,
): string | undefined => {
try {
const text = typeof configText === "string" ? configText : "";
const raw = typeof configText === "string" ? configText : "";
// 归一化中文/全角引号,避免正则提取失败
const text = normalizeQuotes(raw);
if (!text) return undefined;
const m = text.match(/base_url\s*=\s*(['"])([^'\"]+)\1/);
return m && m[2] ? m[2] : undefined;
@@ -390,16 +393,20 @@ export const setCodexBaseUrl = (
if (!trimmed) {
return configText;
}
// 归一化原文本中的引号(既能匹配,也能输出稳定格式)
const normalizedText = normalizeQuotes(configText);
const normalizedUrl = trimmed.replace(/\s+/g, "").replace(/\/+$/, "");
const replacementLine = `base_url = "${normalizedUrl}"`;
const pattern = /base_url\s*=\s*(["'])([^"']+)\1/;
if (pattern.test(configText)) {
return configText.replace(pattern, replacementLine);
if (pattern.test(normalizedText)) {
return normalizedText.replace(pattern, replacementLine);
}
const prefix =
configText && !configText.endsWith("\n") ? `${configText}\n` : configText;
normalizedText && !normalizedText.endsWith("\n")
? `${normalizedText}\n`
: normalizedText;
return `${prefix}${replacementLine}\n`;
};

View File

@@ -0,0 +1,20 @@
/**
* 将常见的中文/全角/弯引号统一为 ASCII 引号,以避免 TOML 解析失败。
* - 双引号:” “ „ ‟ → "
* - 单引号:’ → '
* 保守起见,不替换书名号/角引号(《》、「」等),避免误伤内容语义。
*/
export const normalizeQuotes = (text: string): string => {
if (!text) return text;
return text
// 双引号族 → "
.replace(/[“”„‟"]/g, '"')
// 单引号族 → '
.replace(/[]/g, "'");
};
/**
* 专用于 TOML 文本的归一化;目前等同于 normalizeQuotes后续可扩展如空白、行尾等
*/
export const normalizeTomlText = (text: string): string => normalizeQuotes(text);

View File

@@ -1,4 +1,5 @@
import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
import { normalizeTomlText } from "@/utils/textNormalization";
import { McpServerSpec } from "../types";
/**
@@ -9,7 +10,8 @@ import { McpServerSpec } from "../types";
export const validateToml = (text: string): string => {
if (!text.trim()) return "";
try {
const parsed = parseToml(text);
const normalized = normalizeTomlText(text);
const parsed = parseToml(normalized);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
return "mustBeObject";
}
@@ -23,21 +25,11 @@ export const validateToml = (text: string): string => {
/**
* 将 McpServerSpec 对象转换为 TOML 字符串
* 使用 @iarna/toml 的 stringify自动处理转义与嵌套表
* 保留所有字段(包括扩展字段如 timeout_ms
*/
export const mcpServerToToml = (server: McpServerSpec): string => {
const obj: any = {};
if (server.type) obj.type = server.type;
if (server.type === "stdio") {
if (server.command !== undefined) obj.command = server.command;
if (server.args && Array.isArray(server.args)) obj.args = server.args;
if (server.cwd !== undefined) obj.cwd = server.cwd;
if (server.env && typeof server.env === "object") obj.env = server.env;
} else if (server.type === "http") {
if (server.url !== undefined) obj.url = server.url;
if (server.headers && typeof server.headers === "object")
obj.headers = server.headers;
}
// 先复制所有字段(保留扩展字段)
const obj: any = { ...server };
// 去除未定义字段,确保输出更干净
for (const k of Object.keys(obj)) {
@@ -62,7 +54,7 @@ export const tomlToMcpServer = (tomlText: string): McpServerSpec => {
throw new Error("TOML 内容不能为空");
}
const parsed = parseToml(tomlText);
const parsed = parseToml(normalizeTomlText(tomlText));
// 情况 1: 直接是服务器配置(包含 type/command/url 等字段)
if (
@@ -103,6 +95,7 @@ export const tomlToMcpServer = (tomlText: string): McpServerSpec => {
/**
* 规范化服务器配置对象为 McpServer 格式
* 保留所有字段(包括扩展字段如 timeout_ms
*/
function normalizeServerConfig(config: any): McpServerSpec {
if (!config || typeof config !== "object") {
@@ -111,6 +104,9 @@ function normalizeServerConfig(config: any): McpServerSpec {
const type = (config.type as string) || "stdio";
// 已知字段列表(用于后续排除)
const knownFields = new Set<string>();
if (type === "stdio") {
if (!config.command || typeof config.command !== "string") {
throw new Error("stdio 类型的 MCP 服务器必须包含 command 字段");
@@ -120,10 +116,13 @@ function normalizeServerConfig(config: any): McpServerSpec {
type: "stdio",
command: config.command,
};
knownFields.add("type");
knownFields.add("command");
// 可选字段
if (config.args && Array.isArray(config.args)) {
server.args = config.args.map((arg: any) => String(arg));
knownFields.add("args");
}
if (config.env && typeof config.env === "object") {
const env: Record<string, string> = {};
@@ -131,9 +130,18 @@ function normalizeServerConfig(config: any): McpServerSpec {
env[k] = String(v);
}
server.env = env;
knownFields.add("env");
}
if (config.cwd && typeof config.cwd === "string") {
server.cwd = config.cwd;
knownFields.add("cwd");
}
// 保留所有未知字段(如 timeout_ms 等扩展字段)
for (const key of Object.keys(config)) {
if (!knownFields.has(key)) {
server[key] = config[key];
}
}
return server;
@@ -146,6 +154,8 @@ function normalizeServerConfig(config: any): McpServerSpec {
type: "http",
url: config.url,
};
knownFields.add("type");
knownFields.add("url");
// 可选字段
if (config.headers && typeof config.headers === "object") {
@@ -154,6 +164,14 @@ function normalizeServerConfig(config: any): McpServerSpec {
headers[k] = String(v);
}
server.headers = headers;
knownFields.add("headers");
}
// 保留所有未知字段
for (const key of Object.keys(config)) {
if (!knownFields.has(key)) {
server[key] = config[key];
}
}
return server;
@@ -169,7 +187,7 @@ function normalizeServerConfig(config: any): McpServerSpec {
*/
export const extractIdFromToml = (tomlText: string): string => {
try {
const parsed = parseToml(tomlText);
const parsed = parseToml(normalizeTomlText(tomlText));
// 尝试从 [mcp.servers.<id>] 或 [mcp_servers.<id>] 中提取 ID
if (parsed.mcp && typeof parsed.mcp === "object") {