性能: 共享httpx连接池/设置缓存/复合索引自动补建/GZip/分页 ; 商业化v1: APIKey(sha256+scope+限流)/用量账本/版本化端点
This commit is contained in:
+33
-1
@@ -2,7 +2,7 @@
|
||||
import datetime as dt
|
||||
|
||||
from sqlalchemy import (String, Boolean, Integer, Text, DateTime, ForeignKey,
|
||||
JSON, UniqueConstraint)
|
||||
JSON, UniqueConstraint, Index)
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from ..db.session import Base
|
||||
@@ -78,6 +78,7 @@ class Character(Base):
|
||||
class Memory(Base):
|
||||
"""角色记忆,双轨: fact(设定类) / event(事件类)。全部可查可改。"""
|
||||
__tablename__ = "memories"
|
||||
__table_args__ = (Index("ix_mem_user_char", "user_id", "character_id"),)
|
||||
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)
|
||||
@@ -101,6 +102,7 @@ class WorldbookEntry(Base):
|
||||
class ChatSession(Base):
|
||||
"""mode: roleplay(单角色) / novel(动态小说) / group(群聊) / call(语音通话)"""
|
||||
__tablename__ = "chat_sessions"
|
||||
__table_args__ = (Index("ix_sess_user_pin", "user_id", "pinned", "id"),)
|
||||
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")
|
||||
@@ -119,6 +121,10 @@ class ChatSession(Base):
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
__table_args__ = (
|
||||
Index("ix_msg_sess_user_time", "session_id", "user_id", "id"),
|
||||
Index("ix_msg_user_time", "user_id", "id"),
|
||||
)
|
||||
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)
|
||||
@@ -136,6 +142,7 @@ class Message(Base):
|
||||
class Moment(Base):
|
||||
"""角色朋友圈"""
|
||||
__tablename__ = "moments"
|
||||
__table_args__ = (Index("ix_moment_user_time", "user_id", "id"),)
|
||||
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)
|
||||
@@ -160,3 +167,28 @@ 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
|
||||
|
||||
|
||||
class ApiKey(Base):
|
||||
"""商业化预留:第三方 API 密钥。scope: chat / characters / full"""
|
||||
__tablename__ = "api_keys"
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
||||
key_hash: Mapped[str] = mapped_column(String(64), unique=True) # sha256,不存明文
|
||||
name: Mapped[str] = mapped_column(String(128), default="default")
|
||||
scope: Mapped[str] = mapped_column(String(32), default="chat") # chat / full
|
||||
rate_limit: Mapped[int] = mapped_column(Integer, default=60) # req/min
|
||||
revoked: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
|
||||
last_used_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
|
||||
|
||||
|
||||
class UsageLog(Base):
|
||||
"""商业化预留:API 用量账本(计费/配额的数据底座)"""
|
||||
__tablename__ = "usage_logs"
|
||||
__table_args__ = (Index("ix_usage_user_time", "user_id", "id"),)
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
||||
api_key_id: Mapped[int | None] = mapped_column(nullable=True)
|
||||
endpoint: Mapped[str] = mapped_column(String(128))
|
||||
cost_units: Mapped[int] = mapped_column(Integer, default=1) # 计费单位(暂为次数)
|
||||
|
||||
Reference in New Issue
Block a user