refactor(api): simplify app type parameter handling to single required parameter
Replace the previous dual-parameter approach (app_type/app/appType) with a single required `app: String` parameter across all Tauri commands. This change: - Introduces unified `parse_app()` helper replacing complex `resolve_app_type()` logic - Updates all backend commands in config, mcp, and provider modules - Aligns frontend API calls to use consistent `app` parameter naming - Simplifies MSW test handlers by removing optional parameter handling This improves API clarity and reduces parameter ambiguity while maintaining backward compatibility through error handling.
This commit is contained in:
@@ -15,18 +15,17 @@ pub async fn get_claude_config_status() -> Result<ConfigStatus, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 获取应用配置状态
|
/// 获取应用配置状态
|
||||||
#[tauri::command]
|
fn parse_app(app: String) -> Result<AppType, String> {
|
||||||
pub async fn get_config_status(
|
match app.to_lowercase().as_str() {
|
||||||
app_type: Option<AppType>,
|
"claude" => Ok(AppType::Claude),
|
||||||
app: Option<String>,
|
"codex" => Ok(AppType::Codex),
|
||||||
appType: Option<String>,
|
other => Err(format!("unsupported app: {}", other)),
|
||||||
) -> Result<ConfigStatus, String> {
|
}
|
||||||
let app = app_type
|
}
|
||||||
.or_else(|| app.as_deref().map(|s| s.into()))
|
|
||||||
.or_else(|| appType.as_deref().map(|s| s.into()))
|
|
||||||
.unwrap_or(AppType::Claude);
|
|
||||||
|
|
||||||
match app {
|
#[tauri::command]
|
||||||
|
pub async fn get_config_status(app: String) -> Result<ConfigStatus, String> {
|
||||||
|
match parse_app(app)? {
|
||||||
AppType::Claude => Ok(config::get_claude_config_status()),
|
AppType::Claude => Ok(config::get_claude_config_status()),
|
||||||
AppType::Codex => {
|
AppType::Codex => {
|
||||||
let auth_path = codex_config::get_codex_auth_path();
|
let auth_path = codex_config::get_codex_auth_path();
|
||||||
@@ -48,17 +47,8 @@ pub async fn get_claude_code_config_path() -> Result<String, String> {
|
|||||||
|
|
||||||
/// 获取当前生效的配置目录
|
/// 获取当前生效的配置目录
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_config_dir(
|
pub async fn get_config_dir(app: String) -> Result<String, String> {
|
||||||
app_type: Option<AppType>,
|
let dir = match parse_app(app)? {
|
||||||
app: Option<String>,
|
|
||||||
appType: Option<String>,
|
|
||||||
) -> Result<String, String> {
|
|
||||||
let app = app_type
|
|
||||||
.or_else(|| app.as_deref().map(|s| s.into()))
|
|
||||||
.or_else(|| appType.as_deref().map(|s| s.into()))
|
|
||||||
.unwrap_or(AppType::Claude);
|
|
||||||
|
|
||||||
let dir = match app {
|
|
||||||
AppType::Claude => config::get_claude_config_dir(),
|
AppType::Claude => config::get_claude_config_dir(),
|
||||||
AppType::Codex => codex_config::get_codex_config_dir(),
|
AppType::Codex => codex_config::get_codex_config_dir(),
|
||||||
};
|
};
|
||||||
@@ -68,18 +58,8 @@ pub async fn get_config_dir(
|
|||||||
|
|
||||||
/// 打开配置文件夹
|
/// 打开配置文件夹
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn open_config_folder(
|
pub async fn open_config_folder(handle: AppHandle, app: String) -> Result<bool, String> {
|
||||||
handle: AppHandle,
|
let config_dir = match parse_app(app)? {
|
||||||
app_type: Option<AppType>,
|
|
||||||
app: Option<String>,
|
|
||||||
appType: Option<String>,
|
|
||||||
) -> Result<bool, String> {
|
|
||||||
let app_type = app_type
|
|
||||||
.or_else(|| app.as_deref().map(|s| s.into()))
|
|
||||||
.or_else(|| appType.as_deref().map(|s| s.into()))
|
|
||||||
.unwrap_or(AppType::Claude);
|
|
||||||
|
|
||||||
let config_dir = match app_type {
|
|
||||||
AppType::Claude => config::get_claude_config_dir(),
|
AppType::Claude => config::get_claude_config_dir(),
|
||||||
AppType::Codex => codex_config::get_codex_config_dir(),
|
AppType::Codex => codex_config::get_codex_config_dir(),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -47,15 +47,23 @@ pub struct McpConfigResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 获取 MCP 配置(来自 ~/.cc-switch/config.json)
|
/// 获取 MCP 配置(来自 ~/.cc-switch/config.json)
|
||||||
|
fn parse_app(app: String) -> Result<AppType, String> {
|
||||||
|
match app.to_lowercase().as_str() {
|
||||||
|
"claude" => Ok(AppType::Claude),
|
||||||
|
"codex" => Ok(AppType::Codex),
|
||||||
|
other => Err(format!("unsupported app: {}", other)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_mcp_config(
|
pub async fn get_mcp_config(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: Option<String>,
|
app: String,
|
||||||
) -> Result<McpConfigResponse, String> {
|
) -> Result<McpConfigResponse, String> {
|
||||||
let config_path = crate::config::get_app_config_path()
|
let config_path = crate::config::get_app_config_path()
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
let app_ty = AppType::from(app.as_deref().unwrap_or("claude"));
|
let app_ty = parse_app(app)?;
|
||||||
let servers = McpService::get_servers(&state, app_ty).map_err(|e| e.to_string())?;
|
let servers = McpService::get_servers(&state, app_ty).map_err(|e| e.to_string())?;
|
||||||
Ok(McpConfigResponse {
|
Ok(McpConfigResponse {
|
||||||
config_path,
|
config_path,
|
||||||
@@ -67,12 +75,12 @@ pub async fn get_mcp_config(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn upsert_mcp_server_in_config(
|
pub async fn upsert_mcp_server_in_config(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: Option<String>,
|
app: String,
|
||||||
id: String,
|
id: String,
|
||||||
spec: serde_json::Value,
|
spec: serde_json::Value,
|
||||||
sync_other_side: Option<bool>,
|
sync_other_side: Option<bool>,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_ty = AppType::from(app.as_deref().unwrap_or("claude"));
|
let app_ty = parse_app(app)?;
|
||||||
McpService::upsert_server(&state, app_ty, &id, spec, sync_other_side.unwrap_or(false))
|
McpService::upsert_server(&state, app_ty, &id, spec, sync_other_side.unwrap_or(false))
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
@@ -81,10 +89,10 @@ pub async fn upsert_mcp_server_in_config(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn delete_mcp_server_in_config(
|
pub async fn delete_mcp_server_in_config(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: Option<String>,
|
app: String,
|
||||||
id: String,
|
id: String,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_ty = AppType::from(app.as_deref().unwrap_or("claude"));
|
let app_ty = parse_app(app)?;
|
||||||
McpService::delete_server(&state, app_ty, &id).map_err(|e| e.to_string())
|
McpService::delete_server(&state, app_ty, &id).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,11 +100,11 @@ pub async fn delete_mcp_server_in_config(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn set_mcp_enabled(
|
pub async fn set_mcp_enabled(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: Option<String>,
|
app: String,
|
||||||
id: String,
|
id: String,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_ty = AppType::from(app.as_deref().unwrap_or("claude"));
|
let app_ty = parse_app(app)?;
|
||||||
McpService::set_enabled(&state, app_ty, &id, enabled).map_err(|e| e.to_string())
|
McpService::set_enabled(&state, app_ty, &id, enabled).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,32 +11,11 @@ fn missing_param(param: &str) -> String {
|
|||||||
format!("缺少 {} 参数 (Missing {} parameter)", param, param)
|
format!("缺少 {} 参数 (Missing {} parameter)", param, param)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_app_type(app_type: Option<AppType>, app: Option<String>) -> Result<AppType, String> {
|
fn parse_app(app: String) -> Result<AppType, String> {
|
||||||
match (app_type, app) {
|
match app.to_lowercase().as_str() {
|
||||||
(Some(at), None) => Ok(at),
|
"claude" => Ok(AppType::Claude),
|
||||||
(None, Some(a)) => match a.to_lowercase().as_str() {
|
"codex" => Ok(AppType::Codex),
|
||||||
"claude" => Ok(AppType::Claude),
|
other => Err(format!("unsupported app: {}", other)),
|
||||||
"codex" => Ok(AppType::Codex),
|
|
||||||
other => Err(format!(
|
|
||||||
"params.invalid: 无效的 app 值: {} (Invalid app)",
|
|
||||||
other
|
|
||||||
)),
|
|
||||||
},
|
|
||||||
(Some(at), Some(a)) => {
|
|
||||||
let a_norm = a.to_lowercase();
|
|
||||||
let at_norm = at.as_str().to_string();
|
|
||||||
if a_norm == at_norm {
|
|
||||||
// 接受但提示:建议仅传 app
|
|
||||||
log::warn!("params.deprecated: 同时传递 app 与 app_type,建议仅使用 app");
|
|
||||||
Ok(at)
|
|
||||||
} else {
|
|
||||||
Err(format!(
|
|
||||||
"params.conflict: app 与 app_type 冲突 (app={}, app_type={})",
|
|
||||||
a_norm, at_norm
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(None, None) => Err(missing_param("app")),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,10 +23,9 @@ fn resolve_app_type(app_type: Option<AppType>, app: Option<String>) -> Result<Ap
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_providers(
|
pub fn get_providers(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
) -> Result<HashMap<String, Provider>, String> {
|
) -> Result<HashMap<String, Provider>, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
ProviderService::list(state.inner(), app_type).map_err(|e| e.to_string())
|
ProviderService::list(state.inner(), app_type).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,10 +33,9 @@ pub fn get_providers(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_current_provider(
|
pub fn get_current_provider(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
) -> Result<String, String> {
|
) -> Result<String, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
ProviderService::current(state.inner(), app_type).map_err(|e| e.to_string())
|
ProviderService::current(state.inner(), app_type).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,11 +43,10 @@ pub fn get_current_provider(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn add_provider(
|
pub fn add_provider(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
provider: Provider,
|
provider: Provider,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
ProviderService::add(state.inner(), app_type, provider).map_err(|e| e.to_string())
|
ProviderService::add(state.inner(), app_type, provider).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,11 +54,10 @@ pub fn add_provider(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn update_provider(
|
pub fn update_provider(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
provider: Provider,
|
provider: Provider,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
ProviderService::update(state.inner(), app_type, provider).map_err(|e| e.to_string())
|
ProviderService::update(state.inner(), app_type, provider).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,11 +65,10 @@ pub fn update_provider(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn delete_provider(
|
pub fn delete_provider(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
id: String,
|
id: String,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
ProviderService::delete(state.inner(), app_type, &id)
|
ProviderService::delete(state.inner(), app_type, &id)
|
||||||
.map(|_| true)
|
.map(|_| true)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
@@ -117,11 +91,10 @@ pub fn switch_provider_test_hook(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn switch_provider(
|
pub fn switch_provider(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
id: String,
|
id: String,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
switch_provider_internal(&state, app_type, &id)
|
switch_provider_internal(&state, app_type, &id)
|
||||||
.map(|_| true)
|
.map(|_| true)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
@@ -143,10 +116,9 @@ pub fn import_default_config_test_hook(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn import_default_config(
|
pub fn import_default_config(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
import_default_config_internal(&state, app_type)
|
import_default_config_internal(&state, app_type)
|
||||||
.map(|_| true)
|
.map(|_| true)
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
@@ -157,11 +129,10 @@ pub fn import_default_config(
|
|||||||
pub async fn query_provider_usage(
|
pub async fn query_provider_usage(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
provider_id: Option<String>,
|
provider_id: Option<String>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
) -> Result<crate::provider::UsageResult, String> {
|
) -> Result<crate::provider::UsageResult, String> {
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
ProviderService::query_usage(state.inner(), app_type, &provider_id)
|
ProviderService::query_usage(state.inner(), app_type, &provider_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
@@ -169,9 +140,8 @@ pub async fn query_provider_usage(
|
|||||||
|
|
||||||
/// 读取当前生效的配置内容
|
/// 读取当前生效的配置内容
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn read_live_provider_settings(app_type: Option<AppType>) -> Result<serde_json::Value, String> {
|
pub fn read_live_provider_settings(app: String) -> Result<serde_json::Value, String> {
|
||||||
let app_type = app_type.unwrap_or(AppType::Claude);
|
let app_type = parse_app(app)?;
|
||||||
|
|
||||||
ProviderService::read_live_settings(app_type).map_err(|e| e.to_string())
|
ProviderService::read_live_settings(app_type).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,11 +160,10 @@ pub async fn test_api_endpoints(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_custom_endpoints(
|
pub fn get_custom_endpoints(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
provider_id: Option<String>,
|
provider_id: Option<String>,
|
||||||
) -> Result<Vec<crate::settings::CustomEndpoint>, String> {
|
) -> Result<Vec<crate::settings::CustomEndpoint>, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
||||||
ProviderService::get_custom_endpoints(state.inner(), app_type, &provider_id)
|
ProviderService::get_custom_endpoints(state.inner(), app_type, &provider_id)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
@@ -204,12 +173,11 @@ pub fn get_custom_endpoints(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn add_custom_endpoint(
|
pub fn add_custom_endpoint(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
provider_id: Option<String>,
|
provider_id: Option<String>,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
||||||
ProviderService::add_custom_endpoint(state.inner(), app_type, &provider_id, url)
|
ProviderService::add_custom_endpoint(state.inner(), app_type, &provider_id, url)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
@@ -219,12 +187,11 @@ pub fn add_custom_endpoint(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn remove_custom_endpoint(
|
pub fn remove_custom_endpoint(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
provider_id: Option<String>,
|
provider_id: Option<String>,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
||||||
ProviderService::remove_custom_endpoint(state.inner(), app_type, &provider_id, url)
|
ProviderService::remove_custom_endpoint(state.inner(), app_type, &provider_id, url)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
@@ -234,12 +201,11 @@ pub fn remove_custom_endpoint(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn update_endpoint_last_used(
|
pub fn update_endpoint_last_used(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
provider_id: Option<String>,
|
provider_id: Option<String>,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
||||||
ProviderService::update_endpoint_last_used(state.inner(), app_type, &provider_id, url)
|
ProviderService::update_endpoint_last_used(state.inner(), app_type, &provider_id, url)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
@@ -249,10 +215,9 @@ pub fn update_endpoint_last_used(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn update_providers_sort_order(
|
pub fn update_providers_sort_order(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app_type: Option<AppType>,
|
app: String,
|
||||||
app: Option<String>,
|
|
||||||
updates: Vec<ProviderSortUpdate>,
|
updates: Vec<ProviderSortUpdate>,
|
||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
let app_type = resolve_app_type(app_type, app)?;
|
let app_type = parse_app(app)?;
|
||||||
ProviderService::update_sort_order(state.inner(), app_type, updates).map_err(|e| e.to_string())
|
ProviderService::update_sort_order(state.inner(), app_type, updates).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -267,12 +267,7 @@ fn switch_provider_internal(
|
|||||||
let app_type_str = app_type.as_str().to_string();
|
let app_type_str = app_type.as_str().to_string();
|
||||||
let provider_id_clone = provider_id.clone();
|
let provider_id_clone = provider_id.clone();
|
||||||
|
|
||||||
crate::commands::switch_provider(
|
crate::commands::switch_provider(app_state.clone(), app_type_str.clone(), provider_id)
|
||||||
app_state.clone(),
|
|
||||||
Some(app_type),
|
|
||||||
Some(app_type_str.clone()),
|
|
||||||
provider_id,
|
|
||||||
)
|
|
||||||
.map_err(AppError::Message)?;
|
.map_err(AppError::Message)?;
|
||||||
|
|
||||||
// 切换成功后重新创建托盘菜单
|
// 切换成功后重新创建托盘菜单
|
||||||
|
|||||||
@@ -31,11 +31,11 @@ export const settingsApi = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async getConfigDir(appType: AppType): Promise<string> {
|
async getConfigDir(appType: AppType): Promise<string> {
|
||||||
return await invoke("get_config_dir", { app_type: appType });
|
return await invoke("get_config_dir", { app: appType });
|
||||||
},
|
},
|
||||||
|
|
||||||
async openConfigFolder(appType: AppType): Promise<void> {
|
async openConfigFolder(appType: AppType): Promise<void> {
|
||||||
await invoke("open_config_folder", { app_type: appType });
|
await invoke("open_config_folder", { app: appType });
|
||||||
},
|
},
|
||||||
|
|
||||||
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
|
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export const usageApi = {
|
|||||||
async query(providerId: string, appType: AppType): Promise<UsageResult> {
|
async query(providerId: string, appType: AppType): Promise<UsageResult> {
|
||||||
return await invoke("query_provider_usage", {
|
return await invoke("query_provider_usage", {
|
||||||
provider_id: providerId,
|
provider_id: providerId,
|
||||||
app_type: appType,
|
app: appType,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export interface EndpointLatencyResult {
|
|||||||
|
|
||||||
export const vscodeApi = {
|
export const vscodeApi = {
|
||||||
async getLiveProviderSettings(appType: AppType) {
|
async getLiveProviderSettings(appType: AppType) {
|
||||||
return await invoke("read_live_provider_settings", { app_type: appType });
|
return await invoke("read_live_provider_settings", { app: appType });
|
||||||
},
|
},
|
||||||
|
|
||||||
async testApiEndpoints(
|
async testApiEndpoints(
|
||||||
@@ -29,7 +29,7 @@ export const vscodeApi = {
|
|||||||
providerId: string,
|
providerId: string,
|
||||||
): Promise<CustomEndpoint[]> {
|
): Promise<CustomEndpoint[]> {
|
||||||
return await invoke("get_custom_endpoints", {
|
return await invoke("get_custom_endpoints", {
|
||||||
app_type: appType,
|
app: appType,
|
||||||
provider_id: providerId,
|
provider_id: providerId,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -40,7 +40,7 @@ export const vscodeApi = {
|
|||||||
url: string,
|
url: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await invoke("add_custom_endpoint", {
|
await invoke("add_custom_endpoint", {
|
||||||
app_type: appType,
|
app: appType,
|
||||||
provider_id: providerId,
|
provider_id: providerId,
|
||||||
url,
|
url,
|
||||||
});
|
});
|
||||||
@@ -52,7 +52,7 @@ export const vscodeApi = {
|
|||||||
url: string,
|
url: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await invoke("remove_custom_endpoint", {
|
await invoke("remove_custom_endpoint", {
|
||||||
app_type: appType,
|
app: appType,
|
||||||
provider_id: providerId,
|
provider_id: providerId,
|
||||||
url,
|
url,
|
||||||
});
|
});
|
||||||
@@ -64,7 +64,7 @@ export const vscodeApi = {
|
|||||||
url: string,
|
url: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await invoke("update_endpoint_last_used", {
|
await invoke("update_endpoint_last_used", {
|
||||||
app_type: appType,
|
app: appType,
|
||||||
provider_id: providerId,
|
provider_id: providerId,
|
||||||
url,
|
url,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -37,81 +37,59 @@ const success = <T>(payload: T) => HttpResponse.json(payload as any);
|
|||||||
|
|
||||||
export const handlers = [
|
export const handlers = [
|
||||||
http.post(`${TAURI_ENDPOINT}/get_providers`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/get_providers`, async ({ request }) => {
|
||||||
const { app_type, app } = await withJson<{ app_type?: AppType; app?: AppType }>(
|
const { app } = await withJson<{ app: AppType }>(request);
|
||||||
request,
|
return success(getProviders(app));
|
||||||
);
|
|
||||||
const appType = app ?? app_type!;
|
|
||||||
return success(getProviders(appType));
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/get_current_provider`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/get_current_provider`, async ({ request }) => {
|
||||||
const { app_type, app } = await withJson<{ app_type?: AppType; app?: AppType }>(
|
const { app } = await withJson<{ app: AppType }>(request);
|
||||||
request,
|
return success(getCurrentProviderId(app));
|
||||||
);
|
|
||||||
const appType = app ?? app_type!;
|
|
||||||
return success(getCurrentProviderId(appType));
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/update_providers_sort_order`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/update_providers_sort_order`, async ({ request }) => {
|
||||||
const { updates = [], app_type, app } = await withJson<{
|
const { updates = [], app } = await withJson<{
|
||||||
updates: { id: string; sortIndex: number }[];
|
updates: { id: string; sortIndex: number }[];
|
||||||
app_type?: AppType;
|
app: AppType;
|
||||||
app?: AppType;
|
|
||||||
}>(request);
|
}>(request);
|
||||||
const appType = app ?? app_type!;
|
updateSortOrder(app, updates);
|
||||||
updateSortOrder(appType, updates);
|
|
||||||
return success(true);
|
return success(true);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/update_tray_menu`, () => success(true)),
|
http.post(`${TAURI_ENDPOINT}/update_tray_menu`, () => success(true)),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/switch_provider`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/switch_provider`, async ({ request }) => {
|
||||||
const { id, app_type, app } = await withJson<{
|
const { id, app } = await withJson<{ id: string; app: AppType }>(request);
|
||||||
id: string;
|
const providers = listProviders(app);
|
||||||
app_type?: AppType;
|
|
||||||
app?: AppType;
|
|
||||||
}>(request);
|
|
||||||
const appType = app ?? app_type!;
|
|
||||||
const providers = listProviders(appType);
|
|
||||||
if (!providers[id]) {
|
if (!providers[id]) {
|
||||||
return HttpResponse.json(false, { status: 404 });
|
return HttpResponse.json(false, { status: 404 });
|
||||||
}
|
}
|
||||||
setCurrentProviderId(appType, id);
|
setCurrentProviderId(app, id);
|
||||||
return success(true);
|
return success(true);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/add_provider`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/add_provider`, async ({ request }) => {
|
||||||
const { provider, app_type, app } = await withJson<{
|
const { provider, app } = await withJson<{
|
||||||
provider: Provider & { id?: string };
|
provider: Provider & { id?: string };
|
||||||
app_type?: AppType;
|
app: AppType;
|
||||||
app?: AppType;
|
|
||||||
}>(request);
|
}>(request);
|
||||||
|
|
||||||
const newId = provider.id ?? `mock-${Date.now()}`;
|
const newId = provider.id ?? `mock-${Date.now()}`;
|
||||||
const appType = app ?? app_type!;
|
addProvider(app, { ...provider, id: newId });
|
||||||
addProvider(appType, { ...provider, id: newId });
|
|
||||||
return success(true);
|
return success(true);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/update_provider`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/update_provider`, async ({ request }) => {
|
||||||
const { provider, app_type, app } = await withJson<{
|
const { provider, app } = await withJson<{
|
||||||
provider: Provider;
|
provider: Provider;
|
||||||
app_type?: AppType;
|
app: AppType;
|
||||||
app?: AppType;
|
|
||||||
}>(request);
|
}>(request);
|
||||||
const appType = app ?? app_type!;
|
updateProvider(app, provider);
|
||||||
updateProvider(appType, provider);
|
|
||||||
return success(true);
|
return success(true);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/delete_provider`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/delete_provider`, async ({ request }) => {
|
||||||
const { id, app_type, app } = await withJson<{
|
const { id, app } = await withJson<{ id: string; app: AppType }>(request);
|
||||||
id: string;
|
deleteProvider(app, id);
|
||||||
app_type?: AppType;
|
|
||||||
app?: AppType;
|
|
||||||
}>(request);
|
|
||||||
const appType = app ?? app_type!;
|
|
||||||
deleteProvider(appType, id);
|
|
||||||
return success(true);
|
return success(true);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -184,8 +162,8 @@ export const handlers = [
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/get_config_dir`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/get_config_dir`, async ({ request }) => {
|
||||||
const { app_type } = await withJson<{ app_type: AppType }>(request);
|
const { app } = await withJson<{ app: AppType }>(request);
|
||||||
return success(app_type === "claude" ? "/default/claude" : "/default/codex");
|
return success(app === "claude" ? "/default/claude" : "/default/codex");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
|
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
|
||||||
|
|||||||
Reference in New Issue
Block a user