Files
aigc-home/calliope-backend/src/calliope/agent/video_agent.py
T
Justo Stoll 2d095b7fbb LLM client: per-call extra_body hook; h3_rewrite_extra_body setting for the H3 profile rewrite
LLMClient.chat / chat_stream / _chat_blocking accept extra_body, merged into the
OpenAI-compatible payload (model / messages / stream cannot be overridden). New
setting h3_rewrite_extra_body (default {}, never sent when empty) is passed on
the minimax_h3_ref rewrite only, so e.g. {"chat_template_kwargs":
{"enable_thinking": false}} (Qwen3 on oMLX / vLLM / SGLang) turns a 158 s
thinking preview into a 27 s one without touching story/script calls.
Settings API + config file round-trip; three tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit 395a4a03a68444dee8fce83f887682c17d96c6ee)
2026-09-06 18:34:59 +02:00

473 lines
18 KiB
Python

"""Enqueue per-scene video generation jobs."""
from __future__ import annotations
import hashlib
import json
import logging
import sqlite3
from pathlib import Path
from typing import Any
from calliope.agent.llm import LLMClient
from calliope.agent.prompts import (
build_minimax_h3_ref_messages,
minimax_h3_ref_fallback,
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
from calliope.events.bus import event_bus
from calliope.queue.manager import queue_manager
logger = logging.getLogger("calliope.video_agent")
def _h3_subjects(
characters: list[dict[str, Any]],
location: dict[str, Any] | None,
loc_image: str | None,
inputs: list[dict[str, Any]],
) -> tuple[list[dict[str, Any]], list[str]]:
"""Ordered subject roster + ref image paths for the H3 ref profile.
Order = ref wiring order: scene characters first (scene order), then the
location. Capped at the workflow's generic (Input:image) slot count so
<Subject N> always matches an actually-wired reference image.
"""
subjects: list[dict[str, Any]] = []
paths: list[str] = []
for c in characters:
img = c.get("sheet_path") or c.get("portrait_path")
if not img:
continue
subjects.append(
{
"index": len(subjects) + 1,
"kind": "character",
"name": c.get("name"),
"appearance": c.get("consistency_prompt") or c.get("appearance") or "",
}
)
paths.append(img)
if loc_image:
loc = location or {}
subjects.append(
{
"index": len(subjects) + 1,
"kind": "location",
"name": loc.get("name"),
"appearance": loc.get("consistency_prompt") or loc.get("description") or "",
}
)
paths.append(loc_image)
cap = len(ref_image_slots(inputs))
return subjects[:cap], paths[:cap]
async def _h3_rewrite(
scene: dict[str, Any],
subjects: list[dict[str, Any]],
*,
timeout: float = 120.0,
) -> str:
"""LLM rewrite into H3's six-section format, deterministic template on failure.
The timeout bounds the whole wait for a dead endpoint before the template
kicks in — the preview path passes a short value so the UI fails fast.
"""
client = LLMClient.for_role("video", timeout=timeout)
try:
return await client.chat(
build_minimax_h3_ref_messages(scene, subjects),
temperature=0.4,
extra_body=settings.h3_rewrite_extra_body or None,
)
except Exception as exc:
logger.warning("MiniMax H3 prompt rewrite failed (%s); using fallback template", exc)
return minimax_h3_ref_fallback(scene, subjects)
finally:
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 {}
raw = workflow.get("workflow_json")
if isinstance(raw, str):
return json.loads(raw)
return raw or {}
def _get_workflow(workflow_id: int | None = None) -> dict[str, Any] | None:
conn = get_db(settings.db_path)
try:
if workflow_id:
row = conn.execute(
"SELECT * FROM workflows WHERE id = ? AND is_enabled = 1", (workflow_id,)
).fetchone()
else:
row = conn.execute(
"SELECT * FROM workflows WHERE kind = 'video' AND is_enabled = 1 ORDER BY id ASC LIMIT 1"
).fetchone()
if not row:
row = conn.execute(
"SELECT * FROM workflows WHERE is_enabled = 1 ORDER BY id ASC LIMIT 1"
).fetchone()
return row_to_dict(row) if row else None
finally:
conn.close()
def _scene_video_settings(scene: dict[str, Any]) -> dict[str, Any]:
"""Parsed scenes.video_settings_json — {} when unset or malformed."""
raw = scene.get("video_settings_json")
if not raw:
return {}
try:
data = json.loads(raw)
except (json.JSONDecodeError, TypeError):
return {}
return data if isinstance(data, dict) else {}
def _stored_input_values(scene: dict[str, Any]) -> dict[str, Any]:
"""Saved per-scene form values (input_values inside video_settings)."""
values = _scene_video_settings(scene).get("input_values")
if isinstance(values, dict):
return {k: v for k, v in values.items() if v not in (None, "")}
return {}
def _stored_prompt_draft(scene: dict[str, Any]) -> str | None:
draft = _scene_video_settings(scene).get("prompt_draft")
return draft if isinstance(draft, str) and draft.strip() else None
def _scene_prompt_hash(scene: dict[str, Any]) -> str:
"""Cheap fingerprint of the inputs a draft was based on (stale detection)."""
basis = "|".join(
str(scene.get(k) or "")
for k in ("heading", "action", "dialog", "duration_sec", "location_id")
)
chars = ",".join(str(c) for c in sorted(scene.get("character_ids") or []))
return hashlib.sha256((basis + "|" + chars).encode()).hexdigest()[:16]
async def preview_scene_prompt(
project_id: int,
scene_id: int,
workflow_id: int | None = None,
) -> dict[str, Any]:
"""Resolve the exact prompt a Generate would send — without enqueueing.
Returns {"prompt": str, "profile": str, "from_draft": bool, "based_on": str}
so the UI can show/edit/save it before the user commits to a render.
"""
conn = get_db(settings.db_path)
try:
row = conn.execute(
"SELECT * FROM scenes WHERE id = ? AND project_id = ?",
(scene_id, project_id),
).fetchone()
if not row:
raise ValueError(f"Scene {scene_id} not found in project {project_id}")
scene = row_to_dict(row)
char_rows = conn.execute(
"""
SELECT c.* FROM characters c
JOIN scene_characters sc ON sc.character_id = c.id
WHERE sc.scene_id = ?
""",
(scene_id,),
).fetchall()
characters = [row_to_dict(r) for r in char_rows]
loc_image = scene.get("env_image_path")
loc_row: dict[str, Any] | None = None
if scene.get("location_id"):
loc = conn.execute(
"SELECT name, description, consistency_prompt, reference_image_path "
"FROM locations WHERE id = ?",
(scene["location_id"],),
).fetchone()
if loc:
loc_row = row_to_dict(loc)
if not loc_image:
loc_image = loc_row["reference_image_path"]
finally:
conn.close()
wf_id = workflow_id or scene.get("workflow_id")
workflow = _get_workflow(wf_id)
if not workflow:
raise ValueError("No enabled video workflow found — configure one in Settings")
inputs = parse_dynamic_inputs(_workflow_json(workflow))
profile = workflow.get("prompt_profile") or "prose"
based_on = _scene_prompt_hash(scene)
if profile == "minimax_h3_ref":
# Fresh saved draft short-circuits the LLM call
draft = _stored_prompt_draft(scene)
if draft:
meta = _scene_video_settings(scene).get("prompt_draft_meta") or {}
if meta.get("based_on") == based_on:
return {
"prompt": draft,
"profile": profile,
"from_draft": True,
"based_on": based_on,
}
subjects, _ = _h3_subjects(characters, loc_row, loc_image, inputs)
await event_bus.publish(
"agent.thinking",
{"message": f"H3 prompt rewrite · scene {scene.get('order_index')}", "project_id": project_id},
)
# Preview is interactive — fail fast to the deterministic template
# instead of making the user wait out a dead endpoint.
prompt = await _h3_rewrite(scene, subjects, timeout=30.0)
else:
prompt = scene_video_prompt(scene, characters)
return {"prompt": prompt, "profile": profile, "from_draft": False, "based_on": based_on}
async def enqueue_video_jobs(
project_id: int,
*,
scene_ids: list[int] | None = None,
workflow_id: int | None = None,
input_values_override: dict[str, Any] | None = None,
prompts: dict[int, str] | None = None,
session_id: int | None = None,
) -> list[dict[str, Any]]:
await event_bus.publish(
"agent.thinking", {"message": "Queuing video jobs…", "project_id": project_id}
)
conn = get_db(settings.db_path)
jobs: list[dict[str, Any]] = []
try:
query = "SELECT * FROM scenes WHERE project_id = ?"
params: list[Any] = [project_id]
if scene_ids:
placeholders = ",".join("?" * len(scene_ids))
query += f" AND id IN ({placeholders})"
params.extend(scene_ids)
query += " ORDER BY order_index"
scenes = [row_to_dict(r) for r in conn.execute(query, params).fetchall()]
for scene in scenes:
# Supersede leftover pending jobs so a re-Generate always starts fresh
conn.execute(
"""
UPDATE jobs SET status = 'failed', error = 'superseded by new generate',
completed_at = CURRENT_TIMESTAMP
WHERE scene_id = ? AND kind = 'video' AND status = 'pending'
""",
(scene["id"],),
)
# Clear prior clip so UI shows generating instead of the old video
conn.execute(
"UPDATE scenes SET video_path = NULL WHERE id = ?",
(scene["id"],),
)
# Commit BEFORE enqueue: queue_manager.enqueue writes on its own
# connection, and holding this transaction open across that call
# self-deadlocks (sqlite3.OperationalError: database is locked).
conn.commit()
wf_id = workflow_id or scene.get("workflow_id")
workflow = _get_workflow(wf_id)
workflow_json = _workflow_json(workflow)
inputs = parse_dynamic_inputs(workflow_json) if workflow_json else []
duration = scene.get("duration_sec")
char_rows = conn.execute(
"""
SELECT c.* FROM characters c
JOIN scene_characters sc ON sc.character_id = c.id
WHERE sc.scene_id = ?
""",
(scene["id"],),
).fetchall()
characters = [row_to_dict(r) for r in char_rows]
char_image = next(
(c.get("sheet_path") or c.get("portrait_path") for c in characters if c.get("sheet_path") or c.get("portrait_path")),
None,
)
loc_image = scene.get("env_image_path")
loc_row: dict[str, Any] | None = None
if scene.get("location_id"):
row = conn.execute(
"SELECT name, description, consistency_prompt, reference_image_path "
"FROM locations WHERE id = ?",
(scene["location_id"],),
).fetchone()
if row:
loc_row = row_to_dict(row)
if not loc_image:
loc_image = loc_row["reference_image_path"]
profile = (workflow or {}).get("prompt_profile") or "prose"
# Merge base: saved per-scene setup first, explicit request wins on
# top — so batch Generate-all honors persisted form setups.
stored_values = _stored_input_values(scene)
extra_values: dict[str, Any] = {**stored_values}
if input_values_override:
extra_values.update(
{k: v for k, v in input_values_override.items() if v not in (None, "")}
)
if profile == "minimax_h3_ref":
subjects, ref_paths = _h3_subjects(characters, loc_row, loc_image, inputs)
# Prompt precedence: explicit request → saved (fresh) draft → LLM.
explicit_prompt = (prompts or {}).get(scene["id"])
fresh_draft = None
if explicit_prompt is None:
candidate = _stored_prompt_draft(scene)
if candidate:
meta = _scene_video_settings(scene).get("prompt_draft_meta") or {}
if meta.get("based_on") == _scene_prompt_hash(scene):
fresh_draft = candidate
if explicit_prompt is not None:
prompt = explicit_prompt
elif fresh_draft:
prompt = fresh_draft
else:
await event_bus.publish(
"agent.thinking",
{
"message": f"H3 prompt rewrite · scene {scene.get('order_index')}",
"project_id": project_id,
},
)
prompt = await _h3_rewrite(scene, subjects)
values = smart_fill_inputs(
inputs,
prompt=prompt,
ref_images=ref_paths,
duration=duration,
extra=extra_values,
)
else:
prompt = (prompts or {}).get(scene["id"]) or scene_video_prompt(
scene, characters
)
values = smart_fill_inputs(
inputs,
prompt=prompt,
character_image=char_image,
location_image=loc_image,
duration=duration,
extra=extra_values,
)
# Stored per-scene setups override smart-fill's context choices
# (e.g. an edited duration). smart_fill skips duration-role nodes
# in `extra` by design — re-apply them here, request values win.
explicit_final = {
**stored_values,
**{k: v for k, v in (input_values_override or {}).items() if v not in (None, "")},
}
for k, v in explicit_final.items():
if v not in (None, ""):
values[str(k)] = v
payload: dict[str, Any] = {"input_values": values, "prompt": prompt}
if session_id is not None:
payload["session_id"] = session_id
if scene.get("chain_from_prev"):
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",
workflow_id=workflow["id"] if workflow else None,
scene_id=scene["id"],
payload=payload,
)
if workflow and not scene.get("workflow_id"):
conn.execute(
"UPDATE scenes SET workflow_id = ? WHERE id = ?",
(workflow["id"], scene["id"]),
)
# Never hold a write lock across awaits (event publish below).
conn.commit()
jobs.append(job)
await event_bus.publish(
"job.created",
{
"job_id": job["id"],
"kind": "video",
"message": f"Scene · {scene.get('heading') or scene.get('order_index')}",
"project_id": project_id,
},
)
conn.commit()
finally:
conn.close()
return jobs