Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fb8be9c460 | |||
| ae2b58e0b8 | |||
| 4638c72680 | |||
| 1f11112e04 | |||
| cac56740cc | |||
| f75a9abecc | |||
| afc474b0c4 | |||
| a963bec3f3 | |||
| 81079c7364 | |||
| 4079397625 | |||
| 9b2f7fc7cf | |||
| 981eab9dcf | |||
| d0442f5bb9 |
@@ -132,3 +132,21 @@ pnpm start:prod
|
|||||||
标签页图标采用 `public/stellar-icon-v2.svg`,同时提供多尺寸 `favicon.ico`、180/192/512 PNG,覆盖浏览器、Apple 主屏幕和 PWA。版本化资源路径用于刷新旧图标缓存。
|
标签页图标采用 `public/stellar-icon-v2.svg`,同时提供多尺寸 `favicon.ico`、180/192/512 PNG,覆盖浏览器、Apple 主屏幕和 PWA。版本化资源路径用于刷新旧图标缓存。
|
||||||
|
|
||||||
通用实验消费者可经 `GET /api/digital/decisions/:key` 读取本人类型决策的 `data.value`,沿用 HttpOnly 会话与不缓存策略。类型、测试方案和用户分组全部在后台管理;新类型需要应用真实使用返回值并完成后台接入验收后才可启动。
|
通用实验消费者可经 `GET /api/digital/decisions/:key` 读取本人类型决策的 `data.value`,沿用 HttpOnly 会话与不缓存策略。类型、测试方案和用户分组全部在后台管理;新类型需要应用真实使用返回值并完成后台接入验收后才可启动。
|
||||||
|
|
||||||
|
`src/components/conversation-row.tsx` 提供会话列表横滑删除、桌面删除入口与模态确认。灵感回复读取本人会话上下文,选择后只加入草稿,原草稿保留;回车发送覆盖 H5/Web,支持中文输入法、Shift+Enter 与手机换行按钮。
|
||||||
|
|
||||||
|
内部滚动区域统一隐藏原生轨道,保留触摸/键盘滚动,设定表单关闭横向溢出。风格控制提示使用只读 `GET /api/digital/conversations/:id/style-policy`,打开设定不会写入实验分组、决策或曝光。
|
||||||
|
|
||||||
|
实验决策支持 A~Z 动态组;未入组时组别为 null。界面消费 `decision.value`(`new` / `true` 为新版),组名仅用于归因。决策过期使用服务端的 `fallbackValue`,不将无实验用户伪装成 A 组;旧服务未返回value时保留既有兼容路径。
|
||||||
|
|
||||||
|
## 登录跳转回归
|
||||||
|
|
||||||
|
- `src/lib/auth-redirect.ts`:登出与会话过期后使用站内相对 `Location`,避免跳到 Next.js 内部监听地址 `0.0.0.0:3210`;仅允许站内返回路径。
|
||||||
|
- `tests/auth-redirect.test.mjs`:覆盖正式域名/本地地址的登录页跳转、站内参数和外部地址拦截。
|
||||||
|
- 执行:`node --experimental-strip-types --test tests/auth-redirect.test.mjs`(Node.js 22)。
|
||||||
|
|
||||||
|
聊天滚动将“跟随最新”与当前底部距离分开:停在底部时,流式回复、结束后操作栏及软键盘导致的尺寸变化均继续贴底;主动上翻才暂停跟随,点击“回到最新”重新启用。等待首段正文仅显示思考点,收到文字后才显示打字光标,结束时移除生成提示。
|
||||||
|
|
||||||
|
聊天输入区“继续”让 NPC 基于上一段内容继续说话,不发送或清空输入框草稿,也不插入用户气泡;可连续点击生成多段,生成时沿用停止按钮。角色尚无可接续内容时按钮不可用。
|
||||||
|
|
||||||
|
NPC 尚未出字就停止或失败时,“继续”仍可接续此前已完成的角色内容,输入草稿保持不变。
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
/** 清理会话后返回当前站点,保持反向代理域名与本地访问地址。 */
|
||||||
|
import { NextRequest } from "next/server";
|
||||||
|
import { logoutRedirect } from "@/lib/auth-redirect";
|
||||||
|
|
||||||
import { clearSessionToken } from "@/lib/session";
|
import { clearSessionToken } from "@/lib/session";
|
||||||
|
|
||||||
|
/** 客户端主动退出时仅清理 Cookie,由页面处理导航。 */
|
||||||
export async function POST() {
|
export async function POST() {
|
||||||
await clearSessionToken();
|
await clearSessionToken();
|
||||||
return Response.json({ success: true, data: null });
|
return Response.json({ success: true, data: null });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 会话过期或链接退出后跳转站内页面,不使用服务端监听地址。 */
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
await clearSessionToken();
|
await clearSessionToken();
|
||||||
const next = request.nextUrl.searchParams.get("next") || "/login";
|
return logoutRedirect(request.nextUrl.searchParams.get("next"));
|
||||||
|
|
||||||
return NextResponse.redirect(new URL(next, request.url));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,13 +33,19 @@ async function proxy(
|
|||||||
const { path } = await context.params;
|
const { path } = await context.params;
|
||||||
const joined = path.join("/");
|
const joined = path.join("/");
|
||||||
if (
|
if (
|
||||||
!/^(bootstrap|profile|exposure|decisions\/[a-zA-Z0-9_-]+|conversations(?:\/[a-zA-Z0-9_-]+(?:\/(?:stream|stop|branch|messages\/[a-zA-Z0-9_-]+))?)?)$/.test(
|
!/^(bootstrap|profile|exposure|decisions\/[a-zA-Z0-9_-]+|conversations(?:\/[a-zA-Z0-9_-]+(?:\/(?:stream|stop|branch|suggestions|style-policy|messages\/[a-zA-Z0-9_-]+))?)?)$/.test(
|
||||||
joined,
|
joined,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return Response.json({ message: "接口不存在" }, { status: 404 });
|
return Response.json({ message: "接口不存在" }, { status: 404 });
|
||||||
if (joined.startsWith("decisions/") && request.method !== "GET")
|
if (
|
||||||
return Response.json({ message: "仅支持读取决策" }, {status:405,headers:{Allow:"GET"}});
|
(joined.startsWith("decisions/") || joined.endsWith("/style-policy")) &&
|
||||||
|
request.method !== "GET"
|
||||||
|
)
|
||||||
|
return Response.json(
|
||||||
|
{ message: "仅支持读取决策" },
|
||||||
|
{ status: 405, headers: { Allow: "GET" } },
|
||||||
|
);
|
||||||
if (request.method !== "GET") {
|
if (request.method !== "GET") {
|
||||||
const origin = request.headers.get("origin");
|
const origin = request.headers.get("origin");
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -0,0 +1,209 @@
|
|||||||
|
"use client";
|
||||||
|
/** 会话列表的水平滑动操作与删除确认;纵向滑动继续交给页面滚动。 */
|
||||||
|
import { useEffect, useId, useRef, useState, type PointerEvent } from "react";
|
||||||
|
import { LoaderCircle, Trash2 } from "lucide-react";
|
||||||
|
import type { Card, Summary } from "@/lib/digital";
|
||||||
|
|
||||||
|
/** 一次只展开一条会话;拖动不会触发进入会话,桌面和键盘保留显式删除入口。 */
|
||||||
|
export function ConversationRow({
|
||||||
|
summary,
|
||||||
|
card,
|
||||||
|
revealed,
|
||||||
|
disabled,
|
||||||
|
onReveal,
|
||||||
|
onOpen,
|
||||||
|
onDelete,
|
||||||
|
}: {
|
||||||
|
summary: Summary;
|
||||||
|
card?: Card;
|
||||||
|
revealed: boolean;
|
||||||
|
disabled: boolean;
|
||||||
|
onReveal: (id: string | null) => void;
|
||||||
|
onOpen: () => void;
|
||||||
|
onDelete: () => void;
|
||||||
|
}) {
|
||||||
|
const [drag, setDrag] = useState<number | null>(null);
|
||||||
|
const gesture = useRef<{
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
base: number;
|
||||||
|
offset: number;
|
||||||
|
axis: "pending" | "x" | "y";
|
||||||
|
} | null>(null),
|
||||||
|
suppressClick = useRef(false);
|
||||||
|
const distance = 88;
|
||||||
|
function start(e: PointerEvent<HTMLButtonElement>) {
|
||||||
|
if (disabled || e.button !== 0) return;
|
||||||
|
suppressClick.current = false;
|
||||||
|
gesture.current = {
|
||||||
|
x: e.clientX,
|
||||||
|
y: e.clientY,
|
||||||
|
base: revealed ? -distance : 0,
|
||||||
|
offset: revealed ? -distance : 0,
|
||||||
|
axis: "pending",
|
||||||
|
};
|
||||||
|
e.currentTarget.setPointerCapture(e.pointerId);
|
||||||
|
}
|
||||||
|
function move(e: PointerEvent<HTMLButtonElement>) {
|
||||||
|
const g = gesture.current;
|
||||||
|
if (!g) return;
|
||||||
|
const dx = e.clientX - g.x,
|
||||||
|
dy = e.clientY - g.y;
|
||||||
|
if (g.axis === "pending" && Math.max(Math.abs(dx), Math.abs(dy)) > 8) {
|
||||||
|
g.axis = Math.abs(dx) > Math.abs(dy) * 1.2 ? "x" : "y";
|
||||||
|
if (g.axis === "x") onReveal(summary.id);
|
||||||
|
}
|
||||||
|
if (g.axis !== "x") return;
|
||||||
|
g.offset = Math.max(-distance, Math.min(0, g.base + dx));
|
||||||
|
setDrag(g.offset);
|
||||||
|
}
|
||||||
|
function finish(e: PointerEvent<HTMLButtonElement>, cancelled = false) {
|
||||||
|
const g = gesture.current;
|
||||||
|
if (!g) return;
|
||||||
|
gesture.current = null;
|
||||||
|
if (g.axis === "x") {
|
||||||
|
suppressClick.current = true;
|
||||||
|
onReveal(
|
||||||
|
(cancelled ? g.base : g.offset) < -distance / 2 ? summary.id : null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
setDrag(null);
|
||||||
|
if (e.currentTarget.hasPointerCapture(e.pointerId))
|
||||||
|
e.currentTarget.releasePointerCapture(e.pointerId);
|
||||||
|
}
|
||||||
|
const exposed = revealed || (drag !== null && drag < 0);
|
||||||
|
return (
|
||||||
|
<article className={`dh-conversation-row ${exposed ? "is-revealed" : ""}`}>
|
||||||
|
<button
|
||||||
|
className="dh-row-delete-swipe"
|
||||||
|
aria-label={`删除对话:${summary.title}`}
|
||||||
|
tabIndex={exposed ? 0 : -1}
|
||||||
|
aria-hidden={!exposed}
|
||||||
|
disabled={disabled}
|
||||||
|
onClick={onDelete}
|
||||||
|
>
|
||||||
|
<Trash2 size={18} />
|
||||||
|
<span>删除</span>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className={`dh-conversation-open ${drag !== null ? "is-dragging" : ""}`}
|
||||||
|
style={{
|
||||||
|
transform: `translateX(${drag ?? (revealed ? -distance : 0)}px)`,
|
||||||
|
}}
|
||||||
|
disabled={disabled}
|
||||||
|
onPointerDown={start}
|
||||||
|
onPointerMove={move}
|
||||||
|
onPointerUp={(e) => finish(e)}
|
||||||
|
onPointerCancel={(e) => finish(e, true)}
|
||||||
|
onLostPointerCapture={() => {
|
||||||
|
if (gesture.current) {
|
||||||
|
gesture.current = null;
|
||||||
|
setDrag(null);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onClick={(e) => {
|
||||||
|
if (suppressClick.current && e.detail !== 0) {
|
||||||
|
suppressClick.current = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (revealed) {
|
||||||
|
onReveal(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onOpen();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{/* 封面地址沿用服务端目录,不在小型服务器做图片转换。 */}
|
||||||
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||||
|
<img src={card?.cover || "/icon.svg"} alt="" draggable={false} />
|
||||||
|
<div>
|
||||||
|
<h3>{summary.title}</h3>
|
||||||
|
<p>{card?.description || "继续这段私人对话"}</p>
|
||||||
|
</div>
|
||||||
|
<small>
|
||||||
|
{new Date(summary.updatedAt).toLocaleDateString("zh-CN", {
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
})}
|
||||||
|
</small>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="dh-row-delete-desktop"
|
||||||
|
aria-label={`删除对话:${summary.title}`}
|
||||||
|
disabled={disabled}
|
||||||
|
onClick={onDelete}
|
||||||
|
>
|
||||||
|
<Trash2 size={17} />
|
||||||
|
</button>
|
||||||
|
</article>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 原生模态对话框负责焦点约束,删除成功前保留原会话,失败可在原处重试。 */
|
||||||
|
export function ConversationDeleteDialog({
|
||||||
|
title,
|
||||||
|
busy,
|
||||||
|
error,
|
||||||
|
onCancel,
|
||||||
|
onConfirm,
|
||||||
|
}: {
|
||||||
|
title: string;
|
||||||
|
busy: boolean;
|
||||||
|
error: string;
|
||||||
|
onCancel: () => void;
|
||||||
|
onConfirm: () => void;
|
||||||
|
}) {
|
||||||
|
const ref = useRef<HTMLDialogElement>(null),
|
||||||
|
id = useId();
|
||||||
|
useEffect(() => {
|
||||||
|
const dialog = ref.current;
|
||||||
|
dialog?.showModal();
|
||||||
|
return () => dialog?.close();
|
||||||
|
}, []);
|
||||||
|
return (
|
||||||
|
<dialog
|
||||||
|
ref={ref}
|
||||||
|
className="dh-delete-dialog"
|
||||||
|
role="alertdialog"
|
||||||
|
aria-labelledby={id}
|
||||||
|
aria-describedby={`${id}-description`}
|
||||||
|
onCancel={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!busy) onCancel();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span className="dh-delete-symbol">
|
||||||
|
<Trash2 size={24} />
|
||||||
|
</span>
|
||||||
|
<h2 id={id}>删除这段对话?</h2>
|
||||||
|
<p id={`${id}-description`}>
|
||||||
|
「{title}」的对话记录将被永久删除,无法恢复。
|
||||||
|
</p>
|
||||||
|
{error && (
|
||||||
|
<p className="dh-delete-error" role="alert">
|
||||||
|
{error}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<div className="dh-delete-actions">
|
||||||
|
<button type="button" disabled={busy} onClick={onCancel}>
|
||||||
|
取消
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="is-danger"
|
||||||
|
disabled={busy}
|
||||||
|
onClick={onConfirm}
|
||||||
|
>
|
||||||
|
{busy ? (
|
||||||
|
<>
|
||||||
|
<LoaderCircle size={16} className="dh-spin" />
|
||||||
|
正在删除
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
"删除对话"
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
+420
-69
@@ -48,6 +48,7 @@ import {
|
|||||||
type Summary,
|
type Summary,
|
||||||
type Bootstrap,
|
type Bootstrap,
|
||||||
} from "@/lib/digital";
|
} from "@/lib/digital";
|
||||||
|
import { ConversationRow, ConversationDeleteDialog } from "./conversation-row";
|
||||||
import "./digital.css";
|
import "./digital.css";
|
||||||
type Tab = "discover" | "chats" | "library" | "me";
|
type Tab = "discover" | "chats" | "library" | "me";
|
||||||
/** 接收文本与视觉打字分离;减少动画设置下直接呈现完整文本。 */
|
/** 接收文本与视觉打字分离;减少动画设置下直接呈现完整文本。 */
|
||||||
@@ -89,6 +90,14 @@ function MessageText({
|
|||||||
message: Message;
|
message: Message;
|
||||||
animate: boolean;
|
animate: boolean;
|
||||||
}) {
|
}) {
|
||||||
|
if (message.status === "generating" && !message.content.trim())
|
||||||
|
return (
|
||||||
|
<span className="dh-thinking" role="status" aria-label="正在思考">
|
||||||
|
<i aria-hidden />
|
||||||
|
<i aria-hidden />
|
||||||
|
<i aria-hidden />
|
||||||
|
</span>
|
||||||
|
);
|
||||||
return (
|
return (
|
||||||
<div className="dh-message-text">
|
<div className="dh-message-text">
|
||||||
<TypedText
|
<TypedText
|
||||||
@@ -104,7 +113,7 @@ function MessageText({
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
/** 前台无分组按钮、调试开关或实验配置。 */
|
/** 前台管理个人会话偏好;后台方案仅展示覆盖提示,不开放实验操作。 */
|
||||||
export function DigitalHome({
|
export function DigitalHome({
|
||||||
initial,
|
initial,
|
||||||
initialSummaries,
|
initialSummaries,
|
||||||
@@ -125,6 +134,9 @@ export function DigitalHome({
|
|||||||
[opening, setOpening] = useState(false),
|
[opening, setOpening] = useState(false),
|
||||||
[error, setError] = useState(""),
|
[error, setError] = useState(""),
|
||||||
[notice, setNotice] = useState("");
|
[notice, setNotice] = useState("");
|
||||||
|
const [stylePolicy, setStylePolicy] = useState<
|
||||||
|
"loading" | "personal" | "managed" | "unknown"
|
||||||
|
>("loading");
|
||||||
const [showSettings, setShowSettings] = useState(false),
|
const [showSettings, setShowSettings] = useState(false),
|
||||||
[settings, setSettings] = useState<Settings | null>(null),
|
[settings, setSettings] = useState<Settings | null>(null),
|
||||||
[profile, setProfile] = useState<Profile | null>(initial.profile);
|
[profile, setProfile] = useState<Profile | null>(initial.profile);
|
||||||
@@ -139,6 +151,19 @@ export function DigitalHome({
|
|||||||
bookIds: string[];
|
bookIds: string[];
|
||||||
}>({ bookIds: [] });
|
}>({ bookIds: [] });
|
||||||
const selectionTrigger = useRef<HTMLElement | null>(null);
|
const selectionTrigger = useRef<HTMLElement | null>(null);
|
||||||
|
const [swipedConversation, setSwipedConversation] = useState<string | null>(
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
const [deleteTarget, setDeleteTarget] = useState<Summary | null>(null),
|
||||||
|
[deleting, setDeleting] = useState(false),
|
||||||
|
[deleteError, setDeleteError] = useState("");
|
||||||
|
const [ideasOpen, setIdeasOpen] = useState(false),
|
||||||
|
[ideasLoading, setIdeasLoading] = useState(false),
|
||||||
|
[ideas, setIdeas] = useState<string[]>([]),
|
||||||
|
[ideasError, setIdeasError] = useState("");
|
||||||
|
const ideasRequest = useRef<AbortController | null>(null),
|
||||||
|
imeComposing = useRef(false),
|
||||||
|
deleteLock = useRef(false);
|
||||||
const favoritePending = useRef(false);
|
const favoritePending = useRef(false);
|
||||||
const [activeMessage, setActiveMessage] = useState<string | null>(null),
|
const [activeMessage, setActiveMessage] = useState<string | null>(null),
|
||||||
[showJump, setShowJump] = useState(false),
|
[showJump, setShowJump] = useState(false),
|
||||||
@@ -149,13 +174,48 @@ export function DigitalHome({
|
|||||||
viewport = useRef<HTMLDivElement>(null),
|
viewport = useRef<HTMLDivElement>(null),
|
||||||
messagesElement = useRef<HTMLDivElement>(null),
|
messagesElement = useRef<HTMLDivElement>(null),
|
||||||
textarea = useRef<HTMLTextAreaElement>(null),
|
textarea = useRef<HTMLTextAreaElement>(null),
|
||||||
|
// 跟随是用户意图,内容增高导致暂时离底不能自动退出跟随。
|
||||||
atBottom = useRef(true),
|
atBottom = useRef(true),
|
||||||
|
touchY = useRef<number | null>(null),
|
||||||
|
draggingScroll = useRef(false),
|
||||||
currentId = useRef<string | null>(null),
|
currentId = useRef<string | null>(null),
|
||||||
openSequence = useRef(0);
|
openSequence = useRef(0);
|
||||||
const cards = boot?.catalog.filter((c) => c.kind === "character") ?? [],
|
const cards = boot?.catalog.filter((c) => c.kind === "character") ?? [],
|
||||||
books = boot?.catalog.filter((c) => c.kind === "worldbook") ?? [],
|
books = boot?.catalog.filter((c) => c.kind === "worldbook") ?? [],
|
||||||
presets = boot?.catalog.filter((c) => c.kind === "preset") ?? [];
|
presets = boot?.catalog.filter((c) => c.kind === "preset") ?? [];
|
||||||
const conversationId = conversation?.id;
|
const conversationId = conversation?.id;
|
||||||
|
// 尚未产生正文的失败/停止不算新台词,继续时接回上一段;真实用户消息仍是边界。
|
||||||
|
const lastReply = conversation?.messages.findLast(
|
||||||
|
(m) =>
|
||||||
|
!(
|
||||||
|
m.role === "assistant" &&
|
||||||
|
["stopped", "failed"].includes(m.status) &&
|
||||||
|
!m.content.trim()
|
||||||
|
),
|
||||||
|
);
|
||||||
|
const canContinue =
|
||||||
|
lastReply?.role === "assistant" &&
|
||||||
|
["complete", "stopped"].includes(lastReply.status) &&
|
||||||
|
!!lastReply.content.trim();
|
||||||
|
// 状态提示走只读接口,打开设定不应写入实验分组、决策或曝光。
|
||||||
|
useEffect(() => {
|
||||||
|
if (!showSettings || !conversationId) return;
|
||||||
|
const controller = new AbortController();
|
||||||
|
digital<{ managed: boolean }>(
|
||||||
|
`conversations/${conversationId}/style-policy`,
|
||||||
|
"GET",
|
||||||
|
undefined,
|
||||||
|
controller.signal,
|
||||||
|
)
|
||||||
|
.then((policy) => {
|
||||||
|
if (!controller.signal.aborted)
|
||||||
|
setStylePolicy(policy.managed ? "managed" : "personal");
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (!controller.signal.aborted) setStylePolicy("unknown");
|
||||||
|
});
|
||||||
|
return () => controller.abort();
|
||||||
|
}, [showSettings, conversationId]);
|
||||||
const decisionExpiresAt = boot?.decision.expiresAt,
|
const decisionExpiresAt = boot?.decision.expiresAt,
|
||||||
decisionServerNow = boot?.serverNow;
|
decisionServerNow = boot?.serverNow;
|
||||||
const currentCard = cards.find((c) => c.id === conversation?.characterId);
|
const currentCard = cards.find((c) => c.id === conversation?.characterId);
|
||||||
@@ -212,6 +272,7 @@ export function DigitalHome({
|
|||||||
decision: {
|
decision: {
|
||||||
...current.decision,
|
...current.decision,
|
||||||
variant: current.decision.fallbackVariant,
|
variant: current.decision.fallbackVariant,
|
||||||
|
value: current.decision.fallbackValue,
|
||||||
decisionId: null,
|
decisionId: null,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -227,17 +288,34 @@ export function DigitalHome({
|
|||||||
return () => clearTimeout(timer);
|
return () => clearTimeout(timer);
|
||||||
}, [notice]);
|
}, [notice]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const el = messagesElement.current;
|
const el = messagesElement.current,
|
||||||
if (!el) return;
|
view = viewport.current;
|
||||||
const observer = new ResizeObserver(() => {
|
if (!el || !view) return;
|
||||||
if (atBottom.current)
|
let frame = 0;
|
||||||
viewport.current?.scrollTo({
|
const follow = () => {
|
||||||
top: viewport.current.scrollHeight,
|
cancelAnimationFrame(frame);
|
||||||
behavior: "instant",
|
frame = requestAnimationFrame(() => {
|
||||||
});
|
if (!atBottom.current) return;
|
||||||
});
|
view.scrollTo({ top: view.scrollHeight, behavior: "instant" });
|
||||||
|
setShowJump(false);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
// 同时观察内容与可视区:视觉打字、结束后的操作栏、输入框和软键盘都会改变底部位置。
|
||||||
|
const observer = new ResizeObserver(follow);
|
||||||
observer.observe(el);
|
observer.observe(el);
|
||||||
return () => observer.disconnect();
|
observer.observe(view);
|
||||||
|
const endDrag = () => {
|
||||||
|
draggingScroll.current = false;
|
||||||
|
};
|
||||||
|
window.addEventListener("pointerup", endDrag);
|
||||||
|
window.addEventListener("pointercancel", endDrag);
|
||||||
|
follow();
|
||||||
|
return () => {
|
||||||
|
observer.disconnect();
|
||||||
|
cancelAnimationFrame(frame);
|
||||||
|
window.removeEventListener("pointerup", endDrag);
|
||||||
|
window.removeEventListener("pointercancel", endDrag);
|
||||||
|
};
|
||||||
}, [conversationId]);
|
}, [conversationId]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!conversationId) return;
|
if (!conversationId) return;
|
||||||
@@ -362,6 +440,9 @@ export function DigitalHome({
|
|||||||
};
|
};
|
||||||
}, [busy, conversationId]);
|
}, [busy, conversationId]);
|
||||||
async function openConversation(id: string) {
|
async function openConversation(id: string) {
|
||||||
|
closeIdeas();
|
||||||
|
imeComposing.current = false;
|
||||||
|
setSwipedConversation(null);
|
||||||
if (streamingId.current && streamingId.current !== id) {
|
if (streamingId.current && streamingId.current !== id) {
|
||||||
setNotice("请先停止当前回复,再切换会话");
|
setNotice("请先停止当前回复,再切换会话");
|
||||||
return;
|
return;
|
||||||
@@ -379,6 +460,7 @@ export function DigitalHome({
|
|||||||
setActiveMessage(null);
|
setActiveMessage(null);
|
||||||
setShowMenu(false);
|
setShowMenu(false);
|
||||||
atBottom.current = true;
|
atBottom.current = true;
|
||||||
|
setShowJump(false);
|
||||||
try {
|
try {
|
||||||
setDraft(sessionStorage.getItem(`dh-draft:${id}`) ?? "");
|
setDraft(sessionStorage.getItem(`dh-draft:${id}`) ?? "");
|
||||||
} catch {
|
} catch {
|
||||||
@@ -428,7 +510,7 @@ export function DigitalHome({
|
|||||||
favoritePending.current = false;
|
favoritePending.current = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async function send(mode: "send" | "regenerate" = "send") {
|
async function send(mode: "send" | "regenerate" | "continue" = "send") {
|
||||||
if (
|
if (
|
||||||
!conversation ||
|
!conversation ||
|
||||||
busy ||
|
busy ||
|
||||||
@@ -436,16 +518,21 @@ export function DigitalHome({
|
|||||||
(mode === "send" && !draft.trim())
|
(mode === "send" && !draft.trim())
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
closeIdeas();
|
||||||
const id = conversation.id,
|
const id = conversation.id,
|
||||||
content = draft.trim(),
|
content = mode === "send" ? draft.trim() : "",
|
||||||
requestId = crypto.randomUUID();
|
requestId = crypto.randomUUID();
|
||||||
let assistantId = `pending-${requestId}`;
|
let assistantId = `pending-${requestId}`;
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
setError("");
|
setError("");
|
||||||
setNotice("");
|
setNotice("");
|
||||||
atBottom.current = true;
|
atBottom.current = true;
|
||||||
|
setShowJump(false);
|
||||||
streamingId.current = id;
|
streamingId.current = id;
|
||||||
if (mode === "send") setDraft("");
|
if (mode === "send") {
|
||||||
|
setDraft("");
|
||||||
|
if (textarea.current) textarea.current.style.height = "auto";
|
||||||
|
}
|
||||||
const local: Message[] = [
|
const local: Message[] = [
|
||||||
...(mode === "send"
|
...(mode === "send"
|
||||||
? [
|
? [
|
||||||
@@ -570,11 +657,12 @@ export function DigitalHome({
|
|||||||
if (
|
if (
|
||||||
event.key === "Enter" &&
|
event.key === "Enter" &&
|
||||||
!event.shiftKey &&
|
!event.shiftKey &&
|
||||||
|
!imeComposing.current &&
|
||||||
!event.nativeEvent.isComposing &&
|
!event.nativeEvent.isComposing &&
|
||||||
window.matchMedia("(min-width: 768px)").matches
|
event.nativeEvent.keyCode !== 229
|
||||||
) {
|
) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
void send();
|
if (!event.repeat) void send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async function pin(message: Message) {
|
async function pin(message: Message) {
|
||||||
@@ -615,21 +703,133 @@ export function DigitalHome({
|
|||||||
reportError(e);
|
reportError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async function removeConversation() {
|
function requestDelete(target: Summary) {
|
||||||
if (
|
if (target.id === conversation?.id && busy) {
|
||||||
!conversation ||
|
setNotice("请先停止当前回复,再删除对话");
|
||||||
busy ||
|
|
||||||
!window.confirm("删除此会话和全部消息?此操作无法撤销。")
|
|
||||||
)
|
|
||||||
return;
|
return;
|
||||||
try {
|
|
||||||
await digital(`conversations/${conversation.id}`, "DELETE");
|
|
||||||
setConversation(null);
|
|
||||||
currentId.current = null;
|
|
||||||
await refreshSummaries();
|
|
||||||
} catch (e) {
|
|
||||||
reportError(e);
|
|
||||||
}
|
}
|
||||||
|
setDeleteError("");
|
||||||
|
setDeleteTarget(target);
|
||||||
|
setShowMenu(false);
|
||||||
|
}
|
||||||
|
function removeConversation() {
|
||||||
|
if (conversation)
|
||||||
|
requestDelete({
|
||||||
|
id: conversation.id,
|
||||||
|
title: conversation.title,
|
||||||
|
characterId: conversation.characterId,
|
||||||
|
updatedAt: "",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async function confirmDelete() {
|
||||||
|
if (!deleteTarget || deleteLock.current) return;
|
||||||
|
const id = deleteTarget.id;
|
||||||
|
deleteLock.current = true;
|
||||||
|
setDeleting(true);
|
||||||
|
setDeleteError("");
|
||||||
|
try {
|
||||||
|
await digital(`conversations/${id}`, "DELETE");
|
||||||
|
setSummaries((current) => current.filter((s) => s.id !== id));
|
||||||
|
setSwipedConversation(null);
|
||||||
|
if (currentId.current === id) {
|
||||||
|
closeIdeas();
|
||||||
|
setConversation(null);
|
||||||
|
currentId.current = null;
|
||||||
|
setDraft("");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
sessionStorage.removeItem(`dh-draft:${id}`);
|
||||||
|
} catch {}
|
||||||
|
setDeleteTarget(null);
|
||||||
|
setNotice("对话已删除");
|
||||||
|
} catch (e) {
|
||||||
|
setDeleteError(e instanceof Error ? e.message : "删除失败,请重试");
|
||||||
|
} finally {
|
||||||
|
deleteLock.current = false;
|
||||||
|
setDeleting(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/** 灵感建议只填入草稿,旧请求和已离开的会话不能覆盖当前输入。 */
|
||||||
|
function closeIdeas() {
|
||||||
|
ideasRequest.current?.abort();
|
||||||
|
ideasRequest.current = null;
|
||||||
|
setIdeasOpen(false);
|
||||||
|
setIdeasLoading(false);
|
||||||
|
setIdeasError("");
|
||||||
|
setIdeas([]);
|
||||||
|
}
|
||||||
|
async function generateIdeas() {
|
||||||
|
if (!conversation || busy || streamingId.current || ideasRequest.current)
|
||||||
|
return;
|
||||||
|
const id = conversation.id,
|
||||||
|
anchor = conversation.messages.at(-1)?.id ?? null,
|
||||||
|
controller = new AbortController();
|
||||||
|
ideasRequest.current = controller;
|
||||||
|
setIdeasOpen(true);
|
||||||
|
setIdeasLoading(true);
|
||||||
|
setIdeasError("");
|
||||||
|
setIdeas([]);
|
||||||
|
try {
|
||||||
|
const result = await digital<{
|
||||||
|
suggestions: string[];
|
||||||
|
anchorId: string | null;
|
||||||
|
}>(
|
||||||
|
`conversations/${id}/suggestions`,
|
||||||
|
"POST",
|
||||||
|
{ draft: draft.slice(0, 1000) },
|
||||||
|
controller.signal,
|
||||||
|
);
|
||||||
|
if (ideasRequest.current !== controller || currentId.current !== id)
|
||||||
|
return;
|
||||||
|
if (result.anchorId !== anchor) {
|
||||||
|
setIdeasError("对话已更新,请重新打开会话后再试");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setIdeas(result.suggestions);
|
||||||
|
} catch (e) {
|
||||||
|
if (!controller.signal.aborted && currentId.current === id)
|
||||||
|
setIdeasError(e instanceof Error ? e.message : "灵感生成失败,请重试");
|
||||||
|
} finally {
|
||||||
|
if (ideasRequest.current === controller) {
|
||||||
|
ideasRequest.current = null;
|
||||||
|
setIdeasLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function chooseIdea(value: string) {
|
||||||
|
const next = draft
|
||||||
|
? `${draft}${draft.endsWith("\n") ? "" : "\n"}${value}`
|
||||||
|
: value;
|
||||||
|
if (next.length > 4000) {
|
||||||
|
setIdeasError("草稿已接近字数上限,请先缩短内容");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setDraft(next);
|
||||||
|
closeIdeas();
|
||||||
|
setNotice("已加入草稿,可修改后发送");
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
const input = textarea.current;
|
||||||
|
if (input) {
|
||||||
|
input.focus();
|
||||||
|
input.style.height = "auto";
|
||||||
|
input.style.height = `${Math.min(input.scrollHeight, 160)}px`;
|
||||||
|
input.setSelectionRange(next.length, next.length);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function insertLineBreak() {
|
||||||
|
const input = textarea.current;
|
||||||
|
if (!input || draft.length >= 4000) return;
|
||||||
|
const start = input.selectionStart,
|
||||||
|
end = input.selectionEnd,
|
||||||
|
next = draft.slice(0, start) + "\n" + draft.slice(end);
|
||||||
|
setDraft(next);
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
input.focus();
|
||||||
|
input.setSelectionRange(start + 1, start + 1);
|
||||||
|
input.style.height = "auto";
|
||||||
|
input.style.height = `${Math.min(input.scrollHeight, 160)}px`;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
/** 抽屉仅编辑临时值;确认后回填设定,取消不改动已有选择。 */
|
/** 抽屉仅编辑临时值;确认后回填设定,取消不改动已有选择。 */
|
||||||
function openSelection(kind: "style" | "books") {
|
function openSelection(kind: "style" | "books") {
|
||||||
@@ -658,7 +858,11 @@ export function DigitalHome({
|
|||||||
await digital(`conversations/${conversation.id}`, "PATCH", { settings });
|
await digital(`conversations/${conversation.id}`, "PATCH", { settings });
|
||||||
setConversation((c) => (c ? { ...c, settings } : c));
|
setConversation((c) => (c ? { ...c, settings } : c));
|
||||||
setShowSettings(false);
|
setShowSettings(false);
|
||||||
setNotice("对话设定已保存,下次回复生效");
|
setNotice(
|
||||||
|
stylePolicy === "managed"
|
||||||
|
? "设定已保存;当前叙事风格仍由统一方案控制"
|
||||||
|
: "对话设定已保存,下次回复生效",
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reportError(e);
|
reportError(e);
|
||||||
}
|
}
|
||||||
@@ -705,6 +909,8 @@ export function DigitalHome({
|
|||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
}
|
}
|
||||||
function switchTab(value: Tab) {
|
function switchTab(value: Tab) {
|
||||||
|
closeIdeas();
|
||||||
|
setSwipedConversation(null);
|
||||||
if (conversation && busy) {
|
if (conversation && busy) {
|
||||||
setNotice("请先停止当前回复,再离开会话");
|
setNotice("请先停止当前回复,再离开会话");
|
||||||
return;
|
return;
|
||||||
@@ -736,7 +942,15 @@ export function DigitalHome({
|
|||||||
<legend className={inSheet ? "dh-visually-hidden" : undefined}>
|
<legend className={inSheet ? "dh-visually-hidden" : undefined}>
|
||||||
叙事风格
|
叙事风格
|
||||||
</legend>
|
</legend>
|
||||||
<p className="dh-style-help">为这段故事选一种氛围</p>
|
<p className="dh-style-help" role="status">
|
||||||
|
{stylePolicy === "loading"
|
||||||
|
? "正在确认风格设置…"
|
||||||
|
: stylePolicy === "managed"
|
||||||
|
? "当前使用统一叙事风格。可以保存个人偏好,统一风格解除后采用。"
|
||||||
|
: stylePolicy === "unknown"
|
||||||
|
? "暂时无法确认当前风格,可以先保存个人偏好。"
|
||||||
|
: "为这段故事选一种氛围,也可以跟随角色默认风格。"}
|
||||||
|
</p>
|
||||||
<div className="dh-style-options">
|
<div className="dh-style-options">
|
||||||
{[
|
{[
|
||||||
{
|
{
|
||||||
@@ -750,8 +964,7 @@ export function DigitalHome({
|
|||||||
{
|
{
|
||||||
id: value,
|
id: value,
|
||||||
name: "当前会话风格",
|
name: "当前会话风格",
|
||||||
description:
|
description: "这个风格暂不可用,回复时会采用可用的默认风格。",
|
||||||
"此风格暂不在可选列表中,可以保留或选择其他风格。",
|
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
: []),
|
: []),
|
||||||
@@ -818,7 +1031,18 @@ export function DigitalHome({
|
|||||||
))}
|
))}
|
||||||
</fieldset>
|
</fieldset>
|
||||||
);
|
);
|
||||||
const modern = boot?.decision.variant === "B";
|
useEffect(
|
||||||
|
() => () => {
|
||||||
|
ideasRequest.current?.abort();
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
// UI按消费者返回值决定,组名仅用于归因;C到Z同样可以返回新版UI。
|
||||||
|
// 缺少value时兼容尚未升级的服务响应。
|
||||||
|
const modern =
|
||||||
|
boot?.decision.value !== undefined
|
||||||
|
? boot.decision.value === "new" || boot.decision.value === true
|
||||||
|
: boot?.decision.variant === "B";
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`dh-app ${modern ? "dh-modern" : "dh-classic"} ${conversation ? "dh-in-chat" : ""}`}
|
className={`dh-app ${modern ? "dh-modern" : "dh-classic"} ${conversation ? "dh-in-chat" : ""}`}
|
||||||
@@ -909,6 +1133,7 @@ export function DigitalHome({
|
|||||||
aria-label="对话设定"
|
aria-label="对话设定"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setSettings(conversation.settings);
|
setSettings(conversation.settings);
|
||||||
|
setStylePolicy("loading");
|
||||||
setShowSettings(true);
|
setShowSettings(true);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -946,12 +1171,45 @@ export function DigitalHome({
|
|||||||
<div
|
<div
|
||||||
className="dh-messages"
|
className="dh-messages"
|
||||||
ref={viewport}
|
ref={viewport}
|
||||||
|
tabIndex={0}
|
||||||
|
aria-label="聊天消息"
|
||||||
|
onWheel={(event) => {
|
||||||
|
if (event.deltaY < 0 && event.currentTarget.scrollTop > 0)
|
||||||
|
atBottom.current = false;
|
||||||
|
}}
|
||||||
|
onTouchStart={(event) => {
|
||||||
|
touchY.current = event.touches[0]?.clientY ?? null;
|
||||||
|
}}
|
||||||
|
onTouchMove={(event) => {
|
||||||
|
const y = event.touches[0]?.clientY;
|
||||||
|
if (
|
||||||
|
y !== undefined &&
|
||||||
|
touchY.current !== null &&
|
||||||
|
y > touchY.current &&
|
||||||
|
event.currentTarget.scrollTop > 0
|
||||||
|
)
|
||||||
|
atBottom.current = false;
|
||||||
|
touchY.current = y ?? null;
|
||||||
|
}}
|
||||||
|
onTouchEnd={() => {
|
||||||
|
touchY.current = null;
|
||||||
|
}}
|
||||||
|
onPointerDown={(event) => {
|
||||||
|
draggingScroll.current =
|
||||||
|
event.pointerType === "mouse" &&
|
||||||
|
event.target === event.currentTarget;
|
||||||
|
}}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (["ArrowUp", "PageUp", "Home"].includes(event.key))
|
||||||
|
atBottom.current = false;
|
||||||
|
}}
|
||||||
onScroll={() => {
|
onScroll={() => {
|
||||||
const v = viewport.current;
|
const v = viewport.current;
|
||||||
if (v) {
|
if (v) {
|
||||||
atBottom.current =
|
const gap = v.scrollHeight - v.scrollTop - v.clientHeight;
|
||||||
v.scrollHeight - v.scrollTop - v.clientHeight < 110;
|
if (gap <= 2) atBottom.current = true;
|
||||||
setShowJump(!atBottom.current);
|
else if (draggingScroll.current) atBottom.current = false;
|
||||||
|
setShowJump(!atBottom.current && gap > 80);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -987,13 +1245,6 @@ export function DigitalHome({
|
|||||||
m.id === activeMessage && m.status === "generating"
|
m.id === activeMessage && m.status === "generating"
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
{!m.content && m.status === "generating" && (
|
|
||||||
<span className="dh-thinking">
|
|
||||||
<i />
|
|
||||||
<i />
|
|
||||||
<i />
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
{!busy && m.content && (
|
{!busy && m.content && (
|
||||||
<div className="dh-message-actions">
|
<div className="dh-message-actions">
|
||||||
@@ -1072,9 +1323,10 @@ export function DigitalHome({
|
|||||||
className="dh-jump"
|
className="dh-jump"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
atBottom.current = true;
|
atBottom.current = true;
|
||||||
|
setShowJump(false);
|
||||||
viewport.current?.scrollTo({
|
viewport.current?.scrollTo({
|
||||||
top: viewport.current.scrollHeight,
|
top: viewport.current.scrollHeight,
|
||||||
behavior: "smooth",
|
behavior: "instant",
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -1083,6 +1335,95 @@ export function DigitalHome({
|
|||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
<div className="dh-composer-wrap">
|
<div className="dh-composer-wrap">
|
||||||
|
{ideasOpen && (
|
||||||
|
<section
|
||||||
|
className="dh-reply-ideas"
|
||||||
|
aria-label="灵感回复"
|
||||||
|
aria-busy={ideasLoading}
|
||||||
|
>
|
||||||
|
<header>
|
||||||
|
<strong>
|
||||||
|
<Sparkles size={14} />
|
||||||
|
灵感回复
|
||||||
|
</strong>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="dh-icon"
|
||||||
|
aria-label="关闭灵感回复"
|
||||||
|
onClick={closeIdeas}
|
||||||
|
>
|
||||||
|
<X size={16} />
|
||||||
|
</button>
|
||||||
|
</header>
|
||||||
|
<p className="dh-ideas-note">
|
||||||
|
{draft
|
||||||
|
? "选择后追加到现有草稿,不会自动发送。"
|
||||||
|
: "根据当前对话生成,选择后可编辑再发送。"}
|
||||||
|
</p>
|
||||||
|
{ideasLoading ? (
|
||||||
|
<p className="dh-ideas-loading" role="status">
|
||||||
|
<LoaderCircle size={16} className="dh-spin" />
|
||||||
|
正在寻找合适的接话方式…
|
||||||
|
</p>
|
||||||
|
) : ideasError ? (
|
||||||
|
<p className="dh-ideas-error" role="alert">
|
||||||
|
{ideasError}
|
||||||
|
</p>
|
||||||
|
) : (
|
||||||
|
<div className="dh-ideas-list">
|
||||||
|
{ideas.map((idea, i) => (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
key={i}
|
||||||
|
onClick={() => chooseIdea(idea)}
|
||||||
|
>
|
||||||
|
<small>{i + 1}</small>
|
||||||
|
<span>{idea}</span>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{!ideasLoading && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="dh-ideas-refresh"
|
||||||
|
onClick={generateIdeas}
|
||||||
|
>
|
||||||
|
<RefreshCw size={13} />
|
||||||
|
{ideasError ? "重新生成" : "换一组"}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
|
<div className="dh-compose-tools">
|
||||||
|
<div className="dh-compose-actions">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={busy || ideasLoading}
|
||||||
|
onClick={generateIdeas}
|
||||||
|
>
|
||||||
|
<Sparkles size={14} />
|
||||||
|
{ideasLoading ? "灵感生成中…" : "灵感回复"}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
aria-label="让角色继续"
|
||||||
|
title="不发送输入框里的草稿,让角色接着说"
|
||||||
|
disabled={busy || ideasLoading || !canContinue}
|
||||||
|
onClick={() => send("continue")}
|
||||||
|
>
|
||||||
|
<ChevronRight size={14} />
|
||||||
|
继续
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="dh-mobile-linebreak"
|
||||||
|
onClick={insertLineBreak}
|
||||||
|
>
|
||||||
|
换行
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<div className="dh-composer">
|
<div className="dh-composer">
|
||||||
<textarea
|
<textarea
|
||||||
ref={textarea}
|
ref={textarea}
|
||||||
@@ -1096,6 +1437,13 @@ export function DigitalHome({
|
|||||||
e.target.style.height = "auto";
|
e.target.style.height = "auto";
|
||||||
e.target.style.height = `${Math.min(e.target.scrollHeight, 160)}px`;
|
e.target.style.height = `${Math.min(e.target.scrollHeight, 160)}px`;
|
||||||
}}
|
}}
|
||||||
|
enterKeyHint="send"
|
||||||
|
onCompositionStart={() => {
|
||||||
|
imeComposing.current = true;
|
||||||
|
}}
|
||||||
|
onCompositionEnd={() => {
|
||||||
|
imeComposing.current = false;
|
||||||
|
}}
|
||||||
onKeyDown={onKey}
|
onKeyDown={onKey}
|
||||||
/>
|
/>
|
||||||
{busy ? (
|
{busy ? (
|
||||||
@@ -1298,30 +1646,22 @@ export function DigitalHome({
|
|||||||
开启新故事
|
开启新故事
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
{summaries.length > 0 && (
|
||||||
|
<p className="dh-swipe-hint">向左滑动对话可删除</p>
|
||||||
|
)}
|
||||||
<div className="dh-conversation-list">
|
<div className="dh-conversation-list">
|
||||||
{summaries.map((s) => {
|
{summaries.map((s) => (
|
||||||
const c = cards.find((c) => c.id === s.characterId);
|
<ConversationRow
|
||||||
return (
|
key={s.id}
|
||||||
<button
|
summary={s}
|
||||||
key={s.id}
|
card={cards.find((c) => c.id === s.characterId)}
|
||||||
onClick={() => openConversation(s.id)}
|
revealed={swipedConversation === s.id}
|
||||||
disabled={opening}
|
disabled={opening || deleting}
|
||||||
>
|
onReveal={setSwipedConversation}
|
||||||
<img src={c?.cover || "/icon.svg"} alt="" />
|
onOpen={() => openConversation(s.id)}
|
||||||
<div>
|
onDelete={() => requestDelete(s)}
|
||||||
<h3>{s.title}</h3>
|
/>
|
||||||
<p>{c?.description || "继续这段私人对话"}</p>
|
))}
|
||||||
</div>
|
|
||||||
<small>
|
|
||||||
{new Date(s.updatedAt).toLocaleDateString("zh-CN", {
|
|
||||||
month: "short",
|
|
||||||
day: "numeric",
|
|
||||||
})}
|
|
||||||
</small>
|
|
||||||
<MessageCircle size={18} />
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
</div>
|
||||||
{!summaries.length && (
|
{!summaries.length && (
|
||||||
<div className="dh-empty">
|
<div className="dh-empty">
|
||||||
@@ -1658,7 +1998,7 @@ export function DigitalHome({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<p className="dh-form-note">
|
<p className="dh-form-note">
|
||||||
保存后从下一次回复生效。可以固定单条消息,将它加入对话记忆。
|
保存后用于接下来的回复。固定单条消息,可将它加入对话记忆。
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<button className="dh-primary" type="submit">
|
<button className="dh-primary" type="submit">
|
||||||
@@ -1748,6 +2088,17 @@ export function DigitalHome({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{deleteTarget && (
|
||||||
|
<ConversationDeleteDialog
|
||||||
|
title={deleteTarget.title}
|
||||||
|
busy={deleting}
|
||||||
|
error={deleteError}
|
||||||
|
onCancel={() => {
|
||||||
|
if (!deleting) setDeleteTarget(null);
|
||||||
|
}}
|
||||||
|
onConfirm={confirmDelete}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+313
-7
@@ -588,7 +588,7 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
}
|
}
|
||||||
.dh-conversation-list > button {
|
.dh-conversation-list .dh-conversation-open {
|
||||||
border: 1px solid var(--dh-line);
|
border: 1px solid var(--dh-line);
|
||||||
background: var(--dh-panel);
|
background: var(--dh-panel);
|
||||||
border-radius: 14px;
|
border-radius: 14px;
|
||||||
@@ -598,7 +598,7 @@
|
|||||||
gap: 16px;
|
gap: 16px;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
}
|
}
|
||||||
.dh-conversation-list > button:hover {
|
.dh-conversation-list .dh-conversation-open:hover {
|
||||||
border-color: #ffffff25;
|
border-color: #ffffff25;
|
||||||
}
|
}
|
||||||
.dh-conversation-list img {
|
.dh-conversation-list img {
|
||||||
@@ -620,7 +620,7 @@
|
|||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
margin-top: 5px;
|
margin-top: 5px;
|
||||||
}
|
}
|
||||||
.dh-conversation-list > button > div {
|
.dh-conversation-list .dh-conversation-open > div {
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
@@ -633,6 +633,273 @@
|
|||||||
color: var(--dh-accent);
|
color: var(--dh-accent);
|
||||||
flex: none;
|
flex: none;
|
||||||
}
|
}
|
||||||
|
/* 横滑只移动会话卡片,删除在明确确认后才执行。 */
|
||||||
|
.dh-conversation-row {
|
||||||
|
position: relative;
|
||||||
|
overflow: hidden;
|
||||||
|
border-radius: 14px;
|
||||||
|
background: #9a394b;
|
||||||
|
}
|
||||||
|
.dh-conversation-list .dh-conversation-open {
|
||||||
|
position: relative;
|
||||||
|
z-index: 1;
|
||||||
|
width: 100%;
|
||||||
|
touch-action: pan-y;
|
||||||
|
transition: transform 180ms ease;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
.dh-conversation-list .dh-conversation-open.is-dragging {
|
||||||
|
transition: none;
|
||||||
|
}
|
||||||
|
.dh-app .dh-row-delete-swipe {
|
||||||
|
position: absolute;
|
||||||
|
inset: 0 0 0 auto;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 5px;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 88px;
|
||||||
|
color: #fff;
|
||||||
|
background: #9a394b;
|
||||||
|
visibility: hidden;
|
||||||
|
}
|
||||||
|
.dh-conversation-row.is-revealed .dh-row-delete-swipe {
|
||||||
|
visibility: visible;
|
||||||
|
}
|
||||||
|
.dh-conversation-list .dh-row-delete-swipe svg {
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
.dh-row-delete-swipe span {
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.dh-row-delete-desktop {
|
||||||
|
position: absolute;
|
||||||
|
z-index: 2;
|
||||||
|
top: 50%;
|
||||||
|
right: 12px;
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
width: 34px;
|
||||||
|
height: 34px;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
border-radius: 9px;
|
||||||
|
}
|
||||||
|
.dh-conversation-list .dh-row-delete-desktop svg {
|
||||||
|
color: var(--dh-muted);
|
||||||
|
}
|
||||||
|
.dh-app .dh-row-delete-desktop:hover {
|
||||||
|
background: #f8717120;
|
||||||
|
}
|
||||||
|
.dh-conversation-row.is-revealed .dh-row-delete-desktop {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.dh-swipe-hint {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.dh-delete-dialog {
|
||||||
|
width: min(420px, calc(100% - 32px));
|
||||||
|
max-height: 90dvh;
|
||||||
|
padding: 26px;
|
||||||
|
color: var(--dh-text);
|
||||||
|
background: var(--dh-panel);
|
||||||
|
border: 1px solid #ffffff1c;
|
||||||
|
border-radius: 20px;
|
||||||
|
}
|
||||||
|
.dh-delete-dialog::backdrop {
|
||||||
|
background: #000b;
|
||||||
|
backdrop-filter: blur(6px);
|
||||||
|
}
|
||||||
|
.dh-delete-symbol {
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
width: 46px;
|
||||||
|
height: 46px;
|
||||||
|
margin-bottom: 18px;
|
||||||
|
color: #fda4af;
|
||||||
|
background: #fb718519;
|
||||||
|
border-radius: 14px;
|
||||||
|
}
|
||||||
|
.dh-delete-dialog h2 {
|
||||||
|
font-size: 21px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
.dh-delete-dialog p {
|
||||||
|
margin-top: 12px;
|
||||||
|
color: var(--dh-muted);
|
||||||
|
font-size: 13px;
|
||||||
|
line-height: 1.8;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
.dh-delete-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
margin-top: 24px;
|
||||||
|
}
|
||||||
|
.dh-app .dh-delete-actions button {
|
||||||
|
display: flex;
|
||||||
|
flex: 1;
|
||||||
|
gap: 7px;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
min-height: 44px;
|
||||||
|
font-size: 13px;
|
||||||
|
background: var(--dh-soft);
|
||||||
|
border: 1px solid var(--dh-line);
|
||||||
|
border-radius: 11px;
|
||||||
|
}
|
||||||
|
.dh-app .dh-delete-actions .is-danger {
|
||||||
|
color: #fff;
|
||||||
|
background: #a3374c;
|
||||||
|
border-color: #a3374c;
|
||||||
|
}
|
||||||
|
.dh-delete-dialog .dh-delete-error {
|
||||||
|
color: #fda4af;
|
||||||
|
}
|
||||||
|
.dh-compose-tools {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin: 0 4px 8px;
|
||||||
|
}
|
||||||
|
.dh-compose-actions {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 14px;
|
||||||
|
}
|
||||||
|
.dh-app .dh-compose-tools button {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
min-height: 32px;
|
||||||
|
padding: 0 4px;
|
||||||
|
color: var(--dh-accent);
|
||||||
|
font-size: 11px;
|
||||||
|
}
|
||||||
|
.dh-app .dh-compose-tools .dh-mobile-linebreak {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.dh-reply-ideas {
|
||||||
|
margin: 0 0 12px;
|
||||||
|
padding: 13px;
|
||||||
|
background: var(--dh-panel);
|
||||||
|
border: 1px solid var(--dh-line);
|
||||||
|
border-radius: 15px;
|
||||||
|
}
|
||||||
|
.dh-reply-ideas header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
.dh-reply-ideas strong {
|
||||||
|
display: flex;
|
||||||
|
gap: 7px;
|
||||||
|
align-items: center;
|
||||||
|
color: var(--dh-accent);
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
.dh-reply-ideas header .dh-icon {
|
||||||
|
width: 28px;
|
||||||
|
height: 28px;
|
||||||
|
}
|
||||||
|
.dh-ideas-note {
|
||||||
|
margin: 2px 0 10px !important;
|
||||||
|
color: var(--dh-muted);
|
||||||
|
font-size: 10px;
|
||||||
|
}
|
||||||
|
.dh-ideas-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 7px;
|
||||||
|
max-height: 30dvh;
|
||||||
|
overflow-y: auto;
|
||||||
|
overscroll-behavior: contain;
|
||||||
|
}
|
||||||
|
.dh-app .dh-ideas-list button {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
align-items: flex-start;
|
||||||
|
padding: 10px;
|
||||||
|
text-align: left;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.7;
|
||||||
|
background: var(--dh-soft);
|
||||||
|
border: 1px solid transparent;
|
||||||
|
border-radius: 9px;
|
||||||
|
}
|
||||||
|
.dh-app .dh-ideas-list button:hover {
|
||||||
|
border-color: var(--dh-accent);
|
||||||
|
}
|
||||||
|
.dh-ideas-list small {
|
||||||
|
flex: none;
|
||||||
|
color: var(--dh-accent);
|
||||||
|
font-size: 10px;
|
||||||
|
}
|
||||||
|
.dh-ideas-loading {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 58px;
|
||||||
|
color: var(--dh-muted);
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.dh-ideas-error {
|
||||||
|
padding: 12px 0;
|
||||||
|
color: #fda4af;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.dh-app .dh-ideas-refresh {
|
||||||
|
display: flex;
|
||||||
|
gap: 6px;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 32px;
|
||||||
|
margin-top: 7px;
|
||||||
|
color: var(--dh-accent);
|
||||||
|
font-size: 11px;
|
||||||
|
}
|
||||||
|
.dh-spin {
|
||||||
|
animation: dh-spin 1s linear infinite;
|
||||||
|
}
|
||||||
|
@keyframes dh-spin {
|
||||||
|
to {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media (width>600px) {
|
||||||
|
.dh-conversation-list .dh-conversation-open {
|
||||||
|
padding-right: 56px !important;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media (width<=600px) {
|
||||||
|
.dh-row-delete-desktop {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.dh-swipe-hint {
|
||||||
|
display: block;
|
||||||
|
margin: -10px 0 14px !important;
|
||||||
|
color: var(--dh-muted);
|
||||||
|
font-size: 10px;
|
||||||
|
}
|
||||||
|
.dh-delete-dialog {
|
||||||
|
width: 100%;
|
||||||
|
max-width: 100%;
|
||||||
|
margin: auto 0 0;
|
||||||
|
padding: 24px 22px calc(22px + env(safe-area-inset-bottom));
|
||||||
|
border-radius: 22px 22px 0 0;
|
||||||
|
border-bottom: 0;
|
||||||
|
}
|
||||||
|
.dh-app .dh-compose-tools .dh-mobile-linebreak {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
.dh-reply-ideas {
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
.dh-ideas-list {
|
||||||
|
max-height: 28dvh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.dh-page-lead {
|
.dh-page-lead {
|
||||||
max-width: 650px;
|
max-width: 650px;
|
||||||
color: var(--dh-muted);
|
color: var(--dh-muted);
|
||||||
@@ -707,6 +974,11 @@
|
|||||||
color: #d4cbd5;
|
color: #d4cbd5;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
.dh-settings > label small {
|
||||||
|
color: var(--dh-muted);
|
||||||
|
font-size: 11px;
|
||||||
|
line-height: 1.7;
|
||||||
|
}
|
||||||
.dh-profile-form textarea,
|
.dh-profile-form textarea,
|
||||||
.dh-settings textarea {
|
.dh-settings textarea {
|
||||||
resize: vertical;
|
resize: vertical;
|
||||||
@@ -1339,8 +1611,7 @@
|
|||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
overflow-x: hidden;
|
overflow-x: hidden;
|
||||||
overscroll-behavior: contain;
|
overscroll-behavior: contain;
|
||||||
scrollbar-width: thin;
|
scrollbar-width: none;
|
||||||
scrollbar-color: #ffffff20 transparent;
|
|
||||||
}
|
}
|
||||||
.dh-messages-inner {
|
.dh-messages-inner {
|
||||||
max-width: 840px;
|
max-width: 840px;
|
||||||
@@ -1824,7 +2095,7 @@
|
|||||||
.dh-library-card h3 {
|
.dh-library-card h3 {
|
||||||
margin-top: 16px;
|
margin-top: 16px;
|
||||||
}
|
}
|
||||||
.dh-conversation-list > button {
|
.dh-conversation-list .dh-conversation-open {
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
padding: 13px;
|
padding: 13px;
|
||||||
}
|
}
|
||||||
@@ -1838,7 +2109,7 @@
|
|||||||
.dh-conversation-list p {
|
.dh-conversation-list p {
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
}
|
}
|
||||||
.dh-conversation-list > button > svg {
|
.dh-conversation-list .dh-conversation-open > svg {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
.dh-profile-banner {
|
.dh-profile-banner {
|
||||||
@@ -2027,3 +2298,38 @@
|
|||||||
body:has(.dh-app) .environment-badge {
|
body:has(.dh-app) .environment-badge {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 内部区域保留手势和键盘滚动,不显示系统轨道,也不允许设定表单横向漂移。 */
|
||||||
|
.dh-app,
|
||||||
|
.dh-app * {
|
||||||
|
scrollbar-width: none !important;
|
||||||
|
}
|
||||||
|
.dh-app::-webkit-scrollbar,
|
||||||
|
.dh-app *::-webkit-scrollbar {
|
||||||
|
display: none !important;
|
||||||
|
width: 0 !important;
|
||||||
|
height: 0 !important;
|
||||||
|
}
|
||||||
|
.dh-settings-body,
|
||||||
|
.dh-selection-body,
|
||||||
|
.dh-detail-body,
|
||||||
|
.dh-ideas-list {
|
||||||
|
min-width: 0;
|
||||||
|
max-width: 100%;
|
||||||
|
overflow-x: hidden;
|
||||||
|
}
|
||||||
|
.dh-settings-body {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.dh-settings-body > label,
|
||||||
|
.dh-settings fieldset,
|
||||||
|
.dh-selection-sheet fieldset {
|
||||||
|
min-width: 0;
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
.dh-app textarea {
|
||||||
|
width: 100%;
|
||||||
|
max-width: 100%;
|
||||||
|
overflow-x: hidden;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/** 登出后的站内跳转,避免反向代理后的内部监听地址泄漏到浏览器。 */
|
||||||
|
|
||||||
|
/** 使用相对 Location 留在当前站点,并拒绝外部地址和 URL 控制字符。 */
|
||||||
|
export function logoutRedirect(next: string | null) {
|
||||||
|
let destination = "/login";
|
||||||
|
if (
|
||||||
|
next?.startsWith("/") &&
|
||||||
|
!/^\/[/\\]/.test(next) &&
|
||||||
|
!/[\\\u0000-\u0020\u007f]/.test(next)
|
||||||
|
) {
|
||||||
|
const local = new URL(next, "https://same-origin.invalid");
|
||||||
|
// 消除点路径后仍须禁止双斜杠,避免 /.//host 被规范化为外站地址。
|
||||||
|
if (local.origin === "https://same-origin.invalid" && !local.pathname.startsWith("//")) {
|
||||||
|
destination = local.pathname + local.search + local.hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Route Handler 可直接返回相对 Location;不使用 request.url(生产中可能是 0.0.0.0)。
|
||||||
|
return new Response(null, { status: 307, headers: { Location: destination } });
|
||||||
|
}
|
||||||
+4
-2
@@ -123,10 +123,12 @@ export type Bootstrap = {
|
|||||||
catalog: Card[];
|
catalog: Card[];
|
||||||
profile: Profile;
|
profile: Profile;
|
||||||
decision: {
|
decision: {
|
||||||
variant: "A" | "B";
|
variant: string | null;
|
||||||
|
value?: unknown;
|
||||||
|
fallbackValue?: unknown;
|
||||||
decisionId: string | null;
|
decisionId: string | null;
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
fallbackVariant: "A" | "B";
|
fallbackVariant: string | null;
|
||||||
};
|
};
|
||||||
serverNow: number;
|
serverNow: number;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/** 回归登录过期后的跳转:仅返回站内路径,不把服务监听地址发送给浏览器。 */
|
||||||
|
import test from 'node:test';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { logoutRedirect } from '../src/lib/auth-redirect.ts';
|
||||||
|
|
||||||
|
test('登录过期后跳回当前站点的登录页,不依赖服务内部地址', () => {
|
||||||
|
const response = logoutRedirect(null);
|
||||||
|
assert.equal(response.status, 307);
|
||||||
|
assert.equal(response.headers.get('location'), '/login');
|
||||||
|
assert.equal(new URL(response.headers.get('location'), 'https://mrzhan.top/api/auth/logout').href, 'https://mrzhan.top/login');
|
||||||
|
assert.equal(new URL(response.headers.get('location'), 'http://localhost:3210/api/auth/logout').href, 'http://localhost:3210/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('保留站内路径和查询参数', () => {
|
||||||
|
assert.equal(logoutRedirect('/login?reason=expired').headers.get('location'), '/login?reason=expired');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('拒绝外部链接、协议相对地址及反斜杠变体', () => {
|
||||||
|
for (const next of ['https://example.com', '//example.com', '/\\example.com', 'javascript:alert(1)', '\n//example.com', '/\n/example.com', '/.//example.com', '/%2e//example.com']) {
|
||||||
|
assert.equal(logoutRedirect(next).headers.get('location'), '/login', next);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user