1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-prompt-settings.int.spec.ts
Z8MB1E de27a53044 feat(npc-dialogue): make the AI prompt voice configurable
The system prompt was a single constant, so tuning how drafted dialogue
sounds meant editing code and redeploying. Split it into the half that is
copy and the half that is a contract.

VOICE and optional HOUSE RULES are now editable from a new
`npc-dialogue-settings` global, and per conversation through an
`voiceOverride` field on `npc-dialogues`. Precedence is dialogue, then
global, then the built-in default, resolved by the pure `resolveVoice`.
STRUCTURE, FIELDS, STATE, OUTPUT and the worked example stay in
`LOCKED_RULES`: the draft validator enforces the same rules, so an edit
there would make every draft fail for reasons an author cannot trace.

Both surfaces render the assembled prompt through the same pure
composer the server calls, so the preview cannot drift from what the
model receives. The global normalises dashes on save, bumps `revision`
only on a real text change, and keeps 20 versions for rollback.

Provenance gains `promptSource` and `promptRevision` so a stored draft
still names the voice that shaped it. A draft that fails twice while a
custom voice is active now says so, instead of looking like a model bug.

`composeDialogueSystemPrompt()` with no arguments reproduces the old
prompt byte for byte, pinned by a golden fixture.
2026-11-24 12:53:33 -05:00

178 lines
6.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
composeDialogueSystemPrompt,
composeRewriteSystemPrompt,
DEFAULT_VOICE,
LOCKED_RULES,
REWRITE_BASE_SYSTEM,
resolveVoice,
} from "@/lib/npc-dialogue/generationPrompt";
import { cleanPromptText, normalizeDashes } from "@/lib/npc-dialogue/normalize";
/**
* The prompt split is the riskiest kind of refactor: it moves a string that a
* model's behaviour depends on. The golden fixture below is the prompt as it was
* before the voice became configurable, so any accidental whitespace or wording
* drift fails here instead of silently changing every draft.
*
* When the prompt is changed on purpose, update this fixture in the same commit
* and bump PROMPT_VERSION.
*/
const ORIGINAL_PROMPT = `You write conversations for NPCs in an Arma 3 military unit roleplay.
VOICE
Write like a soldier with limited patience: concrete, physical, unsentimental. Short sentences. No fantasy vendor patter, no exclamation marks, no em-dashes.
Each NPC has an occupation and a place. Let that show in what they notice and what they dismiss.
Never write a line that explains the mechanics to the player.
STRUCTURE
Exactly one node has "isStart": true, and it is the opening line.
Every "target" and "nextTarget" must name an id that exists in the same output.
choice nodes need two or three choices, each with short button text.
Use an end node to close a branch. Do not use delay nodes.
Keep the conversation between the requested size bounds.
FIELDS
Fields not used by a node's type must be empty: "" for strings, [] for arrays, 0 for numbers, "is_true" and "set_true" for the enum defaults.
"body" carries the spoken line. "leadIn" carries the situation text above a choice. "closing" carries the last line before the conversation ends.
Leave "speaker" empty so the NPC's own name is used.
STATE
Use only the player flag keys listed in the brief. Never invent a flag key: a branch on a flag that does not exist never fires.
Allowed effects are "none", "notify" and "flag". You may not log game events, post to Discord, or grant rewards.
OUTPUT
Return one JSON object matching the provided schema. No commentary, no code fences.`;
describe("composeDialogueSystemPrompt", () => {
it("reproduces the original prompt exactly when nothing is configured", () => {
expect(composeDialogueSystemPrompt()).toBe(ORIGINAL_PROMPT);
expect(composeDialogueSystemPrompt({})).toBe(ORIGINAL_PROMPT);
// Whitespace-only input means "unset", not "an empty voice section".
expect(composeDialogueSystemPrompt({ voice: " ", extraRules: "\n\t " })).toBe(
ORIGINAL_PROMPT,
);
});
it("replaces the voice and keeps every locked section", () => {
const prompt = composeDialogueSystemPrompt({ voice: "Grunt, short, no ceremony." });
expect(prompt).toContain("VOICE\nGrunt, short, no ceremony.");
expect(prompt).not.toContain(DEFAULT_VOICE);
// The contract is what the validator enforces; it must survive any voice.
expect(prompt).toContain(LOCKED_RULES);
expect(prompt).toContain("STRUCTURE");
expect(prompt).toContain("STATE");
expect(prompt).toContain("OUTPUT");
});
it("appends house rules between the voice and the locked rules", () => {
const prompt = composeDialogueSystemPrompt({
voice: "Clipped.",
extraRules: "Never use the word 'sir' as a greeting.",
});
expect(prompt).toContain("Never use the word 'sir' as a greeting.");
// Order matters: house rules are voice-adjacent, before the contract.
expect(prompt.indexOf("HOUSE RULES")).toBeLessThan(prompt.indexOf("STRUCTURE"));
expect(prompt.indexOf("VOICE")).toBeLessThan(prompt.indexOf("HOUSE RULES"));
});
it("supports house rules without a custom voice", () => {
const prompt = composeDialogueSystemPrompt({ extraRules: "Refer to the unit as the Task Force." });
expect(prompt).toContain(DEFAULT_VOICE);
expect(prompt).toContain("HOUSE RULES\nRefer to the unit as the Task Force.");
});
});
describe("composeRewriteSystemPrompt", () => {
it("is the plain instruction when no custom voice is set", () => {
expect(composeRewriteSystemPrompt()).toBe(REWRITE_BASE_SYSTEM);
// The built-in voice is already implied by the base instruction, so the
// default never duplicates it.
expect(composeRewriteSystemPrompt({ voice: null })).toBe(REWRITE_BASE_SYSTEM);
});
it("appends a custom voice so rewrites match drafts", () => {
const system = composeRewriteSystemPrompt({ voice: "Drawling, unhurried." });
expect(system.startsWith(REWRITE_BASE_SYSTEM)).toBe(true);
expect(system).toContain("HOUSE VOICE\nDrawling, unhurried.");
});
});
describe("resolveVoice", () => {
it("prefers the dialogue override", () => {
const resolved = resolveVoice({
dialogueVoice: "This NPC only.",
globalVoice: "Whole unit.",
globalExtraRules: "House rules.",
globalRevision: 7,
});
expect(resolved.source).toBe("dialogue");
expect(resolved.voice).toBe("This NPC only.");
expect(resolved.revision).toBe(7);
});
it("falls back to the global voice", () => {
const resolved = resolveVoice({
dialogueVoice: " ",
globalVoice: "Whole unit.",
globalExtraRules: null,
globalRevision: 3,
});
expect(resolved.source).toBe("global");
expect(resolved.voice).toBe("Whole unit.");
expect(resolved.revision).toBe(3);
});
it("falls back to the built-in voice and carries no revision", () => {
const resolved = resolveVoice({ dialogueVoice: null, globalVoice: null });
expect(resolved.source).toBe("builtin");
expect(resolved.voice).toBeNull();
expect(resolved.revision).toBeNull();
});
it("always takes extra rules from the global, never from the dialogue", () => {
const resolved = resolveVoice({
dialogueVoice: "Per dialogue.",
globalExtraRules: "Unit-wide convention.",
globalRevision: 4,
});
expect(resolved.extraRules).toBe("Unit-wide convention.");
});
});
describe("normalizeDashes", () => {
it("turns a spaced dash into a comma", () => {
expect(normalizeDashes("Hold position \u2014 wait for orders.")).toBe(
"Hold position, wait for orders.",
);
});
it("turns an unspaced em-dash into a comma", () => {
expect(normalizeDashes("Rifles\u2014mine are empty.")).toBe("Rifles, mine are empty.");
});
it("keeps a range readable as a hyphen", () => {
expect(normalizeDashes("1\u201320 rounds")).toBe("1-20 rounds");
});
});
describe("cleanPromptText", () => {
it("trims, normalises dashes, and treats blank as unset", () => {
expect(cleanPromptText(" Short \u2014 blunt. ", 100)).toBe("Short, blunt.");
expect(cleanPromptText(" ", 100)).toBeNull();
expect(cleanPromptText(undefined, 100)).toBeNull();
expect(cleanPromptText(42, 100)).toBeNull();
});
it("caps over-long input instead of rejecting it", () => {
const capped = cleanPromptText("x".repeat(50), 10);
expect(capped).toHaveLength(10);
});
});