Files
aiagent/backend/app/models/chat_message.py
renjianbo a06082480a
Some checks failed
CI/CD Pipeline / Backend — Lint & Test (push) Has been cancelled
CI/CD Pipeline / Frontend — Lint & Build (push) Has been cancelled
CI/CD Pipeline / Docker — Build Check (push) Has been cancelled
feat: persistent chat message storage + Android pull-to-load history
Backend:
- Add ChatMessage model + Alembic migration 024
- Add on_message callback to AgentRuntime for persisting messages during SSE streaming
- Plumb session_id from ChatRequest to AgentContext in all 4 chat endpoints
- Add GET /agent-chat/{id}/sessions and /sessions/{sid}/messages with cursor pagination

Android:
- Add DTOs/ApiService/MessageDao for server-side chat history
- ChatRepository: fetchOlderMessages (API + Room cache), offline fallback
- ChatViewModel: loadMoreHistory with isLoadingMore/hasMoreMessages state
- ChatScreen: scroll-to-top detection + top loading indicator

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-30 00:07:26 +08:00

44 lines
1.9 KiB
Python

"""
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,
}