From 3728bb007bba12c8902da927ebd24b8850886204 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 10 Jul 2025 19:32:44 +0800 Subject: [PATCH] fix --- python_core/ai_video/jsonrpc.py | 66 +++++++++++++++++++------ python_core/ai_video/video_generator.py | 37 +++++++++++++- src-tauri/src/commands/ai_video.rs | 9 ++++ 3 files changed, 95 insertions(+), 17 deletions(-) diff --git a/python_core/ai_video/jsonrpc.py b/python_core/ai_video/jsonrpc.py index 5b1acd9..52862a0 100644 --- a/python_core/ai_video/jsonrpc.py +++ b/python_core/ai_video/jsonrpc.py @@ -54,24 +54,60 @@ class JSONRPCResponse: self._send_response(notification) def _send_response(self, response: Dict[str, Any]) -> None: - """Send JSON-RPC response to stdout""" + """Send JSON-RPC response to stdout with proper encoding""" try: - json_str = json.dumps(response, ensure_ascii=False, separators=(',', ':')) - print(f"JSONRPC:{json_str}") - sys.stdout.flush() + # Use ensure_ascii=True to avoid Unicode encoding issues + json_str = json.dumps(response, ensure_ascii=True, separators=(',', ':')) + + # Ensure stdout is properly configured for UTF-8 + output_line = f"JSONRPC:{json_str}" + + # Write to stdout with explicit encoding handling + if hasattr(sys.stdout, 'buffer'): + # Python 3: write bytes to buffer to ensure proper encoding + sys.stdout.buffer.write(output_line.encode('utf-8')) + sys.stdout.buffer.write(b'\n') + sys.stdout.buffer.flush() + else: + # Fallback for older Python versions + print(output_line) + sys.stdout.flush() + except Exception as e: - # Fallback error response - fallback = { - "jsonrpc": "2.0", - "id": self.request_id, - "error": { - "code": -32603, - "message": "Internal error", - "data": str(e) + # Fallback error response with safe encoding + try: + fallback = { + "jsonrpc": "2.0", + "id": self.request_id, + "error": { + "code": -32603, + "message": "Internal error", + "data": str(e) + } } - } - print(f"JSONRPC:{json.dumps(fallback)}") - sys.stdout.flush() + fallback_json = json.dumps(fallback, ensure_ascii=True) + fallback_line = f"JSONRPC:{fallback_json}" + + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.write(fallback_line.encode('utf-8')) + sys.stdout.buffer.write(b'\n') + sys.stdout.buffer.flush() + else: + print(fallback_line) + sys.stdout.flush() + except Exception as fallback_error: + # Last resort: simple error message + error_msg = f"JSONRPC:{{\"jsonrpc\":\"2.0\",\"id\":{self.request_id},\"error\":{{\"code\":-32603,\"message\":\"Encoding error\"}}}}" + try: + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.write(error_msg.encode('utf-8')) + sys.stdout.buffer.write(b'\n') + sys.stdout.buffer.flush() + else: + print(error_msg) + sys.stdout.flush() + except: + pass # Give up if even this fails class ProgressReporter: diff --git a/python_core/ai_video/video_generator.py b/python_core/ai_video/video_generator.py index 079b89c..9fd8d3b 100644 --- a/python_core/ai_video/video_generator.py +++ b/python_core/ai_video/video_generator.py @@ -357,6 +357,24 @@ def main(): """Command line interface for AI video generation.""" import argparse import json + import os + + # Configure encoding for consistent output + if os.name == 'nt': # Windows + # Set console code page to UTF-8 on Windows + try: + import subprocess + subprocess.run(['chcp', '65001'], shell=True, capture_output=True) + except: + pass + + # Ensure stdout uses UTF-8 encoding + if hasattr(sys.stdout, 'reconfigure'): + try: + sys.stdout.reconfigure(encoding='utf-8') + sys.stderr.reconfigure(encoding='utf-8') + except: + pass parser = argparse.ArgumentParser(description="AI Video Generator") parser.add_argument("--action", required=True, @@ -421,14 +439,29 @@ def main(): request_id="cli_batch_request" ) - print(json.dumps(result, ensure_ascii=False, indent=2)) + # Use safe encoding for final output + result_json = json.dumps(result, ensure_ascii=True, indent=2) + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.write(result_json.encode('utf-8')) + sys.stdout.buffer.write(b'\n') + sys.stdout.buffer.flush() + else: + print(result_json) + sys.stdout.flush() except Exception as e: error_result = { "status": False, "error": str(e) } - print(json.dumps(error_result, ensure_ascii=False, indent=2)) + error_json = json.dumps(error_result, ensure_ascii=True, indent=2) + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.write(error_json.encode('utf-8')) + sys.stdout.buffer.write(b'\n') + sys.stdout.buffer.flush() + else: + print(error_json) + sys.stdout.flush() sys.exit(1) diff --git a/src-tauri/src/commands/ai_video.rs b/src-tauri/src/commands/ai_video.rs index ac65c07..a8a07c7 100644 --- a/src-tauri/src/commands/ai_video.rs +++ b/src-tauri/src/commands/ai_video.rs @@ -59,6 +59,15 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu cmd.stdout(std::process::Stdio::piped()); cmd.stderr(std::process::Stdio::piped()); + // Set environment variables for consistent encoding + cmd.env("PYTHONIOENCODING", "utf-8"); + cmd.env("PYTHONUNBUFFERED", "1"); + + // On Windows, ensure UTF-8 console output + if cfg!(target_os = "windows") { + cmd.env("PYTHONUTF8", "1"); + } + match cmd.spawn() { Ok(process) => { println!("Successfully started Python process with: {}", python_cmd);