agent.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from sqlalchemy import DateTime, ForeignKey, Integer, JSON, String, Text
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from app.db.base import Base, TimestampMixin, UUIDPrimaryKeyMixin, utcnow
  6. class AgentSession(UUIDPrimaryKeyMixin, TimestampMixin, Base):
  7. __tablename__ = "agent_sessions"
  8. mine_id: Mapped[str | None] = mapped_column(
  9. String(32), ForeignKey("mines.id", ondelete="SET NULL"), nullable=True, index=True
  10. )
  11. user_name: Mapped[str] = mapped_column(String(128), default="default", index=True)
  12. thread_id: Mapped[str] = mapped_column(String(64), unique=True, index=True)
  13. title: Mapped[str] = mapped_column(String(255), default="")
  14. workflow: Mapped[str] = mapped_column(String(64), default="chat")
  15. status: Mapped[str] = mapped_column(String(32), default="active", index=True)
  16. context: Mapped[dict] = mapped_column(JSON, default=dict)
  17. last_message_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
  18. class AgentRun(UUIDPrimaryKeyMixin, Base):
  19. __tablename__ = "agent_runs"
  20. session_id: Mapped[str] = mapped_column(
  21. String(32), ForeignKey("agent_sessions.id", ondelete="CASCADE"), index=True
  22. )
  23. run_id: Mapped[str] = mapped_column(String(64), unique=True, index=True)
  24. workflow: Mapped[str] = mapped_column(String(64), index=True)
  25. status: Mapped[str] = mapped_column(String(32), default="running", index=True)
  26. input_payload: Mapped[dict] = mapped_column(JSON, default=dict)
  27. output_payload: Mapped[dict] = mapped_column(JSON, default=dict)
  28. error: Mapped[str] = mapped_column(Text, default="")
  29. model_name: Mapped[str] = mapped_column(String(128), default="")
  30. input_tokens: Mapped[int] = mapped_column(Integer, default=0)
  31. output_tokens: Mapped[int] = mapped_column(Integer, default=0)
  32. started_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
  33. finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)