diff --git a/README.md b/README.md index 82fa0ad..f253d2d 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ http://localhost:8848/ ├── src/ # 前端源码 │ ├── api/ # HTTP API 封装,业务接口集中在 access.ts │ ├── assets/ # 图片、SVG、iconfont 等静态资源 -│ ├── components/ # 模板沉淀的通用组件 +│ ├── components/ # 通用组件,DigitalMessageText.vue 提供对白/叙述预览与聊天记录展示 │ ├── config/ # 读取 public/platform-config.json 的运行时配置 │ ├── directives/ # 自定义指令,如权限、复制、长按、波纹 │ ├── layout/ # 后台布局、菜单、标签页、导航栏与布局 hooks @@ -70,11 +70,12 @@ http://localhost:8848/ │ ├── router/ # 静态路由、剩余路由、路由实例与守卫 │ ├── store/ # Pinia store,含用户、权限、标签页、主题等模块 │ ├── style/ # 全局样式、主题、暗色模式、侧边栏和过渡样式 -│ ├── utils/ # 鉴权、HTTP、缓存、消息、进度条、树处理等工具 +│ ├── utils/ # 鉴权、HTTP、缓存及 digital-message-format.ts 受限文本解析 │ └── views/ │ ├── digital/ # 内容库、实验类型、测试方案和用户分组 │ └── ... # 原门店与员工业务兼容页面 ├── types/ # 全局类型声明、组件声明、路由声明和 Vue shim +├── tests/ # 数字人对白/叙述格式回归,与 role-user 使用相同用例 ├── AGENTS.md # Codex/Agent 入口规则,转到 RTK.md 并登记本地 skill ├── RTK.md # 本仓库协作规则与 README 同步要求 ├── Dockerfile # 容器构建入口 @@ -214,6 +215,10 @@ pnpm build ## 数字人运营 +角色「人格与开场」提供对白/叙述格式说明和首次问候、示例对白、其他开场的实时预览,预览替换角色/用户占位符但不修改原文。生成预设始终遵守系统统一排版:`*叙述*` 与 `“对白”`,切换时空一行。用户管理的聊天记录也使用 `DigitalMessageText.vue`,用户原文保持纯文本,角色消息兼容旧括号与引号。 + +`src/utils/digital-message-format.ts` 与 `role-user/src/lib/digital-message-format.ts` 保持同一实现和用例,两仓独立构建。所有内容通过文本节点展示,不使用 HTML 注入。执行 `node --experimental-strip-types --test tests/*.test.mjs` 验证协议兼容。 + - `src/views/digital/content.vue`:角色卡、世界书、预设,草稿/发布/下架,JSON 导入导出。 - `src/views/digital/experiments.vue`:测试方案的类型选择、A/B 返回值、百分比、人群、生命周期、效果和审计。 - `src/views/digital/experiment-types.vue`:类型创建、默认值契约、来源、调用示例与接入验收。 diff --git a/src/components/DigitalMessageText.vue b/src/components/DigitalMessageText.vue new file mode 100644 index 0000000..e122458 --- /dev/null +++ b/src/components/DigitalMessageText.vue @@ -0,0 +1,79 @@ + + + + + + + {{ block.text }} + {{ block.text }} + + + {{ text || "(未生成正文)" }} + + + + diff --git a/src/utils/digital-message-format.ts b/src/utils/digital-message-format.ts new file mode 100644 index 0000000..11f011f --- /dev/null +++ b/src/utils/digital-message-format.ts @@ -0,0 +1,151 @@ +/** 数字人消息的受限文本格式:对白、故事叙述与旧版括号;始终返回文本,不解释 HTML。 */ + +/** 有序展示片段;原始 content 继续用于存储、复制、记忆和上下文。 */ +export type DigitalMessageBlock = { + kind: "dialogue" | "narration" | "plain"; + text: string; +}; + +const quotePairs: Record = { + '"': '"', + "“": "”", + "「": "」", + "『": "』", + "«": "»", + """: """ +}; + +/** 跳过转义字符;引号不跨行,括号按同类型嵌套匹配,避免误吞后续内容。 */ +function closingIndex( + text: string, + from: number, + open: string, + close: string, + nested = false +) { + let depth = 1; + for (let i = from; i < text.length; i++) { + if (text[i] === "\\") { + i++; + continue; + } + if (!nested && quotePairs[open] && text[i] === "\n") return -1; + if (nested && text.startsWith(open, i)) depth++; + if (text.startsWith(close, i)) { + depth--; + if (depth === 0) return i; + } + } + return -1; +} + +/** 星号需要处于词边界,避免把乘法、文件名或普通单词里的星号视为排版。 */ +function markerBoundary(text: string, index: number) { + return ( + index === 0 || /[\s。!?!?,,;;::))\]”"」』»]/u.test(text[index - 1]) + ); +} + +/** + * 解析累计正文而非单个 SSE delta。未闭合标记仅在流式/中断展示时延续当前样式; + * 旧版括号外文本按对白处理,有引号的旧开场白将引号外文字作为叙述,其他纯文本保留。 + * 与 role-user/src/lib/digital-message-format.ts 保持同一实现和用例,独立构建时无需跨仓引用。 + */ +export function parseDigitalMessage( + source: string, + streaming = false +): DigitalMessageBlock[] { + const blocks: DigitalMessageBlock[] = []; + for (const paragraph of source.replace(/\r\n?/g, "\n").split(/\n\s*\n/)) { + const parts: DigitalMessageBlock[] = []; + let legacyNarration = false; + const push = (kind: DigitalMessageBlock["kind"], text: string) => { + if (!text) return; + const last = parts[parts.length - 1]; + if (last?.kind === kind) last.text += text; + else parts.push({ kind, text }); + }; + for (let i = 0; i < paragraph.length; ) { + const char = paragraph[i]; + if ( + char === "\\" && + /[*"\\()()“”「」『』]/u.test(paragraph[i + 1] ?? "") + ) { + push("plain", paragraph[i + 1]); + i += 2; + continue; + } + const quoteEnd = quotePairs[char]; + if (quoteEnd) { + const close = closingIndex(paragraph, i + 1, char, quoteEnd); + const lineEnd = paragraph.indexOf("\n", i + 1); + const end = + close >= 0 ? close + 1 : lineEnd >= 0 ? lineEnd : paragraph.length; + push("dialogue", paragraph.slice(i, end)); + i = end; + continue; + } + if ( + char === "*" && + markerBoundary(paragraph, i) && + !/\s/u.test(paragraph[i + 1] ?? "") + ) { + const marker = paragraph.startsWith("**", i) ? "**" : "*"; + const close = closingIndex( + paragraph, + i + marker.length, + marker, + marker + ); + if (close >= 0 || streaming) { + push( + marker === "**" ? "dialogue" : "narration", + paragraph.slice(i + marker.length, close >= 0 ? close : undefined) + ); + i = close >= 0 ? close + marker.length : paragraph.length; + continue; + } + // 格式不完整的历史正文原样保留,不猜测后面的哪些字符属于标记。 + push("plain", paragraph.slice(i)); + break; + } + if ((char === "(" || char === "(") && markerBoundary(paragraph, i)) { + const close = closingIndex( + paragraph, + i + 1, + char, + char === "(" ? ")" : ")", + true + ); + if (close >= 0 || streaming) { + legacyNarration = true; + push( + "narration", + paragraph.slice(i + 1, close >= 0 ? close : undefined) + ); + i = close >= 0 ? close + 1 : paragraph.length; + continue; + } + push("plain", paragraph.slice(i)); + break; + } + push("plain", char); + i++; + } + const hasDialogue = parts.some(part => part.kind === "dialogue"); + for (const part of parts) { + const kind = + part.kind !== "plain" + ? part.kind + : legacyNarration + ? "dialogue" + : hasDialogue + ? "narration" + : "plain"; + for (const line of part.text.split(/\n+/)) { + if (line.trim()) blocks.push({ kind, text: line.trim() }); + } + } + } + return blocks; +} diff --git a/src/views/digital/content.vue b/src/views/digital/content.vue index b207090..9a903c9 100644 --- a/src/views/digital/content.vue +++ b/src/views/digital/content.vue @@ -1,8 +1,9 @@ "; + assert.deepEqual(parse(text), [{ kind: "plain", text }]); +});
+ {{ block.text }} + {{ block.text }} +