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