Open Maopaoya M0-M2: 用户体系+权限开关+后台 / 渠道管理 / 单角色扮演(记忆可篡改+世界书+TTS) / 动态小说管线(实体抽取→建档→立绘→世界书注册) / AES-GCM数据加密+登录解密迁移 / 液态玻璃UI
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
.env
|
||||||
|
*.db
|
||||||
|
__pycache__/
|
||||||
|
.venv/
|
||||||
|
*.pyc
|
||||||
|
.alembic/
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
# Open Maopaoya 🦆
|
||||||
|
|
||||||
|
冒泡鸭开源复刻:AI 角色扮演平台 + 动态小说引擎(AI 编剧自主扩列建角色)。
|
||||||
|
|
||||||
|
## 快速启动
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv sync
|
||||||
|
uv run uvicorn app.main:app --port 8820
|
||||||
|
# 打开 http://localhost:8820
|
||||||
|
# 默认管理员: admin / maopaoya-admin(首次登录后请改)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 功能
|
||||||
|
- 单角色扮演:角色卡 / 记忆(完全透明可篡改)/ 世界书 / TTS
|
||||||
|
- 动态小说:AI 自主引入新角色 → 自动建档 → 生成立绘 → 注册世界书 → 新角色可单独对话
|
||||||
|
- 多渠道:OpenAI 兼容 API,内置 Step Plan 预设(叙事/抽取 step-3.5-flash,TTS stepaudio-2.5-tts,生图槽位留空可接任意兼容接口)
|
||||||
|
- 多用户:注册/注销开关、BYOK 开关、渠道分配、用户数据硬隔离
|
||||||
|
- 管理后台:用户管理、权限开关、「数据可见」开关
|
||||||
|
- 数据可见 OFF → 用户数据 AES-GCM 加密落库(密钥由用户密码派生,服务器不存明文),管理员只读密文
|
||||||
|
- OFF→ON → 存量密文在用户下次登录时自动解密转明文,静默不告知用户
|
||||||
|
- 修改密码时自动重加密;注销删除全部数据不可恢复
|
||||||
|
|
||||||
|
## 配置(.env,均带 OMO_ 前缀)
|
||||||
|
```
|
||||||
|
OMO_DATABASE_URL=sqlite+aiosqlite:///./data.db # 商用切: mysql+aiomysql://... / postgresql+asyncpg://...
|
||||||
|
OMO_JWT_SECRET=change-me
|
||||||
|
OMO_FIRST_ADMIN_USER=admin
|
||||||
|
OMO_FIRST_ADMIN_PASS=maopaoya-admin
|
||||||
|
```
|
||||||
|
|
||||||
|
## 商用扩展
|
||||||
|
- 数据库切换只需改 DATABASE_URL(可选依赖: `uv sync --extra mysql` 或 `--extra postgres`)
|
||||||
|
- 数据访问统一走 `app/db/repos.py` 仓储层
|
||||||
|
- Alembic 迁移: `uv run alembic init migrations` 后接入 `app.db.session.Base.metadata`
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
"""管理员后台:用户管理 / 权限开关 / 渠道分配 / 站点概览"""
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from ...db.session import get_db
|
||||||
|
from ...db import repos
|
||||||
|
from ...models.entities import User, Channel, ChatSession
|
||||||
|
from ...api.routers.auth import current_user
|
||||||
|
from ...core import security
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||||
|
|
||||||
|
|
||||||
|
async def require_admin(user: User = Depends(current_user)):
|
||||||
|
if not user.is_admin:
|
||||||
|
raise HTTPException(403, "需要管理员权限")
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/settings")
|
||||||
|
async def get_settings(db=Depends(get_db), _: User = Depends(require_admin)):
|
||||||
|
return await repos.SettingRepo.all(db)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/settings")
|
||||||
|
async def set_setting(key: str, value: str, db=Depends(get_db),
|
||||||
|
_: User = Depends(require_admin)):
|
||||||
|
allowed = {"allow_register", "allow_delete_account", "allow_byok",
|
||||||
|
"admin_data_visible"}
|
||||||
|
if key not in allowed:
|
||||||
|
raise HTTPException(400, "未知开关")
|
||||||
|
await repos.SettingRepo.set(db, key, "true" if value == "true" else "false")
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/users")
|
||||||
|
async def list_users(db=Depends(get_db), _: User = Depends(require_admin)):
|
||||||
|
users = await repos.UserRepo.all(db)
|
||||||
|
n_sessions = len((await db.execute(
|
||||||
|
__import__("sqlalchemy").select(ChatSession))).scalars().all())
|
||||||
|
return {"users": [{"id": u.id, "username": u.username, "is_admin": u.is_admin,
|
||||||
|
"is_active": u.is_active} for u in users],
|
||||||
|
"stats": {"users": len(users), "sessions": n_sessions}}
|
||||||
|
|
||||||
|
|
||||||
|
class UserPatch(BaseModel):
|
||||||
|
is_active: bool | None = None
|
||||||
|
is_admin: bool | None = None
|
||||||
|
new_password: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/users/{uid}")
|
||||||
|
async def patch_user(uid: int, body: UserPatch, db=Depends(get_db),
|
||||||
|
_: User = Depends(require_admin)):
|
||||||
|
u = await repos.UserRepo.by_id(db, uid)
|
||||||
|
if not u:
|
||||||
|
raise HTTPException(404, "用户不存在")
|
||||||
|
if body.is_active is not None:
|
||||||
|
u.is_active = body.is_active
|
||||||
|
if body.is_admin is not None:
|
||||||
|
u.is_admin = body.is_admin
|
||||||
|
if body.new_password:
|
||||||
|
u.password_hash = security.hash_password(body.new_password)
|
||||||
|
security.key_cache.pop(uid, None)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
class AuthInLike(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/users")
|
||||||
|
async def create_user(body: AuthInLike, db=Depends(get_db),
|
||||||
|
_: User = Depends(require_admin)):
|
||||||
|
if await repos.UserRepo.by_username(db, body.username):
|
||||||
|
raise HTTPException(409, "用户名已存在")
|
||||||
|
salt = security.os.urandom(16)
|
||||||
|
db.add(User(username=body.username,
|
||||||
|
password_hash=security.hash_password(body.password),
|
||||||
|
kdf_salt=salt.hex()))
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/channels/{cid}/grant/{uid}")
|
||||||
|
async def grant_channel(cid: int, uid: int, db=Depends(get_db),
|
||||||
|
_: User = Depends(require_admin)):
|
||||||
|
db.add(__import__("app.models.entities", fromlist=["ChannelGrant"]).ChannelGrant(
|
||||||
|
channel_id=cid, user_id=uid))
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/channels/{cid}/grant/{uid}")
|
||||||
|
async def revoke_channel(cid: int, uid: int, db=Depends(get_db),
|
||||||
|
_: User = Depends(require_admin)):
|
||||||
|
from ...models.entities import ChannelGrant
|
||||||
|
from sqlalchemy import delete as _del
|
||||||
|
await db.execute(_del(ChannelGrant).where(
|
||||||
|
ChannelGrant.channel_id == cid, ChannelGrant.user_id == uid))
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/channels/{cid}/grants")
|
||||||
|
async def list_grants(cid: int, db=Depends(get_db), _: User = Depends(require_admin)):
|
||||||
|
from ...models.entities import ChannelGrant
|
||||||
|
rows = (await db.execute(__import__("sqlalchemy").select(ChannelGrant).where(
|
||||||
|
ChannelGrant.channel_id == cid))).scalars().all()
|
||||||
|
return {"user_ids": [r.user_id for r in rows]}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy import select
|
||||||
|
|
||||||
|
from ...db.session import get_db
|
||||||
|
from ...db import repos
|
||||||
|
from ...models.entities import User
|
||||||
|
from ...core import security
|
||||||
|
from ...core.config import settings
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/auth", tags=["auth"])
|
||||||
|
|
||||||
|
|
||||||
|
class AuthIn(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
|
||||||
|
|
||||||
|
async def current_user(request: Request, db=Depends(get_db)) -> User:
|
||||||
|
token = request.headers.get("Authorization", "").removeprefix("Bearer ")
|
||||||
|
payload = security.parse_token(token)
|
||||||
|
if not payload:
|
||||||
|
raise HTTPException(401, "未登录")
|
||||||
|
user = await repos.UserRepo.by_id(db, int(payload["uid"]))
|
||||||
|
if not user or not user.is_active:
|
||||||
|
raise HTTPException(401, "账户无效")
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/register")
|
||||||
|
async def register(body: AuthIn, db=Depends(get_db)):
|
||||||
|
if await repos.SettingRepo.get(db, "allow_register") != "true":
|
||||||
|
raise HTTPException(403, "本站已关闭注册")
|
||||||
|
if await repos.UserRepo.by_username(db, body.username):
|
||||||
|
raise HTTPException(409, "用户名已存在")
|
||||||
|
salt = security.os.urandom(16)
|
||||||
|
u = User(username=body.username,
|
||||||
|
password_hash=security.hash_password(body.password),
|
||||||
|
kdf_salt=salt.hex())
|
||||||
|
# 首个注册用户 = 管理员
|
||||||
|
if not (await db.execute(select(User))).scalars().first():
|
||||||
|
u.is_admin = True
|
||||||
|
db.add(u)
|
||||||
|
await db.commit()
|
||||||
|
return await token_response(u, body.password)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/login")
|
||||||
|
async def login(body: AuthIn, db=Depends(get_db)):
|
||||||
|
u = await repos.UserRepo.by_username(db, body.username)
|
||||||
|
if not u or not security.verify_password(body.password, u.password_hash):
|
||||||
|
raise HTTPException(401, "用户名或密码错误")
|
||||||
|
if not u.is_active:
|
||||||
|
raise HTTPException(403, "账户已被封禁")
|
||||||
|
return await token_response(u, body.password)
|
||||||
|
|
||||||
|
|
||||||
|
async def token_response(u: User, password: str):
|
||||||
|
key = security.derive_key(password, bytes.fromhex(u.kdf_salt))
|
||||||
|
security.key_cache[u.id] = key
|
||||||
|
# "数据可见" OFF→ON 迁移:登录时把存量密文解开转明文
|
||||||
|
await migrate_decrypt(u, key)
|
||||||
|
return {"token": security.make_token(u.id, u.is_admin),
|
||||||
|
"username": u.username, "is_admin": u.is_admin}
|
||||||
|
|
||||||
|
|
||||||
|
async def migrate_decrypt(u: User, key: bytes):
|
||||||
|
"""admin_data_visible=true 时,解密该用户所有存量密文(静默)。"""
|
||||||
|
from ...db.session import SessionLocal
|
||||||
|
from ...models.entities import Memory, WorldbookEntry, Message, ChatSession
|
||||||
|
async with SessionLocal() as db:
|
||||||
|
visible = await repos.SettingRepo.get(db, "admin_data_visible") == "true"
|
||||||
|
if not visible:
|
||||||
|
return
|
||||||
|
for field_rows, field in [(await db.execute(select(Memory).where(Memory.user_id == u.id))).scalars().all(), "content_enc"], \
|
||||||
|
[(await db.execute(select(WorldbookEntry).where(WorldbookEntry.user_id == u.id))).scalars().all(), "content_enc"], \
|
||||||
|
[(await db.execute(select(Message).where(Message.user_id == u.id))).scalars().all(), "content_enc"]:
|
||||||
|
for row in field_rows:
|
||||||
|
if security.is_encrypted(getattr(row, field)):
|
||||||
|
setattr(row, field, security.decrypt_text(key, getattr(row, field)))
|
||||||
|
# novel_setting
|
||||||
|
for s in (await db.execute(select(ChatSession).where(ChatSession.user_id == u.id))).scalars().all():
|
||||||
|
if s.novel_setting_enc and security.is_encrypted(s.novel_setting_enc):
|
||||||
|
s.novel_setting_enc = security.decrypt_text(key, s.novel_setting_enc)
|
||||||
|
await db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/delete-account")
|
||||||
|
async def delete_account(body: AuthIn, user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
if await repos.SettingRepo.get(db, "allow_delete_account") != "true":
|
||||||
|
raise HTTPException(403, "本站已关闭注销")
|
||||||
|
if not security.verify_password(body.password, user.password_hash):
|
||||||
|
raise HTTPException(401, "密码错误")
|
||||||
|
# 硬删除:数据随账户消失,不可恢复
|
||||||
|
from ...models.entities import (Memory, WorldbookEntry, Message,
|
||||||
|
ChatSession, Character, Channel)
|
||||||
|
for model, col in [(Memory, "user_id"), (WorldbookEntry, "user_id"),
|
||||||
|
(Message, "user_id"), (ChatSession, "user_id"),
|
||||||
|
(Character, "user_id"), (Channel, "owner_id")]:
|
||||||
|
await db.execute(select(model)) # touch
|
||||||
|
from sqlalchemy import delete as _del
|
||||||
|
await db.execute(_del(model).where(getattr(model, col) == user.id))
|
||||||
|
await db.delete(user)
|
||||||
|
await db.commit()
|
||||||
|
security.key_cache.pop(user.id, None)
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/me")
|
||||||
|
async def me(user: User = Depends(current_user)):
|
||||||
|
return {"id": user.id, "username": user.username, "is_admin": user.is_admin}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
"""渠道管理:CRUD + 测试 + Step Plan 预设"""
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from ...db.session import get_db
|
||||||
|
from ...db import repos
|
||||||
|
from ...models.entities import User, Channel
|
||||||
|
from ...api.routers.auth import current_user
|
||||||
|
from ...core.config import STEP_PRESET
|
||||||
|
from ...services.llm import LLMClient
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/channels", tags=["channels"])
|
||||||
|
|
||||||
|
|
||||||
|
class ChannelIn(BaseModel):
|
||||||
|
name: str
|
||||||
|
base_url: str
|
||||||
|
api_key: str
|
||||||
|
models: dict = {}
|
||||||
|
visibility: str = "admin" # public / granted / admin
|
||||||
|
|
||||||
|
|
||||||
|
def _view(c: Channel) -> dict:
|
||||||
|
return {"id": c.id, "name": c.name, "base_url": c.base_url,
|
||||||
|
"models": c.models or {}, "owner_id": c.owner_id,
|
||||||
|
"visibility": c.visibility,
|
||||||
|
"has_key": bool(c.api_key_enc)}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/preset")
|
||||||
|
async def preset():
|
||||||
|
return STEP_PRESET
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_channels(user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
rows = await repos.ChannelRepo.visible_to(db, user)
|
||||||
|
return {"channels": [_view(c) for c in rows]}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("")
|
||||||
|
async def create_channel(body: ChannelIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
if not user.is_admin and (await repos.SettingRepo.get(db, "allow_byok")) != "true":
|
||||||
|
raise HTTPException(403, "本站不允许用户自定义 API 渠道")
|
||||||
|
c = Channel(name=body.name, base_url=body.base_url,
|
||||||
|
api_key_enc=body.api_key, # BYOK key 也走加密列(M2 加密开关接入后统一加密)
|
||||||
|
models=body.models, owner_id=None if user.is_admin else user.id,
|
||||||
|
visibility=body.visibility if user.is_admin else "admin")
|
||||||
|
db.add(c)
|
||||||
|
await db.commit()
|
||||||
|
return _view(c)
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/{cid}")
|
||||||
|
async def update_channel(cid: int, body: ChannelIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
c = await repos.ChannelRepo.can_use(db, user, cid)
|
||||||
|
if not c or (c.owner_id and c.owner_id != user.id and not user.is_admin):
|
||||||
|
raise HTTPException(403, "无权修改该渠道")
|
||||||
|
c.name, c.base_url, c.models = body.name, body.base_url, body.models
|
||||||
|
if body.api_key:
|
||||||
|
c.api_key_enc = body.api_key
|
||||||
|
if user.is_admin:
|
||||||
|
c.visibility = body.visibility
|
||||||
|
await db.commit()
|
||||||
|
return _view(c)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{cid}")
|
||||||
|
async def delete_channel(cid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
c = await repos.ChannelRepo.can_use(db, user, cid)
|
||||||
|
if not c or (c.owner_id and c.owner_id != user.id and not user.is_admin):
|
||||||
|
raise HTTPException(403, "无权删除该渠道")
|
||||||
|
await db.delete(c)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/{cid}/test")
|
||||||
|
async def test_channel(cid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
c = await repos.ChannelRepo.can_use(db, user, cid)
|
||||||
|
if not c:
|
||||||
|
raise HTTPException(403, "无权访问该渠道")
|
||||||
|
model = (c.models or {}).get("narrative") or "step-3.5-flash"
|
||||||
|
client = LLMClient(c.base_url, c.api_key_enc)
|
||||||
|
ok, msg = await client.ping(model)
|
||||||
|
return {"ok": ok, "message": msg, "model": model}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{cid}/grants")
|
||||||
|
async def list_grants(cid: int, user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
from ...models.entities import ChannelGrant
|
||||||
|
from sqlalchemy import select
|
||||||
|
if not user.is_admin:
|
||||||
|
raise HTTPException(403, "需要管理员权限")
|
||||||
|
rows = (await db.execute(select(ChannelGrant).where(
|
||||||
|
ChannelGrant.channel_id == cid))).scalars().all()
|
||||||
|
return {"user_ids": [r.user_id for r in rows]}
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
"""角色 / 记忆 / 世界书:全部可查可改可删,零黑箱。"""
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy import select, delete
|
||||||
|
|
||||||
|
from ...db.session import get_db
|
||||||
|
from ...db import repos
|
||||||
|
from ...models.entities import User, Character, Memory, WorldbookEntry
|
||||||
|
from ...api.routers.auth import current_user
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/characters", tags=["characters"])
|
||||||
|
|
||||||
|
|
||||||
|
class CharacterIn(BaseModel):
|
||||||
|
name: str
|
||||||
|
persona: str = ""
|
||||||
|
greeting: str = ""
|
||||||
|
voice: str = "tongtong"
|
||||||
|
avatar_url: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryIn(BaseModel):
|
||||||
|
kind: str = "fact" # fact / event
|
||||||
|
content: str
|
||||||
|
|
||||||
|
|
||||||
|
class WorldbookIn(BaseModel):
|
||||||
|
scope: str = "character" # character / novel
|
||||||
|
scope_id: int = 0
|
||||||
|
keywords: str = ""
|
||||||
|
content: str
|
||||||
|
constant: bool = False
|
||||||
|
enabled: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
def _key(user):
|
||||||
|
return security.key_cache.get(user.id)
|
||||||
|
|
||||||
|
|
||||||
|
from ...core import security # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
async def _own_character(db, user, cid) -> Character:
|
||||||
|
c = await db.get(Character, cid)
|
||||||
|
if not c or c.user_id != user.id:
|
||||||
|
raise HTTPException(404, "角色不存在")
|
||||||
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_characters(user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
rows = await repos.CharacterRepo.list_for(db, user.id)
|
||||||
|
return {"characters": [{"id": c.id, "name": c.name, "persona": c.persona,
|
||||||
|
"greeting": c.greeting, "voice": c.voice,
|
||||||
|
"avatar_url": c.avatar_url, "source": c.source,
|
||||||
|
"novel_id": c.novel_id} for c in rows]}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("")
|
||||||
|
async def create_character(body: CharacterIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
c = Character(user_id=user.id, **body.model_dump())
|
||||||
|
db.add(c)
|
||||||
|
await db.commit()
|
||||||
|
return {"id": c.id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/{cid}")
|
||||||
|
async def update_character(cid: int, body: CharacterIn,
|
||||||
|
user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
c = await _own_character(db, user, cid)
|
||||||
|
for k, v in body.model_dump().items():
|
||||||
|
setattr(c, k, v)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{cid}")
|
||||||
|
async def delete_character(cid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
c = await _own_character(db, user, cid)
|
||||||
|
await db.execute(delete(Memory).where(Memory.character_id == cid))
|
||||||
|
await db.delete(c)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 记忆(完全透明可篡改) ----------
|
||||||
|
|
||||||
|
@router.get("/{cid}/memories")
|
||||||
|
async def list_memories(cid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
await _own_character(db, user, cid)
|
||||||
|
rows = await repos.MemoryRepo.list_for(db, user.id, cid)
|
||||||
|
key = _key(user)
|
||||||
|
return {"memories": [{"id": m.id, "kind": m.kind,
|
||||||
|
"content": repos.dec(key, m.content_enc)} for m in rows]}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/{cid}/memories")
|
||||||
|
async def add_memory(cid: int, body: MemoryIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
await _own_character(db, user, cid)
|
||||||
|
key = _key(user)
|
||||||
|
visible = await repos.SettingRepo.get(db, "admin_data_visible") == "true"
|
||||||
|
db.add(Memory(character_id=cid, user_id=user.id, kind=body.kind,
|
||||||
|
content_enc=repos.enc(None if visible else key, body.content)))
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/{cid}/memories/{mid}")
|
||||||
|
async def edit_memory(cid: int, mid: int, body: MemoryIn,
|
||||||
|
user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
m = await db.get(Memory, mid)
|
||||||
|
if not m or m.user_id != user.id or m.character_id != cid:
|
||||||
|
raise HTTPException(404, "记忆不存在")
|
||||||
|
key = _key(user)
|
||||||
|
visible = await repos.SettingRepo.get(db, "admin_data_visible") == "true"
|
||||||
|
m.content_enc = repos.enc(None if visible else key, body.content)
|
||||||
|
m.kind = body.kind
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{cid}/memories/{mid}")
|
||||||
|
async def delete_memory(cid: int, mid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
m = await db.get(Memory, mid)
|
||||||
|
if not m or m.user_id != user.id:
|
||||||
|
raise HTTPException(404, "记忆不存在")
|
||||||
|
await db.delete(m)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 世界书 ----------
|
||||||
|
|
||||||
|
@router.get("/worldbook/all")
|
||||||
|
async def list_worldbook(user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
rows = await repos.WorldbookRepo.list_for(db, user.id)
|
||||||
|
key = _key(user)
|
||||||
|
return {"entries": [{"id": w.id, "scope": w.scope, "scope_id": w.scope_id,
|
||||||
|
"keywords": w.keywords, "constant": w.constant,
|
||||||
|
"enabled": w.enabled,
|
||||||
|
"content": repos.dec(key, w.content_enc)} for w in rows]}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/worldbook")
|
||||||
|
async def add_worldbook(body: WorldbookIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
key = _key(user)
|
||||||
|
visible = await repos.SettingRepo.get(db, "admin_data_visible") == "true"
|
||||||
|
db.add(WorldbookEntry(user_id=user.id, scope=body.scope, scope_id=body.scope_id,
|
||||||
|
keywords=body.keywords, constant=body.constant,
|
||||||
|
enabled=body.enabled,
|
||||||
|
content_enc=repos.enc(None if visible else key, body.content)))
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/worldbook/{wid}")
|
||||||
|
async def edit_worldbook(wid: int, body: WorldbookIn,
|
||||||
|
user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
w = await db.get(WorldbookEntry, wid)
|
||||||
|
if not w or w.user_id != user.id:
|
||||||
|
raise HTTPException(404, "条目不存在")
|
||||||
|
key = _key(user)
|
||||||
|
visible = await repos.SettingRepo.get(db, "admin_data_visible") == "true"
|
||||||
|
for k, v in body.model_dump().items():
|
||||||
|
setattr(w, k, v)
|
||||||
|
w.content_enc = repos.enc(None if visible else key, body.content)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/worldbook/{wid}")
|
||||||
|
async def delete_worldbook(wid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
w = await db.get(WorldbookEntry, wid)
|
||||||
|
if not w or w.user_id != user.id:
|
||||||
|
raise HTTPException(404, "条目不存在")
|
||||||
|
await db.delete(w)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
@@ -0,0 +1,264 @@
|
|||||||
|
"""聊天主链路:单角色扮演 + 动态小说两种模式。上下文不做节省。"""
|
||||||
|
import datetime as dt
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from fastapi.responses import Response
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy import select, delete
|
||||||
|
|
||||||
|
from ...db.session import get_db
|
||||||
|
from ...db import repos
|
||||||
|
from ...models.entities import (User, ChatSession, Message, Character,
|
||||||
|
Memory, WorldbookEntry)
|
||||||
|
from ...api.routers.auth import current_user
|
||||||
|
from ...core import security
|
||||||
|
from ...services.llm import LLMClient
|
||||||
|
from ...services import novel_pipeline
|
||||||
|
|
||||||
|
router = APIRouter(tags=["chat"])
|
||||||
|
|
||||||
|
|
||||||
|
class SessionIn(BaseModel):
|
||||||
|
mode: str = "roleplay"
|
||||||
|
title: str = "新会话"
|
||||||
|
character_id: int | None = None
|
||||||
|
novel_setting: str | None = None
|
||||||
|
channel_id: int | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class SendIn(BaseModel):
|
||||||
|
content: str
|
||||||
|
auto_extract: bool = True # 小说模式:自动抽角色
|
||||||
|
auto_avatar: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
class MessageIn(BaseModel):
|
||||||
|
content: str
|
||||||
|
|
||||||
|
|
||||||
|
def _key(user):
|
||||||
|
return security.key_cache.get(user.id)
|
||||||
|
|
||||||
|
|
||||||
|
def _enc(user, text):
|
||||||
|
return repos.enc(_key(user), text)
|
||||||
|
|
||||||
|
|
||||||
|
def _dec(user, text):
|
||||||
|
return repos.dec(_key(user), text)
|
||||||
|
|
||||||
|
|
||||||
|
async def _own_session(db, user, sid) -> ChatSession:
|
||||||
|
s = await db.get(ChatSession, sid)
|
||||||
|
if not s or s.user_id != user.id:
|
||||||
|
raise HTTPException(404, "会话不存在")
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
async def _channel_for(db, user, session) -> tuple[LLMClient, dict]:
|
||||||
|
cid = session.channel_id
|
||||||
|
if not cid:
|
||||||
|
rows = await repos.ChannelRepo.visible_to(db, user)
|
||||||
|
if not rows:
|
||||||
|
raise HTTPException(400, "没有可用渠道,请先到渠道页配置 API")
|
||||||
|
c = rows[0]
|
||||||
|
else:
|
||||||
|
c = await repos.ChannelRepo.can_use(db, user, cid)
|
||||||
|
if not c:
|
||||||
|
raise HTTPException(403, "无权使用该渠道")
|
||||||
|
return LLMClient(c.base_url, c.api_key_enc), (c.models or {})
|
||||||
|
|
||||||
|
|
||||||
|
async def build_context(db, user, session, history) -> list[dict]:
|
||||||
|
"""拼装上下文:人设 + 记忆 + 世界书命中 + 全量最近对话。"""
|
||||||
|
msgs = []
|
||||||
|
if session.mode == "roleplay":
|
||||||
|
ch = await db.get(Character, session.character_id)
|
||||||
|
persona = f"你扮演角色「{ch.name}」。人设:\n{ch.persona}\n始终以该角色的口吻回复,不要出戏。"
|
||||||
|
mems = await repos.MemoryRepo.list_for(db, user.id, ch.id)
|
||||||
|
key = _key(user)
|
||||||
|
mem_text = "\n".join(f"- [{m.kind}] {_dec(user, m.content_enc)}" for m in mems)
|
||||||
|
if mem_text:
|
||||||
|
persona += "\n\n你已知的记忆:\n" + mem_text
|
||||||
|
msgs.append({"role": "system", "content": persona})
|
||||||
|
scope_hits = []
|
||||||
|
else:
|
||||||
|
setting = _dec(user, session.novel_setting_enc or "")
|
||||||
|
msgs.append({"role": "system",
|
||||||
|
"content": ("你是小说编剧,与用户共创一部小说。用户可以控制主角行动,"
|
||||||
|
"你负责推进剧情、描写场景与其他角色的言行。剧情需要时,"
|
||||||
|
"你可以自主引入新的配角,给他们完整的性格与名字。\n"
|
||||||
|
f"小说设定:\n{setting}")})
|
||||||
|
scope_hits = []
|
||||||
|
# 世界书:角色模式查 character scope,小说模式查 novel scope
|
||||||
|
scope = "character" if session.mode == "roleplay" else "novel"
|
||||||
|
scope_id = session.character_id or session.id
|
||||||
|
entries = (await db.execute(select(WorldbookEntry).where(
|
||||||
|
WorldbookEntry.user_id == user.id, WorldbookEntry.scope == scope,
|
||||||
|
WorldbookEntry.enabled == True))).scalars().all() # noqa: E712
|
||||||
|
last_text = history[-1].content_enc if history else ""
|
||||||
|
hits = novel_pipeline.match_worldbook(entries, _dec(user, last_text or ""), _key(user))
|
||||||
|
for e in entries:
|
||||||
|
if e.constant:
|
||||||
|
h = novel_pipeline.match_worldbook([e], "", _key(user))
|
||||||
|
if h:
|
||||||
|
hits.append(h[0])
|
||||||
|
if hits:
|
||||||
|
msgs.append({"role": "system", "content": "【世界观设定】\n" + "\n".join(hits)})
|
||||||
|
for m in history:
|
||||||
|
role = "user" if m.role == "user" else "assistant"
|
||||||
|
msgs.append({"role": role, "content": _dec(user, m.content_enc)})
|
||||||
|
return msgs
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/sessions")
|
||||||
|
async def create_session(body: SessionIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
s = ChatSession(user_id=user.id, mode=body.mode, title=body.title,
|
||||||
|
character_id=body.character_id,
|
||||||
|
novel_setting_enc=_enc(user, body.novel_setting or "") if body.novel_setting else None,
|
||||||
|
channel_id=body.channel_id)
|
||||||
|
db.add(s)
|
||||||
|
await db.commit()
|
||||||
|
# 角色模式自动塞开场白
|
||||||
|
if body.character_id:
|
||||||
|
ch = await db.get(Character, body.character_id)
|
||||||
|
if ch and ch.greeting:
|
||||||
|
db.add(Message(session_id=s.id, user_id=user.id, role="assistant",
|
||||||
|
speaker=ch.name, content_enc=_enc(user, ch.greeting)))
|
||||||
|
await db.commit()
|
||||||
|
return {"id": s.id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/sessions")
|
||||||
|
async def list_sessions(user: User = Depends(current_user), db=Depends(get_db)):
|
||||||
|
rows = await repos.SessionRepo.list_for(db, user.id)
|
||||||
|
return {"sessions": [{"id": s.id, "mode": s.mode, "title": s.title,
|
||||||
|
"character_id": s.character_id} for s in rows]}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/sessions/{sid}/messages")
|
||||||
|
async def get_messages(sid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
await _own_session(db, user, sid)
|
||||||
|
rows = await repos.MessageRepo.history(db, user.id, sid, limit=500)
|
||||||
|
return {"messages": [{"id": m.id, "role": m.role, "speaker": m.speaker,
|
||||||
|
"content": _dec(user, m.content_enc)} for m in rows]}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/sessions/{sid}/send")
|
||||||
|
async def send(sid: int, body: SendIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
s = await _own_session(db, user, sid)
|
||||||
|
client, models = await _channel_for(db, user, s)
|
||||||
|
narrative_model = models.get("narrative") or "step-3.5-flash"
|
||||||
|
|
||||||
|
db.add(Message(session_id=sid, user_id=user.id, role="user",
|
||||||
|
content_enc=_enc(user, body.content)))
|
||||||
|
await db.commit()
|
||||||
|
|
||||||
|
history = await repos.MessageRepo.history(db, user.id, sid, limit=80)
|
||||||
|
msgs = await build_context(db, user, s, history)
|
||||||
|
reply = await client.chat(narrative_model, msgs, temperature=0.9, max_tokens=4096)
|
||||||
|
|
||||||
|
reply_row = Message(session_id=sid, user_id=user.id,
|
||||||
|
role="assistant" if s.mode == "roleplay" else "narrator",
|
||||||
|
content_enc=_enc(user, reply))
|
||||||
|
db.add(reply_row)
|
||||||
|
await db.commit()
|
||||||
|
|
||||||
|
new_chars = []
|
||||||
|
if s.mode == "novel" and body.auto_extract:
|
||||||
|
new_chars = await _run_extraction(db, user, s, client, models,
|
||||||
|
body.auto_avatar, reply)
|
||||||
|
return {"reply": reply, "message_id": reply_row.id, "new_characters": new_chars}
|
||||||
|
|
||||||
|
|
||||||
|
async def _run_extraction(db, user, session, client, models, auto_avatar, story_text):
|
||||||
|
"""动态小说灵魂:抽取新角色 → 建档 → 生图 → 世界书注册"""
|
||||||
|
extract_model = models.get("extract") or "step-3.5-flash"
|
||||||
|
found = await novel_pipeline.extract_new_characters(
|
||||||
|
client, extract_model, story_text)
|
||||||
|
existing = {c.name for c in await repos.CharacterRepo.list_for(db, user.id)}
|
||||||
|
image_model = models.get("image") or ""
|
||||||
|
out = []
|
||||||
|
for d in found:
|
||||||
|
if d["name"] in existing:
|
||||||
|
continue
|
||||||
|
data = novel_pipeline.build_character(user.id, session.id, d)
|
||||||
|
if auto_avatar and image_model:
|
||||||
|
data["avatar_url"] = await novel_pipeline.gen_avatar(
|
||||||
|
client, image_model, d.get("appearance", ""))
|
||||||
|
ch = Character(**data)
|
||||||
|
db.add(ch)
|
||||||
|
await db.flush()
|
||||||
|
# 自动记忆
|
||||||
|
key = _key(user)
|
||||||
|
visible = await repos.SettingRepo.get(db, "admin_data_visible") == "true"
|
||||||
|
db.add(Memory(character_id=ch.id, user_id=user.id, kind="fact",
|
||||||
|
content_enc=repos.enc(None if visible else key,
|
||||||
|
data["persona"])))
|
||||||
|
# 自动注册世界书
|
||||||
|
db.add(WorldbookEntry(
|
||||||
|
user_id=user.id, scope="novel", scope_id=session.id,
|
||||||
|
keywords=d["name"], constant=False, enabled=True,
|
||||||
|
content_enc=repos.enc(None if visible else key,
|
||||||
|
f"{d['name']}:{d.get('identity','')}。"
|
||||||
|
f"{d.get('personality','')} 与主角关系:{d.get('relation','')}")))
|
||||||
|
out.append({"id": ch.id, "name": ch.name, "persona": ch.persona,
|
||||||
|
"avatar_url": ch.avatar_url, "voice": ch.voice})
|
||||||
|
await db.commit()
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/messages/{mid}")
|
||||||
|
async def edit_message(mid: int, body: MessageIn, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
m = await db.get(Message, mid)
|
||||||
|
if not m or m.user_id != user.id:
|
||||||
|
raise HTTPException(404, "消息不存在")
|
||||||
|
m.content_enc = _enc(user, body.content)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/messages/{mid}")
|
||||||
|
async def delete_message(mid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
m = await db.get(Message, mid)
|
||||||
|
if not m or m.user_id != user.id:
|
||||||
|
raise HTTPException(404, "消息不存在")
|
||||||
|
await db.delete(m)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/sessions/{sid}")
|
||||||
|
async def delete_session(sid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
s = await _own_session(db, user, sid)
|
||||||
|
await db.execute(delete(Message).where(Message.session_id == sid))
|
||||||
|
await db.delete(s)
|
||||||
|
await db.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/sessions/{sid}/tts/{mid}")
|
||||||
|
async def tts(sid: int, mid: int, user: User = Depends(current_user),
|
||||||
|
db=Depends(get_db)):
|
||||||
|
s = await _own_session(db, user, sid)
|
||||||
|
m = await db.get(Message, mid)
|
||||||
|
if not m or m.user_id != user.id or m.session_id != sid:
|
||||||
|
raise HTTPException(404, "消息不存在")
|
||||||
|
client, models = await _channel_for(db, user, s)
|
||||||
|
model = models.get("tts") or "stepaudio-2.5-tts"
|
||||||
|
voice = "tongtong"
|
||||||
|
if s.character_id:
|
||||||
|
ch = await db.get(Character, s.character_id)
|
||||||
|
if ch:
|
||||||
|
voice = ch.voice
|
||||||
|
try:
|
||||||
|
audio = await client.tts(model, _dec(user, m.content_enc), voice)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(502, f"TTS 失败: {e}")
|
||||||
|
return Response(content=audio, media_type="audio/wav")
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
app_name: str = "Open Maopaoya"
|
||||||
|
database_url: str = "sqlite+aiosqlite:///./data.db"
|
||||||
|
jwt_secret: str = "change-me-in-env"
|
||||||
|
jwt_expire_minutes: int = 60 * 24 * 7
|
||||||
|
first_admin_user: str = "admin"
|
||||||
|
first_admin_pass: str = "maopaoya-admin"
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
env_file = ".env"
|
||||||
|
env_prefix = "OMO_"
|
||||||
|
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
|
|
||||||
|
# 四个模型槽位的默认预设(Step Plan)
|
||||||
|
STEP_PRESET = {
|
||||||
|
"name": "Step Plan (阶跃星辰)",
|
||||||
|
"base_url": "https://api.stepfun.com/v1",
|
||||||
|
"models": {
|
||||||
|
"narrative": "step-3.5-flash", # 主叙事/角色扮演:现役最便宜,角色扮演最强
|
||||||
|
"extract": "step-3.5-flash", # 实体抽取
|
||||||
|
"tts": "stepaudio-2.5-tts", # 语音合成
|
||||||
|
"image": "", # 生图:阶跃已全下线,留槽位接任意 OpenAI 兼容生图
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import base64
|
||||||
|
import os
|
||||||
|
|
||||||
|
import bcrypt
|
||||||
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||||
|
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
||||||
|
from cryptography.hazmat.primitives import hashes
|
||||||
|
from jose import jwt, JWTError
|
||||||
|
|
||||||
|
from .config import settings
|
||||||
|
|
||||||
|
_JWT_KEY = settings.jwt_secret.encode()
|
||||||
|
|
||||||
|
|
||||||
|
def hash_password(pw: str) -> str:
|
||||||
|
return bcrypt.hashpw(pw.encode(), bcrypt.gensalt()).decode()
|
||||||
|
|
||||||
|
|
||||||
|
def verify_password(pw: str, hashed: str) -> bool:
|
||||||
|
return bcrypt.checkpw(pw.encode(), hashed.encode())
|
||||||
|
|
||||||
|
|
||||||
|
def make_token(user_id: int, is_admin: bool) -> str:
|
||||||
|
return jwt.encode(
|
||||||
|
{"uid": user_id, "adm": is_admin},
|
||||||
|
_JWT_KEY, algorithm="HS256")
|
||||||
|
|
||||||
|
|
||||||
|
def parse_token(token: str) -> dict | None:
|
||||||
|
try:
|
||||||
|
return jwt.decode(token, _JWT_KEY, algorithms=["HS256"])
|
||||||
|
except JWTError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 用户数据加密(管理员不可见模式) ----------
|
||||||
|
# 密钥从用户密码派生,服务器不落盘。登录时派生,缓存在进程内 session map。
|
||||||
|
|
||||||
|
_ENC_PREFIX = "ENC::"
|
||||||
|
_PBKDF2_ITERS = 200_000
|
||||||
|
|
||||||
|
|
||||||
|
def derive_key(password: str, salt: bytes) -> bytes:
|
||||||
|
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32,
|
||||||
|
salt=salt, iterations=_PBKDF2_ITERS)
|
||||||
|
return kdf.derive(password.encode())
|
||||||
|
|
||||||
|
|
||||||
|
def encrypt_text(key: bytes, plaintext: str) -> str:
|
||||||
|
if not plaintext or plaintext.startswith(_ENC_PREFIX):
|
||||||
|
return plaintext
|
||||||
|
nonce = os.urandom(12)
|
||||||
|
ct = AESGCM(key).encrypt(nonce, plaintext.encode(), None)
|
||||||
|
return _ENC_PREFIX + base64.b64encode(nonce + ct).decode()
|
||||||
|
|
||||||
|
|
||||||
|
def decrypt_text(key: bytes, stored: str) -> str:
|
||||||
|
if not stored or not stored.startswith(_ENC_PREFIX):
|
||||||
|
return stored
|
||||||
|
raw = base64.b64decode(stored[len(_ENC_PREFIX):])
|
||||||
|
return AESGCM(key).decrypt(raw[:12], raw[12:], None).decode()
|
||||||
|
|
||||||
|
|
||||||
|
def is_encrypted(stored: str | None) -> bool:
|
||||||
|
return bool(stored) and stored.startswith(_ENC_PREFIX)
|
||||||
|
|
||||||
|
|
||||||
|
# 进程内派生密钥缓存 {user_id: key}(重启后需重新登录)
|
||||||
|
key_cache: dict[int, bytes] = {}
|
||||||
+127
@@ -0,0 +1,127 @@
|
|||||||
|
"""统一数据访问层(Repository)。商用切库只动这里和 DATABASE_URL。"""
|
||||||
|
from sqlalchemy import select, delete
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from ..models import entities as E
|
||||||
|
|
||||||
|
|
||||||
|
def enc(key: bytes | None, text: str) -> str:
|
||||||
|
from ..core.security import encrypt_text
|
||||||
|
return encrypt_text(key, text) if key else text
|
||||||
|
|
||||||
|
|
||||||
|
def dec(key: bytes | None, stored: str) -> str:
|
||||||
|
from ..core.security import decrypt_text
|
||||||
|
return decrypt_text(key, stored) if key else stored
|
||||||
|
|
||||||
|
|
||||||
|
class UserRepo:
|
||||||
|
@staticmethod
|
||||||
|
async def by_username(db: AsyncSession, username: str):
|
||||||
|
return (await db.execute(select(E.User).where(E.User.username == username))).scalar_one_or_none()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def by_id(db: AsyncSession, uid: int):
|
||||||
|
return await db.get(E.User, uid)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def all(db: AsyncSession):
|
||||||
|
return (await db.execute(select(E.User).order_by(E.User.id))).scalars().all()
|
||||||
|
|
||||||
|
|
||||||
|
class SettingRepo:
|
||||||
|
DEFAULTS = {
|
||||||
|
"allow_register": "true",
|
||||||
|
"allow_delete_account": "true",
|
||||||
|
"allow_byok": "true",
|
||||||
|
"admin_data_visible": "true", # 默认明文;关闭后加密落库
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def get(db: AsyncSession, key: str) -> str:
|
||||||
|
v = (await db.execute(select(E.SiteSetting).where(E.SiteSetting.key == key))).scalar_one_or_none()
|
||||||
|
return v.value if v else SettingRepo.DEFAULTS.get(key, "")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def set(db: AsyncSession, key: str, value: str):
|
||||||
|
row = (await db.execute(select(E.SiteSetting).where(E.SiteSetting.key == key))).scalar_one_or_none()
|
||||||
|
if row:
|
||||||
|
row.value = value
|
||||||
|
else:
|
||||||
|
db.add(E.SiteSetting(key=key, value=value))
|
||||||
|
await db.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def all(db: AsyncSession) -> dict:
|
||||||
|
rows = (await db.execute(select(E.SiteSetting))).scalars().all()
|
||||||
|
out = dict(SettingRepo.DEFAULTS)
|
||||||
|
out.update({r.key: r.value for r in rows})
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
class ChannelRepo:
|
||||||
|
@staticmethod
|
||||||
|
async def visible_to(db: AsyncSession, user: E.User) -> list[E.Channel]:
|
||||||
|
q = select(E.Channel)
|
||||||
|
if user.is_admin:
|
||||||
|
rows = (await db.execute(q)).scalars().all()
|
||||||
|
else:
|
||||||
|
rows = (await db.execute(q.where(E.Channel.owner_id == user.id))).scalars().all()
|
||||||
|
pub = (await db.execute(q.where(E.Channel.visibility == "public"))).scalars().all()
|
||||||
|
granted_ids = (await db.execute(
|
||||||
|
select(E.ChannelGrant.channel_id).where(E.ChannelGrant.user_id == user.id))).scalars().all()
|
||||||
|
granted = [await db.get(E.Channel, cid) for cid in granted_ids]
|
||||||
|
merged = {c.id: c for c in [*rows, *pub]}
|
||||||
|
for c in granted:
|
||||||
|
if c:
|
||||||
|
merged[c.id] = c
|
||||||
|
rows = list(merged.values())
|
||||||
|
return rows
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def can_use(db: AsyncSession, user: E.User, channel_id: int) -> E.Channel | None:
|
||||||
|
c = await db.get(E.Channel, channel_id)
|
||||||
|
if not c:
|
||||||
|
return None
|
||||||
|
if user.is_admin or c.owner_id == user.id or c.visibility == "public":
|
||||||
|
return c
|
||||||
|
g = (await db.execute(select(E.ChannelGrant).where(
|
||||||
|
E.ChannelGrant.channel_id == channel_id, E.ChannelGrant.user_id == user.id))).scalar_one_or_none()
|
||||||
|
return c if g else None
|
||||||
|
|
||||||
|
|
||||||
|
class CharacterRepo:
|
||||||
|
@staticmethod
|
||||||
|
async def list_for(db: AsyncSession, uid: int):
|
||||||
|
return (await db.execute(select(E.Character).where(
|
||||||
|
E.Character.user_id == uid).order_by(E.Character.id.desc()))).scalars().all()
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryRepo:
|
||||||
|
@staticmethod
|
||||||
|
async def list_for(db: AsyncSession, uid: int, character_id: int):
|
||||||
|
return (await db.execute(select(E.Memory).where(
|
||||||
|
E.Memory.user_id == uid, E.Memory.character_id == character_id))).scalars().all()
|
||||||
|
|
||||||
|
|
||||||
|
class WorldbookRepo:
|
||||||
|
@staticmethod
|
||||||
|
async def list_for(db: AsyncSession, uid: int):
|
||||||
|
return (await db.execute(select(E.WorldbookEntry).where(
|
||||||
|
E.WorldbookEntry.user_id == uid))).scalars().all()
|
||||||
|
|
||||||
|
|
||||||
|
class SessionRepo:
|
||||||
|
@staticmethod
|
||||||
|
async def list_for(db: AsyncSession, uid: int):
|
||||||
|
return (await db.execute(select(E.ChatSession).where(
|
||||||
|
E.ChatSession.user_id == uid).order_by(E.ChatSession.id.desc()))).scalars().all()
|
||||||
|
|
||||||
|
|
||||||
|
class MessageRepo:
|
||||||
|
@staticmethod
|
||||||
|
async def history(db: AsyncSession, uid: int, session_id: int, limit: int = 60):
|
||||||
|
rows = (await db.execute(select(E.Message).where(
|
||||||
|
E.Message.user_id == uid, E.Message.session_id == session_id
|
||||||
|
).order_by(E.Message.id.desc()).limit(limit))).scalars().all()
|
||||||
|
return list(reversed(rows))
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||||
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
|
||||||
|
from ..core.config import settings
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
engine = create_async_engine(settings.database_url, echo=False)
|
||||||
|
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_db():
|
||||||
|
async with SessionLocal() as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def init_db():
|
||||||
|
from ..models import entities # noqa: F401
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.responses import FileResponse, JSONResponse
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
|
from .db.session import init_db
|
||||||
|
from .api.routers import auth, admin, channels, characters, chat
|
||||||
|
|
||||||
|
app = FastAPI(title="Open Maopaoya", docs_url="/api/docs", openapi_url="/api/openapi.json")
|
||||||
|
|
||||||
|
for r in (auth.router, admin.router, channels.router, characters.router, chat.router):
|
||||||
|
app.include_router(r, prefix="/api")
|
||||||
|
|
||||||
|
|
||||||
|
@app.on_event("startup")
|
||||||
|
async def startup():
|
||||||
|
await init_db()
|
||||||
|
# 首个管理员
|
||||||
|
from .db.session import SessionLocal
|
||||||
|
from .db import repos
|
||||||
|
from .models.entities import User
|
||||||
|
from .core import security
|
||||||
|
from .core.config import settings
|
||||||
|
import os
|
||||||
|
async with SessionLocal() as db:
|
||||||
|
users = await repos.UserRepo.all(db)
|
||||||
|
if not users:
|
||||||
|
salt = os.urandom(16)
|
||||||
|
db.add(User(username=settings.first_admin_user,
|
||||||
|
password_hash=security.hash_password(settings.first_admin_pass),
|
||||||
|
kdf_salt=salt.hex(), is_admin=True))
|
||||||
|
await db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def error_handler(request: Request, exc: Exception):
|
||||||
|
return JSONResponse(status_code=500, content={"detail": str(exc)[:300]})
|
||||||
|
|
||||||
|
|
||||||
|
app.mount("/", StaticFiles(directory="web", html=True), name="web")
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
"""数据模型。商用切 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)
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
"""OpenAI 兼容客户端:聊天、生图、TTS,全部按渠道转发。"""
|
||||||
|
import base64
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
|
class LLMClient:
|
||||||
|
def __init__(self, base_url: str, api_key: str):
|
||||||
|
self.base_url = base_url.rstrip("/")
|
||||||
|
self.api_key = api_key
|
||||||
|
|
||||||
|
async def chat(self, model: str, messages: list[dict], temperature: float = 0.9,
|
||||||
|
max_tokens: int = 2048) -> str:
|
||||||
|
async with httpx.AsyncClient(timeout=120) as c:
|
||||||
|
r = await c.post(f"{self.base_url}/chat/completions",
|
||||||
|
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||||
|
json={"model": model, "messages": messages,
|
||||||
|
"temperature": temperature, "max_tokens": max_tokens})
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()["choices"][0]["message"]["content"] or ""
|
||||||
|
|
||||||
|
async def image(self, model: str, prompt: str, size: str = "1024x1024") -> str:
|
||||||
|
async with httpx.AsyncClient(timeout=180) as c:
|
||||||
|
r = await c.post(f"{self.base_url}/images/generations",
|
||||||
|
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||||
|
json={"model": model, "prompt": prompt,
|
||||||
|
"size": size, "n": 1, "response_format": "url"})
|
||||||
|
r.raise_for_status()
|
||||||
|
d = r.json()["data"][0]
|
||||||
|
return d.get("url") or ("data:image/png;base64," + d.get("b64_json", ""))
|
||||||
|
|
||||||
|
async def tts(self, model: str, text: str, voice: str) -> bytes:
|
||||||
|
"""优先 OpenAI 兼容 /audio/speech;失败回落阶跃 stepaudio TTS 格式。"""
|
||||||
|
async with httpx.AsyncClient(timeout=120) as c:
|
||||||
|
headers = {"Authorization": f"Bearer {self.api_key}"}
|
||||||
|
try:
|
||||||
|
r = await c.post(f"{self.base_url}/audio/speech", headers=headers,
|
||||||
|
json={"model": model, "input": text, "voice": voice})
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.content
|
||||||
|
except httpx.HTTPError:
|
||||||
|
r = await c.post(f"{self.base_url}/audio/speech", headers=headers,
|
||||||
|
json={"model": model, "text": text, "voice": voice,
|
||||||
|
"response_format": "wav"})
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.content
|
||||||
|
|
||||||
|
async def ping(self, model: str) -> tuple[bool, str]:
|
||||||
|
try:
|
||||||
|
reply = await self.chat(model, [{"role": "user", "content": "ping"}],
|
||||||
|
temperature=0, max_tokens=8)
|
||||||
|
return True, reply or "ok"
|
||||||
|
except Exception as e:
|
||||||
|
return False, str(e)[:300]
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
"""动态小说核心管线:实体抽取 → 角色建档 → 世界书注册。
|
||||||
|
|
||||||
|
不做 prompt 节省:抽取时给全量上下文,输出严格 JSON。
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
|
||||||
|
from ..db.repos import enc, dec
|
||||||
|
from ..services.llm import LLMClient
|
||||||
|
|
||||||
|
EXTRACT_SYSTEM = """你是小说叙事的结构化信息抽取器。给定一段剧情文本,抽取其中出现的【新角色】。
|
||||||
|
规则:
|
||||||
|
1. 只抽取具名的、有独立人格的角色(路人甲除外)。
|
||||||
|
2. 对每个新角色输出完整档案,宁全勿缺。
|
||||||
|
3. 只输出 JSON 数组,无新角色输出 []。格式:
|
||||||
|
[{"name":"角色名","identity":"身份/职业","appearance":"外貌描述(用于生图,详细)",
|
||||||
|
"personality":"性格","speaking_style":"说话风格","relation":"与主角的关系",
|
||||||
|
"voice":"从这些音色中选一个最合适的: tongtong/xiaoyu/xiaochen/laowang/xiaolin",
|
||||||
|
"first_impression":"该角色登场时的场景一句话"}]
|
||||||
|
4. 不要臆造原文没有的角色。"""
|
||||||
|
|
||||||
|
ADD_PROMPT = """你是小说世界的管理员。剧情中新角色「{name}」已建档,请为世界书写一条该角色的设定条目,"
|
||||||
|
"包含身份、性格、与主角关系,200字以内,直接输出条目正文,不要解释。"""
|
||||||
|
|
||||||
|
|
||||||
|
async def extract_new_characters(client: LLMClient, model: str,
|
||||||
|
recent_story: str) -> list[dict]:
|
||||||
|
text = await client.chat(model, [
|
||||||
|
{"role": "system", "content": EXTRACT_SYSTEM},
|
||||||
|
{"role": "user", "content": recent_story},
|
||||||
|
], temperature=0.1, max_tokens=4096)
|
||||||
|
m = re.search(r"\[.*\]", text, re.S)
|
||||||
|
if not m:
|
||||||
|
return []
|
||||||
|
try:
|
||||||
|
data = json.loads(m.group(0))
|
||||||
|
return [d for d in data if isinstance(d, dict) and d.get("name")]
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def build_character(user_id: int, novel_id: int, d: dict) -> dict:
|
||||||
|
"""抽取结果 -> Character 行数据(不含加密字段)"""
|
||||||
|
return {
|
||||||
|
"user_id": user_id,
|
||||||
|
"name": d["name"][:64],
|
||||||
|
"persona": (f"身份:{d.get('identity','未知')}\n性格:{d.get('personality','')}\n"
|
||||||
|
f"说话风格:{d.get('speaking_style','')}\n与主角关系:{d.get('relation','')}\n"
|
||||||
|
f"登场:{d.get('first_impression','')}"),
|
||||||
|
"voice": d.get("voice") or "tongtong",
|
||||||
|
"source": "novel_auto",
|
||||||
|
"novel_id": novel_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def gen_avatar(client: LLMClient, model: str, appearance: str) -> str:
|
||||||
|
if not model:
|
||||||
|
return ""
|
||||||
|
prompt = (f"动漫风格角色立绘,半身像,干净背景。角色外貌:{appearance}。"
|
||||||
|
f"高质量,细节丰富,柔和光影。")
|
||||||
|
try:
|
||||||
|
return await client.image(model, prompt)
|
||||||
|
except Exception:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def match_worldbook(entries: list, text: str, key: bytes | None) -> list[str]:
|
||||||
|
"""世界书命中:常驻 or 关键词命中"""
|
||||||
|
hits = []
|
||||||
|
for e in entries:
|
||||||
|
if not e.enabled:
|
||||||
|
continue
|
||||||
|
if e.constant:
|
||||||
|
hits.append(dec(key, e.content_enc))
|
||||||
|
continue
|
||||||
|
kws = [k.strip() for k in (e.keywords or "").split(",") if k.strip()]
|
||||||
|
if any(k in text for k in kws):
|
||||||
|
hits.append(dec(key, e.content_enc))
|
||||||
|
return hits
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
[project]
|
||||||
|
name = "open-maopaoya"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Open Maopaoya - AI roleplay platform with dynamic novel engine"
|
||||||
|
requires-python = ">=3.12"
|
||||||
|
dependencies = [
|
||||||
|
"fastapi>=0.115",
|
||||||
|
"uvicorn[standard]>=0.34",
|
||||||
|
"sqlalchemy>=2.0",
|
||||||
|
"alembic>=1.14",
|
||||||
|
"pydantic>=2.10",
|
||||||
|
"pydantic-settings>=2.7",
|
||||||
|
"python-jose[cryptography]>=3.3",
|
||||||
|
"bcrypt>=4.2",
|
||||||
|
"httpx>=0.28",
|
||||||
|
"cryptography>=44.0",
|
||||||
|
"python-multipart>=0.0.20",
|
||||||
|
"aiosqlite>=0.20",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
# 商用切换: DATABASE_URL=mysql+aiomysql://... 或 postgresql+asyncpg://...
|
||||||
|
mysql = ["aiomysql>=0.2"]
|
||||||
|
postgres = ["asyncpg>=0.30"]
|
||||||
|
|
||||||
|
[tool.uv]
|
||||||
|
package = false
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
exec .venv/bin/uvicorn app.main:app --host 0.0.0.0 --port "${PORT:-8820}"
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
/* Open Maopaoya — iOS 液态玻璃 */
|
||||||
|
:root {
|
||||||
|
--glass: rgba(255,255,255,.08);
|
||||||
|
--glass-strong: rgba(255,255,255,.14);
|
||||||
|
--stroke: rgba(255,255,255,.22);
|
||||||
|
--text: #f2f4ff;
|
||||||
|
--dim: rgba(242,244,255,.55);
|
||||||
|
--accent: #7c9aff;
|
||||||
|
}
|
||||||
|
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
|
||||||
|
html, body { height: 100%; margin: 0; }
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, "SF Pro", "PingFang SC", sans-serif;
|
||||||
|
background: #0d0e18; color: var(--text);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.bg-blobs { position: fixed; inset: 0; z-index: -1; overflow: hidden; }
|
||||||
|
.bg-blobs i {
|
||||||
|
position: absolute; border-radius: 50%; filter: blur(90px); opacity: .45;
|
||||||
|
animation: drift 22s infinite alternate ease-in-out;
|
||||||
|
}
|
||||||
|
.bg-blobs i:nth-child(1) { width: 55vmax; height: 55vmax; left: -15vmax; top: -18vmax;
|
||||||
|
background: radial-gradient(circle, #5b6bff, transparent 70%); }
|
||||||
|
.bg-blobs i:nth-child(2) { width: 45vmax; height: 45vmax; right: -12vmax; top: 8vmax;
|
||||||
|
background: radial-gradient(circle, #b45bff, transparent 70%); animation-delay: -8s; }
|
||||||
|
.bg-blobs i:nth-child(3) { width: 50vmax; height: 50vmax; left: 10vmax; bottom: -20vmax;
|
||||||
|
background: radial-gradient(circle, #ff6bb0, transparent 70%); animation-delay: -14s; }
|
||||||
|
@keyframes drift { to { transform: translate(6vmax, 4vmax) scale(1.12); } }
|
||||||
|
|
||||||
|
/* —— 液态玻璃核心 —— */
|
||||||
|
.glass-card, .glass-nav, .glass-drawer, .glass-inputbar {
|
||||||
|
background: var(--glass);
|
||||||
|
-webkit-backdrop-filter: blur(28px) saturate(1.7);
|
||||||
|
backdrop-filter: blur(28px) saturate(1.7);
|
||||||
|
border: 1px solid var(--stroke);
|
||||||
|
box-shadow: 0 8px 32px rgba(0,0,0,.35),
|
||||||
|
inset 0 1px 0 rgba(255,255,255,.25),
|
||||||
|
inset 0 -1px 0 rgba(255,255,255,.06);
|
||||||
|
border-radius: 24px;
|
||||||
|
}
|
||||||
|
.glass-card { padding: 22px; }
|
||||||
|
.center-card {
|
||||||
|
position: fixed; left: 50%; top: 50%; transform: translate(-50%,-50%);
|
||||||
|
width: min(400px, 92vw); display: flex; flex-direction: column; gap: 12px;
|
||||||
|
z-index: 50;
|
||||||
|
}
|
||||||
|
.logo { margin: 0; font-size: 26px; }
|
||||||
|
.sub { margin: 0; color: var(--dim); font-size: 13px; }
|
||||||
|
input, textarea, select {
|
||||||
|
width: 100%; padding: 12px 14px; border-radius: 14px;
|
||||||
|
border: 1px solid var(--stroke); background: rgba(255,255,255,.06);
|
||||||
|
color: var(--text); font-size: 15px; outline: none;
|
||||||
|
-webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
|
||||||
|
}
|
||||||
|
input:focus, textarea:focus { border-color: var(--accent); }
|
||||||
|
textarea { min-height: 90px; resize: vertical; }
|
||||||
|
.btn {
|
||||||
|
padding: 11px 18px; border-radius: 14px; border: 1px solid var(--stroke);
|
||||||
|
background: var(--glass-strong); color: var(--text); font-size: 15px;
|
||||||
|
cursor: pointer; transition: transform .15s, background .2s;
|
||||||
|
}
|
||||||
|
.btn:active { transform: scale(.96); }
|
||||||
|
.btn.primary {
|
||||||
|
background: linear-gradient(135deg, #6f86ff, #9a6bff);
|
||||||
|
border: none; font-weight: 600;
|
||||||
|
}
|
||||||
|
.btn.small { padding: 6px 12px; font-size: 13px; border-radius: 10px; }
|
||||||
|
.btn.danger { border-color: rgba(255,110,110,.5); color: #ff9c9c; }
|
||||||
|
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
|
||||||
|
.row.spread { justify-content: space-between; }
|
||||||
|
.spacer { flex: 1; }
|
||||||
|
.msg { color: #ff9c9c; font-size: 13px; min-height: 16px; margin: 0; }
|
||||||
|
.hint { color: var(--dim); font-size: 13px; }
|
||||||
|
.hidden { display: none !important; }
|
||||||
|
|
||||||
|
/* 导航 */
|
||||||
|
.view { height: 100vh; display: flex; flex-direction: column; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }
|
||||||
|
.glass-nav {
|
||||||
|
display: flex; gap: 6px; align-items: center; margin: 10px 12px; padding: 8px;
|
||||||
|
border-radius: 20px; overflow-x: auto; flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.nav-btn {
|
||||||
|
padding: 9px 14px; border-radius: 12px; border: none; background: transparent;
|
||||||
|
color: var(--dim); font-size: 14px; cursor: pointer; white-space: nowrap;
|
||||||
|
}
|
||||||
|
.nav-btn.active { background: var(--glass-strong); color: var(--text);
|
||||||
|
box-shadow: inset 0 1px 0 rgba(255,255,255,.3); }
|
||||||
|
.who { color: var(--dim); font-size: 13px; padding: 0 6px; }
|
||||||
|
|
||||||
|
.page { flex: 1; overflow-y: auto; padding: 4px 16px 90px; }
|
||||||
|
.page h2 { margin: 8px 0 14px; }
|
||||||
|
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
|
||||||
|
.mini-card {
|
||||||
|
padding: 16px; border-radius: 20px; cursor: pointer; position: relative;
|
||||||
|
background: var(--glass); border: 1px solid var(--stroke);
|
||||||
|
-webkit-backdrop-filter: blur(24px) saturate(1.6); backdrop-filter: blur(24px) saturate(1.6);
|
||||||
|
box-shadow: inset 0 1px 0 rgba(255,255,255,.22);
|
||||||
|
transition: transform .15s;
|
||||||
|
}
|
||||||
|
.mini-card:active { transform: scale(.97); }
|
||||||
|
.mini-card h4 { margin: 0 0 6px; font-size: 15px; }
|
||||||
|
.mini-card p { margin: 0; font-size: 12px; color: var(--dim);
|
||||||
|
display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
|
||||||
|
.mini-card .avatar { width: 100%; border-radius: 14px; margin-bottom: 8px; }
|
||||||
|
.tag { font-size: 11px; padding: 2px 8px; border-radius: 8px;
|
||||||
|
background: var(--glass-strong); color: var(--dim); }
|
||||||
|
|
||||||
|
/* 聊天 */
|
||||||
|
#chat-log { flex: 1; overflow-y: auto; padding: 10px 14px 10px; display: flex; flex-direction: column; gap: 10px; }
|
||||||
|
.bubble {
|
||||||
|
max-width: 82%; padding: 12px 15px; border-radius: 20px; font-size: 15px;
|
||||||
|
line-height: 1.55; white-space: pre-wrap; word-break: break-word;
|
||||||
|
-webkit-backdrop-filter: blur(20px) saturate(1.5); backdrop-filter: blur(20px) saturate(1.5);
|
||||||
|
border: 1px solid var(--stroke); position: relative;
|
||||||
|
}
|
||||||
|
.bubble.user { align-self: flex-end;
|
||||||
|
background: linear-gradient(135deg, rgba(111,134,255,.35), rgba(154,107,255,.35));
|
||||||
|
border-bottom-right-radius: 6px; }
|
||||||
|
.bubble.ai { align-self: flex-start; background: var(--glass);
|
||||||
|
border-bottom-left-radius: 6px; }
|
||||||
|
.bubble .speaker { font-size: 11px; color: var(--accent); margin-bottom: 4px; }
|
||||||
|
.bubble .acts { position: absolute; top: -12px; right: 8px; display: none; gap: 4px; }
|
||||||
|
.bubble:hover .acts, .bubble:active .acts { display: flex; }
|
||||||
|
.acts button { font-size: 11px; padding: 4px 8px; border-radius: 9px; cursor: pointer;
|
||||||
|
border: 1px solid var(--stroke); background: rgba(20,22,40,.9); color: var(--text); }
|
||||||
|
.new-char-banner { align-self: center; font-size: 13px; color: #ffd479;
|
||||||
|
background: rgba(255,212,121,.12); border: 1px solid rgba(255,212,121,.35);
|
||||||
|
padding: 8px 16px; border-radius: 14px; }
|
||||||
|
.glass-inputbar {
|
||||||
|
display: flex; gap: 8px; margin: 0 12px calc(12px + env(safe-area-inset-bottom));
|
||||||
|
padding: 8px; flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.glass-inputbar input { flex: 1; }
|
||||||
|
|
||||||
|
/* 抽屉 */
|
||||||
|
.drawer-mask { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 60;
|
||||||
|
-webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
|
||||||
|
.glass-drawer {
|
||||||
|
position: fixed; right: 0; top: 0; bottom: 0; width: min(420px, 92vw);
|
||||||
|
z-index: 70; border-radius: 24px 0 0 24px; padding: 20px;
|
||||||
|
overflow-y: auto; transform: translateX(100%); transition: transform .3s cubic-bezier(.3,.9,.3,1);
|
||||||
|
}
|
||||||
|
.glass-drawer.open { transform: translateX(0); }
|
||||||
|
.mem-item { padding: 10px 12px; border-radius: 12px; background: rgba(255,255,255,.05);
|
||||||
|
border: 1px solid var(--stroke); margin-bottom: 8px; font-size: 13px; }
|
||||||
|
.mem-item .kind { font-size: 10px; color: var(--accent); text-transform: uppercase; }
|
||||||
|
.mem-item .row { margin-top: 6px; }
|
||||||
|
|
||||||
|
/* 弹窗 */
|
||||||
|
.modal {
|
||||||
|
position: fixed; left: 50%; top: 50%; transform: translate(-50%,-50%);
|
||||||
|
width: min(460px, 92vw); max-height: 86vh; overflow-y: auto; z-index: 80;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 引导 */
|
||||||
|
.guide-mask { position: fixed; inset: 0; z-index: 90;
|
||||||
|
background: rgba(0,0,0,.55);
|
||||||
|
-webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); }
|
||||||
|
|
||||||
|
/* toast */
|
||||||
|
.toast { position: fixed; bottom: calc(90px + env(safe-area-inset-bottom)); left: 50%;
|
||||||
|
transform: translateX(-50%); padding: 10px 18px; border-radius: 14px; z-index: 100;
|
||||||
|
background: rgba(30,32,60,.9); border: 1px solid var(--stroke); font-size: 14px;
|
||||||
|
-webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px); }
|
||||||
|
|
||||||
|
@media (max-width: 640px) {
|
||||||
|
.card-grid { grid-template-columns: repeat(2, 1fr); }
|
||||||
|
.bubble { max-width: 90%; }
|
||||||
|
}
|
||||||
+125
@@ -0,0 +1,125 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no">
|
||||||
|
<title>Open Maopaoya · 冒泡鸭开源复刻</title>
|
||||||
|
<link rel="stylesheet" href="/css/glass.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="bg-blobs"><i></i><i></i><i></i></div>
|
||||||
|
|
||||||
|
<!-- 登录/注册 -->
|
||||||
|
<div id="auth-view" class="view glass-card center-card">
|
||||||
|
<h1 class="logo">🦆 Open Maopaoya</h1>
|
||||||
|
<p class="sub">冒泡鸭开源复刻 · 动态小说引擎</p>
|
||||||
|
<input id="auth-user" placeholder="用户名" autocomplete="username">
|
||||||
|
<input id="auth-pass" type="password" placeholder="密码" autocomplete="current-password">
|
||||||
|
<div class="row">
|
||||||
|
<button class="btn primary" onclick="doLogin()">登录</button>
|
||||||
|
<button class="btn" onclick="doRegister()">注册</button>
|
||||||
|
</div>
|
||||||
|
<p id="auth-msg" class="msg"></p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 主应用 -->
|
||||||
|
<div id="app-view" class="view hidden">
|
||||||
|
<nav class="glass-nav">
|
||||||
|
<button data-page="shelf" class="nav-btn active">📚 书架</button>
|
||||||
|
<button data-page="chars" class="nav-btn">🎭 角色</button>
|
||||||
|
<button data-page="novel" class="nav-btn">📖 小说</button>
|
||||||
|
<button data-page="channels" class="nav-btn">🔌 渠道</button>
|
||||||
|
<button data-page="admin" class="nav-btn admin-only">⚙️ 后台</button>
|
||||||
|
<span class="spacer"></span>
|
||||||
|
<span id="whoami" class="who"></span>
|
||||||
|
<button class="nav-btn" onclick="logout()">退出</button>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<!-- 书架 -->
|
||||||
|
<section id="page-shelf" class="page">
|
||||||
|
<div class="row spread"><h2>书架</h2>
|
||||||
|
<button class="btn primary" onclick="newRoleplaySession()">+ 新对话</button></div>
|
||||||
|
<div id="shelf-list" class="card-grid"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 角色 -->
|
||||||
|
<section id="page-chars" class="page hidden">
|
||||||
|
<div class="row spread"><h2>角色库</h2>
|
||||||
|
<button class="btn primary" onclick="editCharacter(null)">+ 新角色</button></div>
|
||||||
|
<div id="chars-grid" class="card-grid"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 小说 -->
|
||||||
|
<section id="page-novel" class="page hidden">
|
||||||
|
<div class="row spread"><h2>动态小说</h2>
|
||||||
|
<button class="btn primary" onclick="newNovelSession()">+ 开新小说</button></div>
|
||||||
|
<p class="hint">开一本自定义小说,AI 编剧会自主引入新角色并自动建档、生成立绘。新角色可以在「角色」页单独拉出来聊。</p>
|
||||||
|
<div id="novel-list" class="card-grid"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 渠道 -->
|
||||||
|
<section id="page-channels" class="page hidden">
|
||||||
|
<div class="row spread"><h2>模型渠道</h2>
|
||||||
|
<button class="btn primary" onclick="editChannel(null)">+ 添加渠道</button></div>
|
||||||
|
<div id="channel-list" class="card-grid"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 后台 -->
|
||||||
|
<section id="page-admin" class="page hidden">
|
||||||
|
<h2>管理后台</h2>
|
||||||
|
<div class="glass-card">
|
||||||
|
<h3>权限开关</h3>
|
||||||
|
<div id="toggles"></div>
|
||||||
|
</div>
|
||||||
|
<div class="glass-card">
|
||||||
|
<h3>用户管理 <span id="admin-stats" class="hint"></span></h3>
|
||||||
|
<div id="admin-users"></div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 聊天页 -->
|
||||||
|
<div id="chat-view" class="view hidden">
|
||||||
|
<header class="glass-nav">
|
||||||
|
<button class="nav-btn" onclick="backToShelf()">←</button>
|
||||||
|
<b id="chat-title">会话</b>
|
||||||
|
<span class="spacer"></span>
|
||||||
|
<button class="nav-btn" onclick="openDrawer('memory')">🧠 记忆</button>
|
||||||
|
<button class="nav-btn" onclick="openDrawer('worldbook')">🌍 世界书</button>
|
||||||
|
<button class="nav-btn" onclick="openDrawer('card')">🎭 角色</button>
|
||||||
|
</header>
|
||||||
|
<div id="chat-log"></div>
|
||||||
|
<div class="glass-inputbar">
|
||||||
|
<input id="chat-input" placeholder="说点什么…" onkeydown="if(event.key==='Enter')sendMsg()">
|
||||||
|
<button class="btn primary" onclick="sendMsg()">发送</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 侧滑抽屉 -->
|
||||||
|
<div id="drawer-mask" class="drawer-mask hidden" onclick="closeDrawer()"></div>
|
||||||
|
<aside id="drawer" class="glass-drawer">
|
||||||
|
<div class="row spread"><b id="drawer-title">面板</b>
|
||||||
|
<button class="btn small" onclick="closeDrawer()">✕</button></div>
|
||||||
|
<div id="drawer-body"></div>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<!-- 编辑弹窗 -->
|
||||||
|
<div id="modal-mask" class="drawer-mask hidden"></div>
|
||||||
|
<div id="modal" class="glass-card modal hidden"></div>
|
||||||
|
|
||||||
|
<!-- 新手指引 -->
|
||||||
|
<div id="guide" class="guide-mask hidden">
|
||||||
|
<div class="glass-card center-card">
|
||||||
|
<h2>👋 三步开聊</h2>
|
||||||
|
<div id="guide-body"></div>
|
||||||
|
<div class="row">
|
||||||
|
<button class="btn primary" onclick="guideNext()">下一步</button>
|
||||||
|
<button class="btn" onclick="closeGuide()">跳过</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="toast" class="toast hidden"></div>
|
||||||
|
<script src="/js/app.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
+414
@@ -0,0 +1,414 @@
|
|||||||
|
/* Open Maopaoya SPA */
|
||||||
|
const $ = (s) => document.querySelector(s);
|
||||||
|
const API = "/api";
|
||||||
|
let TOKEN = localStorage.token || "";
|
||||||
|
let ME = null;
|
||||||
|
let CUR = { sessionId: null, mode: "roleplay", characterId: null };
|
||||||
|
let GUIDE_STEP = 0;
|
||||||
|
|
||||||
|
async function api(path, opts = {}) {
|
||||||
|
const r = await fetch(API + path, {
|
||||||
|
...opts,
|
||||||
|
headers: { "Content-Type": "application/json", Authorization: "Bearer " + TOKEN,
|
||||||
|
...(opts.headers || {}) },
|
||||||
|
body: opts.body ? JSON.stringify(opts.body) : undefined,
|
||||||
|
});
|
||||||
|
if (!r.ok) throw new Error((await r.json().catch(() => ({}))).detail || r.status);
|
||||||
|
return r.json();
|
||||||
|
}
|
||||||
|
function toast(t) { const el = $("#toast"); el.textContent = t; el.classList.remove("hidden");
|
||||||
|
setTimeout(() => el.classList.add("hidden"), 2200); }
|
||||||
|
|
||||||
|
/* ---------- auth ---------- */
|
||||||
|
async function doLogin() {
|
||||||
|
try {
|
||||||
|
const d = await api("/auth/login", { method: "POST",
|
||||||
|
body: { username: $("#auth-user").value, password: $("#auth-pass").value } });
|
||||||
|
TOKEN = d.token; localStorage.token = TOKEN;
|
||||||
|
ME = { username: d.username, is_admin: d.is_admin };
|
||||||
|
enterApp();
|
||||||
|
} catch (e) { $("#auth-msg").textContent = e.message; }
|
||||||
|
}
|
||||||
|
async function doRegister() {
|
||||||
|
try {
|
||||||
|
const d = await api("/auth/register", { method: "POST",
|
||||||
|
body: { username: $("#auth-user").value, password: $("#auth-pass").value } });
|
||||||
|
TOKEN = d.token; localStorage.token = TOKEN;
|
||||||
|
ME = { username: d.username, is_admin: d.is_admin };
|
||||||
|
enterApp(); GUIDE_STEP = 0; openGuide();
|
||||||
|
} catch (e) { $("#auth-msg").textContent = e.message; }
|
||||||
|
}
|
||||||
|
function logout() { localStorage.removeItem("token"); location.reload(); }
|
||||||
|
|
||||||
|
async function enterApp() {
|
||||||
|
$("#auth-view").classList.add("hidden");
|
||||||
|
$("#app-view").classList.remove("hidden");
|
||||||
|
$("#whoami").textContent = ME.username;
|
||||||
|
if (!ME.is_admin) document.querySelectorAll(".admin-only").forEach(e => e.classList.add("hidden"));
|
||||||
|
await Promise.all([loadShelf(), loadCharacters(), loadNovels(), loadChannels()]);
|
||||||
|
if (ME.is_admin) loadAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 导航 ---------- */
|
||||||
|
document.querySelectorAll(".nav-btn[data-page]").forEach(b =>
|
||||||
|
b.onclick = () => {
|
||||||
|
document.querySelectorAll(".nav-btn[data-page]").forEach(x => x.classList.remove("active"));
|
||||||
|
b.classList.add("active");
|
||||||
|
document.querySelectorAll(".page").forEach(p => p.classList.add("hidden"));
|
||||||
|
$("#page-" + b.dataset.page).classList.remove("hidden");
|
||||||
|
});
|
||||||
|
|
||||||
|
/* ---------- 书架 / 会话 ---------- */
|
||||||
|
async function loadShelf() {
|
||||||
|
const d = await api("/sessions");
|
||||||
|
$("#shelf-list").innerHTML = d.sessions.filter(s => s.mode === "roleplay").map(s =>
|
||||||
|
`<div class="mini-card" onclick="openChat(${s.id},'roleplay')">
|
||||||
|
<h4>${esc(s.title)}</h4><p><span class="tag">角色扮演</span></p></div>`).join("")
|
||||||
|
|| '<p class="hint">还没有会话,点右上角开一个。</p>';
|
||||||
|
}
|
||||||
|
async function newRoleplaySession() {
|
||||||
|
const chars = (await api("/characters")).characters;
|
||||||
|
openModal(`<h3>新对话</h3>
|
||||||
|
<select id="pick-char">${chars.map(c => `<option value="${c.id}">${esc(c.name)}</option>`).join("")}</select>
|
||||||
|
<div class="row" style="margin-top:12px">
|
||||||
|
<button class="btn primary" onclick="confirmRoleplay()">开始</button>
|
||||||
|
<button class="btn" onclick="closeModal()">取消</button></div>
|
||||||
|
${chars.length ? "" : '<p class="hint">还没有角色,先去「角色」页建一个。</p>'}`);
|
||||||
|
}
|
||||||
|
async function confirmRoleplay() {
|
||||||
|
const cid = +$("#pick-char").value;
|
||||||
|
const s = await api("/sessions", { method: "POST",
|
||||||
|
body: { mode: "roleplay", title: "与角色对话", character_id: cid } });
|
||||||
|
closeModal(); openChat(s.id, "roleplay", cid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 角色 ---------- */
|
||||||
|
async function loadCharacters() {
|
||||||
|
const d = await api("/characters");
|
||||||
|
$("#chars-grid").innerHTML = d.characters.map(c =>
|
||||||
|
`<div class="mini-card">
|
||||||
|
${c.avatar_url ? `<img class="avatar" src="${c.avatar_url}">` : ""}
|
||||||
|
<h4>${esc(c.name)} ${c.source === "novel_auto" ? '<span class="tag">AI生成</span>' : ""}</h4>
|
||||||
|
<p>${esc(c.persona || "(无人设)")}</p>
|
||||||
|
<div class="row" style="margin-top:8px">
|
||||||
|
<button class="btn small" onclick="openChat(null,'roleplay',${c.id})">对话</button>
|
||||||
|
<button class="btn small" onclick="editCharacter(${c.id})">编辑</button>
|
||||||
|
<button class="btn small danger" onclick="delCharacter(${c.id})">删</button>
|
||||||
|
</div></div>`).join("") || '<p class="hint">还没有角色。</p>';
|
||||||
|
}
|
||||||
|
async function editCharacter(id) {
|
||||||
|
let c = { name: "", persona: "", greeting: "", voice: "tongtong", avatar_url: "" };
|
||||||
|
if (id) c = (await api("/characters")).characters.find(x => x.id === id);
|
||||||
|
openModal(`<h3>${id ? "编辑" : "新建"}角色</h3>
|
||||||
|
<input id="c-name" placeholder="名字" value="${esc(c.name)}">
|
||||||
|
<textarea id="c-persona" placeholder="人设:性格、身份、说话风格…">${esc(c.persona)}</textarea>
|
||||||
|
<textarea id="c-greeting" placeholder="开场白">${esc(c.greeting)}</textarea>
|
||||||
|
<select id="c-voice">${["tongtong","xiaoyu","xiaochen","laowang","xiaolin"]
|
||||||
|
.map(v => `<option ${v === c.voice ? "selected" : ""}>${v}</option>`).join("")}</select>
|
||||||
|
<input id="c-avatar" placeholder="立绘 URL(可留空)" value="${esc(c.avatar_url)}">
|
||||||
|
<div class="row"><button class="btn primary" onclick="saveCharacter(${id || 0})">保存</button>
|
||||||
|
<button class="btn" onclick="closeModal()">取消</button></div>`);
|
||||||
|
}
|
||||||
|
async function saveCharacter(id) {
|
||||||
|
const body = { name: $("#c-name").value, persona: $("#c-persona").value,
|
||||||
|
greeting: $("#c-greeting").value, voice: $("#c-voice").value,
|
||||||
|
avatar_url: $("#c-avatar").value };
|
||||||
|
if (id) await api(`/characters/${id}`, { method: "PATCH", body });
|
||||||
|
else await api("/characters", { method: "POST", body });
|
||||||
|
closeModal(); loadCharacters(); toast("已保存");
|
||||||
|
}
|
||||||
|
async function delCharacter(id) {
|
||||||
|
if (!confirm("删除该角色及其记忆?")) return;
|
||||||
|
await api(`/characters/${id}`, { method: "DELETE" }); loadCharacters();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 小说 ---------- */
|
||||||
|
async function loadNovels() {
|
||||||
|
const d = await api("/sessions");
|
||||||
|
$("#novel-list").innerHTML = d.sessions.filter(s => s.mode === "novel").map(s =>
|
||||||
|
`<div class="mini-card" onclick="openChat(${s.id},'novel')">
|
||||||
|
<h4>${esc(s.title)}</h4><p><span class="tag">动态小说 · AI自主扩列</span></p></div>`).join("")
|
||||||
|
|| '<p class="hint">还没有小说。</p>';
|
||||||
|
}
|
||||||
|
function newNovelSession() {
|
||||||
|
openModal(`<h3>开新小说</h3>
|
||||||
|
<input id="n-title" placeholder="小说标题">
|
||||||
|
<textarea id="n-setting" placeholder="设定:世界观、主角、开篇场景…写多细都行,AI 会照着演"></textarea>
|
||||||
|
<div class="row" style="margin-top:12px">
|
||||||
|
<button class="btn primary" onclick="confirmNovel()">开写</button>
|
||||||
|
<button class="btn" onclick="closeModal()">取消</button></div>`);
|
||||||
|
}
|
||||||
|
async function confirmNovel() {
|
||||||
|
const s = await api("/sessions", { method: "POST", body: {
|
||||||
|
mode: "novel", title: $("#n-title").value || "未命名小说",
|
||||||
|
novel_setting: $("#n-setting").value } });
|
||||||
|
closeModal(); openChat(s.id, "novel");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 聊天 ---------- */
|
||||||
|
async function openChat(sid, mode, cid) {
|
||||||
|
CUR = { sessionId: sid, mode, characterId: cid };
|
||||||
|
$("#app-view").classList.add("hidden");
|
||||||
|
$("#chat-view").classList.remove("hidden");
|
||||||
|
const d = await api(`/sessions/${sid}/messages`);
|
||||||
|
$("#chat-log").innerHTML = d.messages.map(renderBubble).join("");
|
||||||
|
$("#chat-log").scrollTop = 1e9;
|
||||||
|
}
|
||||||
|
function backToShelf() {
|
||||||
|
$("#chat-view").classList.add("hidden");
|
||||||
|
$("#app-view").classList.remove("hidden");
|
||||||
|
loadShelf(); loadCharacters(); loadNovels();
|
||||||
|
}
|
||||||
|
function renderBubble(m) {
|
||||||
|
const cls = m.role === "user" ? "user" : "ai";
|
||||||
|
return `<div class="bubble ${cls}" data-mid="${m.id}">
|
||||||
|
${m.speaker ? `<div class="speaker">${esc(m.speaker)}</div>` : ""}${esc(m.content)}
|
||||||
|
<div class="acts">
|
||||||
|
<button onclick="editMsg(${m.id})">✏️</button>
|
||||||
|
<button onclick="delMsg(${m.id})">🗑</button>
|
||||||
|
${m.role !== "user" ? `<button onclick="ttsMsg(${m.id},this)">🔊</button>` : ""}
|
||||||
|
</div></div>`;
|
||||||
|
}
|
||||||
|
async function sendMsg() {
|
||||||
|
const input = $("#chat-input");
|
||||||
|
const text = input.value.trim();
|
||||||
|
if (!text) return;
|
||||||
|
input.value = "";
|
||||||
|
$("#chat-log").insertAdjacentHTML("beforeend",
|
||||||
|
`<div class="bubble user">${esc(text)}</div>`);
|
||||||
|
const thinking = document.createElement("div");
|
||||||
|
thinking.className = "bubble ai"; thinking.textContent = "…";
|
||||||
|
$("#chat-log").appendChild(thinking);
|
||||||
|
$("#chat-log").scrollTop = 1e9;
|
||||||
|
try {
|
||||||
|
const d = await api(`/sessions/${CUR.sessionId}/send`, { method: "POST", body: { content: text } });
|
||||||
|
thinking.innerHTML = renderBubble({ id: d.message_id, role: "assistant", content: d.reply });
|
||||||
|
d.new_characters.forEach(c => {
|
||||||
|
$("#chat-log").insertAdjacentHTML("beforeend",
|
||||||
|
`<div class="new-char-banner">✨ 新角色入场:${esc(c.name)}(已自动建档${c.avatar_url ? " + 生成立绘" : ""})</div>`);
|
||||||
|
});
|
||||||
|
} catch (e) { thinking.textContent = "⚠️ " + e.message; }
|
||||||
|
$("#chat-log").scrollTop = 1e9;
|
||||||
|
}
|
||||||
|
async function editMsg(mid) {
|
||||||
|
const d = await api(`/sessions/${CUR.sessionId}/messages`);
|
||||||
|
const m = d.messages.find(x => x.id === mid);
|
||||||
|
const nv = prompt("编辑消息", m.content);
|
||||||
|
if (nv === null) return;
|
||||||
|
await api(`/messages/${mid}`, { method: "PATCH", body: { content: nv } });
|
||||||
|
openChat(CUR.sessionId, CUR.mode, CUR.characterId);
|
||||||
|
}
|
||||||
|
async function delMsg(mid) {
|
||||||
|
if (!confirm("删除这条消息?")) return;
|
||||||
|
await api(`/messages/${mid}`, { method: "DELETE" });
|
||||||
|
openChat(CUR.sessionId, CUR.mode, CUR.characterId);
|
||||||
|
}
|
||||||
|
async function ttsMsg(mid, btn) {
|
||||||
|
btn.textContent = "⏳";
|
||||||
|
try {
|
||||||
|
const r = await fetch(`${API}/sessions/${CUR.sessionId}/tts/${mid}`,
|
||||||
|
{ headers: { Authorization: "Bearer " + TOKEN } });
|
||||||
|
if (!r.ok) throw new Error(await r.text());
|
||||||
|
btn.textContent = "🔊";
|
||||||
|
new Audio(URL.createObjectURL(await r.blob())).play();
|
||||||
|
} catch (e) { btn.textContent = "🔊"; toast("TTS 失败:" + e.message); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 抽屉:记忆 / 世界书 / 角色卡 ---------- */
|
||||||
|
async function openDrawer(kind) {
|
||||||
|
$("#drawer").classList.add("open");
|
||||||
|
$("#drawer-mask").classList.remove("hidden");
|
||||||
|
const body = $("#drawer-body");
|
||||||
|
if (kind === "memory") {
|
||||||
|
$("#drawer-title").textContent = "🧠 角色记忆(可篡改)";
|
||||||
|
if (!CUR.characterId) { body.innerHTML = '<p class="hint">小说模式请在世界书里看角色。</p>'; return; }
|
||||||
|
const d = await api(`/characters/${CUR.characterId}/memories`);
|
||||||
|
body.innerHTML = d.memories.map(m =>
|
||||||
|
`<div class="mem-item"><span class="kind">${m.kind}</span><br>${esc(m.content)}
|
||||||
|
<div class="row"><button class="btn small" onclick="editMemory(${m.id})">改</button>
|
||||||
|
<button class="btn small danger" onclick="delMemory(${m.id})">删</button></div></div>`).join("")
|
||||||
|
|| '<p class="hint">暂无记忆。聊天中重要事实会被自动记录,你也可以手动添加。</p>';
|
||||||
|
body.insertAdjacentHTML("beforeend", `<div class="row">
|
||||||
|
<button class="btn small primary" onclick="addMemory('fact')">+ 设定</button>
|
||||||
|
<button class="btn small primary" onclick="addMemory('event')">+ 事件</button></div>`);
|
||||||
|
} else if (kind === "worldbook") {
|
||||||
|
$("#drawer-title").textContent = "🌍 世界书";
|
||||||
|
const d = await api("/characters/worldbook/all");
|
||||||
|
const entries = d.entries.filter(e => e.scope === (CUR.mode === "novel" ? "novel" : "character"));
|
||||||
|
body.innerHTML = entries.map(w =>
|
||||||
|
`<div class="mem-item"><span class="kind">${w.constant ? "常驻" : esc(w.keywords || "关键词")}</span><br>${esc(w.content)}
|
||||||
|
<div class="row"><button class="btn small" onclick="editWb(${w.id})">改</button>
|
||||||
|
<button class="btn small danger" onclick="delWb(${w.id})">删</button></div></div>`).join("")
|
||||||
|
|| '<p class="hint">暂无条目。动态小说中 AI 新建的角色会自动注册进来。</p>';
|
||||||
|
body.insertAdjacentHTML("beforeend",
|
||||||
|
`<button class="btn small primary" onclick="editWb(null)">+ 新条目</button>`);
|
||||||
|
} else {
|
||||||
|
$("#drawer-title").textContent = "🎭 角色卡";
|
||||||
|
if (!CUR.characterId) { body.innerHTML = '<p class="hint">小说模式暂无单角色卡。</p>'; return; }
|
||||||
|
const c = (await api("/characters")).characters.find(x => x.id === CUR.characterId);
|
||||||
|
body.innerHTML = `<input value="${esc(c.name)}" disabled>
|
||||||
|
<textarea disabled style="margin-top:8px">${esc(c.persona)}</textarea>
|
||||||
|
<div class="row" style="margin-top:10px">
|
||||||
|
<button class="btn small primary" onclick="editCharacter(${c.id})">编辑角色</button></div>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function closeDrawer() { $("#drawer").classList.remove("open");
|
||||||
|
$("#drawer-mask").classList.add("hidden"); }
|
||||||
|
async function addMemory(kind) {
|
||||||
|
const c = prompt("记忆内容");
|
||||||
|
if (!c) return;
|
||||||
|
await api(`/characters/${CUR.characterId}/memories`, { method: "POST", body: { kind, content: c } });
|
||||||
|
openDrawer("memory");
|
||||||
|
}
|
||||||
|
async function editMemory(mid) {
|
||||||
|
const d = await api(`/characters/${CUR.characterId}/memories`);
|
||||||
|
const m = d.memories.find(x => x.id === mid);
|
||||||
|
const c = prompt("修改记忆", m.content);
|
||||||
|
if (c === null) return;
|
||||||
|
await api(`/characters/${CUR.characterId}/memories/${mid}`, { method: "PATCH", body: { kind: m.kind, content: c } });
|
||||||
|
openDrawer("memory");
|
||||||
|
}
|
||||||
|
async function delMemory(mid) {
|
||||||
|
if (!confirm("删除该记忆?")) return;
|
||||||
|
await api(`/characters/${CUR.characterId}/memories/${mid}`, { method: "DELETE" });
|
||||||
|
openDrawer("memory");
|
||||||
|
}
|
||||||
|
async function editWb(wid) {
|
||||||
|
let w = { keywords: "", content: "", constant: false };
|
||||||
|
if (wid) w = (await api("/characters/worldbook/all")).entries.find(x => x.id === wid);
|
||||||
|
const scope = CUR.mode === "novel" ? "novel" : "character";
|
||||||
|
const kw = prompt("触发关键词(逗号分隔,留空=常驻)", w.keywords);
|
||||||
|
if (kw === null) return;
|
||||||
|
const ct = prompt("条目内容", w.content);
|
||||||
|
if (ct === null) return;
|
||||||
|
const body = { scope, scope_id: CUR.sessionId || 0, keywords: kw, content: ct, constant: !kw, enabled: true };
|
||||||
|
if (wid) await api(`/characters/worldbook/${wid}`, { method: "PATCH", body });
|
||||||
|
else await api("/characters/worldbook", { method: "POST", body });
|
||||||
|
openDrawer("worldbook");
|
||||||
|
}
|
||||||
|
async function delWb(wid) {
|
||||||
|
if (!confirm("删除条目?")) return;
|
||||||
|
await api(`/characters/worldbook/${wid}`, { method: "DELETE" });
|
||||||
|
openDrawer("worldbook");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 渠道 ---------- */
|
||||||
|
async function loadChannels() {
|
||||||
|
const d = await api("/channels");
|
||||||
|
$("#channel-list").innerHTML = d.channels.map(c =>
|
||||||
|
`<div class="mini-card"><h4>${esc(c.name)} ${c.owner_id ? '<span class="tag">私有</span>' : '<span class="tag">全局</span>'}</h4>
|
||||||
|
<p>${esc(c.base_url)}<br>叙事: ${esc((c.models || {}).narrative || "-")}</p>
|
||||||
|
<div class="row" style="margin-top:8px">
|
||||||
|
<button class="btn small" onclick="testChannel(${c.id},this)">测试</button>
|
||||||
|
<button class="btn small" onclick="editChannel(${c.id})">编辑</button>
|
||||||
|
<button class="btn small danger" onclick="delChannel(${c.id})">删</button></div></div>`).join("")
|
||||||
|
|| '<p class="hint">还没有渠道。添加一个 OpenAI 兼容 API 即可开聊。</p>';
|
||||||
|
}
|
||||||
|
async function editChannel(id) {
|
||||||
|
const preset = await api("/channels/preset");
|
||||||
|
let c = { name: preset.name, base_url: preset.base_url, models: preset.models, api_key: "" };
|
||||||
|
if (id) c = (await api("/channels")).channels.find(x => x.id === id);
|
||||||
|
openModal(`<h3>${id ? "编辑" : "添加"}渠道</h3>
|
||||||
|
<input id="ch-name" placeholder="名称" value="${esc(c.name)}">
|
||||||
|
<input id="ch-url" placeholder="Base URL" value="${esc(c.base_url)}">
|
||||||
|
<input id="ch-key" type="password" placeholder="API Key${id ? "(留空不改)" : ""}">
|
||||||
|
<input id="ch-nar" placeholder="叙事模型" value="${esc((c.models || {}).narrative || "")}">
|
||||||
|
<input id="ch-tts" placeholder="TTS 模型" value="${esc((c.models || {}).tts || "")}">
|
||||||
|
<input id="ch-img" placeholder="生图模型(可留空)" value="${esc((c.models || {}).image || "")}">
|
||||||
|
<div class="row" style="margin-top:12px">
|
||||||
|
<button class="btn primary" onclick="saveChannel(${id || 0})">保存</button>
|
||||||
|
<button class="btn" onclick="closeModal()">取消</button></div>`);
|
||||||
|
}
|
||||||
|
async function saveChannel(id) {
|
||||||
|
const body = { name: $("#ch-name").value, base_url: $("#ch-url").value,
|
||||||
|
api_key: $("#ch-key").value,
|
||||||
|
models: { narrative: $("#ch-nar").value, extract: $("#ch-nar").value,
|
||||||
|
tts: $("#ch-tts").value, image: $("#ch-img").value } };
|
||||||
|
if (id) await api(`/channels/${id}`, { method: "PATCH", body });
|
||||||
|
else await api("/channels", { method: "POST", body });
|
||||||
|
closeModal(); loadChannels(); toast("渠道已保存");
|
||||||
|
}
|
||||||
|
async function testChannel(id, btn) {
|
||||||
|
btn.textContent = "⏳";
|
||||||
|
try { const d = await api(`/channels/${id}/test`, { method: "POST" });
|
||||||
|
btn.textContent = d.ok ? "✅" : "❌"; toast(d.ok ? "渠道正常:" + d.model : "失败:" + d.message);
|
||||||
|
} catch (e) { btn.textContent = "❌"; toast(e.message); }
|
||||||
|
setTimeout(() => btn.textContent = "测试", 3000);
|
||||||
|
}
|
||||||
|
async function delChannel(id) {
|
||||||
|
if (!confirm("删除渠道?")) return;
|
||||||
|
await api(`/channels/${id}`, { method: "DELETE" }); loadChannels();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 后台 ---------- */
|
||||||
|
async function loadAdmin() {
|
||||||
|
const s = await api("/admin/settings");
|
||||||
|
const labels = { allow_register: "允许注册", allow_delete_account: "允许注销",
|
||||||
|
allow_byok: "允许用户自定义 API 渠道", admin_data_visible: "数据可见(关闭=加密存储,管理员不可读,静默生效)" };
|
||||||
|
$("#toggles").innerHTML = Object.entries(labels).map(([k, label]) =>
|
||||||
|
`<div class="row spread" style="margin-bottom:10px"><span>${label}</span>
|
||||||
|
<button class="btn small ${s[k] === "true" ? "primary" : ""}" onclick="toggleSetting('${k}',this)">${s[k] === "true" ? "开" : "关"}</button></div>`).join("");
|
||||||
|
const u = await api("/admin/users");
|
||||||
|
$("#admin-stats").textContent = `用户 ${u.stats.users} · 会话 ${u.stats.sessions}`;
|
||||||
|
$("#admin-users").innerHTML = u.users.map(x =>
|
||||||
|
`<div class="mem-item"><b>${esc(x.username)}</b> ${x.is_admin ? '<span class="tag">管理员</span>' : ""}
|
||||||
|
${x.is_active ? "" : '<span class="tag">已封禁</span>'}
|
||||||
|
<div class="row">
|
||||||
|
<button class="btn small" onclick="adminPatch(${x.id},{'is_active':${!x.is_active}})">${x.is_active ? "封禁" : "解封"}</button>
|
||||||
|
<button class="btn small" onclick="adminPatch(${x.id},{'is_admin':${!x.is_admin}})">${x.is_admin ? "去管理员" : "设管理员"}</button>
|
||||||
|
<button class="btn small" onclick="adminResetPwd(${x.id})">重置密码</button>
|
||||||
|
</div></div>`).join("");
|
||||||
|
}
|
||||||
|
async function toggleSetting(key, btn) {
|
||||||
|
const cur = btn.textContent === "开";
|
||||||
|
await api(`/admin/settings?key=${key}&value=${!cur}`, { method: "POST" });
|
||||||
|
loadAdmin();
|
||||||
|
if (key === "admin_data_visible") toast(!cur ? "已改为明文(用户下次登录时解密存量数据)" : "已改为加密(管理员不可读)");
|
||||||
|
}
|
||||||
|
async function adminPatch(id, body) {
|
||||||
|
await api(`/admin/users/${id}`, { method: "PATCH", body }); loadAdmin();
|
||||||
|
}
|
||||||
|
async function adminResetPwd(id) {
|
||||||
|
const p = prompt("新密码"); if (!p) return;
|
||||||
|
await api(`/admin/users/${id}`, { method: "PATCH", body: { new_password: p } });
|
||||||
|
toast("已重置");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 引导 ---------- */
|
||||||
|
function openGuide() { $("#guide").classList.remove("hidden"); renderGuide(); }
|
||||||
|
function closeGuide() { $("#guide").classList.add("hidden"); }
|
||||||
|
async function renderGuide() {
|
||||||
|
const preset = await api("/channels/preset");
|
||||||
|
const bodies = [
|
||||||
|
`<h3>第 1 步 · 填 Key</h3><p class="hint">已预填 Step Plan 预设(${preset.base_url}),你只需要粘贴 API Key。</p>
|
||||||
|
<input id="g-key" type="password" placeholder="粘贴 Step Plan API Key">`,
|
||||||
|
`<h3>第 2 步 · 确认模型</h3><p class="hint">默认:叙事/抽取 ${preset.models.narrative},TTS ${preset.models.tts}。直接下一步就行。</p>`,
|
||||||
|
`<h3>第 3 步 · 开聊!</h3><p class="hint">去书架开一个角色对话,或去小说页开一本 AI 自主扩列的动态小说。</p>`];
|
||||||
|
$("#guide-body").innerHTML = bodies[GUIDE_STEP];
|
||||||
|
}
|
||||||
|
async function guideNext() {
|
||||||
|
if (GUIDE_STEP === 0) {
|
||||||
|
const key = $("#g-key") ? $("#g-key").value : "";
|
||||||
|
if (key) { await api("/channels", { method: "POST", body: {
|
||||||
|
name: "Step Plan", base_url: (await api("/channels/preset")).base_url,
|
||||||
|
api_key: key, models: (await api("/channels/preset")).models } }); }
|
||||||
|
GUIDE_STEP = 1;
|
||||||
|
} else if (GUIDE_STEP === 2) { closeGuide(); return; }
|
||||||
|
else GUIDE_STEP++;
|
||||||
|
renderGuide(); if (GUIDE_STEP === 1) renderGuide();
|
||||||
|
loadChannels();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 通用 ---------- */
|
||||||
|
function openModal(html) { $("#modal").innerHTML = html; $("#modal").classList.remove("hidden");
|
||||||
|
$("#modal-mask").classList.remove("hidden"); }
|
||||||
|
function closeModal() { $("#modal").classList.add("hidden");
|
||||||
|
$("#modal-mask").classList.add("hidden"); }
|
||||||
|
function esc(s) { return String(s || "").replace(/[&<>"']/g, c =>
|
||||||
|
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c])); }
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
if (!TOKEN) return;
|
||||||
|
try { ME = await api("/auth/me"); enterApp(); }
|
||||||
|
catch (e) { localStorage.removeItem("token"); }
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user