nex_animation/backend/app/schemas/__init__.py

626 lines
19 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""请求/响应模型(Pydantic v2):与前端表单一一对应,字段名保持中文语义可读。"""
from __future__ import annotations
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
class Strict(BaseModel):
model_config = ConfigDict(extra="ignore")
# ---------------------------------------------------------------- 项目
class ProjectCreate(Strict):
name: str = Field(min_length=1, max_length=200)
description: str = ""
genre: str = ""
target_duration: float = 60.0
aspect_ratio: str = "16:9"
resolution: str = "1080p"
fps: int = 24
style_guide: str = ""
global_negative_prompt: str = ""
default_provider: str = "mock"
default_model: str = ""
default_transition: str = "cut"
settings: dict[str, Any] = Field(default_factory=dict)
# 建项目时顺带从平台资源库引入母版(人物/场景/声线)。
# 资源库里的定妆图、场景母图、音色样本是跨项目复用的,以前必须先建项目、
# 再跳到资源库页面勾选「引入到项目」,新项目的第一个镜头因此总是零素材起步。
platform_assets: list["PlatformRef"] = Field(default_factory=list)
class ProjectUpdate(Strict):
name: str | None = None
description: str | None = None
genre: str | None = None
target_duration: float | None = None
aspect_ratio: str | None = None
resolution: str | None = None
fps: int | None = None
style_guide: str | None = None
global_negative_prompt: str | None = None
default_provider: str | None = None
default_model: str | None = None
default_transition: str | None = None
status: str | None = None
settings: dict[str, Any] | None = None
# ---------------------------------------------------------------- 资产库(模块①)
class CharacterCreate(Strict):
name: str = Field(min_length=1, max_length=120)
code: str = ""
role: str = "配角"
gender: str = ""
personality: str = ""
appearance: str = ""
persona_prompt: str = ""
voice_profile: str = ""
default_voice_id: int | None = None
style_version: str = "v1"
tags: list[str] = Field(default_factory=list)
notes: str = ""
class CharacterUpdate(Strict):
name: str | None = None
code: str | None = None
role: str | None = None
gender: str | None = None
personality: str | None = None
appearance: str | None = None
persona_prompt: str | None = None
voice_profile: str | None = None
default_voice_id: int | None = None
style_version: str | None = None
tags: list[str] | None = None
notes: str | None = None
is_active: bool | None = None
class SceneCreate(Strict):
name: str = Field(min_length=1, max_length=160)
code: str = ""
description: str = ""
prompt: str = ""
lighting: str = ""
time_of_day: str = ""
palette: str = ""
tags: list[str] = Field(default_factory=list)
class SceneUpdate(Strict):
name: str | None = None
code: str | None = None
description: str | None = None
prompt: str | None = None
lighting: str | None = None
time_of_day: str | None = None
palette: str | None = None
tags: list[str] | None = None
class VoiceCreate(Strict):
name: str = Field(min_length=1, max_length=160)
provider: str = "mock"
provider_voice_id: str = ""
language: str = "zh-CN"
gender: str = ""
description: str = ""
tuning: dict[str, Any] = Field(default_factory=dict)
version: str = "v1"
is_default: bool = False
class VoiceUpdate(Strict):
name: str | None = None
provider: str | None = None
provider_voice_id: str | None = None
language: str | None = None
gender: str | None = None
description: str | None = None
tuning: dict[str, Any] | None = None
version: str | None = None
is_default: bool | None = None
class AssetNote(Strict):
name: str | None = None
note: str | None = None
class AssetGenerate(Strict):
target_type: Literal["human", "scene", "voice", "shot"]
target_id: int
kind: str
provider: str = ""
model: str = ""
prompt: str = ""
negative_prompt: str = ""
width: int = 1024
height: int = 1024
seed: int | None = None
sample_text: str = ""
reference_asset_ids: list[int] = Field(default_factory=list)
drain: bool = True
class TextAssist(Strict):
target_type: Literal["character", "scene", "storyboard"] = "character"
target_id: int | None = None
provider: str = ""
model: str = ""
instruction: str = ""
# ---------------------------------------------------------------- 分镜(模块②)
class ShotCreate(Strict):
title: str = ""
code: str = ""
synopsis: str = ""
description: str = ""
style_note: str = ""
shot_size: str = "medium"
camera_angle: str = "eye_level"
camera_movement: str = "static"
lens_note: str = ""
duration: float = 5.0
transition_out: str = "cut"
transition_duration_ms: int = 500
dialogue: str = ""
# 逐条台词 [{character_id, name, text}]:一个镜头里多人对话时,
# 每一行都要标出「是谁说的」,配音与字幕才能分行按各自的嗓子走。
lines: list[dict[str, Any]] | None = None
subtitle_text: str = ""
subtitle_enabled: bool = True
speaker_character_id: int | None = None
voice_asset_id: int | None = None
# 环境声(只填听得见的名词短语:键盘敲击声、安静的房间底噪)与镜头级音效素材
ambience: str = ""
sound_effects: list[dict[str, Any]] | None = None
provider: str | None = None
model: str | None = None
mode: str = "i2v"
seed: int | None = None
params: dict[str, Any] = Field(default_factory=dict)
order_index: int | None = None
class ShotUpdate(Strict):
title: str | None = None
code: str | None = None
synopsis: str | None = None
description: str | None = None
prompt: str | None = None
negative_prompt: str | None = None
style_note: str | None = None
shot_size: str | None = None
camera_angle: str | None = None
camera_movement: str | None = None
lens_note: str | None = None
duration: float | None = None
transition_out: str | None = None
transition_duration_ms: int | None = None
dialogue: str | None = None
lines: list[dict[str, Any]] | None = None
subtitle_text: str | None = None
subtitle_enabled: bool | None = None
speaker_character_id: int | None = None
voice_asset_id: int | None = None
ambience: str | None = None
sound_effects: list[dict[str, Any]] | None = None
provider: str | None = None
model: str | None = None
mode: str | None = None
seed: int | None = None
params: dict[str, Any] | None = None
order_index: int | None = None
class ShotBindings(Strict):
character_ids: list[int] | None = None
scene_ids: list[int] | None = None
speaker_character_id: int | None = None
voice_asset_id: int | None = None
class PromptEdit(Strict):
prompt: str | None = None
negative_prompt: str | None = None
reason: str = "人工调整提示词"
actor: str = "studio"
auto_sync: bool = False
class Reorder(Strict):
shot_ids: list[int]
class ShotBulk(Strict):
"""分镜批量操作:一次请求完成复制 / 删除 / 移动 / 统一改字段,避免前端逐条发请求。"""
action: Literal["duplicate", "delete", "move", "patch"]
shot_ids: list[int] = Field(default_factory=list)
delta: int = 0 # move:相对位移(-1 上移一位,-999 移到最前)
index: int | None = None # move:绝对落点(1 基,移动到第 N 镜)
fields: dict[str, Any] = Field(default_factory=dict) # patch 要统一写入的字段
class PreviewOptions(Strict):
"""镜头审片预览封装:画面用模型产物,声音换成平台配音。"""
shot_ids: list[int] | None = None
force: bool = False
actor: str = "studio"
class ImportOptions(Strict):
script_text: str = ""
script_name: str = ""
image_names: list[str] = Field(default_factory=list)
default_duration: float = 5.0
dry_run: bool = False
replace: bool = False
keep_existing: bool = True
# ---------------------------------------------------------------- 调度(模块③)
class PlanOptions(Strict):
"""任务清单预览(GET/POST /tasklist)。
注:曾有 `strict_preflight`,但「有阻塞则不入队」一直由 `allow_unready=False`
承担(见 dispatch.plan_task_list),该字段从未被读取,故删除。
"""
shot_ids: list[int] | None = None
force: bool = False
allow_unready: bool = False
include_voiceover: bool = True
class TaskCreate(Strict):
"""入队(POST /tasks)。
注:不再继承 PlanOptions——入队只有一个配音开关 `with_voiceover`
(同时用于清单里的 include_voiceover),旧 schema 里两个开关并存,
前端多出的「入队时同时排配音」勾了其实不生效。
"""
shot_ids: list[int] | None = None
force: bool = False
allow_unready: bool = False
priority: int = 5
with_voiceover: bool = True
actor: str = "studio"
class DispatchOptions(Strict):
limit: int | None = None
drain: bool = False
timeout: float = 600.0
class QCRun(Strict):
"""整批复检(POST /qc/run)。
质检规则升级后(例如新增画面质检),已生成甚至已审核通过的镜头也需要
重新过一遍,否则历史项目永远停留在旧标准的满分上。
"""
shot_ids: list[int] | None = None
include_approved: bool = True # 默认连「已审核通过」的一起重检
only_failed: bool = False # 只复查上次没过的镜头
drain: bool = True # inline 模式同步跑完再返回
timeout: float = 1800.0
class RetryOptions(Strict):
task_ids: list[int] | None = None
reset_attempts: bool = True
only_codes: list[str] | None = None
class CancelOptions(Strict):
reason: str = "人工取消"
class Approve(Strict):
notes: str = ""
reviewer: str = "studio"
class Reject(Strict):
notes: str = ""
reviewer: str = "studio"
prompt: str | None = None
negative_prompt: str | None = None
regenerate: bool = True
priority: int = 4
# ---------------------------------------------------------------- 合成(模块④)
class TimelineBuild(Strict):
name: str = ""
only_approved: bool = True
shot_ids: list[int] | None = None
settings: dict[str, Any] = Field(default_factory=dict)
class SegmentPatch(Strict):
duration_ms: int | None = None
transition_out: str | None = None
transition_duration_ms: int | None = None
volume: float | None = None
muted: bool | None = None
source_in_ms: int | None = None
label: str | None = None
class SubtitlePatch(Strict):
shot_id: int | None = None
text: str
speaker: str = ""
start_ms: int | None = None
end_ms: int | None = None
cue_id: int | None = None
class TimelineAudioAdd(Strict):
kind: Literal["voiceover", "bgm", "sfx"] = "bgm"
source_path: str = ""
label: str = ""
start_ms: int = 0
duration_ms: int = 0
gain_db: float = 0.0
loop: bool = False
ducking: bool = True
shot_id: int | None = None
class TimelineSettingsPatch(Strict):
settings: dict[str, Any]
class VoiceoverRequest(Strict):
shot_ids: list[int] | None = None
only_missing: bool = True
priority: int = 4
class ComposeRequest(Strict):
preset: str = "web_1080p"
name: str = ""
burn_subtitle: bool | None = None
subtitle_mode: str | None = None # off / sidecar / embed / burn / both,优先于 burn_subtitle
timeline_id: int | None = None
priority: int = 2
# ---------------------------------------------------------------- LLM 项目调度
class AgentPlanRequest(Strict):
message: str = Field(min_length=1, max_length=8000)
auto_approve: bool = False
# 思考型模型的思考预算:off=最快(关深度思考)/ low=限量思考(默认)/ high=不限
thinking: Literal["off", "low", "high"] = "low"
# 页面用 false:先落 planning 记录立即返回,规划与后续执行全部在服务端线程里推进,
# 浏览器刷新/断线都不会再让一次指令凭空消失。脚本可以 wait=true 同步拿结果。
wait: bool = True
# 计划生成后是否由服务端直接开始执行整条白名单(等价于「计划生成后立即执行」)
auto_execute: bool = False
timeout_seconds: int = Field(default=7200, ge=30, le=21600)
class AgentExecuteRequest(Strict):
# 默认异步返回,前端轮询 run;脚本/测试可 wait=true 同步等待。
wait: bool = False
timeout_seconds: int = Field(default=7200, ge=30, le=21600)
class AgentCancelRequest(Strict):
reason: str = "用户在项目主页取消智能调度"
# ---------------------------------------------------------------- 模型注册表
class ModelSpecCreate(Strict):
provider: str = Field(min_length=1, max_length=60)
model: str = ""
display_name: str = ""
media_type: str = "video"
adapter: str = "mock"
enabled: bool = True
is_default: bool = False
model_code: str = ""
timeout_seconds: int = Field(default=120, ge=1, le=3600)
api_key: str | None = None
max_concurrency: int = 2
requests_per_minute: int = 30
burst: int = 1
modes: list[str] = Field(default_factory=list)
resolutions: list[str] = Field(default_factory=list)
aspect_ratios: list[str] = Field(default_factory=list)
min_duration: float = 1.0
max_duration: float = 10.0
max_reference_images: int = 0
price_per_second: float = 0.0
price_per_1k_chars: float = 0.0
currency: str = "CNY"
default_params: dict[str, Any] = Field(default_factory=dict)
config: dict[str, Any] = Field(default_factory=dict)
notes: str = ""
class ModelSpecUpdate(Strict):
provider: str | None = None
model: str | None = None
display_name: str | None = None
media_type: str | None = None
adapter: str | None = None
enabled: bool | None = None
is_default: bool | None = None
status: str | None = None
model_code: str | None = None
timeout_seconds: int | None = Field(default=None, ge=1, le=3600)
api_key: str | None = None
clear_api_key: bool = False
max_concurrency: int | None = None
requests_per_minute: int | None = None
burst: int | None = None
modes: list[str] | None = None
resolutions: list[str] | None = None
aspect_ratios: list[str] | None = None
min_duration: float | None = None
max_duration: float | None = None
max_reference_images: int | None = None
price_per_second: float | None = None
price_per_1k_chars: float | None = None
currency: str | None = None
default_params: dict[str, Any] | None = None
config: dict[str, Any] | None = None
# config 默认是「增量合并」:只想调 megapixels 时不必回传整份配置。
# 传 replace_config=true 才做全量替换(用于删键、回到干净配置)。
replace_config: bool = False
notes: str | None = None
# ---------------------------------------------------------------- 平台数字资产
class PlatformCharacterCreate(CharacterCreate):
# 平台人物母版没有「性别」列(项目人物才有):这里排除继承来的字段,
# 否则 model_dump() 会带着 gender 去构造 ORM 对象而抛 TypeError。
gender: str = Field(default="", exclude=True)
is_active: bool = True
class PlatformCharacterUpdate(CharacterUpdate):
gender: str | None = Field(default=None, exclude=True)
class PlatformSceneCreate(SceneCreate):
is_active: bool = True
class PlatformSceneUpdate(SceneUpdate):
is_active: bool | None = None
class PlatformVoiceCreate(VoiceCreate):
provider: str = ""
provider_voice_id: str = ""
code: str = ""
tags: list[str] = Field(default_factory=list)
is_active: bool = True
class PlatformVoiceUpdate(VoiceUpdate):
code: str | None = None
tags: list[str] | None = None
is_active: bool | None = None
class PlatformImport(Strict):
project_id: int
asset_type: Literal["human", "scene", "voice"]
asset_id: int
name: str = ""
class PlatformPublish(Strict):
project_id: int
asset_type: Literal["human", "scene", "voice"]
asset_id: int
asset: dict[str, Any] = Field(default_factory=dict)
# 同名母版已存在时复用该行并升版;显式 False 才允许再建一份
reuse: bool = True
class PlatformRef(Strict):
owner_type: Literal["human", "scene", "voice"]
asset_id: int
class PlatformBatchDelete(Strict):
items: list[PlatformRef] = Field(default_factory=list)
class PlatformBatchTag(Strict):
items: list[PlatformRef] = Field(default_factory=list)
add: list[str] = Field(default_factory=list)
remove: list[str] = Field(default_factory=list)
class PlatformBatchImport(Strict):
project_id: int
items: list[PlatformRef] = Field(default_factory=list)
class PlatformMediaBatchDelete(Strict):
media_ids: list[int] = Field(default_factory=list)
class VersionAction(Strict):
"""版本级操作(设为当前 / 删除版本)的可选请求体,只用于记录操作者。"""
actor: str = "ui"
class PlatformGenerate(Strict):
kind: str
provider: str = ""
model: str = ""
prompt: str = ""
negative_prompt: str = ""
width: int = 1024
height: int = 1024
seed: int | None = None
sample_text: str = ""
reference_media_id: int | None = None
reference_media_ids: list[int] = Field(default_factory=list)
class PlatformTextAssist(Strict):
instruction: str = ""
provider: str = ""
model: str = ""
# ---------------------------------------------------------------- 用户体系
class LoginRequest(Strict):
username: str = Field(min_length=1, max_length=60)
password: str = Field(min_length=1, max_length=200)
class ProfileUpdate(Strict):
display_name: str | None = None
email: str | None = None
prefs: dict[str, Any] | None = None
class PasswordChange(Strict):
current_password: str = ""
new_password: str = Field(min_length=1, max_length=200)
class UserCreate(Strict):
username: str = Field(min_length=2, max_length=60)
password: str = Field(min_length=1, max_length=200)
display_name: str = ""
email: str = ""
role: str = "editor"
note: str = ""
class UserUpdate(Strict):
display_name: str | None = None
email: str | None = None
role: str | None = None
is_active: bool | None = None
note: str | None = None
class AdminPasswordReset(Strict):
new_password: str = Field(min_length=1, max_length=200)
must_change: bool = True
revoke_sessions: bool = True
# ProjectCreate.platform_assets 引用了下面才定义的 PlatformRef(同一模块内的前向引用),
# 在这里显式重建,避免等到第一次请求校验时才报「字段类型未知」。
ProjectCreate.model_rebuild()