refactor(providers): add flexible app type resolution with dual parameter support

Add `resolve_app_type` helper to support both enum and string-based app type
parameters across all provider commands. This change:

- Eliminates implicit default to Claude (previously used `unwrap_or`)
- Supports two parameter forms: `app_type` (enum, priority 1) and `app` (string, priority 2)
- Provides explicit error handling when both parameters are missing
- Updates all 14 provider command functions with consistent parameter validation
- Fixes tray menu provider switching to pass the new `app` parameter

This dual-parameter approach maintains backward compatibility while enabling
future CLI tool integration and more flexible API usage patterns.

Technical details:
- Priority order: `app_type` enum > `app` string > error
- Invalid `app` strings now return errors instead of defaulting
- All existing tests pass (45/45)
This commit is contained in:
Jason
2025-10-29 20:33:30 +08:00
parent 7d56aed543
commit 590be4e136
3 changed files with 69 additions and 35 deletions

View File

@@ -15,31 +15,31 @@ export interface ProviderSwitchEvent {
export const providersApi = {
async getAll(appType: AppType): Promise<Record<string, Provider>> {
return await invoke("get_providers", { app_type: appType });
return await invoke("get_providers", { app: appType });
},
async getCurrent(appType: AppType): Promise<string> {
return await invoke("get_current_provider", { app_type: appType });
return await invoke("get_current_provider", { app: appType });
},
async add(provider: Provider, appType: AppType): Promise<boolean> {
return await invoke("add_provider", { provider, app_type: appType });
return await invoke("add_provider", { provider, app: appType });
},
async update(provider: Provider, appType: AppType): Promise<boolean> {
return await invoke("update_provider", { provider, app_type: appType });
return await invoke("update_provider", { provider, app: appType });
},
async delete(id: string, appType: AppType): Promise<boolean> {
return await invoke("delete_provider", { id, app_type: appType });
return await invoke("delete_provider", { id, app: appType });
},
async switch(id: string, appType: AppType): Promise<boolean> {
return await invoke("switch_provider", { id, app_type: appType });
return await invoke("switch_provider", { id, app: appType });
},
async importDefault(appType: AppType): Promise<boolean> {
return await invoke("import_default_config", { app_type: appType });
return await invoke("import_default_config", { app: appType });
},
async updateTrayMenu(): Promise<boolean> {
@@ -50,10 +50,7 @@ export const providersApi = {
updates: ProviderSortUpdate[],
appType: AppType,
): Promise<boolean> {
return await invoke("update_providers_sort_order", {
updates,
app_type: appType,
});
return await invoke("update_providers_sort_order", { updates, app: appType });
},
async onSwitched(