115 lines
5.4 KiB
Python
115 lines
5.4 KiB
Python
"""数据模型。商用切 MySQL/PG 只需改 DATABASE_URL,表结构不变。"""
|
|
import datetime as dt
|
|
|
|
from sqlalchemy import (String, Boolean, Integer, Text, DateTime, ForeignKey,
|
|
JSON, UniqueConstraint)
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from ..db.session import Base
|
|
|
|
|
|
def now():
|
|
return dt.datetime.utcnow()
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
username: Mapped[str] = mapped_column(String(64), unique=True)
|
|
password_hash: Mapped[str] = mapped_column(String(128))
|
|
# 用户密码盐,用于派生加密密钥(明文盐可存,派生密钥不存)
|
|
kdf_salt: Mapped[str] = mapped_column(String(64))
|
|
is_admin: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
deleted_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
|
|
|
|
|
|
class SiteSetting(Base):
|
|
"""管理员权限开关,key-value。键值见 api/routers/admin.py"""
|
|
__tablename__ = "site_settings"
|
|
key: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
value: Mapped[str] = mapped_column(String(256))
|
|
|
|
|
|
class Channel(Base):
|
|
"""AI 渠道。管理员全局渠道 or 用户 BYOK 私有渠道(owner_id 非空)。"""
|
|
__tablename__ = "channels"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(128))
|
|
base_url: Mapped[str] = mapped_column(String(256))
|
|
api_key_enc: Mapped[str] = mapped_column(Text) # 加密存储
|
|
# 槽位模型映射: narrative / extract / tts / image
|
|
models: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
owner_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
|
|
visibility: Mapped[str] = mapped_column(String(16), default="admin") # public/granted/admin
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
|
|
|
|
class ChannelGrant(Base):
|
|
__tablename__ = "channel_grants"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
channel_id: Mapped[int] = mapped_column(ForeignKey("channels.id"))
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
|
|
|
|
|
class Character(Base):
|
|
__tablename__ = "characters"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
name: Mapped[str] = mapped_column(String(128))
|
|
persona: Mapped[str] = mapped_column(Text, default="") # 人设
|
|
greeting: Mapped[str] = mapped_column(Text, default="") # 开场白
|
|
voice: Mapped[str] = mapped_column(String(64), default="tongtong") # 音色
|
|
avatar_url: Mapped[str] = mapped_column(Text, default="")
|
|
source: Mapped[str] = mapped_column(String(16), default="manual") # manual / novel_auto
|
|
novel_id: Mapped[int | None] = mapped_column(nullable=True)
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
|
|
|
|
class Memory(Base):
|
|
"""角色记忆,双轨: fact(设定类) / event(事件类)。全部可查可改。"""
|
|
__tablename__ = "memories"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
character_id: Mapped[int] = mapped_column(ForeignKey("characters.id"), index=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
kind: Mapped[str] = mapped_column(String(16), default="fact")
|
|
content_enc: Mapped[str] = mapped_column(Text)
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
|
|
|
|
class WorldbookEntry(Base):
|
|
__tablename__ = "worldbook_entries"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
scope: Mapped[str] = mapped_column(String(32), default="character") # character/novel
|
|
scope_id: Mapped[int] = mapped_column(Integer, default=0)
|
|
keywords: Mapped[str] = mapped_column(String(512), default="") # 逗号分隔
|
|
content_enc: Mapped[str] = mapped_column(Text)
|
|
constant: Mapped[bool] = mapped_column(Boolean, default=False) # 常驻
|
|
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
|
|
class ChatSession(Base):
|
|
"""mode: roleplay(单角色) / novel(动态小说)"""
|
|
__tablename__ = "chat_sessions"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
mode: Mapped[str] = mapped_column(String(16), default="roleplay")
|
|
title: Mapped[str] = mapped_column(String(256), default="新会话")
|
|
character_id: Mapped[int | None] = mapped_column(nullable=True)
|
|
novel_setting_enc: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
channel_id: Mapped[int | None] = mapped_column(nullable=True)
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
|
|
|
|
class Message(Base):
|
|
__tablename__ = "messages"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
session_id: Mapped[int] = mapped_column(ForeignKey("chat_sessions.id"), index=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
role: Mapped[str] = mapped_column(String(16)) # user / assistant / system / narrator
|
|
content_enc: Mapped[str] = mapped_column(Text)
|
|
speaker: Mapped[str | None] = mapped_column(String(128), nullable=True) # 小说模式中的说话角色
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|