2025-10-09 21:08:42 +08:00
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
2025-10-09 22:02:56 +08:00
|
|
|
|
use crate::app_config::{AppType, McpConfig, MultiAppConfig};
|
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
|
|
|
|
use crate::error::AppError;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
|
2025-11-16 16:15:17 +08:00
|
|
|
|
/// 基础校验:允许 stdio/http/sse;或省略 type(视为 stdio)。对应必填字段存在
|
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
|
|
|
|
fn validate_server_spec(spec: &Value) -> Result<(), AppError> {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
if !spec.is_object() {
|
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
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"MCP 服务器连接定义必须为 JSON 对象".into(),
|
|
|
|
|
|
));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
2025-10-09 22:02:56 +08:00
|
|
|
|
let t_opt = spec.get("type").and_then(|x| x.as_str());
|
2025-11-16 16:15:17 +08:00
|
|
|
|
// 支持三种:stdio/http/sse;若缺省 type 则按 stdio 处理(与社区常见 .mcp.json 一致)
|
2025-10-09 22:02:56 +08:00
|
|
|
|
let is_stdio = t_opt.map(|t| t == "stdio").unwrap_or(true);
|
|
|
|
|
|
let is_http = t_opt.map(|t| t == "http").unwrap_or(false);
|
2025-11-16 16:15:17 +08:00
|
|
|
|
let is_sse = t_opt.map(|t| t == "sse").unwrap_or(false);
|
2025-10-12 00:08:37 +08:00
|
|
|
|
|
2025-11-16 16:15:17 +08:00
|
|
|
|
if !(is_stdio || is_http || is_sse) {
|
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
|
|
|
|
return Err(AppError::McpValidation(
|
2025-11-16 16:15:17 +08:00
|
|
|
|
"MCP 服务器 type 必须是 'stdio'、'http' 或 'sse'(或省略表示 stdio)".into(),
|
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
|
|
|
|
));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
2025-10-09 22:02:56 +08:00
|
|
|
|
|
|
|
|
|
|
if is_stdio {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
let cmd = spec.get("command").and_then(|x| x.as_str()).unwrap_or("");
|
|
|
|
|
|
if cmd.trim().is_empty() {
|
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
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"stdio 类型的 MCP 服务器缺少 command 字段".into(),
|
|
|
|
|
|
));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-09 22:02:56 +08:00
|
|
|
|
if is_http {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
let url = spec.get("url").and_then(|x| x.as_str()).unwrap_or("");
|
|
|
|
|
|
if url.trim().is_empty() {
|
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
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"http 类型的 MCP 服务器缺少 url 字段".into(),
|
|
|
|
|
|
));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-16 16:15:17 +08:00
|
|
|
|
if is_sse {
|
|
|
|
|
|
let url = spec.get("url").and_then(|x| x.as_str()).unwrap_or("");
|
|
|
|
|
|
if url.trim().is_empty() {
|
|
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"sse 类型的 MCP 服务器缺少 url 字段".into(),
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-09 21:08:42 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
#[allow(dead_code)] // v3.7.0: 旧的验证逻辑,保留用于未来可能的迁移
|
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
|
|
|
|
fn validate_mcp_entry(entry: &Value) -> Result<(), AppError> {
|
2025-10-12 00:08:37 +08:00
|
|
|
|
let obj = entry
|
|
|
|
|
|
.as_object()
|
2025-10-28 11:58:57 +08:00
|
|
|
|
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目必须为 JSON 对象".into()))?;
|
2025-10-12 00:08:37 +08:00
|
|
|
|
|
|
|
|
|
|
let server = obj
|
|
|
|
|
|
.get("server")
|
2025-10-28 11:58:57 +08:00
|
|
|
|
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目缺少 server 字段".into()))?;
|
2025-10-12 00:08:37 +08:00
|
|
|
|
validate_server_spec(server)?;
|
|
|
|
|
|
|
|
|
|
|
|
for key in ["name", "description", "homepage", "docs"] {
|
|
|
|
|
|
if let Some(val) = obj.get(key) {
|
|
|
|
|
|
if !val.is_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
|
|
|
|
return Err(AppError::McpValidation(format!(
|
2025-11-12 10:47:34 +08:00
|
|
|
|
"MCP 服务器 {key} 必须为字符串"
|
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
|
|
|
|
)));
|
2025-10-12 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(tags) = obj.get("tags") {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
let arr = tags
|
|
|
|
|
|
.as_array()
|
|
|
|
|
|
.ok_or_else(|| AppError::McpValidation("MCP 服务器 tags 必须为字符串数组".into()))?;
|
2025-10-12 00:08:37 +08:00
|
|
|
|
if !arr.iter().all(|item| item.is_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
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"MCP 服务器 tags 必须为字符串数组".into(),
|
|
|
|
|
|
));
|
2025-10-12 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(enabled) = obj.get("enabled") {
|
|
|
|
|
|
if !enabled.is_boolean() {
|
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
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"MCP 服务器 enabled 必须为布尔值".into(),
|
|
|
|
|
|
));
|
2025-10-12 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 16:21:32 +08:00
|
|
|
|
fn normalize_server_keys(map: &mut HashMap<String, Value>) -> usize {
|
|
|
|
|
|
let mut change_count = 0usize;
|
|
|
|
|
|
let mut renames: Vec<(String, String)> = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
|
for (key_ref, value) in map.iter_mut() {
|
|
|
|
|
|
let key = key_ref.clone();
|
|
|
|
|
|
let Some(obj) = value.as_object_mut() else {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let id_value = obj.get("id").cloned();
|
|
|
|
|
|
|
|
|
|
|
|
let target_id: String;
|
|
|
|
|
|
|
|
|
|
|
|
match id_value {
|
|
|
|
|
|
Some(id_val) => match id_val.as_str() {
|
|
|
|
|
|
Some(id_str) => {
|
|
|
|
|
|
let trimmed = id_str.trim();
|
|
|
|
|
|
if trimmed.is_empty() {
|
|
|
|
|
|
obj.insert("id".into(), json!(key.clone()));
|
|
|
|
|
|
change_count += 1;
|
|
|
|
|
|
target_id = key.clone();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if trimmed != id_str {
|
|
|
|
|
|
obj.insert("id".into(), json!(trimmed));
|
|
|
|
|
|
change_count += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
target_id = trimmed.to_string();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
None => {
|
|
|
|
|
|
obj.insert("id".into(), json!(key.clone()));
|
|
|
|
|
|
change_count += 1;
|
|
|
|
|
|
target_id = key.clone();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
None => {
|
|
|
|
|
|
obj.insert("id".into(), json!(key.clone()));
|
|
|
|
|
|
change_count += 1;
|
|
|
|
|
|
target_id = key.clone();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if target_id != key {
|
|
|
|
|
|
renames.push((key, target_id));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (old_key, new_key) in renames {
|
|
|
|
|
|
if old_key == new_key {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if map.contains_key(&new_key) {
|
2025-11-12 23:38:43 +08:00
|
|
|
|
log::warn!("MCP 条目 '{old_key}' 的内部 id '{new_key}' 与现有键冲突,回退为原键");
|
2025-10-12 16:21:32 +08:00
|
|
|
|
if let Some(value) = map.get_mut(&old_key) {
|
|
|
|
|
|
if let Some(obj) = value.as_object_mut() {
|
|
|
|
|
|
if obj
|
|
|
|
|
|
.get("id")
|
|
|
|
|
|
.and_then(|v| v.as_str())
|
|
|
|
|
|
.map(|s| s != old_key)
|
|
|
|
|
|
.unwrap_or(true)
|
|
|
|
|
|
{
|
|
|
|
|
|
obj.insert("id".into(), json!(old_key.clone()));
|
|
|
|
|
|
change_count += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(mut value) = map.remove(&old_key) {
|
|
|
|
|
|
if let Some(obj) = value.as_object_mut() {
|
|
|
|
|
|
obj.insert("id".into(), json!(new_key.clone()));
|
|
|
|
|
|
}
|
2025-11-12 10:47:34 +08:00
|
|
|
|
log::info!("MCP 条目键名已自动修复: '{old_key}' -> '{new_key}'");
|
2025-10-12 16:21:32 +08:00
|
|
|
|
map.insert(new_key, value);
|
|
|
|
|
|
change_count += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
change_count
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn normalize_servers_for(config: &mut MultiAppConfig, app: &AppType) -> usize {
|
|
|
|
|
|
let servers = &mut config.mcp_for_mut(app).servers;
|
|
|
|
|
|
normalize_server_keys(servers)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
fn extract_server_spec(entry: &Value) -> Result<Value, AppError> {
|
2025-10-12 00:08:37 +08:00
|
|
|
|
let obj = entry
|
|
|
|
|
|
.as_object()
|
2025-10-28 11:58:57 +08:00
|
|
|
|
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目必须为 JSON 对象".into()))?;
|
2025-10-12 00:08:37 +08:00
|
|
|
|
let server = obj
|
|
|
|
|
|
.get("server")
|
2025-10-28 11:58:57 +08:00
|
|
|
|
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目缺少 server 字段".into()))?;
|
2025-10-12 00:08:37 +08:00
|
|
|
|
|
|
|
|
|
|
if !server.is_object() {
|
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
|
|
|
|
return Err(AppError::McpValidation(
|
|
|
|
|
|
"MCP 服务器 server 字段必须为 JSON 对象".into(),
|
|
|
|
|
|
));
|
2025-10-12 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(server.clone())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-09 21:08:42 +08:00
|
|
|
|
/// 返回已启用的 MCP 服务器(过滤 enabled==true)
|
|
|
|
|
|
fn collect_enabled_servers(cfg: &McpConfig) -> HashMap<String, Value> {
|
|
|
|
|
|
let mut out = HashMap::new();
|
2025-10-12 00:08:37 +08:00
|
|
|
|
for (id, entry) in cfg.servers.iter() {
|
|
|
|
|
|
let enabled = entry
|
2025-10-09 21:08:42 +08:00
|
|
|
|
.get("enabled")
|
|
|
|
|
|
.and_then(|v| v.as_bool())
|
|
|
|
|
|
.unwrap_or(false);
|
2025-10-12 00:08:37 +08:00
|
|
|
|
if !enabled {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
match extract_server_spec(entry) {
|
|
|
|
|
|
Ok(spec) => {
|
|
|
|
|
|
out.insert(id.clone(), spec);
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(err) => {
|
2025-11-12 10:47:34 +08:00
|
|
|
|
log::warn!("跳过无效的 MCP 条目 '{id}': {err}");
|
2025-10-12 00:08:37 +08:00
|
|
|
|
}
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
out
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
#[allow(dead_code)] // v3.7.0: 旧的分应用 API,保留用于未来可能的迁移
|
2025-10-12 16:21:32 +08:00
|
|
|
|
pub fn get_servers_snapshot_for(
|
|
|
|
|
|
config: &mut MultiAppConfig,
|
|
|
|
|
|
app: &AppType,
|
|
|
|
|
|
) -> (HashMap<String, Value>, usize) {
|
|
|
|
|
|
let normalized = normalize_servers_for(config, app);
|
2025-10-12 00:08:37 +08:00
|
|
|
|
let mut snapshot = config.mcp_for(app).servers.clone();
|
|
|
|
|
|
snapshot.retain(|id, value| {
|
|
|
|
|
|
let Some(obj) = value.as_object_mut() else {
|
2025-11-12 10:47:34 +08:00
|
|
|
|
log::warn!("跳过无效的 MCP 条目 '{id}': 必须为 JSON 对象");
|
2025-10-12 00:08:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
obj.entry(String::from("id")).or_insert(json!(id));
|
|
|
|
|
|
|
|
|
|
|
|
match validate_mcp_entry(value) {
|
|
|
|
|
|
Ok(()) => true,
|
|
|
|
|
|
Err(err) => {
|
2025-11-12 10:47:34 +08:00
|
|
|
|
log::error!("config.json 中存在无效的 MCP 条目 '{id}': {err}");
|
2025-10-12 00:08:37 +08:00
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-10-12 16:21:32 +08:00
|
|
|
|
(snapshot, normalized)
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
#[allow(dead_code)] // v3.7.0: 旧的分应用 API,保留用于未来可能的迁移
|
2025-10-09 22:02:56 +08:00
|
|
|
|
pub fn upsert_in_config_for(
|
|
|
|
|
|
config: &mut MultiAppConfig,
|
|
|
|
|
|
app: &AppType,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
spec: Value,
|
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<bool, AppError> {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
if id.trim().is_empty() {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
2025-10-12 16:21:32 +08:00
|
|
|
|
normalize_servers_for(config, app);
|
2025-10-12 00:08:37 +08:00
|
|
|
|
validate_mcp_entry(&spec)?;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
|
2025-10-12 00:08:37 +08:00
|
|
|
|
let mut entry_obj = spec
|
|
|
|
|
|
.as_object()
|
|
|
|
|
|
.cloned()
|
2025-10-28 11:58:57 +08:00
|
|
|
|
.ok_or_else(|| AppError::McpValidation("MCP 服务器条目必须为 JSON 对象".into()))?;
|
2025-10-12 00:08:37 +08:00
|
|
|
|
if let Some(existing_id) = entry_obj.get("id") {
|
|
|
|
|
|
let Some(existing_id_str) = existing_id.as_str() else {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
return Err(AppError::McpValidation("MCP 服务器 id 必须为字符串".into()));
|
2025-10-12 00:08:37 +08:00
|
|
|
|
};
|
|
|
|
|
|
if existing_id_str != 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
|
|
|
|
return Err(AppError::McpValidation(format!(
|
2025-11-12 10:47:34 +08:00
|
|
|
|
"MCP 服务器条目中的 id '{existing_id_str}' 与参数 id '{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
|
|
|
|
)));
|
2025-10-12 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
entry_obj.insert(String::from("id"), json!(id));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 00:08:37 +08:00
|
|
|
|
let value = Value::Object(entry_obj);
|
|
|
|
|
|
|
2025-10-09 22:02:56 +08:00
|
|
|
|
let servers = &mut config.mcp_for_mut(app).servers;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
let before = servers.get(id).cloned();
|
2025-10-12 00:08:37 +08:00
|
|
|
|
servers.insert(id.to_string(), value);
|
2025-10-09 21:08:42 +08:00
|
|
|
|
|
|
|
|
|
|
Ok(before.is_none())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
#[allow(dead_code)] // v3.7.0: 旧的分应用 API,保留用于未来可能的迁移
|
2025-10-12 16:21:32 +08:00
|
|
|
|
pub fn delete_in_config_for(
|
|
|
|
|
|
config: &mut MultiAppConfig,
|
|
|
|
|
|
app: &AppType,
|
|
|
|
|
|
id: &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
|
|
|
|
) -> Result<bool, AppError> {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
if id.trim().is_empty() {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
2025-10-12 16:21:32 +08:00
|
|
|
|
normalize_servers_for(config, app);
|
2025-10-09 22:02:56 +08:00
|
|
|
|
let existed = config.mcp_for_mut(app).servers.remove(id).is_some();
|
2025-10-09 21:08:42 +08:00
|
|
|
|
Ok(existed)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
#[allow(dead_code)] // v3.7.0: 旧的分应用 API,保留用于未来可能的迁移
|
refactor(backend): extract MCP service layer with snapshot isolation
Extract all MCP business logic from command layer into `services/mcp.rs`,
implementing snapshot isolation pattern to optimize lock granularity after
RwLock migration in Phase 5.
## Key Changes
### Service Layer (`services/mcp.rs`)
- Add `McpService` with 7 methods: `get_servers`, `upsert_server`,
`delete_server`, `set_enabled`, `sync_enabled`, `import_from_claude`,
`import_from_codex`
- Implement snapshot isolation: acquire write lock only for in-memory
modifications, clone config snapshot, release lock, then perform file I/O
with snapshot
- Use conditional cloning: only clone config when sync is actually needed
(e.g., when `enabled` flag is true or `sync_other_side` is requested)
### Command Layer (`commands/mcp.rs`)
- Reduce to thin wrappers: parse parameters and delegate to `McpService`
- Remove all `*_internal` and `*_test_hook` functions (-94 lines)
- Each command now 5-10 lines (parameter parsing + service call + error mapping)
### Core Logic Refactoring (`mcp.rs`)
- Rename `set_enabled_and_sync_for` → `set_enabled_flag_for`
- Remove file sync logic from low-level function, move sync responsibility
to service layer for better separation of concerns
### Test Adaptation (`tests/mcp_commands.rs`)
- Replace test hooks with direct `McpService` calls
- All 5 MCP integration tests pass
### Additional Fixes
- Add `Default` impl for `AppState` (clippy suggestion)
- Remove unnecessary auto-deref in `commands/provider.rs` and `lib.rs`
- Update Phase 4/5 progress in `BACKEND_REFACTOR_PLAN.md`
## Performance Impact
**Before**: Write lock held during file I/O (~10ms), blocking all readers
**After**: Write lock held only for memory ops (~100μs), file I/O lock-free
Estimated throughput improvement: ~2x in high-concurrency read scenarios
## Testing
- ✅ All tests pass: 5 MCP commands + 7 provider service tests
- ✅ Zero clippy warnings with `-D warnings`
- ✅ No behavioral changes, maintains original save semantics
Part of Phase 4 (Service Layer Abstraction) of backend refactoring roadmap.
2025-10-28 14:59:28 +08:00
|
|
|
|
/// 设置启用状态(不执行落盘或文件同步)
|
|
|
|
|
|
pub fn set_enabled_flag_for(
|
2025-10-09 22:02:56 +08:00
|
|
|
|
config: &mut MultiAppConfig,
|
|
|
|
|
|
app: &AppType,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
enabled: bool,
|
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<bool, AppError> {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
if id.trim().is_empty() {
|
2025-10-28 11:58:57 +08:00
|
|
|
|
return Err(AppError::InvalidInput("MCP 服务器 ID 不能为空".into()));
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
2025-10-12 16:21:32 +08:00
|
|
|
|
normalize_servers_for(config, app);
|
2025-10-09 22:02:56 +08:00
|
|
|
|
if let Some(spec) = config.mcp_for_mut(app).servers.get_mut(id) {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
// 写入 enabled 字段
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let mut obj = spec
|
|
|
|
|
|
.as_object()
|
|
|
|
|
|
.cloned()
|
2025-10-28 11:58:57 +08:00
|
|
|
|
.ok_or_else(|| AppError::McpValidation("MCP 服务器定义必须为 JSON 对象".into()))?;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
obj.insert("enabled".into(), json!(enabled));
|
|
|
|
|
|
*spec = Value::Object(obj);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 若不存在则直接返回 false
|
|
|
|
|
|
return Ok(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 将 config.json 中 enabled==true 的项投影写入 ~/.claude.json
|
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
|
|
|
|
pub fn sync_enabled_to_claude(config: &MultiAppConfig) -> Result<(), AppError> {
|
2025-10-09 22:02:56 +08:00
|
|
|
|
let enabled = collect_enabled_servers(&config.mcp.claude);
|
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
|
|
|
|
crate::claude_mcp::set_mcp_servers_map(&enabled)
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
/// 从 ~/.claude.json 导入 mcpServers 到统一结构(v3.7.0+)
|
|
|
|
|
|
/// 已存在的服务器将启用 Claude 应用,不覆盖其他字段和应用状态
|
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
|
|
|
|
pub fn import_from_claude(config: &mut MultiAppConfig) -> Result<usize, AppError> {
|
2025-11-14 23:33:54 +08:00
|
|
|
|
use crate::app_config::{McpApps, McpServer};
|
|
|
|
|
|
|
2025-10-09 21:08:42 +08:00
|
|
|
|
let text_opt = crate::claude_mcp::read_mcp_json()?;
|
|
|
|
|
|
let Some(text) = text_opt else { return Ok(0) };
|
2025-11-14 23:33:54 +08:00
|
|
|
|
|
2025-10-28 11:58:57 +08:00
|
|
|
|
let v: Value = serde_json::from_str(&text)
|
2025-11-12 10:47:34 +08:00
|
|
|
|
.map_err(|e| AppError::McpValidation(format!("解析 ~/.claude.json 失败: {e}")))?;
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let Some(map) = v.get("mcpServers").and_then(|x| x.as_object()) else {
|
2025-11-14 23:33:54 +08:00
|
|
|
|
return Ok(0);
|
2025-10-12 16:21:32 +08:00
|
|
|
|
};
|
2025-10-09 21:08:42 +08:00
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
// 确保新结构存在
|
|
|
|
|
|
if config.mcp.servers.is_none() {
|
|
|
|
|
|
config.mcp.servers = Some(HashMap::new());
|
|
|
|
|
|
}
|
|
|
|
|
|
let servers = config.mcp.servers.as_mut().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let mut changed = 0;
|
|
|
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
|
|
2025-10-09 21:08:42 +08:00
|
|
|
|
for (id, spec) in map.iter() {
|
2025-11-14 23:33:54 +08:00
|
|
|
|
// 校验:单项失败不中止,收集错误继续处理
|
|
|
|
|
|
if let Err(e) = validate_server_spec(spec) {
|
|
|
|
|
|
log::warn!("跳过无效 MCP 服务器 '{id}': {e}");
|
|
|
|
|
|
errors.push(format!("{id}: {e}"));
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(existing) = servers.get_mut(id) {
|
|
|
|
|
|
// 已存在:仅启用 Claude 应用
|
|
|
|
|
|
if !existing.apps.claude {
|
|
|
|
|
|
existing.apps.claude = true;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
changed += 1;
|
2025-11-14 23:33:54 +08:00
|
|
|
|
log::info!("MCP 服务器 '{id}' 已启用 Claude 应用");
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
2025-11-14 23:33:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 新建服务器:默认仅启用 Claude
|
|
|
|
|
|
servers.insert(
|
|
|
|
|
|
id.clone(),
|
|
|
|
|
|
McpServer {
|
|
|
|
|
|
id: id.clone(),
|
|
|
|
|
|
name: id.clone(),
|
|
|
|
|
|
server: spec.clone(),
|
|
|
|
|
|
apps: McpApps {
|
|
|
|
|
|
claude: true,
|
|
|
|
|
|
codex: false,
|
|
|
|
|
|
gemini: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
description: None,
|
|
|
|
|
|
homepage: None,
|
|
|
|
|
|
docs: None,
|
|
|
|
|
|
tags: Vec::new(),
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
changed += 1;
|
|
|
|
|
|
log::info!("导入新 MCP 服务器 '{id}'");
|
2025-10-09 21:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-14 23:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
|
|
log::warn!(
|
|
|
|
|
|
"导入完成,但有 {} 项失败: {:?}",
|
|
|
|
|
|
errors.len(),
|
|
|
|
|
|
errors
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-09 21:08:42 +08:00
|
|
|
|
Ok(changed)
|
|
|
|
|
|
}
|
2025-10-10 12:35:02 +08:00
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
/// 从 ~/.codex/config.toml 导入 MCP 到统一结构(v3.7.0+)
|
|
|
|
|
|
/// 支持两种 schema:[mcp.servers.<id>] 与 [mcp_servers.<id>]
|
|
|
|
|
|
/// 已存在的服务器将启用 Codex 应用,不覆盖其他字段和应用状态
|
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
|
|
|
|
pub fn import_from_codex(config: &mut MultiAppConfig) -> Result<usize, AppError> {
|
2025-11-14 23:33:54 +08:00
|
|
|
|
use crate::app_config::{McpApps, McpServer};
|
|
|
|
|
|
|
2025-10-10 14:59:02 +08:00
|
|
|
|
let text = crate::codex_config::read_and_validate_codex_config_text()?;
|
|
|
|
|
|
if text.trim().is_empty() {
|
|
|
|
|
|
return Ok(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-28 11:58:57 +08:00
|
|
|
|
let root: toml::Table = toml::from_str(&text)
|
2025-11-12 10:47:34 +08:00
|
|
|
|
.map_err(|e| AppError::McpValidation(format!("解析 ~/.codex/config.toml 失败: {e}")))?;
|
2025-10-10 14:59:02 +08:00
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
// 确保新结构存在
|
|
|
|
|
|
if config.mcp.servers.is_none() {
|
|
|
|
|
|
config.mcp.servers = Some(HashMap::new());
|
|
|
|
|
|
}
|
|
|
|
|
|
let servers = config.mcp.servers.as_mut().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let mut changed_total = 0usize;
|
|
|
|
|
|
|
2025-10-10 14:59:02 +08:00
|
|
|
|
// helper:处理一组 servers 表
|
|
|
|
|
|
let mut import_servers_tbl = |servers_tbl: &toml::value::Table| {
|
|
|
|
|
|
let mut changed = 0usize;
|
|
|
|
|
|
for (id, entry_val) in servers_tbl.iter() {
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let Some(entry_tbl) = entry_val.as_table() else {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
};
|
2025-10-10 14:59:02 +08:00
|
|
|
|
|
|
|
|
|
|
// type 缺省为 stdio
|
|
|
|
|
|
let typ = entry_tbl
|
|
|
|
|
|
.get("type")
|
|
|
|
|
|
.and_then(|v| v.as_str())
|
|
|
|
|
|
.unwrap_or("stdio");
|
|
|
|
|
|
|
|
|
|
|
|
// 构建 JSON 规范
|
|
|
|
|
|
let mut spec = serde_json::Map::new();
|
|
|
|
|
|
spec.insert("type".into(), json!(typ));
|
|
|
|
|
|
|
|
|
|
|
|
match typ {
|
|
|
|
|
|
"stdio" => {
|
|
|
|
|
|
if let Some(cmd) = entry_tbl.get("command").and_then(|v| v.as_str()) {
|
|
|
|
|
|
spec.insert("command".into(), json!(cmd));
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(args) = entry_tbl.get("args").and_then(|v| v.as_array()) {
|
|
|
|
|
|
let arr = args
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.filter_map(|x| x.as_str())
|
|
|
|
|
|
.map(|s| json!(s))
|
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
if !arr.is_empty() {
|
|
|
|
|
|
spec.insert("args".into(), serde_json::Value::Array(arr));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(cwd) = entry_tbl.get("cwd").and_then(|v| v.as_str()) {
|
|
|
|
|
|
if !cwd.trim().is_empty() {
|
|
|
|
|
|
spec.insert("cwd".into(), json!(cwd));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(env_tbl) = entry_tbl.get("env").and_then(|v| v.as_table()) {
|
|
|
|
|
|
let mut env_json = serde_json::Map::new();
|
|
|
|
|
|
for (k, v) in env_tbl.iter() {
|
|
|
|
|
|
if let Some(sv) = v.as_str() {
|
|
|
|
|
|
env_json.insert(k.clone(), json!(sv));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !env_json.is_empty() {
|
|
|
|
|
|
spec.insert("env".into(), serde_json::Value::Object(env_json));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-16 16:15:17 +08:00
|
|
|
|
"http" | "sse" => {
|
2025-10-10 14:59:02 +08:00
|
|
|
|
if let Some(url) = entry_tbl.get("url").and_then(|v| v.as_str()) {
|
|
|
|
|
|
spec.insert("url".into(), json!(url));
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(headers_tbl) = entry_tbl.get("headers").and_then(|v| v.as_table()) {
|
|
|
|
|
|
let mut headers_json = serde_json::Map::new();
|
|
|
|
|
|
for (k, v) in headers_tbl.iter() {
|
|
|
|
|
|
if let Some(sv) = v.as_str() {
|
|
|
|
|
|
headers_json.insert(k.clone(), json!(sv));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !headers_json.is_empty() {
|
|
|
|
|
|
spec.insert("headers".into(), serde_json::Value::Object(headers_json));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-14 23:33:54 +08:00
|
|
|
|
_ => {
|
|
|
|
|
|
log::warn!("跳过未知类型 '{typ}' 的 Codex MCP 项 '{id}'");
|
|
|
|
|
|
return changed;
|
|
|
|
|
|
}
|
2025-10-10 14:59:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let spec_v = serde_json::Value::Object(spec);
|
|
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
// 校验:单项失败继续处理
|
2025-10-12 00:08:37 +08:00
|
|
|
|
if let Err(e) = validate_server_spec(&spec_v) {
|
2025-11-12 10:47:34 +08:00
|
|
|
|
log::warn!("跳过无效 Codex MCP 项 '{id}': {e}");
|
2025-10-10 14:59:02 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
if let Some(existing) = servers.get_mut(id) {
|
|
|
|
|
|
// 已存在:仅启用 Codex 应用
|
|
|
|
|
|
if !existing.apps.codex {
|
|
|
|
|
|
existing.apps.codex = true;
|
2025-10-10 14:59:02 +08:00
|
|
|
|
changed += 1;
|
2025-11-14 23:33:54 +08:00
|
|
|
|
log::info!("MCP 服务器 '{id}' 已启用 Codex 应用");
|
2025-10-10 14:59:02 +08:00
|
|
|
|
}
|
2025-11-14 23:33:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 新建服务器:默认仅启用 Codex
|
|
|
|
|
|
servers.insert(
|
|
|
|
|
|
id.clone(),
|
|
|
|
|
|
McpServer {
|
|
|
|
|
|
id: id.clone(),
|
|
|
|
|
|
name: id.clone(),
|
|
|
|
|
|
server: spec_v,
|
|
|
|
|
|
apps: McpApps {
|
|
|
|
|
|
claude: false,
|
|
|
|
|
|
codex: true,
|
|
|
|
|
|
gemini: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
description: None,
|
|
|
|
|
|
homepage: None,
|
|
|
|
|
|
docs: None,
|
|
|
|
|
|
tags: Vec::new(),
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
changed += 1;
|
|
|
|
|
|
log::info!("导入新 MCP 服务器 '{id}'");
|
2025-10-10 14:59:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
changed
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 1) 处理 mcp.servers
|
|
|
|
|
|
if let Some(mcp_val) = root.get("mcp") {
|
|
|
|
|
|
if let Some(mcp_tbl) = mcp_val.as_table() {
|
|
|
|
|
|
if let Some(servers_val) = mcp_tbl.get("servers") {
|
|
|
|
|
|
if let Some(servers_tbl) = servers_val.as_table() {
|
|
|
|
|
|
changed_total += import_servers_tbl(servers_tbl);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2) 处理 mcp_servers
|
|
|
|
|
|
if let Some(servers_val) = root.get("mcp_servers") {
|
|
|
|
|
|
if let Some(servers_tbl) = servers_val.as_table() {
|
|
|
|
|
|
changed_total += import_servers_tbl(servers_tbl);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(changed_total)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 12:35:02 +08:00
|
|
|
|
/// 将 config.json 中 Codex 的 enabled==true 项以 TOML 形式写入 ~/.codex/config.toml 的 [mcp.servers]
|
|
|
|
|
|
/// 策略:
|
|
|
|
|
|
/// - 读取现有 config.toml;若语法无效则报错,不尝试覆盖
|
2025-10-11 16:35:51 +08:00
|
|
|
|
/// - 仅更新 `mcp.servers` 或 `mcp_servers` 子表,保留 `mcp` 其它键
|
|
|
|
|
|
/// - 仅写入启用项;无启用项时清理对应子表
|
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
|
|
|
|
pub fn sync_enabled_to_codex(config: &MultiAppConfig) -> Result<(), AppError> {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
use toml_edit::{DocumentMut, Item, Table};
|
2025-10-10 12:35:02 +08:00
|
|
|
|
|
|
|
|
|
|
// 1) 收集启用项(Codex 维度)
|
|
|
|
|
|
let enabled = collect_enabled_servers(&config.mcp.codex);
|
|
|
|
|
|
|
2025-10-29 23:52:46 +08:00
|
|
|
|
// 2) 读取现有 config.toml 文本;保持无效 TOML 的错误返回(不覆盖文件)
|
2025-10-10 12:35:02 +08:00
|
|
|
|
let base_text = crate::codex_config::read_and_validate_codex_config_text()?;
|
2025-10-29 23:52:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 3) 使用 toml_edit 解析(允许空文件)
|
|
|
|
|
|
let mut doc: DocumentMut = if base_text.trim().is_empty() {
|
|
|
|
|
|
DocumentMut::default()
|
2025-10-10 12:35:02 +08:00
|
|
|
|
} else {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
base_text
|
|
|
|
|
|
.parse::<DocumentMut>()
|
2025-11-12 10:47:34 +08:00
|
|
|
|
.map_err(|e| AppError::McpValidation(format!("解析 config.toml 失败: {e}")))?
|
2025-10-10 12:35:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-29 23:52:46 +08:00
|
|
|
|
enum Target {
|
|
|
|
|
|
McpServers, // 顶层 mcp_servers
|
|
|
|
|
|
McpDotServers, // mcp.servers
|
|
|
|
|
|
}
|
2025-10-11 16:35:51 +08:00
|
|
|
|
|
2025-10-29 23:52:46 +08:00
|
|
|
|
// 4) 选择目标风格:优先沿用既有子表;其次在 mcp 表下新建;最后退回顶层 mcp_servers
|
|
|
|
|
|
let has_mcp_dot_servers = doc
|
|
|
|
|
|
.get("mcp")
|
|
|
|
|
|
.and_then(|m| m.get("servers"))
|
|
|
|
|
|
.and_then(|s| s.as_table_like())
|
|
|
|
|
|
.is_some();
|
|
|
|
|
|
let has_mcp_servers = doc
|
|
|
|
|
|
.get("mcp_servers")
|
|
|
|
|
|
.and_then(|s| s.as_table_like())
|
|
|
|
|
|
.is_some();
|
|
|
|
|
|
let mcp_is_table = doc.get("mcp").and_then(|m| m.as_table_like()).is_some();
|
|
|
|
|
|
|
|
|
|
|
|
let target = if has_mcp_dot_servers {
|
|
|
|
|
|
Target::McpDotServers
|
|
|
|
|
|
} else if has_mcp_servers {
|
|
|
|
|
|
Target::McpServers
|
|
|
|
|
|
} else if mcp_is_table {
|
|
|
|
|
|
Target::McpDotServers
|
2025-10-10 12:35:02 +08:00
|
|
|
|
} else {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
Target::McpServers
|
|
|
|
|
|
};
|
2025-10-10 12:35:02 +08:00
|
|
|
|
|
2025-10-29 23:52:46 +08:00
|
|
|
|
// 构造目标 servers 表(稳定的键顺序)
|
|
|
|
|
|
let build_servers_table = || -> Table {
|
|
|
|
|
|
let mut servers = Table::new();
|
|
|
|
|
|
let mut ids: Vec<_> = enabled.keys().cloned().collect();
|
|
|
|
|
|
ids.sort();
|
|
|
|
|
|
for id in ids {
|
|
|
|
|
|
let spec = enabled.get(&id).expect("spec must exist");
|
|
|
|
|
|
let mut t = Table::new();
|
2025-10-12 16:21:32 +08:00
|
|
|
|
let typ = spec.get("type").and_then(|v| v.as_str()).unwrap_or("stdio");
|
2025-10-29 23:52:46 +08:00
|
|
|
|
t["type"] = toml_edit::value(typ);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
match typ {
|
|
|
|
|
|
"stdio" => {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
let cmd = spec.get("command").and_then(|v| v.as_str()).unwrap_or("");
|
|
|
|
|
|
t["command"] = toml_edit::value(cmd);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
if let Some(args) = spec.get("args").and_then(|v| v.as_array()) {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
let mut arr_v = toml_edit::Array::default();
|
|
|
|
|
|
for a in args.iter().filter_map(|x| x.as_str()) {
|
|
|
|
|
|
arr_v.push(a);
|
|
|
|
|
|
}
|
|
|
|
|
|
if !arr_v.is_empty() {
|
|
|
|
|
|
t["args"] = toml_edit::Item::Value(toml_edit::Value::Array(arr_v));
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(cwd) = spec.get("cwd").and_then(|v| v.as_str()) {
|
|
|
|
|
|
if !cwd.trim().is_empty() {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
t["cwd"] = toml_edit::value(cwd);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(env) = spec.get("env").and_then(|v| v.as_object()) {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
let mut env_tbl = Table::new();
|
2025-10-10 12:35:02 +08:00
|
|
|
|
for (k, v) in env.iter() {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
if let Some(s) = v.as_str() {
|
|
|
|
|
|
env_tbl[&k[..]] = toml_edit::value(s);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !env_tbl.is_empty() {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
t["env"] = Item::Table(env_tbl);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-16 16:15:17 +08:00
|
|
|
|
"http" | "sse" => {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
let url = spec.get("url").and_then(|v| v.as_str()).unwrap_or("");
|
|
|
|
|
|
t["url"] = toml_edit::value(url);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
if let Some(headers) = spec.get("headers").and_then(|v| v.as_object()) {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
let mut h_tbl = Table::new();
|
2025-10-10 12:35:02 +08:00
|
|
|
|
for (k, v) in headers.iter() {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
if let Some(s) = v.as_str() {
|
|
|
|
|
|
h_tbl[&k[..]] = toml_edit::value(s);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !h_tbl.is_empty() {
|
2025-10-29 23:52:46 +08:00
|
|
|
|
t["headers"] = Item::Table(h_tbl);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-10 14:59:02 +08:00
|
|
|
|
_ => {}
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
2025-10-29 23:52:46 +08:00
|
|
|
|
servers[&id[..]] = Item::Table(t);
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
2025-10-29 23:52:46 +08:00
|
|
|
|
servers
|
|
|
|
|
|
};
|
2025-10-10 12:35:02 +08:00
|
|
|
|
|
2025-10-29 23:52:46 +08:00
|
|
|
|
// 5) 应用更新:仅就地更新目标子表;避免改动其它键/注释/空白
|
|
|
|
|
|
if enabled.is_empty() {
|
|
|
|
|
|
// 无启用项:移除两种 servers 表(如果存在),但保留 mcp 其它字段
|
|
|
|
|
|
if let Some(mcp_item) = doc.get_mut("mcp") {
|
|
|
|
|
|
if let Some(tbl) = mcp_item.as_table_like_mut() {
|
|
|
|
|
|
tbl.remove("servers");
|
2025-10-11 16:35:51 +08:00
|
|
|
|
}
|
2025-10-29 23:52:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
doc.as_table_mut().remove("mcp_servers");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let servers_tbl = build_servers_table();
|
|
|
|
|
|
match target {
|
|
|
|
|
|
Target::McpDotServers => {
|
|
|
|
|
|
// 确保 mcp 为表
|
|
|
|
|
|
if doc.get("mcp").and_then(|m| m.as_table_like()).is_none() {
|
|
|
|
|
|
doc["mcp"] = Item::Table(Table::new());
|
|
|
|
|
|
}
|
|
|
|
|
|
doc["mcp"]["servers"] = Item::Table(servers_tbl);
|
|
|
|
|
|
// 去重:若存在顶层 mcp_servers,则移除以避免重复定义
|
|
|
|
|
|
doc.as_table_mut().remove("mcp_servers");
|
2025-10-11 16:35:51 +08:00
|
|
|
|
}
|
2025-10-29 23:52:46 +08:00
|
|
|
|
Target::McpServers => {
|
|
|
|
|
|
doc["mcp_servers"] = Item::Table(servers_tbl);
|
|
|
|
|
|
// 去重:若存在 mcp.servers,则移除该子表,保留 mcp 其它键
|
|
|
|
|
|
if let Some(mcp_item) = doc.get_mut("mcp") {
|
|
|
|
|
|
if let Some(tbl) = mcp_item.as_table_like_mut() {
|
|
|
|
|
|
tbl.remove("servers");
|
2025-10-11 16:35:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-10 14:59:02 +08:00
|
|
|
|
}
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-29 23:52:46 +08:00
|
|
|
|
// 6) 写回(仅改 TOML,不触碰 auth.json);toml_edit 会尽量保留未改区域的注释/空白/顺序
|
|
|
|
|
|
let new_text = doc.to_string();
|
2025-10-10 12:35:02 +08:00
|
|
|
|
let path = crate::codex_config::get_codex_config_path();
|
|
|
|
|
|
crate::config::write_text_file(&path, &new_text)?;
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
2025-11-14 10:02:27 +08:00
|
|
|
|
|
|
|
|
|
|
/// 将 config.json 中 enabled==true 的项投影写入 ~/.gemini/settings.json
|
|
|
|
|
|
pub fn sync_enabled_to_gemini(config: &MultiAppConfig) -> Result<(), AppError> {
|
|
|
|
|
|
let enabled = collect_enabled_servers(&config.mcp.gemini);
|
|
|
|
|
|
crate::gemini_mcp::set_mcp_servers_map(&enabled)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
/// 从 ~/.gemini/settings.json 导入 mcpServers 到统一结构(v3.7.0+)
|
|
|
|
|
|
/// 已存在的服务器将启用 Gemini 应用,不覆盖其他字段和应用状态
|
2025-11-14 10:02:27 +08:00
|
|
|
|
pub fn import_from_gemini(config: &mut MultiAppConfig) -> Result<usize, AppError> {
|
2025-11-14 23:33:54 +08:00
|
|
|
|
use crate::app_config::{McpApps, McpServer};
|
|
|
|
|
|
|
2025-11-14 10:02:27 +08:00
|
|
|
|
let text_opt = crate::gemini_mcp::read_mcp_json()?;
|
|
|
|
|
|
let Some(text) = text_opt else { return Ok(0) };
|
2025-11-14 23:33:54 +08:00
|
|
|
|
|
2025-11-14 10:02:27 +08:00
|
|
|
|
let v: Value = serde_json::from_str(&text)
|
|
|
|
|
|
.map_err(|e| AppError::McpValidation(format!("解析 ~/.gemini/settings.json 失败: {e}")))?;
|
|
|
|
|
|
let Some(map) = v.get("mcpServers").and_then(|x| x.as_object()) else {
|
2025-11-14 23:33:54 +08:00
|
|
|
|
return Ok(0);
|
2025-11-14 10:02:27 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-14 23:33:54 +08:00
|
|
|
|
// 确保新结构存在
|
|
|
|
|
|
if config.mcp.servers.is_none() {
|
|
|
|
|
|
config.mcp.servers = Some(HashMap::new());
|
|
|
|
|
|
}
|
|
|
|
|
|
let servers = config.mcp.servers.as_mut().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let mut changed = 0;
|
|
|
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
|
|
2025-11-14 10:02:27 +08:00
|
|
|
|
for (id, spec) in map.iter() {
|
2025-11-14 23:33:54 +08:00
|
|
|
|
// 校验:单项失败不中止,收集错误继续处理
|
|
|
|
|
|
if let Err(e) = validate_server_spec(spec) {
|
|
|
|
|
|
log::warn!("跳过无效 MCP 服务器 '{id}': {e}");
|
|
|
|
|
|
errors.push(format!("{id}: {e}"));
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(existing) = servers.get_mut(id) {
|
|
|
|
|
|
// 已存在:仅启用 Gemini 应用
|
|
|
|
|
|
if !existing.apps.gemini {
|
|
|
|
|
|
existing.apps.gemini = true;
|
2025-11-14 10:02:27 +08:00
|
|
|
|
changed += 1;
|
2025-11-14 23:33:54 +08:00
|
|
|
|
log::info!("MCP 服务器 '{id}' 已启用 Gemini 应用");
|
2025-11-14 10:02:27 +08:00
|
|
|
|
}
|
2025-11-14 23:33:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 新建服务器:默认仅启用 Gemini
|
|
|
|
|
|
servers.insert(
|
|
|
|
|
|
id.clone(),
|
|
|
|
|
|
McpServer {
|
|
|
|
|
|
id: id.clone(),
|
|
|
|
|
|
name: id.clone(),
|
|
|
|
|
|
server: spec.clone(),
|
|
|
|
|
|
apps: McpApps {
|
|
|
|
|
|
claude: false,
|
|
|
|
|
|
codex: false,
|
|
|
|
|
|
gemini: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
description: None,
|
|
|
|
|
|
homepage: None,
|
|
|
|
|
|
docs: None,
|
|
|
|
|
|
tags: Vec::new(),
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
changed += 1;
|
|
|
|
|
|
log::info!("导入新 MCP 服务器 '{id}'");
|
2025-11-14 10:02:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-14 23:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
|
|
log::warn!(
|
|
|
|
|
|
"导入完成,但有 {} 项失败: {:?}",
|
|
|
|
|
|
errors.len(),
|
|
|
|
|
|
errors
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 10:02:27 +08:00
|
|
|
|
Ok(changed)
|
|
|
|
|
|
}
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
// v3.7.0 新增:单个服务器同步和删除函数
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/// 将单个 MCP 服务器同步到 Claude live 配置
|
|
|
|
|
|
pub fn sync_single_server_to_claude(
|
|
|
|
|
|
_config: &MultiAppConfig,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
server_spec: &Value,
|
|
|
|
|
|
) -> Result<(), AppError> {
|
|
|
|
|
|
// 读取现有的 MCP 配置
|
|
|
|
|
|
let current = crate::claude_mcp::read_mcp_servers_map()?;
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新的 HashMap,包含现有的所有服务器 + 当前要同步的服务器
|
|
|
|
|
|
let mut updated = current;
|
|
|
|
|
|
updated.insert(id.to_string(), server_spec.clone());
|
|
|
|
|
|
|
|
|
|
|
|
// 写回
|
|
|
|
|
|
crate::claude_mcp::set_mcp_servers_map(&updated)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 从 Claude live 配置中移除单个 MCP 服务器
|
|
|
|
|
|
pub fn remove_server_from_claude(id: &str) -> Result<(), AppError> {
|
|
|
|
|
|
// 读取现有的 MCP 配置
|
|
|
|
|
|
let mut current = crate::claude_mcp::read_mcp_servers_map()?;
|
|
|
|
|
|
|
|
|
|
|
|
// 移除指定服务器
|
|
|
|
|
|
current.remove(id);
|
|
|
|
|
|
|
|
|
|
|
|
// 写回
|
|
|
|
|
|
crate::claude_mcp::set_mcp_servers_map(¤t)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 12:57:14 +08:00
|
|
|
|
/// Helper: 将 JSON MCP 服务器规范转换为 toml_edit::Table
|
|
|
|
|
|
fn json_server_to_toml_table(spec: &Value) -> Result<toml_edit::Table, AppError> {
|
|
|
|
|
|
use toml_edit::{Array, Item, Table};
|
|
|
|
|
|
|
|
|
|
|
|
let mut t = Table::new();
|
|
|
|
|
|
let typ = spec.get("type").and_then(|v| v.as_str()).unwrap_or("stdio");
|
|
|
|
|
|
t["type"] = toml_edit::value(typ);
|
|
|
|
|
|
|
|
|
|
|
|
match typ {
|
|
|
|
|
|
"stdio" => {
|
|
|
|
|
|
let cmd = spec.get("command").and_then(|v| v.as_str()).unwrap_or("");
|
|
|
|
|
|
t["command"] = toml_edit::value(cmd);
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(args) = spec.get("args").and_then(|v| v.as_array()) {
|
|
|
|
|
|
let mut arr_v = Array::default();
|
|
|
|
|
|
for a in args.iter().filter_map(|x| x.as_str()) {
|
|
|
|
|
|
arr_v.push(a);
|
|
|
|
|
|
}
|
|
|
|
|
|
if !arr_v.is_empty() {
|
|
|
|
|
|
t["args"] = Item::Value(toml_edit::Value::Array(arr_v));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(cwd) = spec.get("cwd").and_then(|v| v.as_str()) {
|
|
|
|
|
|
if !cwd.trim().is_empty() {
|
|
|
|
|
|
t["cwd"] = toml_edit::value(cwd);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(env) = spec.get("env").and_then(|v| v.as_object()) {
|
|
|
|
|
|
let mut env_tbl = Table::new();
|
|
|
|
|
|
for (k, v) in env.iter() {
|
|
|
|
|
|
if let Some(s) = v.as_str() {
|
|
|
|
|
|
env_tbl[&k[..]] = toml_edit::value(s);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !env_tbl.is_empty() {
|
|
|
|
|
|
t["env"] = Item::Table(env_tbl);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-16 16:15:17 +08:00
|
|
|
|
"http" | "sse" => {
|
2025-11-14 12:57:14 +08:00
|
|
|
|
let url = spec.get("url").and_then(|v| v.as_str()).unwrap_or("");
|
|
|
|
|
|
t["url"] = toml_edit::value(url);
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(headers) = spec.get("headers").and_then(|v| v.as_object()) {
|
|
|
|
|
|
let mut h_tbl = Table::new();
|
|
|
|
|
|
for (k, v) in headers.iter() {
|
|
|
|
|
|
if let Some(s) = v.as_str() {
|
|
|
|
|
|
h_tbl[&k[..]] = toml_edit::value(s);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !h_tbl.is_empty() {
|
|
|
|
|
|
t["headers"] = Item::Table(h_tbl);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(t)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 12:51:24 +08:00
|
|
|
|
/// 将单个 MCP 服务器同步到 Codex live 配置
|
|
|
|
|
|
pub fn sync_single_server_to_codex(
|
|
|
|
|
|
_config: &MultiAppConfig,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
server_spec: &Value,
|
|
|
|
|
|
) -> Result<(), AppError> {
|
2025-11-14 12:57:14 +08:00
|
|
|
|
use toml_edit::Item;
|
|
|
|
|
|
|
2025-11-14 12:51:24 +08:00
|
|
|
|
// 读取现有的 config.toml
|
2025-11-14 12:57:14 +08:00
|
|
|
|
let config_path = crate::codex_config::get_codex_config_path();
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
let mut doc = if config_path.exists() {
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
let content =
|
|
|
|
|
|
std::fs::read_to_string(&config_path).map_err(|e| AppError::io(&config_path, e))?;
|
2025-11-14 12:51:24 +08:00
|
|
|
|
content
|
|
|
|
|
|
.parse::<toml_edit::DocumentMut>()
|
|
|
|
|
|
.map_err(|e| AppError::McpValidation(format!("解析 Codex config.toml 失败: {e}")))?
|
|
|
|
|
|
} else {
|
|
|
|
|
|
toml_edit::DocumentMut::new()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 确保 [mcp] 表存在
|
|
|
|
|
|
if !doc.contains_key("mcp") {
|
|
|
|
|
|
doc["mcp"] = toml_edit::table();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 确保 [mcp.servers] 子表存在
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
if doc["mcp"]
|
2025-11-14 12:51:24 +08:00
|
|
|
|
.as_table()
|
|
|
|
|
|
.and_then(|t| t.get("servers"))
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
.is_none()
|
2025-11-14 12:51:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
doc["mcp"]["servers"] = toml_edit::table();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 12:57:14 +08:00
|
|
|
|
// 将 JSON 服务器规范转换为 TOML 表
|
|
|
|
|
|
let toml_table = json_server_to_toml_table(server_spec)?;
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
2025-11-14 12:57:14 +08:00
|
|
|
|
doc["mcp"]["servers"][id] = Item::Table(toml_table);
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
// 写回文件
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
std::fs::write(&config_path, doc.to_string()).map_err(|e| AppError::io(&config_path, e))?;
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 从 Codex live 配置中移除单个 MCP 服务器
|
|
|
|
|
|
pub fn remove_server_from_codex(id: &str) -> Result<(), AppError> {
|
2025-11-14 12:57:14 +08:00
|
|
|
|
let config_path = crate::codex_config::get_codex_config_path();
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
if !config_path.exists() {
|
|
|
|
|
|
return Ok(()); // 文件不存在,无需删除
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
let content =
|
|
|
|
|
|
std::fs::read_to_string(&config_path).map_err(|e| AppError::io(&config_path, e))?;
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
let mut doc = content
|
|
|
|
|
|
.parse::<toml_edit::DocumentMut>()
|
|
|
|
|
|
.map_err(|e| AppError::McpValidation(format!("解析 Codex config.toml 失败: {e}")))?;
|
|
|
|
|
|
|
|
|
|
|
|
// 从 [mcp.servers] 中删除
|
|
|
|
|
|
if let Some(mcp_table) = doc.get_mut("mcp").and_then(|t| t.as_table_mut()) {
|
|
|
|
|
|
if let Some(servers) = mcp_table.get_mut("servers").and_then(|s| s.as_table_mut()) {
|
|
|
|
|
|
servers.remove(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 写回文件
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
This commit finalizes the v3.7.0 unified MCP architecture migration by
removing all deprecated code paths and eliminating compiler warnings.
Frontend Changes (~950 lines removed):
- Remove deprecated components: McpPanel, McpListItem, McpToggle
- Remove deprecated hook: useMcpActions
- Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers
- Simplify McpFormModal by removing dual-mode logic (unified/legacy)
- Remove syncOtherSide checkbox and conflict detection
- Clean up unused imports and state variables
- Delete associated test files
Backend Changes (~400 lines cleaned):
- Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_*
- Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server
- Add #[allow(deprecated)] to compatibility layer commands
- Add #[allow(dead_code)] to legacy helper functions for future migration
- Simplify boolean expression in mcp.rs per Clippy suggestion
API Deprecation:
- Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.)
- Preserve backward compatibility for v3.x, planned removal in v4.0
Verification:
- ✅ Zero TypeScript errors (pnpm typecheck)
- ✅ Zero Clippy warnings (cargo clippy)
- ✅ All code formatted (prettier + cargo fmt)
- ✅ Builds successfully
Total cleanup: ~1,350 lines of code removed/marked
Breaking changes: None (all legacy APIs still functional)
2025-11-14 22:43:25 +08:00
|
|
|
|
std::fs::write(&config_path, doc.to_string()).map_err(|e| AppError::io(&config_path, e))?;
|
2025-11-14 12:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 将单个 MCP 服务器同步到 Gemini live 配置
|
|
|
|
|
|
pub fn sync_single_server_to_gemini(
|
|
|
|
|
|
_config: &MultiAppConfig,
|
|
|
|
|
|
id: &str,
|
|
|
|
|
|
server_spec: &Value,
|
|
|
|
|
|
) -> Result<(), AppError> {
|
|
|
|
|
|
// 读取现有的 MCP 配置
|
|
|
|
|
|
let current = crate::gemini_mcp::read_mcp_servers_map()?;
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新的 HashMap,包含现有的所有服务器 + 当前要同步的服务器
|
|
|
|
|
|
let mut updated = current;
|
|
|
|
|
|
updated.insert(id.to_string(), server_spec.clone());
|
|
|
|
|
|
|
|
|
|
|
|
// 写回
|
|
|
|
|
|
crate::gemini_mcp::set_mcp_servers_map(&updated)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 从 Gemini live 配置中移除单个 MCP 服务器
|
|
|
|
|
|
pub fn remove_server_from_gemini(id: &str) -> Result<(), AppError> {
|
|
|
|
|
|
// 读取现有的 MCP 配置
|
|
|
|
|
|
let mut current = crate::gemini_mcp::read_mcp_servers_map()?;
|
|
|
|
|
|
|
|
|
|
|
|
// 移除指定服务器
|
|
|
|
|
|
current.remove(id);
|
|
|
|
|
|
|
|
|
|
|
|
// 写回
|
|
|
|
|
|
crate::gemini_mcp::set_mcp_servers_map(¤t)
|
|
|
|
|
|
}
|