Files
aigc-home/calliope-backend/scripts/export_contracts.py
T
Benjiyaya ce0b4e1983 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.
2026-09-11 18:05:21 +08:00

105 lines
4.1 KiB
Python

"""Contract vocabulary export: single registry of names shared across layers.
Calliope's backend and frontend communicate through stringly-typed contracts
(agent tool names, harness event types, SSE bus events, ComfyUI roles,
prompt profiles, guard codes). Nothing type-checks across that boundary, so
a rename ships silently until a user clicks. This script emits the positive
allowlist — `contracts.json` — consumed by tests/test_contract_vocabulary.py,
which fails when the web bundle references a name that no longer exists.
Regenerate after ANY change to: roles.py alias tables, log.py event types,
harness tool registrations, event_bus.publish("…"), prompt profiles, guard codes:
python scripts/export_contracts.py
"""
from __future__ import annotations
import json
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "src" / "calliope"
OUT = ROOT / "contracts.json"
# ── roles (from comfyui/roles.py — parsed, not imported, so this script
# never needs the venv) ────────────────────────────────────────────────
_ROLE_BLOCK = re.compile(
r"(INPUT|OUTPUT)_ROLE_ALIASES[^=]*=\s*\{(.*?)\n\}", re.DOTALL
)
def _extract_roles() -> dict[str, list[str]]:
text = (SRC / "comfyui" / "roles.py").read_text(encoding="utf-8")
roles: dict[str, list[str]] = {"input": [], "output": []}
for kind, block in _ROLE_BLOCK.findall(text):
key = kind.lower()
for canon, aliases in re.findall(r'"([a-z0-9_-]+)"\s*:\s*frozenset\(\{(.*?)\}\)', block, re.DOTALL):
names = [canon] + re.findall(r'"([a-z0-9_-]+)"', aliases)
roles[key].extend(sorted(set(names)))
return roles
def _extract_event_types() -> list[str]:
text = (SRC / "agent" / "harness" / "log.py").read_text(encoding="utf-8")
return sorted(set(re.findall(r'^[A-Z_]+ = "([a-z/]+)"', text, re.MULTILINE)))
def _extract_tool_names() -> list[str]:
names: set[str] = set()
for plugin in (SRC / "agent" / "harness" / "plugins").glob("*.py"):
text = plugin.read_text(encoding="utf-8")
names.update(re.findall(r'name="([a-z_]+)"', text))
return sorted(names)
def _extract_sse_events() -> list[str]:
"""Every literal published on the SSE bus, across the whole backend."""
events: set[str] = set()
pattern = re.compile(r'(?:event_bus\.publish|publish_user_event)\(\s*\n?\s*"([a-z_.]+)"')
for py in SRC.rglob("*.py"):
text = py.read_text(encoding="utf-8")
events.update(pattern.findall(text))
return sorted(events)
def _extract_prompt_profiles() -> list[str]:
text = (SRC / "comfyui" / "profiles.py").read_text(encoding="utf-8") if (SRC / "comfyui" / "profiles.py").exists() else ""
if not text:
# profiles may live elsewhere; fall back to the canonical two from AGENTS.md
return ["prose", "minimax_h3_ref"]
return sorted(set(re.findall(r'"(prose|minimax_h3_ref)"', text)) or {"prose", "minimax_h3_ref"})
def _extract_guard_codes() -> list[str]:
# Guard codes are DEFINED in registry.py (avoids the circular import) and
# re-exported through harness/__init__.py — scan both.
text = (SRC / "agent" / "harness" / "__init__.py").read_text(encoding="utf-8")
text += "\n" + (SRC / "agent" / "harness" / "registry.py").read_text(encoding="utf-8")
return sorted(set(re.findall(r'GUARD_[A-Z_]+ = "([a-z_]+)"', text)))
def build_registry() -> dict[str, list[str]]:
return {
"input_roles": _extract_roles()["input"],
"output_roles": _extract_roles()["output"],
"agent_event_types": _extract_event_types(),
"agent_tool_names": _extract_tool_names(),
"sse_events": _extract_sse_events(),
"prompt_profiles": _extract_prompt_profiles(),
"guard_codes": _extract_guard_codes(),
}
def main() -> int:
registry = build_registry()
OUT.write_text(json.dumps(registry, indent=2) + "\n", encoding="utf-8")
total = sum(len(v) for v in registry.values())
print(f"wrote {OUT.relative_to(ROOT)} ({total} names)")
return 0
if __name__ == "__main__":
sys.exit(main())