1
0
Fork 0
polaris-task-force/src/collections/game/NpcDialogues.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

216 lines
6.9 KiB
TypeScript

import { CollectionConfig } from "payload";
import { requirePermission } from "@/utils/access-control/hasPermission";
import { CONDITION_OPS } from "@/lib/npc-dialogue/dialogue";
import { cleanPromptText, MAX_VOICE_CHARS } from "@/lib/npc-dialogue/normalize";
export const NpcDialogues: CollectionConfig = {
slug: "npc-dialogues",
admin: {
group: "Game",
useAsTitle: "name",
listSearchableFields: ["name"],
defaultColumns: ["name", "npc", "priority"],
pagination: { defaultLimit: 50 },
},
access: {
read: requirePermission("npc-dialogues:read"),
create: requirePermission("npc-dialogues:create"),
update: requirePermission("npc-dialogues:update"),
delete: requirePermission("npc-dialogues:delete"),
},
fields: [
{
name: "name",
type: "text",
required: true,
admin: {
description: "A short label for this conversation (e.g. 'Merchant greeting').",
},
},
{
name: "npc",
type: "relationship",
relationTo: "game-npcs",
required: true,
hasMany: false,
admin: {
description: "The contact this conversation belongs to.",
},
filterOptions: {
canMessage: { equals: true },
},
},
{
name: "priority",
type: "number",
defaultValue: 0,
admin: {
description:
"When a contact has several conversations, the highest-priority one whose entry gate passes is the one offered. Ties break by oldest first.",
},
},
{
name: "entryGate",
type: "group",
admin: {
description:
"Only offer this conversation to players whose flags pass this condition. Leave the key empty to always offer it (ungated conversations are the fallback).",
},
fields: [
{
name: "key",
type: "text",
admin: {
description:
"player-flags key checked for the reading player. Player-specific flag takes priority; falls back to global. Missing = false / 0.",
},
},
{
name: "op",
type: "select",
defaultValue: "is_true",
options: CONDITION_OPS,
admin: {
description: "Comparison applied to the flag's effective value.",
},
},
{
name: "amount",
type: "number",
admin: {
condition: (_data, blockData) =>
["gte", "lte", "gt", "lt", "eq"].includes(String(blockData?.op)),
description: "Right-hand side for the numeric comparisons.",
},
},
],
},
{
type: "tabs",
tabs: [
{
label: "Flow Editor",
description:
"Build the conversation: dialogue lines, branching choices, and trigger events. Mark one node as the start.",
fields: [
{
name: "flowData",
type: "json",
required: true,
defaultValue: () => ({
nodes: [],
edges: [],
}),
admin: {
components: {
Field: "@/components/admin/npc-dialogue/NpcDialogueEditor",
},
},
},
],
},
{
label: "AI Drafting",
description:
"How the drafting tools should sound for this conversation. The preview shows the assembled instruction for a whole-conversation draft; a generated branch swaps only the structure section.",
fields: [
{
name: "voiceOverride",
type: "textarea",
hooks: {
// The same cleaning the global voice gets: this text outranks it,
// so leaving dashes and unbounded length here would make the most
// specific instruction the least constrained one.
beforeValidate: [({ value }) => cleanPromptText(value, MAX_VOICE_CHARS)],
},
admin: {
description:
"Replaces the global voice for this conversation only. Leave blank to use the global setting, or the built-in voice if that is blank too. Write about register and personality only: structure and field rules are fixed in code.",
},
},
{
name: "promptPreview",
type: "ui",
admin: {
components: {
Field: "@/components/admin/npc-dialogue/PromptPreview#DialoguePromptPreview",
},
},
},
],
},
],
},
{
name: "generation",
type: "group",
label: "AI Draft Provenance",
admin: {
readOnly: true,
description:
"Which model drafted this conversation and from what brief. Written when the document is saved, so it describes the draft that was accepted, not every attempt. Editing the flow by hand afterwards does not clear it.",
},
fields: [
{
name: "model",
type: "text",
admin: { description: "Served model id that produced the draft.", readOnly: true },
},
{
name: "promptVersion",
type: "text",
admin: { description: "Prompt contract version, so an old draft is comparable.", readOnly: true },
},
{
name: "promptSource",
type: "select",
options: [
{ label: "Built-in voice", value: "builtin" },
{ label: "Global setting", value: "global" },
{ label: "This dialogue", value: "dialogue" },
],
admin: {
description: "Which voice shaped the prompt: the built-in default, the global setting, or this dialogue's override.",
readOnly: true,
},
},
{
name: "connectionLabel",
type: "text",
admin: {
description:
"The admin connection that served the model. Empty means the Unsloth environment defaults.",
readOnly: true,
},
},
{
name: "promptRevision",
type: "number",
admin: {
description:
"Revision of the prompt settings in force when this draft was made. Empty for the built-in voice, which has no settings revision.",
readOnly: true,
},
},
{
name: "attempts",
type: "number",
admin: { description: "1 when the first output validated, 2 when it was retried.", readOnly: true },
},
{
name: "generatedAt",
type: "date",
admin: { description: "When the draft was generated.", readOnly: true },
},
{
name: "brief",
type: "json",
admin: {
description: "The form brief the author filled in when drafting.",
readOnly: true,
},
},
],
},
],
};