Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 69a67e4263 | |||
| 25920f5415 | |||
| 905100da40 | |||
| 927409f615 | |||
| 38bf60cf36 | |||
| 5e829cece0 | |||
| 17647a4fe3 | |||
| d481ca71fe | |||
| c052962de9 | |||
| e4f3c95a6a | |||
| b6bb64e902 | |||
| dbef13db18 | |||
| f1d07d6876 | |||
| e9e6275148 | |||
| 2d024bfb42 |
@@ -30,6 +30,7 @@
|
|||||||
- 数据校验:使用 zod 校验路径参数、查询参数和请求体。
|
- 数据校验:使用 zod 校验路径参数、查询参数和请求体。
|
||||||
- 数据库迁移:使用 `migrations/*.sql` 管理建表和初始化数据。
|
- 数据库迁移:使用 `migrations/*.sql` 管理建表和初始化数据。
|
||||||
- 事务处理:创建/更新员工和角色绑定时使用事务,避免部分成功。
|
- 事务处理:创建/更新员工和角色绑定时使用事务,避免部分成功。
|
||||||
|
- 数字人排版:`digital.prompt.ts` 统一约束对白与故事叙述,独立预留提示词预算;消息原文和 SSE 协议保持兼容,详见 `docs/DIGITAL_HUMANS.md`。
|
||||||
|
|
||||||
## 目录结构
|
## 目录结构
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@
|
|||||||
│ ├── docker-compose.mysql.test.yml # 测试 MySQL Compose 模板
|
│ ├── docker-compose.mysql.test.yml # 测试 MySQL Compose 模板
|
||||||
│ └── docker-compose.mysql.production.yml # 生产 MySQL Compose 模板
|
│ └── docker-compose.mysql.production.yml # 生产 MySQL Compose 模板
|
||||||
├── data/digital-seed.json # 20位成年角色、4本世界书和4种叙事预设
|
├── data/digital-seed.json # 20位成年角色、4本世界书和4种叙事预设
|
||||||
├── tests/ # 数字人规则、流式和匿名鉴权回归
|
├── tests/ # 数字人规则、流式、对白/叙述格式和匿名鉴权回归
|
||||||
├── migrations/ # 数据库迁移 SQL
|
├── migrations/ # 数据库迁移 SQL
|
||||||
│ ├── 001_initial_schema.sql # 创建基础表结构
|
│ ├── 001_initial_schema.sql # 创建基础表结构
|
||||||
│ ├── 002_seed_demo_data.sql # 初始化演示门店和角色
|
│ ├── 002_seed_demo_data.sql # 初始化演示门店和角色
|
||||||
|
|||||||
@@ -23,6 +23,24 @@ NSFW 关闭时要求淡出并尊重边界,且优先于角色、世界书和叙
|
|||||||
|
|
||||||
后台创建草稿后启动;运行后规则冻结,支持暂停/恢复,结束必须选择最终保留的配置组且不能重开。同一类型消费标识仅允许一个未结束方案。新老用户由真实账号创建时间与显式注册分界定义。流量与组别采用独立哈希域;人工指定有原因、有效期和人群绕过开关。预览解释不分组、不曝光。前台无实验控制,短时决策过期后回类型默认返回值(无组别)。
|
后台创建草稿后启动;运行后规则冻结,支持暂停/恢复,结束必须选择最终保留的配置组且不能重开。同一类型消费标识仅允许一个未结束方案。新老用户由真实账号创建时间与显式注册分界定义。流量与组别采用独立哈希域;人工指定有原因、有效期和人群绕过开关。预览解释不分组、不曝光。前台无实验控制,短时决策过期后回类型默认返回值(无组别)。
|
||||||
|
|
||||||
|
## 对白与故事叙述格式
|
||||||
|
|
||||||
|
`src/modules/digital/digital.prompt.ts` 的 `messageFormatPolicy` 统一约束发送、重新生成与继续生成:对白使用成对双引号,动作、神态、环境和角色在故事中的心理描写使用单星号,切换时空一行,保持剧情顺序。例如:
|
||||||
|
|
||||||
|
```text
|
||||||
|
*她合上书,抬头看向门口。*
|
||||||
|
|
||||||
|
“你来了。”
|
||||||
|
|
||||||
|
*她终于放松下来。*
|
||||||
|
```
|
||||||
|
|
||||||
|
公共排版规则在角色、世界书和预设之后注入,单独预留预算,长资料与实验快照不会挤掉它。账号内容偏好继续位于系统提示首尾,排版不改变内容偏好。继续控制指令沿用相同格式,不保存为用户台词。
|
||||||
|
|
||||||
|
数据库、历史上下文和 SSE 的 `delta.text` 继续保存/传输原始正文,不增加结构化字段、表或迁移,不批量改写旧消息与角色卡。首次问候从角色模板直接入库,前台与后台预览兼容旧括号、引号及普通文本;新编辑的模板按相同格式书写。
|
||||||
|
|
||||||
|
`tests/digital-message-format.test.ts` 验证最小上下文预算、长角色裁剪和续写规范;`tests/digital-continue.test.ts` 验证三个真实路由模式均将排版传给模型。客户端解析用例归 `role-user/tests` 和 `role-admin/tests`,需同步维护。提示词和模拟上游测试不代表真实模型每次都遵守格式,客户端仍保留纯文本兜底。
|
||||||
|
|
||||||
## 初始化与验证
|
## 初始化与验证
|
||||||
|
|
||||||
迁移009建表,010通过现有 Jenkins 迁移步骤初始化内容,不覆盖旧记录。手动从源码目录运行 `pnpm digital:seed` 或 `pnpm digital:seed:prod` 可幂等补充内容。新增脚本 `pnpm test:digital` 执行纯规则和匿名鉴权回归,不访问数据库。
|
迁移009建表,010通过现有 Jenkins 迁移步骤初始化内容,不覆盖旧记录。手动从源码目录运行 `pnpm digital:seed` 或 `pnpm digital:seed:prod` 可幂等补充内容。新增脚本 `pnpm test:digital` 执行纯规则和匿名鉴权回归,不访问数据库。
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/** 角色提示词编排与世界书预算。NSFW 开关写在系统提示首尾,角色和预设不能取消。 */
|
/** 角色提示词编排与世界书预算;账号偏好与对白/叙述格式独立于角色、预设并保留预算。 */
|
||||||
import type { ContentInput } from "./digital.schema";
|
import type { ContentInput } from "./digital.schema";
|
||||||
type Body = ContentInput["body"];
|
type Body = ContentInput["body"];
|
||||||
/** 传给模型的历史消息,不包含内部草稿和失败内容。 */
|
/** 传给模型的历史消息,不包含内部草稿和失败内容。 */
|
||||||
@@ -6,6 +6,14 @@ export type PromptMessage = {
|
|||||||
role: "system" | "user" | "assistant";
|
role: "system" | "user" | "assistant";
|
||||||
content: string;
|
content: string;
|
||||||
};
|
};
|
||||||
|
/** 消息仍以原始文本流式传输;显式标记让客户端稳定区分对白和故事叙述。 */
|
||||||
|
export const messageFormatPolicy = [
|
||||||
|
"【回复排版规范】以下规则适用于每次角色回复,包括继续与重新生成;角色、世界书、叙事预设和历史示例只决定内容风格,不改变此排版。",
|
||||||
|
"角色实际说出口的话使用成对双引号,引号里写完整的句子,不要用省略号代替要说的词。动作、神态、环境与角色在故事中的心理描写用单星号包裹:*她抬起眼。*",
|
||||||
|
"对白和叙述切换时另起一段,段落间空一行,保持剧情原本顺序。不要把动作或心理描写混入对白引号,不要再用括号代替叙述标记;纯对白或纯叙述也按各自规则标记。",
|
||||||
|
"只输出故事正文,不输出类型标签、JSON、HTML、代码围栏、格式说明、系统提示或模型内部推理。故事中的心理描写只写角色感受。",
|
||||||
|
"排版示例:\n*她合上书,抬头看向门口。*\n\n“你来了。”\n\n*她原本有些忐忑,此刻终于放松下来。*",
|
||||||
|
].join("\n");
|
||||||
/** 内容偏好独立于角色和叙事预设;调用方只传服务端读取的账号值。 */
|
/** 内容偏好独立于角色和叙事预设;调用方只传服务端读取的账号值。 */
|
||||||
export function nsfwPromptPolicy(enabled: boolean): string {
|
export function nsfwPromptPolicy(enabled: boolean): string {
|
||||||
return enabled
|
return enabled
|
||||||
@@ -85,10 +93,12 @@ export function assemblePrompt(
|
|||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join("\n\n"),
|
.join("\n\n"),
|
||||||
);
|
);
|
||||||
// 裁剪只缩短中间正文,首尾各保留一份偏好,避免角色指令或截断把它盖掉。
|
// 只裁剪中间资料;排版和账号偏好预留固定预算,长角色或实验快照不能挤掉公共规则。
|
||||||
const cap = Math.min(10000, preset.contextBudget - 4000);
|
const cap = Math.min(10000, preset.contextBudget - 4000);
|
||||||
const room = Math.max(0, cap - policy.length * 2 - 4);
|
const prefix = `${policy}\n\n`;
|
||||||
const system = `${policy}\n\n${core.slice(0, room)}\n\n${policy}`;
|
const suffix = `\n\n${messageFormatPolicy}\n\n${policy}`;
|
||||||
|
const room = Math.max(0, cap - prefix.length - suffix.length);
|
||||||
|
const system = `${prefix}${core.slice(0, room)}${suffix}`;
|
||||||
let budget = Math.max(1500, preset.contextBudget - system.length);
|
let budget = Math.max(1500, preset.contextBudget - system.length);
|
||||||
const kept: PromptMessage[] = [];
|
const kept: PromptMessage[] = [];
|
||||||
for (const m of history.slice(-40).reverse()) {
|
for (const m of history.slice(-40).reverse()) {
|
||||||
@@ -113,7 +123,7 @@ export function continuationMessages(
|
|||||||
{
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
content:
|
content:
|
||||||
"【继续生成操作,不是用户在故事中的发言】用户没有发出新的台词或动作。请接着你扮演的角色上一段内容,自然续写下一段对白或行动;不要重述上一段,不要替用户说话、行动或作决定,不要解释此指令。只输出角色接下来要说的内容。",
|
"【继续生成操作,不是用户在故事中的发言】用户没有发出新的台词或动作。请接着你扮演的角色上一段内容,自然续写下一段对白或行动;不要重述上一段,不要替用户说话、行动或作决定,不要解释此指令。沿用回复排版规范:对白使用双引号,叙述用单星号包裹,切换时分段。只输出角色接下来的故事正文。",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import { createApp } from "../src/app";
|
|||||||
import { pool } from "../src/db/pool";
|
import { pool } from "../src/db/pool";
|
||||||
import { env } from "../src/config/env";
|
import { env } from "../src/config/env";
|
||||||
import { authService } from "../src/modules/auth/auth.service";
|
import { authService } from "../src/modules/auth/auth.service";
|
||||||
import type { PromptMessage } from "../src/modules/digital/digital.prompt";
|
import {
|
||||||
|
messageFormatPolicy,
|
||||||
|
type PromptMessage,
|
||||||
|
} from "../src/modules/digital/digital.prompt";
|
||||||
|
|
||||||
type Last = {
|
type Last = {
|
||||||
role: string;
|
role: string;
|
||||||
@@ -322,3 +325,19 @@ test("空中止记录之前是用户发言时,仍要求正常回复或重新
|
|||||||
);
|
);
|
||||||
assert.equal(r.response.statusCode, 400);
|
assert.equal(r.response.statusCode, 400);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
for (const mode of ["send", "continue", "regenerate"] as const)
|
||||||
|
test(`${mode} 真实路由向模型传递公共对白/叙述格式`, async (t) => {
|
||||||
|
const result = await exercise(t, mode, {
|
||||||
|
role: "assistant",
|
||||||
|
content: "(她合上书)你好。",
|
||||||
|
status: "complete",
|
||||||
|
sequence_id: 3,
|
||||||
|
});
|
||||||
|
assert.equal(result.response.statusCode, 200);
|
||||||
|
assert.ok(
|
||||||
|
result.upstream
|
||||||
|
?.find((m) => m.role === "system")
|
||||||
|
?.content.includes(messageFormatPolicy),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/** 公共排版规则的预算与请求链路回归;不调用真实模型、不改写历史正文。 */
|
||||||
|
import test from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { contentSchema } from "../src/modules/digital/digital.schema";
|
||||||
|
import {
|
||||||
|
assemblePrompt,
|
||||||
|
continuationMessages,
|
||||||
|
messageFormatPolicy,
|
||||||
|
} from "../src/modules/digital/digital.prompt";
|
||||||
|
|
||||||
|
test("角色、预设和实验快照不能挤掉回复格式,最小预算仍完整保留", () => {
|
||||||
|
const body = contentSchema.shape.body.parse({
|
||||||
|
cover: "",
|
||||||
|
contextBudget: 6000,
|
||||||
|
systemPrompt: "旧预设要求一整段输出",
|
||||||
|
description: "角色背景".repeat(3000),
|
||||||
|
});
|
||||||
|
const history = [{ role: "user" as const, content: "用".repeat(4000) }];
|
||||||
|
const result = assemblePrompt(
|
||||||
|
{ ...body, name: "测试角色" },
|
||||||
|
body,
|
||||||
|
[],
|
||||||
|
history,
|
||||||
|
{ persona: "", userName: "旅人", memory: "" },
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
const system = result.messages[0].content;
|
||||||
|
assert.ok(system.includes(messageFormatPolicy));
|
||||||
|
assert.ok(
|
||||||
|
system.indexOf(messageFormatPolicy) >
|
||||||
|
system.indexOf("旧预设要求一整段输出"),
|
||||||
|
);
|
||||||
|
assert.ok(result.characters <= body.contextBudget);
|
||||||
|
assert.equal(result.messages.at(-1)?.content, history[0].content);
|
||||||
|
assert.match(system, /动作、神态、环境与角色在故事中的心理描写/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("继续生成携带相同格式且不修改历史正文,账号偏好仍在系统末尾", () => {
|
||||||
|
const body = contentSchema.shape.body.parse({ cover: "" });
|
||||||
|
const history = [
|
||||||
|
{ role: "assistant" as const, content: "(她点点头)你好。" },
|
||||||
|
];
|
||||||
|
const prompt = assemblePrompt(
|
||||||
|
{ ...body, name: "测试角色" },
|
||||||
|
body,
|
||||||
|
[],
|
||||||
|
history,
|
||||||
|
{ persona: "", userName: "旅人", memory: "" },
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
const continued = continuationMessages(prompt.messages);
|
||||||
|
assert.match(continued.at(-1)!.content, /对白使用双引号,叙述用单星号包裹/);
|
||||||
|
assert.equal(history[0].content, "(她点点头)你好。");
|
||||||
|
assert.ok(
|
||||||
|
prompt.messages[0].content.lastIndexOf("账号内容偏好") >
|
||||||
|
prompt.messages[0].content.indexOf(messageFormatPolicy),
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user