import { describe, expect, it } from "vitest"; import type { ChatMessage } from "../src/llm/client.js"; import { importSillyTavernPreset } from "../src/preset/importer.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: [ { role: "assistant", content: "昨夜灯灭" }, { role: "user", content: "昨天来过" }, ], worldBookAfter: [{ id: "vars", name: "变量", content: "HP 3" }], turn: "我推门", postTurn: [{ id: "tail", name: "尾注", content: "勿剧透" }], }; describe("assemblePlayWorkerMessages", () => { it("walks extended prompt_order: ST pre, our before, history, our after, turn, ST post, postTurn", () => { const report = importSillyTavernPreset({ prompts: [ { identifier: "main", name: "Main", role: "system", content: "MAIN" }, { identifier: "charDescription", name: "Desc", role: "system", marker: true, }, { identifier: "chatHistory", name: "History", role: "system", marker: true, }, { identifier: "jailbreak", name: "PHI", role: "system", content: "PHI" }, ], prompt_order: [ { character_id: 1, order: [ { identifier: "main", enabled: true }, { identifier: "charDescription", enabled: true }, { identifier: "chatHistory", enabled: true }, { identifier: "jailbreak", enabled: true }, ], }, ], }); const messages = assemblePlayWorkerMessages({ systemPrompt: "SKILL", preset: report.preset, pack, persona: null, }); expect(messages.map((m) => [m.role, m.content])).toEqual([ // 任务契约并进 main,不再单独一条 ["system", "MAIN\n\nSKILL"], [ "system", "### 舞台\n\n古镇夜雨", ], ["assistant", "昨夜灯灭"], ["user", "昨天来过"], ["system", "### 变量\n\nHP 3"], ["user", "我推门"], ["system", "PHI"], ["system", "### 尾注\n\n勿剧透"], ]); }); it("does not emit any message before the first preset order entry", () => { const report = importSillyTavernPreset({ prompts: [ { identifier: "wb-head", name: "世界书首", role: "system", content: "<世界书>", }, { identifier: "chatHistory", name: "History", role: "system", marker: true, }, { identifier: "wb-tail", name: "世界书尾", role: "system", content: "", }, ], prompt_order: [ { character_id: 1, order: [ { identifier: "wb-head", enabled: true }, { identifier: "chatHistory", enabled: true }, { identifier: "wb-tail", enabled: true }, ], }, ], }); const messages = assemblePlayWorkerMessages({ systemPrompt: "创作步骤契约", preset: report.preset, pack: { worldBookBefore: [{ id: "lore", name: "设定", content: "港口" }], history: [{ role: "user", content: "昨日对话" }], worldBookAfter: [], turn: "本轮", postTurn: [], }, persona: null, }); expect(messages[0]).toEqual({ role: "system", content: "<世界书>" }); expect(messages.some((m) => m.content === "创作步骤契约")).toBe(false); expect( messages.some((m) => m.content.includes("### 任务契约\n\n创作步骤契约")), ).toBe(true); }); it("passes ST card markers to the resolver (play-frame gates filling on the card slot)", () => { const report = importSillyTavernPreset({ prompts: [ { identifier: "charDescription", name: "Desc", role: "system", marker: true, }, { identifier: "chatHistory", name: "History", role: "system", marker: true, }, ], prompt_order: [ { character_id: 1, order: [ { identifier: "charDescription", enabled: true }, { identifier: "chatHistory", enabled: true }, ], }, ], }); const messages = assemblePresetMessages(report.preset, (id) => { if (id === "charDescription") return "CARD"; if (id === "chatHistory") return "HIST"; return null; }); // 这里测的是装配器本身:marker 会交给 resolver,填不填由 play-frame 的槽位决定。 expect(messages.map((m) => m.content)).toEqual(["CARD", "HIST"]); }); it("fills personaDescription and expands @玩家 with the given persona", () => { const report = importSillyTavernPreset({ prompts: [ { identifier: "personaDescription", name: "Persona", role: "system", marker: true, }, { identifier: "main", name: "Main", role: "system", content: "对 @玩家 说话", }, { identifier: "chatHistory", name: "History", role: "system", marker: true, }, ], prompt_order: [ { character_id: 1, order: [ { identifier: "personaDescription", enabled: true }, { identifier: "main", enabled: true }, { identifier: "chatHistory", enabled: true }, ], }, ], }); const messages = assemblePlayWorkerMessages({ systemPrompt: "SKILL", preset: report.preset, pack: { worldBookBefore: [], history: [{ role: "assistant", content: "昨日" }], worldBookAfter: [], turn: "本轮", postTurn: [], }, persona: { name: "@玩家", description: "占位人设" }, }); 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"); }); });