"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(null); const [error, setError] = useState(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 (
AI {REWRITE_ACTIONS.map((action) => ( ))}
{error &&

{error}

}
); }