feat(content): 增加对白与叙述排版预览
- DigitalMessageText.vue: 为角色预览与聊天记录提供统一的受限文本展示 - content.vue: 增加首次问候、备用开场、示例对白的实时预览与格式说明 - users.vue: 按相同规则展示角色聊天记录,保留用户纯文本 - digital-message-format.ts、tests、README.md: 同步前台格式实现、兼容用例与目录说明
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<script setup lang="ts">
|
||||
/** 角色编辑预览与聊天记录共用的受限文本展示,用户消息和 HTML 字面量均不作为标签解释。 */
|
||||
import { computed } from "vue";
|
||||
import { parseDigitalMessage } from "@/utils/digital-message-format";
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
text: string;
|
||||
role?: "assistant" | "user";
|
||||
incomplete?: boolean;
|
||||
}>(),
|
||||
{ role: "assistant", incomplete: false }
|
||||
);
|
||||
const blocks = computed(() =>
|
||||
parseDigitalMessage(props.text, props.incomplete)
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
class="digital-message-text"
|
||||
:class="{ 'is-prose': role === 'assistant' }"
|
||||
>
|
||||
<template v-if="role === 'assistant' && blocks.length">
|
||||
<p
|
||||
v-for="(block, index) in blocks"
|
||||
:key="index"
|
||||
:class="[
|
||||
block.kind,
|
||||
{
|
||||
'is-latin':
|
||||
/[A-Za-z]/.test(block.text) && !/[\u3400-\u9fff]/.test(block.text)
|
||||
}
|
||||
]"
|
||||
>
|
||||
<strong v-if="block.kind === 'dialogue'">{{ block.text }}</strong>
|
||||
<template v-else>{{ block.text }}</template>
|
||||
</p>
|
||||
</template>
|
||||
<template v-else>{{ text || "(未生成正文)" }}</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.digital-message-text {
|
||||
line-height: 1.9;
|
||||
overflow-wrap: anywhere;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.is-prose {
|
||||
white-space: normal;
|
||||
}
|
||||
|
||||
.is-prose > p {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.is-prose > p + p {
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
.dialogue {
|
||||
color: var(--digital-dialogue-ink, var(--el-text-color-primary));
|
||||
}
|
||||
|
||||
.dialogue strong {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.narration {
|
||||
color: var(--digital-narration-ink, var(--el-text-color-regular));
|
||||
}
|
||||
|
||||
.narration.is-latin {
|
||||
font-style: italic;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,151 @@
|
||||
/** 数字人消息的受限文本格式:对白、故事叙述与旧版括号;始终返回文本,不解释 HTML。 */
|
||||
|
||||
/** 有序展示片段;原始 content 继续用于存储、复制、记忆和上下文。 */
|
||||
export type DigitalMessageBlock = {
|
||||
kind: "dialogue" | "narration" | "plain";
|
||||
text: string;
|
||||
};
|
||||
|
||||
const quotePairs: Record<string, string> = {
|
||||
'"': '"',
|
||||
"“": "”",
|
||||
"「": "」",
|
||||
"『": "』",
|
||||
"«": "»",
|
||||
""": """
|
||||
};
|
||||
|
||||
/** 跳过转义字符;引号不跨行,括号按同类型嵌套匹配,避免误吞后续内容。 */
|
||||
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;
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
/** 数字人内容库:角色、世界书、叙事预设分别编辑,发布状态显式可见。 */
|
||||
/** 数字人内容库:角色、世界书、预设分别编辑,开场与示例使用聊天格式实时预览。 */
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { ElMessage } from "element-plus";
|
||||
import { digital, type Content, type Entry } from "@/api/access";
|
||||
import DigitalMessageText from "@/components/DigitalMessageText.vue";
|
||||
const contents = ref<Content[]>([]),
|
||||
kind = ref("character"),
|
||||
query = ref(""),
|
||||
@@ -29,6 +30,12 @@ const statusNames: Record<string, string> = {
|
||||
published: "已发布",
|
||||
archived: "已下架"
|
||||
};
|
||||
/** 占位符仅用于当前预览,不改写编辑内容或数据库中的角色模板。 */
|
||||
function previewText(value: string) {
|
||||
return value
|
||||
.replaceAll("{{char}}", edit.value?.name || "角色")
|
||||
.replaceAll("{{user}}", "旅人");
|
||||
}
|
||||
const books = computed(() =>
|
||||
contents.value.filter(c => c.kind === "worldbook" && c.status === "published")
|
||||
);
|
||||
@@ -323,7 +330,12 @@ onMounted(load);
|
||||
v-if="edit.kind === 'character'"
|
||||
label="人格与开场"
|
||||
name="persona"
|
||||
><el-form-item label="性格、背景和关系"
|
||||
><el-alert
|
||||
class="format-guide"
|
||||
title="对白与叙述分段书写"
|
||||
description="对白使用成对引号,如:“你来了。”;动作、神态、心理和环境用单星号包裹,如:*她合上书,抬头微笑。*。切换时空一行,下方预览与聊天使用相同的格式规则。"
|
||||
:closable="false"
|
||||
/><el-form-item label="性格、背景和关系"
|
||||
><el-input
|
||||
v-model="edit.body.personality"
|
||||
type="textarea"
|
||||
@@ -334,15 +346,36 @@ onMounted(load);
|
||||
type="textarea"
|
||||
:rows="3" /></el-form-item
|
||||
><el-form-item label="首次问候(支持 {{char}} / {{user}} 占位符)"
|
||||
><el-input
|
||||
v-model="edit.body.greeting"
|
||||
type="textarea"
|
||||
:rows="4" /></el-form-item
|
||||
><div class="format-editor">
|
||||
<el-input
|
||||
v-model="edit.body.greeting"
|
||||
type="textarea"
|
||||
:rows="4"
|
||||
placeholder="*她合上书,抬头看向你。* “你来了。”"
|
||||
/>
|
||||
<div
|
||||
v-if="edit.body.greeting.trim()"
|
||||
class="format-preview"
|
||||
aria-label="首次问候展示预览"
|
||||
>
|
||||
<small>首次问候 · 展示效果</small>
|
||||
<DigitalMessageText :text="previewText(edit.body.greeting)" />
|
||||
</div></div></el-form-item
|
||||
><el-form-item label="示例对白"
|
||||
><el-input
|
||||
v-model="edit.body.examples"
|
||||
type="textarea"
|
||||
:rows="4" /></el-form-item
|
||||
><div class="format-editor">
|
||||
<el-input
|
||||
v-model="edit.body.examples"
|
||||
type="textarea"
|
||||
:rows="4"
|
||||
/>
|
||||
<div
|
||||
v-if="edit.body.examples.trim()"
|
||||
class="format-preview"
|
||||
aria-label="示例对白展示预览"
|
||||
>
|
||||
<small>示例对白 · 展示效果</small>
|
||||
<DigitalMessageText :text="previewText(edit.body.examples)" />
|
||||
</div></div></el-form-item
|
||||
><el-form-item label="角色系统指令"
|
||||
><el-input
|
||||
v-model="edit.body.systemPrompt"
|
||||
@@ -355,12 +388,24 @@ onMounted(load);
|
||||
:key="i"
|
||||
class="entry-row"
|
||||
>
|
||||
<el-input
|
||||
v-model="edit.body.alternateGreetings[i]"
|
||||
type="textarea"
|
||||
:rows="2"
|
||||
/><el-button
|
||||
@click="edit.body.alternateGreetings.splice(i, 1)"
|
||||
<div class="format-editor">
|
||||
<el-input
|
||||
v-model="edit.body.alternateGreetings[i]"
|
||||
type="textarea"
|
||||
:rows="2"
|
||||
/>
|
||||
<div
|
||||
v-if="edit.body.alternateGreetings[i].trim()"
|
||||
class="format-preview"
|
||||
:aria-label="`其他开场 ${i + 1} 展示预览`"
|
||||
>
|
||||
<small>其他开场 {{ i + 1 }} · 展示效果</small>
|
||||
<DigitalMessageText
|
||||
:text="previewText(edit.body.alternateGreetings[i])"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<el-button @click="edit.body.alternateGreetings.splice(i, 1)"
|
||||
>移除</el-button
|
||||
>
|
||||
</div>
|
||||
@@ -465,7 +510,11 @@ onMounted(load);
|
||||
v-if="edit.kind === 'preset'"
|
||||
label="生成编排"
|
||||
name="generation"
|
||||
><el-form-item label="系统指令"
|
||||
><el-alert
|
||||
class="format-guide"
|
||||
title="系统统一区分对白和叙述"
|
||||
description="本页设置叙事风格;对白使用引号、叙述使用单星号并分段的排版规则始终生效,发送、继续与重新生成共用。"
|
||||
:closable="false" /><el-form-item label="系统指令"
|
||||
><el-input
|
||||
v-model="edit.body.systemPrompt"
|
||||
type="textarea"
|
||||
@@ -512,6 +561,37 @@ onMounted(load);
|
||||
</div>
|
||||
</template>
|
||||
<style scoped>
|
||||
.format-guide {
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.format-editor {
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.format-preview {
|
||||
--digital-dialogue-ink: #fff5fc;
|
||||
--digital-narration-ink: #c2b6c4;
|
||||
|
||||
max-height: 300px;
|
||||
padding: 14px 18px;
|
||||
margin-top: 10px;
|
||||
overflow: auto;
|
||||
font-size: 14px;
|
||||
color: #e6dfe7;
|
||||
background: #211c23;
|
||||
border: 1px solid #ffffff12;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.format-preview > small {
|
||||
display: block;
|
||||
margin-bottom: 10px;
|
||||
font-size: 11px;
|
||||
color: #b5a6b8;
|
||||
}
|
||||
|
||||
.heading {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
/** 独立 C 端用户管理:开户、内容偏好、停用、改密与持久化聊天记录。 */
|
||||
/** 独立 C 端用户管理:开户、内容偏好、停用、改密与区分对白/叙述的持久化聊天记录。 */
|
||||
import { onMounted, ref } from "vue";
|
||||
import { ElMessage } from "element-plus";
|
||||
import DigitalMessageText from "@/components/DigitalMessageText.vue";
|
||||
import {
|
||||
digital,
|
||||
type DigitalUser,
|
||||
@@ -477,7 +478,14 @@ onMounted(load);
|
||||
>{{ states[m.status] ?? m.status }}</el-tag
|
||||
><el-tag v-if="m.pinned" size="small">已固定</el-tag>
|
||||
</div>
|
||||
<p>{{ m.content || "(未生成正文)" }}</p>
|
||||
<DigitalMessageText
|
||||
class="history-message-body"
|
||||
:text="m.content"
|
||||
:role="m.role"
|
||||
:incomplete="
|
||||
['generating', 'stopped', 'failed'].includes(m.status)
|
||||
"
|
||||
/>
|
||||
</article>
|
||||
</div>
|
||||
<el-pagination
|
||||
@@ -606,7 +614,7 @@ h2 {
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
|
||||
.messages article p {
|
||||
.messages article .history-message-body {
|
||||
padding: 12px 14px;
|
||||
margin: 6px 0;
|
||||
overflow-wrap: anywhere;
|
||||
@@ -615,7 +623,7 @@ h2 {
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.messages article.user p {
|
||||
.messages article.user .history-message-body {
|
||||
background: var(--el-color-primary-light-9);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user