feat: validate video input for continue scenes at enqueue

This commit is contained in:
Benjiyaya
2026-08-29 04:14:05 +08:00
parent 59188a5b00
commit 85cccaef52
2 changed files with 287 additions and 7 deletions
@@ -3,6 +3,8 @@ from __future__ import annotations
import json
import logging
import sqlite3
from pathlib import Path
from typing import Any
from calliope.agent.llm import LLMClient
@@ -12,6 +14,7 @@ from calliope.agent.prompts import (
scene_video_prompt,
)
from calliope.comfyui.parser import parse_dynamic_inputs
from calliope.comfyui.roles import input_has_role
from calliope.comfyui.smart_fill import ref_image_slots, smart_fill_inputs
from calliope.config import settings
from calliope.db import get_db, row_to_dict
@@ -77,6 +80,42 @@ async def _h3_rewrite(scene: dict[str, Any], subjects: list[dict[str, Any]]) ->
await client.close()
def _video_input(inputs: list[dict[str, Any]]) -> dict[str, Any] | None:
"""First workflow input whose canonical role is ``video``."""
for inp in inputs:
if input_has_role(inp, "video"):
return inp
return None
def _previous_clip(
conn: sqlite3.Connection,
project_id: int,
order_index: int,
) -> tuple[str | None, bool]:
"""Nearest earlier scene's clip path + whether any earlier scene exists.
Returns ``(path, has_earlier_scene)``; path is None when the clip file does
not exist on disk (not yet generated) or there is no earlier scene.
"""
row = conn.execute(
"""
SELECT video_path FROM scenes
WHERE project_id = ? AND order_index < ? AND video_path IS NOT NULL
ORDER BY order_index DESC LIMIT 1
""",
(project_id, order_index),
).fetchone()
has_earlier = conn.execute(
"SELECT 1 FROM scenes WHERE project_id = ? AND order_index < ? LIMIT 1",
(project_id, order_index),
).fetchone() is not None
path = row["video_path"] if row else None
if path and Path(path).exists():
return path, has_earlier
return None, has_earlier
def _workflow_json(workflow: dict[str, Any] | None) -> dict[str, Any]:
if not workflow:
return {}
@@ -211,13 +250,37 @@ async def enqueue_video_jobs(
)
payload: dict[str, Any] = {"input_values": values, "prompt": prompt}
if scene.get("chain_from_prev"):
# Resolved at RUN time by the worker (the previous scene's clip may not
# exist yet at enqueue time — e.g. a batch queues all scenes up front).
payload["chain_from_prev"] = True
payload["scene_order_index"] = scene.get("order_index")
# The value the location/first-image ref slot holds right now, so the
# worker can find and replace that exact slot whatever the layout.
payload["chain_replace_value"] = loc_image
video_input = _video_input(inputs)
if not video_input:
raise ValueError(
f"Scene {scene.get('order_index')} is marked continue-from-previous "
f"but workflow '{(workflow or {}).get('name') or workflow_id}' has no "
"video input — pick a workflow with a (Input:video) node."
)
video_node_id = str(video_input["nodeId"])
if not values.get(video_node_id):
# Explicit clip from the form / input_values_override wins.
prev_clip, has_earlier = _previous_clip(
conn, project_id, scene.get("order_index") or 0
)
if prev_clip:
# Local path: the worker's prepare_media_inputs uploads it
# to ComfyUI before queuing (same shape as char/loc refs).
values[video_node_id] = prev_clip
elif has_earlier:
# Previous clip not generated yet (typical when a batch is
# queued in one go). The worker resolves it at RUN time —
# the queue is concurrency-1, so the earlier scene's clip
# will exist by then.
payload["continue_source"] = {
"scene_order_index": scene.get("order_index"),
}
else:
raise ValueError(
f"Scene {scene.get('order_index')} is marked continue-from-previous "
"but no previous clip exists yet — generate an earlier clip first "
"or upload a video in the Video stage."
)
job = queue_manager.enqueue(
project_id=project_id,
kind="video",