98 lines
3.5 KiB
Python
98 lines
3.5 KiB
Python
"""
|
||
AI对话系统 - 数据库模型
|
||
"""
|
||
from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, ForeignKey, JSON
|
||
from sqlalchemy.ext.declarative import declarative_base
|
||
from sqlalchemy.orm import sessionmaker, relationship
|
||
from datetime import datetime
|
||
import os
|
||
|
||
DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:///./ai_chat.db')
|
||
|
||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||
Base = declarative_base()
|
||
|
||
|
||
class User(Base):
|
||
"""用户表 - 支持网页用户和Matrix用户"""
|
||
__tablename__ = 'users'
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
user_id = Column(String(100), unique=True, index=True) # 唯一标识:web_xxx 或 @xxx:matrix.org
|
||
display_name = Column(String(100))
|
||
user_type = Column(String(20), default='web') # web 或 matrix
|
||
matrix_user_id = Column(String(200), nullable=True) # Matrix用户ID
|
||
created_at = Column(DateTime, default=datetime.utcnow)
|
||
last_active_at = Column(DateTime, default=datetime.utcnow)
|
||
is_active = Column(Boolean, default=True)
|
||
|
||
conversations = relationship("Conversation", back_populates="user")
|
||
|
||
|
||
class Conversation(Base):
|
||
"""对话会话"""
|
||
__tablename__ = 'conversations'
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
conversation_id = Column(String(100), unique=True, index=True) # 会话唯一标识
|
||
user_id = Column(Integer, ForeignKey('users.id'))
|
||
title = Column(String(200), nullable=True)
|
||
created_at = Column(DateTime, default=datetime.utcnow)
|
||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||
is_active = Column(Boolean, default=True)
|
||
extra_data = Column(JSON, nullable=True) # 额外元数据
|
||
|
||
user = relationship("User", back_populates="conversations")
|
||
messages = relationship("Message", back_populates="conversation")
|
||
|
||
|
||
class Message(Base):
|
||
"""消息表"""
|
||
__tablename__ = 'messages'
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
conversation_id = Column(Integer, ForeignKey('conversations.id'))
|
||
role = Column(String(20)) # user, assistant, system
|
||
content = Column(Text)
|
||
source = Column(String(20)) # web, matrix
|
||
created_at = Column(DateTime, default=datetime.utcnow)
|
||
extra_data = Column(JSON, nullable=True) # 额外元数据(如Matrix event_id)
|
||
|
||
conversation = relationship("Conversation", back_populates="messages")
|
||
|
||
|
||
class SystemConfig(Base):
|
||
"""系统配置表"""
|
||
__tablename__ = 'system_config'
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
key = Column(String(100), unique=True, index=True)
|
||
value = Column(Text)
|
||
description = Column(String(500))
|
||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||
|
||
|
||
class MatrixRoomMapping(Base):
|
||
"""Matrix房间映射 - 绑定房间到用户会话"""
|
||
__tablename__ = 'matrix_room_mapping'
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
room_id = Column(String(200), unique=True, index=True) # Matrix房间ID
|
||
user_id = Column(Integer, ForeignKey('users.id'))
|
||
conversation_id = Column(Integer, ForeignKey('conversations.id'))
|
||
created_at = Column(DateTime, default=datetime.utcnow)
|
||
|
||
|
||
def init_db():
|
||
"""初始化数据库"""
|
||
Base.metadata.create_all(bind=engine)
|
||
|
||
|
||
def get_db():
|
||
"""获取数据库会话"""
|
||
db = SessionLocal()
|
||
try:
|
||
yield db
|
||
finally:
|
||
db.close() |