1
0
Fork 0
polaris-task-force/src/components/admin/npc-dialogue/LineAssist.tsx
Z8MB1E 4264431e4d feat(npc-dialogue): AI-assisted conversation drafting behind the review gate
Add a Generate button to the flow editor that drafts a whole conversation
from a form brief, and per-line Rewrite/Shorten/Sharpen controls in the node
dialog. Both land in unsaved field state, so a draft only reaches players when
an author saves the document: neither server action writes to npc-dialogues.

- src/lib/ai/generate.ts: extract the Unsloth client out of payload.config so
  Compose and dialogue drafting share one path; add generateStructured and
  resolveModel, with an optional UNSLOTH_MODEL_STRUCTURED override
- generationSchema.ts (pure): strict-safe JSON schema, normalizer with
  breadth layout, validator that rejects unknown flag keys, unauthorised
  effects, dangling hops and missing keys
- generation.ts: context loading, one validate-and-retry, then give up rather
  than hand back a broken graph; dash normalization keeps shipped copy clean
- effect vocabulary limited to notify and flag; emit, reward and discord stay
  author-only
- provenance group on npc-dialogues (migration 20261112_171955) records model,
  prompt version, attempts and the brief the draft was accepted from
- tests: 21 cases covering the normalizer, validator, retry paths and the
  rewrite parser, with the model call injected
- docs/npc-dialogue-ai.md and src/lib/npc-dialogue/AGENTS.md
2026-11-12 12:26:35 -05:00

94 lines
3 KiB
TypeScript

"use client";
import { useState } from "react";
import { Loader2, Sparkles } from "lucide-react";
import { Button } from "@/components/ui/button";
import { rewriteNpcDialogueLine } from "@/app/(frontend)/personnel/contacts/[id]/actions";
import { REWRITE_ACTIONS, REWRITE_LABELS } from "@/lib/npc-dialogue/generationPrompt";
import type { RewriteAction } from "@/lib/npc-dialogue/generationPrompt";
interface LineAssistProps {
/** The contact being written for. Null until the document is saved. */
readonly npcId: number | null;
readonly nodeType: string;
/** Current text of the field this control sits under. */
readonly value: string;
/** The line before this one, so the model does not restate it. */
readonly previousLine?: string | null;
/** Receives the replacement, which the caller writes into its draft state. */
readonly onChange: (next: string) => void;
}
/**
* Rewrite controls for one authored line.
*
* The model's replacement goes straight into the caller's draft state, so it is
* undone by the same means as typing (and committed only by the node dialog's
* Save). Nothing here can alter a stored conversation.
*/
export function LineAssist({
npcId,
nodeType,
value,
previousLine,
onChange,
}: LineAssistProps) {
const [busyAction, setBusyAction] = useState<RewriteAction | null>(null);
const [error, setError] = useState<string | null>(null);
const disabled = npcId === null || busyAction !== null || value.trim().length === 0;
async function run(action: RewriteAction) {
if (npcId === null) return;
setBusyAction(action);
setError(null);
try {
const res = await rewriteNpcDialogueLine(npcId, {
action,
text: value,
nodeType,
previousLine: previousLine ?? undefined,
});
if (res.success && res.data) {
onChange(res.data.text);
} else {
setError(res.error ?? "The model returned nothing usable.");
}
} finally {
setBusyAction(null);
}
}
return (
<div className="flex flex-col gap-1">
<div className="flex flex-wrap items-center gap-1">
<span className="inline-flex items-center gap-1 text-[9px] uppercase tracking-wider text-white/35">
<Sparkles size={10} />
AI
</span>
{REWRITE_ACTIONS.map((action) => (
<Button
key={action}
type="button"
size="sm"
variant="outline"
disabled={disabled}
title={
npcId === null
? "Save this conversation against a contact first"
: `${REWRITE_LABELS[action]} this line`
}
onClick={() => void run(action)}
className="h-6 border-white/15 px-2 text-[10px] text-white/70 hover:bg-zinc-800 disabled:opacity-30"
>
{busyAction === action ? <Loader2 className="size-3 animate-spin" /> : null}
{REWRITE_LABELS[action]}
</Button>
))}
</div>
{error && <p className="text-[10px] text-red-400">{error}</p>}
</div>
);
}