agent_models.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from datetime import UTC, datetime
  2. from sqlalchemy import JSON, ForeignKey, Index, String, Text
  3. from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
  4. from app.infrastructure.mysql.types import UTCDateTime
  5. def utc_now() -> datetime:
  6. return datetime.now(UTC)
  7. class AgentBase(DeclarativeBase):
  8. pass
  9. class AgentThreadRecord(AgentBase):
  10. __tablename__ = "agent_threads"
  11. __table_args__ = (
  12. Index("ix_agent_threads_owner_updated", "owner_type", "owner_id", "updated_at"),
  13. )
  14. id: Mapped[str] = mapped_column(String(26), primary_key=True)
  15. owner_type: Mapped[str] = mapped_column(String(16), nullable=False)
  16. owner_id: Mapped[str] = mapped_column(String(26), nullable=False)
  17. persona: Mapped[str] = mapped_column(String(32), nullable=False)
  18. title: Mapped[str] = mapped_column(String(128), nullable=False, default="")
  19. status: Mapped[str] = mapped_column(String(32), nullable=False, default="ACTIVE")
  20. created_at: Mapped[datetime] = mapped_column(UTCDateTime(), nullable=False, default=utc_now)
  21. updated_at: Mapped[datetime] = mapped_column(
  22. UTCDateTime(), nullable=False, default=utc_now, onupdate=utc_now
  23. )
  24. class AgentMessageRecord(AgentBase):
  25. __tablename__ = "agent_messages"
  26. __table_args__ = (Index("ix_agent_messages_thread_created", "thread_id", "created_at"),)
  27. id: Mapped[str] = mapped_column(String(26), primary_key=True)
  28. thread_id: Mapped[str] = mapped_column(
  29. String(26), ForeignKey("agent_threads.id"), nullable=False
  30. )
  31. role: Mapped[str] = mapped_column(String(16), nullable=False)
  32. content: Mapped[str] = mapped_column(Text, nullable=False)
  33. metadata_json: Mapped[dict[str, object]] = mapped_column(JSON, nullable=False)
  34. created_at: Mapped[datetime] = mapped_column(UTCDateTime(), nullable=False, default=utc_now)
  35. class AgentRunRecord(AgentBase):
  36. __tablename__ = "agent_runs"
  37. __table_args__ = (
  38. Index("ix_agent_runs_thread_created", "thread_id", "created_at"),
  39. Index("ix_agent_runs_trace", "trace_id"),
  40. )
  41. id: Mapped[str] = mapped_column(String(26), primary_key=True)
  42. request_id: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
  43. thread_id: Mapped[str] = mapped_column(
  44. String(26), ForeignKey("agent_threads.id"), nullable=False
  45. )
  46. trace_id: Mapped[str | None] = mapped_column(String(64))
  47. persona: Mapped[str] = mapped_column(String(32), nullable=False)
  48. status: Mapped[str] = mapped_column(String(32), nullable=False)
  49. input_json: Mapped[dict[str, object]] = mapped_column(JSON, nullable=False)
  50. output_json: Mapped[dict[str, object] | None] = mapped_column(JSON)
  51. error_code: Mapped[str | None] = mapped_column(String(64))
  52. created_at: Mapped[datetime] = mapped_column(UTCDateTime(), nullable=False, default=utc_now)
  53. completed_at: Mapped[datetime | None] = mapped_column(UTCDateTime())