1
0
Fork 0
polaris-task-force/src/collections/game/NpcDialogueSettings.ts
Z8MB1E 7adfcde72d fix(npc-dialogue): keep the conversation reachable when a branch attaches
Review findings, all in code this range introduced.

Attaching a branch to a node that already had a next step replaced that
hop, and "come back where it attached" pointed the branch back at the
caller, which loops: the caller then leads into the branch it just
returned from. Everything downstream was orphaned. The branch now
continues into the step that hop used to point at, so attaching inserts
in front of it instead of deleting it. Rejoining a condition is refused
outright, because re-entering re-evaluates the same flags and retakes the
same branch. Return candidates are limited to node types that read
`next`: a choice or condition open end would have received a hop no
walker follows.

The dialog says which of the three applies, and only offers the toggle
when a return exists, so an untouched default is not sent and reported
back as a refusal.

Condition true/false dots were drawn on the input handle: the hit-area
class sets left and right on the handle itself, which over-constrains
React Flow's `right: 0`, so both dots drifted to the card's left edge.
They now take the hit area as a child, like the other handles.

Refusals are honest: `connectHop` reports `changed: false` for a write it
did not make (it used to report true and return a new array), the editor
explains instead of rewriting an identical graph, and a refused
tail-drag keeps the original hop rather than deleting it first.

A passing connection test now really is invalidated when the
configuration changes. That ran in `afterChange`, whose return value is
only the operation's response, so the stored row kept showing a stale
pass while the test asserted the response. It moved to `beforeChange`,
counts only fields actually submitted (so the Test button's own result
write does not invalidate itself), and the test reads the row back.

Also: the fragment prompt no longer teaches `isStart: true` through the
worked example or asks for "the whole conversation" on retry; requests
carry a ten minute ceiling so an unreachable-but-accepting host cannot
hang Generate, Compose or the Test button; the API key description no
longer claims at-rest encryption, which does not exist; the per-dialogue
voice override gets the same dash and length cleaning as the global it
outranks; and the unused `describeAttachmentPoint` is gone, with the
attachment output now included in the hint the model receives.
2026-11-24 14:47:08 -05:00

102 lines
3.5 KiB
TypeScript

import type { GlobalConfig } from "payload";
import { requirePermission } from "@/utils/access-control/hasPermission";
import {
cleanPromptText,
MAX_EXTRA_RULES_CHARS,
MAX_VOICE_CHARS,
} from "@/lib/npc-dialogue/normalize";
/**
* Admin-editable half of the AI dialogue prompt.
*
* The prompt has two halves. The voice is copy: register, personality, house
* quirks, and it is safe for an author to change. The rest (structure, field
* conventions, flag rules, the JSON contract) is enforced again by
* `generationSchema.ts` and `validateGeneratedFlow`, so it stays in code: a
* prompt that disagrees with the validator makes every draft fail for reasons the
* author cannot trace.
*
* Because this is a Payload global, an edit here takes effect on the next draft
* with no build and no restart. `versions` keeps the last 20 texts so a bad edit
* can be reverted from the admin panel.
*/
export const NpcDialogueSettings: GlobalConfig = {
slug: "npc-dialogue-settings",
label: "NPC Dialogue Prompt",
admin: {
description:
"How AI-drafted NPC conversations sound. Changes apply to the next draft, with no deploy.",
},
versions: {
max: 20,
},
access: {
read: requirePermission("npc-dialogues:read"),
update: requirePermission("npc-dialogues:update"),
},
hooks: {
beforeChange: [
({ data, originalDoc }) => {
// Cap rather than reject: an over-long paste should still save something
// usable instead of failing the form with no explanation.
const voice = cleanPromptText(data?.voice, MAX_VOICE_CHARS);
const extraRules = cleanPromptText(data?.extraRules, MAX_EXTRA_RULES_CHARS);
const voiceChanged = (originalDoc?.voice ?? null) !== voice;
const extraChanged = (originalDoc?.extraRules ?? null) !== extraRules;
const previousRevision =
typeof originalDoc?.revision === "number" ? originalDoc.revision : null;
// Only a real text change bumps the revision, so re-saving the form does
// not make every stored draft look like it came from a different prompt.
const revision =
previousRevision === null
? 1
: voiceChanged || extraChanged
? previousRevision + 1
: previousRevision;
return { ...data, voice, extraRules, revision };
},
],
},
fields: [
{
name: "voice",
type: "textarea",
admin: {
description:
"Replaces the built-in voice for every conversation that does not set its own. Leave blank to use the built-in. Write about register and personality only: structure and field rules are not editable here, because the draft validator enforces them. Dashes are converted to plain punctuation on save.",
},
},
{
name: "extraRules",
type: "textarea",
admin: {
description:
"House conventions appended after the voice, for every conversation including ones with their own voice. Keep it short and concrete, e.g. how the unit refers to itself or which slang is banned.",
},
},
{
name: "revision",
type: "number",
admin: {
readOnly: true,
description:
"Incremented automatically when the voice or house rules change. Recorded on each stored draft so you can tell which prompt produced it.",
},
},
{
name: "preview",
type: "ui",
admin: {
components: {
Field: "@/components/admin/npc-dialogue/PromptPreview#GlobalPromptPreview",
},
},
},
],
};