2025-08-30 21:54:11 +08:00
|
|
|
|
mod app_config;
|
2025-10-15 09:15:53 +08:00
|
|
|
|
mod app_store;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
mod claude_mcp;
|
2025-10-12 16:21:32 +08:00
|
|
|
|
mod claude_plugin;
|
2025-08-30 21:54:11 +08:00
|
|
|
|
mod codex_config;
|
2025-08-27 11:00:53 +08:00
|
|
|
|
mod commands;
|
2025-08-23 20:15:10 +08:00
|
|
|
|
mod config;
|
refactor(backend): phase 1 - unified error handling with thiserror
Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.
## Changes
### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility
### Module Migrations (60% complete)
- config.rs: Full migration to AppError
- read_json_file, write_json_file, atomic_write
- archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
- get_mcp_status, read_mcp_json, upsert_mcp_server
- delete_mcp_server, validate_command_in_path
- set_mcp_servers_map
- codex_config.rs: Full migration to AppError
- write_codex_live_atomic with rollback support
- read_and_validate_codex_config_text
- validate_config_toml
- app_config.rs: Partial migration
- MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
- AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
- Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
- sync_enabled_to_claude uses Into::into conversion
### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration
## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)
## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules
Co-authored-by: Claude <claude@anthropic.com>
2025-10-27 16:29:11 +08:00
|
|
|
|
mod error;
|
2025-10-05 23:33:07 +08:00
|
|
|
|
mod import_export;
|
2025-10-12 16:21:32 +08:00
|
|
|
|
mod mcp;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
mod migration;
|
2025-08-23 20:15:10 +08:00
|
|
|
|
mod provider;
|
2025-10-28 11:58:57 +08:00
|
|
|
|
mod services;
|
2025-09-20 21:20:07 +08:00
|
|
|
|
mod settings;
|
feat: Implement Speed Test Function
* feat: add unified endpoint speed test for API providers
Add a comprehensive endpoint latency testing system that allows users to:
- Test multiple API endpoints concurrently
- Auto-select the fastest endpoint based on latency
- Add/remove custom endpoints dynamically
- View latency results with color-coded indicators
Backend (Rust):
- Implement parallel HTTP HEAD requests with configurable timeout
- Handle various error scenarios (timeout, connection failure, invalid URL)
- Return structured latency data with status codes
Frontend (React):
- Create interactive speed test UI component with auto-sort by latency
- Support endpoint management (add/remove custom endpoints)
- Extract and update Codex base_url from TOML configuration
- Integrate with provider presets for default endpoint candidates
This feature improves user experience when selecting optimal API endpoints,
especially useful for users with multiple provider options or proxy setups.
* refactor: convert endpoint speed test to modal dialog
- Transform EndpointSpeedTest component into a modal dialog
- Add "Advanced" button next to base URL input to open modal
- Support ESC key and backdrop click to close modal
- Apply Linear design principles: minimal styling, clean layout
- Remove unused showBaseUrlInput variable
- Implement same modal pattern for both Claude and Codex
* fix: prevent modal cascade closing when ESC is pressed
- Add state checks to prevent parent modal from closing when child modals (endpoint speed test or template wizard) are open
- Update ESC key handler dependencies to track all modal states
- Ensures only the topmost modal responds to ESC key
* refactor: unify speed test panel UI with project design system
UI improvements:
- Update modal border radius from rounded-lg to rounded-xl
- Unify header padding from px-6 py-4 to p-6
- Change speed test button color to blue theme (bg-blue-500) for consistency
- Update footer background from bg-gray-50 to bg-gray-100
- Style "Done" button as primary action button with blue theme
- Adjust footer button spacing and hover states
Simplify endpoint display:
- Remove endpoint labels (e.g., "Current Address", "Custom 1")
- Display only URL for cleaner interface
- Clean up all label-related logic:
* Remove label field from EndpointCandidate interface
* Remove label generation in buildInitialEntries function
* Remove label handling in useEffect merge logic
* Remove label generation in handleAddEndpoint
* Remove label parameters from claudeSpeedTestEndpoints
* Remove label parameters from codexSpeedTestEndpoints
* refactor: improve endpoint list UI consistency
- Show delete button for all endpoints on hover for uniform UI
- Change selected state to use blue theme matching main interface:
* Blue border (border-blue-500) for selected items
* Light blue background (bg-blue-50/dark:bg-blue-900/20)
* Blue indicator dot (bg-blue-500/dark:bg-blue-400)
- Switch from compact list (space-y-px) to card-based layout (space-y-2)
- Add rounded corners to each endpoint item for better visual separation
* feat: persist custom endpoints to settings.json
- Extend AppSettings to store custom endpoints for Claude and Codex
- Add Tauri commands: get/add/remove/update custom endpoints
- Update frontend API with endpoint persistence methods
- Modify EndpointSpeedTest to load/save custom endpoints via API
- Track endpoint last used time for future sorting/cleanup
- Store endpoints per app type in settings.json instead of localStorage
* - feat(types): add Provider.meta and ProviderMeta (snake_case) with custom_endpoints map
- feat(provider-form): persist custom endpoints on provider create by merging EndpointSpeedTest’s custom URLs into meta.custom_endpoints on submit
- feat(endpoint-speed-test): add onCustomEndpointsChange callback emitting normalized custom URLs; wire it for both Claude/Codex modals
- fix(api): send alias param names (app/appType/app_type and provider_id/providerId) in Tauri invokes to avoid “missing providerId” with older backends
- storage: custom endpoints are stored in ~/.cc-switch/config.json under providers[<id>].meta.custom_endpoints (not in settings.json)
- behavior: edit flow remains immediate writes; create flow now writes once via addProvider, removing the providerId dependency during creation
* feat: add endpoint candidates support and code formatting improvements
- Add endpointCandidates field to ProviderPreset and CodexProviderPreset interfaces
- Integrate preset endpoint candidates into speed test endpoint selection
- Add multiple endpoint options for PackyCode providers (Claude & Codex)
- Apply consistent code formatting (trailing commas, line breaks)
- Improve template value type safety and readability
* refactor: improve endpoint management button UX
Replace ambiguous "Advanced" text with intuitive "Manage & Test" label accompanied by Zap icon, making the endpoint management panel entry point more discoverable and self-explanatory for both Claude and Codex configurations.
* - merge: merge origin/main, resolve conflicts and preserve both feature sets
- feat(tauri): register import/export and file dialogs; keep endpoint speed test and custom endpoints
- feat(api): add updateTrayMenu and onProviderSwitched; wire import/export APIs
- feat(types): extend global API declarations (import/export)
- chore(presets): GLM preset supports both new and legacy model keys
- chore(rust): add chrono dependency; refresh lockfile
---------
Co-authored-by: Jason <farion1231@gmail.com>
2025-10-07 19:14:32 +08:00
|
|
|
|
mod speedtest;
|
2025-10-12 16:21:32 +08:00
|
|
|
|
mod store;
|
2025-10-27 13:20:59 +08:00
|
|
|
|
mod usage_script;
|
2025-08-23 20:15:10 +08:00
|
|
|
|
|
2025-10-27 22:30:57 +08:00
|
|
|
|
pub use app_config::{AppType, MultiAppConfig};
|
2025-10-28 09:55:10 +08:00
|
|
|
|
pub use codex_config::{get_codex_auth_path, get_codex_config_path, write_codex_live_atomic};
|
2025-10-28 11:58:57 +08:00
|
|
|
|
pub use commands::*;
|
2025-10-27 23:26:42 +08:00
|
|
|
|
pub use config::{get_claude_mcp_path, get_claude_settings_path, read_json_file};
|
2025-10-28 11:58:57 +08:00
|
|
|
|
pub use error::AppError;
|
2025-10-28 10:47:48 +08:00
|
|
|
|
pub use import_export::{
|
|
|
|
|
|
create_backup, export_config_to_file, import_config_from_path, sync_current_providers_to_live,
|
|
|
|
|
|
};
|
2025-10-28 11:58:57 +08:00
|
|
|
|
pub use mcp::{
|
|
|
|
|
|
import_from_claude, import_from_codex, sync_enabled_to_claude, sync_enabled_to_codex,
|
|
|
|
|
|
};
|
2025-10-27 22:30:57 +08:00
|
|
|
|
pub use provider::Provider;
|
2025-10-28 11:58:57 +08:00
|
|
|
|
pub use services::ProviderService;
|
2025-10-27 22:30:57 +08:00
|
|
|
|
pub use settings::{update_settings, AppSettings};
|
2025-10-28 09:55:10 +08:00
|
|
|
|
pub use store::AppState;
|
2025-10-27 22:30:57 +08:00
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
use tauri::{
|
|
|
|
|
|
menu::{CheckMenuItem, Menu, MenuBuilder, MenuItem},
|
2025-09-14 21:55:41 +08:00
|
|
|
|
tray::{TrayIconBuilder, TrayIconEvent},
|
2025-09-06 16:21:21 +08:00
|
|
|
|
};
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
use tauri::{ActivationPolicy, RunEvent};
|
2025-09-06 16:21:21 +08:00
|
|
|
|
use tauri::{Emitter, Manager};
|
|
|
|
|
|
|
|
|
|
|
|
/// 创建动态托盘菜单
|
|
|
|
|
|
fn create_tray_menu(
|
|
|
|
|
|
app: &tauri::AppHandle,
|
|
|
|
|
|
app_state: &AppState,
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
) -> Result<Menu<tauri::Wry>, AppError> {
|
2025-10-28 12:23:44 +08:00
|
|
|
|
let config = app_state.config.read().map_err(AppError::from)?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
let mut menu_builder = MenuBuilder::new(app);
|
|
|
|
|
|
|
2025-09-14 16:07:51 +08:00
|
|
|
|
// 顶部:打开主界面
|
|
|
|
|
|
let show_main_item = MenuItem::with_id(app, "show_main", "打开主界面", true, None::<&str>)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建打开主界面菜单失败: {}", e)))?;
|
2025-09-14 16:07:51 +08:00
|
|
|
|
menu_builder = menu_builder.item(&show_main_item).separator();
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
// 直接添加所有供应商到主菜单(扁平化结构,更简单可靠)
|
|
|
|
|
|
if let Some(claude_manager) = config.get_manager(&crate::app_config::AppType::Claude) {
|
|
|
|
|
|
// 添加Claude标题(禁用状态,仅作为分组标识)
|
|
|
|
|
|
let claude_header =
|
|
|
|
|
|
MenuItem::with_id(app, "claude_header", "─── Claude ───", false, None::<&str>)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建Claude标题失败: {}", e)))?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
menu_builder = menu_builder.item(&claude_header);
|
|
|
|
|
|
|
|
|
|
|
|
if !claude_manager.providers.is_empty() {
|
2025-10-15 22:21:06 +08:00
|
|
|
|
// Sort providers by sortIndex, then by createdAt, then by name
|
|
|
|
|
|
let mut sorted_providers: Vec<_> = claude_manager.providers.iter().collect();
|
|
|
|
|
|
sorted_providers.sort_by(|(_, a), (_, b)| {
|
|
|
|
|
|
// Priority 1: sortIndex
|
|
|
|
|
|
match (a.sort_index, b.sort_index) {
|
|
|
|
|
|
(Some(idx_a), Some(idx_b)) => return idx_a.cmp(&idx_b),
|
|
|
|
|
|
(Some(_), None) => return std::cmp::Ordering::Less,
|
|
|
|
|
|
(None, Some(_)) => return std::cmp::Ordering::Greater,
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Priority 2: createdAt
|
|
|
|
|
|
match (a.created_at, b.created_at) {
|
|
|
|
|
|
(Some(time_a), Some(time_b)) => return time_a.cmp(&time_b),
|
|
|
|
|
|
(Some(_), None) => return std::cmp::Ordering::Greater,
|
|
|
|
|
|
(None, Some(_)) => return std::cmp::Ordering::Less,
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Priority 3: name
|
|
|
|
|
|
a.name.cmp(&b.name)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for (id, provider) in sorted_providers {
|
2025-09-06 16:21:21 +08:00
|
|
|
|
let is_current = claude_manager.current == *id;
|
|
|
|
|
|
let item = CheckMenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
format!("claude_{}", id),
|
|
|
|
|
|
&provider.name,
|
|
|
|
|
|
true,
|
|
|
|
|
|
is_current,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建菜单项失败: {}", e)))?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
menu_builder = menu_builder.item(&item);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 没有供应商时显示提示
|
|
|
|
|
|
let empty_hint = MenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
"claude_empty",
|
|
|
|
|
|
" (无供应商,请在主界面添加)",
|
|
|
|
|
|
false,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建Claude空提示失败: {}", e)))?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
menu_builder = menu_builder.item(&empty_hint);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(codex_manager) = config.get_manager(&crate::app_config::AppType::Codex) {
|
|
|
|
|
|
// 添加Codex标题(禁用状态,仅作为分组标识)
|
|
|
|
|
|
let codex_header =
|
|
|
|
|
|
MenuItem::with_id(app, "codex_header", "─── Codex ───", false, None::<&str>)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建Codex标题失败: {}", e)))?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
menu_builder = menu_builder.item(&codex_header);
|
|
|
|
|
|
|
|
|
|
|
|
if !codex_manager.providers.is_empty() {
|
2025-10-15 22:21:06 +08:00
|
|
|
|
// Sort providers by sortIndex, then by createdAt, then by name
|
|
|
|
|
|
let mut sorted_providers: Vec<_> = codex_manager.providers.iter().collect();
|
|
|
|
|
|
sorted_providers.sort_by(|(_, a), (_, b)| {
|
|
|
|
|
|
// Priority 1: sortIndex
|
|
|
|
|
|
match (a.sort_index, b.sort_index) {
|
|
|
|
|
|
(Some(idx_a), Some(idx_b)) => return idx_a.cmp(&idx_b),
|
|
|
|
|
|
(Some(_), None) => return std::cmp::Ordering::Less,
|
|
|
|
|
|
(None, Some(_)) => return std::cmp::Ordering::Greater,
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Priority 2: createdAt
|
|
|
|
|
|
match (a.created_at, b.created_at) {
|
|
|
|
|
|
(Some(time_a), Some(time_b)) => return time_a.cmp(&time_b),
|
|
|
|
|
|
(Some(_), None) => return std::cmp::Ordering::Greater,
|
|
|
|
|
|
(None, Some(_)) => return std::cmp::Ordering::Less,
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Priority 3: name
|
|
|
|
|
|
a.name.cmp(&b.name)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for (id, provider) in sorted_providers {
|
2025-09-06 16:21:21 +08:00
|
|
|
|
let is_current = codex_manager.current == *id;
|
|
|
|
|
|
let item = CheckMenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
format!("codex_{}", id),
|
|
|
|
|
|
&provider.name,
|
|
|
|
|
|
true,
|
|
|
|
|
|
is_current,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建菜单项失败: {}", e)))?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
menu_builder = menu_builder.item(&item);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 没有供应商时显示提示
|
|
|
|
|
|
let empty_hint = MenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
"codex_empty",
|
|
|
|
|
|
" (无供应商,请在主界面添加)",
|
|
|
|
|
|
false,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建Codex空提示失败: {}", e)))?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
menu_builder = menu_builder.item(&empty_hint);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分隔符和退出菜单
|
|
|
|
|
|
let quit_item = MenuItem::with_id(app, "quit", "退出", true, None::<&str>)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("创建退出菜单失败: {}", e)))?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
menu_builder = menu_builder.separator().item(&quit_item);
|
|
|
|
|
|
|
|
|
|
|
|
menu_builder
|
|
|
|
|
|
.build()
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.map_err(|e| AppError::Message(format!("构建菜单失败: {}", e)))
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
fn apply_tray_policy(app: &tauri::AppHandle, dock_visible: bool) {
|
|
|
|
|
|
let desired_policy = if dock_visible {
|
|
|
|
|
|
ActivationPolicy::Regular
|
|
|
|
|
|
} else {
|
|
|
|
|
|
ActivationPolicy::Accessory
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(err) = app.set_dock_visibility(dock_visible) {
|
|
|
|
|
|
log::warn!("设置 Dock 显示状态失败: {}", err);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(err) = app.set_activation_policy(desired_policy) {
|
|
|
|
|
|
log::warn!("设置激活策略失败: {}", err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
/// 处理托盘菜单事件
|
|
|
|
|
|
fn handle_tray_menu_event(app: &tauri::AppHandle, event_id: &str) {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("处理托盘菜单事件: {}", event_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
match event_id {
|
2025-09-14 16:07:51 +08:00
|
|
|
|
"show_main" => {
|
|
|
|
|
|
if let Some(window) = app.get_webview_window("main") {
|
2025-09-26 20:18:11 +08:00
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = window.set_skip_taskbar(false);
|
|
|
|
|
|
}
|
2025-09-14 16:07:51 +08:00
|
|
|
|
let _ = window.unminimize();
|
|
|
|
|
|
let _ = window.show();
|
|
|
|
|
|
let _ = window.set_focus();
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
|
|
|
|
|
apply_tray_policy(app, true);
|
|
|
|
|
|
}
|
2025-09-14 16:07:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-06 16:21:21 +08:00
|
|
|
|
"quit" => {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("退出应用");
|
2025-09-06 16:21:21 +08:00
|
|
|
|
app.exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
id if id.starts_with("claude_") => {
|
|
|
|
|
|
let provider_id = id.strip_prefix("claude_").unwrap();
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("切换到Claude供应商: {}", provider_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行切换
|
|
|
|
|
|
let app_handle = app.clone();
|
|
|
|
|
|
let provider_id = provider_id.to_string();
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
if let Err(e) = switch_provider_internal(
|
|
|
|
|
|
&app_handle,
|
|
|
|
|
|
crate::app_config::AppType::Claude,
|
|
|
|
|
|
provider_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
.await
|
2025-09-18 09:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
log::error!("切换Claude供应商失败: {}", e);
|
|
|
|
|
|
}
|
2025-09-06 16:21:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
id if id.starts_with("codex_") => {
|
|
|
|
|
|
let provider_id = id.strip_prefix("codex_").unwrap();
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("切换到Codex供应商: {}", provider_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行切换
|
|
|
|
|
|
let app_handle = app.clone();
|
|
|
|
|
|
let provider_id = provider_id.to_string();
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
if let Err(e) = switch_provider_internal(
|
|
|
|
|
|
&app_handle,
|
|
|
|
|
|
crate::app_config::AppType::Codex,
|
|
|
|
|
|
provider_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
.await
|
2025-09-18 09:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
log::error!("切换Codex供应商失败: {}", e);
|
|
|
|
|
|
}
|
2025-09-06 16:21:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::warn!("未处理的菜单事件: {}", event_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-14 16:07:51 +08:00
|
|
|
|
//
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
/// 内部切换供应商函数
|
|
|
|
|
|
async fn switch_provider_internal(
|
|
|
|
|
|
app: &tauri::AppHandle,
|
|
|
|
|
|
app_type: crate::app_config::AppType,
|
|
|
|
|
|
provider_id: String,
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
) -> Result<(), AppError> {
|
2025-09-06 16:21:21 +08:00
|
|
|
|
if let Some(app_state) = app.try_state::<AppState>() {
|
|
|
|
|
|
// 在使用前先保存需要的值
|
|
|
|
|
|
let app_type_str = app_type.as_str().to_string();
|
|
|
|
|
|
let provider_id_clone = provider_id.clone();
|
|
|
|
|
|
|
|
|
|
|
|
crate::commands::switch_provider(
|
2025-10-12 16:52:32 +08:00
|
|
|
|
app_state.clone(),
|
2025-09-06 16:21:21 +08:00
|
|
|
|
Some(app_type),
|
|
|
|
|
|
None,
|
|
|
|
|
|
None,
|
|
|
|
|
|
provider_id,
|
|
|
|
|
|
)
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
.await
|
|
|
|
|
|
.map_err(AppError::Message)?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 切换成功后重新创建托盘菜单
|
|
|
|
|
|
if let Ok(new_menu) = create_tray_menu(app, app_state.inner()) {
|
|
|
|
|
|
if let Some(tray) = app.tray_by_id("main") {
|
|
|
|
|
|
if let Err(e) = tray.set_menu(Some(new_menu)) {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::error!("更新托盘菜单失败: {}", e);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 发射事件到前端,通知供应商已切换
|
|
|
|
|
|
let event_data = serde_json::json!({
|
|
|
|
|
|
"appType": app_type_str,
|
|
|
|
|
|
"providerId": provider_id_clone
|
|
|
|
|
|
});
|
|
|
|
|
|
if let Err(e) = app.emit("provider-switched", event_data) {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::error!("发射供应商切换事件失败: {}", e);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 更新托盘菜单的Tauri命令
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
async fn update_tray_menu(
|
|
|
|
|
|
app: tauri::AppHandle,
|
|
|
|
|
|
state: tauri::State<'_, AppState>,
|
|
|
|
|
|
) -> Result<bool, String> {
|
refactor(backend): complete phase 1 - full AppError migration (100%)
Finalized the backend error handling refactoring by migrating all remaining
modules to use AppError, eliminating all temporary error conversions.
## Changes
### Fully Migrated Modules
- **mcp.rs** (129 lines changed)
- Migrated 13 functions from Result<T, String> to Result<T, AppError>
- Added AppError::McpValidation for domain-specific validation errors
- Functions: validate_server_spec, validate_mcp_entry, upsert_in_config_for,
delete_in_config_for, set_enabled_and_sync_for, sync_enabled_to_claude,
import_from_claude, import_from_codex, sync_enabled_to_codex
- Removed all temporary error conversions
- **usage_script.rs** (143 lines changed)
- Migrated 4 functions: execute_usage_script, send_http_request,
validate_result, validate_single_usage
- Used AppError::Message for JS runtime errors
- Used AppError::InvalidInput for script validation errors
- Improved error construction with ok_or_else (lazy evaluation)
- **lib.rs** (47 lines changed)
- Migrated create_tray_menu() and switch_provider_internal()
- Simplified PoisonError handling with AppError::from
- Added error logging in update_tray_menu()
- Improved error handling in menu update logic
- **migration.rs** (10 lines changed)
- Migrated migrate_copies_into_config()
- Used AppError::io() helper for file operations
- **speedtest.rs** (8 lines changed)
- Migrated build_client() and test_endpoints()
- Used AppError::Message for HTTP client errors
- **app_store.rs** (14 lines changed)
- Migrated set_app_config_dir_to_store() and migrate_app_config_dir_from_settings()
- Used AppError::Message for Tauri Store errors
- Used AppError::io() for file system operations
### Fixed Previous Temporary Solutions
- **import_export.rs** (2 lines changed)
- Removed AppError::Message wrapper for mcp::sync_enabled_to_codex
- Now directly calls the AppError-returning function (no conversion needed)
- **commands.rs** (6 lines changed)
- Updated query_provider_usage() and test_api_endpoints()
- Explicit .to_string() conversion for Tauri command interface
## New Error Types
- **AppError::McpValidation**: Domain-specific error for MCP configuration validation
- Separates MCP validation errors from generic Config errors
- Follows domain-driven design principles
## Statistics
- Files changed: 8
- Lines changed: +237/-122 (net +115)
- Compilation: ✅ Success (7.13s, 0 warnings)
- Tests: ✅ 4/4 passed
## Benefits
- **100% Migration**: All modules now use AppError consistently
- **Domain Errors**: Added McpValidation for better error categorization
- **No Temporary Solutions**: Eliminated all AppError::Message conversions for internal calls
- **Performance**: Used ok_or_else for lazy error construction
- **Maintainability**: Removed ~60 instances of .map_err(|e| format!("...", e))
- **Debugging**: Added error logging in critical paths (tray menu updates)
## Phase 1 Complete
Total impact across 3 commits:
- 25 files changed
- +671/-302 lines (net +369)
- 100% of codebase migrated from Result<T, String> to Result<T, AppError>
- 0 compilation warnings
- All tests passing
Ready for Phase 2: Splitting commands.rs by domain.
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 20:36:08 +08:00
|
|
|
|
match create_tray_menu(&app, state.inner()) {
|
|
|
|
|
|
Ok(new_menu) => {
|
|
|
|
|
|
if let Some(tray) = app.tray_by_id("main") {
|
|
|
|
|
|
tray.set_menu(Some(new_menu))
|
|
|
|
|
|
.map_err(|e| format!("更新托盘菜单失败: {}", e))?;
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(err) => {
|
|
|
|
|
|
log::error!("创建托盘菜单失败: {}", err);
|
|
|
|
|
|
Ok(false)
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-23 20:15:10 +08:00
|
|
|
|
|
2025-08-23 19:57:42 +08:00
|
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
|
|
|
|
pub fn run() {
|
2025-09-23 10:02:23 +08:00
|
|
|
|
let mut builder = tauri::Builder::default();
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
|
|
|
|
|
|
{
|
2025-09-26 20:18:11 +08:00
|
|
|
|
builder = builder.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
|
|
|
|
|
|
if let Some(window) = app.get_webview_window("main") {
|
|
|
|
|
|
let _ = window.unminimize();
|
|
|
|
|
|
let _ = window.show();
|
|
|
|
|
|
let _ = window.set_focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
2025-09-23 10:02:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let builder = builder
|
2025-09-26 20:18:11 +08:00
|
|
|
|
// 拦截窗口关闭:根据设置决定是否最小化到托盘
|
2025-10-12 16:52:32 +08:00
|
|
|
|
.on_window_event(|window, event| {
|
|
|
|
|
|
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
|
2025-09-26 20:18:11 +08:00
|
|
|
|
let settings = crate::settings::get_settings();
|
|
|
|
|
|
|
|
|
|
|
|
if settings.minimize_to_tray_on_close {
|
|
|
|
|
|
api.prevent_close();
|
|
|
|
|
|
let _ = window.hide();
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = window.set_skip_taskbar(true);
|
|
|
|
|
|
}
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
2025-10-12 16:52:32 +08:00
|
|
|
|
apply_tray_policy(window.app_handle(), false);
|
2025-09-29 17:03:13 +08:00
|
|
|
|
}
|
2025-09-26 20:18:11 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
window.app_handle().exit(0);
|
|
|
|
|
|
}
|
2025-09-14 16:07:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2025-09-08 16:58:41 +08:00
|
|
|
|
.plugin(tauri_plugin_process::init())
|
2025-09-20 21:20:07 +08:00
|
|
|
|
.plugin(tauri_plugin_dialog::init())
|
2025-08-25 20:16:29 +08:00
|
|
|
|
.plugin(tauri_plugin_opener::init())
|
2025-10-15 09:15:53 +08:00
|
|
|
|
.plugin(tauri_plugin_store::Builder::new().build())
|
2025-08-23 20:15:10 +08:00
|
|
|
|
.setup(|app| {
|
2025-10-15 09:15:53 +08:00
|
|
|
|
// 设置全局 AppHandle 以供 Store 使用
|
|
|
|
|
|
app_store::set_app_handle(app.handle().clone());
|
2025-09-08 16:58:41 +08:00
|
|
|
|
// 注册 Updater 插件(桌面端)
|
|
|
|
|
|
#[cfg(desktop)]
|
2025-09-09 10:28:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
if let Err(e) = app
|
|
|
|
|
|
.handle()
|
|
|
|
|
|
.plugin(tauri_plugin_updater::Builder::new().build())
|
|
|
|
|
|
{
|
|
|
|
|
|
// 若配置不完整(如缺少 pubkey),跳过 Updater 而不中断应用
|
|
|
|
|
|
log::warn!("初始化 Updater 插件失败,已跳过:{}", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-25 23:06:54 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设置 macOS 标题栏背景色为主界面蓝色
|
|
|
|
|
|
if let Some(window) = app.get_webview_window("main") {
|
|
|
|
|
|
use objc2::rc::Retained;
|
2025-08-27 11:00:53 +08:00
|
|
|
|
use objc2::runtime::AnyObject;
|
2025-08-25 23:06:54 +08:00
|
|
|
|
use objc2_app_kit::NSColor;
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-25 23:06:54 +08:00
|
|
|
|
let ns_window_ptr = window.ns_window().unwrap();
|
2025-08-27 11:00:53 +08:00
|
|
|
|
let ns_window: Retained<AnyObject> =
|
|
|
|
|
|
unsafe { Retained::retain(ns_window_ptr as *mut AnyObject).unwrap() };
|
|
|
|
|
|
|
2025-08-25 23:06:54 +08:00
|
|
|
|
// 使用与主界面 banner 相同的蓝色 #3498db
|
|
|
|
|
|
// #3498db = RGB(52, 152, 219)
|
|
|
|
|
|
let bg_color = unsafe {
|
|
|
|
|
|
NSColor::colorWithRed_green_blue_alpha(
|
2025-08-27 11:00:53 +08:00
|
|
|
|
52.0 / 255.0, // R: 52
|
|
|
|
|
|
152.0 / 255.0, // G: 152
|
|
|
|
|
|
219.0 / 255.0, // B: 219
|
|
|
|
|
|
1.0, // Alpha: 1.0
|
2025-08-25 23:06:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
};
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-25 23:06:54 +08:00
|
|
|
|
unsafe {
|
|
|
|
|
|
use objc2::msg_send;
|
|
|
|
|
|
let _: () = msg_send![&*ns_window, setBackgroundColor: &*bg_color];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-23 20:15:10 +08:00
|
|
|
|
// 初始化日志
|
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
|
|
app.handle().plugin(
|
|
|
|
|
|
tauri_plugin_log::Builder::default()
|
|
|
|
|
|
.level(log::LevelFilter::Info)
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
)?;
|
|
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-24 23:04:55 +08:00
|
|
|
|
// 初始化应用状态(仅创建一次,并在本函数末尾注入 manage)
|
2025-08-23 20:15:10 +08:00
|
|
|
|
let app_state = AppState::new();
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-10-15 09:15:53 +08:00
|
|
|
|
// 迁移旧的 app_config_dir 配置到 Store
|
|
|
|
|
|
if let Err(e) = app_store::migrate_app_config_dir_from_settings(&app.handle()) {
|
|
|
|
|
|
log::warn!("迁移 app_config_dir 失败: {}", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 16:16:51 +08:00
|
|
|
|
// 首次启动迁移:扫描副本文件,合并到 config.json,并归档副本;旧 config.json 先归档
|
2025-08-23 20:15:10 +08:00
|
|
|
|
{
|
2025-10-28 12:23:44 +08:00
|
|
|
|
let mut config_guard = app_state.config.write().unwrap();
|
2025-10-12 16:52:32 +08:00
|
|
|
|
let migrated = migration::migrate_copies_into_config(&mut config_guard)?;
|
2025-09-04 16:16:51 +08:00
|
|
|
|
if migrated {
|
|
|
|
|
|
log::info!("已将副本文件导入到 config.json,并完成归档");
|
2025-08-23 20:15:10 +08:00
|
|
|
|
}
|
2025-09-04 16:16:51 +08:00
|
|
|
|
// 确保两个 App 条目存在
|
|
|
|
|
|
config_guard.ensure_app(&app_config::AppType::Claude);
|
|
|
|
|
|
config_guard.ensure_app(&app_config::AppType::Codex);
|
2025-08-23 20:15:10 +08:00
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-30 21:54:11 +08:00
|
|
|
|
// 保存配置
|
|
|
|
|
|
let _ = app_state.save();
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
// 创建动态托盘菜单
|
2025-10-12 16:52:32 +08:00
|
|
|
|
let menu = create_tray_menu(app.handle(), &app_state)?;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
2025-09-14 21:55:41 +08:00
|
|
|
|
// 构建托盘
|
|
|
|
|
|
let mut tray_builder = TrayIconBuilder::with_id("main")
|
2025-09-14 16:07:51 +08:00
|
|
|
|
.on_tray_icon_event(|_tray, event| match event {
|
|
|
|
|
|
// 左键点击已通过 show_menu_on_left_click(true) 打开菜单,这里不再额外处理
|
|
|
|
|
|
TrayIconEvent::Click { .. } => {}
|
|
|
|
|
|
_ => log::debug!("unhandled event {event:?}"),
|
2025-09-06 16:21:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
.menu(&menu)
|
|
|
|
|
|
.on_menu_event(|app, event| {
|
|
|
|
|
|
handle_tray_menu_event(app, &event.id.0);
|
|
|
|
|
|
})
|
2025-09-14 21:55:41 +08:00
|
|
|
|
.show_menu_on_left_click(true);
|
|
|
|
|
|
|
|
|
|
|
|
// 统一使用应用默认图标;待托盘模板图标就绪后再启用
|
|
|
|
|
|
tray_builder = tray_builder.icon(app.default_window_icon().unwrap().clone());
|
|
|
|
|
|
|
|
|
|
|
|
let _tray = tray_builder.build(app)?;
|
2025-08-24 23:04:55 +08:00
|
|
|
|
// 将同一个实例注入到全局状态,避免重复创建导致的不一致
|
|
|
|
|
|
app.manage(app_state);
|
2025-08-23 20:15:10 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
|
})
|
|
|
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
|
|
commands::get_providers,
|
|
|
|
|
|
commands::get_current_provider,
|
|
|
|
|
|
commands::add_provider,
|
|
|
|
|
|
commands::update_provider,
|
|
|
|
|
|
commands::delete_provider,
|
|
|
|
|
|
commands::switch_provider,
|
|
|
|
|
|
commands::import_default_config,
|
|
|
|
|
|
commands::get_claude_config_status,
|
2025-08-30 21:54:11 +08:00
|
|
|
|
commands::get_config_status,
|
2025-08-23 20:15:10 +08:00
|
|
|
|
commands::get_claude_code_config_path,
|
2025-09-20 21:20:07 +08:00
|
|
|
|
commands::get_config_dir,
|
2025-08-23 20:15:10 +08:00
|
|
|
|
commands::open_config_folder,
|
2025-09-20 21:20:07 +08:00
|
|
|
|
commands::pick_directory,
|
2025-08-23 20:15:10 +08:00
|
|
|
|
commands::open_external,
|
2025-09-07 11:36:09 +08:00
|
|
|
|
commands::get_app_config_path,
|
|
|
|
|
|
commands::open_app_config_folder,
|
2025-10-10 15:47:57 +08:00
|
|
|
|
commands::read_live_provider_settings,
|
2025-09-07 11:36:09 +08:00
|
|
|
|
commands::get_settings,
|
|
|
|
|
|
commands::save_settings,
|
2025-10-15 09:15:53 +08:00
|
|
|
|
commands::restart_app,
|
2025-09-07 11:36:09 +08:00
|
|
|
|
commands::check_for_updates,
|
2025-09-24 11:25:33 +08:00
|
|
|
|
commands::is_portable_mode,
|
2025-10-01 21:23:55 +08:00
|
|
|
|
commands::get_claude_plugin_status,
|
|
|
|
|
|
commands::read_claude_plugin_config,
|
|
|
|
|
|
commands::apply_claude_plugin_config,
|
|
|
|
|
|
commands::is_claude_plugin_applied,
|
2025-10-08 22:34:58 +08:00
|
|
|
|
// Claude MCP management
|
|
|
|
|
|
commands::get_claude_mcp_status,
|
|
|
|
|
|
commands::read_claude_mcp_config,
|
|
|
|
|
|
commands::upsert_claude_mcp_server,
|
|
|
|
|
|
commands::delete_claude_mcp_server,
|
|
|
|
|
|
commands::validate_mcp_command,
|
2025-10-15 09:15:25 +08:00
|
|
|
|
// usage query
|
|
|
|
|
|
commands::query_provider_usage,
|
2025-10-09 21:08:42 +08:00
|
|
|
|
// New MCP via config.json (SSOT)
|
|
|
|
|
|
commands::get_mcp_config,
|
|
|
|
|
|
commands::upsert_mcp_server_in_config,
|
|
|
|
|
|
commands::delete_mcp_server_in_config,
|
|
|
|
|
|
commands::set_mcp_enabled,
|
|
|
|
|
|
commands::sync_enabled_mcp_to_claude,
|
2025-10-10 12:35:02 +08:00
|
|
|
|
commands::sync_enabled_mcp_to_codex,
|
2025-10-09 21:08:42 +08:00
|
|
|
|
commands::import_mcp_from_claude,
|
2025-10-10 14:59:02 +08:00
|
|
|
|
commands::import_mcp_from_codex,
|
feat: Implement Speed Test Function
* feat: add unified endpoint speed test for API providers
Add a comprehensive endpoint latency testing system that allows users to:
- Test multiple API endpoints concurrently
- Auto-select the fastest endpoint based on latency
- Add/remove custom endpoints dynamically
- View latency results with color-coded indicators
Backend (Rust):
- Implement parallel HTTP HEAD requests with configurable timeout
- Handle various error scenarios (timeout, connection failure, invalid URL)
- Return structured latency data with status codes
Frontend (React):
- Create interactive speed test UI component with auto-sort by latency
- Support endpoint management (add/remove custom endpoints)
- Extract and update Codex base_url from TOML configuration
- Integrate with provider presets for default endpoint candidates
This feature improves user experience when selecting optimal API endpoints,
especially useful for users with multiple provider options or proxy setups.
* refactor: convert endpoint speed test to modal dialog
- Transform EndpointSpeedTest component into a modal dialog
- Add "Advanced" button next to base URL input to open modal
- Support ESC key and backdrop click to close modal
- Apply Linear design principles: minimal styling, clean layout
- Remove unused showBaseUrlInput variable
- Implement same modal pattern for both Claude and Codex
* fix: prevent modal cascade closing when ESC is pressed
- Add state checks to prevent parent modal from closing when child modals (endpoint speed test or template wizard) are open
- Update ESC key handler dependencies to track all modal states
- Ensures only the topmost modal responds to ESC key
* refactor: unify speed test panel UI with project design system
UI improvements:
- Update modal border radius from rounded-lg to rounded-xl
- Unify header padding from px-6 py-4 to p-6
- Change speed test button color to blue theme (bg-blue-500) for consistency
- Update footer background from bg-gray-50 to bg-gray-100
- Style "Done" button as primary action button with blue theme
- Adjust footer button spacing and hover states
Simplify endpoint display:
- Remove endpoint labels (e.g., "Current Address", "Custom 1")
- Display only URL for cleaner interface
- Clean up all label-related logic:
* Remove label field from EndpointCandidate interface
* Remove label generation in buildInitialEntries function
* Remove label handling in useEffect merge logic
* Remove label generation in handleAddEndpoint
* Remove label parameters from claudeSpeedTestEndpoints
* Remove label parameters from codexSpeedTestEndpoints
* refactor: improve endpoint list UI consistency
- Show delete button for all endpoints on hover for uniform UI
- Change selected state to use blue theme matching main interface:
* Blue border (border-blue-500) for selected items
* Light blue background (bg-blue-50/dark:bg-blue-900/20)
* Blue indicator dot (bg-blue-500/dark:bg-blue-400)
- Switch from compact list (space-y-px) to card-based layout (space-y-2)
- Add rounded corners to each endpoint item for better visual separation
* feat: persist custom endpoints to settings.json
- Extend AppSettings to store custom endpoints for Claude and Codex
- Add Tauri commands: get/add/remove/update custom endpoints
- Update frontend API with endpoint persistence methods
- Modify EndpointSpeedTest to load/save custom endpoints via API
- Track endpoint last used time for future sorting/cleanup
- Store endpoints per app type in settings.json instead of localStorage
* - feat(types): add Provider.meta and ProviderMeta (snake_case) with custom_endpoints map
- feat(provider-form): persist custom endpoints on provider create by merging EndpointSpeedTest’s custom URLs into meta.custom_endpoints on submit
- feat(endpoint-speed-test): add onCustomEndpointsChange callback emitting normalized custom URLs; wire it for both Claude/Codex modals
- fix(api): send alias param names (app/appType/app_type and provider_id/providerId) in Tauri invokes to avoid “missing providerId” with older backends
- storage: custom endpoints are stored in ~/.cc-switch/config.json under providers[<id>].meta.custom_endpoints (not in settings.json)
- behavior: edit flow remains immediate writes; create flow now writes once via addProvider, removing the providerId dependency during creation
* feat: add endpoint candidates support and code formatting improvements
- Add endpointCandidates field to ProviderPreset and CodexProviderPreset interfaces
- Integrate preset endpoint candidates into speed test endpoint selection
- Add multiple endpoint options for PackyCode providers (Claude & Codex)
- Apply consistent code formatting (trailing commas, line breaks)
- Improve template value type safety and readability
* refactor: improve endpoint management button UX
Replace ambiguous "Advanced" text with intuitive "Manage & Test" label accompanied by Zap icon, making the endpoint management panel entry point more discoverable and self-explanatory for both Claude and Codex configurations.
* - merge: merge origin/main, resolve conflicts and preserve both feature sets
- feat(tauri): register import/export and file dialogs; keep endpoint speed test and custom endpoints
- feat(api): add updateTrayMenu and onProviderSwitched; wire import/export APIs
- feat(types): extend global API declarations (import/export)
- chore(presets): GLM preset supports both new and legacy model keys
- chore(rust): add chrono dependency; refresh lockfile
---------
Co-authored-by: Jason <farion1231@gmail.com>
2025-10-07 19:14:32 +08:00
|
|
|
|
// ours: endpoint speed test + custom endpoint management
|
|
|
|
|
|
commands::test_api_endpoints,
|
|
|
|
|
|
commands::get_custom_endpoints,
|
|
|
|
|
|
commands::add_custom_endpoint,
|
|
|
|
|
|
commands::remove_custom_endpoint,
|
|
|
|
|
|
commands::update_endpoint_last_used,
|
2025-10-15 09:15:53 +08:00
|
|
|
|
// app_config_dir override via Store
|
|
|
|
|
|
commands::get_app_config_dir_override,
|
|
|
|
|
|
commands::set_app_config_dir_override,
|
2025-10-15 22:21:06 +08:00
|
|
|
|
// provider sort order management
|
|
|
|
|
|
commands::update_providers_sort_order,
|
feat: Implement Speed Test Function
* feat: add unified endpoint speed test for API providers
Add a comprehensive endpoint latency testing system that allows users to:
- Test multiple API endpoints concurrently
- Auto-select the fastest endpoint based on latency
- Add/remove custom endpoints dynamically
- View latency results with color-coded indicators
Backend (Rust):
- Implement parallel HTTP HEAD requests with configurable timeout
- Handle various error scenarios (timeout, connection failure, invalid URL)
- Return structured latency data with status codes
Frontend (React):
- Create interactive speed test UI component with auto-sort by latency
- Support endpoint management (add/remove custom endpoints)
- Extract and update Codex base_url from TOML configuration
- Integrate with provider presets for default endpoint candidates
This feature improves user experience when selecting optimal API endpoints,
especially useful for users with multiple provider options or proxy setups.
* refactor: convert endpoint speed test to modal dialog
- Transform EndpointSpeedTest component into a modal dialog
- Add "Advanced" button next to base URL input to open modal
- Support ESC key and backdrop click to close modal
- Apply Linear design principles: minimal styling, clean layout
- Remove unused showBaseUrlInput variable
- Implement same modal pattern for both Claude and Codex
* fix: prevent modal cascade closing when ESC is pressed
- Add state checks to prevent parent modal from closing when child modals (endpoint speed test or template wizard) are open
- Update ESC key handler dependencies to track all modal states
- Ensures only the topmost modal responds to ESC key
* refactor: unify speed test panel UI with project design system
UI improvements:
- Update modal border radius from rounded-lg to rounded-xl
- Unify header padding from px-6 py-4 to p-6
- Change speed test button color to blue theme (bg-blue-500) for consistency
- Update footer background from bg-gray-50 to bg-gray-100
- Style "Done" button as primary action button with blue theme
- Adjust footer button spacing and hover states
Simplify endpoint display:
- Remove endpoint labels (e.g., "Current Address", "Custom 1")
- Display only URL for cleaner interface
- Clean up all label-related logic:
* Remove label field from EndpointCandidate interface
* Remove label generation in buildInitialEntries function
* Remove label handling in useEffect merge logic
* Remove label generation in handleAddEndpoint
* Remove label parameters from claudeSpeedTestEndpoints
* Remove label parameters from codexSpeedTestEndpoints
* refactor: improve endpoint list UI consistency
- Show delete button for all endpoints on hover for uniform UI
- Change selected state to use blue theme matching main interface:
* Blue border (border-blue-500) for selected items
* Light blue background (bg-blue-50/dark:bg-blue-900/20)
* Blue indicator dot (bg-blue-500/dark:bg-blue-400)
- Switch from compact list (space-y-px) to card-based layout (space-y-2)
- Add rounded corners to each endpoint item for better visual separation
* feat: persist custom endpoints to settings.json
- Extend AppSettings to store custom endpoints for Claude and Codex
- Add Tauri commands: get/add/remove/update custom endpoints
- Update frontend API with endpoint persistence methods
- Modify EndpointSpeedTest to load/save custom endpoints via API
- Track endpoint last used time for future sorting/cleanup
- Store endpoints per app type in settings.json instead of localStorage
* - feat(types): add Provider.meta and ProviderMeta (snake_case) with custom_endpoints map
- feat(provider-form): persist custom endpoints on provider create by merging EndpointSpeedTest’s custom URLs into meta.custom_endpoints on submit
- feat(endpoint-speed-test): add onCustomEndpointsChange callback emitting normalized custom URLs; wire it for both Claude/Codex modals
- fix(api): send alias param names (app/appType/app_type and provider_id/providerId) in Tauri invokes to avoid “missing providerId” with older backends
- storage: custom endpoints are stored in ~/.cc-switch/config.json under providers[<id>].meta.custom_endpoints (not in settings.json)
- behavior: edit flow remains immediate writes; create flow now writes once via addProvider, removing the providerId dependency during creation
* feat: add endpoint candidates support and code formatting improvements
- Add endpointCandidates field to ProviderPreset and CodexProviderPreset interfaces
- Integrate preset endpoint candidates into speed test endpoint selection
- Add multiple endpoint options for PackyCode providers (Claude & Codex)
- Apply consistent code formatting (trailing commas, line breaks)
- Improve template value type safety and readability
* refactor: improve endpoint management button UX
Replace ambiguous "Advanced" text with intuitive "Manage & Test" label accompanied by Zap icon, making the endpoint management panel entry point more discoverable and self-explanatory for both Claude and Codex configurations.
* - merge: merge origin/main, resolve conflicts and preserve both feature sets
- feat(tauri): register import/export and file dialogs; keep endpoint speed test and custom endpoints
- feat(api): add updateTrayMenu and onProviderSwitched; wire import/export APIs
- feat(types): extend global API declarations (import/export)
- chore(presets): GLM preset supports both new and legacy model keys
- chore(rust): add chrono dependency; refresh lockfile
---------
Co-authored-by: Jason <farion1231@gmail.com>
2025-10-07 19:14:32 +08:00
|
|
|
|
// theirs: config import/export and dialogs
|
2025-10-05 23:33:07 +08:00
|
|
|
|
import_export::export_config_to_file,
|
|
|
|
|
|
import_export::import_config_from_file,
|
|
|
|
|
|
import_export::save_file_dialog,
|
|
|
|
|
|
import_export::open_file_dialog,
|
2025-10-27 13:20:59 +08:00
|
|
|
|
import_export::sync_current_providers_live,
|
2025-09-06 16:21:21 +08:00
|
|
|
|
update_tray_menu,
|
2025-09-16 10:42:59 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
let app = builder
|
|
|
|
|
|
.build(tauri::generate_context!())
|
2025-08-23 20:15:10 +08:00
|
|
|
|
.expect("error while running tauri application");
|
2025-09-16 10:42:59 +08:00
|
|
|
|
|
|
|
|
|
|
app.run(|app_handle, event| {
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
// macOS 在 Dock 图标被点击并重新激活应用时会触发 Reopen 事件,这里手动恢复主窗口
|
2025-10-12 16:52:32 +08:00
|
|
|
|
if let RunEvent::Reopen { .. } = event {
|
|
|
|
|
|
if let Some(window) = app_handle.get_webview_window("main") {
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = window.set_skip_taskbar(false);
|
2025-09-16 10:42:59 +08:00
|
|
|
|
}
|
2025-10-12 16:52:32 +08:00
|
|
|
|
let _ = window.unminimize();
|
|
|
|
|
|
let _ = window.show();
|
|
|
|
|
|
let _ = window.set_focus();
|
|
|
|
|
|
apply_tray_policy(app_handle, true);
|
2025-09-16 10:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = (app_handle, event);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-10-27 13:20:59 +08:00
|
|
|
|
}
|