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", }, }, }, ], };