- Backend: workspace_id isolation for 14 model tables + safe migration/backfill - Backend: RBAC system with 4 roles and 23 permissions, seeded on startup - Backend: workspace admin endpoints (list/manage all workspaces) - Backend: admin user management API (CRUD, reset password) - Backend: billing API with subscription plans, usage tracking, rate limiting - Backend: fix system_logs.py UNION query and wrong column references - Backend: WebSocket JWT auth and workspace enforcement - Frontend: sidebar navigation replacing top dropdown menu - Frontend: user management page (Users.vue) for admins - Frontend: enhanced Workspaces.vue with admin table view - Frontend: workspace RBAC computed properties in user store - Android: agent marketplace, billing/subscription UI, onboarding wizard - Android: phone login, analytics tracker, crash handler, network diagnostics - Android: splash screen, encrypted token storage, app update enhancements - Docs: multi-tenant RBAC guide with 8 sections and role-permission matrix Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
46 lines
2.1 KiB
Python
46 lines
2.1 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")
|
|
workspace_id = Column(CHAR(36), ForeignKey("workspaces.id"), nullable=True, index=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,
|
|
"workspace_id": self.workspace_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,
|
|
}
|