feat: release 1.5.0 — Build Scene 3D blockout composer + 1 scene : N shot clips

Build Scene (/build-scene): three.js + mannequin-js blockout viewport ported
from open-media, with per-session compositions (origin='scene' keeps rails
separate), agent shot_* tools as the only toolset for that surface, viewport
toolbar, keyframe motion + camera timeline with 60s video export, and
captures usable as project reference images.

Scene to Clips: clips table (1 scene : N renderable shots), coverage agent
breaks scenes into shots with full dialogue/action allocation, script
generation stays faithful to the screenplay (verbatim dialogue, content-
derived durations, chunked + durable inserts), video pipeline re-scoped to
clips (enqueue, continue-from, export, drafts), Script/Video UI clip-first
(#N.M labels, per-scene Break Into Shots scoped to one scene).

Agent loop: lean ReAct discipline with fail-streak escalation and budget
nudge; policy treats explicit user commands as permission; Build Scene tool
scope enforced mechanically; document + video attachments feed the VLM.
This commit is contained in:
Benjiyaya
2026-09-11 18:05:21 +08:00
parent f8faa72653
commit ce0b4e1983
90 changed files with 12482 additions and 913 deletions
@@ -4,9 +4,14 @@ from __future__ import annotations
from typing import Any
from calliope.agent.llm import generate_structured
from calliope.agent.prompts import build_script_chunk_messages, recommend_scene_count
from calliope.agent.prompts import (
build_script_chunk_messages,
estimate_scene_duration_sec,
estimate_target_seconds,
recommend_scene_count,
)
from calliope.config import settings
from calliope.db import get_db, row_to_dict
from calliope.db import ensure_default_clip, get_db, row_to_dict
from calliope.events.bus import event_bus
# Scenes per LLM call. A single 20-scene request means a multi-thousand-token
@@ -59,7 +64,7 @@ async def _request_chunk(
return scenes[:chunk_scenes]
async def _generate_scenes_chunked(
async def _iter_scene_chunks(
*,
project_id: int,
p: dict[str, Any],
@@ -67,7 +72,13 @@ async def _generate_scenes_chunked(
characters: list[dict[str, Any]],
locations: list[dict[str, Any]],
required_scenes: int,
) -> list[dict[str, Any]]:
):
"""Yield one list of scenes per chunk as each LLM call completes.
Yields promptly so `generate_script` can persist + commit chunk by chunk —
a crash mid-generation keeps every already-committed chunk instead of
losing the whole script. Raises at the end if the board came up short.
"""
plan: list[tuple[int, int]] = []
start = 1
while start <= required_scenes:
@@ -102,12 +113,35 @@ async def _generate_scenes_chunked(
for offset, scene in enumerate(scenes):
scene["order_index"] = chunk_start + offset
collected.extend(scenes)
yield scenes
if len(collected) < required_scenes:
raise ValueError(
f"Script returned {len(collected)} scenes but {required_scenes} were required. "
"Add scenes again and retry, or raise target duration."
)
async def _generate_scenes_chunked(
*,
project_id: int,
p: dict[str, Any],
beats: list[dict[str, Any]],
characters: list[dict[str, Any]],
locations: list[dict[str, Any]],
required_scenes: int,
) -> list[dict[str, Any]]:
"""Whole-board convenience wrapper over `_iter_scene_chunks`."""
collected: list[dict[str, Any]] = []
async for scenes in _iter_scene_chunks(
project_id=project_id,
p=p,
beats=beats,
characters=characters,
locations=locations,
required_scenes=required_scenes,
):
collected.extend(scenes)
return collected[:required_scenes]
@@ -146,6 +180,7 @@ def _persist_scenes(
),
)
scene_id = cur.lastrowid
ensure_default_clip(conn, scene_id, project_id)
for cid in scene.get("character_ids") or []:
exists = conn.execute(
"SELECT id FROM characters WHERE id = ? AND project_id = ?",
@@ -166,6 +201,7 @@ async def generate_script(
*,
replace: bool = True,
scene_count: int | None = None,
with_clips: bool = True,
) -> dict[str, Any]:
await event_bus.publish(
"agent.thinking", {"message": "Writing scene script…", "project_id": project_id}
@@ -213,33 +249,70 @@ async def generate_script(
"project_id": project_id,
},
)
target_seconds = estimate_target_seconds(p.get("target_duration"))
# Chunked generation: one LLM call per SCRIPT_CHUNK scenes. A single
# 20+ scene request means a multi-thousand-token JSON answer that
# small/local models take minutes to stream (or fail outright and
# trigger a full-script retry) — the "Regenerate Script hangs" bug.
# Small boards (one chunk) keep the exact single-call behavior.
scenes_out = await _generate_scenes_chunked(
created: list[dict[str, Any]] = []
first_chunk = True
async for scenes in _iter_scene_chunks(
project_id=project_id,
p=p,
beats=beats,
characters=characters,
locations=locations,
required_scenes=required_scenes,
)
):
if replace and first_chunk:
# Clear the old board only once the first replacement chunk is
# in hand — a failed first chunk leaves the old script intact.
scene_ids = [
r["id"]
for r in conn.execute(
"SELECT id FROM scenes WHERE project_id = ?", (project_id,)
).fetchall()
]
for sid in scene_ids:
conn.execute("DELETE FROM scene_characters WHERE scene_id = ?", (sid,))
conn.execute("DELETE FROM scenes WHERE project_id = ?", (project_id,))
first_chunk = False
created.extend(_persist_scenes(conn, project_id, scenes))
# Commit per chunk: every finished chunk is durable, so a crash,
# stop, or disconnect mid-generation keeps what was already paid
# for instead of losing the whole script in one failed transaction.
conn.commit()
await event_bus.publish(
"agent.thinking",
{
"message": f"Saved {len(created)}/{required_scenes} scenes…",
"project_id": project_id,
},
)
if replace:
scene_ids = [
r["id"]
for r in conn.execute(
"SELECT id FROM scenes WHERE project_id = ?", (project_id,)
).fetchall()
# Durations are OUR estimate from actual content, not the model's flat
# guess — and the user's target runtime is the total they sum to. Runs
# AFTER all chunks are committed, as a bulk UPDATE over the durable
# board (the rescale is global; the rows were persisted per chunk).
if target_seconds:
all_rows = conn.execute(
"SELECT id, action, dialog FROM scenes WHERE project_id = ? ORDER BY order_index",
(project_id,),
).fetchall()
estimates = [
estimate_scene_duration_sec({"action": r["action"], "dialog": r["dialog"]})
for r in all_rows
]
for sid in scene_ids:
conn.execute("DELETE FROM scene_characters WHERE scene_id = ?", (sid,))
conn.execute("DELETE FROM scenes WHERE project_id = ?", (project_id,))
created = _persist_scenes(conn, project_id, scenes_out)
total_est = sum(estimates)
if total_est > 0:
scale = max(target_seconds / total_est, 1.0)
for row, est in zip(all_rows, estimates):
conn.execute(
"UPDATE scenes SET duration_sec = ? WHERE id = ?",
(max(3, round(est * scale)), row["id"]),
)
conn.execute(
"UPDATE projects SET status = 'in_progress', updated_at = CURRENT_TIMESTAMP WHERE id = ?",
@@ -253,6 +326,44 @@ async def generate_script(
"project_id": project_id,
},
)
return {"ok": True, "scenes": created, "generated": {"scenes": scenes_out}}
# Break into shots: expand every fresh scene into renderable shot
# clips as part of the SAME tool call. `expand_scene_coverage`
# persists per scene (one commit per scene), so this stage is
# durable too — a crash keeps the script plus every clip already
# expanded. The default clip from `_persist_scenes` is what gets
# replaced here, so nothing is lost if this stage is interrupted.
clips_summary: list[dict[str, Any]] = []
if with_clips and created:
await event_bus.publish(
"agent.thinking",
{
"message": f"Breaking {len(created)} scenes into shot clips…",
"project_id": project_id,
},
)
from calliope.agent.coverage_agent import expand_scene_coverage
coverage = await expand_scene_coverage(
project_id,
[int(s["id"]) for s in created],
)
clips_summary = coverage.get("scenes") or []
total_clips = sum(int(r.get("clips") or 0) for r in clips_summary)
await event_bus.publish(
"agent.thinking",
{
"message": f"Shot list done — {total_clips} clips across "
f"{len(clips_summary)} scenes",
"project_id": project_id,
},
)
return {
"ok": True,
"scenes": created,
"clips": clips_summary,
"generated": {"scenes": created},
}
finally:
conn.close()