"use client"; import { useEffect, useMemo, useState } from "react"; import { useFormFields } from "@payloadcms/ui"; import { composeDialogueSystemPrompt, DEFAULT_VOICE, resolveVoice, } from "@/lib/npc-dialogue/generationPrompt"; import type { VoiceSource } from "@/lib/npc-dialogue/generationPrompt"; import { getDialoguePromptSettings } from "@/app/(frontend)/personnel/contacts/[id]/actions"; /** * Shows the exact system prompt a draft will receive, including the sections an * author cannot edit. * * This exists because a voice textarea is invisible plumbing: without seeing the * assembled result you cannot tell whether your edit is doing what you think, or * whether the built-in voice still applies. The preview is composed with the same * pure helpers the server calls, so it cannot drift from what is sent. * * Styled in zinc because the admin bundle does not load the frontend theme's CSS * variables (see `src/components/admin/npc-dialogue/AGENTS` note in the area guide). */ const SOURCE_LABELS: Record = { builtin: "Built-in voice", global: "Global setting", dialogue: "This dialogue", }; function PreviewFrame({ source, revision, voice, extraRules, hint, }: { source: VoiceSource; revision: number | null; voice: string | null; extraRules: string | null; hint: string; }) { const prompt = composeDialogueSystemPrompt({ voice, extraRules }); return (
{SOURCE_LABELS[source]} {revision !== null ? revision {revision} : null} {hint}
        {prompt}
      

VOICE and HOUSE RULES follow the fields above. STRUCTURE, FIELDS, STATE and OUTPUT are fixed in code because the draft validator enforces them. This is the prompt for a whole-conversation draft; a generated branch swaps only the STRUCTURE section. {voice ? "" : ` No custom voice is set, so the built-in voice is used: ${DEFAULT_VOICE.length} characters.`}

); } /** Preview for the global settings page: reflects unsaved edits immediately. */ export function GlobalPromptPreview() { const values = useFormFields(([fields]) => ({ voice: fields?.voice?.value as string | undefined, extraRules: fields?.extraRules?.value as string | undefined, revision: fields?.revision?.value as number | undefined, })); const voice = values.voice?.trim() || null; const extraRules = values.extraRules?.trim() || null; const revision = typeof values.revision === "number" ? values.revision : null; return ( ); } /** * Preview for one dialogue. Reads the (possibly unsaved) override from the form * and the global settings from the server, then resolves the same precedence the * generator uses. */ export function DialoguePromptPreview() { const voiceOverride = useFormFields( ([fields]) => fields?.voiceOverride?.value as string | undefined, ); const [settings, setSettings] = useState<{ voice: string | null; extraRules: string | null; revision: number | null; } | null>(null); const [failed, setFailed] = useState(false); useEffect(() => { let cancelled = false; void getDialoguePromptSettings() .then((result) => { if (cancelled) return; if (!result.success || !result.data) { setFailed(true); return; } setSettings(result.data); }) .catch(() => { if (!cancelled) setFailed(true); }); return () => { cancelled = true; }; }, []); const resolved = useMemo( () => resolveVoice({ dialogueVoice: voiceOverride, globalVoice: settings?.voice, globalExtraRules: settings?.extraRules, globalRevision: settings?.revision, }), [voiceOverride, settings], ); return ( ); }