1
0
Fork 0
polaris-task-force/src/components/admin/npc-dialogue/PromptPreview.tsx
Z8MB1E 5ccb25e643 fix(ai): bound the structured path too, and stop the dialog overpromising
The timeout from the previous commit only covered the plain-text fetch, so
the default drafting path was still unbounded: `streamObject` for
json_schema had no abort signal, which is what `generateDialogueDraft` and
the probe's structured half use. A provider that answers the plain probe
and then stalls could still hold the Generate modal and the Test button
open, while the docs claimed every request was bounded. Both
`streamObject` calls (the shared structured path and Compose's fallback)
now carry the same ceiling.

Dialog copy told the author the branch continues into the successor even
after they cleared the return box, which is when it does not. It now says
that step becomes unreachable until wired by hand. When the node cannot
take a return at all the checkbox used to vanish without explanation; the
reason is now stated where the toggle would be.

A choice node marked as the start could receive the return hop, drawing an
edge into the start node that `connectHop` and the drop handler both
refuse. It no longer returns, and its open end stays free like the other
cases.

Also: the two misplaced doc comments now sit above the declarations they
describe, and the whole-conversation caveat moved into the shared preview
component so the global settings tab carries it too.
2026-11-24 15:00:30 -05:00

154 lines
4.9 KiB
TypeScript

"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<VoiceSource, string> = {
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 (
<div className="mt-2 rounded-md border border-white/15 bg-zinc-900/60 p-3">
<div className="flex flex-wrap items-center gap-2 text-[11px] uppercase tracking-wider text-white/50">
<span className="rounded border border-white/20 px-1.5 py-0.5 text-white/80">
{SOURCE_LABELS[source]}
</span>
{revision !== null ? <span>revision {revision}</span> : null}
<span className="normal-case tracking-normal text-white/40">{hint}</span>
</div>
<pre className="mt-2 max-h-72 overflow-auto whitespace-pre-wrap break-words rounded border border-white/10 bg-zinc-950/70 p-2 font-mono text-[11px] leading-relaxed text-white/70">
{prompt}
</pre>
<p className="mt-2 text-[11px] text-white/40">
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.`}
</p>
</div>
);
}
/** 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 (
<PreviewFrame
source={voice ? "global" : "builtin"}
revision={revision}
voice={voice}
extraRules={extraRules}
hint="Applies to every conversation without its own voice."
/>
);
}
/**
* 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 (
<PreviewFrame
source={resolved.source}
revision={resolved.revision}
voice={resolved.voice}
extraRules={resolved.extraRules}
hint={
failed
? "Could not read the global settings; this shows the built-in voice."
: "Unsaved edits are reflected here but are not used until you save."
}
/>
);
}