PRINTFILM: AI short-video marketing and AI short-drama creation platform
Fix AI re-plan to clear old episode fragments when force=true.
yi8d95d34d4a67
diff --git a/backend/app/services/drama/jobs.py b/backend/app/services/drama/jobs.pyindex 582a14a..ee45050 100644--- a/backend/app/services/drama/jobs.py+++ b/backend/app/services/drama/jobs.py@@ -412,6 +412,7 @@ async def run_episode_fragment_plan_job(*,fallback_rules: bool = True,subtitle_enabled: bool | None = None,+ force: bool = False,) -> dict[str, Any]:# Worker:LLM 规划本集分镜并落库;失败可选回退规则切分from app.services.drama.build_fragments import build_fragments_from_episode_body@@ -496,27 +497,29 @@ async def run_episode_fragment_plan_job(exclude_episode_id=episode.id,)- # 本集已有视频/手改分镜:续拆时锁定,并把已介绍角色并入去重集+ # force:覆盖本集全部分镜;否则锁定已有视频/手改分镜并续拆from app.services.drama.build_fragments import extract_introduced_names_from_content- protected_frags = sorted(- [f for f in (episode.fragments or []) if _fragment_is_protected(f)],- key=lambda f: int(f.sort_order or 0),- )+ protected_frags: list[DramaEpisodeFragment] = []locked_summaries: list[str] = []- for frag in protected_frags:- for name in extract_introduced_names_from_content(frag.content or ""):- already_introduced.add(name)- # 摘要:去掉 cue 行后取前几行画面/对白- narr: list[str] = []- for raw in (frag.content or "").replace("\r\n", "\n").split("\n"):- line = raw.strip()- if not line or line.startswith("@") or line.startswith("【"):- continue- narr.append(line)- if len(narr) >= 3:- break- locked_summaries.append(";".join(narr) if narr else f"分镜#{frag.sort_order}")+ if not force:+ protected_frags = sorted(+ [f for f in (episode.fragments or []) if _fragment_is_protected(f)],+ key=lambda f: int(f.sort_order or 0),+ )+ for frag in protected_frags:+ for name in extract_introduced_names_from_content(frag.content or ""):+ already_introduced.add(name)+ # 摘要:去掉 cue 行后取前几行画面/对白+ narr: list[str] = []+ for raw in (frag.content or "").replace("\r\n", "\n").split("\n"):+ line = raw.strip()+ if not line or line.startswith("@") or line.startswith("【"):+ continue+ narr.append(line)+ if len(narr) >= 3:+ break+ locked_summaries.append(";".join(narr) if narr else f"分镜#{frag.sort_order}")mode_used = "llm"summary = script.summary if script and isinstance(script.summary, dict) else {}@@ -532,7 +535,7 @@ async def run_episode_fragment_plan_job(episode_bodies=all_bodies,story_type=str(summary.get("storyType") or "") or None,)- continuation = bool(locked_summaries)+ continuation = bool(locked_summaries) and not forcetry:drafts = await plan_fragments_with_llm(episode_name=episode.name or "",@@ -581,7 +584,7 @@ async def run_episode_fragment_plan_job(body,assets,drafts,- preserve_protected=True,+ preserve_protected=not force,continuation=continuation,)# 重新加载 params(replace 会写 fingerprint)diff --git a/backend/app/services/tasks/handlers.py b/backend/app/services/tasks/handlers.pyindex e9ecf68..fc20697 100644--- a/backend/app/services/tasks/handlers.py+++ b/backend/app/services/tasks/handlers.py@@ -53,6 +53,7 @@ async def _run_drama_fragment_plan(task: TaskRun) -> dict[str, Any] | None:_require_int(task.episode_id, "episode_id"),fallback_rules=bool(payload.get("fallback_rules", True)),subtitle_enabled=payload.get("subtitle_enabled"),+ force=bool(payload.get("force")),)diff --git a/backend/tests/test_fragment_plan_force.py b/backend/tests/test_fragment_plan_force.pynew file mode 100644index 0000000..84d6872--- /dev/null+++ b/backend/tests/test_fragment_plan_force.py@@ -0,0 +1,43 @@+"""AI 重新分镜:force 须清空旧分镜(含已有视频/手改)。"""++from types import SimpleNamespace++from app.services.drama.seed import _fragment_is_protected+++def test_fragment_is_protected_detects_video_and_user_edited():+ with_video = SimpleNamespace(+ video="https://example.com/a.mp4",+ params={},+ )+ user_edited = SimpleNamespace(+ video="",+ params={"user_edited": True},+ )+ plain = SimpleNamespace(video="", params={"user_edited": False})+ assert _fragment_is_protected(with_video) is True+ assert _fragment_is_protected(user_edited) is True+ assert _fragment_is_protected(plain) is False+++def test_fragment_plan_handler_passes_force_flag():+ from unittest.mock import AsyncMock, patch++ from app.services.tasks import handlers++ fake_run = AsyncMock(return_value={"ok": True})+ task = SimpleNamespace(+ episode_id=99,+ payload={"force": True, "fallback_rules": False},+ )+ with patch("app.services.drama.jobs.run_episode_fragment_plan_job", fake_run):+ import asyncio++ asyncio.run(handlers._run_drama_fragment_plan(task))++ fake_run.assert_awaited_once_with(+ 99,+ fallback_rules=False,+ subtitle_enabled=None,+ force=True,+ )