64 Commits

Author SHA1 Message Date
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
Jason
8d77866160 Release v3.6.0: Major architecture refactoring and feature enhancements
New Features:
- Provider duplication and manual sorting via drag-and-drop
- Custom endpoint management for aggregator providers
- Usage query with auto-refresh interval and test script API
- Config editor improvements (JSON format button, real-time TOML validation)
- Auto-sync on directory change for WSL environment support
- Load live config when editing active provider to protect manual modifications
- New provider presets: DMXAPI, Azure Codex, AnyRouter, AiHubMix, MiniMax
- Partner promotion mechanism (Zhipu GLM Z.ai)

Architecture Improvements:
- Backend: 5-phase refactoring (error handling → command split → services → concurrency)
- Frontend: 4-stage refactoring (tests → hooks → components → cleanup)
- Testing: 100% hooks unit test coverage, integration tests for critical flows

Documentation:
- Complete README rewrite with detailed architecture overview
- Separate Chinese (README_ZH.md) and English (README.md) versions
- Comprehensive v3.6.0 changelog with categorized changes
- New bilingual screenshots and partner banners

Bug Fixes:
- Fixed configuration sync issues (apiKeyUrl priority, MCP sync, import sync)
- Fixed usage query interval timing and refresh button animation
- Fixed UI issues (edit mode alignment, language switch state)
- Fixed endpoint speed test and provider duplicate insertion position
- Force exit on config error to prevent silent fallback

Technical Details:
- Updated to Tauri 2.8.x, TailwindCSS 4.x, TanStack Query v5.90.x
- Removed legacy v1 migration logic for better startup performance
- Standardized command parameters (unified to camelCase `app`)
- Result pattern for graceful error handling
2025-11-07 16:27:51 +08:00
Jason
7305b1124b fix(forms): show endpoint input for all non-official providers
Previously, endpoint URL input was only shown for aggregator, third_party,
and custom categories. This excluded cn_official providers from managing
custom endpoints.

Changed logic to show endpoint input for all categories except official,
which fixes the issue and simplifies the condition.
2025-11-07 11:25:55 +08:00
Jason
dc79c31148 refactor(settings): improve directory configuration UI layout
- Rebrand "CC-Switch" to "CC Switch" across all UI text
- Separate CC Switch config directory into standalone section at top
- Update description to highlight cloud sync capability
- Remove redundant descriptions for Claude/Codex directory inputs
- Improve Chinese grammar for WSL configuration description
2025-11-06 23:05:25 +08:00
Jason
03e15916dd chore: apply cargo fmt before release
- Format code in app_config.rs to comply with rustfmt rules
- Remove extra blank line in config.rs
- Format test code in app_config_load.rs for consistency
2025-11-06 16:32:45 +08:00
Jason
69c0a09604 refactor(cleanup): remove dead code and optimize Option checks
- Remove 5 unused functions that were left over from migration refactoring:
  - get_archive_root, ensure_unique_path, archive_file (config.rs)
  - read_config_text_from_path, read_and_validate_config_from_path (codex_config.rs)
- Simplify is_v1 check using is_some_and instead of map_or for better readability
- Remove outdated comments about removed functions

This eliminates all dead_code warnings and improves code maintainability.
2025-11-06 16:22:05 +08:00
Jason
4e23250755 feat: add Z.ai GLM partner and fix apiKeyUrl priority
- Add Z.ai GLM as official partner with promotion support
- Fix apiKeyUrl not being prioritized for cn_official and aggregator categories
- Now apiKeyUrl (with promotion parameters) takes precedence over websiteUrl for cn_official, aggregator, and third_party categories
2025-11-06 16:04:10 +08:00
Jason
5f78e58ffc feat: add partner promotion feature for Zhipu GLM
- Add isPartner and partnerPromotionKey fields to Provider and ProviderPreset types
- Display gold star badge on partner presets in selector
- Show promotional message in API Key section for partners
- Configure Zhipu GLM as official partner with 10% discount promotion
- Support both Claude and Codex provider presets
- Add i18n support for partner promotion messages (zh/en)
2025-11-06 15:22:38 +08:00
Jason
e4416c9da8 docs(changelog): document breaking changes in [Unreleased]
Add comprehensive documentation for three breaking changes:
1. Runtime auto-migration from v1 to v2 config format removed
2. Legacy v1 copy file migration logic removed
3. Tauri commands now only accept 'app' parameter

Also document improvements and new tests added in recent commits.
2025-11-06 12:30:45 +08:00
Jason
6f05a91226 refactor(migration): remove legacy v1 copy file migration logic
Remove the entire migration module (435 lines) that was used for
one-time migration from v3.1.0 to v3.2.0. This cleans up technical
debt and improves startup performance.

Changes:
- Delete src-tauri/src/migration.rs (copy file scanning and merging)
- Remove migration module reference from lib.rs
- Simplify startup logic to only ensure config structure exists
- Remove automatic deduplication and archiving logic

BREAKING CHANGE: Users upgrading from v3.1.0 must first upgrade to
v3.2.x to automatically migrate their configurations.
2025-11-06 12:22:48 +08:00
Jason
db80e96786 refactor(config): remove v1 auto-migration and improve error handling
BREAKING CHANGE: Runtime auto-migration from v1 to v2 config format has been removed.

Changes:
- Remove automatic v1→v2 migration logic from MultiAppConfig::load()
- Improve v1 detection using structural analysis (checks for 'apps' key absence)
- Return clear error with migration instructions when v1 config is detected
- Add comprehensive tests for config loading edge cases
- Fix false positive detection when v1 config contains 'version' or 'mcp' fields

Migration path for users:
1. Install v3.2.x to perform one-time auto-migration, OR
2. Manually edit ~/.cc-switch/config.json to v2 format

Rationale:
- Separates concerns: load() should be read-only, not perform side effects
- Fail-fast principle: unsupported formats should error immediately
- Simplifies code maintenance by removing migration logic from hot path

Tests added:
- load_v1_config_returns_error_and_does_not_write
- load_v1_with_extra_version_still_treated_as_v1
- load_invalid_json_returns_parse_error_and_does_not_write
- load_valid_v2_config_succeeds
2025-11-06 09:18:21 +08:00
Jason
d6fa0060fb chore: unify code formatting and remove unused code
- Apply cargo fmt to Rust code with multiline error handling
- Apply Prettier formatting to TypeScript code with trailing commas
- Unify #[allow(non_snake_case)] attribute formatting
- Remove unused ProviderNotFound error variant from error.rs
- Add vitest-report.json to .gitignore to exclude test artifacts
- Optimize readability of error handling chains with vertical alignment

All tests passing: 22 Rust tests + 126 frontend tests
2025-11-05 23:17:34 +08:00
Jason
4f4c1e4ed7 feat(ui): move usage display inline next to enable button
- Refactor UsageFooter to support inline mode with two-row layout
  - Row 1: Last refresh time + refresh button (right-aligned)
  - Row 2: Used + Remaining + Unit
- Update ProviderCard layout to show usage inline instead of below
- Improve text spacing: reduce gap from 1 to 0.5 for tighter label-value pairs
- Update Chinese translation: "使用" → "已使用" for better clarity
- Maintain backward compatibility with bottom display mode

This change unifies card heights and improves visual consistency
across providers with and without usage queries enabled.
2025-11-05 22:46:30 +08:00
Jason
ce24b37b39 fix(usage): resolve auto-query interval timing issue
Problem:
- User configured 5-minute auto-query interval
- Actual queries executed every 10 minutes instead of 5

Root Cause:
- staleTime (5 min) conflicted with refetchInterval (5 min)
- React Query skipped refetch when data was still within staleTime
- First interval trigger at T+5min: data considered "fresh", skipped
- Second interval trigger at T+10min: data "stale", executed

Solution:
- Set staleTime to 0 for usage queries
- Ensures refetchInterval executes precisely as configured
- Auto-query is meant to fetch fresh data periodically, not use cache

Technical Details:
- Modified useUsageQuery in src/lib/query/queries.ts
- Changed: staleTime: 5 * 60 * 1000 → staleTime: 0
- Added explanatory comment in Chinese
- Manual queries still work via refetch() button
2025-11-05 22:02:12 +08:00
Jason
a428e618d2 refactor(usage): consolidate query logic to eliminate DRY violations
Breaking Changes:
- Removed useAutoUsageQuery hook (119 lines)
- Unified all usage queries into single useUsageQuery hook

Technical Improvements:
- Eliminated duplicate state management (React Query + manual useState)
- Fixed single source of truth principle violation
- Replaced manual setInterval with React Query's built-in refetchInterval
- Reduced UsageFooter complexity by 28% (54 → 39 lines)

New Features:
- useUsageQuery now accepts autoQueryInterval option
- Automatic query interval control (0 = disabled, min 1 minute)
- Built-in lastQueriedAt timestamp from dataUpdatedAt
- Auto-query only enabled for currently active provider

Architecture Benefits:
- Single data source: manual and auto queries share same cache
- No more state inconsistency between manual/auto query results
- Leverages React Query's caching, deduplication, and background updates
- Cleaner separation of concerns

Code Changes:
- src/lib/query/queries.ts: Enhanced useUsageQuery with auto-query support
- src/components/UsageFooter.tsx: Simplified to use single query hook
- src/hooks/useAutoUsageQuery.ts: Deleted (redundant)
- All type checks passed
2025-11-05 21:40:06 +08:00
Jason
21d29b9c2d feat(usage): add auto-refresh interval for usage queries
New Features:
- Users can configure auto-query interval in "Configure Usage Query" dialog
- Interval in minutes (0 = disabled, recommend 5-60 minutes)
- Auto-query only enabled for currently active provider
- Display last query timestamp in relative time format (e.g., "5 min ago")
- Execute first query immediately when enabled, then repeat at intervals

Technical Implementation:
- Backend: Add auto_query_interval field to UsageScript struct
- Frontend: Create useAutoUsageQuery Hook to manage timers and query state
- UI: Add auto-query interval input field in UsageScriptModal
- Integration: Display auto-query results and timestamp in UsageFooter
- i18n: Add Chinese and English translations

UX Improvements:
- Minimum interval protection (1 minute) to prevent API abuse
- Auto-cleanup timers on component unmount
- Silent failure handling for auto-queries, non-intrusive to users
- Prioritize auto-query results, fallback to manual query results
- Timestamp display positioned next to refresh button for better clarity
2025-11-05 15:48:19 +08:00
Jason
254896e5eb feat(providers): add DMXAPI provider and refine naming
- Add DMXAPI provider for both Claude and Codex
- Rename "Codex Official" to "OpenAI Official" for clarity
- Rename "Azure OpenAI (gpt-5-codex)" to "Azure OpenAI"
- Reorganize AiHubMix position in Claude presets
2025-11-05 14:48:07 +08:00
kenwoodjw
dbf220d85f add azure codex provider (#168) 2025-11-05 10:29:28 +08:00
Jason
b498f0fe91 refactor(i18n): complete error message internationalization and code cleanup
Replaced all remaining hardcoded error types with AppError::localized for
full bilingual support and simplified error handling logic.

Backend changes:
- usage_script.rs: Converted InvalidHttpMethod to localized error
- provider.rs: Replaced all 7 ProviderNotFound instances with localized errors
  * Line 436: Delete provider validation
  * Line 625: Update provider metadata
  * Line 785: Test usage script provider lookup
  * Line 855: Query usage provider lookup
  * Line 924: Prepare Codex provider switch
  * Line 1011: Prepare Claude provider switch
  * Line 1272: Delete provider snapshot
- provider.rs: Simplified error message formatting (removed 40+ lines)
  * Removed redundant string matching fallback logic
  * Now uses clean language-based selection for Localized errors
  * Falls back to default Display for other error types
- error.rs: Removed unused error variants
  * Deleted InvalidHttpMethod (replaced with localized)
  * Deleted ProviderNotFound (replaced with localized)

Code quality improvements:
- Reduced complexity: 40+ lines of string matching removed
- Better maintainability: Centralized error message handling
- Type safety: All provider errors now use consistent localized format

Impact:
- 100% i18n coverage for provider and usage script error messages
- Cleaner, more maintainable error handling code
- No unused error variants remaining
2025-11-05 10:11:47 +08:00
Jason
f92dd4cc5a fix(i18n): internationalize test script related error messages
Fixed 5 hardcoded Chinese error messages to support bilingual display:

Backend changes (services):
- provider.rs: Fixed 4 error messages:
  * Data format error when deserializing array (line 731)
  * Data format error when deserializing single object (line 738)
  * Regex initialization failure (line 1163)
  * App type not found (line 1191)
- speedtest.rs: Fixed 1 error message:
  * HTTP client creation failure (line 104)

All errors now use AppError::localized to provide both Chinese and English messages.

Impact:
- Users will now see properly localized error messages when testing usage scripts
- Error messages respect the application language setting
- Better user experience for English-speaking users
2025-11-05 08:52:36 +08:00
Jason
720c4d9774 feat(i18n): complete internationalization for usage query feature
Fully internationalized the usage query feature to support both Chinese and English.

Frontend changes:
- Refactored UsageScriptModal to use i18n translation keys
- Replaced hardcoded Chinese template names with constants
- Implemented dynamic template generation with i18n support
- Internationalized all labels, placeholders, and code comments
- Added template name mapping for translation

Backend changes:
- Replaced all hardcoded Chinese error messages in usage_script.rs
- Converted 33 error instances from AppError::Message to AppError::localized
- Added bilingual error messages for runtime, parsing, HTTP, and validation errors

Translation updates:
- Added 11 new translation key pairs to zh.json and en.json
- Covered template names, field labels, placeholders, and code comments

Impact:
- 100% i18n coverage for usage query functionality
- All user-facing text and error messages now support language switching
- Better user experience for English-speaking users
2025-11-05 00:07:54 +08:00
Jason
bafddb8e52 feat(usage): add test script API with refactored execution logic
- Add private helper method `execute_and_format_usage_result` to eliminate code duplication
- Refactor `query_usage` to use helper method instead of duplicating result processing
- Add new `test_usage_script` method to test temporary script without saving
- Add backend command `test_usage_script` accepting script content as parameter
- Register new command in lib.rs invoke_handler
- Add frontend `usageApi.testScript` method to call the new backend API
- Update `UsageScriptModal.handleTest` to test current editor content instead of saved script
- Improve DX: users can now test script changes before saving
2025-11-04 23:04:44 +08:00
Jason
05e58e9e14 feat(usage): add selected state styling to template buttons
- Add visual feedback for currently selected template
- Use blue background and white text for selected button
- Apply gray styling with hover effect for unselected buttons
- Support dark mode with appropriate color variants
- Match the same styling pattern used in provider preset selector
2025-11-04 21:58:25 +08:00
Jason
802c3bffd9 feat(usage): add custom blank template for usage script
- Add "自定义" (Custom) template as the first preset option
- Provide minimal structure with empty URL and headers for full customization
- Include basic extractor function returning remaining and unit fields
- Allow users to build usage queries from scratch without starting from complex examples
2025-11-04 21:53:42 +08:00
Jason
7be74806e8 feat(usage): conditionally show advanced config for NewAPI template
- Add template tracking state to monitor which preset is selected
- Move advanced config (Access Token & User ID) before script editor for better visibility
- Show advanced config only when NewAPI template is selected
- Auto-detect NewAPI template on modal open if accessToken/userId exist
- Clear advanced fields when switching from NewAPI to other templates
- Improve UX by placing critical config fields at the top
2025-11-04 21:44:20 +08:00
Jason
a6b6c199b4 refactor(commands): remove unused missing_param helper function
Remove dead code that was flagged by Rust compiler. Error handling is now unified through the AppError enum.
2025-11-04 15:54:45 +08:00
Jason
0778347f84 refactor(endpoints): implement deferred submission and fix clear-all bug
Implement Solution A (complete deferred submission) for custom endpoint
management, replacing the dual-mode system with unified local staging.

Changes:
- Remove immediate backend saves from EndpointSpeedTest
  * handleAddEndpoint: local state update only
  * handleRemoveEndpoint: local state update only
  * handleSelect: remove lastUsed timestamp update
- Add explicit clear detection in ProviderForm
  * Distinguish "user cleared endpoints" from "user didn't modify"
  * Pass empty object {} as clear signal vs null for no-change
- Fix mergeProviderMeta to handle three distinct cases:
  * null/undefined: don't modify endpoints (no meta sent)
  * empty object {}: explicitly clear endpoints (send empty meta)
  * with data: add/update endpoints (overwrite)

Fixed Critical Bug:
When users deleted all custom endpoints, changes were not saved because:
- draftCustomEndpoints=[] resulted in customEndpointsToSave=null
- mergeProviderMeta(meta, null) returned undefined
- Backend interpreted missing meta as "don't modify", preserving old values

Solution:
Detect when user had endpoints and cleared them (hadEndpoints && length===0),
then pass empty object to mergeProviderMeta as explicit clear signal.

Architecture Improvements:
- Transaction atomicity: all fields submitted together on form save
- UX consistency: add/edit modes behave identically
- Cancel button: true rollback with no immediate saves
- Code simplification: removed ~40 lines of immediate save error handling

Testing:
- TypeScript type check: passed
- Rust backend tests: 10/10 passed
- Build: successful
2025-11-04 15:30:54 +08:00
Jason
49c2855b10 feat(usage): add support for access token and user ID in usage scripts
Add optional accessToken and userId fields to usage query scripts,
enabling queries to authenticated endpoints like /api/user/self.

Changes:
- Add accessToken and userId fields to UsageScript type (frontend & backend)
- Extend script engine to support {{accessToken}} and {{userId}} placeholders
- Update NewAPI preset template to use /api/user/self endpoint
- Add UI inputs for access token and user ID in UsageScriptModal
- Pass new parameters through service layer to script executor

This allows users to query usage data from endpoints that require
login credentials, providing more accurate balance information for
services like NewAPI/OneAPI platforms.
2025-11-04 11:30:14 +08:00
Jason
ccb011fba1 fix(usage): ensure refresh button shows loading animation on click
Changed from isLoading to isFetching to properly track all query states.
Previously, the refresh button spinner would not animate when clicking
refresh because isLoading only indicates initial load without cached data.
isFetching covers all query states including manual refetches.
2025-11-03 23:13:30 +08:00
Jason
0f62829599 fix: resolve name collision in get_init_error command
The Tauri command `get_init_error` was importing a function with the
same name from `init_status` module, causing a compile-time error:
"the name `get_init_error` is defined multiple times".

Changes:
- Remove `get_init_error` from the use statement in misc.rs
- Use fully qualified path `crate::init_status::get_init_error()`
  in the command implementation to call the underlying function

This eliminates the ambiguity while keeping the public API unchanged.
2025-11-03 22:51:01 +08:00
Jason
cc5d59ce56 refactor: eliminate code duplication and force exit on config error
Improve config loading error handling in frontend by extracting common
logic and enforcing safer exit behavior.

Changes:

1. Extract handleConfigLoadError() function (DRY principle)
   - Previously: Identical error handling code duplicated in two places
     * Event listener for "configLoadError"
     * Bootstrap polling via get_init_error command
   - Now: Single reusable function called by both code paths
   - Reduces code from 86 lines to 70 lines (-35 duplicates, +30 new)

2. Replace confirm() with forced exit (safer UX)
   - Previously: User could click "Cancel" leaving app in broken state
     * No tray menu initialized
     * No AppState managed (all commands would fail)
     * Window open but non-functional
   - Now: App automatically exits after showing error message
   - Rationale: When config is corrupted, graceful exit is the only safe option

3. Add TypeScript interface for type safety
   - Define ConfigLoadErrorPayload interface explicitly
   - Improves IDE autocomplete and catches typos at compile time
   - Makes payload structure self-documenting

Benefits:
- Cleaner, more maintainable code (single source of truth)
- Safer user experience (no half-initialized state)
- Better type safety and developer experience
- Easier to add i18n support in the future (one string to translate)

This addresses code review feedback items #1 and #2.
2025-11-03 22:43:20 +08:00
Jason
4afa68eac6 fix: prevent silent config fallback and data loss on startup
This commit introduces fail-fast error handling for config loading failures,
replacing the previous silent fallback to default config which could cause
data loss (e.g., all user providers disappearing).

Key changes:

Backend (Rust):
- Replace AppState::new() with AppState::try_new() to explicitly propagate errors
- Remove Default trait to prevent accidental empty state creation
- Add init_status module as global error cache (OnceLock + RwLock)
- Implement dual-channel error notification:
  1. Event emission (low-latency, may race with frontend subscription)
  2. Command-based polling (reliable, guaranteed delivery)
- Remove unconditional save on startup to prevent overwriting corrupted config

Frontend (TypeScript):
- Add event listener for "configLoadError" (fast path)
- Add bootstrap-time polling via get_init_error command (reliable path)
- Display detailed error dialog with recovery instructions
- Prompt user to exit for manual repair

Impact:
- First-time users: No change (load() returns Ok(default) when file missing)
- Corrupted config: Application shows error and exits gracefully
- Prevents accidental config overwrite during initialization

Fixes the only critical issue identified in previous code review (silent
fallback causing data loss).
2025-11-03 22:33:10 +08:00
Jason
36fd61b2a2 refactor: migrate all Tauri commands to camelCase parameters
This commit addresses parameter naming inconsistencies caused by Tauri v2's
requirement for camelCase parameter names in IPC commands.

Backend changes (Rust):
- Updated all command parameters from snake_case to camelCase
- Commands affected:
  * provider.rs: providerId (×4), timeoutSecs
  * import_export.rs: filePath (×2), defaultName
  * config.rs: defaultPath
- Added #[allow(non_snake_case)] attributes for camelCase parameters
- Removed unused QueryUsageParams struct

Frontend changes (TypeScript):
- Removed redundant snake_case parameters from all invoke() calls
- Updated API files:
  * usage.ts: removed debug logs, unified to providerId
  * vscode.ts: updated 8 functions (providerId, timeoutSecs, filePath, defaultName)
  * settings.ts: updated 4 functions (defaultPath, filePath, defaultName)
- Ensured all parameters now use camelCase exclusively

Test updates:
- Updated MSW handlers to accept both old and new parameter formats during transition
- Added i18n mock compatibility for tests

Root cause:
The issue stemmed from Tauri v2 strictly requiring camelCase for command
parameters, while the codebase was using snake_case. This caused parameters
like 'provider_id' to not be recognized by the backend, resulting in
"missing providerId parameter" errors.

BREAKING CHANGE: All Tauri command invocations now require camelCase parameters.
Any external tools or scripts calling these commands must be updated accordingly.

Fixes: Usage query always failing with "missing providerId" error
Fixes: Custom endpoint management not receiving provider ID
Fixes: Import/export dialogs not respecting default paths
2025-11-03 16:50:23 +08:00
Jason
85334d8dce refine(usage): enhance query robustness and error handling
Backend improvements:
- Add InvalidHttpMethod error enum for better error semantics
- Clamp HTTP timeout to 2-30s to prevent config abuse
- Strict HTTP method validation instead of silent fallback to GET

Frontend improvements:
- Add i18n support for usage query errors (en/zh)
- Improve error handling with type-safe unknown instead of any
- Optimize i18n import (direct import instead of dynamic)
- Disable auto-retry for usage queries to avoid API stampede

Additional changes:
- Apply prettier formatting to affected files

Files changed:
- src-tauri/src/error.rs (+2)
- src-tauri/src/usage_script.rs (+8 -2)
- src/i18n/locales/{en,zh}.json (+4 -1 each)
- src/lib/api/usage.ts (+21 -4)
- src/lib/query/queries.ts (+1)
- style: prettier formatting on 6 other files
2025-11-03 10:24:59 +08:00
Jason
ab2833e626 feat(provider): enable endpoint management for aggregator providers
Allow aggregator category providers (like AiHubMix) to access the endpoint
speed test and management features, previously limited to third-party and
custom providers only.
2025-11-02 23:40:11 +08:00
Jason
b4f10d8316 refine(ui): improve model placeholders and simplify provider hints
- Add dedicated Haiku model placeholder "GLM-4.5-Air"
- Unify API key hints for all non-official providers
- Fix AiHubMix category from cn_official to aggregator
- Standardize GLM model name format (glm-4.6)
2025-11-02 23:24:49 +08:00
Jason
50eb4538ca feat: support ANTHROPIC_API_KEY field and add AiHubMix provider
Add compatibility for providers that use ANTHROPIC_API_KEY instead of
ANTHROPIC_AUTH_TOKEN, enabling support for AiHubMix and similar services.

Changes:
- Backend: Add fallback to ANTHROPIC_API_KEY in credential extraction
  - migration.rs: Support API_KEY during config migration
  - services/provider.rs: Extract credentials from either field
- Frontend: Smart API key field detection and management
  - getApiKeyFromConfig: Read from either field (AUTH_TOKEN priority)
  - setApiKeyInConfig: Write to existing field, preserve user choice
  - hasApiKeyField: Detect presence of either field
- Add AiHubMix provider preset with dual endpoint candidates
- Define apiKeyField metadata for provider presets (documentation)

Technical Details:
- Uses or_else() for zero-cost field fallback in Rust
- Runtime field detection ensures correct field name preservation
- Maintains backward compatibility with existing configurations
- Priority: ANTHROPIC_AUTH_TOKEN > ANTHROPIC_API_KEY
2025-11-02 23:10:21 +08:00
Jason
c56866f48c fix(codex): auto-sync API key from auth.json to form field
- Add useEffect to automatically extract and sync OPENAI_API_KEY from codexAuth to codexApiKey state
- Fix issue where API key wasn't populated in form after applying configuration wizard
- Optimize handleCodexApiKeyChange to trim input upfront
- Move getCodexAuthApiKey function closer to usage for better code organization
- Remove redundant dependency from useEffect dependency array
2025-11-02 22:44:40 +08:00
Jason
972650377d feat(codex): add AiHubMix provider and enhance configuration UX
- Add new AiHubMix provider preset with dual endpoints
- Fix AnyRouter base_url inconsistency (add /v1 suffix)
- Add configuration wizard shortcut link for Codex custom mode
- Improve accessibility with aria-label for wizard button
- Remove unused isCustomMode prop from CodexConfigEditor
- Add internationalization for new UI elements (zh/en)
2025-11-02 22:22:45 +08:00
Jason
faeca6b6ce feat(config): add MiniMax provider and update existing presets
- Add MiniMax provider for Claude with extended timeout configuration
- Update KAT-Coder URLs and model names to versioned variants (Pro V1/Air V1)
- Unify PackyCode domain from codex.packycode.com to www.packyapi.com
- Remove redundant comment in Zhipu GLM configuration
2025-11-02 21:51:14 +08:00
Jason
cb83089866 refactor(config): rename providerPresets to claudeProviderPresets
- Rename src/config/providerPresets.ts to claudeProviderPresets.ts
- Update all import statements across 11 files to reflect the new name
- Establish symmetrical naming convention with codexProviderPresets.ts
- Improve code clarity and maintainability
2025-11-02 21:05:48 +08:00
Jason
ebb7106102 refactor(ui): remove redundant KimiModelSelector and unify model configuration
Remove KimiModelSelector component and useKimiModelSelector hook to
eliminate code duplication and unify model configuration across all
Claude-compatible providers.

**Problem Statement:**
Previously, we maintained two separate implementations for the same
functionality:
- KimiModelSelector: API-driven dropdown with 211 lines of code
- ClaudeFormFields: Simple text inputs for model configuration

After removing API fetching logic from KimiModelSelector, both components
became functionally identical (4 text inputs), violating DRY principle
and creating unnecessary maintenance burden.

**Changes:**

Backend (Rust):
- No changes (model normalization logic already in place)

Frontend (React):
- Delete KimiModelSelector.tsx (-211 lines)
- Delete useKimiModelSelector.ts (-142 lines)
- Update ClaudeFormFields.tsx: remove Kimi-specific props (-35 lines)
- Update ProviderForm.tsx: unify display logic (-31 lines)
- Clean up hooks/index.ts: remove useKimiModelSelector export (-1 line)

Configuration:
- Update Kimi preset: kimi-k2-turbo-preview → kimi-k2-0905-preview
  * Uses official September 2025 release
  * 256K context window (vs 128K in older version)

Internationalization:
- Remove kimiSelector.* i18n keys (15 keys × 2 languages = -36 lines)
- Remove providerForm.kimiApiKeyHint

**Unified Architecture:**

Before (complex branching):
  ProviderForm
  ├─ if Kimi → useKimiModelSelector → KimiModelSelector (4 inputs)
  └─ else → useModelState → ClaudeFormFields inline (4 inputs)

After (single path):
  ProviderForm
  └─ useModelState → ClaudeFormFields (4 inputs for all providers)

Display logic simplified:
  - Old: shouldShowModelSelector = category !== "official" && !shouldShowKimiSelector
  - New: shouldShowModelSelector = category !== "official"

**Impact:**

Code Quality:
- Remove 457 lines of redundant code (-98.5%)
- Eliminate dual-track maintenance
- Improve code consistency
- Pass TypeScript type checking with zero errors
- Zero remaining references to deleted code

User Experience:
- Consistent UI across all providers (including Kimi)
- Same model configuration workflow for everyone
- No functional changes from user perspective

Architecture:
- Single source of truth for model configuration
- Easier to extend for future providers
- Reduced bundle size (removed lucide-react icons dependency)

**Testing:**
-  TypeScript compilation passes
-  No dangling references
-  All model configuration fields functional
-  Display logic works for official/cn_official/aggregator categories

**Migration Notes:**
- Existing Kimi users: configurations automatically upgraded to new model name
- No manual intervention required
- Backend normalization ensures backward compatibility
2025-11-02 20:57:16 +08:00
Jason
4811aa2dcd refactor(models): migrate to granular model configuration architecture
Upgrade Claude model configuration from dual-key to quad-key system for
better model tier differentiation.

**Breaking Changes:**
- Replace `ANTHROPIC_SMALL_FAST_MODEL` with three granular keys:
  - `ANTHROPIC_DEFAULT_HAIKU_MODEL`
  - `ANTHROPIC_DEFAULT_SONNET_MODEL`
  - `ANTHROPIC_DEFAULT_OPUS_MODEL`

**Backend (Rust):**
- Add `normalize_claude_models_in_value()` for automatic migration
- Implement fallback chain: `DEFAULT_* || SMALL_FAST || MODEL`
- Auto-cleanup: remove legacy `SMALL_FAST` key after normalization
- Apply normalization across 6 critical paths:
  - Add/update provider
  - Read from live config
  - Write to live config
  - Refresh config snapshot

**Frontend (React):**
- Expand UI from 2 to 4 model input fields
- Implement smart fallback in `useModelState` hook
- Update `useKimiModelSelector` for Kimi model picker
- Add i18n keys for Haiku/Sonnet/Opus labels (zh/en)

**Configuration:**
- Update all 7 provider presets to new format
- DeepSeek/Qwen/Moonshot: use same model for all tiers
- Zhipu: preserve tier differentiation (glm-4.5-air for Haiku)

**Backward Compatibility:**
- Old configs auto-upgrade on first read/write
- Fallback chain ensures graceful degradation
- No manual migration required

Closes #[issue-number]
2025-11-02 18:02:22 +08:00
Jason
2ebe34810c refactor(hooks): introduce unified post-change sync utility
- Add postChangeSync.ts utility with Result pattern for graceful error handling
- Replace try-catch with syncCurrentProvidersLiveSafe in useImportExport
- Add directory-change-triggered sync in useSettings to maintain SSOT
- Introduce partial-success status to distinguish import success from sync failures
- Add test coverage for sync behavior in different scenarios

This refactoring ensures config.json changes are reliably synced to live
files while providing better user feedback for edge cases.
2025-11-01 23:58:29 +08:00
Jason
87f408c163 feat(editor): add JSON format button to editors
Add one-click format functionality for JSON editors with toast notifications:

- Add format button to JsonEditor (CodeMirror)
- Add format button to CodexAuthSection (auth.json)
- Add format button to CommonConfigEditor (settings + modal)
- Add formatJSON utility function
- Add i18n keys: format, formatSuccess, formatError

Note: TOML formatting was intentionally NOT added to avoid losing comments
during parse/stringify operations. TOML validation remains available via
the existing useCodexTomlValidation hook.
2025-11-01 21:05:01 +08:00
Jason
b1f7840e45 fix(provider): add footer and improve layout spacing for common config editor dialog
- Add DialogFooter with Cancel and Save buttons to match Codex common config modal
- Improve dialog layout spacing with proper padding (px-6, py-4)
- Add flex layout with overflow handling for better responsiveness
- Fix content being too close to dialog edges
2025-11-01 18:59:19 +08:00
Jason Young
c168873c1e Merge pull request #164 from farion1231/refactor/project-restructure
comprehensive architectural refactoring
2025-10-31 21:17:38 +08:00
96 changed files with 4887 additions and 1839 deletions

1
.gitignore vendored
View File

@@ -11,3 +11,4 @@ CLAUDE.md
AGENTS.md
/.claude
/.vscode
vitest-report.json

View File

@@ -5,6 +5,114 @@ All notable changes to CC Switch will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.6.0] - 2025-11-07
### ✨ New Features
- **Provider Duplicate** - Quick duplicate existing provider configurations for easy variant creation
- **Edit Mode Toggle** - Show/hide drag handles to optimize editing experience
- **Custom Endpoint Management** - Support multi-endpoint configuration for aggregator providers
- **Usage Query Enhancements**
- Auto-refresh interval: Support periodic automatic usage query
- Test Script API: Validate JavaScript scripts before execution
- Template system expansion: Custom blank template, support for access token and user ID parameters
- **Configuration Editor Improvements**
- Add JSON format button
- Real-time TOML syntax validation for Codex configuration
- **Auto-sync on Directory Change** - When switching Claude/Codex config directories (e.g., WSL environment), automatically sync current provider to new directory without manual operation
- **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)
### 🔧 Improvements
- **Configuration Directory Switching**
- Introduced unified post-change sync utility (`postChangeSync.ts`)
- Auto-sync current providers to new directory when changing Claude/Codex config directories
- Perfect support for WSL environment switching
- Auto-sync after config import to ensure immediate effectiveness
- Use Result pattern for graceful error handling without blocking main flow
- Distinguish "fully successful" and "partially successful" states for precise user feedback
- **UI/UX Enhancements**
- Provider cards: Unique icons and color identification
- Unified border design system across all components
- Drag interaction optimization: Push effect animation, improved handle icons
- Enhanced current provider visual feedback
- Dialog size standardization and layout consistency
- Form experience: Optimized model placeholders, simplified provider hints, category-specific hints
- **Complete Internationalization Coverage**
- Error messages internationalization
- Tray menu internationalization
- All UI components internationalization
- **Usage Display Moved Inline** - Usage display moved next to enable button
### 🐛 Bug Fixes
- **Configuration Sync**
- Fixed `apiKeyUrl` priority issue
- Fixed MCP sync-to-other-side functionality failure
- Fixed sync issues after config import
- Prevent silent fallback and data loss on config error
- **Usage Query**
- Fixed auto-query interval timing issue
- Ensure 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
- **Configuration Management**
- Fixed Codex API Key auto-sync
- Fixed endpoint speed test functionality
- Fixed provider duplicate insertion position (next to original provider)
- Fixed custom endpoint preservation in edit mode
- **Startup Issues**
- Force exit on config error (no silent fallback)
- Eliminate code duplication causing initialization errors
### 🏗️ Technical Improvements (For Developers)
**Backend Refactoring (Rust)** - Completed 5-phase refactoring:
- **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**: 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)
**Frontend Refactoring (React + TypeScript)** - Completed 4-stage refactoring:
- **Stage 1**: 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
**Testing System**:
- Hooks unit tests 100% coverage
- Integration tests covering key processes (App, SettingsDialog, MCP Panel)
- MSW mocking backend API to ensure test independence
**Code Quality**:
- Unified parameter format: All Tauri commands migrated to camelCase (Tauri 2 specification)
- `AppType` renamed to `AppId`: Semantically clearer
- Unified parsing with `FromStr` trait: Centralized `app` parameter parsing
- Eliminate code duplication: DRY violations cleanup
- Remove unused code: `missing_param` helper function, deprecated `tauri-api.ts`, redundant `KimiModelSelector` component
**Internal Optimizations**:
- **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 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
- **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
## [3.5.0] - 2025-01-15
### ⚠ Breaking Changes
@@ -257,13 +365,35 @@ For users upgrading from v2.x (Electron version):
### ⚠️ Breaking Changes
- Tauri 命令统一仅接受 `app` 参数,移除历史 `app_type`/`appType` 兼容路径;传入未知 `app` 时会明确报错,并提示可选值。
- **Runtime auto-migration from v1 to v2 config format has been removed**
- `MultiAppConfig::load()` no longer automatically migrates v1 configs
- When a v1 config is detected, the app now returns a clear error with migration instructions
- **Migration path**: Install v3.2.x to perform one-time auto-migration, OR manually edit `~/.cc-switch/config.json` to v2 format
- **Rationale**: Separates concerns (load() should be read-only), fail-fast principle, simplifies maintenance
- Related: `app_config.rs` (v1 detection improved with structural analysis), `app_config_load.rs` (comprehensive test coverage added)
- **Legacy v1 copy file migration logic has been removed**
- Removed entire `migration.rs` module (435 lines) that handled one-time migration from v3.1.0 to v3.2.0
- No longer scans/merges legacy copy files (`settings-*.json`, `auth-*.json`, `config-*.toml`)
- No longer archives copy files or performs automatic deduplication
- **Migration path**: Users upgrading from v3.1.0 must first upgrade to v3.2.x to automatically migrate their configurations
- **Benefits**: Improved startup performance (no file scanning), reduced code complexity, cleaner codebase
- **Tauri commands now only accept `app` parameter**
- Removed legacy `app_type`/`appType` compatibility paths
- Explicit error with available values when unknown `app` is provided
### 🔧 Improvements
- 统一 `AppType` 解析:集中到 `FromStr` 实现,命令层不再各自实现 `parse_app()`,减少重复与漂移。
- 错误消息本地化与更友好:对不支持的 `app` 返回中英双语提示,并包含可选值清单。
- Unified `AppType` parsing: centralized to `FromStr` implementation, command layer no longer implements separate `parse_app()`, reducing code duplication and drift
- Localized and user-friendly error messages: returns bilingual (Chinese/English) hints for unsupported `app` values with a list of available options
- Simplified startup logic: Only ensures config structure exists, no migration overhead
### 🧪 Tests
- 新增单元测试覆盖 `AppType::from_str`:大小写、裁剪空白、未知值错误消息。
- Added unit tests covering `AppType::from_str`: case sensitivity, whitespace trimming, unknown value error messages
- Added comprehensive config loading tests:
- `load_v1_config_returns_error_and_does_not_write`
- `load_v1_with_extra_version_still_treated_as_v1`
- `load_invalid_json_returns_parse_error_and_does_not_write`
- `load_valid_v2_config_succeeds`

408
README.md
View File

@@ -1,284 +1,346 @@
# Claude Code & Codex 供应商切换器
<div align="center">
[![Version](https://img.shields.io/badge/version-3.5.1-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)
[![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/)
一个用于管理和切换 Claude Code 与 Codex 不同供应商配置的桌面应用。
English | [中文](README_ZH.md) | [Changelog](CHANGELOG.md)
> v3.5.0 :新增 **MCP 管理**、**配置导入/导出**、**端点速度测试**功能,完善国际化覆盖,新增 Longcat、kat-coder 预设,标准化发布文件命名规范。
A desktop application for managing and switching between different provider configurations & MCP for Claude Code and Codex.
> v3.4.0 :新增 i18next 国际化还有部分未完成、对新模型qwen-3-max, GLM-4.6, DeepSeek-V3.2-Exp的支持、Claude 插件、单实例守护、托盘最小化及安装器优化等。
</div>
> v3.3.0 VS Code Codex 插件一键配置/移除默认自动同步、Codex 通用配置片段与自定义向导增强、WSL 环境支持、跨平台托盘与 UI 优化。(该 VS Code 写入功能已在 v3.4.x 停用)
## ❤Sponsor
> v3.2.0 :全新 UI、macOS系统托盘、内置更新器、原子写入与回滚、改进暗色样式、单一事实源SSOT与一次性迁移/归档。
![Zhipu GLM](assets/partners/banners/glm-en.jpg)
> v3.1.0 :新增 Codex 供应商管理与一键切换,支持导入当前 Codex 配置为默认供应商,并在内部配置从 v1 → v2 迁移前自动备份(详见下文“迁移与归档”)。
This project is sponsored by Z.ai, supporting us with their GLM CODING PLAN.
> v3.0.0 重大更新:从 Electron 完全迁移到 Tauri 2.0,应用体积显著降低、启动性能大幅提升。
GLM CODING PLAN is a subscription service designed for AI coding, starting at just $3/month. It provides access to their flagship GLM-4.6 model across 10+ popular AI coding tools (Claude Code, Cline, Roo Code, etc.), offering developers top-tier, fast, and stable coding experiences.
## 功能特性v3.5.0
Get 10% OFF the GLM CODING PLAN with [this link](https://z.ai/subscribe?ic=8JVLJQFSKB)!
- **MCP (Model Context Protocol) 管理**:完整的 MCP 服务器配置管理系统
- 支持 stdio 和 http 服务器类型,并提供命令校验
- 内置常用 MCP 服务器模板(如 mcp-fetch 等)
- 实时启用/禁用 MCP 服务器,原子文件写入防止配置损坏
- **配置导入/导出**:备份和恢复你的供应商配置
- 一键导出所有配置到 JSON 文件
- 导入配置时自动验证并备份,自动轮换备份(保留最近 10 个)
- 带有详细状态反馈的进度模态框
- **端点速度测试**:测试 API 端点响应时间
- 测量不同供应商端点的延迟,可视化连接质量指示器
- 帮助用户选择最快的供应商
- **国际化与语言切换**:完整的 i18next 国际化覆盖,默认显示中文,可在设置中快速切换到英文,界面文案自动实时刷新。
- **Claude 插件同步**:内置按钮可一键应用或恢复 Claude 插件配置,切换供应商后立即生效。
- **供应商预设扩展**:新增 Longcat、kat-coder 等预设,更新 GLM 供应商配置至最新模型。
- **系统托盘与窗口行为**窗口关闭可最小化到托盘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>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
![主界面](screenshots/main.png)
| Main Interface | Add Provider |
| :-----------------------------------------------: | :--------------------------------------------: |
| ![Main Interface](assets/screenshots/main-en.png) | ![Add Provider](assets/screenshots/add-en.png) |
### 添加供应商
## Features
![添加供应商](screenshots/add.png)
### Current Version: v3.6.1 | [Full Changelog](CHANGELOG.md)
## 下载安装
**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
- **Windows**: Windows 10 及以上
- **macOS**: macOS 10.15 (Catalina) 及以上
- **Linux**: Ubuntu 22.04+ / Debian 11+ / Fedora 34+ 等主流发行版
**v3.6 Highlights**
### Windows 用户
- 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
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch-v{版本号}-Windows.msi` 安装包或者 `CC-Switch-v{版本号}-Windows-Portable.zip` 绿色版。
**System Features**
### macOS 用户
- System tray with quick switching
- Single instance daemon
- Built-in auto-updater
- Atomic writes with rollback protection
**方式一:通过 Homebrew 安装(推荐)**
## Download & Installation
### System Requirements
- **Windows**: Windows 10 and above
- **macOS**: macOS 10.15 (Catalina) and above
- **Linux**: Ubuntu 22.04+ / Debian 11+ / Fedora 34+ and other mainstream distributions
### Windows Users
Download the latest `CC-Switch-v{version}-Windows.msi` installer or `CC-Switch-v{version}-Windows-Portable.zip` portable version from the [Releases](../../releases) page.
### macOS Users
**Method 1: Install via Homebrew (Recommended)**
```bash
brew tap farion1231/ccswitch
brew install --cask cc-switch
```
更新:
Update:
```bash
brew upgrade --cask cc-switch
```
**方式二:手动下载**
**Method 2: Manual Download**
从 [Releases](../../releases) 页面下载 `CC-Switch-v{版本号}-macOS.zip` 解压使用。
Download `CC-Switch-v{version}-macOS.zip` from the [Releases](../../releases) page and extract to use.
> **注意**:由于作者没有苹果开发者账号,首次打开可能出现"未知开发者"警告,请先关闭,然后前往"系统设置" → "隐私与安全性" → 点击"仍要打开",之后便可以正常打开
> **Note**: Since the author doesn't have an Apple Developer account, you may see an "unidentified developer" warning on first launch. Please close it first, then go to "System Settings" → "Privacy & Security" → click "Open Anyway", and you'll be able to open it normally afterwards.
### Linux 用户
### Linux Users
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch-v{版本号}-Linux.deb` 包或者 `CC-Switch-v{版本号}-Linux.AppImage` 安装包。
Download the latest `CC-Switch-v{version}-Linux.deb` package or `CC-Switch-v{version}-Linux.AppImage` from the [Releases](../../releases) page.
## 使用说明
## Quick Start
1. 点击"添加供应商"添加你的 API 配置
2. 切换方式:
- 在主界面选择供应商后点击切换
- 或通过“系统托盘(菜单栏)”直接选择目标供应商,立即生效
3. 切换会写入对应应用的“live 配置文件”Claude`settings.json`Codex`auth.json` + `config.toml`
4. 重启或新开终端以确保生效
5. 若需切回官方登录,在预设中选择“官方登录”并切换即可;重启终端后按官方流程登录
### Basic Usage
### MCP 配置说明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)
- 管理位置:所有 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 Management
### 检查更新
- **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)
- 在“设置”中点击“检查更新”,若内置 Updater 配置可用将直接检测与下载;否则会回退打开 Releases 页面
### Configuration Files
### 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 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 说明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 config: `~/.codex/auth.json` (required) + `config.toml` (optional)
- API key field: `OPENAI_API_KEY` in `auth.json`
- MCP servers: `~/.codex/config.toml``[mcp.servers]`
### 迁移与归档(自 v3.2.0 起)
**CC Switch Storage**
- 一次性迁移:首次启动 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` 以便回滚
- 注意:迁移后不再持续归档日常切换/编辑操作,如需长期审计请自备备份方案
- Main config (SSOT): `~/.cc-switch/config.json`
- Settings: `~/.cc-switch/settings.json`
- Backups: `~/.cc-switch/backups/` (auto-rotate, keep 10)
## 架构总览v3.5.x
### Cloud Sync Setup
- 前端Renderer
- 技术栈TypeScript + React 18 + Vite + TailwindCSS
- 数据层TanStack React Query 统一查询与变更(`@/lib/query`Tauri API 统一封装(`@/lib/api`
- 事件流:监听后端 `provider-switched` 事件,驱动 UI 刷新与托盘状态一致
- 组织结构按领域拆分组件providers/settings/mcp动作逻辑下沉至 Hooks`useProviderActions`
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
- 后端Tauri + Rust
- Commands接口层`src-tauri/src/commands/*` 按领域拆分provider/config/mcp 等)
- Services业务层`src-tauri/src/services/*` 承载核心逻辑Provider/MCP/Config/Speedtest
- 模型与状态:`provider.rs`(领域模型)+ `app_config.rs`(多应用配置)+ `store.rs`(全局 RwLock
- 可靠性:
- 统一错误类型 `AppError`(包含本地化消息)
- 事务式变更(配置快照 + 失败回滚)与原子写入(避免半写入)
- 托盘菜单与事件:切换后重建菜单并向前端发射 `provider-switched` 事件
> **Note**: First launch auto-imports existing Claude/Codex configs as default provider.
- 设计要点SSOT
- 单一事实源:供应商配置集中存放于 `~/.cc-switch/config.json`
- 切换时仅写 live 配置Claude: `settings.json`Codex: `auth.json` + `config.toml`
- 首次缺省导入:当某应用无任何供应商时,会从已有 live 配置生成默认项
## Architecture Overview
- 兼容性与变更
- 命令参数统一Tauri 命令仅接受 `app`(值为 `claude` / `codex`
- 前端类型统一:使用 `AppId` 表达应用标识(替代历史 `AppType` 导出)
### Design Principles
## 开发
```
┌─────────────────────────────────────────────────────────────┐
│ 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) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────┘
```
### 环境要求
**Core Design Patterns**
- **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)
**Key Components**
- **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
**v3.6 Refactoring**
- 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
### Environment Requirements
- Node.js 18+
- pnpm 8+
- Rust 1.75+
- Tauri CLI 2.0+
- Rust 1.85+
- Tauri CLI 2.8+
### 开发命令
### Development Commands
```bash
# 安装依赖
# Install dependencies
pnpm install
# 开发模式(热重载)
# Dev mode (hot reload)
pnpm dev
# 类型检查
# Type check
pnpm typecheck
# 代码格式化
# Format code
pnpm format
# 检查代码格式
# Check code format
pnpm format:check
# 运行前端单元测试
# Run frontend unit tests
pnpm test:unit
# 监听模式运行测试
# Run tests in watch mode (recommended for development)
pnpm test:unit:watch
# 构建应用
# Build application
pnpm build
# 构建调试版本
# Build debug version
pnpm tauri build --debug
```
### Rust 后端开发
### Rust Backend Development
```bash
cd src-tauri
# 格式化 Rust 代码
# Format Rust code
cargo fmt
# 运行 clippy 检查
# Run clippy checks
cargo clippy
# 运行测试
# Run backend tests
cargo test
# Run specific tests
cargo test test_name
# Run tests with test-hooks feature
cargo test --features test-hooks
```
## 技术栈
### Testing Guide (v3.6 New)
- **[Tauri 2](https://tauri.app/)** - 跨平台桌面应用框架(集成 updater/process/opener/log/tray-icon
- **[React 18](https://react.dev/)** - 用户界面库
- **[TypeScript](https://www.typescriptlang.org/)** - 类型安全的 JavaScript
- **[Vite](https://vitejs.dev/)** - 极速的前端构建工具
- **[Rust](https://www.rust-lang.org/)** - 系统级编程语言(后端)
- **[TanStack Query](https://tanstack.com/query/latest)** - 前端数据获取与缓存
- **[i18next](https://www.i18next.com/)** - 国际化框架
**Frontend Testing**:
## 项目结构
- Uses **vitest** as test framework
- Uses **MSW (Mock Service Worker)** to mock Tauri API calls
- Uses **@testing-library/react** for component testing
**Test Coverage**:
- Hooks unit tests (100% coverage)
- `useProviderActions` - Provider operations
- `useMcpActions` - MCP management
- `useSettings` series - Settings management
- `useImportExport` - Import/export
- Integration tests
- App main application flow
- SettingsDialog complete interaction
- MCP panel functionality
**Running Tests**:
```bash
# Run all tests
pnpm test:unit
# Watch mode (auto re-run)
pnpm test:unit:watch
# With coverage report
pnpm test:unit --coverage
```
## Tech Stack
**Frontend**: React 18 · TypeScript · Vite · TailwindCSS 4 · TanStack Query v5 · react-i18next · react-hook-form · zod · shadcn/ui · @dnd-kit
**Backend**: Tauri 2.8 · Rust · serde · tokio · thiserror · tauri-plugin-updater/process/dialog/store/log
**Testing**: vitest · MSW · @testing-library/react
## Project Structure
```
├── src/ # 前端代码 (React + TypeScript)
│ ├── components/ # React 组件(providers/settings/mcp/ui 等)
│ ├── hooks/ # 领域动作与状态(如 useProviderActions
├── src/ # Frontend (React + TypeScript)
│ ├── components/ # UI components (providers/settings/mcp/ui)
│ ├── hooks/ # Custom hooks (business logic)
│ ├── lib/
│ │ ├── api/ # Tauri API 封装providers/settings/mcp 等)
│ │ └── query/ # TanStack Query 查询/变更与 client
│ ├── i18n/ # 国际化资源
│ ├── config/ # 供应商/MCP 预设
│ └── utils/ # 工具函数
├── src-tauri/ # 后端代码 (Rust)
── src/ # Rust 源代码
├── commands/ # Tauri 命令定义(按域拆分)
├── services/ # 领域服务Provider/MCP/Speedtest 等)
├── mcp.rs # MCP 同步与规范化
├── migration.rs # 配置迁移逻辑
├── config.rs # 配置文件管理
── provider.rs # 供应商管理逻辑
└── store.rs # 状态管理
│ ├── capabilities/ # 权限配置
│ └── icons/ # 应用图标资源
└── screenshots/ # 界面截图
│ │ ├── api/ # Tauri API wrapper (type-safe)
│ │ └── query/ # TanStack Query config
│ ├── 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
查看 [CHANGELOG.md](CHANGELOG.md) 了解版本更新详情。
See [CHANGELOG.md](CHANGELOG.md) for version update details.
## Electron 旧版
## Legacy Electron Version
[Releases](../../releases) 里保留 v2.0.3 Electron 旧版
[Releases](../../releases) retains v2.0.3 legacy Electron version
如果需要旧版 Electron 代码,可以拉取 electron-legacy 分支
If you need legacy Electron code, you can pull the electron-legacy branch
## 贡献
## Contributing
欢迎提交 Issue 反馈问题和建议!
Issues and suggestions are welcome!
提交 PR 前请确保:
- 通过类型检查:`pnpm typecheck`
- 通过格式检查:`pnpm format:check`
- 通过单元测试:`pnpm test:unit`
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
## Star History

351
README_ZH.md Normal file
View File

@@ -0,0 +1,351 @@
<div align="center">
# Claude Code & Codex 供应商管理器
[![Version](https://img.shields.io/badge/version-3.6.1-blue.svg)](https://github.com/farion1231/cc-switch/releases)
[![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/)
[English](README.md) | 中文 | [更新日志](CHANGELOG.md)
一个用于管理和切换 Claude Code 与 Codex 不同供应商配置、MCP的桌面应用。
</div>
## ❤️赞助商
![智谱 GLM](assets/partners/banners/glm-zh.jpg)
感谢智谱AI的 GLM CODING PLAN 赞助了本项目!
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)购买可以享受九折优惠。
---
<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) |
## 功能特性
### 当前版本v3.6.1 | [完整更新日志](CHANGELOG.md)
**核心功能**
- **供应商管理**:一键切换 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
**系统功能**
- 系统托盘快速切换
- 单实例守护
- 内置自动更新器
- 原子写入与回滚保护
## 下载安装
### 系统要求
- **Windows**: Windows 10 及以上
- **macOS**: macOS 10.15 (Catalina) 及以上
- **Linux**: Ubuntu 22.04+ / Debian 11+ / Fedora 34+ 等主流发行版
### Windows 用户
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch-v{版本号}-Windows.msi` 安装包或者 `CC-Switch-v{版本号}-Windows-Portable.zip` 绿色版。
### macOS 用户
**方式一:通过 Homebrew 安装(推荐)**
```bash
brew tap farion1231/ccswitch
brew install --cask cc-switch
```
更新:
```bash
brew upgrade --cask cc-switch
```
**方式二:手动下载**
从 [Releases](../../releases) 页面下载 `CC-Switch-v{版本号}-macOS.zip` 解压使用。
> **注意**:由于作者没有苹果开发者账号,首次打开可能出现"未知开发者"警告,请先关闭,然后前往"系统设置" → "隐私与安全性" → 点击"仍要打开",之后便可以正常打开
### Linux 用户
从 [Releases](../../releases) 页面下载最新版本的 `CC-Switch-v{版本号}-Linux.deb` 包或者 `CC-Switch-v{版本号}-Linux.AppImage` 安装包。
## 快速开始
### 基本使用
1. **添加供应商**:点击"添加供应商" → 选择预设或创建自定义配置
2. **切换供应商**
- 主界面:选择供应商 → 点击"启用"
- 系统托盘:直接点击供应商名称(立即生效)
3. **生效方式**:重启终端或 Claude Code/Codex 以应用更改
4. **恢复官方登录**:选择"官方登录"预设,重启终端后使用 `/login`Claude或官方登录流程Codex
### MCP 管理
- **位置**:点击右上角"MCP"按钮
- **添加服务器**使用内置模板mcp-fetch、mcp-filesystem或自定义配置
- **启用/禁用**:切换开关以控制哪些服务器同步到 live 配置
- **同步**:启用的服务器自动同步到 `~/.claude.json`Claude`~/.codex/config.toml`Codex
### 配置文件
**Claude Code**
- Live 配置:`~/.claude/settings.json`(或 `claude.json`
- API key 字段:`env.ANTHROPIC_AUTH_TOKEN``env.ANTHROPIC_API_KEY`
- MCP 服务器:`~/.claude.json``mcpServers`
**Codex**
- Live 配置:`~/.codex/auth.json`(必需)+ `config.toml`(可选)
- API key 字段:`auth.json` 中的 `OPENAI_API_KEY`
- MCP 服务器:`~/.codex/config.toml``[mcp.servers]`
**CC Switch 存储**
- 主配置SSOT`~/.cc-switch/config.json`
- 设置:`~/.cc-switch/settings.json`
- 备份:`~/.cc-switch/backups/`(自动轮换,保留 10 个)
### 云同步设置
1. 前往设置 → "自定义配置目录"
2. 选择您的云同步文件夹Dropbox、OneDrive、iCloud、坚果云等
3. 重启应用以应用
4. 在其他设备上重复操作以启用跨设备同步
> **注意**:首次启动会自动导入现有 Claude/Codex 配置作为默认供应商。
## 架构总览
### 设计原则
```
┌─────────────────────────────────────────────────────────────┐
│ 前端 (React + TS) │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Components │ │ Hooks │ │ TanStack Query │ │
│ │ (UI) │──│ (业务逻辑) │──│ (缓存/同步) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
└────────────────────────┬────────────────────────────────────┘
│ Tauri IPC
┌────────────────────────▼────────────────────────────────────┐
│ 后端 (Tauri + Rust) │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Commands │ │ Services │ │ Models/Config │ │
│ │ (API 层) │──│ (业务层) │──│ (数据) │ │
│ └─────────────┘ └──────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────┘
```
**核心设计模式**
- **SSOT**(单一事实源):所有供应商配置存储在 `~/.cc-switch/config.json`
- **双向同步**:切换时写入 live 文件,编辑当前供应商时从 live 回填
- **原子写入**:临时文件 + 重命名模式防止配置损坏
- **并发安全**RwLock 与作用域守卫避免死锁
- **分层架构**清晰分离Commands → Services → Models
**核心组件**
- **ProviderService**:供应商增删改查、切换、回填、排序
- **McpService**MCP 服务器管理、导入导出、live 文件同步
- **ConfigService**:配置导入导出、备份轮换
- **SpeedtestService**API 端点延迟测量
**v3.6 重构**
- 后端5 阶段重构(错误处理 → 命令拆分 → 测试 → 服务 → 并发)
- 前端4 阶段重构(测试基础 → hooks → 组件 → 清理)
- 测试100% hooks 覆盖 + 集成测试vitest + MSW
## 开发
### 环境要求
- Node.js 18+
- pnpm 8+
- Rust 1.85+
- Tauri CLI 2.8+
### 开发命令
```bash
# 安装依赖
pnpm install
# 开发模式(热重载)
pnpm dev
# 类型检查
pnpm typecheck
# 代码格式化
pnpm format
# 检查代码格式
pnpm format:check
# 运行前端单元测试
pnpm test:unit
# 监听模式运行测试(推荐开发时使用)
pnpm test:unit:watch
# 构建应用
pnpm build
# 构建调试版本
pnpm tauri build --debug
```
### Rust 后端开发
```bash
cd src-tauri
# 格式化 Rust 代码
cargo fmt
# 运行 clippy 检查
cargo clippy
# 运行后端测试
cargo test
# 运行特定测试
cargo test test_name
# 运行带测试 hooks 的测试
cargo test --features test-hooks
```
### 测试说明v3.6 新增)
**前端测试**
- 使用 **vitest** 作为测试框架
- 使用 **MSW (Mock Service Worker)** 模拟 Tauri API 调用
- 使用 **@testing-library/react** 进行组件测试
**测试覆盖**
- Hooks 单元测试100% 覆盖)
- `useProviderActions` - 供应商操作
- `useMcpActions` - MCP 管理
- `useSettings` 系列 - 设置管理
- `useImportExport` - 导入导出
- 集成测试
- App 主应用流程
- SettingsDialog 完整交互
- MCP 面板功能
**运行测试**
```bash
# 运行所有测试
pnpm test:unit
# 监听模式(自动重跑)
pnpm test:unit:watch
# 带覆盖率报告
pnpm test:unit --coverage
```
## 技术栈
**前端**React 18 · TypeScript · Vite · TailwindCSS 4 · TanStack Query v5 · react-i18next · react-hook-form · zod · shadcn/ui · @dnd-kit
**后端**Tauri 2.8 · Rust · serde · tokio · thiserror · tauri-plugin-updater/process/dialog/store/log
**测试**vitest · MSW · @testing-library/react
## 项目结构
```
├── src/ # 前端 (React + TypeScript)
│ ├── components/ # UI 组件 (providers/settings/mcp/ui)
│ ├── hooks/ # 自定义 hooks (业务逻辑)
│ ├── lib/
│ │ ├── api/ # Tauri API 封装(类型安全)
│ │ └── query/ # TanStack Query 配置
│ ├── i18n/locales/ # 翻译 (zh/en)
│ ├── config/ # 预设 (providers/mcp)
│ └── types/ # TypeScript 类型定义
├── src-tauri/ # 后端 (Rust)
│ └── src/
│ ├── commands/ # Tauri 命令层(按领域)
│ ├── services/ # 业务逻辑层
│ ├── app_config.rs # 配置数据模型
│ ├── provider.rs # 供应商领域模型
│ ├── mcp.rs # MCP 同步与校验
│ └── lib.rs # 应用入口 & 托盘菜单
├── tests/ # 前端测试
│ ├── hooks/ # 单元测试
│ └── components/ # 集成测试
└── assets/ # 截图 & 合作商资源
```
## 更新日志
查看 [CHANGELOG.md](CHANGELOG.md) 了解版本更新详情。
## Electron 旧版
[Releases](../../releases) 里保留 v2.0.3 Electron 旧版
如果需要旧版 Electron 代码,可以拉取 electron-legacy 分支
## 贡献
欢迎提交 Issue 反馈问题和建议!
提交 PR 前请确保:
- 通过类型检查:`pnpm typecheck`
- 通过格式检查:`pnpm format:check`
- 通过单元测试:`pnpm test:unit`
- 功能性 PR 请先经过 issue 区讨论
## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=farion1231/cc-switch&type=Date)](https://www.star-history.com/#farion1231/cc-switch&Date)
## License
MIT © Jason Young

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 205 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.5.1",
"version": "3.6.1",
"description": "Claude Code & Codex 供应商切换工具",
"scripts": {
"dev": "pnpm tauri dev",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 200 KiB

2
src-tauri/Cargo.lock generated
View File

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

View File

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

View File

@@ -89,7 +89,7 @@ impl Default for MultiAppConfig {
}
impl MultiAppConfig {
/// 从文件加载配置(处理v1到v2的迁移
/// 从文件加载配置(仅支持 v2 结构
pub fn load() -> Result<Self, AppError> {
let config_path = get_app_config_path();
@@ -102,45 +102,27 @@ impl MultiAppConfig {
let content =
std::fs::read_to_string(&config_path).map_err(|e| AppError::io(&config_path, e))?;
// 检查是否是旧版本格式v1
if let Ok(v1_config) = serde_json::from_str::<ProviderManager>(&content) {
log::info!("检测到v1配置自动迁移到v2");
// 迁移到新格式
let mut apps = HashMap::new();
apps.insert("claude".to_string(), v1_config);
apps.insert("codex".to_string(), ProviderManager::default());
let config = Self {
version: 2,
apps,
mcp: McpRoot::default(),
};
// 迁移前备份旧版(v1)配置文件
let backup_dir = get_app_config_dir();
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let backup_path = backup_dir.join(format!("config.v1.backup.{}.json", ts));
match copy_file(&config_path, &backup_path) {
Ok(()) => log::info!(
"已备份旧版配置文件: {} -> {}",
config_path.display(),
backup_path.display()
),
Err(e) => log::warn!("备份旧版配置文件失败: {}", e),
}
// 保存迁移后的配置
config.save()?;
return Ok(config);
// 先解析为 Value以便严格判定是否为 v1 结构;
// 满足:顶层同时包含 providers(object) + current(string),且不包含 version/apps/mcp 关键键,即视为 v1
let value: serde_json::Value =
serde_json::from_str(&content).map_err(|e| AppError::json(&config_path, e))?;
let is_v1 = value.as_object().is_some_and(|map| {
let has_providers = map.get("providers").map(|v| v.is_object()).unwrap_or(false);
let has_current = map.get("current").map(|v| v.is_string()).unwrap_or(false);
// v1 的充分必要条件:有 providers 和 current且 apps 不存在version/mcp 可能存在但不作为 v2 判据)
let has_apps = map.contains_key("apps");
has_providers && has_current && !has_apps
});
if is_v1 {
return Err(AppError::localized(
"config.unsupported_v1",
"检测到旧版 v1 配置格式。当前版本已不再支持运行时自动迁移。\n\n解决方案:\n1. 安装 v3.2.x 版本进行一次性自动迁移\n2. 或手动编辑 ~/.cc-switch/config.json将顶层结构调整为\n {\"version\": 2, \"claude\": {...}, \"codex\": {...}, \"mcp\": {...}}\n\n",
"Detected legacy v1 config. Runtime auto-migration is no longer supported.\n\nSolutions:\n1. Install v3.2.x for one-time auto-migration\n2. Or manually edit ~/.cc-switch/config.json to adjust the top-level structure:\n {\"version\": 2, \"claude\": {...}, \"codex\": {...}, \"mcp\": {...}}\n\n",
));
}
// 尝试读取v2格式
serde_json::from_str::<Self>(&content).map_err(|e| AppError::json(&config_path, e))
// 解析 v2 结构
serde_json::from_value::<Self>(value).map_err(|e| AppError::json(&config_path, e))
}
/// 保存配置到文件

View File

@@ -56,8 +56,6 @@ pub fn delete_codex_provider_config(
Ok(())
}
//(移除未使用的备份/保存/恢复/导入函数,避免 dead_code 告警)
/// 原子写 Codex 的 `auth.json` 与 `config.toml`,在第二步失败时回滚第一步
pub fn write_codex_live_atomic(
auth: &Value,
@@ -118,15 +116,6 @@ pub fn read_codex_config_text() -> Result<String, AppError> {
}
}
/// 从给定路径读取 config.toml 文本(路径存在时);路径不存在则返回空字符串
pub fn read_config_text_from_path(path: &Path) -> Result<String, AppError> {
if path.exists() {
std::fs::read_to_string(path).map_err(|e| AppError::io(path, e))
} else {
Ok(String::new())
}
}
/// 对非空的 TOML 文本进行语法校验
pub fn validate_config_toml(text: &str) -> Result<(), AppError> {
if text.trim().is_empty() {
@@ -143,10 +132,3 @@ pub fn read_and_validate_codex_config_text() -> Result<String, AppError> {
validate_config_toml(&s)?;
Ok(s)
}
/// 从指定路径读取并校验 config.toml返回文本可能为空
pub fn read_and_validate_config_from_path(path: &Path) -> Result<String, AppError> {
let s = read_config_text_from_path(path)?;
validate_config_toml(&s)?;
Ok(s)
}

View File

@@ -73,9 +73,9 @@ pub async fn open_config_folder(handle: AppHandle, app: String) -> Result<bool,
#[tauri::command]
pub async fn pick_directory(
app: AppHandle,
default_path: Option<String>,
#[allow(non_snake_case)] defaultPath: Option<String>,
) -> Result<Option<String>, String> {
let initial = default_path
let initial = defaultPath
.map(|p| p.trim().to_string())
.filter(|p| !p.is_empty());

View File

@@ -11,14 +11,16 @@ use crate::store::AppState;
/// 导出配置文件
#[tauri::command]
pub async fn export_config_to_file(file_path: String) -> Result<Value, String> {
pub async fn export_config_to_file(
#[allow(non_snake_case)] filePath: String,
) -> Result<Value, String> {
tauri::async_runtime::spawn_blocking(move || {
let target_path = PathBuf::from(&file_path);
let target_path = PathBuf::from(&filePath);
ConfigService::export_config_to_path(&target_path)?;
Ok::<_, AppError>(json!({
"success": true,
"message": "Configuration exported successfully",
"filePath": file_path
"filePath": filePath
}))
})
.await
@@ -29,11 +31,11 @@ pub async fn export_config_to_file(file_path: String) -> Result<Value, String> {
/// 从文件导入配置
#[tauri::command]
pub async fn import_config_from_file(
file_path: String,
#[allow(non_snake_case)] filePath: String,
state: State<'_, AppState>,
) -> Result<Value, String> {
let (new_config, backup_id) = tauri::async_runtime::spawn_blocking(move || {
let path_buf = PathBuf::from(&file_path);
let path_buf = PathBuf::from(&filePath);
ConfigService::load_config_for_import(&path_buf)
})
.await
@@ -77,13 +79,13 @@ pub async fn sync_current_providers_live(state: State<'_, AppState>) -> Result<V
#[tauri::command]
pub async fn save_file_dialog<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
default_name: String,
#[allow(non_snake_case)] defaultName: String,
) -> Result<Option<String>, String> {
let dialog = app.dialog();
let result = dialog
.file()
.add_filter("JSON", &["json"])
.set_file_name(&default_name)
.set_file_name(&defaultName)
.blocking_save_file();
Ok(result.map(|p| p.to_string()))

View File

@@ -1,5 +1,6 @@
#![allow(non_snake_case)]
use crate::init_status::InitErrorPayload;
use tauri::AppHandle;
use tauri_plugin_opener::OpenerExt;
@@ -43,3 +44,10 @@ pub async fn is_portable_mode() -> Result<bool, String> {
Ok(false)
}
}
/// 获取应用启动阶段的初始化错误(若有)。
/// 用于前端在早期主动拉取,避免事件订阅竞态导致的提示缺失。
#[tauri::command]
pub async fn get_init_error() -> Result<Option<InitErrorPayload>, String> {
Ok(crate::init_status::get_init_error())
}

View File

@@ -6,11 +6,6 @@ use crate::error::AppError;
use crate::provider::Provider;
use crate::services::{EndpointLatency, ProviderService, ProviderSortUpdate, SpeedtestService};
use crate::store::AppState;
fn missing_param(param: &str) -> String {
format!("缺少 {} 参数 (Missing {} parameter)", param, param)
}
use std::str::FromStr;
/// 获取所有供应商
@@ -113,19 +108,50 @@ pub fn import_default_config(state: State<'_, AppState>, app: String) -> Result<
}
/// 查询供应商用量
#[allow(non_snake_case)]
#[tauri::command]
pub async fn query_provider_usage(
pub async fn queryProviderUsage(
state: State<'_, AppState>,
provider_id: Option<String>,
#[allow(non_snake_case)] providerId: String, // 使用 camelCase 匹配前端
app: String,
) -> Result<crate::provider::UsageResult, String> {
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
ProviderService::query_usage(state.inner(), app_type, &provider_id)
ProviderService::query_usage(state.inner(), app_type, &providerId)
.await
.map_err(|e| e.to_string())
}
/// 测试用量脚本(使用当前编辑器中的脚本,不保存)
#[allow(non_snake_case)]
#[allow(clippy::too_many_arguments)]
#[tauri::command]
pub async fn testUsageScript(
state: State<'_, AppState>,
#[allow(non_snake_case)] providerId: String,
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> {
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
ProviderService::test_usage_script(
state.inner(),
app_type,
&providerId,
&scriptCode,
timeout.unwrap_or(10),
apiKey.as_deref(),
baseUrl.as_deref(),
accessToken.as_deref(),
userId.as_deref(),
)
.await
.map_err(|e| e.to_string())
}
/// 读取当前生效的配置内容
#[tauri::command]
pub fn read_live_provider_settings(app: String) -> Result<serde_json::Value, String> {
@@ -137,9 +163,9 @@ pub fn read_live_provider_settings(app: String) -> Result<serde_json::Value, Str
#[tauri::command]
pub async fn test_api_endpoints(
urls: Vec<String>,
timeout_secs: Option<u64>,
#[allow(non_snake_case)] timeoutSecs: Option<u64>,
) -> Result<Vec<EndpointLatency>, String> {
SpeedtestService::test_endpoints(urls, timeout_secs)
SpeedtestService::test_endpoints(urls, timeoutSecs)
.await
.map_err(|e| e.to_string())
}
@@ -149,11 +175,10 @@ pub async fn test_api_endpoints(
pub fn get_custom_endpoints(
state: State<'_, AppState>,
app: String,
provider_id: Option<String>,
#[allow(non_snake_case)] providerId: String,
) -> Result<Vec<crate::settings::CustomEndpoint>, String> {
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
ProviderService::get_custom_endpoints(state.inner(), app_type, &provider_id)
ProviderService::get_custom_endpoints(state.inner(), app_type, &providerId)
.map_err(|e| e.to_string())
}
@@ -162,12 +187,11 @@ pub fn get_custom_endpoints(
pub fn add_custom_endpoint(
state: State<'_, AppState>,
app: String,
provider_id: Option<String>,
#[allow(non_snake_case)] providerId: String,
url: String,
) -> Result<(), String> {
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
ProviderService::add_custom_endpoint(state.inner(), app_type, &provider_id, url)
ProviderService::add_custom_endpoint(state.inner(), app_type, &providerId, url)
.map_err(|e| e.to_string())
}
@@ -176,12 +200,11 @@ pub fn add_custom_endpoint(
pub fn remove_custom_endpoint(
state: State<'_, AppState>,
app: String,
provider_id: Option<String>,
#[allow(non_snake_case)] providerId: String,
url: String,
) -> Result<(), String> {
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
ProviderService::remove_custom_endpoint(state.inner(), app_type, &provider_id, url)
ProviderService::remove_custom_endpoint(state.inner(), app_type, &providerId, url)
.map_err(|e| e.to_string())
}
@@ -190,12 +213,11 @@ pub fn remove_custom_endpoint(
pub fn update_endpoint_last_used(
state: State<'_, AppState>,
app: String,
provider_id: Option<String>,
#[allow(non_snake_case)] providerId: String,
url: String,
) -> Result<(), String> {
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
ProviderService::update_endpoint_last_used(state.inner(), app_type, &provider_id, url)
ProviderService::update_endpoint_last_used(state.inner(), app_type, &providerId, url)
.map_err(|e| e.to_string())
}

View File

@@ -78,55 +78,6 @@ pub fn get_app_config_path() -> PathBuf {
get_app_config_dir().join("config.json")
}
/// 归档根目录 ~/.cc-switch/archive
pub fn get_archive_root() -> PathBuf {
get_app_config_dir().join("archive")
}
fn ensure_unique_path(dest: PathBuf) -> PathBuf {
if !dest.exists() {
return dest;
}
let file_name = dest
.file_stem()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_else(|| "file".into());
let ext = dest
.extension()
.map(|s| format!(".{}", s.to_string_lossy()))
.unwrap_or_default();
let parent = dest.parent().map(|p| p.to_path_buf()).unwrap_or_default();
for i in 2..1000 {
let mut candidate = parent.clone();
candidate.push(format!("{}-{}{}", file_name, i, ext));
if !candidate.exists() {
return candidate;
}
}
dest
}
/// 将现有文件归档到 `~/.cc-switch/archive/<ts>/<category>/` 下,返回归档路径
pub fn archive_file(ts: u64, category: &str, src: &Path) -> Result<Option<PathBuf>, AppError> {
if !src.exists() {
return Ok(None);
}
let mut dest_dir = get_archive_root();
dest_dir.push(ts.to_string());
dest_dir.push(category);
fs::create_dir_all(&dest_dir).map_err(|e| AppError::io(&dest_dir, e))?;
let file_name = src
.file_name()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_else(|| "file".into());
let mut dest = dest_dir.join(file_name);
dest = ensure_unique_path(dest);
copy_file(src, &dest)?;
Ok(Some(dest))
}
/// 清理供应商名称,确保文件名安全
pub fn sanitize_provider_name(name: &str) -> String {
name.chars()
@@ -304,5 +255,3 @@ pub fn get_claude_config_status() -> ConfigStatus {
path: path.to_string_lossy().to_string(),
}
}
//(移除未使用的备份/导入函数,避免 dead_code 告警)

View File

@@ -40,8 +40,6 @@ pub enum AppError {
},
#[error("锁获取失败: {0}")]
Lock(String),
#[error("供应商不存在: {0}")]
ProviderNotFound(String),
#[error("MCP 校验失败: {0}")]
McpValidation(String),
#[error("{0}")]

View File

@@ -0,0 +1,41 @@
use serde::Serialize;
use std::sync::{OnceLock, RwLock};
#[derive(Debug, Clone, Serialize)]
pub struct InitErrorPayload {
pub path: String,
pub error: String,
}
static INIT_ERROR: OnceLock<RwLock<Option<InitErrorPayload>>> = OnceLock::new();
fn cell() -> &'static RwLock<Option<InitErrorPayload>> {
INIT_ERROR.get_or_init(|| RwLock::new(None))
}
pub fn set_init_error(payload: InitErrorPayload) {
if let Ok(mut guard) = cell().write() {
*guard = Some(payload);
}
}
pub fn get_init_error() -> Option<InitErrorPayload> {
cell().read().ok()?.clone()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn init_error_roundtrip() {
let payload = InitErrorPayload {
path: "/tmp/config.json".into(),
error: "broken json".into(),
};
set_init_error(payload.clone());
let got = get_init_error().expect("should get payload back");
assert_eq!(got.path, payload.path);
assert_eq!(got.error, payload.error);
}
}

View File

@@ -6,8 +6,8 @@ mod codex_config;
mod commands;
mod config;
mod error;
mod init_status;
mod mcp;
mod migration;
mod provider;
mod services;
mod settings;
@@ -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);
// 执行切换
@@ -437,27 +443,42 @@ pub fn run() {
app_store::refresh_app_config_dir_override(app.handle());
// 初始化应用状态(仅创建一次,并在本函数末尾注入 manage
let app_state = AppState::new();
// 如果配置解析失败,则向前端发送错误事件并提前结束 setup不落盘、不覆盖配置
let app_state = match AppState::try_new() {
Ok(state) => state,
Err(err) => {
let path = crate::config::get_app_config_path();
let payload_json = serde_json::json!({
"path": path.display().to_string(),
"error": err.to_string(),
});
// 事件通知(可能早于前端订阅,不保证送达)
if let Err(e) = app.emit("configLoadError", payload_json) {
log::error!("发射配置加载错误事件失败: {}", e);
}
// 同时缓存错误,供前端启动阶段主动拉取
crate::init_status::set_init_error(crate::init_status::InitErrorPayload {
path: path.display().to_string(),
error: err.to_string(),
});
// 不再继续构建托盘/命令依赖的状态,交由前端提示后退出。
return Ok(());
}
};
// 迁移旧的 app_config_dir 配置到 Store
if let Err(e) = app_store::migrate_app_config_dir_from_settings(app.handle()) {
log::warn!("迁移 app_config_dir 失败: {}", e);
}
// 首次启动迁移:扫描副本文件,合并到 config.json并归档副本旧 config.json 先归档
// 确保配置结构就绪(已移除旧版本的副本迁移逻辑)
{
let mut config_guard = app_state.config.write().unwrap();
let migrated = migration::migrate_copies_into_config(&mut config_guard)?;
if migrated {
log::info!("已将副本文件导入到 config.json并完成归档");
}
// 确保两个 App 条目存在
config_guard.ensure_app(&app_config::AppType::Claude);
config_guard.ensure_app(&app_config::AppType::Codex);
}
// 保存配置
let _ = app_state.save();
// 启动阶段不再无条件保存,避免意外覆盖用户配置
// 创建动态托盘菜单
let menu = create_tray_menu(app.handle(), &app_state)?;
@@ -498,6 +519,7 @@ pub fn run() {
commands::open_config_folder,
commands::pick_directory,
commands::open_external,
commands::get_init_error,
commands::get_app_config_path,
commands::open_app_config_folder,
commands::read_live_provider_settings,
@@ -517,7 +539,8 @@ pub fn run() {
commands::delete_claude_mcp_server,
commands::validate_mcp_command,
// usage query
commands::query_provider_usage,
commands::queryProviderUsage,
commands::testUsageScript,
// New MCP via config.json (SSOT)
commands::get_mcp_config,
commands::upsert_mcp_server_in_config,

View File

@@ -1,432 +0,0 @@
use crate::app_config::{AppType, MultiAppConfig};
use crate::config::{
archive_file, delete_file, get_app_config_dir, get_app_config_path, get_claude_config_dir,
};
use crate::error::AppError;
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::PathBuf;
fn now_ts() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}
fn get_marker_path() -> PathBuf {
get_app_config_dir().join("migrated.copies.v1")
}
fn sanitized_id(base: &str) -> String {
crate::config::sanitize_provider_name(base)
}
fn next_unique_id(existing: &HashSet<String>, base: &str) -> String {
let base = sanitized_id(base);
if !existing.contains(&base) {
return base;
}
for i in 2..1000 {
let candidate = format!("{}-{}", base, i);
if !existing.contains(&candidate) {
return candidate;
}
}
format!("{}-dup", base)
}
fn extract_claude_api_key(value: &Value) -> Option<String> {
value
.get("env")
.and_then(|env| env.get("ANTHROPIC_AUTH_TOKEN"))
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}
fn extract_codex_api_key(value: &Value) -> Option<String> {
value
.get("auth")
.and_then(|auth| {
auth.get("OPENAI_API_KEY")
.or_else(|| auth.get("openai_api_key"))
})
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}
fn norm_name(s: &str) -> String {
s.trim().to_lowercase()
}
// 去重策略name + 原始 key 直接比较(不做哈希)
fn scan_claude_copies() -> Vec<(String, PathBuf, Value)> {
let mut items = Vec::new();
let dir = get_claude_config_dir();
if !dir.exists() {
return items;
}
if let Ok(rd) = fs::read_dir(&dir) {
for e in rd.flatten() {
let p = e.path();
let fname = match p.file_name().and_then(|s| s.to_str()) {
Some(s) => s,
None => continue,
};
if fname == "settings.json" || fname == "claude.json" {
continue;
}
if !fname.starts_with("settings-") || !fname.ends_with(".json") {
continue;
}
let name = fname
.trim_start_matches("settings-")
.trim_end_matches(".json");
if let Ok(val) = crate::config::read_json_file::<Value>(&p) {
items.push((name.to_string(), p, val));
}
}
}
items
}
fn scan_codex_copies() -> Vec<(String, Option<PathBuf>, Option<PathBuf>, Value)> {
let mut by_name: HashMap<String, (Option<PathBuf>, Option<PathBuf>)> = HashMap::new();
let dir = crate::codex_config::get_codex_config_dir();
if !dir.exists() {
return Vec::new();
}
if let Ok(rd) = fs::read_dir(&dir) {
for e in rd.flatten() {
let p = e.path();
let fname = match p.file_name().and_then(|s| s.to_str()) {
Some(s) => s,
None => continue,
};
if fname.starts_with("auth-") && fname.ends_with(".json") {
let name = fname.trim_start_matches("auth-").trim_end_matches(".json");
let entry = by_name.entry(name.to_string()).or_default();
entry.0 = Some(p);
} else if fname.starts_with("config-") && fname.ends_with(".toml") {
let name = fname
.trim_start_matches("config-")
.trim_end_matches(".toml");
let entry = by_name.entry(name.to_string()).or_default();
entry.1 = Some(p);
}
}
}
let mut items = Vec::new();
for (name, (auth_path, config_path)) in by_name {
if let Some(authp) = auth_path {
if let Ok(auth) = crate::config::read_json_file::<Value>(&authp) {
let config_str = if let Some(cfgp) = &config_path {
match crate::codex_config::read_and_validate_config_from_path(cfgp) {
Ok(s) => s,
Err(e) => {
log::warn!("跳过无效 Codex config-{}.toml: {}", name, e);
String::new()
}
}
} else {
String::new()
};
let settings = serde_json::json!({
"auth": auth,
"config": config_str,
});
items.push((name, Some(authp), config_path, settings));
}
}
}
items
}
pub fn migrate_copies_into_config(config: &mut MultiAppConfig) -> Result<bool, AppError> {
// 如果已迁移过则跳过;若目录不存在则先创建,避免新装用户写入标记时失败
let marker = get_marker_path();
if let Some(parent) = marker.parent() {
std::fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
}
if marker.exists() {
return Ok(false);
}
let claude_items = scan_claude_copies();
let codex_items = scan_codex_copies();
if claude_items.is_empty() && codex_items.is_empty() {
// 即便没有可迁移项,也写入标记避免每次扫描
fs::write(&marker, b"no-copies").map_err(|e| AppError::io(&marker, e))?;
return Ok(false);
}
// 备份旧的 config.json
let ts = now_ts();
let app_cfg_path = get_app_config_path();
if app_cfg_path.exists() {
let _ = archive_file(ts, "cc-switch", &app_cfg_path);
}
// 读取 liveClaudesettings.json / claude.json
let live_claude: Option<(String, Value)> = {
let settings_path = crate::config::get_claude_settings_path();
if settings_path.exists() {
match crate::config::read_json_file::<Value>(&settings_path) {
Ok(val) => Some(("default".to_string(), val)),
Err(e) => {
log::warn!("读取 Claude live 配置失败: {}", e);
None
}
}
} else {
None
}
};
// 合并Claude优先 live然后副本 - 去重键: name + apiKey直接比较
config.ensure_app(&AppType::Claude);
let manager = config.get_manager_mut(&AppType::Claude).unwrap();
let mut ids: HashSet<String> = manager.providers.keys().cloned().collect();
let mut live_claude_id: Option<String> = None;
if let Some((name, value)) = &live_claude {
let cand_key = extract_claude_api_key(value);
let exist_id = manager.providers.iter().find_map(|(id, p)| {
let pk = extract_claude_api_key(&p.settings_config);
if norm_name(&p.name) == norm_name(name) && pk == cand_key {
Some(id.clone())
} else {
None
}
});
if let Some(exist_id) = exist_id {
if let Some(prov) = manager.providers.get_mut(&exist_id) {
log::info!("合并到已存在 Claude 供应商 '{}' (by name+key)", name);
prov.settings_config = value.clone();
live_claude_id = Some(exist_id);
}
} else {
let id = next_unique_id(&ids, name);
ids.insert(id.clone());
let provider =
crate::provider::Provider::with_id(id.clone(), name.clone(), value.clone(), None);
manager.providers.insert(provider.id.clone(), provider);
live_claude_id = Some(id);
}
}
for (name, path, value) in claude_items.iter() {
let cand_key = extract_claude_api_key(value);
let exist_id = manager.providers.iter().find_map(|(id, p)| {
let pk = extract_claude_api_key(&p.settings_config);
if norm_name(&p.name) == norm_name(name) && pk == cand_key {
Some(id.clone())
} else {
None
}
});
if let Some(exist_id) = exist_id {
if let Some(prov) = manager.providers.get_mut(&exist_id) {
log::info!(
"覆盖 Claude 供应商 '{}' 来自 {} (by name+key)",
name,
path.display()
);
prov.settings_config = value.clone();
}
} else {
let id = next_unique_id(&ids, name);
ids.insert(id.clone());
let provider =
crate::provider::Provider::with_id(id.clone(), name.clone(), value.clone(), None);
manager.providers.insert(provider.id.clone(), provider);
}
}
// 读取 liveCodexauth.json 必需config.toml 可空)
let live_codex: Option<(String, Value)> = {
let auth_path = crate::codex_config::get_codex_auth_path();
if auth_path.exists() {
match crate::config::read_json_file::<Value>(&auth_path) {
Ok(auth) => {
let cfg = match crate::codex_config::read_and_validate_codex_config_text() {
Ok(s) => s,
Err(e) => {
log::warn!("读取/校验 Codex live config.toml 失败: {}", e);
String::new()
}
};
Some((
"default".to_string(),
serde_json::json!({"auth": auth, "config": cfg}),
))
}
Err(e) => {
log::warn!("读取 Codex live auth.json 失败: {}", e);
None
}
}
} else {
None
}
};
// 合并Codex优先 live然后副本 - 去重键: name + OPENAI_API_KEY直接比较
config.ensure_app(&AppType::Codex);
let manager = config.get_manager_mut(&AppType::Codex).unwrap();
let mut ids: HashSet<String> = manager.providers.keys().cloned().collect();
let mut live_codex_id: Option<String> = None;
if let Some((name, value)) = &live_codex {
let cand_key = extract_codex_api_key(value);
let exist_id = manager.providers.iter().find_map(|(id, p)| {
let pk = extract_codex_api_key(&p.settings_config);
if norm_name(&p.name) == norm_name(name) && pk == cand_key {
Some(id.clone())
} else {
None
}
});
if let Some(exist_id) = exist_id {
if let Some(prov) = manager.providers.get_mut(&exist_id) {
log::info!("合并到已存在 Codex 供应商 '{}' (by name+key)", name);
prov.settings_config = value.clone();
live_codex_id = Some(exist_id);
}
} else {
let id = next_unique_id(&ids, name);
ids.insert(id.clone());
let provider =
crate::provider::Provider::with_id(id.clone(), name.clone(), value.clone(), None);
manager.providers.insert(provider.id.clone(), provider);
live_codex_id = Some(id);
}
}
for (name, authp, cfgp, value) in codex_items.iter() {
let cand_key = extract_codex_api_key(value);
let exist_id = manager.providers.iter().find_map(|(id, p)| {
let pk = extract_codex_api_key(&p.settings_config);
if norm_name(&p.name) == norm_name(name) && pk == cand_key {
Some(id.clone())
} else {
None
}
});
if let Some(exist_id) = exist_id {
if let Some(prov) = manager.providers.get_mut(&exist_id) {
log::info!(
"覆盖 Codex 供应商 '{}' 来自 {:?}/{:?} (by name+key)",
name,
authp,
cfgp
);
prov.settings_config = value.clone();
}
} else {
let id = next_unique_id(&ids, name);
ids.insert(id.clone());
let provider =
crate::provider::Provider::with_id(id.clone(), name.clone(), value.clone(), None);
manager.providers.insert(provider.id.clone(), provider);
}
}
// 若当前为空,将 live 导入项设为当前
{
let manager = config.get_manager_mut(&AppType::Claude).unwrap();
if manager.current.is_empty() {
if let Some(id) = live_claude_id {
manager.current = id;
}
}
}
{
let manager = config.get_manager_mut(&AppType::Codex).unwrap();
if manager.current.is_empty() {
if let Some(id) = live_codex_id {
manager.current = id;
}
}
}
// 归档副本文件
for (_, p, _) in claude_items.into_iter() {
match archive_file(ts, "claude", &p) {
Ok(Some(_)) => {
let _ = delete_file(&p);
}
_ => {
// 归档失败则不要删除原文件,保守处理
}
}
}
for (_, ap, cp, _) in codex_items.into_iter() {
if let Some(ap) = ap {
if let Ok(Some(_)) = archive_file(ts, "codex", &ap) {
let _ = delete_file(&ap);
}
}
if let Some(cp) = cp {
if let Ok(Some(_)) = archive_file(ts, "codex", &cp) {
let _ = delete_file(&cp);
}
}
}
// 标记完成
// 仅在迁移阶段执行一次全量去重(忽略大小写的名称 + API Key
let removed = dedupe_config(config);
if removed > 0 {
log::info!("迁移阶段已去重重复供应商 {} 个", removed);
}
fs::write(&marker, b"done").map_err(|e| AppError::io(&marker, e))?;
Ok(true)
}
/// 启动时对现有配置做一次去重:按名称(忽略大小写)+API Key
pub fn dedupe_config(config: &mut MultiAppConfig) -> usize {
use std::collections::HashMap as Map;
fn dedupe_one(
mgr: &mut crate::provider::ProviderManager,
extract_key: &dyn Fn(&Value) -> Option<String>,
) -> usize {
let mut keep: Map<String, String> = Map::new(); // key -> id 保留
let mut remove: Vec<String> = Vec::new();
for (id, p) in mgr.providers.iter() {
let k = format!(
"{}|{}",
norm_name(&p.name),
extract_key(&p.settings_config).unwrap_or_default()
);
if let Some(exist_id) = keep.get(&k) {
// 若当前是正在使用的,则用当前替换之前的,反之丢弃当前
if *id == mgr.current {
// 替换:把原先的标记为删除,改保留为当前
remove.push(exist_id.clone());
keep.insert(k, id.clone());
} else {
remove.push(id.clone());
}
} else {
keep.insert(k, id.clone());
}
}
for id in remove.iter() {
mgr.providers.remove(id);
}
remove.len()
}
let mut removed = 0;
if let Some(mgr) = config.get_manager_mut(&crate::app_config::AppType::Claude) {
removed += dedupe_one(mgr, &extract_claude_api_key);
}
if let Some(mgr) = config.get_manager_mut(&crate::app_config::AppType::Codex) {
removed += dedupe_one(mgr, &extract_codex_api_key);
}
removed
}

View File

@@ -63,6 +63,26 @@ 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用于需要用户标识的接口NewAPI 模板使用)
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "userId")]
pub user_id: Option<String>,
/// 自动查询间隔单位分钟0 表示禁用自动查询)
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "autoQueryInterval")]
pub auto_query_interval: Option<u64>,
}
/// 用量数据

View File

@@ -13,7 +13,7 @@ use crate::config::{
use crate::error::AppError;
use crate::mcp;
use crate::provider::{Provider, ProviderMeta, UsageData, UsageResult};
use crate::settings::CustomEndpoint;
use crate::settings::{self, CustomEndpoint};
use crate::store::AppState;
use crate::usage_script;
@@ -112,6 +112,86 @@ mod tests {
}
impl ProviderService {
/// 归一化 Claude 模型键:读旧键(ANTHROPIC_SMALL_FAST_MODEL),写新键(DEFAULT_*), 并删除旧键
fn normalize_claude_models_in_value(settings: &mut Value) -> bool {
let mut changed = false;
let env = match settings.get_mut("env") {
Some(v) if v.is_object() => v.as_object_mut().unwrap(),
_ => return changed,
};
let model = env
.get("ANTHROPIC_MODEL")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let small_fast = env
.get("ANTHROPIC_SMALL_FAST_MODEL")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let current_haiku = env
.get("ANTHROPIC_DEFAULT_HAIKU_MODEL")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let current_sonnet = env
.get("ANTHROPIC_DEFAULT_SONNET_MODEL")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let current_opus = env
.get("ANTHROPIC_DEFAULT_OPUS_MODEL")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let target_haiku = current_haiku
.or_else(|| small_fast.clone())
.or_else(|| model.clone());
let target_sonnet = current_sonnet
.or_else(|| model.clone())
.or_else(|| small_fast.clone());
let target_opus = current_opus
.or_else(|| model.clone())
.or_else(|| small_fast.clone());
if env.get("ANTHROPIC_DEFAULT_HAIKU_MODEL").is_none() {
if let Some(v) = target_haiku {
env.insert(
"ANTHROPIC_DEFAULT_HAIKU_MODEL".to_string(),
Value::String(v),
);
changed = true;
}
}
if env.get("ANTHROPIC_DEFAULT_SONNET_MODEL").is_none() {
if let Some(v) = target_sonnet {
env.insert(
"ANTHROPIC_DEFAULT_SONNET_MODEL".to_string(),
Value::String(v),
);
changed = true;
}
}
if env.get("ANTHROPIC_DEFAULT_OPUS_MODEL").is_none() {
if let Some(v) = target_opus {
env.insert("ANTHROPIC_DEFAULT_OPUS_MODEL".to_string(), Value::String(v));
changed = true;
}
}
if env.remove("ANTHROPIC_SMALL_FAST_MODEL").is_some() {
changed = true;
}
changed
}
fn normalize_provider_if_claude(app_type: &AppType, provider: &mut Provider) {
if matches!(app_type, AppType::Claude) {
let mut v = provider.settings_config.clone();
if Self::normalize_claude_models_in_value(&mut v) {
provider.settings_config = v;
}
}
}
fn run_transaction<R, F>(state: &AppState, f: F) -> Result<R, AppError>
where
F: FnOnce(&mut MultiAppConfig) -> Result<(R, Option<PostCommitAction>), AppError>,
@@ -209,7 +289,8 @@ impl ProviderService {
"Claude settings file missing; cannot refresh snapshot",
));
}
let live_after = read_json_file::<Value>(&settings_path)?;
let mut live_after = read_json_file::<Value>(&settings_path)?;
let _ = Self::normalize_claude_models_in_value(&mut live_after);
{
let mut guard = state.config.write().map_err(AppError::from)?;
if let Some(manager) = guard.get_manager_mut(app_type) {
@@ -308,6 +389,9 @@ impl ProviderService {
/// 新增供应商
pub fn add(state: &AppState, app_type: AppType, provider: Provider) -> Result<bool, AppError> {
let mut provider = provider;
// 归一化 Claude 模型键
Self::normalize_provider_if_claude(&app_type, &mut provider);
Self::validate_provider_settings(&app_type, &provider)?;
let app_type_clone = app_type.clone();
@@ -347,6 +431,9 @@ impl ProviderService {
app_type: AppType,
provider: Provider,
) -> Result<bool, AppError> {
let mut provider = provider;
// 归一化 Claude 模型键
Self::normalize_provider_if_claude(&app_type, &mut provider);
Self::validate_provider_settings(&app_type, &provider)?;
let provider_id = provider.id.clone();
let app_type_clone = app_type.clone();
@@ -358,28 +445,27 @@ impl ProviderService {
.ok_or_else(|| Self::app_not_found(&app_type_clone))?;
if !manager.providers.contains_key(&provider_id) {
return Err(AppError::ProviderNotFound(provider_id.clone()));
return Err(AppError::localized(
"provider.not_found",
format!("供应商不存在: {}", provider_id),
format!("Provider not found: {}", provider_id),
));
}
let is_current = manager.current == provider_id;
let merged = if let Some(existing) = manager.providers.get(&provider_id) {
let mut updated = provider_clone.clone();
match (existing.meta.as_ref(), updated.meta.take()) {
// 前端未提供 meta表示不修改沿用旧值
(Some(old_meta), None) => {
updated.meta = Some(old_meta.clone());
}
(Some(old_meta), Some(mut new_meta)) => {
let mut merged_map = old_meta.custom_endpoints.clone();
for (url, ep) in new_meta.custom_endpoints.drain() {
merged_map.entry(url).or_insert(ep);
}
updated.meta = Some(ProviderMeta {
custom_endpoints: merged_map,
usage_script: new_meta.usage_script.clone(),
});
(None, None) => {
updated.meta = None;
}
(None, maybe_new) => {
updated.meta = maybe_new;
// 前端提供的 meta 视为权威,直接覆盖(其中 custom_endpoints 允许是空,表示删除所有自定义端点)
(_old, Some(new_meta)) => {
updated.meta = Some(new_meta);
}
}
updated
@@ -440,16 +526,19 @@ impl ProviderService {
"Claude settings file is missing",
));
}
read_json_file(&settings_path)?
let mut v = read_json_file::<Value>(&settings_path)?;
let _ = Self::normalize_claude_models_in_value(&mut v);
v
}
};
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)?;
@@ -543,10 +632,13 @@ impl ProviderService {
let manager = cfg
.get_manager_mut(&app_type)
.ok_or_else(|| Self::app_not_found(&app_type))?;
let provider = manager
.providers
.get_mut(provider_id)
.ok_or_else(|| AppError::ProviderNotFound(provider_id.to_string()))?;
let provider = manager.providers.get_mut(provider_id).ok_or_else(|| {
AppError::localized(
"provider.not_found",
format!("供应商不存在: {}", provider_id),
format!("Provider not found: {}", provider_id),
)
})?;
let meta = provider.meta.get_or_insert_with(ProviderMeta::default);
let endpoint = CustomEndpoint {
@@ -634,60 +726,42 @@ impl ProviderService {
Ok(true)
}
/// 查询供应商用量
pub async fn query_usage(
state: &AppState,
app_type: AppType,
provider_id: &str,
/// 执行用量脚本并格式化结果(私有辅助方法)
async fn execute_and_format_usage_result(
script_code: &str,
api_key: &str,
base_url: &str,
timeout: u64,
access_token: Option<&str>,
user_id: Option<&str>,
) -> Result<UsageResult, AppError> {
let (provider, script_code, timeout) = {
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))?;
let provider = manager
.providers
.get(provider_id)
.cloned()
.ok_or_else(|| AppError::ProviderNotFound(provider_id.to_string()))?;
let (script_code, timeout) = {
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),
)
};
(provider, script_code, timeout)
};
let (api_key, base_url) = Self::extract_credentials(&provider, &app_type)?;
match usage_script::execute_usage_script(&script_code, &api_key, &base_url, timeout).await {
match usage_script::execute_usage_script(
script_code,
api_key,
base_url,
timeout,
access_token,
user_id,
)
.await
{
Ok(data) => {
let usage_list: Vec<UsageData> = if data.is_array() {
serde_json::from_value(data)
.map_err(|e| AppError::Message(format!("数据格式错误: {}", e)))?
serde_json::from_value(data).map_err(|e| {
AppError::localized(
"usage_script.data_format_error",
format!("数据格式错误: {}", e),
format!("Data format error: {}", e),
)
})?
} else {
let single: UsageData = serde_json::from_value(data)
.map_err(|e| AppError::Message(format!("数据格式错误: {}", e)))?;
let single: UsageData = serde_json::from_value(data).map_err(|e| {
AppError::localized(
"usage_script.data_format_error",
format!("数据格式错误: {}", e),
format!("Data format error: {}", e),
)
})?;
vec![single]
};
@@ -697,14 +771,116 @@ impl ProviderService {
error: None,
})
}
Err(err) => Ok(UsageResult {
success: false,
data: None,
error: Some(err.to_string()),
}),
Err(err) => {
let lang = settings::get_settings()
.language
.unwrap_or_else(|| "zh".to_string());
let msg = match err {
AppError::Localized { zh, en, .. } => {
if lang == "en" {
en
} else {
zh
}
}
other => other.to_string(),
};
Ok(UsageResult {
success: false,
data: None,
error: Some(msg),
})
}
}
}
/// 查询供应商用量(使用已保存的脚本配置)
pub async fn query_usage(
state: &AppState,
app_type: AppType,
provider_id: &str,
) -> Result<UsageResult, AppError> {
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)
.ok_or_else(|| Self::app_not_found(&app_type))?;
let provider = manager.providers.get(provider_id).cloned().ok_or_else(|| {
AppError::localized(
"provider.not_found",
format!("供应商不存在: {}", provider_id),
format!("Provider not found: {}", provider_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(),
)
};
Self::execute_and_format_usage_result(
&script_code,
&api_key,
&base_url,
timeout,
access_token.as_deref(),
user_id.as_deref(),
)
.await
}
/// 测试用量脚本(使用临时脚本内容,不保存)
#[allow(clippy::too_many_arguments)]
pub async fn test_usage_script(
_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> {
// 直接使用传入的凭证参数进行测试
Self::execute_and_format_usage_result(
script_code,
api_key.unwrap_or(""),
base_url.unwrap_or(""),
timeout,
access_token,
user_id,
)
.await
}
/// 切换指定应用的供应商
pub fn switch(state: &AppState, app_type: AppType, provider_id: &str) -> Result<(), AppError> {
let app_type_clone = app_type.clone();
@@ -739,7 +915,13 @@ impl ProviderService {
.providers
.get(provider_id)
.cloned()
.ok_or_else(|| AppError::ProviderNotFound(provider_id.to_string()))?;
.ok_or_else(|| {
AppError::localized(
"provider.not_found",
format!("供应商不存在: {}", provider_id),
format!("Provider not found: {}", provider_id),
)
})?;
Self::backfill_codex_current(config, provider_id)?;
@@ -820,7 +1002,13 @@ impl ProviderService {
.providers
.get(provider_id)
.cloned()
.ok_or_else(|| AppError::ProviderNotFound(provider_id.to_string()))?;
.ok_or_else(|| {
AppError::localized(
"provider.not_found",
format!("供应商不存在: {}", provider_id),
format!("Provider not found: {}", provider_id),
)
})?;
Self::backfill_claude_current(config, provider_id)?;
@@ -848,7 +1036,8 @@ impl ProviderService {
return Ok(());
}
let live = read_json_file::<Value>(&settings_path)?;
let mut live = read_json_file::<Value>(&settings_path)?;
let _ = Self::normalize_claude_models_in_value(&mut live);
if let Some(manager) = config.get_manager_mut(&AppType::Claude) {
if let Some(current) = manager.providers.get_mut(&current_id) {
current.settings_config = live;
@@ -864,7 +1053,10 @@ impl ProviderService {
std::fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
}
write_json_file(&settings_path, &provider.settings_config)?;
// 归一化后再写入
let mut content = provider.settings_config.clone();
let _ = Self::normalize_claude_models_in_value(&mut content);
write_json_file(&settings_path, &content)?;
Ok(())
}
@@ -931,6 +1123,7 @@ impl ProviderService {
Ok(())
}
#[allow(dead_code)]
fn extract_credentials(
provider: &Provider,
app_type: &AppType,
@@ -951,6 +1144,7 @@ impl ProviderService {
let api_key = env
.get("ANTHROPIC_AUTH_TOKEN")
.or_else(|| env.get("ANTHROPIC_API_KEY"))
.and_then(|v| v.as_str())
.ok_or_else(|| {
AppError::localized(
@@ -1007,8 +1201,13 @@ impl ProviderService {
.unwrap_or("");
let base_url = if config_toml.contains("base_url") {
let re = Regex::new(r#"base_url\s*=\s*["']([^"']+)["']"#)
.map_err(|e| AppError::Message(format!("正则初始化失败: {}", e)))?;
let re = Regex::new(r#"base_url\s*=\s*["']([^"']+)["']"#).map_err(|e| {
AppError::localized(
"provider.regex_init_failed",
format!("正则初始化失败: {}", e),
format!("Failed to initialize regex: {}", e),
)
})?;
re.captures(config_toml)
.and_then(|caps| caps.get(1))
.map(|m| m.as_str().to_string())
@@ -1033,7 +1232,11 @@ impl ProviderService {
}
fn app_not_found(app_type: &AppType) -> AppError {
AppError::Message(format!("应用类型不存在: {:?}", app_type))
AppError::localized(
"provider.app_not_found",
format!("应用类型不存在: {:?}", app_type),
format!("App type not found: {:?}", app_type),
)
}
fn now_millis() -> i64 {
@@ -1058,11 +1261,13 @@ impl ProviderService {
));
}
manager
.providers
.get(provider_id)
.cloned()
.ok_or_else(|| AppError::ProviderNotFound(provider_id.to_string()))?
manager.providers.get(provider_id).cloned().ok_or_else(|| {
AppError::localized(
"provider.not_found",
format!("供应商不存在: {}", provider_id),
format!("Provider not found: {}", provider_id),
)
})?
};
match app_type {

View File

@@ -101,7 +101,13 @@ impl SpeedtestService {
.redirect(reqwest::redirect::Policy::limited(5))
.user_agent("cc-switch-speedtest/1.0")
.build()
.map_err(|e| AppError::Message(format!("创建 HTTP 客户端失败: {e}")))
.map_err(|e| {
AppError::localized(
"speedtest.client_create_failed",
format!("创建 HTTP 客户端失败: {e}"),
format!("Failed to create HTTP client: {e}"),
)
})
}
fn sanitize_timeout(timeout_secs: Option<u64>) -> u64 {

View File

@@ -7,23 +7,14 @@ pub struct AppState {
pub config: RwLock<MultiAppConfig>,
}
impl Default for AppState {
fn default() -> Self {
Self::new()
}
}
impl AppState {
/// 创建新的应用状态
pub fn new() -> Self {
let config = MultiAppConfig::load().unwrap_or_else(|e| {
log::warn!("加载配置失败: {}, 使用默认配置", e);
MultiAppConfig::default()
});
Self {
/// 注意:仅在配置成功加载时返回;不会在失败时回退默认值。
pub fn try_new() -> Result<Self, AppError> {
let config = MultiAppConfig::load()?;
Ok(Self {
config: RwLock::new(config),
}
})
}
/// 保存配置到文件

View File

@@ -12,88 +12,189 @@ pub async fn execute_usage_script(
api_key: &str,
base_url: &str,
timeout_secs: u64,
access_token: Option<&str>,
user_id: Option<&str>,
) -> Result<Value, AppError> {
// 1. 替换变量
let replaced = script_code
let mut replaced = script_code
.replace("{{apiKey}}", api_key)
.replace("{{baseUrl}}", base_url);
// 替换 accessToken 和 userId
if let Some(token) = access_token {
replaced = replaced.replace("{{accessToken}}", token);
}
if let Some(uid) = user_id {
replaced = replaced.replace("{{userId}}", uid);
}
// 2. 在独立作用域中提取 request 配置(确保 Runtime/Context 在 await 前释放)
let request_config = {
let runtime =
Runtime::new().map_err(|e| AppError::Message(format!("创建 JS 运行时失败: {}", e)))?;
let context = Context::full(&runtime)
.map_err(|e| AppError::Message(format!("创建 JS 上下文失败: {}", e)))?;
let runtime = Runtime::new().map_err(|e| {
AppError::localized(
"usage_script.runtime_create_failed",
format!("创建 JS 运行时失败: {}", e),
format!("Failed to create JS runtime: {}", e),
)
})?;
let context = Context::full(&runtime).map_err(|e| {
AppError::localized(
"usage_script.context_create_failed",
format!("创建 JS 上下文失败: {}", e),
format!("Failed to create JS context: {}", e),
)
})?;
context.with(|ctx| {
// 执行用户代码,获取配置对象
let config: rquickjs::Object = ctx
.eval(replaced.clone())
.map_err(|e| AppError::Message(format!("解析配置失败: {}", e)))?;
let config: rquickjs::Object = ctx.eval(replaced.clone()).map_err(|e| {
AppError::localized(
"usage_script.config_parse_failed",
format!("解析配置失败: {}", e),
format!("Failed to parse config: {}", e),
)
})?;
// 提取 request 配置
let request: rquickjs::Object = config
.get("request")
.map_err(|e| AppError::Message(format!("缺少 request 配置: {}", e)))?;
let request: rquickjs::Object = config.get("request").map_err(|e| {
AppError::localized(
"usage_script.request_missing",
format!("缺少 request 配置: {}", e),
format!("Missing request config: {}", e),
)
})?;
// 将 request 转换为 JSON 字符串
let request_json: String = ctx
.json_stringify(request)
.map_err(|e| AppError::Message(format!("序列化 request 失败: {}", e)))?
.ok_or_else(|| AppError::Message("序列化返回 None".into()))?
.map_err(|e| {
AppError::localized(
"usage_script.request_serialize_failed",
format!("序列化 request 失败: {}", e),
format!("Failed to serialize request: {}", e),
)
})?
.ok_or_else(|| {
AppError::localized(
"usage_script.serialize_none",
"序列化返回 None",
"Serialization returned None",
)
})?
.get()
.map_err(|e| AppError::Message(format!("获取字符串失败: {}", e)))?;
.map_err(|e| {
AppError::localized(
"usage_script.get_string_failed",
format!("获取字符串失败: {}", e),
format!("Failed to get string: {}", e),
)
})?;
Ok::<_, AppError>(request_json)
})?
}; // Runtime 和 Context 在这里被 drop
// 3. 解析 request 配置
let request: RequestConfig = serde_json::from_str(&request_config)
.map_err(|e| AppError::Message(format!("request 配置格式错误: {}", e)))?;
let request: RequestConfig = serde_json::from_str(&request_config).map_err(|e| {
AppError::localized(
"usage_script.request_format_invalid",
format!("request 配置格式错误: {}", e),
format!("Invalid request config format: {}", e),
)
})?;
// 4. 发送 HTTP 请求
let response_data = send_http_request(&request, timeout_secs).await?;
// 5. 在独立作用域中执行 extractor确保 Runtime/Context 在函数结束前释放)
let result: Value = {
let runtime =
Runtime::new().map_err(|e| AppError::Message(format!("创建 JS 运行时失败: {}", e)))?;
let context = Context::full(&runtime)
.map_err(|e| AppError::Message(format!("创建 JS 上下文失败: {}", e)))?;
let runtime = Runtime::new().map_err(|e| {
AppError::localized(
"usage_script.runtime_create_failed",
format!("创建 JS 运行时失败: {}", e),
format!("Failed to create JS runtime: {}", e),
)
})?;
let context = Context::full(&runtime).map_err(|e| {
AppError::localized(
"usage_script.context_create_failed",
format!("创建 JS 上下文失败: {}", e),
format!("Failed to create JS context: {}", e),
)
})?;
context.with(|ctx| {
// 重新 eval 获取配置对象
let config: rquickjs::Object = ctx
.eval(replaced.clone())
.map_err(|e| AppError::Message(format!("重新解析配置失败: {}", e)))?;
let config: rquickjs::Object = ctx.eval(replaced.clone()).map_err(|e| {
AppError::localized(
"usage_script.config_reparse_failed",
format!("重新解析配置失败: {}", e),
format!("Failed to re-parse config: {}", e),
)
})?;
// 提取 extractor 函数
let extractor: Function = config
.get("extractor")
.map_err(|e| AppError::Message(format!("缺少 extractor 函数: {}", e)))?;
let extractor: Function = config.get("extractor").map_err(|e| {
AppError::localized(
"usage_script.extractor_missing",
format!("缺少 extractor 函数: {}", e),
format!("Missing extractor function: {}", e),
)
})?;
// 将响应数据转换为 JS 值
let response_js: rquickjs::Value = ctx
.json_parse(response_data.as_str())
.map_err(|e| AppError::Message(format!("解析响应 JSON 失败: {}", e)))?;
let response_js: rquickjs::Value =
ctx.json_parse(response_data.as_str()).map_err(|e| {
AppError::localized(
"usage_script.response_parse_failed",
format!("解析响应 JSON 失败: {}", e),
format!("Failed to parse response JSON: {}", e),
)
})?;
// 调用 extractor(response)
let result_js: rquickjs::Value = extractor
.call((response_js,))
.map_err(|e| AppError::Message(format!("执行 extractor 失败: {}", e)))?;
let result_js: rquickjs::Value = extractor.call((response_js,)).map_err(|e| {
AppError::localized(
"usage_script.extractor_exec_failed",
format!("执行 extractor 失败: {}", e),
format!("Failed to execute extractor: {}", e),
)
})?;
// 转换为 JSON 字符串
let result_json: String = ctx
.json_stringify(result_js)
.map_err(|e| AppError::Message(format!("序列化结果失败: {}", e)))?
.ok_or_else(|| AppError::Message("序列化返回 None".into()))?
.map_err(|e| {
AppError::localized(
"usage_script.result_serialize_failed",
format!("序列化结果失败: {}", e),
format!("Failed to serialize result: {}", e),
)
})?
.ok_or_else(|| {
AppError::localized(
"usage_script.serialize_none",
"序列化返回 None",
"Serialization returned None",
)
})?
.get()
.map_err(|e| AppError::Message(format!("获取字符串失败: {}", e)))?;
.map_err(|e| {
AppError::localized(
"usage_script.get_string_failed",
format!("获取字符串失败: {}", e),
format!("Failed to get string: {}", e),
)
})?;
// 解析为 serde_json::Value
serde_json::from_str(&result_json)
.map_err(|e| AppError::Message(format!("JSON 解析失败: {}", e)))
serde_json::from_str(&result_json).map_err(|e| {
AppError::localized(
"usage_script.json_parse_failed",
format!("JSON 解析失败: {}", e),
format!("JSON parse failed: {}", e),
)
})
})?
}; // Runtime 和 Context 在这里被 drop
@@ -116,12 +217,27 @@ struct RequestConfig {
/// 发送 HTTP 请求
async fn send_http_request(config: &RequestConfig, timeout_secs: u64) -> Result<String, AppError> {
// 约束超时范围,防止异常配置导致长时间阻塞
let timeout = timeout_secs.clamp(2, 30);
let client = Client::builder()
.timeout(Duration::from_secs(timeout_secs))
.timeout(Duration::from_secs(timeout))
.build()
.map_err(|e| AppError::Message(format!("创建客户端失败: {}", e)))?;
.map_err(|e| {
AppError::localized(
"usage_script.client_create_failed",
format!("创建客户端失败: {}", e),
format!("Failed to create client: {}", e),
)
})?;
let method = config.method.parse().unwrap_or(reqwest::Method::GET);
// 严格校验 HTTP 方法,非法值不回退为 GET
let method: reqwest::Method = config.method.parse().map_err(|_| {
AppError::localized(
"usage_script.invalid_http_method",
format!("不支持的 HTTP 方法: {}", config.method),
format!("Unsupported HTTP method: {}", config.method),
)
})?;
let mut req = client.request(method.clone(), &config.url);
@@ -136,16 +252,22 @@ async fn send_http_request(config: &RequestConfig, timeout_secs: u64) -> Result<
}
// 发送请求
let resp = req
.send()
.await
.map_err(|e| AppError::Message(format!("请求失败: {}", e)))?;
let resp = req.send().await.map_err(|e| {
AppError::localized(
"usage_script.request_failed",
format!("请求失败: {}", e),
format!("Request failed: {}", e),
)
})?;
let status = resp.status();
let text = resp
.text()
.await
.map_err(|e| AppError::Message(format!("读取响应失败: {}", e)))?;
let text = resp.text().await.map_err(|e| {
AppError::localized(
"usage_script.read_response_failed",
format!("读取响应失败: {}", e),
format!("Failed to read response: {}", e),
)
})?;
if !status.is_success() {
let preview = if text.len() > 200 {
@@ -153,7 +275,11 @@ async fn send_http_request(config: &RequestConfig, timeout_secs: u64) -> Result<
} else {
text.clone()
};
return Err(AppError::Message(format!("HTTP {} : {}", status, preview)));
return Err(AppError::localized(
"usage_script.http_error",
format!("HTTP {} : {}", status, preview),
format!("HTTP {} : {}", status, preview),
));
}
Ok(text)
@@ -164,11 +290,20 @@ fn validate_result(result: &Value) -> Result<(), AppError> {
// 如果是数组,验证每个元素
if let Some(arr) = result.as_array() {
if arr.is_empty() {
return Err(AppError::InvalidInput("脚本返回的数组不能为空".into()));
return Err(AppError::localized(
"usage_script.empty_array",
"脚本返回的数组不能为空",
"Script returned empty array",
));
}
for (idx, item) in arr.iter().enumerate() {
validate_single_usage(item)
.map_err(|e| AppError::InvalidInput(format!("数组索引[{}]验证失败: {}", idx, e)))?;
validate_single_usage(item).map_err(|e| {
AppError::localized(
"usage_script.array_validation_failed",
format!("数组索引[{}]验证失败: {}", idx, e),
format!("Validation failed at index [{}]: {}", idx, e),
)
})?;
}
return Ok(());
}
@@ -179,50 +314,82 @@ fn validate_result(result: &Value) -> Result<(), AppError> {
/// 验证单个用量数据对象
fn validate_single_usage(result: &Value) -> Result<(), AppError> {
let obj = result
.as_object()
.ok_or_else(|| AppError::InvalidInput("脚本必须返回对象或对象数组".into()))?;
let obj = result.as_object().ok_or_else(|| {
AppError::localized(
"usage_script.must_return_object",
"脚本必须返回对象或对象数组",
"Script must return object or array of objects",
)
})?;
// 所有字段均为可选,只进行类型检查
if obj.contains_key("isValid")
&& !result["isValid"].is_null()
&& !result["isValid"].is_boolean()
{
return Err(AppError::InvalidInput("isValid 必须是布尔值或 null".into()));
return Err(AppError::localized(
"usage_script.isvalid_type_error",
"isValid 必须是布尔值或 null",
"isValid must be boolean or null",
));
}
if obj.contains_key("invalidMessage")
&& !result["invalidMessage"].is_null()
&& !result["invalidMessage"].is_string()
{
return Err(AppError::InvalidInput(
"invalidMessage 必须是字符串或 null".into(),
return Err(AppError::localized(
"usage_script.invalidmessage_type_error",
"invalidMessage 必须是字符串或 null",
"invalidMessage must be string or null",
));
}
if obj.contains_key("remaining")
&& !result["remaining"].is_null()
&& !result["remaining"].is_number()
{
return Err(AppError::InvalidInput("remaining 必须是数字或 null".into()));
return Err(AppError::localized(
"usage_script.remaining_type_error",
"remaining 必须是数字或 null",
"remaining must be number or null",
));
}
if obj.contains_key("unit") && !result["unit"].is_null() && !result["unit"].is_string() {
return Err(AppError::InvalidInput("unit 必须是字符串或 null".into()));
return Err(AppError::localized(
"usage_script.unit_type_error",
"unit 必须是字符串或 null",
"unit must be string or null",
));
}
if obj.contains_key("total") && !result["total"].is_null() && !result["total"].is_number() {
return Err(AppError::InvalidInput("total 必须是数字或 null".into()));
return Err(AppError::localized(
"usage_script.total_type_error",
"total 必须是数字或 null",
"total must be number or null",
));
}
if obj.contains_key("used") && !result["used"].is_null() && !result["used"].is_number() {
return Err(AppError::InvalidInput("used 必须是数字或 null".into()));
return Err(AppError::localized(
"usage_script.used_type_error",
"used 必须是数字或 null",
"used must be number or null",
));
}
if obj.contains_key("planName")
&& !result["planName"].is_null()
&& !result["planName"].is_string()
{
return Err(AppError::InvalidInput(
"planName 必须是字符串或 null".into(),
return Err(AppError::localized(
"usage_script.planname_type_error",
"planName 必须是字符串或 null",
"planName must be string or null",
));
}
if obj.contains_key("extra") && !result["extra"].is_null() && !result["extra"].is_string() {
return Err(AppError::InvalidInput("extra 必须是字符串或 null".into()));
return Err(AppError::localized(
"usage_script.extra_type_error",
"extra 必须是字符串或 null",
"extra must be string or null",
));
}
Ok(())

View File

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

View File

@@ -0,0 +1,107 @@
use std::fs;
use std::path::PathBuf;
use cc_switch_lib::{AppError, MultiAppConfig};
mod support;
use support::{ensure_test_home, reset_test_fs, test_mutex};
fn cfg_path() -> PathBuf {
let home = std::env::var("HOME").expect("HOME should be set by ensure_test_home");
PathBuf::from(home).join(".cc-switch").join("config.json")
}
#[test]
fn load_v1_config_returns_error_and_does_not_write() {
let _guard = test_mutex().lock().expect("acquire test mutex");
reset_test_fs();
let home = ensure_test_home();
let path = cfg_path();
fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
// 最小 v1 形状providers + current且不含 version/apps/mcp
let v1_json = r#"{"providers":{},"current":""}"#;
fs::write(&path, v1_json).expect("seed v1 json");
let before = fs::read_to_string(&path).expect("read before");
let err = MultiAppConfig::load().expect_err("v1 should not be auto-migrated");
match err {
AppError::Localized { key, .. } => assert_eq!(key, "config.unsupported_v1"),
other => panic!("expected Localized v1 error, got {other:?}"),
}
// 文件不应有任何变化,且不应生成 .bak
let after = fs::read_to_string(&path).expect("read after");
assert_eq!(before, after, "config.json should not be modified");
let bak = home.join(".cc-switch").join("config.json.bak");
assert!(!bak.exists(), ".bak should not be created on load error");
}
#[test]
fn load_v1_with_extra_version_still_treated_as_v1() {
let _guard = test_mutex().lock().expect("acquire test mutex");
reset_test_fs();
let home = ensure_test_home();
let path = cfg_path();
std::fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
// 畸形:包含 providers + current + version但没有 apps应按 v1 处理
let v1_like = r#"{"providers":{},"current":"","version":2}"#;
std::fs::write(&path, v1_like).expect("seed v1-like json");
let before = std::fs::read_to_string(&path).expect("read before");
let err = MultiAppConfig::load().expect_err("v1-like should not be parsed as v2");
match err {
AppError::Localized { key, .. } => assert_eq!(key, "config.unsupported_v1"),
other => panic!("expected Localized v1 error, got {other:?}"),
}
let after = std::fs::read_to_string(&path).expect("read after");
assert_eq!(before, after, "config.json should not be modified");
let bak = home.join(".cc-switch").join("config.json.bak");
assert!(!bak.exists(), ".bak should not be created on v1-like error");
}
#[test]
fn load_invalid_json_returns_parse_error_and_does_not_write() {
let _guard = test_mutex().lock().expect("acquire test mutex");
reset_test_fs();
let home = ensure_test_home();
let path = cfg_path();
fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
fs::write(&path, "{not json").expect("seed invalid json");
let before = fs::read_to_string(&path).expect("read before");
let err = MultiAppConfig::load().expect_err("invalid json should error");
match err {
AppError::Json { .. } => {}
other => panic!("expected Json error, got {other:?}"),
}
let after = fs::read_to_string(&path).expect("read after");
assert_eq!(before, after, "config.json should remain unchanged");
let bak = home.join(".cc-switch").join("config.json.bak");
assert!(!bak.exists(), ".bak should not be created on parse error");
}
#[test]
fn load_valid_v2_config_succeeds() {
let _guard = test_mutex().lock().expect("acquire test mutex");
reset_test_fs();
let _home = ensure_test_home();
let path = cfg_path();
fs::create_dir_all(path.parent().unwrap()).expect("create cfg dir");
// 使用默认结构序列化为 v2
let default_cfg = MultiAppConfig::default();
let json = serde_json::to_string_pretty(&default_cfg).expect("serialize default cfg");
fs::write(&path, json).expect("write v2 json");
let loaded = MultiAppConfig::load().expect("v2 should load successfully");
assert_eq!(loaded.version, 2);
assert!(loaded
.get_manager(&cc_switch_lib::AppType::Claude)
.is_some());
assert!(loaded.get_manager(&cc_switch_lib::AppType::Codex).is_some());
}

View File

@@ -240,8 +240,8 @@ fn provider_service_switch_missing_provider_returns_error() {
let err = ProviderService::switch(&state, AppType::Claude, "missing")
.expect_err("switching missing provider should fail");
match err {
AppError::ProviderNotFound(id) => assert_eq!(id, "missing"),
other => panic!("expected ProviderNotFound, got {other:?}"),
AppError::Localized { key, .. } => assert_eq!(key, "provider.not_found"),
other => panic!("expected Localized error for provider not found, got {other:?}"),
}
}

View File

@@ -7,6 +7,9 @@ import { EditorState } from "@codemirror/state";
import { placeholder } from "@codemirror/view";
import { linter, Diagnostic } from "@codemirror/lint";
import { useTranslation } from "react-i18next";
import { Wand2 } from "lucide-react";
import { toast } from "sonner";
import { formatJSON } from "@/utils/formatters";
interface JsonEditorProps {
value: string;
@@ -170,7 +173,44 @@ const JsonEditor: React.FC<JsonEditorProps> = ({
}
}, [value]);
return <div ref={editorRef} style={{ width: "100%" }} />;
// 格式化处理函数
const handleFormat = () => {
if (!viewRef.current) return;
const currentValue = viewRef.current.state.doc.toString();
if (!currentValue.trim()) return;
try {
const formatted = formatJSON(currentValue);
onChange(formatted);
toast.success(t("common.formatSuccess", { defaultValue: "格式化成功" }));
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
toast.error(
t("common.formatError", {
defaultValue: "格式化失败:{{error}}",
error: errorMessage,
}),
);
}
};
return (
<div style={{ width: "100%" }}>
<div ref={editorRef} style={{ width: "100%" }} />
{language === "json" && (
<button
type="button"
onClick={handleFormat}
className="mt-2 inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 transition-colors"
>
<Wand2 className="w-3.5 h-3.5" />
{t("common.format", { defaultValue: "格式化" })}
</button>
)}
</div>
);
};
export default JsonEditor;

View File

@@ -1,33 +1,82 @@
import React from "react";
import { RefreshCw, AlertCircle } from "lucide-react";
import { RefreshCw, AlertCircle, Clock } from "lucide-react";
import { useTranslation } from "react-i18next";
import { type AppId } from "@/lib/api";
import { useUsageQuery } from "@/lib/query/queries";
import { UsageData } from "../types";
import { UsageData, Provider } from "@/types";
interface UsageFooterProps {
provider: Provider;
providerId: string;
appId: AppId;
usageEnabled: boolean; // 是否启用了用量查询
isCurrent: boolean; // 是否为当前激活的供应商
inline?: boolean; // 是否内联显示(在按钮左侧)
}
const UsageFooter: React.FC<UsageFooterProps> = ({
provider,
providerId,
appId,
usageEnabled,
isCurrent,
inline = false,
}) => {
const { t } = useTranslation();
// 统一的用量查询(自动查询仅对当前激活的供应商启用)
const autoQueryInterval = isCurrent
? provider.meta?.usage_script?.autoQueryInterval || 0
: 0;
const {
data: usage,
isLoading: loading,
isFetching: loading,
lastQueriedAt,
refetch,
} = useUsageQuery(providerId, appId, usageEnabled);
} = useUsageQuery(providerId, appId, {
enabled: usageEnabled,
autoQueryInterval,
});
// 🆕 定期更新当前时间,用于刷新相对时间显示
const [now, setNow] = React.useState(Date.now());
React.useEffect(() => {
if (!lastQueriedAt) return;
// 每30秒更新一次当前时间触发相对时间显示的刷新
const interval = setInterval(() => {
setNow(Date.now());
}, 30000); // 30秒
return () => clearInterval(interval);
}, [lastQueriedAt]);
// 只在启用用量查询且有数据时显示
if (!usageEnabled || !usage) return null;
// 错误状态
if (!usage.success) {
if (inline) {
return (
<div className="flex items-center gap-2 text-xs">
<div className="flex items-center gap-1.5 text-red-500 dark:text-red-400">
<AlertCircle size={12} />
<span>{t("usage.queryFailed")}</span>
</div>
<button
onClick={() => refetch()}
disabled={loading}
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
title={t("usage.refreshUsage")}
>
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
</button>
</div>
);
}
return (
<div className="mt-3 pt-3 border-t border-border-default ">
<div className="flex items-center justify-between gap-2 text-xs">
@@ -55,21 +104,104 @@ const UsageFooter: React.FC<UsageFooterProps> = ({
// 无数据时不显示
if (usageDataList.length === 0) return null;
// 内联模式:仅显示第一个套餐的核心数据(分上下两行)
if (inline) {
const firstUsage = usageDataList[0];
const isExpired = firstUsage.isValid === false;
return (
<div className="flex flex-col gap-1 text-xs flex-shrink-0">
{/* 第一行:刷新时间 + 刷新按钮 */}
<div className="flex items-center gap-2 justify-end">
{/* 上次查询时间 */}
{lastQueriedAt && (
<span className="text-[10px] text-gray-400 dark:text-gray-500 flex items-center gap-1">
<Clock size={10} />
{formatRelativeTime(lastQueriedAt, now, t)}
</span>
)}
{/* 刷新按钮 */}
<button
onClick={() => refetch()}
disabled={loading}
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
title={t("usage.refreshUsage")}
>
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
</button>
</div>
{/* 第二行:已用 + 剩余 + 单位 */}
<div className="flex items-center gap-2">
{/* 已用 */}
{firstUsage.used !== undefined && (
<div className="flex items-center gap-0.5">
<span className="text-gray-500 dark:text-gray-400">
{t("usage.used")}
</span>
<span className="tabular-nums text-gray-600 dark:text-gray-400 font-medium">
{firstUsage.used.toFixed(2)}
</span>
</div>
)}
{/* 剩余 */}
{firstUsage.remaining !== undefined && (
<div className="flex items-center gap-0.5">
<span className="text-gray-500 dark:text-gray-400">
{t("usage.remaining")}
</span>
<span
className={`font-semibold tabular-nums ${
isExpired
? "text-red-500 dark:text-red-400"
: firstUsage.remaining <
(firstUsage.total || firstUsage.remaining) * 0.1
? "text-orange-500 dark:text-orange-400"
: "text-green-600 dark:text-green-400"
}`}
>
{firstUsage.remaining.toFixed(2)}
</span>
</div>
)}
{/* 单位 */}
{firstUsage.unit && (
<span className="text-gray-500 dark:text-gray-400">
{firstUsage.unit}
</span>
)}
</div>
</div>
);
}
return (
<div className="mt-3 pt-3 border-t border-border-default ">
{/* 标题行:包含刷新按钮 */}
{/* 标题行:包含刷新按钮和自动查询时间 */}
<div className="flex items-center justify-between mb-2">
<span className="text-xs text-gray-500 dark:text-gray-400 font-medium">
{t("usage.planUsage")}
</span>
<button
onClick={() => refetch()}
disabled={loading}
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50"
title={t("usage.refreshUsage")}
>
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
</button>
<div className="flex items-center gap-2">
{/* 自动查询时间提示 */}
{lastQueriedAt && (
<span className="text-[10px] text-gray-400 dark:text-gray-500 flex items-center gap-1">
<Clock size={10} />
{formatRelativeTime(lastQueriedAt, now, t)}
</span>
)}
<button
onClick={() => refetch()}
disabled={loading}
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50"
title={t("usage.refreshUsage")}
>
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
</button>
</div>
</div>
{/* 套餐列表 */}
@@ -197,4 +329,26 @@ const UsagePlanItem: React.FC<{ data: UsageData }> = ({ data }) => {
);
};
// 格式化相对时间
function formatRelativeTime(
timestamp: number,
now: number,
t: (key: string, options?: { count?: number }) => string,
): string {
const diff = Math.floor((now - timestamp) / 1000); // 秒
if (diff < 60) {
return t("usage.justNow");
} else if (diff < 3600) {
const minutes = Math.floor(diff / 60);
return t("usage.minutesAgo", { count: minutes });
} else if (diff < 86400) {
const hours = Math.floor(diff / 3600);
return t("usage.hoursAgo", { count: hours });
} else {
const days = Math.floor(diff / 86400);
return t("usage.daysAgo", { count: days });
}
}
export default UsageFooter;

View File

@@ -1,8 +1,8 @@
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";
import { Provider, UsageScript } from "@/types";
import { usageApi, type AppId } from "@/lib/api";
import JsonEditor from "./JsonEditor";
import * as prettier from "prettier/standalone";
@@ -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;
@@ -25,9 +28,32 @@ interface UsageScriptModalProps {
onSave: (script: UsageScript) => void;
}
// 预设模板JS 对象字面量格式
const PRESET_TEMPLATES: Record<string, string> = {
: `({
// 预设模板键名(用于国际化
const TEMPLATE_KEYS = {
CUSTOM: "custom",
GENERAL: "general",
NEW_API: "newapi",
} as const;
// 生成预设模板的函数(支持国际化)
const generatePresetTemplates = (
t: (key: string) => string,
): Record<string, string> => ({
[TEMPLATE_KEYS.CUSTOM]: `({
request: {
url: "",
method: "GET",
headers: {}
},
extractor: function(response) {
return {
remaining: 0,
unit: "USD"
};
}
})`,
[TEMPLATE_KEYS.GENERAL]: `({
request: {
url: "{{baseUrl}}/user/balance",
method: "GET",
@@ -45,41 +71,39 @@ const PRESET_TEMPLATES: Record<string, string> = {
}
})`,
NewAPI: `({
[TEMPLATE_KEYS.NEW_API]: `({
request: {
url: "{{baseUrl}}/api/usage/token",
url: "{{baseUrl}}/api/user/self",
method: "GET",
headers: {
Authorization: "Bearer {{apiKey}}",
"Content-Type": "application/json",
"Authorization": "Bearer {{accessToken}}",
"New-Api-User": "{{userId}}"
},
},
extractor: function (response) {
if (response.code) {
if (response.data.unlimited_quota) {
return {
planName: response.data.name,
total: -1,
used: response.data.total_used / 500000,
unit: "USD",
};
}
if (response.success && response.data) {
return {
isValid: true,
planName: response.data.name,
total: response.data.total_granted / 500000,
used: response.data.total_used / 500000,
remaining: response.data.total_available / 500000,
planName: response.data.group || "${t("usageScript.defaultPlan")}",
remaining: response.data.quota / 500000,
used: response.data.used_quota / 500000,
total: (response.data.quota + response.data.used_quota) / 500000,
unit: "USD",
};
}
if (response.error) {
return {
isValid: false,
invalidMessage: response.error.message,
};
}
return {
isValid: false,
invalidMessage: response.message || "${t("usageScript.queryFailedMessage")}"
};
},
})`,
});
// 模板名称国际化键映射
const TEMPLATE_NAME_KEYS: Record<string, string> = {
[TEMPLATE_KEYS.CUSTOM]: "usageScript.templateCustom",
[TEMPLATE_KEYS.GENERAL]: "usageScript.templateGeneral",
[TEMPLATE_KEYS.NEW_API]: "usageScript.templateNewAPI",
};
const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
@@ -90,16 +114,16 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
onSave,
}) => {
const { t } = useTranslation();
// 生成带国际化的预设模板
const PRESET_TEMPLATES = generatePresetTemplates(t);
const [script, setScript] = useState<UsageScript>(() => {
return (
provider.meta?.usage_script || {
enabled: false,
language: "javascript",
code: PRESET_TEMPLATES[
t("usageScript.presetTemplate") === "预设模板"
? "通用模板"
: "General"
],
code: PRESET_TEMPLATES[TEMPLATE_KEYS.GENERAL],
timeout: 10,
}
);
@@ -107,6 +131,22 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
const [testing, setTesting] = useState(false);
// 跟踪当前选择的模板类型(用于控制高级配置的显示)
// 初始化:如果已有 accessToken 或 userId说明是 NewAPI 模板
const [selectedTemplate, setSelectedTemplate] = useState<string | null>(
() => {
const existingScript = provider.meta?.usage_script;
if (existingScript?.accessToken || existingScript?.userId) {
return TEMPLATE_KEYS.NEW_API;
}
return null;
},
);
// 控制 API Key 的显示/隐藏
const [showApiKey, setShowApiKey] = useState(false);
const [showAccessToken, setShowAccessToken] = useState(false);
const handleSave = () => {
// 验证脚本格式
if (script.enabled && !script.code.trim()) {
@@ -127,7 +167,17 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
const handleTest = async () => {
setTesting(true);
try {
const result = await usageApi.query(provider.id, appId);
// 使用当前编辑器中的脚本内容进行测试
const result = await usageApi.testScript(
provider.id,
appId,
script.code,
script.timeout,
script.apiKey,
script.baseUrl,
script.accessToken,
script.userId,
);
if (result.success && result.data && result.data.length > 0) {
// 显示所有套餐数据
const summary = result.data
@@ -184,10 +234,41 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
const handleUsePreset = (presetName: string) => {
const preset = PRESET_TEMPLATES[presetName];
if (preset) {
setScript({ ...script, code: preset });
// 根据模板类型清空不同的字段
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 if (presetName === TEMPLATE_KEYS.NEW_API) {
// NewAPI清空 apiKeyNewAPI 不使用通用的 apiKey
setScript({
...script,
code: preset,
apiKey: undefined,
});
}
setSelectedTemplate(presetName); // 记录选择的模板
}
};
// 判断是否应该显示凭证配置区域
const shouldShowCredentialsConfig =
selectedTemplate === TEMPLATE_KEYS.GENERAL || selectedTemplate === TEMPLATE_KEYS.NEW_API;
return (
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-w-3xl max-h-[90vh] flex flex-col">
@@ -200,45 +281,176 @@ 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) => (
<button
key={name}
onClick={() => handleUsePreset(name)}
className="px-3 py-1.5 text-xs bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 rounded transition-colors"
>
{name}
</button>
))}
{Object.keys(PRESET_TEMPLATES).map((name) => {
const isSelected = selectedTemplate === name;
return (
<button
key={name}
onClick={() => handleUsePreset(name)}
className={`px-3 py-1.5 text-xs rounded transition-colors ${
isSelected
? "bg-blue-500 text-white dark:bg-blue-600"
: "bg-gray-100 dark:bg-gray-800 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700"
}`}
>
{t(TEMPLATE_NAME_KEYS[name])}
</button>
);
})}
</div>
</div>
{/* 凭证配置区域:通用和 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>
{/* 通用模板:显示 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 })}
@@ -255,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({
@@ -270,9 +483,32 @@ 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>
{/* 🆕 自动查询间隔 */}
<div className="space-y-2">
<Label htmlFor="usage-auto-interval">
{t("usageScript.autoQueryInterval")}
</Label>
<Input
id="usage-auto-interval"
type="number"
min={0}
max={1440}
step={1}
value={script.autoQueryInterval || 0}
onChange={(e) =>
setScript({
...script,
autoQueryInterval: parseInt(e.target.value) || 0,
})
}
/>
<p className="text-xs text-muted-foreground">
{t("usageScript.autoQueryIntervalHint")}
</p>
</div>
</div>
{/* 脚本说明 */}
@@ -292,10 +528,10 @@ const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
"Authorization": "Bearer {{apiKey}}",
"User-Agent": "cc-switch/1.0"
},
body: JSON.stringify({ key: "value" }) // 可选
body: JSON.stringify({ key: "value" }) // ${t("usageScript.commentOptional")}
},
extractor: function(response) {
// response 是 API 返回的 JSON 数据
// ${t("usageScript.commentResponseIsJson")}
return {
isValid: !response.error,
remaining: response.balance,

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

@@ -16,7 +16,7 @@ import {
ProviderForm,
type ProviderFormValues,
} from "@/components/providers/forms/ProviderForm";
import { providerPresets } from "@/config/providerPresets";
import { providerPresets } from "@/config/claudeProviderPresets";
import { codexProviderPresets } from "@/config/codexProviderPresets";
interface AddProviderDialogProps {

View File

@@ -123,6 +123,7 @@ export function EditProviderDialog({
<div className="flex-1 overflow-y-auto px-6 py-4">
<ProviderForm
appId={appId}
providerId={provider.id}
submitLabel={t("common.save")}
onSubmit={handleSubmit}
onCancel={() => onOpenChange(false)}

View File

@@ -170,20 +170,25 @@ export function ProviderCard({
</div>
</div>
<ProviderActions
isCurrent={isCurrent}
onSwitch={() => onSwitch(provider)}
onEdit={() => onEdit(provider)}
onConfigureUsage={() => onConfigureUsage(provider)}
onDelete={() => onDelete(provider)}
/>
</div>
<div className="flex items-center gap-3">
<UsageFooter
provider={provider}
providerId={provider.id}
appId={appId}
usageEnabled={usageEnabled}
isCurrent={isCurrent}
inline={true}
/>
<UsageFooter
providerId={provider.id}
appId={appId}
usageEnabled={usageEnabled}
/>
<ProviderActions
isCurrent={isCurrent}
onSwitch={() => onSwitch(provider)}
onEdit={() => onEdit(provider)}
onConfigureUsage={() => onConfigureUsage(provider)}
onDelete={() => onDelete(provider)}
/>
</div>
</div>
</div>
);
}

View File

@@ -2,16 +2,16 @@ import { useTranslation } from "react-i18next";
import { FormLabel } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import EndpointSpeedTest from "./EndpointSpeedTest";
import KimiModelSelector from "./KimiModelSelector";
import { ApiKeySection, EndpointField } from "./shared";
import type { ProviderCategory } from "@/types";
import type { TemplateValueConfig } from "@/config/providerPresets";
import type { TemplateValueConfig } from "@/config/claudeProviderPresets";
interface EndpointCandidate {
url: string;
}
interface ClaudeFormFieldsProps {
providerId?: string;
// API Key
shouldShowApiKey: boolean;
apiKey: string;
@@ -19,6 +19,8 @@ interface ClaudeFormFieldsProps {
category?: ProviderCategory;
shouldShowApiKeyLink: boolean;
websiteUrl: string;
isPartner?: boolean;
partnerPromotionKey?: string;
// Template Values
templateValueEntries: Array<[string, TemplateValueConfig]>;
@@ -35,20 +37,17 @@ interface ClaudeFormFieldsProps {
onCustomEndpointsChange: (endpoints: string[]) => void;
// Model Selector
shouldShowKimiSelector: boolean;
shouldShowModelSelector: boolean;
claudeModel: string;
claudeSmallFastModel: string;
defaultHaikuModel: string;
defaultSonnetModel: string;
defaultOpusModel: string;
onModelChange: (
field: "ANTHROPIC_MODEL" | "ANTHROPIC_SMALL_FAST_MODEL",
value: string,
) => void;
// Kimi Model Selector
kimiAnthropicModel: string;
kimiAnthropicSmallFastModel: string;
onKimiModelChange: (
field: "ANTHROPIC_MODEL" | "ANTHROPIC_SMALL_FAST_MODEL",
field:
| "ANTHROPIC_MODEL"
| "ANTHROPIC_DEFAULT_HAIKU_MODEL"
| "ANTHROPIC_DEFAULT_SONNET_MODEL"
| "ANTHROPIC_DEFAULT_OPUS_MODEL",
value: string,
) => void;
@@ -57,12 +56,15 @@ interface ClaudeFormFieldsProps {
}
export function ClaudeFormFields({
providerId,
shouldShowApiKey,
apiKey,
onApiKeyChange,
category,
shouldShowApiKeyLink,
websiteUrl,
isPartner,
partnerPromotionKey,
templateValueEntries,
templateValues,
templatePresetName,
@@ -73,14 +75,12 @@ export function ClaudeFormFields({
isEndpointModalOpen,
onEndpointModalToggle,
onCustomEndpointsChange,
shouldShowKimiSelector,
shouldShowModelSelector,
claudeModel,
claudeSmallFastModel,
defaultHaikuModel,
defaultSonnetModel,
defaultOpusModel,
onModelChange,
kimiAnthropicModel,
kimiAnthropicSmallFastModel,
onKimiModelChange,
speedTestEndpoints,
}: ClaudeFormFieldsProps) {
const { t } = useTranslation();
@@ -95,6 +95,8 @@ export function ClaudeFormFields({
category={category}
shouldShowLink={shouldShowApiKeyLink}
websiteUrl={websiteUrl}
isPartner={isPartner}
partnerPromotionKey={partnerPromotionKey}
/>
)}
@@ -150,6 +152,7 @@ export function ClaudeFormFields({
{shouldShowSpeedTest && isEndpointModalOpen && (
<EndpointSpeedTest
appId="claude"
providerId={providerId}
value={baseUrl}
onChange={onBaseUrlChange}
initialEndpoints={speedTestEndpoints}
@@ -163,12 +166,10 @@ export function ClaudeFormFields({
{shouldShowModelSelector && (
<div className="space-y-3">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{/* ANTHROPIC_MODEL */}
{/* 主模型 */}
<div className="space-y-2">
<FormLabel htmlFor="claudeModel">
{t("providerForm.anthropicModel", {
defaultValue: "主模型",
})}
{t("providerForm.anthropicModel", { defaultValue: "主模型" })}
</FormLabel>
<Input
id="claudeModel"
@@ -178,28 +179,73 @@ export function ClaudeFormFields({
onModelChange("ANTHROPIC_MODEL", e.target.value)
}
placeholder={t("providerForm.modelPlaceholder", {
defaultValue: "claude-3-7-sonnet-20250219",
defaultValue: "",
})}
autoComplete="off"
/>
</div>
{/* ANTHROPIC_SMALL_FAST_MODEL */}
{/* 默认 Haiku */}
<div className="space-y-2">
<FormLabel htmlFor="claudeSmallFastModel">
{t("providerForm.anthropicSmallFastModel", {
defaultValue: "快速模型",
<FormLabel htmlFor="claudeDefaultHaikuModel">
{t("providerForm.anthropicDefaultHaikuModel", {
defaultValue: "Haiku 默认模型",
})}
</FormLabel>
<Input
id="claudeSmallFastModel"
id="claudeDefaultHaikuModel"
type="text"
value={claudeSmallFastModel}
value={defaultHaikuModel}
onChange={(e) =>
onModelChange("ANTHROPIC_SMALL_FAST_MODEL", e.target.value)
onModelChange("ANTHROPIC_DEFAULT_HAIKU_MODEL", e.target.value)
}
placeholder={t("providerForm.smallModelPlaceholder", {
defaultValue: "claude-3-5-haiku-20241022",
placeholder={t("providerForm.haikuModelPlaceholder", {
defaultValue: "",
})}
autoComplete="off"
/>
</div>
{/* 默认 Sonnet */}
<div className="space-y-2">
<FormLabel htmlFor="claudeDefaultSonnetModel">
{t("providerForm.anthropicDefaultSonnetModel", {
defaultValue: "Sonnet 默认模型",
})}
</FormLabel>
<Input
id="claudeDefaultSonnetModel"
type="text"
value={defaultSonnetModel}
onChange={(e) =>
onModelChange(
"ANTHROPIC_DEFAULT_SONNET_MODEL",
e.target.value,
)
}
placeholder={t("providerForm.modelPlaceholder", {
defaultValue: "",
})}
autoComplete="off"
/>
</div>
{/* 默认 Opus */}
<div className="space-y-2">
<FormLabel htmlFor="claudeDefaultOpusModel">
{t("providerForm.anthropicDefaultOpusModel", {
defaultValue: "Opus 默认模型",
})}
</FormLabel>
<Input
id="claudeDefaultOpusModel"
type="text"
value={defaultOpusModel}
onChange={(e) =>
onModelChange("ANTHROPIC_DEFAULT_OPUS_MODEL", e.target.value)
}
placeholder={t("providerForm.modelPlaceholder", {
defaultValue: "",
})}
autoComplete="off"
/>
@@ -213,17 +259,6 @@ export function ClaudeFormFields({
</p>
</div>
)}
{/* Kimi 模型选择器 */}
{shouldShowKimiSelector && (
<KimiModelSelector
apiKey={apiKey}
anthropicModel={kimiAnthropicModel}
anthropicSmallFastModel={kimiAnthropicSmallFastModel}
onModelChange={onKimiModelChange}
disabled={category === "official"}
/>
)}
</>
);
}

View File

@@ -28,8 +28,6 @@ interface CodexConfigEditorProps {
configError: string; // config.toml 错误提示
isCustomMode?: boolean; // 是否为自定义模式
onWebsiteUrlChange?: (url: string) => void; // 更新网址回调
isTemplateModalOpen?: boolean; // 模态框状态

View File

@@ -1,5 +1,8 @@
import React from "react";
import { useTranslation } from "react-i18next";
import { Wand2 } from "lucide-react";
import { toast } from "sonner";
import { formatJSON } from "@/utils/formatters";
interface CodexAuthSectionProps {
value: string;
@@ -19,6 +22,25 @@ export const CodexAuthSection: React.FC<CodexAuthSectionProps> = ({
}) => {
const { t } = useTranslation();
const handleFormat = () => {
if (!value.trim()) return;
try {
const formatted = formatJSON(value);
onChange(formatted);
toast.success(t("common.formatSuccess", { defaultValue: "格式化成功" }));
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
toast.error(
t("common.formatError", {
defaultValue: "格式化失败:{{error}}",
error: errorMessage,
}),
);
}
};
return (
<div className="space-y-2">
<label
@@ -47,13 +69,26 @@ export const CodexAuthSection: React.FC<CodexAuthSectionProps> = ({
data-enable-grammarly="false"
/>
{error && (
<p className="text-xs text-red-500 dark:text-red-400">{error}</p>
)}
<div className="flex items-center justify-between">
<button
type="button"
onClick={handleFormat}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 transition-colors"
>
<Wand2 className="w-3.5 h-3.5" />
{t("common.format", { defaultValue: "格式化" })}
</button>
<p className="text-xs text-gray-500 dark:text-gray-400">
{t("codexConfig.authJsonHint")}
</p>
{error && (
<p className="text-xs text-red-500 dark:text-red-400">{error}</p>
)}
</div>
{!error && (
<p className="text-xs text-gray-500 dark:text-gray-400">
{t("codexConfig.authJsonHint")}
</p>
)}
</div>
);
};
@@ -141,9 +176,11 @@ export const CodexConfigSection: React.FC<CodexConfigSectionProps> = ({
<p className="text-xs text-red-500 dark:text-red-400">{configError}</p>
)}
<p className="text-xs text-gray-500 dark:text-gray-400">
{t("codexConfig.configTomlHint")}
</p>
{!configError && (
<p className="text-xs text-gray-500 dark:text-gray-400">
{t("codexConfig.configTomlHint")}
</p>
)}
</div>
);
};

View File

@@ -8,12 +8,15 @@ interface EndpointCandidate {
}
interface CodexFormFieldsProps {
providerId?: string;
// API Key
codexApiKey: string;
onApiKeyChange: (key: string) => void;
category?: ProviderCategory;
shouldShowApiKeyLink: boolean;
websiteUrl: string;
isPartner?: boolean;
partnerPromotionKey?: string;
// Base URL
shouldShowSpeedTest: boolean;
@@ -28,11 +31,14 @@ interface CodexFormFieldsProps {
}
export function CodexFormFields({
providerId,
codexApiKey,
onApiKeyChange,
category,
shouldShowApiKeyLink,
websiteUrl,
isPartner,
partnerPromotionKey,
shouldShowSpeedTest,
codexBaseUrl,
onBaseUrlChange,
@@ -54,6 +60,8 @@ export function CodexFormFields({
category={category}
shouldShowLink={shouldShowApiKeyLink}
websiteUrl={websiteUrl}
isPartner={isPartner}
partnerPromotionKey={partnerPromotionKey}
placeholder={{
official: t("providerForm.codexOfficialNoApiKey", {
defaultValue: "官方供应商无需 API Key",
@@ -81,6 +89,7 @@ export function CodexFormFields({
{shouldShowSpeedTest && isEndpointModalOpen && (
<EndpointSpeedTest
appId="codex"
providerId={providerId}
value={codexBaseUrl}
onChange={onBaseUrlChange}
initialEndpoints={speedTestEndpoints}

View File

@@ -4,8 +4,13 @@ import {
DialogContent,
DialogHeader,
DialogTitle,
DialogFooter,
} from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
import { Save, Wand2 } from "lucide-react";
import { toast } from "sonner";
import { formatJSON } from "@/utils/formatters";
interface CommonConfigEditorProps {
value: string;
@@ -34,6 +39,44 @@ export function CommonConfigEditor({
}: CommonConfigEditorProps) {
const { t } = useTranslation();
const handleFormatMain = () => {
if (!value.trim()) return;
try {
const formatted = formatJSON(value);
onChange(formatted);
toast.success(t("common.formatSuccess", { defaultValue: "格式化成功" }));
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
toast.error(
t("common.formatError", {
defaultValue: "格式化失败:{{error}}",
error: errorMessage,
}),
);
}
};
const handleFormatModal = () => {
if (!commonConfigSnippet.trim()) return;
try {
const formatted = formatJSON(commonConfigSnippet);
onCommonConfigSnippetChange(formatted);
toast.success(t("common.formatSuccess", { defaultValue: "格式化成功" }));
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
toast.error(
t("common.formatError", {
defaultValue: "格式化失败:{{error}}",
error: errorMessage,
}),
);
}
};
return (
<>
<div className="space-y-2">
@@ -94,26 +137,36 @@ export function CommonConfigEditor({
data-gramm_editor="false"
data-enable-grammarly="false"
/>
<p className="text-xs text-muted-foreground">
{t("claudeConfig.fullSettingsHint", {
defaultValue: "请填写完整的 Claude Code 配置",
})}
</p>
<div className="flex items-center justify-between">
<button
type="button"
onClick={handleFormatMain}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 transition-colors"
>
<Wand2 className="w-3.5 h-3.5" />
{t("common.format", { defaultValue: "格式化" })}
</button>
<p className="text-xs text-muted-foreground">
{t("claudeConfig.fullSettingsHint", {
defaultValue: "请填写完整的 Claude Code 配置",
})}
</p>
</div>
</div>
<Dialog
open={isModalOpen}
onOpenChange={(open) => !open && onModalClose()}
>
<DialogContent className="sm:max-w-[600px]">
<DialogHeader>
<DialogContent className="sm:max-w-[600px] max-h-[90vh] flex flex-col p-0">
<DialogHeader className="px-6 pt-6 pb-0">
<DialogTitle>
{t("claudeConfig.editCommonConfigTitle", {
defaultValue: "编辑通用配置片段",
})}
</DialogTitle>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="flex-1 overflow-auto px-6 py-4 space-y-4">
<p className="text-sm text-muted-foreground">
{t("claudeConfig.commonConfigHint", {
defaultValue: "通用配置片段将合并到所有启用它的供应商配置中",
@@ -134,12 +187,31 @@ export function CommonConfigEditor({
data-gramm_editor="false"
data-enable-grammarly="false"
/>
{commonConfigError && (
<p className="text-sm text-red-500 dark:text-red-400">
{commonConfigError}
</p>
)}
<div className="flex items-center justify-between">
<button
type="button"
onClick={handleFormatModal}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 transition-colors"
>
<Wand2 className="w-3.5 h-3.5" />
{t("common.format", { defaultValue: "格式化" })}
</button>
{commonConfigError && (
<p className="text-sm text-red-500 dark:text-red-400">
{commonConfigError}
</p>
)}
</div>
</div>
<DialogFooter>
<Button type="button" variant="outline" onClick={onModalClose}>
{t("common.cancel")}
</Button>
<Button type="button" onClick={onModalClose} className="gap-2">
<Save className="w-4 h-4" />
{t("common.save")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>

View File

@@ -281,70 +281,35 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
setAddError(null);
// 保存到后端
try {
if (providerId) {
await vscodeApi.addCustomEndpoint(appId, providerId, sanitized);
}
// 更新本地状态(延迟提交,不立即保存到后端
setEntries((prev) => {
if (prev.some((e) => e.url === sanitized)) return prev;
return [
...prev,
{
id: randomId(),
url: sanitized,
isCustom: true,
latency: null,
status: undefined,
error: null,
},
];
});
// 更新本地状态
setEntries((prev) => {
if (prev.some((e) => e.url === sanitized)) return prev;
return [
...prev,
{
id: randomId(),
url: sanitized,
isCustom: true,
latency: null,
status: undefined,
error: null,
},
];
});
if (!normalizedSelected) {
onChange(sanitized);
}
setCustomUrl("");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
setAddError(message || t("endpointTest.saveFailed"));
console.error(t("endpointTest.addEndpointFailed"), error);
if (!normalizedSelected) {
onChange(sanitized);
}
}, [customUrl, entries, normalizedSelected, onChange, appId, providerId, t]);
setCustomUrl("");
}, [customUrl, entries, normalizedSelected, onChange]);
const handleRemoveEndpoint = useCallback(
async (entry: EndpointEntry) => {
(entry: EndpointEntry) => {
// 清空之前的错误提示
setLastError(null);
// 如果有 providerId尝试从后端删除
if (entry.isCustom && providerId) {
try {
await vscodeApi.removeCustomEndpoint(appId, providerId, entry.url);
} catch (error) {
const errorMsg =
error instanceof Error ? error.message : String(error);
// 只有"端点不存在"时才允许删除本地条目
if (
errorMsg.includes("not found") ||
errorMsg.includes("does not exist") ||
errorMsg.includes("不存在")
) {
console.warn(t("endpointTest.removeEndpointFailed"), errorMsg);
// 继续删除本地条目
} else {
// 其他错误:显示错误提示,阻止删除
setLastError(t("endpointTest.removeFailed", { error: errorMsg }));
return;
}
}
}
// 更新本地状态(删除成功)
// 更新本地状态(延迟提交,不立即从后端删除
setEntries((prev) => {
const next = prev.filter((item) => item.id !== entry.id);
if (entry.url === normalizedSelected) {
@@ -354,7 +319,7 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
return next;
});
},
[normalizedSelected, onChange, appId, providerId, t],
[normalizedSelected, onChange],
);
const runSpeedTest = useCallback(async () => {
@@ -432,22 +397,11 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
}, [entries, autoSelect, appId, normalizedSelected, onChange, t]);
const handleSelect = useCallback(
async (url: string) => {
(url: string) => {
if (!url || url === normalizedSelected) return;
// 更新最后使用时间(对自定义端点)
const entry = entries.find((e) => e.url === url);
if (entry?.isCustom && providerId) {
try {
await vscodeApi.updateEndpointLastUsed(appId, providerId, url);
} catch (error) {
console.error(t("endpointTest.updateLastUsedFailed"), error);
}
}
onChange(url);
},
[normalizedSelected, onChange, appId, entries, providerId, t],
[normalizedSelected, onChange],
);
return (

View File

@@ -1,195 +0,0 @@
import React, { useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { ChevronDown, RefreshCw, AlertCircle } from "lucide-react";
interface KimiModel {
id: string;
object: string;
created: number;
owned_by: string;
}
interface KimiModelSelectorProps {
apiKey: string;
anthropicModel: string;
anthropicSmallFastModel: string;
onModelChange: (
field: "ANTHROPIC_MODEL" | "ANTHROPIC_SMALL_FAST_MODEL",
value: string,
) => void;
disabled?: boolean;
}
const KimiModelSelector: React.FC<KimiModelSelectorProps> = ({
apiKey,
anthropicModel,
anthropicSmallFastModel,
onModelChange,
disabled = false,
}) => {
const { t } = useTranslation();
const [models, setModels] = useState<KimiModel[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [debouncedKey, setDebouncedKey] = useState("");
// 获取模型列表
const fetchModelsWithKey = async (key: string) => {
if (!key) {
setError(t("kimiSelector.fillApiKeyFirst"));
return;
}
setLoading(true);
setError("");
try {
const response = await fetch("https://api.moonshot.cn/v1/models", {
headers: {
Authorization: `Bearer ${key}`,
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error(
t("kimiSelector.requestFailed", {
error: `${response.status} ${response.statusText}`,
}),
);
}
const data = await response.json();
if (data.data && Array.isArray(data.data)) {
setModels(data.data);
} else {
throw new Error(t("kimiSelector.invalidData"));
}
} catch (err) {
console.error(t("kimiSelector.fetchModelsFailed") + ":", err);
setError(
err instanceof Error
? err.message
: t("kimiSelector.fetchModelsFailed"),
);
} finally {
setLoading(false);
}
};
// 500ms 防抖 API Key
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedKey(apiKey.trim());
}, 500);
return () => clearTimeout(timer);
}, [apiKey]);
// 当防抖后的 Key 改变时自动获取模型列表
useEffect(() => {
if (debouncedKey) {
fetchModelsWithKey(debouncedKey);
} else {
setModels([]);
setError("");
}
}, [debouncedKey]);
const selectClass = `w-full px-3 py-2 border rounded-lg text-sm transition-colors appearance-none bg-white dark:bg-gray-800 ${
disabled
? "bg-gray-100 dark:bg-gray-800 border-border-default text-gray-400 dark:text-gray-500 cursor-not-allowed"
: "border-border-default dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500/20 dark:focus:ring-blue-400/20 focus:border-border-active "
}`;
const ModelSelect: React.FC<{
label: string;
value: string;
onChange: (value: string) => void;
}> = ({ label, value, onChange }) => (
<div className="space-y-2">
<label className="block text-sm font-medium text-gray-900 dark:text-gray-100">
{label}
</label>
<div className="relative">
<select
value={value}
onChange={(e) => onChange(e.target.value)}
disabled={disabled || loading || models.length === 0}
className={selectClass}
>
<option value="">
{loading
? t("common.loading")
: models.length === 0
? t("kimiSelector.noModels")
: t("kimiSelector.pleaseSelectModel")}
</option>
{models.map((model) => (
<option key={model.id} value={model.id}>
{model.id}
</option>
))}
</select>
<ChevronDown
size={16}
className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-500 dark:text-gray-400 pointer-events-none"
/>
</div>
</div>
);
return (
<div className="space-y-4">
<div className="flex items-center justify-between">
<h3 className="text-sm font-medium text-gray-900 dark:text-gray-100">
{t("kimiSelector.modelConfig")}
</h3>
<button
type="button"
onClick={() => debouncedKey && fetchModelsWithKey(debouncedKey)}
disabled={disabled || loading || !debouncedKey}
className="inline-flex items-center gap-1 px-2 py-1 text-xs text-gray-500 dark:text-gray-400 hover:text-blue-500 dark:hover:text-blue-400 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
{t("kimiSelector.refreshModels")}
</button>
</div>
{error && (
<div className="flex items-center gap-2 p-3 bg-red-100 dark:bg-red-900/20 border border-red-500/20 dark:border-red-500/30 rounded-lg">
<AlertCircle
size={16}
className="text-red-500 dark:text-red-400 flex-shrink-0"
/>
<p className="text-red-500 dark:text-red-400 text-xs">{error}</p>
</div>
)}
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<ModelSelect
label={t("kimiSelector.mainModel")}
value={anthropicModel}
onChange={(value) => onModelChange("ANTHROPIC_MODEL", value)}
/>
<ModelSelect
label={t("kimiSelector.fastModel")}
value={anthropicSmallFastModel}
onChange={(value) =>
onModelChange("ANTHROPIC_SMALL_FAST_MODEL", value)
}
/>
</div>
{!apiKey.trim() && (
<div className="p-3 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-700 rounded-lg">
<p className="text-xs text-amber-600 dark:text-amber-400">
{t("kimiSelector.apiKeyHint")}
</p>
</div>
)}
</div>
);
};
export default KimiModelSelector;

View File

@@ -3,11 +3,14 @@ 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";
import { providerPresets, type ProviderPreset } from "@/config/providerPresets";
import {
providerPresets,
type ProviderPreset,
} from "@/config/claudeProviderPresets";
import {
codexProviderPresets,
type CodexProviderPreset,
@@ -27,8 +30,6 @@ import {
useModelState,
useCodexConfigState,
useApiKeyLink,
useCustomEndpoints,
useKimiModelSelector,
useTemplateValues,
useCommonConfigSnippet,
useCodexCommonConfig,
@@ -46,6 +47,7 @@ type PresetEntry = {
interface ProviderFormProps {
appId: AppId;
providerId?: string;
submitLabel: string;
onSubmit: (values: ProviderFormValues) => void;
onCancel: () => void;
@@ -61,6 +63,7 @@ interface ProviderFormProps {
export function ProviderForm({
appId,
providerId,
submitLabel,
onSubmit,
onCancel,
@@ -76,12 +79,20 @@ export function ProviderForm({
const [activePreset, setActivePreset] = useState<{
id: string;
category?: ProviderCategory;
isPartner?: boolean;
} | null>(null);
const [isEndpointModalOpen, setIsEndpointModalOpen] = useState(false);
// 新建供应商:收集端点测速弹窗中的"自定义端点",提交时一次性落盘到 meta.custom_endpoints
// 编辑供应商:从 initialData.meta.custom_endpoints 恢复端点列表
const [draftCustomEndpoints, setDraftCustomEndpoints] = useState<string[]>(
[],
() => {
if (!initialData?.meta?.custom_endpoints) {
return [];
}
// 从 Record<string, CustomEndpoint> 中提取 URL 列表
return Object.keys(initialData.meta.custom_endpoints);
},
);
// 使用 category hook
@@ -95,6 +106,13 @@ export function ProviderForm({
useEffect(() => {
setSelectedPresetId(initialData ? null : "custom");
setActivePreset(null);
// 重新初始化 draftCustomEndpoints编辑模式时从 meta 恢复)
if (initialData?.meta?.custom_endpoints) {
setDraftCustomEndpoints(Object.keys(initialData.meta.custom_endpoints));
} else {
setDraftCustomEndpoints([]);
}
}, [appId, initialData]);
const defaultValues: ProviderFormData = useMemo(
@@ -140,12 +158,17 @@ export function ProviderForm({
},
});
// 使用 Model hook
const { claudeModel, claudeSmallFastModel, handleModelChange } =
useModelState({
settingsConfig: form.watch("settingsConfig"),
onConfigChange: (config) => form.setValue("settingsConfig", config),
});
// 使用 Model hook(新:主模型 + Haiku/Sonnet/Opus 默认模型)
const {
claudeModel,
defaultHaikuModel,
defaultSonnetModel,
defaultOpusModel,
handleModelChange,
} = useModelState({
settingsConfig: form.watch("settingsConfig"),
onConfigChange: (config) => form.setValue("settingsConfig", config),
});
// 使用 Codex 配置 hook (仅 Codex 模式)
const {
@@ -214,24 +237,6 @@ export function ProviderForm({
}));
}, [appId]);
// 使用 Kimi 模型选择器 hook
const {
shouldShow: shouldShowKimiSelector,
kimiAnthropicModel,
kimiAnthropicSmallFastModel,
handleKimiModelChange,
} = useKimiModelSelector({
initialData,
settingsConfig: form.watch("settingsConfig"),
onConfigChange: (config) => form.setValue("settingsConfig", config),
selectedPresetId,
presetName:
selectedPresetId && selectedPresetId !== "custom"
? presetEntries.find((item) => item.id === selectedPresetId)?.preset
.name || ""
: "",
});
// 使用模板变量 hook (仅 Claude 模式)
const {
templateValues,
@@ -283,7 +288,7 @@ export function ProviderForm({
type: "manual",
message: t("providerForm.fillParameter", {
label: validation.missingField.label,
defaultValue: `填写 ${validation.missingField.label}`,
defaultValue: `<EFBFBD><EFBFBD><EFBFBD>填写 ${validation.missingField.label}`,
}),
});
return;
@@ -322,10 +327,49 @@ export function ProviderForm({
if (activePreset.category) {
payload.presetCategory = activePreset.category;
}
// 继承合作伙伴标识
if (activePreset.isPartner) {
payload.isPartner = activePreset.isPartner;
}
}
// 处理 meta 字段(新建与编辑使用不同策略)
const mergedMeta = mergeProviderMeta(initialData?.meta, customEndpointsMap);
// 处理 meta 字段:基于 draftCustomEndpoints 生成 custom_endpoints
// 注意:不使用 customEndpointsMap因为它包含了候选端点预设、Base URL 等)
// 而我们只需要保存用户真正添加的自定义端点
const customEndpointsToSave: Record<
string,
import("@/types").CustomEndpoint
> | null =
draftCustomEndpoints.length > 0
? draftCustomEndpoints.reduce(
(acc, url) => {
// 尝试从 initialData.meta 中获取原有的端点元数据(保留 addedAt 和 lastUsed
const existing = initialData?.meta?.custom_endpoints?.[url];
if (existing) {
acc[url] = existing;
} else {
// 新端点:使用当前时间戳
const now = Date.now();
acc[url] = { url, addedAt: now, lastUsed: undefined };
}
return acc;
},
{} as Record<string, import("@/types").CustomEndpoint>,
)
: null;
// 检测是否需要清空端点(重要:区分"用户清空端点"和"用户没有修改端点"
const hadEndpoints =
initialData?.meta?.custom_endpoints &&
Object.keys(initialData.meta.custom_endpoints).length > 0;
const needsClearEndpoints =
hadEndpoints && draftCustomEndpoints.length === 0;
// 如果用户明确清空了端点,传递空对象(而不是 null让后端知道要删除
const mergedMeta = needsClearEndpoints
? mergeProviderMeta(initialData?.meta, {})
: mergeProviderMeta(initialData?.meta, customEndpointsToSave);
if (mergedMeta) {
payload.meta = mergedMeta;
}
@@ -350,14 +394,15 @@ export function ProviderForm({
);
}, [groupedPresets]);
// 判断是否显示端点测速(仅第三方和自定义类别
const shouldShowSpeedTest =
category === "third_party" || category === "custom";
// 判断是否显示端点测速(仅官方类别不显示
const shouldShowSpeedTest = category !== "official";
// 使用 API Key 链接 hook (Claude)
const {
shouldShowApiKeyLink: shouldShowClaudeApiKeyLink,
websiteUrl: claudeWebsiteUrl,
isPartner: isClaudePartner,
partnerPromotionKey: claudePartnerPromotionKey,
} = useApiKeyLink({
appId: "claude",
category,
@@ -370,6 +415,8 @@ export function ProviderForm({
const {
shouldShowApiKeyLink: shouldShowCodexApiKeyLink,
websiteUrl: codexWebsiteUrl,
isPartner: isCodexPartner,
partnerPromotionKey: codexPartnerPromotionKey,
} = useApiKeyLink({
appId: "codex",
category,
@@ -378,16 +425,6 @@ export function ProviderForm({
formWebsiteUrl: form.watch("websiteUrl") || "",
});
// 使用自定义端点 hook
const customEndpointsMap = useCustomEndpoints({
appId,
selectedPresetId,
presetEntries,
draftCustomEndpoints,
baseUrl,
codexBaseUrl,
});
// 使用端点测速候选 hook
const speedTestEndpoints = useSpeedTestEndpoints({
appId,
@@ -419,6 +456,7 @@ export function ProviderForm({
setActivePreset({
id: value,
category: entry.preset.category,
isPartner: entry.preset.isPartner,
});
if (appId === "codex") {
@@ -467,6 +505,12 @@ export function ProviderForm({
presetCategoryLabels={presetCategoryLabels}
onPresetChange={handlePresetChange}
category={category}
appId={appId}
onOpenWizard={
appId === "codex"
? () => setIsCodexTemplateModalOpen(true)
: undefined
}
/>
)}
@@ -476,6 +520,7 @@ export function ProviderForm({
{/* Claude 专属字段 */}
{appId === "claude" && (
<ClaudeFormFields
providerId={providerId}
shouldShowApiKey={shouldShowApiKey(
form.watch("settingsConfig"),
isEditMode,
@@ -485,6 +530,8 @@ export function ProviderForm({
category={category}
shouldShowApiKeyLink={shouldShowClaudeApiKeyLink}
websiteUrl={claudeWebsiteUrl}
isPartner={isClaudePartner}
partnerPromotionKey={claudePartnerPromotionKey}
templateValueEntries={templateValueEntries}
templateValues={templateValues}
templatePresetName={templatePreset?.name || ""}
@@ -495,16 +542,12 @@ export function ProviderForm({
isEndpointModalOpen={isEndpointModalOpen}
onEndpointModalToggle={setIsEndpointModalOpen}
onCustomEndpointsChange={setDraftCustomEndpoints}
shouldShowKimiSelector={shouldShowKimiSelector}
shouldShowModelSelector={
category !== "official" && !shouldShowKimiSelector
}
shouldShowModelSelector={category !== "official"}
claudeModel={claudeModel}
claudeSmallFastModel={claudeSmallFastModel}
defaultHaikuModel={defaultHaikuModel}
defaultSonnetModel={defaultSonnetModel}
defaultOpusModel={defaultOpusModel}
onModelChange={handleModelChange}
kimiAnthropicModel={kimiAnthropicModel}
kimiAnthropicSmallFastModel={kimiAnthropicSmallFastModel}
onKimiModelChange={handleKimiModelChange}
speedTestEndpoints={speedTestEndpoints}
/>
)}
@@ -512,11 +555,14 @@ export function ProviderForm({
{/* Codex 专属字段 */}
{appId === "codex" && (
<CodexFormFields
providerId={providerId}
codexApiKey={codexApiKey}
onApiKeyChange={handleCodexApiKeyChange}
category={category}
shouldShowApiKeyLink={shouldShowCodexApiKeyLink}
websiteUrl={codexWebsiteUrl}
isPartner={isCodexPartner}
partnerPromotionKey={codexPartnerPromotionKey}
shouldShowSpeedTest={shouldShowSpeedTest}
codexBaseUrl={codexBaseUrl}
onBaseUrlChange={handleCodexBaseUrlChange}
@@ -529,37 +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}
isCustomMode={selectedPresetId === "custom"}
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 && (
@@ -578,5 +647,6 @@ export function ProviderForm({
export type ProviderFormValues = ProviderFormData & {
presetId?: string;
presetCategory?: ProviderCategory;
isPartner?: boolean;
meta?: ProviderMeta;
};

View File

@@ -1,10 +1,11 @@
import { useTranslation } from "react-i18next";
import { FormLabel } from "@/components/ui/form";
import { ClaudeIcon, CodexIcon } from "@/components/BrandIcons";
import { Zap } from "lucide-react";
import type { ProviderPreset } from "@/config/providerPresets";
import { Zap, Star } from "lucide-react";
import type { ProviderPreset } from "@/config/claudeProviderPresets";
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
import type { ProviderCategory } from "@/types";
import type { AppId } from "@/lib/api";
type PresetEntry = {
id: string;
@@ -18,6 +19,8 @@ interface ProviderPresetSelectorProps {
presetCategoryLabels: Record<string, string>;
onPresetChange: (value: string) => void;
category?: ProviderCategory; // 新增:当前选中的分类
appId?: AppId;
onOpenWizard?: () => void; // Codex 专用:打开配置向导
}
export function ProviderPresetSelector({
@@ -27,11 +30,13 @@ export function ProviderPresetSelector({
presetCategoryLabels,
onPresetChange,
category,
appId,
onOpenWizard,
}: ProviderPresetSelectorProps) {
const { t } = useTranslation();
// 根据分类获取提示文字
const getCategoryHint = () => {
const getCategoryHint = (): React.ReactNode => {
switch (category) {
case "official":
return t("providerForm.officialHint", {
@@ -50,6 +55,23 @@ export function ProviderPresetSelector({
defaultValue: "💡 第三方供应商需要填写 API Key 和请求地址",
});
case "custom":
// Codex 自定义:在此位置显示"手动配置…或者 使用配置向导"
if (appId === "codex" && onOpenWizard) {
return (
<>
{t("providerForm.manualConfig")}
<button
type="button"
onClick={onOpenWizard}
className="ml-1 text-blue-500 dark:text-blue-400 hover:text-blue-600 dark:hover:text-blue-300 underline-offset-2 hover:underline"
aria-label={t("providerForm.openConfigWizard")}
>
{t("providerForm.useConfigWizard")}
</button>
</>
);
}
// 其他情况沿用原提示
return t("providerForm.customApiKeyHint", {
defaultValue: "💡 自定义配置需手动填写所有必要字段",
});
@@ -135,12 +157,13 @@ export function ProviderPresetSelector({
if (!entries || entries.length === 0) return null;
return entries.map((entry) => {
const isSelected = selectedPresetId === entry.id;
const isPartner = entry.preset.isPartner;
return (
<button
key={entry.id}
type="button"
onClick={() => onPresetChange(entry.id)}
className={getPresetButtonClass(isSelected, entry.preset)}
className={`${getPresetButtonClass(isSelected, entry.preset)} relative`}
style={getPresetButtonStyle(isSelected, entry.preset)}
title={
presetCategoryLabels[category] ??
@@ -151,6 +174,11 @@ export function ProviderPresetSelector({
>
{renderPresetIcon(entry.preset)}
{entry.preset.name}
{isPartner && (
<span className="absolute -top-1 -right-1 flex items-center gap-0.5 rounded-full bg-gradient-to-r from-amber-500 to-yellow-500 px-1.5 py-0.5 text-[10px] font-bold text-white shadow-md">
<Star className="h-2.5 w-2.5 fill-current" />
</span>
)}
</button>
);
});

View File

@@ -5,7 +5,6 @@ export { useModelState } from "./useModelState";
export { useCodexConfigState } from "./useCodexConfigState";
export { useApiKeyLink } from "./useApiKeyLink";
export { useCustomEndpoints } from "./useCustomEndpoints";
export { useKimiModelSelector } from "./useKimiModelSelector";
export { useTemplateValues } from "./useTemplateValues";
export { useCommonConfigSnippet } from "./useCommonConfigSnippet";
export { useCodexCommonConfig } from "./useCodexCommonConfig";

View File

@@ -1,7 +1,7 @@
import { useMemo } from "react";
import type { AppId } from "@/lib/api";
import type { ProviderCategory } from "@/types";
import type { ProviderPreset } from "@/config/providerPresets";
import type { ProviderPreset } from "@/config/claudeProviderPresets";
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
type PresetEntry = {
@@ -37,20 +37,39 @@ export function useApiKeyLink({
);
}, [category]);
// 获取当前预设条目
const currentPresetEntry = useMemo(() => {
if (selectedPresetId && selectedPresetId !== "custom") {
return presetEntries.find((item) => item.id === selectedPresetId);
}
return undefined;
}, [selectedPresetId, presetEntries]);
// 获取当前供应商的网址(用于 API Key 链接)
const getWebsiteUrl = useMemo(() => {
if (selectedPresetId && selectedPresetId !== "custom") {
const entry = presetEntries.find((item) => item.id === selectedPresetId);
if (entry) {
const preset = entry.preset;
// 第三方供应商优先使用 apiKeyUrl
return preset.category === "third_party"
? preset.apiKeyUrl || preset.websiteUrl || ""
: preset.websiteUrl || "";
if (currentPresetEntry) {
const preset = currentPresetEntry.preset;
// 对于 cn_official、aggregator、third_party优先使用 apiKeyUrl可能包含推广参数
if (
preset.category === "cn_official" ||
preset.category === "aggregator" ||
preset.category === "third_party"
) {
return preset.apiKeyUrl || preset.websiteUrl || "";
}
return preset.websiteUrl || "";
}
return formWebsiteUrl || "";
}, [selectedPresetId, presetEntries, formWebsiteUrl]);
}, [currentPresetEntry, formWebsiteUrl]);
// 提取合作伙伴信息
const isPartner = useMemo(() => {
return currentPresetEntry?.preset.isPartner ?? false;
}, [currentPresetEntry]);
const partnerPromotionKey = useMemo(() => {
return currentPresetEntry?.preset.partnerPromotionKey;
}, [currentPresetEntry]);
return {
shouldShowApiKeyLink:
@@ -60,5 +79,7 @@ export function useApiKeyLink({
? shouldShowApiKeyLink
: false,
websiteUrl: getWebsiteUrl,
isPartner,
partnerPromotionKey,
};
}

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?: {
@@ -68,6 +69,24 @@ export function useCodexConfigState({ initialData }: UseCodexConfigStateProps) {
}
}, [codexConfig, codexBaseUrl]);
// 获取 API Key从 auth JSON
const getCodexAuthApiKey = useCallback((authString: string): string => {
try {
const auth = JSON.parse(authString || "{}");
return typeof auth.OPENAI_API_KEY === "string" ? auth.OPENAI_API_KEY : "";
} catch {
return "";
}
}, []);
// 从 codexAuth 中提取并同步 API Key
useEffect(() => {
const extractedKey = getCodexAuthApiKey(codexAuth);
if (extractedKey !== codexApiKey) {
setCodexApiKey(extractedKey);
}
}, [codexAuth, codexApiKey]);
// 验证 Codex Auth JSON
const validateCodexAuth = useCallback((value: string): string => {
if (!value.trim()) return "";
@@ -106,10 +125,11 @@ export function useCodexConfigState({ initialData }: UseCodexConfigStateProps) {
// 处理 Codex API Key 输入并写回 auth.json
const handleCodexApiKeyChange = useCallback(
(key: string) => {
setCodexApiKey(key);
const trimmed = key.trim();
setCodexApiKey(trimmed);
try {
const auth = JSON.parse(codexAuth || "{}");
auth.OPENAI_API_KEY = key.trim();
auth.OPENAI_API_KEY = trimmed;
setCodexAuth(JSON.stringify(auth, null, 2));
} catch {
// ignore
@@ -140,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);
}
@@ -178,16 +200,6 @@ export function useCodexConfigState({ initialData }: UseCodexConfigStateProps) {
[setCodexAuth, setCodexConfig],
);
// 获取 API Key从 auth JSON
const getCodexAuthApiKey = useCallback((authString: string): string => {
try {
const auth = JSON.parse(authString || "{}");
return typeof auth.OPENAI_API_KEY === "string" ? auth.OPENAI_API_KEY : "";
} catch {
return "";
}
}, []);
return {
codexAuth,
codexConfig,

View File

@@ -1,7 +1,7 @@
import { useMemo } from "react";
import type { AppId } from "@/lib/api";
import type { CustomEndpoint } from "@/types";
import type { ProviderPreset } from "@/config/providerPresets";
import type { ProviderPreset } from "@/config/claudeProviderPresets";
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
type PresetEntry = {

View File

@@ -1,116 +0,0 @@
import { useState, useEffect, useCallback } from "react";
interface UseKimiModelSelectorProps {
initialData?: {
settingsConfig?: Record<string, unknown>;
};
settingsConfig: string;
onConfigChange: (config: string) => void;
selectedPresetId: string | null;
presetName?: string;
}
/**
* 管理 Kimi 模型选择器的状态和逻辑
*/
export function useKimiModelSelector({
initialData,
settingsConfig,
onConfigChange,
selectedPresetId,
presetName = "",
}: UseKimiModelSelectorProps) {
const [kimiAnthropicModel, setKimiAnthropicModel] = useState("");
const [kimiAnthropicSmallFastModel, setKimiAnthropicSmallFastModel] =
useState("");
// 判断是否显示 Kimi 模型选择器
const shouldShowKimiSelector =
selectedPresetId !== null &&
selectedPresetId !== "custom" &&
presetName.includes("Kimi");
// 判断是否正在编辑 Kimi 供应商
const isEditingKimi = Boolean(
initialData &&
settingsConfig.includes("api.moonshot.cn") &&
settingsConfig.includes("ANTHROPIC_MODEL"),
);
const shouldShow = shouldShowKimiSelector || isEditingKimi;
// 初始化 Kimi 模型选择(编辑模式)
useEffect(() => {
if (
initialData?.settingsConfig &&
typeof initialData.settingsConfig === "object"
) {
const config = initialData.settingsConfig as {
env?: Record<string, unknown>;
};
if (config.env) {
const model =
typeof config.env.ANTHROPIC_MODEL === "string"
? config.env.ANTHROPIC_MODEL
: "";
const smallFastModel =
typeof config.env.ANTHROPIC_SMALL_FAST_MODEL === "string"
? config.env.ANTHROPIC_SMALL_FAST_MODEL
: "";
setKimiAnthropicModel(model);
setKimiAnthropicSmallFastModel(smallFastModel);
}
}
}, [initialData]);
// 处理 Kimi 模型变化
const handleKimiModelChange = useCallback(
(
field: "ANTHROPIC_MODEL" | "ANTHROPIC_SMALL_FAST_MODEL",
value: string,
) => {
if (field === "ANTHROPIC_MODEL") {
setKimiAnthropicModel(value);
} else {
setKimiAnthropicSmallFastModel(value);
}
// 更新配置 JSON
try {
const currentConfig = JSON.parse(settingsConfig || "{}");
if (!currentConfig.env) currentConfig.env = {};
currentConfig.env[field] = value;
const updatedConfigString = JSON.stringify(currentConfig, null, 2);
onConfigChange(updatedConfigString);
} catch (err) {
console.error("更新 Kimi 模型配置失败:", err);
}
},
[settingsConfig, onConfigChange],
);
// 当选择 Kimi 预设时,同步模型值
useEffect(() => {
if (shouldShowKimiSelector && settingsConfig) {
try {
const config = JSON.parse(settingsConfig);
if (config.env) {
const model = config.env.ANTHROPIC_MODEL || "";
const smallFastModel = config.env.ANTHROPIC_SMALL_FAST_MODEL || "";
setKimiAnthropicModel(model);
setKimiAnthropicSmallFastModel(smallFastModel);
}
} catch {
// ignore
}
}
}, [shouldShowKimiSelector, settingsConfig]);
return {
shouldShow,
kimiAnthropicModel,
kimiAnthropicSmallFastModel,
handleKimiModelChange,
};
}

View File

@@ -1,4 +1,4 @@
import { useState, useCallback } from "react";
import { useState, useCallback, useEffect } from "react";
interface UseModelStateProps {
settingsConfig: string;
@@ -14,18 +14,62 @@ export function useModelState({
onConfigChange,
}: UseModelStateProps) {
const [claudeModel, setClaudeModel] = useState("");
const [claudeSmallFastModel, setClaudeSmallFastModel] = useState("");
const [defaultHaikuModel, setDefaultHaikuModel] = useState("");
const [defaultSonnetModel, setDefaultSonnetModel] = useState("");
const [defaultOpusModel, setDefaultOpusModel] = useState("");
// 初始化读取:读新键;若缺失,按兼容优先级回退
// Haiku: DEFAULT_HAIKU || SMALL_FAST || MODEL
// Sonnet: DEFAULT_SONNET || MODEL || SMALL_FAST
// Opus: DEFAULT_OPUS || MODEL || SMALL_FAST
// 仅在 settingsConfig 变化时同步一次(表单加载/切换预设时)
useEffect(() => {
try {
const cfg = settingsConfig ? JSON.parse(settingsConfig) : {};
const env = cfg?.env || {};
const model =
typeof env.ANTHROPIC_MODEL === "string" ? env.ANTHROPIC_MODEL : "";
const small =
typeof env.ANTHROPIC_SMALL_FAST_MODEL === "string"
? env.ANTHROPIC_SMALL_FAST_MODEL
: "";
const haiku =
typeof env.ANTHROPIC_DEFAULT_HAIKU_MODEL === "string"
? env.ANTHROPIC_DEFAULT_HAIKU_MODEL
: small || model;
const sonnet =
typeof env.ANTHROPIC_DEFAULT_SONNET_MODEL === "string"
? env.ANTHROPIC_DEFAULT_SONNET_MODEL
: model || small;
const opus =
typeof env.ANTHROPIC_DEFAULT_OPUS_MODEL === "string"
? env.ANTHROPIC_DEFAULT_OPUS_MODEL
: model || small;
setClaudeModel(model || "");
setDefaultHaikuModel(haiku || "");
setDefaultSonnetModel(sonnet || "");
setDefaultOpusModel(opus || "");
} catch {
// ignore
}
}, [settingsConfig]);
const handleModelChange = useCallback(
(
field: "ANTHROPIC_MODEL" | "ANTHROPIC_SMALL_FAST_MODEL",
field:
| "ANTHROPIC_MODEL"
| "ANTHROPIC_DEFAULT_HAIKU_MODEL"
| "ANTHROPIC_DEFAULT_SONNET_MODEL"
| "ANTHROPIC_DEFAULT_OPUS_MODEL",
value: string,
) => {
if (field === "ANTHROPIC_MODEL") {
setClaudeModel(value);
} else {
setClaudeSmallFastModel(value);
}
if (field === "ANTHROPIC_MODEL") setClaudeModel(value);
if (field === "ANTHROPIC_DEFAULT_HAIKU_MODEL")
setDefaultHaikuModel(value);
if (field === "ANTHROPIC_DEFAULT_SONNET_MODEL")
setDefaultSonnetModel(value);
if (field === "ANTHROPIC_DEFAULT_OPUS_MODEL") setDefaultOpusModel(value);
try {
const currentConfig = settingsConfig
@@ -33,15 +77,18 @@ export function useModelState({
: { env: {} };
if (!currentConfig.env) currentConfig.env = {};
if (value.trim()) {
currentConfig.env[field] = value.trim();
// 新键仅写入;旧键不再写入
const trimmed = value.trim();
if (trimmed) {
currentConfig.env[field] = trimmed;
} else {
delete currentConfig.env[field];
}
// 删除旧键
delete currentConfig.env["ANTHROPIC_SMALL_FAST_MODEL"];
onConfigChange(JSON.stringify(currentConfig, null, 2));
} catch (err) {
// 如果 JSON 解析失败,不做处理
console.error("Failed to update model config:", err);
}
},
@@ -51,8 +98,12 @@ export function useModelState({
return {
claudeModel,
setClaudeModel,
claudeSmallFastModel,
setClaudeSmallFastModel,
defaultHaikuModel,
setDefaultHaikuModel,
defaultSonnetModel,
setDefaultSonnetModel,
defaultOpusModel,
setDefaultOpusModel,
handleModelChange,
};
}

View File

@@ -1,7 +1,7 @@
import { useState, useEffect } from "react";
import type { ProviderCategory } from "@/types";
import type { AppId } from "@/lib/api";
import { providerPresets } from "@/config/providerPresets";
import { providerPresets } from "@/config/claudeProviderPresets";
import { codexProviderPresets } from "@/config/codexProviderPresets";
interface UseProviderCategoryProps {

View File

@@ -1,6 +1,6 @@
import { useMemo } from "react";
import type { AppId } from "@/lib/api";
import type { ProviderPreset } from "@/config/providerPresets";
import type { ProviderPreset } from "@/config/claudeProviderPresets";
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
import type { ProviderMeta, EndpointCandidate } from "@/types";

View File

@@ -2,7 +2,7 @@ import { useState, useEffect, useCallback, useMemo } from "react";
import type {
ProviderPreset,
TemplateValueConfig,
} from "@/config/providerPresets";
} from "@/config/claudeProviderPresets";
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
import { applyTemplateValues } from "@/utils/providerConfigUtils";

View File

@@ -15,6 +15,8 @@ interface ApiKeySectionProps {
thirdParty: string;
};
disabled?: boolean;
isPartner?: boolean;
partnerPromotionKey?: string;
}
export function ApiKeySection({
@@ -27,6 +29,8 @@ export function ApiKeySection({
websiteUrl,
placeholder,
disabled,
isPartner,
partnerPromotionKey,
}: ApiKeySectionProps) {
const { t } = useTranslation();
@@ -57,7 +61,7 @@ export function ApiKeySection({
/>
{/* API Key 获取链接 */}
{shouldShowLink && websiteUrl && (
<div className="-mt-1 pl-1">
<div className="space-y-2 -mt-1 pl-1">
<a
href={websiteUrl}
target="_blank"
@@ -68,6 +72,18 @@ export function ApiKeySection({
defaultValue: "获取 API Key",
})}
</a>
{/* 合作伙伴促销信息 */}
{isPartner && partnerPromotionKey && (
<div className="rounded-md bg-blue-50 dark:bg-blue-950/30 p-2.5 border border-blue-200 dark:border-blue-800">
<p className="text-xs leading-relaxed text-blue-700 dark:text-blue-300">
💡{" "}
{t(`providerForm.partnerPromotion.${partnerPromotionKey}`, {
defaultValue: "",
})}
</p>
</div>
)}
</div>
)}
</div>

View File

@@ -34,49 +34,78 @@ export function DirectorySettings({
const { t } = useTranslation();
return (
<section className="space-y-4">
<header className="space-y-1">
<h3 className="text-sm font-medium">
{t("settings.configDirectoryOverride")}
</h3>
<p className="text-xs text-muted-foreground">
{t("settings.configDirectoryDescription")}
</p>
</header>
<>
{/* CC Switch 配置目录 - 独立区块 */}
<section className="space-y-4">
<header className="space-y-1">
<h3 className="text-sm font-medium">{t("settings.appConfigDir")}</h3>
<p className="text-xs text-muted-foreground">
{t("settings.appConfigDirDescription")}
</p>
</header>
<DirectoryInput
label={t("settings.appConfigDir")}
description={t("settings.appConfigDirDescription")}
value={appConfigDir}
resolvedValue={resolvedDirs.appConfig}
placeholder={t("settings.browsePlaceholderApp")}
onChange={onAppConfigChange}
onBrowse={onBrowseAppConfig}
onReset={onResetAppConfig}
/>
<div className="flex items-center gap-2">
<Input
value={appConfigDir ?? resolvedDirs.appConfig ?? ""}
placeholder={t("settings.browsePlaceholderApp")}
className="font-mono text-xs"
onChange={(event) => onAppConfigChange(event.target.value)}
/>
<Button
type="button"
variant="outline"
size="icon"
onClick={onBrowseAppConfig}
title={t("settings.browseDirectory")}
>
<FolderSearch className="h-4 w-4" />
</Button>
<Button
type="button"
variant="outline"
size="icon"
onClick={onResetAppConfig}
title={t("settings.resetDefault")}
>
<Undo2 className="h-4 w-4" />
</Button>
</div>
</section>
<DirectoryInput
label={t("settings.claudeConfigDir")}
description={t("settings.claudeConfigDirDescription")}
value={claudeDir}
resolvedValue={resolvedDirs.claude}
placeholder={t("settings.browsePlaceholderClaude")}
onChange={(val) => onDirectoryChange("claude", val)}
onBrowse={() => onBrowseDirectory("claude")}
onReset={() => onResetDirectory("claude")}
/>
{/* Claude/Codex 配置目录 - 独立区块 */}
<section className="space-y-4">
<header className="space-y-1">
<h3 className="text-sm font-medium">
{t("settings.configDirectoryOverride")}
</h3>
<p className="text-xs text-muted-foreground">
{t("settings.configDirectoryDescription")}
</p>
</header>
<DirectoryInput
label={t("settings.codexConfigDir")}
description={t("settings.codexConfigDirDescription")}
value={codexDir}
resolvedValue={resolvedDirs.codex}
placeholder={t("settings.browsePlaceholderCodex")}
onChange={(val) => onDirectoryChange("codex", val)}
onBrowse={() => onBrowseDirectory("codex")}
onReset={() => onResetDirectory("codex")}
/>
</section>
<DirectoryInput
label={t("settings.claudeConfigDir")}
description={undefined}
value={claudeDir}
resolvedValue={resolvedDirs.claude}
placeholder={t("settings.browsePlaceholderClaude")}
onChange={(val) => onDirectoryChange("claude", val)}
onBrowse={() => onBrowseDirectory("claude")}
onReset={() => onResetDirectory("claude")}
/>
<DirectoryInput
label={t("settings.codexConfigDir")}
description={undefined}
value={codexDir}
resolvedValue={resolvedDirs.codex}
placeholder={t("settings.browsePlaceholderCodex")}
onChange={(val) => onDirectoryChange("codex", val)}
onBrowse={() => onBrowseDirectory("codex")}
onReset={() => onResetDirectory("codex")}
/>
</section>
</>
);
}

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

@@ -29,7 +29,11 @@ export interface ProviderPreset {
apiKeyUrl?: string;
settingsConfig: object;
isOfficial?: boolean; // 标识是否为官方预设
isPartner?: boolean; // 标识是否为商业合作伙伴
partnerPromotionKey?: string; // 合作伙伴促销信息的 i18n key
category?: ProviderCategory; // 新增:分类
// 新增:指定该预设所使用的 API Key 字段名(默认 ANTHROPIC_AUTH_TOKEN
apiKeyField?: "ANTHROPIC_AUTH_TOKEN" | "ANTHROPIC_API_KEY";
// 新增:模板变量定义,用于动态替换配置中的值
templateValues?: Record<string, TemplateValueConfig>; // editorValue 存储编辑器中的实时输入值
// 新增:请求地址候选列表(用于地址管理/测速)
@@ -61,7 +65,9 @@ export const providerPresets: ProviderPreset[] = [
ANTHROPIC_BASE_URL: "https://api.deepseek.com/anthropic",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "DeepSeek-V3.2-Exp",
ANTHROPIC_SMALL_FAST_MODEL: "DeepSeek-V3.2-Exp",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "DeepSeek-V3.2-Exp",
ANTHROPIC_DEFAULT_SONNET_MODEL: "DeepSeek-V3.2-Exp",
ANTHROPIC_DEFAULT_OPUS_MODEL: "DeepSeek-V3.2-Exp",
},
},
category: "cn_official",
@@ -69,19 +75,38 @@ export const providerPresets: ProviderPreset[] = [
{
name: "Zhipu GLM",
websiteUrl: "https://open.bigmodel.cn",
apiKeyUrl: "https://www.bigmodel.cn/claude-code?ic=RRVJPB5SII",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://open.bigmodel.cn/api/anthropic",
ANTHROPIC_AUTH_TOKEN: "",
// 兼容旧键名,保持前端读取一致
ANTHROPIC_MODEL: "GLM-4.6",
ANTHROPIC_SMALL_FAST_MODEL: "glm-4.5-air",
ANTHROPIC_MODEL: "glm-4.6",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "glm-4.5-air",
ANTHROPIC_DEFAULT_SONNET_MODEL: "glm-4.6",
ANTHROPIC_DEFAULT_OPUS_MODEL: "glm-4.6",
},
},
category: "cn_official",
isPartner: true, // 合作伙伴
partnerPromotionKey: "zhipu", // 促销信息 i18n key
},
{
name: "Z.ai GLM",
websiteUrl: "https://z.ai",
apiKeyUrl: "https://z.ai/subscribe?ic=8JVLJQFSKB",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://api.z.ai/api/anthropic",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "glm-4.6",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "glm-4.5-air",
ANTHROPIC_DEFAULT_SONNET_MODEL: "glm-4.6",
ANTHROPIC_DEFAULT_OPUS_MODEL: "glm-4.6",
},
},
category: "cn_official",
isPartner: true, // 合作伙伴
partnerPromotionKey: "zhipu", // 促销信息 i18n key
},
{
name: "Qwen Coder",
@@ -92,7 +117,9 @@ export const providerPresets: ProviderPreset[] = [
"https://dashscope.aliyuncs.com/api/v2/apps/claude-code-proxy",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "qwen3-max",
ANTHROPIC_SMALL_FAST_MODEL: "qwen3-max",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "qwen3-max",
ANTHROPIC_DEFAULT_SONNET_MODEL: "qwen3-max",
ANTHROPIC_DEFAULT_OPUS_MODEL: "qwen3-max",
},
},
category: "cn_official",
@@ -104,8 +131,10 @@ export const providerPresets: ProviderPreset[] = [
env: {
ANTHROPIC_BASE_URL: "https://api.moonshot.cn/anthropic",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "kimi-k2-turbo-preview",
ANTHROPIC_SMALL_FAST_MODEL: "kimi-k2-turbo-preview",
ANTHROPIC_MODEL: "kimi-k2-thinking",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "kimi-k2-thinking",
ANTHROPIC_DEFAULT_SONNET_MODEL: "kimi-k2-thinking",
ANTHROPIC_DEFAULT_OPUS_MODEL: "kimi-k2-thinking",
},
},
category: "cn_official",
@@ -118,22 +147,26 @@ export const providerPresets: ProviderPreset[] = [
ANTHROPIC_BASE_URL: "https://api-inference.modelscope.cn",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "ZhipuAI/GLM-4.6",
ANTHROPIC_SMALL_FAST_MODEL: "ZhipuAI/GLM-4.6",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "ZhipuAI/GLM-4.6",
ANTHROPIC_DEFAULT_SONNET_MODEL: "ZhipuAI/GLM-4.6",
ANTHROPIC_DEFAULT_OPUS_MODEL: "ZhipuAI/GLM-4.6",
},
},
category: "aggregator",
},
{
name: "KAT-Coder",
websiteUrl: "https://console.streamlake.ai/wanqing/",
apiKeyUrl: "https://console.streamlake.ai/console/wanqing/api-key",
websiteUrl: "https://console.streamlake.ai",
apiKeyUrl: "https://console.streamlake.ai/console/api-key",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL:
"https://vanchin.streamlake.ai/api/gateway/v1/endpoints/${ENDPOINT_ID}/claude-code-proxy",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "KAT-Coder",
ANTHROPIC_SMALL_FAST_MODEL: "KAT-Coder",
ANTHROPIC_MODEL: "KAT-Coder-Pro V1",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "KAT-Coder-Air V1",
ANTHROPIC_DEFAULT_SONNET_MODEL: "KAT-Coder-Pro V1",
ANTHROPIC_DEFAULT_OPUS_MODEL: "KAT-Coder-Pro V1",
},
},
category: "cn_official",
@@ -155,7 +188,7 @@ export const providerPresets: ProviderPreset[] = [
ANTHROPIC_BASE_URL: "https://api.longcat.chat/anthropic",
ANTHROPIC_AUTH_TOKEN: "",
ANTHROPIC_MODEL: "LongCat-Flash-Chat",
ANTHROPIC_SMALL_FAST_MODEL: "LongCat-Flash-Chat",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "LongCat-Flash-Chat",
ANTHROPIC_DEFAULT_SONNET_MODEL: "LongCat-Flash-Chat",
ANTHROPIC_DEFAULT_OPUS_MODEL: "LongCat-Flash-Chat",
CLAUDE_CODE_MAX_OUTPUT_TOKENS: "6000",
@@ -164,10 +197,58 @@ export const providerPresets: ProviderPreset[] = [
},
category: "cn_official",
},
{
name: "MiniMax",
websiteUrl: "https://platform.minimaxi.com",
apiKeyUrl: "https://platform.minimaxi.com/user-center/basic-information",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://api.minimaxi.com/anthropic",
ANTHROPIC_AUTH_TOKEN: "",
API_TIMEOUT_MS: "3000000",
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: 1,
ANTHROPIC_MODEL: "MiniMax-M2",
ANTHROPIC_DEFAULT_SONNET_MODEL: "MiniMax-M2",
ANTHROPIC_DEFAULT_OPUS_MODEL: "MiniMax-M2",
ANTHROPIC_DEFAULT_HAIKU_MODEL: "MiniMax-M2",
},
},
category: "cn_official",
},
{
name: "AiHubMix",
websiteUrl: "https://aihubmix.com",
apiKeyUrl: "https://aihubmix.com",
// 说明:该供应商使用 ANTHROPIC_API_KEY而非 ANTHROPIC_AUTH_TOKEN
apiKeyField: "ANTHROPIC_API_KEY",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://aihubmix.com",
ANTHROPIC_API_KEY: "",
},
},
// 请求地址候选(用于地址管理/测速),用户可自行选择/覆盖
endpointCandidates: ["https://aihubmix.com", "https://api.aihubmix.com"],
category: "aggregator",
},
{
name: "DMXAPI",
websiteUrl: "https://www.dmxapi.cn",
apiKeyUrl: "https://www.dmxapi.cn",
settingsConfig: {
env: {
ANTHROPIC_BASE_URL: "https://www.dmxapi.cn",
ANTHROPIC_API_KEY: "",
},
},
// 请求地址候选(用于地址管理/测速),用户可自行选择/覆盖
endpointCandidates: ["https://aihubmix.com", "https://api.aihubmix.com"],
category: "aggregator",
},
{
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",
@@ -180,6 +261,8 @@ export const providerPresets: ProviderPreset[] = [
"https://api-slb.packyapi.com",
],
category: "third_party",
isPartner: true, // 合作伙伴
partnerPromotionKey: "packycode", // 促销信息 i18n key
},
{
name: "AnyRouter",

View File

@@ -2,7 +2,7 @@
* Codex 预设供应商配置模板
*/
import { ProviderCategory } from "../types";
import type { PresetTheme } from "./providerPresets";
import type { PresetTheme } from "./claudeProviderPresets";
export interface CodexProviderPreset {
name: string;
@@ -12,6 +12,8 @@ export interface CodexProviderPreset {
auth: Record<string, any>; // 将写入 ~/.codex/auth.json
config: string; // 将写入 ~/.codex/config.tomlTOML 字符串)
isOfficial?: boolean; // 标识是否为官方预设
isPartner?: boolean; // 标识是否为商业合作伙伴
partnerPromotionKey?: string; // 合作伙伴促销信息的 i18n key
category?: ProviderCategory; // 新增:分类
isCustomTemplate?: boolean; // 标识是否为自定义模板
// 新增:请求地址候选列表(用于地址管理/测速)
@@ -58,7 +60,7 @@ requires_openai_auth = true`;
export const codexProviderPresets: CodexProviderPreset[] = [
{
name: "Codex Official",
name: "OpenAI Official",
websiteUrl: "https://chatgpt.com/codex",
isOfficial: true,
category: "official",
@@ -70,21 +72,76 @@ export const codexProviderPresets: CodexProviderPreset[] = [
textColor: "#FFFFFF",
},
},
{
name: "Azure OpenAI",
websiteUrl:
"https://learn.microsoft.com/azure/ai-services/openai/how-to/overview",
category: "third_party",
isOfficial: true,
auth: generateThirdPartyAuth(""),
config: `model_provider = "azure"
model = "gpt-5-codex"
model_reasoning_effort = "high"
disable_response_storage = true
[model_providers.azure]
name = "Azure OpenAI"
base_url = "https://YOUR_RESOURCE_NAME.openai.azure.com/openai"
env_key = "OPENAI_API_KEY"
query_params = { "api-version" = "2025-04-01-preview" }
wire_api = "responses"
requires_openai_auth = true`,
endpointCandidates: ["https://YOUR_RESOURCE_NAME.openai.azure.com/openai"],
theme: {
icon: "codex",
backgroundColor: "#0078D4",
textColor: "#FFFFFF",
},
},
{
name: "AiHubMix",
websiteUrl: "https://aihubmix.com",
category: "aggregator",
auth: generateThirdPartyAuth(""),
config: generateThirdPartyConfig(
"aihubmix",
"https://aihubmix.com/v1",
"gpt-5-codex",
),
endpointCandidates: [
"https://aihubmix.com/v1",
"https://api.aihubmix.com/v1",
],
},
{
name: "DMXAPI",
websiteUrl: "https://www.dmxapi.cn",
category: "aggregator",
auth: generateThirdPartyAuth(""),
config: generateThirdPartyConfig(
"dmxapi",
"https://www.dmxapi.cn/v1",
"gpt-5-codex",
),
endpointCandidates: ["https://www.dmxapi.cn/v1"],
},
{
name: "PackyCode",
websiteUrl: "https://codex.packycode.com/",
websiteUrl: "https://www.packyapi.com",
apiKeyUrl: "https://www.packyapi.com/register?aff=cc-switch",
category: "third_party",
auth: generateThirdPartyAuth(""),
config: generateThirdPartyConfig(
"packycode",
"https://codex-api.packycode.com/v1",
"https://www.packyapi.com/v1",
"gpt-5-codex",
),
// Codex 请求地址候选(用于地址管理/测速)
endpointCandidates: [
"https://codex-api.packycode.com/v1",
"https://codex-api-slb.packycode.com/v1",
"https://www.packyapi.com/v1",
"https://api-slb.packyapi.com/v1",
],
isPartner: true, // 合作伙伴
partnerPromotionKey: "packycode", // 促销信息 i18n key
},
{
name: "AnyRouter",
@@ -93,14 +150,13 @@ export const codexProviderPresets: CodexProviderPreset[] = [
auth: generateThirdPartyAuth(""),
config: generateThirdPartyConfig(
"anyrouter",
"https://anyrouter.top",
"https://anyrouter.top/v1",
"gpt-5-codex",
),
// Codex 请求地址候选(用于地址管理/测速)
endpointCandidates: [
"https://anyrouter.top",
"https://q.quuvv.cn",
"https://pmpjfbhq.cn-nb1.rainapp.top",
"https://anyrouter.top/v1",
"https://q.quuvv.cn/v1",
"https://pmpjfbhq.cn-nb1.rainapp.top/v1",
],
},
];

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

@@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import { settingsApi } from "@/lib/api";
import { syncCurrentProvidersLiveSafe } from "@/utils/postChangeSync";
export type ImportStatus =
| "idle"
@@ -105,8 +106,8 @@ export function useImportExport(
setBackupId(result.backupId ?? null);
try {
await settingsApi.syncCurrentProvidersLive();
const syncResult = await syncCurrentProvidersLiveSafe();
if (syncResult.ok) {
setStatus("success");
toast.success(
t("settings.importSuccess", {
@@ -117,8 +118,11 @@ export function useImportExport(
successTimerRef.current = window.setTimeout(() => {
void onImportSuccess?.();
}, 1500);
} catch (error) {
console.error("[useImportExport] Failed to sync live config", error);
} else {
console.error(
"[useImportExport] Failed to sync live config",
syncResult.error,
);
setStatus("partial-success");
toast.warning(
t("settings.importPartialSuccess", {

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

@@ -2,6 +2,7 @@ import { useCallback, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import { providersApi, settingsApi, type AppId } from "@/lib/api";
import { syncCurrentProvidersLiveSafe } from "@/utils/postChangeSync";
import { useSettingsQuery, useSaveSettingsMutation } from "@/lib/query";
import type { Settings } from "@/types";
import { useSettingsForm, type SettingsFormState } from "./useSettingsForm";
@@ -120,6 +121,8 @@ export function useSettings(): UseSettingsResult {
const sanitizedClaudeDir = sanitizeDir(settings.claudeConfigDir);
const sanitizedCodexDir = sanitizeDir(settings.codexConfigDir);
const previousAppDir = initialAppConfigDir;
const previousClaudeDir = sanitizeDir(data?.claudeConfigDir);
const previousCodexDir = sanitizeDir(data?.codexConfigDir);
const payload: Settings = {
...settings,
@@ -167,6 +170,19 @@ export function useSettings(): UseSettingsResult {
console.warn("[useSettings] Failed to refresh tray menu", error);
}
// 如果 Claude/Codex 的目录覆盖发生变化,则立即将“当前使用的供应商”写回对应应用的 live 配置
const claudeDirChanged = sanitizedClaudeDir !== previousClaudeDir;
const codexDirChanged = sanitizedCodexDir !== previousCodexDir;
if (claudeDirChanged || codexDirChanged) {
const syncResult = await syncCurrentProvidersLiveSafe();
if (!syncResult.ok) {
console.warn(
"[useSettings] Failed to sync current providers after directory change",
syncResult.error,
);
}
}
const appDirChanged = sanitizedAppDir !== (previousAppDir ?? undefined);
setRequiresRestart(appDirChanged);
@@ -177,6 +193,7 @@ export function useSettings(): UseSettingsResult {
}
}, [
appConfigDir,
data,
initialAppConfigDir,
saveMutation,
settings,

View File

@@ -22,7 +22,11 @@
"unknown": "Unknown",
"enterValidValue": "Please enter a valid value",
"clear": "Clear",
"toggleTheme": "Toggle theme"
"toggleTheme": "Toggle theme",
"format": "Format",
"formatSuccess": "Formatted successfully",
"formatError": "Format failed: {{error}}",
"copy": "Copy"
},
"apiKeyInput": {
"placeholder": "Enter API Key",
@@ -94,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}}",
@@ -126,7 +131,7 @@
"themeDark": "Dark",
"themeSystem": "System",
"importExport": "Import/Export Config",
"importExportHint": "Import or export cc-switch configuration for backup or migration.",
"importExportHint": "Import or export CC Switch configuration for backup or migration.",
"exportConfig": "Export Config to File",
"selectConfigFile": "Select Config File",
"noFileSelected": "No configuration file selected.",
@@ -152,9 +157,9 @@
"enableClaudePluginIntegration": "Apply to Claude Code extension",
"enableClaudePluginIntegrationDescription": "When enabled, the VS Code Claude Code extension provider will switch with this app",
"configDirectoryOverride": "Configuration Directory Override (Advanced)",
"configDirectoryDescription": "When using Claude Code or Codex in environments like WSL, you can manually specify the configuration directory in WSL to keep provider data consistent with the main environment.",
"appConfigDir": "CC-Switch Configuration Directory",
"appConfigDirDescription": "Customize the storage location for CC-Switch configuration files (config.json, etc.)",
"configDirectoryDescription": "When using Claude Code or Codex in environments like WSL, you can manually specify the configuration directory to the one in WSL to keep provider data consistent with the main environment.",
"appConfigDir": "CC Switch Configuration Directory",
"appConfigDirDescription": "Customize the storage location for CC Switch configuration (point to cloud sync folder to enable config sync)",
"browsePlaceholderApp": "e.g., C:\\Users\\Administrator\\.cc-switch",
"claudeConfigDir": "Claude Code Configuration Directory",
"claudeConfigDirDescription": "Override Claude configuration directory (settings.json) and keep claude.json (MCP) alongside it.",
@@ -181,7 +186,7 @@
"importFailedError": "Import config failed: {{message}}",
"exportFailedError": "Export config failed:",
"restartRequired": "Restart Required",
"restartRequiredMessage": "Modifying the CC-Switch configuration directory requires restarting the application to take effect. Restart now?",
"restartRequiredMessage": "Modifying the CC Switch configuration directory requires restarting the application to take effect. Restart now?",
"restartNow": "Restart Now",
"restartLater": "Restart Later",
"restartFailed": "Application restart failed, please manually close and reopen.",
@@ -223,18 +228,22 @@
"manageAndTest": "Manage & Test",
"configContent": "Config Content",
"useConfigWizard": "Use Configuration Wizard",
"openConfigWizard": "Open configuration wizard",
"manualConfig": "Manually configure provider, requires complete configuration, or",
"officialNoApiKey": "Official login does not require API Key, save directly",
"codexOfficialNoApiKey": "Official does not require API Key, save directly",
"codexApiKeyAutoFill": "Just fill in here, auth.json below will be auto-filled",
"kimiApiKeyHint": "Fill in to get model list",
"apiKeyAutoFill": "Just fill in here, config below will be auto-filled",
"cnOfficialApiKeyHint": "💡 Opensource official providers only need API Key, endpoint is preset",
"aggregatorApiKeyHint": "💡 Aggregator providers only need API Key to use",
"thirdPartyApiKeyHint": "💡 Third-party providers require both API Key and endpoint",
"cnOfficialApiKeyHint": "💡 Only need to fill in API Key, endpoint is preset",
"aggregatorApiKeyHint": "💡 Only need to fill in API Key, endpoint is preset",
"thirdPartyApiKeyHint": "💡 Only need to fill in API Key, endpoint is preset",
"customApiKeyHint": "💡 Custom configuration requires manually filling all necessary fields",
"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",
"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)",
"mainModelPlaceholder": "e.g., GLM-4.6",
@@ -255,8 +264,12 @@
"visitWebsite": "Visit {{url}}",
"anthropicModel": "Main Model",
"anthropicSmallFastModel": "Fast Model",
"modelPlaceholder": "GLM-4.6",
"smallModelPlaceholder": "GLM-4.5-Air",
"anthropicDefaultHaikuModel": "Default Haiku Model",
"anthropicDefaultSonnetModel": "Default Sonnet Model",
"anthropicDefaultOpusModel": "Default Opus Model",
"modelPlaceholder": "",
"smallModelPlaceholder": "",
"haikuModelPlaceholder": "",
"modelHelper": "Optional: Specify default Claude model to use, leave blank to use system default.",
"categoryOfficial": "Official",
"categoryCnOfficial": "Opensource Official",
@@ -331,16 +344,34 @@
"invalid": "Expired",
"total": "Total:",
"used": "Used:",
"remaining": "Remaining:"
"remaining": "Remaining:",
"justNow": "Just now",
"minutesAgo": "{{count}} min ago",
"hoursAgo": "{{count}} hr ago",
"daysAgo": "{{count}} day ago"
},
"usageScript": {
"title": "Configure Usage Query",
"enableUsageQuery": "Enable usage query",
"presetTemplate": "Preset template",
"templateCustom": "Custom",
"templateGeneral": "General",
"templateNewAPI": "NewAPI",
"credentialsConfig": "Credentials",
"accessToken": "Access Token",
"accessTokenPlaceholder": "Generate in 'Security Settings'",
"userId": "User ID",
"userIdPlaceholder": "e.g., 114514",
"defaultPlan": "Default Plan",
"queryFailedMessage": "Query failed",
"queryScript": "Query script (JavaScript)",
"timeoutSeconds": "Timeout (seconds)",
"autoQueryInterval": "Auto Query Interval (minutes)",
"autoQueryIntervalHint": "0 to disable, recommend 5-60 minutes",
"scriptHelp": "Script writing instructions:",
"configFormat": "Configuration format:",
"commentOptional": "optional",
"commentResponseIsJson": "response is the JSON data returned by the API",
"extractorFormat": "Extractor return format (all fields optional):",
"tips": "💡 Tips:",
"testing": "Testing...",
@@ -366,19 +397,10 @@
"tip2": "• Extractor function runs in sandbox environment, supports ES2020+ syntax",
"tip3": "• Entire config must be wrapped in () to form object literal expression"
},
"kimiSelector": {
"modelConfig": "Model Configuration",
"mainModel": "Main Model",
"fastModel": "Fast Model",
"refreshModels": "Refresh Model List",
"pleaseSelectModel": "Please select a model",
"noModels": "No models available",
"fillApiKeyFirst": "Please fill in API Key first",
"requestFailed": "Request failed: {{error}}",
"invalidData": "Invalid response data format",
"fetchModelsFailed": "Failed to fetch model list",
"apiKeyHint": "💡 Fill in API Key to automatically fetch available model list"
"errors": {
"usage_query_failed": "Usage query failed"
},
"presetSelector": {
"title": "Select Configuration Type",
"custom": "Custom",

View File

@@ -22,7 +22,11 @@
"unknown": "未知",
"enterValidValue": "请输入有效的内容",
"clear": "清除",
"toggleTheme": "切换主题"
"toggleTheme": "切换主题",
"format": "格式化",
"formatSuccess": "格式化成功",
"formatError": "格式化失败:{{error}}",
"copy": "复制"
},
"apiKeyInput": {
"placeholder": "请输入API Key",
@@ -94,7 +98,8 @@
"providerSaved": "供应商配置已保存",
"providerDeleted": "供应商删除成功",
"switchSuccess": "切换成功!请重启 {{appName}} 终端以生效",
"switchFailed": "切换失败,请检查配置",
"switchFailedTitle": "切换失败",
"switchFailed": "切换失败:{{error}}",
"autoImported": "已从现有配置创建默认供应商",
"addFailed": "添加供应商失败:{{error}}",
"saveFailed": "保存失败:{{error}}",
@@ -126,7 +131,7 @@
"themeDark": "深色",
"themeSystem": "跟随系统",
"importExport": "导入导出配置",
"importExportHint": "导入导出 cc-switch 配置,便于备份或迁移。",
"importExportHint": "导入导出 CC Switch 配置,便于备份或迁移。",
"exportConfig": "导出配置到文件",
"selectConfigFile": "选择配置文件",
"noFileSelected": "尚未选择配置文件。",
@@ -152,9 +157,9 @@
"enableClaudePluginIntegration": "应用到 Claude Code 插件",
"enableClaudePluginIntegrationDescription": "开启后 Vscode Claude Code 插件的供应商将随本软件切换",
"configDirectoryOverride": "配置目录覆盖(高级)",
"configDirectoryDescription": "在 WSL 等环境使用 Claude Code 或 Codex 的时候,可手动指定 WSL 里的配置目录,供应商数据与主环境保持一致。",
"appConfigDir": "CC-Switch 配置目录",
"appConfigDirDescription": "自定义 CC-Switch 的配置存储位置(config.json 等文件",
"configDirectoryDescription": "在 WSL 等环境使用 Claude Code 或 Codex 的时候,可手动指定 WSL 里的配置目录,供应商数据与主环境保持一致。",
"appConfigDir": "CC Switch 配置目录",
"appConfigDirDescription": "自定义 CC Switch 的配置存储位置(指定到云同步文件夹即可云同步配置",
"browsePlaceholderApp": "例如C:\\Users\\Administrator\\.cc-switch",
"claudeConfigDir": "Claude Code 配置目录",
"claudeConfigDirDescription": "覆盖 Claude 配置目录 (settings.json),同时会在同级存放 Claude MCP 的 claude.json。",
@@ -181,7 +186,7 @@
"importFailedError": "导入配置失败:{{message}}",
"exportFailedError": "导出配置失败:",
"restartRequired": "需要重启应用",
"restartRequiredMessage": "修改 CC-Switch 配置目录后需要重启应用才能生效,是否立即重启?",
"restartRequiredMessage": "修改 CC Switch 配置目录后需要重启应用才能生效,是否立即重启?",
"restartNow": "立即重启",
"restartLater": "稍后重启",
"restartFailed": "应用重启失败,请手动关闭后重新打开。",
@@ -223,18 +228,22 @@
"manageAndTest": "管理与测速",
"configContent": "配置内容",
"useConfigWizard": "使用配置向导",
"openConfigWizard": "打开配置向导",
"manualConfig": "手动配置供应商,需要填写完整的配置信息,或者",
"officialNoApiKey": "官方登录无需填写 API Key直接保存即可",
"codexOfficialNoApiKey": "官方无需填写 API Key直接保存即可",
"codexApiKeyAutoFill": "只需要填这里,下方 auth.json 会自动填充",
"kimiApiKeyHint": "填写后可获取模型列表",
"apiKeyAutoFill": "只需要填这里,下方配置会自动填充",
"cnOfficialApiKeyHint": "💡 开源官方供应商只需填写 API Key请求地址已预设",
"aggregatorApiKeyHint": "💡 聚合服务供应商只需填写 API Key 即可使用",
"thirdPartyApiKeyHint": "💡 第三方供应商需要填写 API Key请求地址",
"cnOfficialApiKeyHint": "💡 只需填写 API Key请求地址已预设",
"aggregatorApiKeyHint": "💡 只需填写 API Key,请求地址已预设",
"thirdPartyApiKeyHint": "💡 只需填写 API Key请求地址已预设",
"customApiKeyHint": "💡 自定义配置需手动填写所有必要字段",
"officialHint": "💡 官方供应商使用浏览器登录,无需配置 API Key",
"getApiKey": "获取 API Key",
"partnerPromotion": {
"zhipu": "智谱 GLM 是 CC Switch 的官方合作伙伴使用此链接充值可以获得9折优惠",
"packycode": "PackyCode 是 CC Switch 的官方合作伙伴,使用此链接注册并在充值时填写 \"cc-switch\" 优惠码可以享受9折优惠"
},
"parameterConfig": "参数配置 - {{name}} *",
"mainModel": "主模型 (可选)",
"mainModelPlaceholder": "例如: GLM-4.6",
@@ -255,8 +264,12 @@
"visitWebsite": "访问 {{url}}",
"anthropicModel": "主模型",
"anthropicSmallFastModel": "快速模型",
"modelPlaceholder": "GLM-4.6",
"smallModelPlaceholder": "GLM-4.5-Air",
"anthropicDefaultHaikuModel": "Haiku 默认模型",
"anthropicDefaultSonnetModel": "Sonnet 默认模型",
"anthropicDefaultOpusModel": "Opus 默认模型",
"modelPlaceholder": "",
"smallModelPlaceholder": "",
"haikuModelPlaceholder": "",
"modelHelper": "可选:指定默认使用的 Claude 模型,留空则使用系统默认。",
"categoryOfficial": "官方",
"categoryCnOfficial": "开源官方",
@@ -330,17 +343,35 @@
"planUsage": "套餐用量",
"invalid": "已失效",
"total": "总:",
"used": "使用:",
"remaining": "剩余:"
"used": "使用:",
"remaining": "剩余:",
"justNow": "刚刚",
"minutesAgo": "{{count}} 分钟前",
"hoursAgo": "{{count}} 小时前",
"daysAgo": "{{count}} 天前"
},
"usageScript": {
"title": "配置用量查询",
"enableUsageQuery": "启用用量查询",
"presetTemplate": "预设模板",
"templateCustom": "自定义",
"templateGeneral": "通用模板",
"templateNewAPI": "NewAPI",
"credentialsConfig": "凭证配置",
"accessToken": "访问令牌",
"accessTokenPlaceholder": "在'安全设置'里生成",
"userId": "用户 ID",
"userIdPlaceholder": "例如114514",
"defaultPlan": "默认套餐",
"queryFailedMessage": "查询失败",
"queryScript": "查询脚本JavaScript",
"timeoutSeconds": "超时时间(秒)",
"autoQueryInterval": "自动查询间隔(分钟)",
"autoQueryIntervalHint": "0 表示不自动查询,建议设置 5-60 分钟",
"scriptHelp": "脚本编写说明:",
"configFormat": "配置格式:",
"commentOptional": "可选",
"commentResponseIsJson": "response 是 API 返回的 JSON 数据",
"extractorFormat": "extractor 返回格式(所有字段均为可选):",
"tips": "💡 提示:",
"testing": "测试中...",
@@ -366,19 +397,10 @@
"tip2": "• extractor 函数在沙箱环境中执行,支持 ES2020+ 语法",
"tip3": "• 整个配置必须用 () 包裹,形成对象字面量表达式"
},
"kimiSelector": {
"modelConfig": "模型配置",
"mainModel": "主模型",
"fastModel": "快速模型",
"refreshModels": "刷新模型列表",
"pleaseSelectModel": "请选择模型",
"noModels": "暂无模型",
"fillApiKeyFirst": "请先填写 API Key",
"requestFailed": "请求失败: {{error}}",
"invalidData": "返回数据格式错误",
"fetchModelsFailed": "获取模型列表失败",
"apiKeyHint": "💡 填写 API Key 后将自动获取可用模型列表"
"errors": {
"usage_query_failed": "用量查询失败"
},
"presetSelector": {
"title": "选择配置类型",
"custom": "自定义",

View File

@@ -39,7 +39,7 @@ export const settingsApi = {
},
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
return await invoke("pick_directory", { default_path: defaultPath });
return await invoke("pick_directory", { defaultPath });
},
async getClaudeCodeConfigPath(): Promise<string> {
@@ -70,10 +70,7 @@ export const settingsApi = {
},
async saveFileDialog(defaultName: string): Promise<string | null> {
return await invoke("save_file_dialog", {
default_name: defaultName,
defaultName,
});
return await invoke("save_file_dialog", { defaultName });
},
async openFileDialog(): Promise<string | null> {
@@ -81,17 +78,11 @@ export const settingsApi = {
},
async exportConfigToFile(filePath: string): Promise<ConfigTransferResult> {
return await invoke("export_config_to_file", {
file_path: filePath,
filePath,
});
return await invoke("export_config_to_file", { filePath });
},
async importConfigFromFile(filePath: string): Promise<ConfigTransferResult> {
return await invoke("import_config_from_file", {
file_path: filePath,
filePath,
});
return await invoke("import_config_from_file", { filePath });
},
async syncCurrentProvidersLive(): Promise<void> {

View File

@@ -1,12 +1,65 @@
import { invoke } from "@tauri-apps/api/core";
import type { UsageResult } from "@/types";
import type { AppId } from "./types";
import i18n from "@/i18n";
export const usageApi = {
async query(providerId: string, appId: AppId): Promise<UsageResult> {
return await invoke("query_provider_usage", {
provider_id: providerId,
app: appId,
});
try {
return await invoke("queryProviderUsage", {
providerId: providerId,
app: appId,
});
} catch (error: unknown) {
// 提取错误消息:优先使用后端返回的错误信息
const message =
typeof error === "string"
? error
: error instanceof Error
? error.message
: "";
// 如果没有错误消息,使用国际化的默认提示
return {
success: false,
error: message || i18n.t("errors.usage_query_failed"),
};
}
},
async testScript(
providerId: string,
appId: AppId,
scriptCode: string,
timeout?: number,
apiKey?: string,
baseUrl?: string,
accessToken?: string,
userId?: string,
): Promise<UsageResult> {
try {
return await invoke("testUsageScript", {
providerId: providerId,
app: appId,
scriptCode: scriptCode,
timeout: timeout,
apiKey: apiKey,
baseUrl: baseUrl,
accessToken: accessToken,
userId: userId,
});
} catch (error: unknown) {
const message =
typeof error === "string"
? error
: error instanceof Error
? error.message
: "";
return {
success: false,
error: message || i18n.t("errors.usage_query_failed"),
};
}
},
};

View File

@@ -20,7 +20,7 @@ export const vscodeApi = {
): Promise<EndpointLatencyResult[]> {
return await invoke("test_api_endpoints", {
urls,
timeout_secs: options?.timeoutSecs,
timeoutSecs: options?.timeoutSecs,
});
},
@@ -30,7 +30,7 @@ export const vscodeApi = {
): Promise<CustomEndpoint[]> {
return await invoke("get_custom_endpoints", {
app: appId,
provider_id: providerId,
providerId: providerId,
});
},
@@ -41,7 +41,7 @@ export const vscodeApi = {
): Promise<void> {
await invoke("add_custom_endpoint", {
app: appId,
provider_id: providerId,
providerId: providerId,
url,
});
},
@@ -53,7 +53,7 @@ export const vscodeApi = {
): Promise<void> {
await invoke("remove_custom_endpoint", {
app: appId,
provider_id: providerId,
providerId: providerId,
url,
});
},
@@ -65,28 +65,25 @@ export const vscodeApi = {
): Promise<void> {
await invoke("update_endpoint_last_used", {
app: appId,
provider_id: providerId,
providerId: providerId,
url,
});
},
async exportConfigToFile(filePath: string) {
return await invoke("export_config_to_file", {
file_path: filePath,
filePath,
});
},
async importConfigFromFile(filePath: string) {
return await invoke("import_config_from_file", {
file_path: filePath,
filePath,
});
},
async saveFileDialog(defaultName: string): Promise<string | null> {
return await invoke("save_file_dialog", {
default_name: defaultName,
defaultName,
});
},

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

@@ -83,16 +83,34 @@ export const useSettingsQuery = (): UseQueryResult<Settings> => {
});
};
export interface UseUsageQueryOptions {
enabled?: boolean;
autoQueryInterval?: number; // 自动查询间隔分钟0 表示禁用
}
export const useUsageQuery = (
providerId: string,
appId: AppId,
enabled: boolean = true,
): UseQueryResult<UsageResult> => {
return useQuery({
options?: UseUsageQueryOptions,
) => {
const { enabled = true, autoQueryInterval = 0 } = options || {};
const query = useQuery<UsageResult>({
queryKey: ["usage", providerId, appId],
queryFn: async () => usageApi.query(providerId, appId),
enabled: enabled && !!providerId,
refetchInterval:
autoQueryInterval > 0
? Math.max(autoQueryInterval, 1) * 60 * 1000 // 最小1分钟
: false,
refetchIntervalInBackground: true, // 后台也继续定时查询
refetchOnWindowFocus: false,
staleTime: 5 * 60 * 1000, // 5分钟
retry: false,
staleTime: 0, // 不使用缓存策略,确保 refetchInterval 准确执行
});
return {
...query,
lastQueriedAt: query.dataUpdatedAt || null,
};
};

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

@@ -9,6 +9,10 @@ import { QueryClientProvider } from "@tanstack/react-query";
import { ThemeProvider } from "@/components/theme-provider";
import { queryClient } from "@/lib/query";
import { Toaster } from "@/components/ui/sonner";
import { listen } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/core";
import { message } from "@tauri-apps/plugin-dialog";
import { exit } from "@tauri-apps/plugin-process";
// 根据平台添加 body class便于平台特定样式
try {
@@ -22,15 +26,68 @@ try {
// 忽略平台检测失败
}
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<ThemeProvider defaultTheme="system" storageKey="cc-switch-theme">
<UpdateProvider>
<App />
<Toaster />
</UpdateProvider>
</ThemeProvider>
</QueryClientProvider>
</React.StrictMode>,
);
// 配置加载错误payload类型
interface ConfigLoadErrorPayload {
path?: string;
error?: string;
}
/**
* 处理配置加载失败:显示错误消息并强制退出应用
* 不给用户"取消"选项,因为配置损坏时应用无法正常运行
*/
async function handleConfigLoadError(
payload: ConfigLoadErrorPayload | null,
): Promise<void> {
const path = payload?.path ?? "~/.cc-switch/config.json";
const detail = payload?.error ?? "Unknown error";
await message(
`无法读取配置文件:\n${path}\n\n错误详情\n${detail}\n\n请手动检查 JSON 是否有效,或从同目录的备份文件(如 config.json.bak恢复。\n\n应用将退出以便您进行修复。`,
{ title: "配置加载失败", kind: "error" },
);
await exit(1);
}
// 监听后端的配置加载错误事件:仅提醒用户并强制退出,不修改任何配置文件
try {
void listen("configLoadError", async (evt) => {
await handleConfigLoadError(evt.payload as ConfigLoadErrorPayload | null);
});
} catch (e) {
// 忽略事件订阅异常(例如在非 Tauri 环境下)
console.error("订阅 configLoadError 事件失败", e);
}
async function bootstrap() {
// 启动早期主动查询后端初始化错误,避免事件竞态
try {
const initError = (await invoke(
"get_init_error",
)) as ConfigLoadErrorPayload | null;
if (initError && (initError.path || initError.error)) {
await handleConfigLoadError(initError);
// 注意:不会执行到这里,因为 exit(1) 会终止进程
return;
}
} catch (e) {
// 忽略拉取错误,继续渲染
console.error("拉取初始化错误失败", e);
}
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<ThemeProvider defaultTheme="system" storageKey="cc-switch-theme">
<UpdateProvider>
<App />
<Toaster />
</UpdateProvider>
</ThemeProvider>
</QueryClientProvider>
</React.StrictMode>,
);
}
void bootstrap();

View File

@@ -14,6 +14,8 @@ export interface Provider {
category?: ProviderCategory;
createdAt?: number; // 添加时间戳(毫秒)
sortIndex?: number; // 排序索引(用于自定义拖拽排序)
// 新增:是否为商业合作伙伴
isPartner?: boolean;
// 可选:供应商元数据(仅存于 ~/.cc-switch/config.json不写入 live 配置)
meta?: ProviderMeta;
}
@@ -43,6 +45,11 @@ export interface UsageScript {
language: "javascript"; // 脚本语言
code: string; // 脚本代码JSON 格式配置)
timeout?: number; // 超时时间(秒,默认 10
apiKey?: string; // 用量查询专用的 API Key通用模板使用
baseUrl?: string; // 用量查询专用的 Base URL通用和 NewAPI 模板使用)
accessToken?: string; // 访问令牌NewAPI 模板使用)
userId?: string; // 用户IDNewAPI 模板使用)
autoQueryInterval?: number; // 自动查询间隔单位分钟0 表示禁用)
}
// 单个套餐用量数据

28
src/utils/formatters.ts Normal file
View File

@@ -0,0 +1,28 @@
/**
* 格式化 JSON 字符串
* @param value - 原始 JSON 字符串
* @returns 格式化后的 JSON 字符串2 空格缩进)
* @throws 如果 JSON 格式无效
*/
export function formatJSON(value: string): string {
const trimmed = value.trim();
if (!trimmed) {
return "";
}
const parsed = JSON.parse(trimmed);
return JSON.stringify(parsed, null, 2);
}
/**
* TOML 格式化功能已禁用
*
* 原因smol-toml 的 parse/stringify 会丢失所有注释和原有排版。
* 由于 TOML 常用于配置文件,注释是重要的文档说明,丢失注释会造成严重的用户体验问题。
*
* 未来可选方案:
* - 使用 @ltd/j-toml支持注释保留但需额外依赖和复杂的 API
* - 实现仅格式化缩进/空白的轻量级方案
* - 使用 toml-eslint-parser + 自定义生成器
*
* 暂时建议:依赖现有的 TOML 语法校验useCodexTomlValidation不提供格式化功能。
*/

View File

@@ -0,0 +1,18 @@
import { settingsApi } from "@/lib/api";
/**
* 统一的“后置同步”工具:将当前使用的供应商写回对应应用的 live 配置。
* 不抛出异常,由调用方根据返回值决定提示策略。
*/
export async function syncCurrentProvidersLiveSafe(): Promise<{
ok: boolean;
error?: Error;
}> {
try {
await settingsApi.syncCurrentProvidersLive();
return { ok: true };
} catch (err) {
const error = err instanceof Error ? err : new Error(String(err ?? ""));
return { ok: false, error };
}
}

View File

@@ -1,6 +1,7 @@
// 供应商配置处理工具函数
import type { TemplateValueConfig } from "../config/providerPresets";
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]";
@@ -164,12 +165,19 @@ export const hasCommonConfigSnippet = (
}
};
// 读取配置中的 API Keyenv.ANTHROPIC_AUTH_TOKEN
// 读取配置中的 API Key优先 ANTHROPIC_AUTH_TOKEN,其次 ANTHROPIC_API_KEY
export const getApiKeyFromConfig = (jsonString: string): string => {
try {
const config = JSON.parse(jsonString);
const key = config?.env?.ANTHROPIC_AUTH_TOKEN;
return typeof key === "string" ? key : "";
const token = config?.env?.ANTHROPIC_AUTH_TOKEN;
const apiKey = config?.env?.ANTHROPIC_API_KEY;
const value =
typeof token === "string"
? token
: typeof apiKey === "string"
? apiKey
: "";
return value;
} catch (err) {
return "";
}
@@ -224,9 +232,10 @@ export const applyTemplateValues = (
export const hasApiKeyField = (jsonString: string): boolean => {
try {
const config = JSON.parse(jsonString);
return Object.prototype.hasOwnProperty.call(
config?.env ?? {},
"ANTHROPIC_AUTH_TOKEN",
const env = config?.env ?? {};
return (
Object.prototype.hasOwnProperty.call(env, "ANTHROPIC_AUTH_TOKEN") ||
Object.prototype.hasOwnProperty.call(env, "ANTHROPIC_API_KEY")
);
} catch (err) {
return false;
@@ -246,10 +255,17 @@ export const setApiKeyInConfig = (
if (!createIfMissing) return jsonString;
config.env = {};
}
if (!("ANTHROPIC_AUTH_TOKEN" in config.env) && !createIfMissing) {
const env = config.env as Record<string, any>;
// 优先写入已存在的字段;若两者均不存在且允许创建,则默认创建 AUTH_TOKEN 字段
if ("ANTHROPIC_AUTH_TOKEN" in env) {
env.ANTHROPIC_AUTH_TOKEN = apiKey;
} else if ("ANTHROPIC_API_KEY" in env) {
env.ANTHROPIC_API_KEY = apiKey;
} else if (createIfMissing) {
env.ANTHROPIC_AUTH_TOKEN = apiKey;
} else {
return jsonString;
}
config.env.ANTHROPIC_AUTH_TOKEN = apiKey;
return JSON.stringify(config, null, 2);
} catch (err) {
return jsonString;
@@ -342,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;
@@ -375,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

@@ -2,9 +2,10 @@ import type { CustomEndpoint, ProviderMeta } from "@/types";
/**
* 合并供应商元数据中的自定义端点。
* - 当 customEndpoints 为空对象或 null 时,移除自定义端点但保留其它元数据。
* - 当 customEndpoints 为空对象时,明确删除自定义端点但保留其它元数据。
* - 当 customEndpoints 为 null/undefined 时,不修改端点(保留原有端点)。
* - 当 customEndpoints 存在时,覆盖原有自定义端点。
* - 若结果为空对象则返回 undefined避免写入空 meta。
* - 若结果为空对象且非明确清空场景则返回 undefined避免写入空 meta。
*/
export function mergeProviderMeta(
initialMeta: ProviderMeta | undefined,
@@ -13,6 +14,12 @@ export function mergeProviderMeta(
const hasCustomEndpoints =
!!customEndpoints && Object.keys(customEndpoints).length > 0;
// 明确清空:传入空对象(非 null/undefined表示用户想要删除所有端点
const isExplicitClear =
customEndpoints !== null &&
customEndpoints !== undefined &&
Object.keys(customEndpoints).length === 0;
if (hasCustomEndpoints) {
return {
...(initialMeta ? { ...initialMeta } : {}),
@@ -20,6 +27,25 @@ export function mergeProviderMeta(
};
}
// 明确清空端点
if (isExplicitClear) {
if (!initialMeta) {
// 新供应商且用户没有添加端点(理论上不会到这里)
return undefined;
}
if ("custom_endpoints" in initialMeta) {
const { custom_endpoints, ...rest } = initialMeta;
// 保留其他字段(如 usage_script
// 即使 rest 为空,也要返回空对象(让后端知道要清空 meta
return Object.keys(rest).length > 0 ? rest : {};
}
// initialMeta 中本来就没有 custom_endpoints
return { ...initialMeta };
}
// null/undefined用户没有修改端点保持不变
if (!initialMeta) {
return undefined;
}

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") {

View File

@@ -19,6 +19,8 @@ vi.mock("react-i18next", () => ({
t: (key: string, params?: Record<string, unknown>) =>
params ? `${key}:${JSON.stringify(params)}` : key,
}),
// 提供 initReactI18next 以兼容 i18n 初始化路径
initReactI18next: { type: "3rdParty", init: () => {} },
}));
vi.mock("@/config/mcpPresets", () => ({

View File

@@ -7,6 +7,7 @@ const mutateAsyncMock = vi.fn();
const useSettingsQueryMock = vi.fn();
const setAppConfigDirOverrideMock = vi.fn();
const applyClaudePluginConfigMock = vi.fn();
const syncCurrentProvidersLiveMock = vi.fn();
const toastErrorMock = vi.fn();
const toastSuccessMock = vi.fn();
@@ -48,6 +49,8 @@ vi.mock("@/lib/api", () => ({
setAppConfigDirOverrideMock(...args),
applyClaudePluginConfig: (...args: unknown[]) =>
applyClaudePluginConfigMock(...args),
syncCurrentProvidersLive: (...args: unknown[]) =>
syncCurrentProvidersLiveMock(...args),
},
}));
@@ -102,6 +105,7 @@ describe("useSettings hook", () => {
useSettingsQueryMock.mockReset();
setAppConfigDirOverrideMock.mockReset();
applyClaudePluginConfigMock.mockReset();
syncCurrentProvidersLiveMock.mockReset();
toastErrorMock.mockReset();
toastSuccessMock.mockReset();
window.localStorage.clear();
@@ -181,6 +185,8 @@ describe("useSettings hook", () => {
expect(metadataMock.setRequiresRestart).toHaveBeenCalledWith(true);
expect(window.localStorage.getItem("language")).toBe("en");
expect(toastErrorMock).not.toHaveBeenCalled();
// 目录有变化,应触发一次同步当前供应商到 live
expect(syncCurrentProvidersLiveMock).toHaveBeenCalledTimes(1);
});
it("saves settings without restart when directory unchanged", async () => {
@@ -209,6 +215,8 @@ describe("useSettings hook", () => {
expect(setAppConfigDirOverrideMock).toHaveBeenCalledWith(null);
expect(applyClaudePluginConfigMock).toHaveBeenCalledWith({ official: true });
expect(metadataMock.setRequiresRestart).toHaveBeenCalledWith(false);
// 目录未变化,不应触发同步
expect(syncCurrentProvidersLiveMock).not.toHaveBeenCalled();
});
it("shows toast when Claude plugin sync fails but continues flow", async () => {

View File

@@ -169,13 +169,21 @@ export const handlers = [
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
http.post(`${TAURI_ENDPOINT}/select_config_directory`, async ({ request }) => {
const { default_path } = await withJson<{ default_path?: string }>(request);
return success(default_path ? `${default_path}/picked` : "/mock/selected-dir");
const { defaultPath, default_path } = await withJson<{
defaultPath?: string;
default_path?: string;
}>(request);
const initial = defaultPath ?? default_path;
return success(initial ? `${initial}/picked` : "/mock/selected-dir");
}),
http.post(`${TAURI_ENDPOINT}/pick_directory`, async ({ request }) => {
const { default_path } = await withJson<{ default_path?: string }>(request);
return success(default_path ? `${default_path}/picked` : "/mock/selected-dir");
const { defaultPath, default_path } = await withJson<{
defaultPath?: string;
default_path?: string;
}>(request);
const initial = defaultPath ?? default_path;
return success(initial ? `${initial}/picked` : "/mock/selected-dir");
}),
http.post(`${TAURI_ENDPOINT}/open_file_dialog`, () =>