feat(usage): add test script API with refactored execution logic

- 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
This commit is contained in:
Jason
2025-11-04 23:04:44 +08:00
parent 05e58e9e14
commit bafddb8e52
5 changed files with 152 additions and 25 deletions

View File

@@ -26,4 +26,37 @@ export const usageApi = {
};
}
},
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"),
};
}
},
};