# AGENTS.md: NPC Dialogue > **Parent**: `../../AGENTS.md` (commands, RBAC, Payload config) and `../AGENTS.md` (lib conventions). Two halves: the runtime that walks a stored conversation, and the authoring surface that builds one. The AI drafting tools are documented in full at `docs/npc-dialogue-ai.md`. ## Never-miss rules 1. **The stored `flowData` graph is the only thing players can see.** The runtime resolves every node server-side from the document, so an unsaved editor draft is unreachable by construction. Do not add a code path that lets the client name a node the document does not contain. 2. **AI output never writes a document.** `generateNpcDialogueDraft` and `rewriteNpcDialogueLine` return values to the editor; only the editor's Save persists them. Keep it that way: a server action that saves on the model's behalf destroys the review gate. 3. **Generated effects are limited to `notify` and `flag`.** `emit`, `reward` and `discord` write game state or post to Discord and are author-only. They are absent from the generation schema and rejected again by the validator. 4. **Flag keys must already exist.** A condition or `{{token}}` naming a missing `player-flags` key never fires and the branch silently dead-ends. Anything that generates dialogue text checks against the live vocabulary. 5. **The prompt's contract half is not editable.** Only `VOICE` (via the `npc-dialogue-settings` global or a dialogue's `voiceOverride`) and `HOUSE RULES` are admin-editable. `STRUCTURE`, `FIELDS`, `STATE`, `OUTPUT` and the worked example live in `LOCKED_RULES` because `generationSchema.ts` and `validateGeneratedFlow` enforce the same rules; exposing them lets an admin break every draft with no way to trace the failure. Precedence is dialogue -> global -> built-in, resolved by the pure `resolveVoice`, and every stored draft records which one applied. ## Files ``` src/lib/npc-dialogue/ dialogue.ts # Node/edge/effect types, hop projection, validateGraph (pure) generationSchema.ts # JSON schema, normalizer, draft validator (pure) generationPrompt.ts # Prompt text, voice resolution/composition, vocabularies (pure) generationSettings.ts # Settings global read + voice precedence resolution (Payload) normalize.ts # Dash normalisation and prompt-text cleaning (pure) generation.ts # Model calls, context loading, validate-and-retry (Payload) interpolate.ts # {{flag}}, {{countdown:key}}, {{ cond ? a : b }} (pure) flags.ts # Flag read/write, condition evaluation (Payload) selection.ts # Which conversation is offered to a player (Payload) states.ts # Per-player read markers (Payload) attachments.ts # media ids to URLs (Payload) reorder.ts, DialogueText.tsx (pure) src/components/admin/npc-dialogue/ NpcDialogueEditor.tsx # React Flow canvas, the custom field component NodeEditDialog.tsx # Per-node form, hosts LineAssist GenerateDraftDialog.tsx# The brief form for whole-conversation drafting LineAssist.tsx # Rewrite / Shorten / Sharpen for one field PromptPreview.tsx # Assembled-prompt preview for the global and per dialogue NpcDialogueNode.tsx, RoutedFlowEdge.tsx, MarkupEditor.tsx, types.ts src/components/frontend/personnel/NpcConversation.tsx # Player-facing walker src/collections/game/NpcDialogues.ts # Collection, voice override, provenance src/collections/game/NpcDialogueSettings.ts # Editable voice global (20 versions) ``` ## Data model in one paragraph `flowData` is `{ nodes, edges }`. Edges are a **derived projection** of the node data (`deriveEdges`) and exist only for the canvas: hops live in the nodes (`next`, `choices`, `trueTarget`/`falseTarget`). Deleting an edge scrubs the hop it represents. Every hop projection goes through `outgoingHops` so the canvas, the validator and the runtime cannot disagree. Node types: `dialogue`, `choice`, `event`, `end`, `delay`, `condition`. Effects (`notify`, `emit`, `reward`, `flag`) are configured on `event` nodes only and are executed by `triggerNpcDialogueEvent`, which re-reads the stored node rather than trusting the client. Availability is double-gated: `game-npcs.canMessage` on the contact, and the `entryGate` / `priority` on the dialogue document selecting which of an NPC's conversations is offered. ## Text markup Lines support `**bold**`, `*italic*`, `{red}color{/}`, `` `code` ``, `![alt](url)`, list prefixes, `{{flag}}`, `{{countdown:key}}`, and ternaries `{{ cond ? a : b }}` including numeric comparisons. Interpolation is a single pass server-side, so a flag value can never smuggle in another token. `DialogueText.tsx` is the only renderer; don't hand-roll one. ## AI drafting behaviour worth knowing - Two attempts maximum: the second prompt carries the validator's complaints. A draft that fails twice is not shown at all. - `normalizeGeneratedFlow` assigns layout positions by breadth from the start node, so a draft opens as a readable chain. - Provenance is only written when a draft lands, so rejected drafts leave no trace. - Dashes in model output are normalised: em-dash to a comma, a remaining en-dash to a hyphen (it is a range). Shipped copy never carries an em-dash. - The model is reachable only through `src/lib/ai/generate.ts`. Do not call `fetch` against Unsloth anywhere else. - Which model serves a draft comes from `resolveConnection(payload, purpose)` (`src/lib/ai/connections.ts`), resolved once per request alongside the voice. A null connection means the env defaults, which is the unchanged behaviour. See `docs/ai-settings.md`. - The voice is resolved once per draft, not per attempt, so a retry cannot change register mid-generation. `rewriteDialogueLine` resolves it the same way, so a house voice shapes drafts and rewrites alike. - A draft that fails twice while a custom voice is active names that voice in the error. Keep that hint: without it a bad voice block looks like a broken model. ## Admin panel styling The admin bundle does not load the frontend's shadcn theme variables, so `bg-background`, `bg-popover`, `border-input`, `text-muted-foreground` and the rest resolve to nothing inside a custom admin field. A surface styled only with those tokens comes out transparent, and a portaled Radix `Select` becomes transparent *and* unable to take pointer events, so it can only be driven with the arrow keys. So: style every admin-panel element explicitly in zinc (`bg-zinc-800` / `bg-zinc-900`, `border-white/20`, `text-white`), as `NodeEditDialog` does, and prefer a native `