Open Maopaoya M0-M2: 用户体系+权限开关+后台 / 渠道管理 / 单角色扮演(记忆可篡改+世界书+TTS) / 动态小说管线(实体抽取→建档→立绘→世界书注册) / AES-GCM数据加密+登录解密迁移 / 液态玻璃UI
This commit is contained in:
@@ -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