"""conversation_archive 会话归档表 ORM 模型。""" from __future__ import annotations from datetime import datetime from typing import Any from sqlalchemy import BigInteger, DateTime, Index, JSON, String, Text, UniqueConstraint, func from sqlalchemy.orm import Mapped, mapped_column from model.base import Base class ConversationArchive(Base): """客服会话消息归档记录。""" __tablename__ = "conversation_archive" __table_args__ = ( UniqueConstraint("session_id", "message_id", name="uk_session_message"), Index("idx_session", "session_id"), Index("idx_user_time", "user_id", "create_time"), Index("idx_agent", "agent_type"), Index("idx_agent_run", "agent_run_id"), {"comment": "会话归档表(审计回溯 + Agent 持续学习素材)"}, ) id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) session_id: Mapped[str] = mapped_column(String(64), nullable=False) customer_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True) user_id: Mapped[int] = mapped_column(BigInteger, nullable=False) agent_type: Mapped[str] = mapped_column(String(32), nullable=False) role: Mapped[str] = mapped_column(String(16), nullable=False) content: Mapped[str | None] = mapped_column(Text, nullable=True) tool_calls: Mapped[list[dict[str, Any]] | None] = mapped_column(JSON, nullable=True) message_id: Mapped[str] = mapped_column(String(64), nullable=False) agent_run_id: Mapped[str | None] = mapped_column(String(64), nullable=True) trace_id: Mapped[str | None] = mapped_column(String(64), nullable=True) create_time: Mapped[datetime] = mapped_column( DateTime, server_default=func.now(), nullable=False )