feat(chat): 区分角色对白与故事叙述
- digital-message.tsx: 统一流式与完成态展示,保留用户纯文本和停止后的尾部 - digital-message-format.ts: 解析星号叙述、中英文对白及旧版括号格式 - digital-home.tsx、digital.css: 接入独立消息组件并区分对白与叙述样式 - tests、README.md: 补齐累计流式、旧消息和文本安全回归及中文说明
This commit is contained in:
@@ -49,70 +49,9 @@ import {
|
||||
type Bootstrap,
|
||||
} from "@/lib/digital";
|
||||
import { ConversationRow, ConversationDeleteDialog } from "./conversation-row";
|
||||
import { DigitalMessage } from "./digital-message";
|
||||
import "./digital.css";
|
||||
type Tab = "discover" | "chats" | "library" | "me";
|
||||
/** 接收文本与视觉打字分离;减少动画设置下直接呈现完整文本。 */
|
||||
function TypedText({ text, animate }: { text: string; animate: boolean }) {
|
||||
const [shown, setShown] = useState(animate ? "" : text);
|
||||
const latest = useRef(text);
|
||||
useEffect(() => {
|
||||
latest.current = text;
|
||||
}, [text]);
|
||||
useEffect(() => {
|
||||
if (!animate) return;
|
||||
const instant = window.matchMedia(
|
||||
"(prefers-reduced-motion: reduce)",
|
||||
).matches;
|
||||
const timer = setInterval(
|
||||
() =>
|
||||
setShown((current) => {
|
||||
const target = latest.current;
|
||||
if (instant || !target.startsWith(current)) return target;
|
||||
const rest = Array.from(target.slice(current.length));
|
||||
return (
|
||||
current +
|
||||
rest
|
||||
.slice(0, Math.max(1, Math.min(10, Math.ceil(rest.length / 15))))
|
||||
.join("")
|
||||
);
|
||||
}),
|
||||
24,
|
||||
);
|
||||
return () => clearInterval(timer);
|
||||
}, [animate]);
|
||||
return <>{animate ? shown : text}</>;
|
||||
}
|
||||
/** 内容完全作为文本渲染,角色对白不允许注入 HTML。 */
|
||||
function MessageText({
|
||||
message,
|
||||
animate,
|
||||
}: {
|
||||
message: Message;
|
||||
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 (
|
||||
<div className="dh-message-text">
|
||||
<TypedText
|
||||
text={
|
||||
message.content ||
|
||||
(message.status === "generating" ? "" : "这次没有生成内容。")
|
||||
}
|
||||
animate={animate}
|
||||
/>
|
||||
{message.status === "generating" && (
|
||||
<span className="dh-caret" aria-hidden />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
/** 前台管理个人会话偏好;后台方案仅展示覆盖提示,不开放实验操作。 */
|
||||
export function DigitalHome({
|
||||
initial,
|
||||
@@ -1246,11 +1185,9 @@ export function DigitalHome({
|
||||
{m.pinned && <Pin size={11} />}
|
||||
</span>
|
||||
<div className="dh-bubble">
|
||||
<MessageText
|
||||
<DigitalMessage
|
||||
message={m}
|
||||
animate={
|
||||
m.id === activeMessage && m.status === "generating"
|
||||
}
|
||||
animate={m.id === activeMessage}
|
||||
/>
|
||||
</div>
|
||||
{!busy && m.content && (
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
"use client";
|
||||
|
||||
/** 角色对白与故事叙述的安全展示;流式与完成态共用排版,用户消息保持纯文本。 */
|
||||
import { memo, useEffect, useMemo, useRef, useState } from "react";
|
||||
import type { Message } from "@/lib/digital";
|
||||
import { parseDigitalMessage } from "@/lib/digital-message-format";
|
||||
|
||||
/** 网络接收与视觉出字分离,结束后继续显示已收到的尾部,减少动画设置下直接显示。 */
|
||||
function useRevealedText(text: string, animate: boolean, generating: boolean) {
|
||||
const [shown, setShown] = useState(animate ? "" : text);
|
||||
const latest = useRef(text);
|
||||
useEffect(() => {
|
||||
latest.current = text;
|
||||
}, [text]);
|
||||
const revealing = animate && (generating || shown !== text);
|
||||
useEffect(() => {
|
||||
if (!revealing) return;
|
||||
const instant = window.matchMedia(
|
||||
"(prefers-reduced-motion: reduce)",
|
||||
).matches;
|
||||
const timer = setInterval(() => {
|
||||
setShown((current) => {
|
||||
const target = latest.current;
|
||||
if (instant || !target.startsWith(current)) return target;
|
||||
const rest = Array.from(target.slice(current.length));
|
||||
return (
|
||||
current +
|
||||
rest
|
||||
.slice(0, Math.max(1, Math.min(10, Math.ceil(rest.length / 15))))
|
||||
.join("")
|
||||
);
|
||||
});
|
||||
}, 24);
|
||||
return () => clearInterval(timer);
|
||||
}, [revealing]);
|
||||
return animate ? shown : text;
|
||||
}
|
||||
|
||||
/** 一条消息仍是一个操作单元;片段均渲染为文本节点,角色内容不能注入 HTML。 */
|
||||
export const DigitalMessage = memo(function DigitalMessage({
|
||||
message,
|
||||
animate,
|
||||
}: {
|
||||
message: Message;
|
||||
animate: boolean;
|
||||
}) {
|
||||
const generating = message.status === "generating";
|
||||
const text = useRevealedText(message.content, animate, generating);
|
||||
const blocks = useMemo(
|
||||
() =>
|
||||
message.role === "assistant"
|
||||
? parseDigitalMessage(
|
||||
text,
|
||||
generating ||
|
||||
["stopped", "failed"].includes(message.status) ||
|
||||
text !== message.content,
|
||||
)
|
||||
: [],
|
||||
[generating, message.content, message.role, message.status, text],
|
||||
);
|
||||
if (generating && !message.content.trim())
|
||||
return (
|
||||
<span className="dh-thinking" role="status" aria-label="正在思考">
|
||||
<i aria-hidden />
|
||||
<i aria-hidden />
|
||||
<i aria-hidden />
|
||||
</span>
|
||||
);
|
||||
const cursor = generating ? <span className="dh-caret" aria-hidden /> : null;
|
||||
return (
|
||||
<div
|
||||
className={`dh-message-text${message.role === "assistant" ? " dh-message-prose" : ""}`}
|
||||
>
|
||||
{message.role === "assistant"
|
||||
? blocks.map((block, index) => (
|
||||
<p
|
||||
key={index}
|
||||
className={`dh-prose-${block.kind}${/[A-Za-z]/.test(block.text) && !/[\u3400-\u9fff]/.test(block.text) ? " dh-prose-latin" : ""}`}
|
||||
>
|
||||
{block.kind === "dialogue" ? (
|
||||
<strong>{block.text}</strong>
|
||||
) : (
|
||||
block.text
|
||||
)}
|
||||
{index === blocks.length - 1 ? cursor : null}
|
||||
</p>
|
||||
))
|
||||
: text}
|
||||
{!blocks.length ? cursor : null}
|
||||
{!message.content && !generating ? "这次没有生成内容。" : null}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -1701,6 +1701,28 @@
|
||||
font-size: 14px;
|
||||
color: #e6dfe7;
|
||||
}
|
||||
.dh-message-prose {
|
||||
white-space: normal;
|
||||
}
|
||||
.dh-message-prose > p {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
.dh-message-prose > p + p {
|
||||
margin-top: 12px;
|
||||
}
|
||||
.dh-prose-dialogue {
|
||||
color: #fff5fc;
|
||||
}
|
||||
.dh-prose-dialogue strong {
|
||||
font-weight: 600;
|
||||
}
|
||||
.dh-prose-narration {
|
||||
color: #c2b6c4;
|
||||
}
|
||||
.dh-prose-narration.dh-prose-latin {
|
||||
font-style: italic;
|
||||
}
|
||||
.dh-message-user {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user