63 lines
3.1 KiB
Python
63 lines
3.1 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)
|
||
# rewrite/1.0:照心试演(kind=rehearsal)无正式人物,character_id 允许为空
|
||
character_id: Mapped[str | None] = mapped_column(
|
||
String(80), ForeignKey("characters.id"), index=True, nullable=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)
|
||
# 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)
|