"use client"; import { useRouter } from "next/navigation"; import { useCallback, useEffect, useState } from "react"; import { badgeVariants } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { MANIFEST_CHANGED, announceManifestChanged } from "../timelineApi"; import { jobLine, type Job } from "./AssistBox"; // --------------------------------------------------------------------------- // CLAIM-GROUP PROPOSALS on the Cut tab (lane L4), optional in every part. // Propose groups starts the editor's `propose-groups` job (routed by // settings.generation: local by default, through the GPU arbiter); its // PROPOSALS come back as rows -- a section, a claim, its clips, why -- each // with Accept and Reject. Nothing is written until Accept, which writes that // one group through the manifest's structural writer (an undo point first; // /api/report/group-proposals, lib/report/groupProposals.mjs). With generation // routed to Claude Code or no editor the button says why; `groups set` and // `groups import` are the manual path. // --------------------------------------------------------------------------- type Proposed = { id: string; section: number; claim: string; ids: string[]; why?: string; status: "proposed" | "accepted" | "rejected" | "failed"; error?: string; group?: string; now?: string | null; }; type Proposals = { at: string; model: string; request?: string; groups: Proposed[]; refused: { group: unknown; why: string }[] }; type View = { proposals: Proposals | null; error: string | null; route: { route: "code" | "api" | "local" | null; model: string | null; why: string | null } | null; editorError: string | null; job: Job | null; token: string | null; }; const STATUS_WORD: Record = { proposed: "proposed", accepted: "accepted", rejected: "rejected", failed: "not accepted" }; const routeWhy = (v: View | null): string | null => { if (!v) return "reading…"; if (!v.route) return `needs the editor${v.editorError ? ` (${v.editorError})` : ""}`; return v.route.why; }; export default function GroupProposalsPanel({ project }: { project: string }) { const router = useRouter(); const [view, setView] = useState(null); const [job, setJob] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const [request, setRequest] = useState(""); const load = useCallback(async () => { const r = await fetch(`/api/report/group-proposals?project=${encodeURIComponent(project)}`, { cache: "no-store" }).catch(() => null); if (!r) return; const j = (await r.json().catch(() => null)) as (View & { error?: string }) | null; if (!r.ok || !j) { setError(j?.error ?? `HTTP ${r.status}`); return; } setView(j); if (j.job && !j.job.ended) setJob(j.job); }, [project]); useEffect(() => { void load(); // Another editor changed the cut: whether each proposal still fits is re-read. const on = (e: Event) => { if ((e as CustomEvent).detail?.project === project) void load(); }; window.addEventListener(MANIFEST_CHANGED, on); return () => window.removeEventListener(MANIFEST_CHANGED, on); }, [load, project]); const live = job && !job.ended ? job.id : null; useEffect(() => { if (!live) return; const t = setInterval(async () => { const r = await fetch(`/api/report/group-proposals?project=${encodeURIComponent(project)}&job=${encodeURIComponent(live)}`, { cache: "no-store" }).catch(() => null); if (!r?.ok) return; const j = (await r.json()) as Job; setJob(j); if (j.ended) await load(); }, 1500); return () => clearInterval(t); }, [live, project, load]); const post = async (body: Record) => { setBusy(true); setError(null); try { const r = await fetch("/api/report/group-proposals", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, ...body }) }); const j = (await r.json().catch(() => ({}))) as Record; return { ok: r.ok, status: r.status, json: j }; } finally { setBusy(false); } }; const start = async () => { setJob(null); const r = await post({ op: "start", ...(request.trim() ? { request: request.trim() } : {}) }); if (r.json.job) setJob(r.json.job as Job); if (!r.ok) setError(String(r.json.error ?? `HTTP ${r.status}`)); }; const accept = async (id: string) => { const r = await post({ op: "accept", id, token: view?.token ?? null, ...(view?.proposals ? { at: view.proposals.at } : {}) }); if (r.status === 409) setError(`${String(r.json.error ?? "the cut changed")} — reloaded; check and accept again`); else if (!r.ok) setError(String(r.json.error ?? `HTTP ${r.status}`)); else if (r.json.failed) setError(`${id} not accepted: ${String(r.json.error ?? "")}`); if (r.ok && r.json.accepted) { announceManifestChanged(project); router.refresh(); } await load(); }; const reject = async (id: string | null) => { const r = await post({ op: "reject", ...(id ? { id } : {}), ...(view?.proposals ? { at: view.proposals.at } : {}) }); if (!r.ok) setError(String(r.json.error ?? `HTTP ${r.status}`)); await load(); }; const why = routeWhy(view); const running = !!job && !job.ended; const p = view?.proposals ?? null; const waiting = p ? p.groups.filter((g) => g.status === "proposed" || g.status === "failed") : []; return (

claim groups — proposed

setRequest(e.target.value)} className="min-w-64 flex-1 rounded border border-[var(--color-line)] bg-[var(--color-ink)] px-1.5 py-0.5 text-[11px] text-[var(--color-text)] outline-none focus:border-[var(--color-sel)]" /> {why && view && ( {why} )} {view?.route && !why && ( {view.route.route === "local" ? `on the local model${view.route.model ? ` (${view.route.model})` : ""}, through the GPU` : `on the Claude API${view.route.model ? ` (${view.route.model})` : ""}`} · nothing changes until you Accept )}
{job && (

{jobLine(job)}

)} {error && (

{error}

)} {view?.error &&

{view.error}

} {p && (

{p.groups.length} proposed by {p.model} · {new Date(p.at).toLocaleString()} {p.request ? ` · “${p.request}”` : ""}

{p.groups.length > 0 && (
    {p.groups.map((g) => (
  • §{g.section} {g.claim} {g.ids.join(" ")} {g.status !== "proposed" && ( {STATUS_WORD[g.status]} {g.group ? ` as ${g.group}` : ""} )} {(g.status === "proposed" || g.status === "failed") && ( <> )} {g.why && {g.why}} {g.now && (g.status === "proposed" || g.status === "failed") && ( now: {g.now} )} {g.error && g.status === "failed" && !g.now && {g.error}}
  • ))}
)} {p.refused.length > 0 && (
    {p.refused.map((r, i) => (
  • not proposed: {r.why}
  • ))}
)} {waiting.length > 1 && ( )}
)}
); }