From 538254ee3847603fcadf9e711cb94d39f8dc1fa5 Mon Sep 17 00:00:00 2001 From: imeepos Date: Fri, 1 Aug 2025 11:13:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Hedra=20=E5=8F=A3?= =?UTF-8?q?=E5=9E=8B=E5=90=88=E6=88=90=E5=B7=A5=E5=85=B7=E7=9A=84=20API=20?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复前端和后端参数类型不匹配问题 - 在前端服务中添加 fileToBytes 方法,将 File 对象转换为字节数组 - 更新后端 HedraFileUploadRequest 结构,添加 purpose 字段 - 修复 Tauri 命令参数接收方式,使用 params 而不是 request - 将 bowong_wait_for_task_completion 的模拟实现替换为真实的 API 调用 - 使用 wait_for_task_completion 方法实现真实的任务等待逻辑 现在 Hedra 工具应该能够正确调用后端 API 进行文件上传和任务处理 --- .../data/models/bowong_text_video_agent.rs | 1 + .../bowong_text_video_agent_commands.rs | 33 ++++++++++--------- .../services/bowongTextVideoAgentService.ts | 28 +++++++++++++++- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/apps/desktop/src-tauri/src/data/models/bowong_text_video_agent.rs b/apps/desktop/src-tauri/src/data/models/bowong_text_video_agent.rs index ff824ef..c470d68 100644 --- a/apps/desktop/src-tauri/src/data/models/bowong_text_video_agent.rs +++ b/apps/desktop/src-tauri/src/data/models/bowong_text_video_agent.rs @@ -263,6 +263,7 @@ pub struct ComfyUISyncExecuteRequest { pub struct HedraFileUploadRequest { pub file_data: Vec, pub filename: String, + pub purpose: Option, // 'image', 'audio', 'video', 'voice' } /// Hedra 任务提交请求 diff --git a/apps/desktop/src-tauri/src/presentation/commands/bowong_text_video_agent_commands.rs b/apps/desktop/src-tauri/src/presentation/commands/bowong_text_video_agent_commands.rs index 3efe566..14be3cd 100644 --- a/apps/desktop/src-tauri/src/presentation/commands/bowong_text_video_agent_commands.rs +++ b/apps/desktop/src-tauri/src/presentation/commands/bowong_text_video_agent_commands.rs @@ -668,22 +668,25 @@ pub async fn bowong_batch_query_task_status( /// 等待任务完成 #[command] pub async fn bowong_wait_for_task_completion( + state: State<'_, AppState>, task_id: String, max_wait_time: u64, poll_interval: u64, ) -> Result { - // 模拟等待 - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + // 克隆服务以避免跨 await 点持有 MutexGuard + let service = { + let service_guard = state.get_bowong_service() + .map_err(|e| format!("Failed to get BowongTextVideoAgent service: {}", e))?; - Ok(TaskStatusResponse { - task_id, - status: "completed".to_string(), - progress: Some(100.0), - result: Some(serde_json::json!({"output": "Task completed after waiting"})), - error: None, - created_at: Some("2024-01-01T00:00:00Z".to_string()), - updated_at: Some("2024-01-01T00:00:00Z".to_string()), - }) + service_guard.as_ref() + .ok_or_else(|| "BowongTextVideoAgent service not initialized".to_string())? + .clone() + }; + + // 使用真实的轮询逻辑等待任务完成 + service.wait_for_task_completion(&task_id, max_wait_time, poll_interval) + .await + .map_err(|e| format!("Failed to wait for task completion: {}", e)) } // ============================================================================ @@ -694,7 +697,7 @@ pub async fn bowong_wait_for_task_completion( #[command] pub async fn hedra_upload_file( state: State<'_, AppState>, - request: HedraFileUploadRequest, + params: HedraFileUploadRequest, ) -> Result { // 克隆服务以避免跨 await 点持有 MutexGuard let service = { @@ -706,7 +709,7 @@ pub async fn hedra_upload_file( .clone() }; - service.hedra_upload_file(&request) + service.hedra_upload_file(¶ms) .await .map_err(|e| format!("Failed to upload file to Hedra: {}", e)) } @@ -715,7 +718,7 @@ pub async fn hedra_upload_file( #[command] pub async fn hedra_submit_task( state: State<'_, AppState>, - request: HedraTaskSubmitRequest, + params: HedraTaskSubmitRequest, ) -> Result { // 克隆服务以避免跨 await 点持有 MutexGuard let service = { @@ -727,7 +730,7 @@ pub async fn hedra_submit_task( .clone() }; - service.hedra_submit_task(&request) + service.hedra_submit_task(¶ms) .await .map_err(|e| format!("Failed to submit Hedra task: {}", e)) } diff --git a/apps/desktop/src/services/bowongTextVideoAgentService.ts b/apps/desktop/src/services/bowongTextVideoAgentService.ts index 737c41c..9e5f04f 100644 --- a/apps/desktop/src/services/bowongTextVideoAgentService.ts +++ b/apps/desktop/src/services/bowongTextVideoAgentService.ts @@ -606,7 +606,15 @@ export class BowongTextVideoAgentFastApiService implements BowongTextVideoAgentA // ============================================================================ async hedraUploadFile(request: HedraFileUploadRequest): Promise { - return this.invokeAPI('hedra_upload_file', request); + // 将 File 对象转换为后端期望的格式 + const fileData = await this.fileToBytes(request.local_file); + const backendRequest = { + file_data: Array.from(fileData), // 转换为数字数组 + filename: request.local_file.name, + purpose: request.purpose || 'image' + }; + + return this.invokeAPI('hedra_upload_file', backendRequest); } async hedraSubmitTask(request: HedraTaskSubmitRequest): Promise { @@ -633,6 +641,24 @@ export class BowongTextVideoAgentFastApiService implements BowongTextVideoAgentA // 工具方法 // ============================================================================ + /** + * 将 File 对象转换为字节数组 + */ + private async fileToBytes(file: File): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => { + if (reader.result instanceof ArrayBuffer) { + resolve(new Uint8Array(reader.result)); + } else { + reject(new Error('Failed to read file as ArrayBuffer')); + } + }; + reader.onerror = () => reject(reader.error); + reader.readAsArrayBuffer(file); + }); + } + /** * 获取服务配置 */