Long-form generation asked the LLM for the whole board in one response — a 50-beat story or 20-scene script is a multi-thousand-token JSON answer that small/local models take minutes to stream, often under-deliver, and then a single short chunk retried the ENTIRE thing. That is the "Draft Storyline / Regenerate Script hangs" bug. Both generators now write incrementally: - Script: SCRIPT_CHUNK (4) scenes per call, each fed the full context plus the last two written scenes for continuity. - Story: STORY_CHUNK (12) beats per call — a brief call (title, logline, cast, opening beats) then continuations given the established cast and recent beats. Both renumber order_index gaplessly, retry only a short chunk, publish agent.thinking progress per chunk, and keep single-call behavior for small boards. The generate_story/generate_script agent tools route through the same code, so the agent loop benefits too.
169 lines
5.3 KiB
Python
169 lines
5.3 KiB
Python
"""Chunked script generation: a big board is written in several modest LLM
|
|
calls (not one giant one that hangs), with progress events and per-chunk
|
|
renumbering."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
|
|
import pytest
|
|
|
|
import calliope.agent.script_agent as sa
|
|
from calliope.agent.script_agent import _generate_scenes_chunked
|
|
|
|
|
|
def _scenes(n: int, start: int = 1):
|
|
return {
|
|
"scenes": [
|
|
{
|
|
"order_index": start + i,
|
|
"heading": f"SCENE {start + i}",
|
|
"action": f"action {start + i}",
|
|
"dialog": "",
|
|
"duration_sec": 5,
|
|
"character_ids": [],
|
|
"location_id": None,
|
|
}
|
|
for i in range(n)
|
|
]
|
|
}
|
|
|
|
|
|
def test_big_board_splits_into_chunks(monkeypatch):
|
|
calls: list[int] = []
|
|
|
|
async def fake_structured(messages, temperature=0.7):
|
|
user = messages[1]["content"]
|
|
# THIS CHUNK: exactly N scenes
|
|
n = 0
|
|
for line in user.splitlines():
|
|
if line.startswith("THIS CHUNK:"):
|
|
n = int(line.split("exactly", 1)[1].split("scenes")[0].strip())
|
|
break
|
|
calls.append(n)
|
|
return _scenes(n)
|
|
|
|
monkeypatch.setattr(sa, "generate_structured", fake_structured)
|
|
out = asyncio.run(
|
|
_generate_scenes_chunked(
|
|
project_id=1,
|
|
p={"title": "T", "idea": "i", "target_duration": "2 minutes"},
|
|
beats=[],
|
|
characters=[],
|
|
locations=[],
|
|
required_scenes=9,
|
|
)
|
|
)
|
|
# 9 scenes at SCRIPT_CHUNK=4 → 4+4+1
|
|
assert calls == [4, 4, 1]
|
|
assert len(out) == 9
|
|
assert [s["order_index"] for s in out] == list(range(1, 10))
|
|
|
|
|
|
def test_small_board_single_call(monkeypatch):
|
|
calls: list[int] = []
|
|
|
|
async def fake_structured(messages, temperature=0.7):
|
|
n = 3
|
|
for line in messages[1]["content"].splitlines():
|
|
if line.startswith("THIS CHUNK:"):
|
|
n = int(line.split("exactly", 1)[1].split("scenes")[0].strip())
|
|
calls.append(n)
|
|
return _scenes(n)
|
|
|
|
monkeypatch.setattr(sa, "generate_structured", fake_structured)
|
|
out = asyncio.run(
|
|
_generate_scenes_chunked(
|
|
project_id=1,
|
|
p={"title": "T", "idea": "i", "target_duration": "30 seconds"},
|
|
beats=[],
|
|
characters=[],
|
|
locations=[],
|
|
required_scenes=3,
|
|
)
|
|
)
|
|
assert calls == [3]
|
|
assert len(out) == 3
|
|
|
|
|
|
def test_restarts_order_index_is_renumbered(monkeypatch):
|
|
"""A model that restarts order_index at 1 for every chunk must still
|
|
produce a 1..N board."""
|
|
|
|
async def fake_structured(messages, temperature=0.7):
|
|
# Always returns order_index 1..n regardless of the chunk requested
|
|
n = 0
|
|
for line in messages[1]["content"].splitlines():
|
|
if line.startswith("THIS CHUNK:"):
|
|
n = int(line.split("exactly", 1)[1].split("scenes")[0].strip())
|
|
return _scenes(n, start=1)
|
|
|
|
monkeypatch.setattr(sa, "generate_structured", fake_structured)
|
|
out = asyncio.run(
|
|
_generate_scenes_chunked(
|
|
project_id=1,
|
|
p={"title": "T", "idea": "i", "target_duration": "2 minutes"},
|
|
beats=[],
|
|
characters=[],
|
|
locations=[],
|
|
required_scenes=8,
|
|
)
|
|
)
|
|
assert [s["order_index"] for s in out] == list(range(1, 9))
|
|
|
|
|
|
def test_short_chunk_retries_only_that_chunk(monkeypatch):
|
|
calls: list[int] = []
|
|
|
|
async def fake_structured(messages, temperature=0.7):
|
|
n = 0
|
|
for line in messages[1]["content"].splitlines():
|
|
if line.startswith("THIS CHUNK:"):
|
|
n = int(line.split("exactly", 1)[1].split("scenes")[0].strip())
|
|
calls.append(n)
|
|
# First attempt for the 4-scene chunk returns only 2; retry is full.
|
|
if len(calls) == 1:
|
|
return _scenes(2)
|
|
return _scenes(n)
|
|
|
|
monkeypatch.setattr(sa, "generate_structured", fake_structured)
|
|
out = asyncio.run(
|
|
_generate_scenes_chunked(
|
|
project_id=1,
|
|
p={"title": "T", "idea": "i", "target_duration": "1 minute"},
|
|
beats=[],
|
|
characters=[],
|
|
locations=[],
|
|
required_scenes=6,
|
|
)
|
|
)
|
|
# chunk1 short (2) → retry chunk1 (4); then chunk2 (2)
|
|
assert calls == [4, 4, 2]
|
|
assert len(out) == 6
|
|
|
|
|
|
def test_continuity_tail_is_passed_to_next_chunk(monkeypatch):
|
|
seen_tails: list[bool] = []
|
|
|
|
async def fake_structured(messages, temperature=0.7):
|
|
user = messages[1]["content"]
|
|
seen_tails.append("Scenes already written" in user)
|
|
n = 0
|
|
for line in user.splitlines():
|
|
if line.startswith("THIS CHUNK:"):
|
|
n = int(line.split("exactly", 1)[1].split("scenes")[0].strip())
|
|
return _scenes(n)
|
|
|
|
monkeypatch.setattr(sa, "generate_structured", fake_structured)
|
|
asyncio.run(
|
|
_generate_scenes_chunked(
|
|
project_id=1,
|
|
p={"title": "T", "idea": "i", "target_duration": "1 minute"},
|
|
beats=[],
|
|
characters=[],
|
|
locations=[],
|
|
required_scenes=6,
|
|
)
|
|
)
|
|
# First chunk has no tail; the second continues from the first's scenes.
|
|
assert seen_tails == [False, True]
|