- Add private helper method `execute_and_format_usage_result` to eliminate code duplication - Refactor `query_usage` to use helper method instead of duplicating result processing - Add new `test_usage_script` method to test temporary script without saving - Add backend command `test_usage_script` accepting script content as parameter - Register new command in lib.rs invoke_handler - Add frontend `usageApi.testScript` method to call the new backend API - Update `UsageScriptModal.handleTest` to test current editor content instead of saved script - Improve DX: users can now test script changes before saving
63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import { invoke } from "@tauri-apps/api/core";
|
|
import type { UsageResult } from "@/types";
|
|
import type { AppId } from "./types";
|
|
import i18n from "@/i18n";
|
|
|
|
export const usageApi = {
|
|
async query(providerId: string, appId: AppId): Promise<UsageResult> {
|
|
try {
|
|
return await invoke("query_provider_usage", {
|
|
providerId: providerId,
|
|
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"),
|
|
};
|
|
}
|
|
},
|
|
|
|
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"),
|
|
};
|
|
}
|
|
},
|
|
};
|
|
|