预设兼容:按真实抓包对齐装配与包装层,补上声明式工具与预设正则。

- 装配只认 prompt_order.enabled;squash_system_messages 只合并相邻 system(新增 system 档,连接符按酒馆用单换行)
- 包装层:new_chat_prompt、示例对话(new_example_chat_prompt + <START> 切段 + name)、角色卡三块与 personality_format/scenario_format、{{user}}/{{char}} 取值、任务契约并进预设条目
- 工具协议:按 ToolBindings / 伪工具协议数据声明真 tools(presetToolPolicy=preset),工具描述用到的条目不再重复作为消息发出
- 正则:promptOnly 作用于对话历史(含 depth 过滤)、写入时脚本改写回复;{{//}} 注释按配平扫描
- tools/st-request-diff:真假上游抓包对比工具、三份预设的机制结论与实测报告
This commit is contained in:
2026-10-23 00:13:08 +08:00
parent ed09e5c831
commit 9dc927282f
94 changed files with 11558 additions and 413 deletions
+113 -11
View File
@@ -1,12 +1,19 @@
import { describe, expect, it } from "vitest";
import type { ChatMessage } from "../src/llm/client.js";
import { importSillyTavernPreset } from "../src/preset/importer.js";
import { assemblePlayWorkerMessages } from "../src/preset/play-frame.js";
import { assemblePresetMessages } from "../src/preset/assembler.js";
import {
assemblePlayWorkerMessages,
shapeFramedMessages,
} from "../src/preset/play-frame.js";
import { assemblePresetMessages, PRESET_HISTORY_MARK } from "../src/preset/assembler.js";
import type { WorldInfoPack } from "../src/preset/world-info-pack.js";
const pack: WorldInfoPack = {
worldBookBefore: [{ id: "lore", name: "舞台", content: "古镇夜雨" }],
history: "用户:昨天来过",
history: [
{ role: "assistant", content: "昨夜灯灭" },
{ role: "user", content: "昨天来过" },
],
worldBookAfter: [{ id: "vars", name: "变量", content: "HP 3" }],
turn: "我推门",
postTurn: [{ id: "tail", name: "尾注", content: "勿剧透" }],
@@ -52,12 +59,14 @@ describe("assemblePlayWorkerMessages", () => {
});
expect(messages.map((m) => [m.role, m.content])).toEqual([
["system", "MAIN"],
// 任务契约并进 main,不再单独一条
["system", "MAIN\n\nSKILL"],
[
"system",
"### 任务契约\n\nSKILL\n\n### 舞台\n\n古镇夜雨",
"### 舞台\n\n古镇夜雨",
],
["system", "用户:昨天来过"],
["assistant", "昨夜灯灭"],
["user", "昨天来过"],
["system", "### 变量\n\nHP 3"],
["user", "我推门"],
["system", "PHI"],
@@ -103,7 +112,7 @@ describe("assemblePlayWorkerMessages", () => {
preset: report.preset,
pack: {
worldBookBefore: [{ id: "lore", name: "设定", content: "港口" }],
history: "昨日对话",
history: [{ role: "user", content: "昨日对话" }],
worldBookAfter: [],
turn: "本轮",
postTurn: [],
@@ -117,7 +126,7 @@ describe("assemblePlayWorkerMessages", () => {
).toBe(true);
});
it("does not fill ST charDescription even if resolver returns content", () => {
it("passes ST card markers to the resolver (play-frame gates filling on the card slot)", () => {
const report = importSillyTavernPreset({
prompts: [
{
@@ -148,7 +157,8 @@ describe("assemblePlayWorkerMessages", () => {
if (id === "chatHistory") return "HIST";
return null;
});
expect(messages.map((m) => m.content)).toEqual(["HIST"]);
// 这里测的是装配器本身:marker 会交给 resolver,填不填由 play-frame 的槽位决定。
expect(messages.map((m) => m.content)).toEqual(["CARD", "HIST"]);
});
it("fills personaDescription and expands @玩家 with the given persona", () => {
@@ -189,7 +199,7 @@ describe("assemblePlayWorkerMessages", () => {
preset: report.preset,
pack: {
worldBookBefore: [],
history: "昨日",
history: [{ role: "assistant", content: "昨日" }],
worldBookAfter: [],
turn: "本轮",
postTurn: [],
@@ -197,6 +207,98 @@ describe("assemblePlayWorkerMessages", () => {
persona: { name: "@玩家", description: "占位人设" },
});
expect(messages.some((m) => m.content === "占位人设")).toBe(true);
expect(messages.some((m) => m.content === "对 @玩家 说话")).toBe(true);
expect(
messages.some((m) => String(m.content).startsWith("对 @玩家 说话")),
).toBe(true);
});
it("injects injection_position=1 entries into the history at injection_depth", () => {
const report = importSillyTavernPreset({
prompts: [
{ identifier: "main", name: "Main", role: "system", content: "MAIN" },
{
identifier: "chatHistory",
name: "History",
role: "system",
marker: true,
},
{
identifier: "deep",
name: "深度破限",
role: "system",
content: "DEEP",
injection_position: 1,
injection_depth: 1,
},
],
prompt_order: [
{
character_id: 1,
order: [
{ identifier: "main", enabled: true },
{ identifier: "chatHistory", enabled: true },
{ identifier: "deep", enabled: true },
],
},
],
});
const messages = assemblePlayWorkerMessages({
systemPrompt: "",
preset: report.preset,
pack: {
worldBookBefore: [],
history: [
{ role: "assistant", content: "昨夜灯灭" },
{ role: "user", content: "昨天来过" },
],
worldBookAfter: [],
turn: "我推门",
postTurn: [],
},
persona: null,
});
expect(messages.map((m) => [m.role, m.content])).toEqual([
["system", "MAIN"],
["assistant", "昨夜灯灭"],
["system", "DEEP"],
["user", "昨天来过"],
["user", "我推门"],
]);
});
it("merges consecutive same-role history turns but keeps them as turns", () => {
const messages: ChatMessage[] = [
{ role: "system", content: "A" },
{ role: "system", content: "B" },
{ role: "assistant", content: "旧1" },
{ role: "user", content: "旧2" },
{ role: "user", content: "旧3(助手当时没出正文)" },
{ role: "system", content: "DEEP" },
{ role: "assistant", content: "旧4" },
{ role: "system", content: "C" },
{ role: "user", content: "本轮" },
];
for (const index of [2, 3, 4, 5, 6]) {
Object.assign(messages[index] as object, { [PRESET_HISTORY_MARK]: true });
}
const shaped = shapeFramedMessages(messages, "strict");
const roles = shaped.map((m) => m.role);
// 历史还是多轮(没被揉成一条),且连发的两条用户合并为一条
expect(roles).toContain("assistant");
expect(shaped.filter((m) => m.role === "user" && m.content.includes("旧2")))
.toHaveLength(1);
expect(
shaped.some(
(m) => m.role === "user" && m.content.includes("旧2") && m.content.includes("旧3"),
),
).toBe(true);
// 深度插入的 system 不被折掉
expect(shaped.some((m) => m.content === "DEEP")).toBe(true);
// 史前 system 仍按 strict 处理(没出现第二条 user 以外的奇形)
expect(shaped.at(-1)!.role).toBe("user");
});
});