2025-10-16 10:00:22 +08:00
|
|
|
import { invoke } from "@tauri-apps/api/core";
|
|
|
|
|
import type { UsageResult } from "@/types";
|
2025-10-30 14:59:15 +08:00
|
|
|
import type { AppId } from "./types";
|
2025-11-03 10:24:59 +08:00
|
|
|
import i18n from "@/i18n";
|
2025-10-16 10:00:22 +08:00
|
|
|
|
|
|
|
|
export const usageApi = {
|
2025-10-30 14:59:15 +08:00
|
|
|
async query(providerId: string, appId: AppId): Promise<UsageResult> {
|
2025-11-03 10:24:59 +08:00
|
|
|
try {
|
|
|
|
|
return await invoke("query_provider_usage", {
|
refactor: migrate all Tauri commands to camelCase parameters
This commit addresses parameter naming inconsistencies caused by Tauri v2's
requirement for camelCase parameter names in IPC commands.
Backend changes (Rust):
- Updated all command parameters from snake_case to camelCase
- Commands affected:
* provider.rs: providerId (×4), timeoutSecs
* import_export.rs: filePath (×2), defaultName
* config.rs: defaultPath
- Added #[allow(non_snake_case)] attributes for camelCase parameters
- Removed unused QueryUsageParams struct
Frontend changes (TypeScript):
- Removed redundant snake_case parameters from all invoke() calls
- Updated API files:
* usage.ts: removed debug logs, unified to providerId
* vscode.ts: updated 8 functions (providerId, timeoutSecs, filePath, defaultName)
* settings.ts: updated 4 functions (defaultPath, filePath, defaultName)
- Ensured all parameters now use camelCase exclusively
Test updates:
- Updated MSW handlers to accept both old and new parameter formats during transition
- Added i18n mock compatibility for tests
Root cause:
The issue stemmed from Tauri v2 strictly requiring camelCase for command
parameters, while the codebase was using snake_case. This caused parameters
like 'provider_id' to not be recognized by the backend, resulting in
"missing providerId parameter" errors.
BREAKING CHANGE: All Tauri command invocations now require camelCase parameters.
Any external tools or scripts calling these commands must be updated accordingly.
Fixes: Usage query always failing with "missing providerId" error
Fixes: Custom endpoint management not receiving provider ID
Fixes: Import/export dialogs not respecting default paths
2025-11-03 16:50:23 +08:00
|
|
|
providerId: providerId,
|
2025-11-03 10:24:59 +08:00
|
|
|
app: appId,
|
|
|
|
|
});
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
// 提取错误消息:优先使用后端返回的错误信息
|
|
|
|
|
const message =
|
|
|
|
|
typeof error === "string"
|
|
|
|
|
? error
|
|
|
|
|
: error instanceof Error
|
|
|
|
|
? error.message
|
|
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
// 如果没有错误消息,使用国际化的默认提示
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: message || i18n.t("errors.usage_query_failed"),
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-10-16 10:00:22 +08:00
|
|
|
},
|
2025-11-04 23:04:44 +08:00
|
|
|
|
|
|
|
|
async testScript(
|
|
|
|
|
providerId: string,
|
|
|
|
|
appId: AppId,
|
|
|
|
|
scriptCode: string,
|
|
|
|
|
timeout?: number,
|
|
|
|
|
accessToken?: string,
|
|
|
|
|
userId?: string
|
|
|
|
|
): Promise<UsageResult> {
|
|
|
|
|
try {
|
|
|
|
|
return await invoke("test_usage_script", {
|
|
|
|
|
providerId: providerId,
|
|
|
|
|
app: appId,
|
|
|
|
|
scriptCode: scriptCode,
|
|
|
|
|
timeout: timeout,
|
|
|
|
|
accessToken: accessToken,
|
|
|
|
|
userId: userId,
|
|
|
|
|
});
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
const message =
|
|
|
|
|
typeof error === "string"
|
|
|
|
|
? error
|
|
|
|
|
: error instanceof Error
|
|
|
|
|
? error.message
|
|
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: message || i18n.t("errors.usage_query_failed"),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-10-16 10:00:22 +08:00
|
|
|
};
|
2025-11-04 23:04:44 +08:00
|
|
|
|