feat(digital): 打造星港响应式数字人对话空间

新增角色探索、收藏、世界书和身份资料;接入流式打字、停止重生成、固定记忆、会话分支与导出;适配手机和桌面并保持实验只由后台控制。
This commit is contained in:
湛兮
2026-10-27 10:25:40 +08:00
parent 6b0dd6ea67
commit b7433acba4
37 changed files with 3914 additions and 106 deletions
+30 -29
View File
@@ -1,38 +1,39 @@
/** 星港登录页,沿用既有账号,无公开注册。 */
import Link from "next/link";
import { LoginForm } from "@/components/login-form";
import "@/components/digital-login.css";
/** 登录前展示独立品牌与简洁表单。 */
export default function LoginPage() {
return (
<main className="login-page">
<section className="login-hero">
<div className="login-brand-line">
<span className="brand-mark" aria-hidden />
<span>Role User</span>
</div>
<p className="hero-kicker">Employee Console</p>
<h1>门店工作,清晰处理</h1>
<p>排班、任务、公告和账号安全集中在同一个移动端工作区。</p>
<div className="login-preview" aria-hidden>
<div className="login-preview__top">
<span />
<span />
</div>
<div className="login-preview__body">
<strong>08:30</strong>
<span>早班 · 正常</span>
</div>
<div className="login-preview__grid">
<span />
<span />
<span />
</div>
<main className="stellar-login">
<section className="stellar-login-story">
<Link href="/" className="stellar-login-brand">
✦{" "}
<span>
星港 <small>STELLAR</small>
</span>
</Link>
<div>
<p>YOUR PRIVATE UNIVERSE</p>
<h1>
每一段相遇,
<br />
都有下一章。
</h1>
<span>
与你心动的角色对话,
<br />
在世界的另一端,写下属于你的故事。
</span>
</div>
<small>虚构相遇,真实灵感。</small>
</section>
<section className="login-panel" aria-label="员工登录">
<div className="login-panel__header">
<h2>员工登录</h2>
<p>使用手机号和临时密码进入工作台。</p>
</div>
<section className="stellar-login-panel">
<span className="stellar-login-star">✧</span>
<h2>欢迎回到星港</h2>
<p>使用你的现有账号,继续未完的故事。</p>
<LoginForm />
<small>私人数字人空间 · 不开放公开注册</small>
</section>
</main>
);
+6 -5
View File
@@ -1,3 +1,4 @@
/** 数字人登录 BFF,令牌仅写入 HttpOnly Cookie,不返回浏览器。 */
import { setSessionToken } from "@/lib/session";
import { BackendError, backendRequest } from "@/lib/backend";
import type { LoginResponse } from "@/lib/types";
@@ -5,13 +6,13 @@ import type { LoginResponse } from "@/lib/types";
export async function POST(request: Request) {
try {
const body = await request.json();
const result = await backendRequest<LoginResponse>("/auth/employee/login", {
const result = await backendRequest<LoginResponse>("/auth/digital/login", {
method: "POST",
body: JSON.stringify({
username: body.username,
password: body.password
password: body.password,
}),
token: ""
token: "",
});
await setSessionToken(result.token);
@@ -21,8 +22,8 @@ export async function POST(request: Request) {
data: {
user: result.user,
tokenType: result.tokenType,
expiresIn: result.expiresIn
}
expiresIn: result.expiresIn,
},
});
} catch (error) {
const status = error instanceof BackendError ? error.status : 500;
+94
View File
@@ -0,0 +1,94 @@
/** 数字人 BFF:HttpOnly 会话只在服务端转发,白名单阻止前台访问实验管理。 */
import { getBackendBaseUrl } from "@/lib/backend";
import { getSessionToken } from "@/lib/session";
/** 限制 BFF 接收体积,后端校验之前也不会无界缓存请求。 */
async function readBody(request: Request) {
if (!request.body) return undefined;
const reader = request.body.getReader(),
decoder = new TextDecoder();
let bytes = 0,
body = "";
try {
for (;;) {
const { value, done } = await reader.read();
if (done) break;
bytes += value.byteLength;
if (bytes > 65536) {
await reader.cancel();
throw new RangeError("请求内容过长");
}
body += decoder.decode(value, { stream: true });
}
return body + decoder.decode();
} finally {
reader.releaseLock();
}
}
/** 私有会话不缓存;取消下游请求会中断上游 SSE。 */
async function proxy(
request: Request,
context: { params: Promise<{ path: string[] }> },
) {
const { path } = await context.params;
const joined = path.join("/");
if (
!/^(bootstrap|profile|exposure|conversations(?:\/[a-zA-Z0-9_-]+(?:\/(?:stream|stop|branch|messages\/[a-zA-Z0-9_-]+))?)?)$/.test(
joined,
)
)
return Response.json({ message: "接口不存在" }, { status: 404 });
if (request.method !== "GET") {
const origin = request.headers.get("origin");
try {
if (!origin || new URL(origin).host !== request.headers.get("host"))
return Response.json({ message: "请求来源不合法" }, { status: 403 });
} catch {
return Response.json({ message: "请求来源不合法" }, { status: 403 });
}
}
const token = await getSessionToken();
if (!token) return Response.json({ message: "请先登录" }, { status: 401 });
const headers = new Headers({
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
});
try {
const response = await fetch(
`${getBackendBaseUrl().replace(/\/$/, "")}/digital/${joined}`,
{
method: request.method,
headers,
body: request.method === "GET" ? undefined : await readBody(request),
cache: "no-store",
signal: request.signal,
},
);
return new Response(response.body, {
status: response.status,
headers: {
"Content-Type":
response.headers.get("content-type") ?? "application/json",
"Cache-Control": "no-store, no-transform",
"X-Accel-Buffering": "no",
},
});
} catch (error) {
if (error instanceof RangeError)
return Response.json({ message: error.message }, { status: 413 });
return Response.json(
{ message: "连接暂时不可用,请稍后重试" },
{ status: 502 },
);
}
}
/** 转发前台读取。 */
export const GET = proxy;
/** 转发创建与生成。 */
export const POST = proxy;
/** 转发用户资料保存。 */
export const PUT = proxy;
/** 转发会话偏好和固定记忆。 */
export const PATCH = proxy;
/** 转发用户自己的会话删除。 */
export const DELETE = proxy;
+12
View File
@@ -0,0 +1,12 @@
/** 数字人前台首屏,服务端并行读取私有数据,避免浏览器加载瀑布。 */
import { requireBackendData } from "@/lib/backend";
import type { Bootstrap, Summary } from "@/lib/digital";
import { DigitalHome } from "@/components/digital-home";
/** 私有入口,后端鉴权失败时由统一助手返回登录。 */
export default async function DiscoverPage() {
const [initial, initialSummaries] = await Promise.all([
requireBackendData<Bootstrap>("/digital/bootstrap"),
requireBackendData<Summary[]>("/digital/conversations"),
]);
return <DigitalHome initial={initial} initialSummaries={initialSummaries} />;
}
+20 -10
View File
@@ -1,32 +1,39 @@
/** 站点元信息与视口配置,保留系统缩放以支持可访问性。 */
import type { Metadata, Viewport } from "next";
import { connection } from "next/server";
import { getAppEnvLabel } from "@/lib/environment";
import "./globals.css";
/** 星港品牌与安装元数据。 */
export const metadata: Metadata = {
title: "员工工作台",
description: "门店员工日常任务、排班、公告和个人中心",
title: "星港 · 数字人空间",
description: "与有故事的人相遇,让每一段对话继续生长",
manifest: "/manifest.webmanifest",
icons: {
icon: "/icon.svg",
apple: "/icon.svg"
apple: "/icon.svg",
},
appleWebApp: {
capable: true,
title: "员工工作台",
statusBarStyle: "default"
}
title: "星港 · 数字人空间",
statusBarStyle: "default",
},
};
/** 允许缩放并覆盖安全区,由页面布局补齐系统边距。 */
export const viewport: Viewport = {
width: "device-width",
initialScale: 1,
maximumScale: 1,
themeColor: "#ffffff"
viewportFit: "cover",
themeColor: "#101010",
};
export default async function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
/** 全站根布局,私有数字人页面自行负责业务鉴权。 */
export default async function RootLayout({
children,
}: Readonly<{ children: React.ReactNode }>) {
await connection();
const environmentLabel = getAppEnvLabel();
@@ -34,7 +41,10 @@ export default async function RootLayout({ children }: Readonly<{ children: Reac
return (
<html lang="zh-CN">
<body>
<div className="environment-badge" aria-label={`当前环境:${environmentLabel}`}>
<div
className="environment-badge"
aria-label={`当前环境:${environmentLabel}`}
>
{environmentLabel}
</div>
{children}
+10 -9
View File
@@ -1,21 +1,22 @@
/** 数字人空间 PWA 元数据,保留原有安装机制。 */
import type { MetadataRoute } from "next";
export default function manifest(): MetadataRoute.Manifest {
return {
name: "role-user 员工工作台",
short_name: "员工工作台",
description: "门店员工任务、公告、排班和个人中心",
start_url: "/dashboard",
name: "星港 · 数字人空间",
short_name: "星港",
description: "你的私人数字人空间",
start_url: "/discover",
scope: "/",
display: "standalone",
background_color: "#f4f4f6",
theme_color: "#ffffff",
background_color: "#141218",
theme_color: "#141218",
icons: [
{
src: "/icon.svg",
sizes: "any",
type: "image/svg+xml"
}
]
type: "image/svg+xml",
},
],
};
}
+3 -2
View File
@@ -1,5 +1,6 @@
/** 默认入口进入数字人空间,登录由目标页统一校验。 */
import { redirect } from "next/navigation";
/** 私有站点首页。 */
export default function HomePage() {
redirect("/dashboard");
redirect("/discover");
}