Files

103 lines
4.1 KiB
Python

"""
backend/app/models/chat.py
用途:对话会话、消息日志与反馈模型。
依赖:models.base.Base。
"""
from __future__ import annotations
from datetime import datetime
from typing import Optional
from sqlalchemy import DateTime, ForeignKey, Index, Integer, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base
class ChatSession(Base):
"""
聊天会话表
用途:独立持久化会话元数据,使生成中会话也能立即出现在历史记录中
"""
__tablename__ = 'chat_sessions'
__table_args__ = (
Index('ix_chat_sessions_user_agent_last_active', 'user_id', 'agent_id', 'last_active_at'),
)
id: Mapped[int] = mapped_column(primary_key=True)
session_id: Mapped[str] = mapped_column(String(100), unique=True, index=True)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), index=True)
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'), index=True)
title: Mapped[str] = mapped_column(String(255), default='')
status: Mapped[str] = mapped_column(String(20), default='running')
external_conversation_id: Mapped[Optional[str]] = mapped_column(String(100), index=True, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
last_active_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
class ChatLog(Base):
"""
对话日志表
用途:存储用户与 Agent 的对话记录
"""
__tablename__ = 'chat_logs'
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'))
query: Mapped[str] = mapped_column(Text)
answer: Mapped[str] = mapped_column(Text)
latency: Mapped[int] = mapped_column(default=0)
conversation_id: Mapped[Optional[str]] = mapped_column(String(100))
session_id: Mapped[Optional[str]] = mapped_column(String(100), index=True, nullable=True)
run_id: Mapped[Optional[int]] = mapped_column(ForeignKey('schedule_runs.id'), index=True, nullable=True)
message_type: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
payload: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
tokens_used: Mapped[int] = mapped_column(default=0)
retriever_resources: Mapped[Optional[list]] = mapped_column(JSON)
attribution_parts: Mapped[Optional[list]] = mapped_column(JSON)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
feedback: Mapped[Optional['Feedback']] = relationship(back_populates='chat_log', uselist=False)
class Feedback(Base):
"""
反馈表
用途:存储用户对回答的评分与评价
"""
__tablename__ = 'feedbacks'
id: Mapped[int] = mapped_column(primary_key=True)
chat_log_id: Mapped[int] = mapped_column(ForeignKey('chat_logs.id'), unique=True)
score: Mapped[int] = mapped_column(Integer)
comment: Mapped[Optional[str]] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
chat_log: Mapped['ChatLog'] = relationship(back_populates='feedback')
class GeneralFeedback(Base):
"""
通用意见反馈表
用于存储 Workspace/Chat 页面提交的通用意见反馈
"""
__tablename__ = 'general_feedbacks'
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), index=True)
chat_log_id: Mapped[Optional[int]] = mapped_column(ForeignKey('chat_logs.id'), nullable=True, index=True)
username: Mapped[str] = mapped_column(String(50), nullable=False)
feedback_type: Mapped[str] = mapped_column(String(32), nullable=False)
feedback_module: Mapped[str] = mapped_column(String(32), nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False)
source_page: Mapped[Optional[str]] = mapped_column(String(32))
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)