← Files PixVerseARCHIVED FILE

pixverse_runtime/core/template.py

5.39 KB · Nov 27, 2026 · 18:46 UTC

↓ Download file

See the change to this file →

"""Read-only template discovery and template-specific submission constraints."""

from __future__ import annotations

import subprocess
from typing import Any

from .shell import run_json

CANVAS_TEMPLATE_SOURCES = {19, 20, 21, 22}
TEMPLATE_FLAGS = frozenset(
    "--template-id --image --video --prompt -q --quality -d --duration --aspect-ratio --seed "
    "--count --off-peak --idempotency-key --no-wait --timeout --json -p --workspace-id "
    "--trace-id --region -h --help -V --version --".split()
)


def with_literal_prompt(argv: list[str]) -> list[str]:
    tokens = iter(argv)
    return ["--prompt=" + next(tokens, "") if token == "--prompt" else token for token in tokens]


def canvas_options(argv: list[str]) -> list[str]:
    args = with_literal_prompt(argv)
    for index, arg in enumerate(args[:-1]):
        if arg.startswith("--prompt=") and not args[index + 1].startswith("-"):
            raise ValueError("Unexpected positional argument after template prompt")
    return args


def option(argv: list[str], *names: str) -> str:
    argv = with_literal_prompt(argv)
    for index, value in enumerate(argv):
        for name in names:
            if value == name:
                following = argv[index + 1] if index + 1 < len(argv) else ""
                if following.partition("=")[0] in TEMPLATE_FLAGS:
                    return ""
                return following
            if value.startswith(name + "="):
                return value.split("=", 1)[1]
    return ""


def is_canvas_template(detail: dict[str, Any]) -> bool:
    return detail.get("template_source") in CANVAS_TEMPLATE_SOURCES


def template_detail(argv: list[str]) -> dict[str, Any]:
    raw_id = option(argv, "--template-id")
    if not raw_id.isdigit() or int(raw_id) <= 0:
        raise ValueError("Template creation requires a positive --template-id")
    region = option(argv, "--region")
    command = ["pixverse", "template", "info", raw_id, "--json"]
    if region:
        command.extend(["--region", region])
    try:
        (result, detail) = run_json(command, timeout=30)
    except subprocess.TimeoutExpired as exc:
        raise ValueError(
            "Template detail query timed out; refresh the catalog before generation"
        ) from exc
    if (
        not result.ok
        or not isinstance(detail, dict)
        or str(detail.get("template_id")) != str(int(raw_id))
    ):
        error = result.stderr or (
            str(detail.get("error") or "") if isinstance(detail, dict) else ""
        )
        raise ValueError(
            "Could not read the selected template detail; refresh login/catalog before generation. "
            + error[:500]
        )
    validate_template_options(argv, detail)
    return detail


def validate_template_options(argv: list[str], detail: dict[str, Any]) -> None:
    if is_canvas_template(detail):
        argv = canvas_options(argv)
        forbidden = {
            "--quality",
            "-q",
            "--duration",
            "-d",
            "--aspect-ratio",
            "--seed",
            "--count",
            "--off-peak",
            "--idempotency-key",
        }
        if any((arg.split("=", 1)[0] in forbidden for arg in argv)):
            raise ValueError(
                "This template has fixed output settings; omit quality/duration/aspect/seed/count/off-peak/idempotency overrides"
            )
        fields = detail.get("input_fields")
        if (
            not isinstance(fields, list)
            or not fields
            or any(
                (
                    not isinstance(item, dict)
                    or item.get("content_type") not in {"text", "image", "video"}
                    for item in fields
                )
            )
        ):
            raise ValueError("Template has no supported input_fields schema")
        text_fields = [item for item in fields if item.get("content_type") == "text"]
        if (
            any(
                item.get("required") is not False and not str(item.get("text") or "").strip()
                for item in text_fields
            )
            and not option(argv, "--prompt").strip()
        ):
            raise ValueError("Template requires user text without a default; provide --prompt")
        for kind in ("text", "video"):
            if sum((item.get("content_type") == kind for item in fields)) > 1:
                raise ValueError(
                    "This template has more video/text slots than the installed CLI supports"
                )


def output_media(detail: dict[str, Any]) -> str:
    return "image" if detail.get("template_type") == 2 else "video"


def template_outputs(payload: dict[str, Any]) -> tuple[list[dict[str, Any]], bool]:
    """Preserve every recoverable receipt, even in an invalid partial response."""
    raw = payload.get("outputs", payload.get("valid_outputs"))
    if not isinstance(raw, list):
        return ([], False)
    valid: list[dict[str, Any]] = []
    for item in raw:
        if not isinstance(item, dict):
            continue
        media_id = item.get("media_id")
        if (
            item.get("media_type") in {"image", "video"}
            and isinstance(media_id, int)
            and (not isinstance(media_id, bool))
            and (media_id > 0)
        ):
            receipt = {"media_id": media_id, "media_type": item["media_type"]}
            if receipt not in valid:
                valid.append(receipt)
    return (valid, bool(raw) and len(valid) == len(raw))

SHA-256: c24939133560fc9c225060b3e233597577e6ac9f50c747618ad0a928d241ec57