Files
aiagent/backend/app/models/chat_message.py

44 lines
1.9 KiB
Python
Raw Normal View History

"""
Chat Message 持久化模型 保存 Agent 会话中的每条消息
"""
from sqlalchemy import Column, String, Text, Integer, DateTime, ForeignKey, Index, func
from sqlalchemy.dialects.mysql import CHAR
from app.core.database import Base
import uuid
class ChatMessage(Base):
"""聊天消息表"""
__tablename__ = "chat_messages"
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="消息ID")
session_id = Column(CHAR(36), nullable=False, comment="会话ID")
agent_id = Column(CHAR(36), ForeignKey("agents.id", ondelete="SET NULL"), nullable=True, comment="智能体ID")
user_id = Column(CHAR(36), ForeignKey("users.id", ondelete="SET NULL"), nullable=True, comment="用户ID")
role = Column(String(20), nullable=False, comment="角色: user/assistant/tool/system")
content = Column(Text, comment="消息内容")
tool_name = Column(String(100), nullable=True, comment="工具名称(仅tool消息)")
tool_input = Column(Text, nullable=True, comment="工具输入参数JSON(仅tool消息)")
tool_output = Column(Text, nullable=True, comment="工具输出结果(仅tool消息)")
iteration = Column(Integer, default=0, comment="Agent 迭代序号")
created_at = Column(DateTime, default=func.now(), comment="创建时间")
__table_args__ = (
Index("ix_chat_messages_session_created", "session_id", "created_at"),
)
def to_dict(self):
return {
"id": self.id,
"session_id": self.session_id,
"agent_id": self.agent_id,
"user_id": self.user_id,
"role": self.role,
"content": self.content,
"tool_name": self.tool_name,
"tool_input": self.tool_input,
"tool_output": self.tool_output,
"iteration": self.iteration,
"created_at": self.created_at.isoformat() if self.created_at else None,
}