163 lines
8.1 KiB
Python
163 lines
8.1 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 / imported
|
|
novel_id: Mapped[int | None] = mapped_column(nullable=True)
|
|
# 广场/分享
|
|
is_public: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
share_code: Mapped[str | None] = mapped_column(String(32), unique=True, nullable=True)
|
|
tags: Mapped[str] = mapped_column(String(256), default="") # 逗号分隔标签
|
|
usage_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
# 公开卡片展示用的明文字段(发布时由作者密钥解密写入)
|
|
public_persona: Mapped[str] = mapped_column(Text, default="")
|
|
public_greeting: Mapped[str] = mapped_column(Text, default="")
|
|
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(动态小说) / group(群聊) / call(语音通话)"""
|
|
__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)
|
|
# 扩展
|
|
group_member_ids: Mapped[str] = mapped_column(String(512), default="") # 逗号分隔角色id
|
|
prompt_suffix: Mapped[str] = mapped_column(Text, default="") # 用户自定义指令模板
|
|
bg_url: Mapped[str] = mapped_column(Text, default="") # 聊天背景
|
|
pinned: Mapped[bool] = mapped_column(Boolean, default=False) # 置顶
|
|
archived: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
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) # 小说模式中的说话角色
|
|
# swipe 多版本:JSON 数组存备选回复,active 为当前选中下标
|
|
swipes_enc: Mapped[str | None] = mapped_column(Text, nullable=True) # JSON [str]
|
|
active_swipe: Mapped[int] = mapped_column(Integer, default=0)
|
|
reply_to_id: Mapped[int | None] = mapped_column(nullable=True) # 引用回复
|
|
starred: Mapped[bool] = mapped_column(Boolean, default=False) # 收藏
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
|
|
|
|
class Moment(Base):
|
|
"""角色朋友圈"""
|
|
__tablename__ = "moments"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
character_id: Mapped[int] = mapped_column(ForeignKey("characters.id"), index=True)
|
|
content_enc: Mapped[str] = mapped_column(Text)
|
|
image_url: Mapped[str] = mapped_column(Text, default="")
|
|
liked_by_me: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
|
|
|
|
class MomentComment(Base):
|
|
__tablename__ = "moment_comments"
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
moment_id: Mapped[int] = mapped_column(ForeignKey("moments.id"), index=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
author: Mapped[str] = mapped_column(String(128)) # "me" 或角色名
|
|
content_enc: Mapped[str] = mapped_column(Text)
|
|
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
|
|
|
|
|
class UserPrefs(Base):
|
|
"""用户偏好:主题/字号/气泡样式等"""
|
|
__tablename__ = "user_prefs"
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), primary_key=True)
|
|
data: Mapped[str] = mapped_column(Text, default="{}") # JSON
|