Files
hunjing/backend/app/models/runtime.py
T

61 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""运行时 Run/Event 模型。"""
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.core.ids import new_event_id, new_run_id
from app.core.time import utc_now
from app.db.base import Base
class Run(Base):
__tablename__ = "runs"
id: Mapped[str] = mapped_column(String(80), primary_key=True, default=new_run_id)
# rewrite/1.0:照心试演(kind=rehearsal)无正式人物,character_id 允许为空;
# 旧 chat_sessions 外键随线性会话模型退役移除
character_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("characters.id"), index=True, nullable=True
)
status: Mapped[str] = mapped_column(String(40), nullable=False, default="queued", index=True)
# run.kind:foreground / background_task / awakening / rehearsal(契约词典 §8.2)
kind: Mapped[str] = mapped_column(
String(40), nullable=False, default="foreground", server_default="foreground"
)
goal: Mapped[str | None] = mapped_column(String(300))
origin: Mapped[str] = mapped_column(String(40), nullable=False, default="chat")
model_hint: Mapped[str | None] = mapped_column(String(120))
started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
error_code: Mapped[str | None] = mapped_column(String(80))
error_message: Mapped[str | None] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=utc_now, onupdate=utc_now
)
class Event(Base):
__tablename__ = "events"
__table_args__ = (UniqueConstraint("run_id", "sequence", name="uq_events_run_sequence"),)
id: Mapped[str] = mapped_column(String(80), primary_key=True, default=new_event_id)
character_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("characters.id"), index=True, nullable=True
)
run_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("runs.id"), index=True, nullable=True
)
type: Mapped[str] = mapped_column(String(80), nullable=False, index=True)
source: Mapped[str] = mapped_column(String(40), nullable=False, default="system")
payload_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}")
visibility: Mapped[str] = mapped_column(String(40), nullable=False, default="public")
sensitivity: Mapped[str] = mapped_column(String(40), nullable=False, default="low")
trace_id: Mapped[str | None] = mapped_column(String(80), index=True)
parent_id: Mapped[str | None] = mapped_column(String(80))
sequence: Mapped[int] = mapped_column(Integer, nullable=False)
evidence_ids_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)