Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(webui): tolerate malformed historical script data

Rudy Celekli1afb1da63279
diff --git a/test/services/test_webui_task_history.py b/test/services/test_webui_task_history.py
index ad897f81..aa6c5713 100644
--- a/test/services/test_webui_task_history.py
+++ b/test/services/test_webui_task_history.py
@@ -1,8 +1,15 @@
import ast
+import json
import os
import re
from collections.abc import Mapping
from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import MagicMock
+
+import pytest
+
+from app.models import const
ROOT_DIR = Path(__file__).parent.parent.parent
@@ -221,3 +228,36 @@ def test_restore_requirements_allow_replacing_upload_with_other_voice_modes():
has_custom_audio=False,
voice_mode=voice_mode,
)
+
+
[email protected]("payload", [[], {"params": []}, {"script": []}])
+def test_history_scan_skips_non_object_script_payload(tmp_path, payload):
+ """One malformed historical artifact must not break the task panel."""
+ tasks_root = tmp_path / "tasks"
+ task_dir = tasks_root / "broken-task"
+ task_dir.mkdir(parents=True)
+ (task_dir / "script.json").write_text(json.dumps(payload), encoding="utf-8")
+
+ tree = ast.parse(WEBUI_MAIN.read_text(encoding="utf-8"))
+ selected = [
+ node
+ for node in tree.body
+ if isinstance(node, ast.FunctionDef)
+ and node.name in {"_safe_load_task_script", "_scan_history_tasks"}
+ ]
+ namespace = {
+ "os": os,
+ "json": json,
+ "logger": MagicMock(),
+ "utils": SimpleNamespace(task_dir=lambda: str(tasks_root)),
+ "const": const,
+ "_find_final_task_video": lambda _task_path: "",
+ }
+ module = ast.fix_missing_locations(ast.Module(body=selected, type_ignores=[]))
+ exec(compile(module, str(WEBUI_MAIN), "exec"), namespace)
+
+ history = namespace["_scan_history_tasks"]()
+
+ assert len(history) == 1
+ assert history[0]["task_id"] == "broken-task"
+ assert history[0]["subject"] == "broken-task"
diff --git a/webui/Main.py b/webui/Main.py
index 3bb15966..b877426d 100644
--- a/webui/Main.py
+++ b/webui/Main.py
@@ -812,7 +812,11 @@ def _safe_load_task_script(task_path):
try:
with open(script_file, "r", encoding="utf-8") as f:
- return json.load(f)
+ payload = json.load(f)
+ if not isinstance(payload, dict):
+ logger.warning(f"task script data is not an object: {script_file}")
+ return {}
+ return payload
except Exception as e:
logger.warning(f"failed to read task script data: {script_file}, {e}")
return {}
@@ -1005,11 +1009,16 @@ def _scan_history_tasks(limit=30):
tasks = []
for mtime, name, task_path in task_entries[:limit]:
script_data = _safe_load_task_script(task_path)
- params_data = script_data.get("params", {}) if script_data else {}
+ params_data = script_data.get("params", {})
+ if not isinstance(params_data, dict):
+ params_data = {}
+ script_text = script_data.get("script", "")
+ if not isinstance(script_text, str):
+ script_text = ""
video_file = _find_final_task_video(task_path)
subject = (
params_data.get("video_subject")
- or script_data.get("script", "")[:40]
+ or script_text[:40]
or name
)
tasks.append(