Files
hunjing/backend/app/models/runtime.py
T
2026-08-21 16:00:13 +08:00

56 lines
2.7 KiB
Python

"""运行时 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)
character_id: Mapped[str] = mapped_column(String(80), ForeignKey("characters.id"), index=True)
session_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("chat_sessions.id"), index=True
)
status: Mapped[str] = mapped_column(String(40), nullable=False, default="queued", index=True)
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)