"""编辑结构验证独立于时间线可执行性,越界草稿必须可以保存。""" from typing import Literal from pydantic import Field, model_validator, field_validator, model_serializer 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) class CaptionPresentation(Strict): punctuation: Literal["hide", "preserve"] pacing: Literal["rhythm", "sentence"] class CaptionEdit(Strict): audio_sha256: str | None = Field( default=None, pattern=r"^[0-9a-f]{64}$", exclude_if=lambda x: x is None ) id: str = Field(pattern=ID) utterance_id: str = Field(pattern=ID) source_text: str = Field(min_length=1, max_length=2000) source_start: int = Field(ge=0) source_end: int = Field(gt=0) deleted: bool = False text_override: bool = False timing_override: bool = False text: str | None = Field(default=None, max_length=2000) start_frame: int | None = None end_frame: int | None = None @model_validator(mode="after") def valid_edit(self): if not self.source_start < self.source_end <= len(self.source_text): raise ValueError("字幕编辑的来源范围无效") if not self.deleted and self.text_override and not (self.text or "").strip(): raise ValueError("请填写保护的字幕文字") if ( not self.deleted and self.timing_override and (self.start_frame is None or self.end_frame is None) ): raise ValueError("请填写保护的镜内起止帧") return self class EditorCaptionsV4(EditorCaptionsV3): presentation: CaptionPresentation | None = None edits: list[CaptionEdit] | None = Field(default=None, max_length=3000) @model_validator(mode="after") def check_extension(self): if (self.presentation is None) != (self.edits is None): raise ValueError("字幕策略与编辑记录必须一起提交") if self.edits and len({e.id for e in self.edits}) != len(self.edits): raise ValueError("字幕编辑 ID 重复") return self @model_serializer(mode="wrap") def omit_absent(self, handler): data = handler(self) if self.presentation is None: data.pop("presentation", None) data.pop("edits", None) return data class EditorManifestV4(EditorManifestV3): schema_version: Literal[4] = 4 captions: EditorCaptionsV4 = Field(default_factory=EditorCaptionsV4) 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) if version == 4: return EditorManifestV4.model_validate(raw) raise ValueError("unsupported editor version") class SaveEditor(Strict): expected_version: int = Field(ge=1) manifest: EditorManifest | EditorManifestV2 | EditorManifestV3 | EditorManifestV4 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", "caption_film", "caption_inspect", "mix_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"] class ForkSpeakerCuts(Strict): expected_version: int = Field(ge=1) shot_id: str = Field(pattern=ID) confirm: bool = False name: str | None = Field(default=None, min_length=1, max_length=120) class ReviewTurns(Strict): expected_version: int = Field(ge=1) report_sha256: str = Field(pattern=r"^[0-9a-f]{64}$") decision: Literal["accept"] note: str = Field(min_length=1, max_length=1000) class PreviewCaptions(Strict): expected_version: int = Field(ge=1) utterance_id: str | None = Field(default=None, pattern=ID) manifest: EditorManifest | EditorManifestV2 | EditorManifestV3 | EditorManifestV4 | None = None @field_validator("manifest", mode="before") @classmethod def parse_preview_version(cls, value): return parse_editor_manifest(value) if value is not None else None