cmvr-visual/backend/app/models/jobs.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

106 lines
5.4 KiB
Python

from datetime import datetime
from sqlalchemy import (
BigInteger,
Boolean,
ForeignKey,
Index,
Integer,
JSON,
String,
Text,
UniqueConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base, Timestamp, new_id, utcnow
class Job(Base):
__tablename__ = "visual_jobs"
__table_args__ = (
UniqueConstraint("owner_id", "request_key", "batch_index"),
Index("visual_jobs_dispatch", "state", "next_action_at"),
)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
folder_id: Mapped[str | None] = mapped_column(ForeignKey("visual_folders.id", ondelete="SET NULL"), nullable=True, index=True)
output_folder_id: Mapped[str | None] = mapped_column(ForeignKey("visual_folders.id", ondelete="SET NULL"), nullable=True, index=True)
owner_id: Mapped[str] = mapped_column(ForeignKey("visual_users.id"), index=True)
task_type: Mapped[str] = mapped_column(String(48))
execution_mode: Mapped[str] = mapped_column(String(16), default="interactive")
policy_version: Mapped[str] = mapped_column(String(32), default="night-v1")
run_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
step_key: Mapped[str | None] = mapped_column(String(32), nullable=True)
batch_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
batch_item_index: Mapped[int | None] = mapped_column(Integer, nullable=True)
explicit_attempt: Mapped[int] = mapped_column(Integer, default=1)
desired_state: Mapped[str] = mapped_column(String(16), default="run")
hold_reasons: Mapped[list] = mapped_column(JSON, default=list)
current_execution_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
admitted_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
state: Mapped[str] = mapped_column(String(24))
phase: Mapped[str] = mapped_column(String(48))
version: Mapped[int] = mapped_column(Integer, default=1)
input_json: Mapped[dict] = mapped_column(JSON)
parameters: Mapped[dict] = mapped_column(JSON)
request_key: Mapped[str | None] = mapped_column(String(128), nullable=True)
request_hash: Mapped[str] = mapped_column(String(64))
batch_index: Mapped[int] = mapped_column(Integer, default=0)
upstream_key: Mapped[str] = mapped_column(String(128), unique=True)
upstream_id: Mapped[str | None] = mapped_column(String(128), nullable=True, unique=True)
upstream_payload: Mapped[dict | None] = mapped_column(JSON, nullable=True)
upstream_result: Mapped[dict | None] = mapped_column(JSON, nullable=True)
result_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
error_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
cancel_requested: Mapped[bool] = mapped_column(Boolean, default=False)
attempt_count: Mapped[int] = mapped_column(Integer, default=0)
work_attempt: Mapped[int] = mapped_column(Integer, default=0)
preemptions: Mapped[int] = mapped_column(Integer, default=0)
last_preempted_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
sync_warning: Mapped[str | None] = mapped_column(String(512), nullable=True)
sync_failures: Mapped[int] = mapped_column(Integer, default=0)
next_action_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow)
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow, index=True)
scheduled_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
started_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
finished_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
deleted_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
class JobAsset(Base):
__tablename__ = "visual_job_assets"
job_id: Mapped[str] = mapped_column(ForeignKey("visual_jobs.id"), primary_key=True)
asset_id: Mapped[str] = mapped_column(ForeignKey("visual_assets.id"), primary_key=True)
class Event(Base):
__tablename__ = "visual_job_events"
__table_args__ = (Index("visual_events_owner_cursor", "owner_id", "id"),)
id: Mapped[int] = mapped_column(
BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True
)
owner_id: Mapped[str] = mapped_column(ForeignKey("visual_users.id"))
job_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
event_type: Mapped[str] = mapped_column(String(48))
data: Mapped[dict] = mapped_column(JSON)
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow)
class Execution(Base):
__tablename__ = "visual_job_executions"
__table_args__ = (UniqueConstraint("job_id", "ordinal"),)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
job_id: Mapped[str] = mapped_column(String(32))
ordinal: Mapped[int] = mapped_column(Integer)
phase_key: Mapped[str] = mapped_column(String(48))
resource_pool: Mapped[str] = mapped_column(String(32))
fence_token: Mapped[str] = mapped_column(String(64))
state: Mapped[str] = mapped_column(String(32), default="allocated")
manifest_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
started_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
finished_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
error_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)