From 98ddd137e0bd7faad8638203f14eed2a0307e58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=9B=E5=85=AE?= Date: Tue, 27 Oct 2026 20:18:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(nsfw):=20=E6=94=AF=E6=8C=81=E7=AE=A1?= =?UTF-8?q?=E7=90=86=20C=20=E7=AB=AF=E8=B4=A6=E5=8F=B7=E6=88=90=E4=BA=BA?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=81=8F=E5=A5=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - src/api/access.ts: 补充 C 端账号 NSFW 偏好字段与中文注释 - src/views/digital/users.vue: 展示开关状态,创建默认开启,编辑可保存账号选择 - README.md: 说明前后台共用偏好及配套发布要求 --- README.md | 2 ++ src/api/access.ts | 2 ++ src/views/digital/users.vue | 58 ++++++++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c96ae40..82fa0ad 100644 --- a/README.md +++ b/README.md @@ -253,3 +253,5 @@ pnpm build `src/views/digital/users.vue` 对应 `/digital/users`,用于创建独立 C 端账号、搜索用户、修改昵称/状态、重置密码,以及按用户查看数字人会话和原始消息。账号由后台开通,无公开注册;与管理员/员工账号分别鉴权,重置 C 端密码不影响后台。停用保留聊天记录。 `src/api/access.ts` 定义 C 端用户、会话和消息响应类型。消息记录第1页为最新内容,每页按发送顺序展示,支持刷新、分页和生成状态查看。原实验账号页面 `/digital/experiment-users` 更名为「实验用户分组」,只针对 C 端用户配置实验。 + +用户列表展示账号的 NSFW 开关;创建用户默认开启,编辑用户可单独开启或关闭。与前台「我的」读取和修改同一份服务端账号偏好,不覆盖收藏、称呼或身份背景。已有账号未保存该字段时默认开启,游客的登录限制不变;需与 `access-manage` 配套发布。 diff --git a/src/api/access.ts b/src/api/access.ts index 0902387..0ae6475 100644 --- a/src/api/access.ts +++ b/src/api/access.ts @@ -1232,6 +1232,8 @@ export type DigitalUser = { username: string; displayName: string; status: "ACTIVE" | "INACTIVE"; + /** 账号未保存内容偏好时默认开启,后端在生成时读取实际值。 */ + nsfwEnabled?: boolean; createdAt: string; lastLoginAt: string | null; conversations: number; diff --git a/src/views/digital/users.vue b/src/views/digital/users.vue index e427e87..6c367dd 100644 --- a/src/views/digital/users.vue +++ b/src/views/digital/users.vue @@ -1,5 +1,5 @@ + + + + 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 }]); +});