177 lines
6.1 KiB
Python
177 lines
6.1 KiB
Python
"""编辑结构验证独立于时间线可执行性,越界草稿必须可以保存。"""
|
|
|
|
from typing import Literal
|
|
from pydantic import Field, model_validator, field_validator
|
|
from app.domain.timeline import Strict, Audio
|
|
from app.domain.stories import ID
|
|
from app.schemas.editor_audio import EditorAudioV2, EditorAudioV3
|
|
from app.domain.continuity import ProductionPolicy
|
|
|
|
|
|
class Binding(Strict):
|
|
asset_id: str = Field(min_length=1, max_length=64)
|
|
sha256: str | None = Field(default=None, pattern=r"^[0-9a-f]{64}$")
|
|
kind: Literal["image", "video", "audio", "text", "json"]
|
|
input_hash: str | None = None
|
|
context_hash: str | None = None
|
|
source: Literal["generated", "manual", "reused"]
|
|
locked: bool = False
|
|
source_run_id: str | None = None
|
|
source_job_id: str | None = None
|
|
generation_parameters: dict | None = None
|
|
duration_seconds: float | None = Field(default=None, ge=0, allow_inf_nan=False)
|
|
duration_frames: int | None = Field(default=None, ge=0)
|
|
sample_count: int | None = Field(default=None, ge=0)
|
|
|
|
|
|
class AudioSegment(Strict):
|
|
id: str = Field(pattern=ID)
|
|
shot_id: str = Field(pattern=ID)
|
|
text: str = Field(max_length=2000)
|
|
voice: str | None = None
|
|
speed: float | None = Field(default=None, ge=0.5, le=2, allow_inf_nan=False)
|
|
|
|
|
|
class EditorAudio(Strict):
|
|
mode: Literal["silent", "manual_track", "generated_narration"] = "silent"
|
|
voice: str = ""
|
|
speed: float = Field(default=1.0, ge=0.5, le=2, allow_inf_nan=False)
|
|
track: Audio | None = None
|
|
segments: list[AudioSegment] = Field(default_factory=list, max_length=1000)
|
|
|
|
|
|
class Cue(Strict):
|
|
id: str = Field(pattern=ID)
|
|
anchor: Literal["absolute", "shot", "audio_segment"]
|
|
shot_id: str | None = Field(default=None, pattern=ID)
|
|
segment_id: str | None = Field(default=None, pattern=ID)
|
|
start_frame: int
|
|
end_frame: int
|
|
text: str = Field(max_length=2000)
|
|
text_override: bool = False
|
|
timing_override: bool = False
|
|
|
|
@model_validator(mode="after")
|
|
def anchor_fields(self):
|
|
if (
|
|
self.anchor == "absolute"
|
|
and (self.shot_id or self.segment_id)
|
|
or self.anchor == "shot"
|
|
and (not self.shot_id or self.segment_id)
|
|
or self.anchor == "audio_segment"
|
|
and (not self.segment_id or self.shot_id)
|
|
):
|
|
raise ValueError("anchor fields are mutually exclusive")
|
|
return self
|
|
|
|
|
|
class EditorCaptions(Strict):
|
|
mode: Literal["none", "manual", "script", "ai_script"] = "none"
|
|
cues: list[Cue] = Field(default_factory=list, max_length=1000)
|
|
|
|
|
|
class EditorManifest(Strict):
|
|
editing_mode: Literal["auto", "manual"] = "auto"
|
|
base_run_id: str | None = Field(default=None, pattern=ID)
|
|
schema_version: Literal[1] = 1
|
|
source_kind: Literal["story", "workflow"]
|
|
spec: dict
|
|
shot_ids: list[str] = Field(min_length=1, max_length=1000)
|
|
bindings: dict[str, Binding] = Field(default_factory=dict, max_length=3000)
|
|
audio: EditorAudio = Field(default_factory=EditorAudio)
|
|
captions: EditorCaptions = Field(default_factory=EditorCaptions)
|
|
|
|
|
|
class BindingV2(Binding):
|
|
sample_rate: int | None = Field(default=None, gt=0)
|
|
provenance: dict | None = None
|
|
|
|
|
|
class EditorManifestV2(EditorManifest):
|
|
audio: EditorAudioV2 = Field(default_factory=EditorAudioV2)
|
|
schema_version: Literal[2] = 2
|
|
production: ProductionPolicy = Field(default_factory=ProductionPolicy)
|
|
bindings: dict[str, BindingV2] = Field(default_factory=dict, max_length=5000)
|
|
|
|
|
|
class CueV3(Cue):
|
|
anchor: Literal["absolute", "shot", "audio_segment", "utterance"]
|
|
utterance_id: str | None = Field(default=None, pattern=ID)
|
|
|
|
@model_validator(mode="after")
|
|
def utterance_anchor(self):
|
|
if (self.anchor == "utterance") != bool(self.utterance_id):
|
|
raise ValueError("utterance anchor requires utterance_id")
|
|
if self.anchor == "utterance" and (self.shot_id or self.segment_id):
|
|
raise ValueError("ambiguous utterance anchor")
|
|
return self
|
|
|
|
|
|
class EditorCaptionsV3(EditorCaptions):
|
|
cues: list[CueV3] = Field(default_factory=list, max_length=1000)
|
|
|
|
|
|
class EditorManifestV3(EditorManifestV2):
|
|
schema_version: Literal[3] = 3
|
|
audio: EditorAudioV3 = Field(default_factory=EditorAudioV3)
|
|
captions: EditorCaptionsV3 = Field(default_factory=EditorCaptionsV3)
|
|
|
|
|
|
def parse_editor_manifest(raw):
|
|
if isinstance(raw, EditorManifest):
|
|
return raw
|
|
if not isinstance(raw, dict):
|
|
raise ValueError("expected editor manifest object")
|
|
version = raw.get("schema_version", 1)
|
|
if version == 1:
|
|
return EditorManifest.model_validate(raw)
|
|
if version == 2:
|
|
return EditorManifestV2.model_validate(raw)
|
|
if version == 3:
|
|
return EditorManifestV3.model_validate(raw)
|
|
raise ValueError("unsupported editor version")
|
|
|
|
|
|
class SaveEditor(Strict):
|
|
expected_version: int = Field(ge=1)
|
|
manifest: EditorManifest | EditorManifestV2 | EditorManifestV3
|
|
name: str | None = Field(default=None, min_length=1, max_length=255)
|
|
|
|
@field_validator("manifest", mode="before")
|
|
@classmethod
|
|
def parse_manifest_version(cls, raw):
|
|
return parse_editor_manifest(raw)
|
|
|
|
@field_validator("name")
|
|
@classmethod
|
|
def normalized_name(cls, value):
|
|
if value is None:
|
|
return None
|
|
value = value.strip()
|
|
if not value:
|
|
raise ValueError("name must not be blank")
|
|
return value
|
|
|
|
|
|
class RestoreEditor(Strict):
|
|
expected_version: int = Field(ge=1)
|
|
run_id: str = Field(min_length=1, max_length=32)
|
|
|
|
|
|
class PlanEditor(Strict):
|
|
expected_version: int = Field(ge=1)
|
|
output_goal: Literal["media", "film"] = "film"
|
|
targets: list[str] = Field(default_factory=list, max_length=3002)
|
|
force: list[str] = Field(default_factory=list, max_length=3002)
|
|
execution_mode: Literal["interactive", "batch"] = "interactive"
|
|
output_folder_id: str | None = None
|
|
|
|
|
|
class StartEditor(PlanEditor):
|
|
expected_plan_hash: str = Field(pattern=r"^[0-9a-f]{64}$")
|
|
|
|
|
|
class EditorMode(Strict):
|
|
expected_version: int = Field(ge=1)
|
|
mode: Literal["auto", "manual"]
|