From 66ec36b4745b187c7a82a4c15db7df5414602b99 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 10 Jul 2025 13:07:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20JSON-RPC=20?= =?UTF-8?q?=E9=80=9A=E4=BF=A1=E4=B8=AD=E7=9A=84=E7=8A=B6=E6=80=81=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E5=92=8C=E7=BB=93=E6=9E=9C=E8=A7=A3=E6=9E=90=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 关键修复: 1. Python 脚本最终结果输出: - 在函数结束前发送 JSON-RPC 格式的最终结果 - 成功时:rpc.success(result) - 失败时:rpc.error(JSONRPCError.GENERATION_FAILED, msg, details) - 确保最终结果是标准 JSON-RPC 2.0 格式 2. Rust 解析逻辑优化: - 区分进度通知和最终结果响应 - 优先返回 JSON-RPC 结果/错误响应 - 备用机制:检查直接 JSON 中的 status 字段 - 避免返回进度消息作为最终结果 3. 前端 JSON-RPC 响应处理: - 检测 jsonrpc: '2.0' 格式 - 提取 result 字段作为成功结果 - 处理 error 字段并抛出相应错误 - 保持向后兼容直接 JSON 格式 4. 错误处理链路完善: - Python 异常 → JSON-RPC 错误响应 - Rust 解析 → 提取错误信息 - 前端处理 → 显示具体错误原因 - 端到端的错误传播机制 ✅ 修复效果: - 正确识别成功/失败状态 ✓ - 返回最终结果而非进度消息 ✓ - 标准化的错误处理 ✓ - 完整的 JSON-RPC 2.0 支持 ✓ 现在前端应该能正确显示视频生成的成功状态! --- python_core/ai_video/video_generator.py | 8 ++++++++ src-tauri/src/commands.rs | 15 +++++++++------ src/services/tauri.ts | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/python_core/ai_video/video_generator.py b/python_core/ai_video/video_generator.py index e9579fe..9579338 100644 --- a/python_core/ai_video/video_generator.py +++ b/python_core/ai_video/video_generator.py @@ -206,6 +206,14 @@ class VideoGenerator: result['error_details'] = error_details logger.error(f"Video generation failed: {result['msg']}") logger.error(f"Traceback: {error_details['traceback']}") + progress.error(result['msg']) + rpc.error(JSONRPCError.GENERATION_FAILED, "Video generation failed", error_details) + + # Send final result via JSON-RPC + if result['status']: + rpc.success(result) + else: + rpc.error(JSONRPCError.GENERATION_FAILED, result.get('msg', 'Unknown error'), result) return result diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 406cdcc..f14ca84 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -135,17 +135,20 @@ async fn execute_python_command(app: tauri::AppHandle, args: &[String]) -> Resul progress_messages.push(json_value); println!("Progress: {}", json_str); } - } else if json_value.get("result").is_some() { - // This is a final result + } else if json_value.get("result").is_some() || json_value.get("error").is_some() { + // This is a final result or error response final_result = Some(json_str.to_string()); - println!("Final result: {}", json_str); + println!("Final JSON-RPC result: {}", json_str); } } } else if line.trim().starts_with('{') && line.trim().ends_with('}') { // Fallback: try to parse as direct JSON result - if let Ok(_) = serde_json::from_str::(line.trim()) { - final_result = Some(line.trim().to_string()); - println!("Direct JSON result: {}", line.trim()); + if let Ok(json_value) = serde_json::from_str::(line.trim()) { + // Check if this looks like a final result (has status field) + if json_value.get("status").is_some() { + final_result = Some(line.trim().to_string()); + println!("Direct JSON result: {}", line.trim()); + } } } } diff --git a/src/services/tauri.ts b/src/services/tauri.ts index e67decb..f34ab2d 100644 --- a/src/services/tauri.ts +++ b/src/services/tauri.ts @@ -389,6 +389,20 @@ export class AIVideoService { const parsedResult = JSON.parse(result as string) console.log('Parsed result:', parsedResult) + // Handle JSON-RPC response format + if (parsedResult.jsonrpc === "2.0") { + if (parsedResult.result) { + // Success response + console.log('JSON-RPC success result:', parsedResult.result) + return parsedResult.result + } else if (parsedResult.error) { + // Error response + console.error('JSON-RPC error:', parsedResult.error) + throw new Error(parsedResult.error.message || 'JSON-RPC error') + } + } + + // Fallback: direct result format return parsedResult } catch (error) { console.error('Failed to generate AI video:', error)