feat(chat): 新增继续按钮并修复回复滚动跟随 #13

Merged
zhanxi merged 1 commits from feature/env-pipeline-split into master 2026-10-27 10:15:51 +00:00
3 changed files with 118 additions and 34 deletions
+4
View File
@@ -144,3 +144,7 @@ pnpm start:prod
- `src/lib/auth-redirect.ts`:登出与会话过期后使用站内相对 `Location`,避免跳到 Next.js 内部监听地址 `0.0.0.0:3210`;仅允许站内返回路径。 - `src/lib/auth-redirect.ts`:登出与会话过期后使用站内相对 `Location`,避免跳到 Next.js 内部监听地址 `0.0.0.0:3210`;仅允许站内返回路径。
- `tests/auth-redirect.test.mjs`:覆盖正式域名/本地地址的登录页跳转、站内参数和外部地址拦截。 - `tests/auth-redirect.test.mjs`:覆盖正式域名/本地地址的登录页跳转、站内参数和外部地址拦截。
- 执行:`node --experimental-strip-types --test tests/auth-redirect.test.mjs`(Node.js 22)。 - 执行:`node --experimental-strip-types --test tests/auth-redirect.test.mjs`(Node.js 22)。
聊天滚动将“跟随最新”与当前底部距离分开:停在底部时,流式回复、结束后操作栏及软键盘导致的尺寸变化均继续贴底;主动上翻才暂停跟随,点击“回到最新”重新启用。等待首段正文仅显示思考点,收到文字后才显示打字光标,结束时移除生成提示。
聊天输入区“继续”让 NPC 基于上一段内容继续说话,不发送或清空输入框草稿,也不插入用户气泡;可连续点击生成多段,生成时沿用停止按钮。角色尚无可接续内容时按钮不可用。
+98 -23
View File
@@ -90,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
@@ -166,13 +174,21 @@ 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.at(-1);
const canContinue =
lastReply?.role === "assistant" &&
["complete", "stopped"].includes(lastReply.status) &&
!!lastReply.content.trim();
// 状态提示走只读接口,打开设定不应写入实验分组、决策或曝光。 // 状态提示走只读接口,打开设定不应写入实验分组、决策或曝光。
useEffect(() => { useEffect(() => {
if (!showSettings || !conversationId) return; if (!showSettings || !conversationId) return;
@@ -264,17 +280,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;
@@ -419,6 +452,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 {
@@ -468,7 +502,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 ||
@@ -478,13 +512,14 @@ export function DigitalHome({
return; return;
closeIdeas(); 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") { if (mode === "send") {
setDraft(""); setDraft("");
@@ -996,7 +1031,8 @@ export function DigitalHome({
); );
// UI按消费者返回值决定,组名仅用于归因;C到Z同样可以返回新版UI。 // UI按消费者返回值决定,组名仅用于归因;C到Z同样可以返回新版UI。
// 缺少value时兼容尚未升级的服务响应。 // 缺少value时兼容尚未升级的服务响应。
const modern = boot?.decision.value !== undefined const modern =
boot?.decision.value !== undefined
? boot.decision.value === "new" || boot.decision.value === true ? boot.decision.value === "new" || boot.decision.value === true
: boot?.decision.variant === "B"; : boot?.decision.variant === "B";
return ( return (
@@ -1127,12 +1163,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);
} }
}} }}
> >
@@ -1168,13 +1237,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">
@@ -1253,9 +1315,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",
}); });
}} }}
> >
@@ -1325,6 +1388,7 @@ export function DigitalHome({
</section> </section>
)} )}
<div className="dh-compose-tools"> <div className="dh-compose-tools">
<div className="dh-compose-actions">
<button <button
type="button" type="button"
disabled={busy || ideasLoading} disabled={busy || ideasLoading}
@@ -1333,6 +1397,17 @@ export function DigitalHome({
<Sparkles size={14} /> <Sparkles size={14} />
{ideasLoading ? "灵感生成中…" : "灵感回复"} {ideasLoading ? "灵感生成中…" : "灵感回复"}
</button> </button>
<button
type="button"
aria-label="让角色继续"
title="不发送输入框里的草稿,让角色接着说"
disabled={busy || ideasLoading || !canContinue}
onClick={() => send("continue")}
>
<ChevronRight size={14} />
继续
</button>
</div>
<button <button
type="button" type="button"
className="dh-mobile-linebreak" className="dh-mobile-linebreak"
+5
View File
@@ -762,6 +762,11 @@
align-items: center; align-items: center;
margin: 0 4px 8px; margin: 0 4px 8px;
} }
.dh-compose-actions {
display: flex;
align-items: center;
gap: 14px;
}
.dh-app .dh-compose-tools button { .dh-app .dh-compose-tools button {
display: flex; display: flex;
align-items: center; align-items: center;