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

@@ -121,6 +121,35 @@ pub async fn query_provider_usage(
.map_err(|e| e.to_string())
}
/// 测试用量脚本(使用当前编辑器中的脚本,不保存)
#[tauri::command]
pub async fn test_usage_script(
state: State<'_, AppState>,
#[allow(non_snake_case)]
providerId: String,
app: String,
#[allow(non_snake_case)]
scriptCode: String,
timeout: Option<u64>,
#[allow(non_snake_case)]
accessToken: Option<String>,
#[allow(non_snake_case)]
userId: Option<String>,
) -> Result<crate::provider::UsageResult, String> {
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
ProviderService::test_usage_script(
state.inner(),
app_type,
&providerId,
&scriptCode,
timeout.unwrap_or(10),
accessToken.as_deref(),
userId.as_deref(),
)
.await
.map_err(|e| e.to_string())
}
/// 读取当前生效的配置内容
#[tauri::command]
pub fn read_live_provider_settings(app: String) -> Result<serde_json::Value, String> {