import type { AttentionItem, SessionState } from "@protocol/types"; import { useEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Modal } from "@/components/ui/dialog"; import { Progress } from "@/components/ui/progress"; import { toast } from "@/components/ui/toast"; import { cn } from "@/lib/utils"; import { diffFilesFromPayload } from "./diff"; import { DiffView } from "./diff-view"; export interface AttentionDialogProps { session: SessionState; onRespond: (item: AttentionItem, response: unknown) => void; /** Fired when the request vanished because another client answered it. */ onRemoteResolved: (requestId: string) => void; } function diffResponse(result: "Accepted" | "Rejected", path?: string): string { return JSON.stringify({ result, ...(path ? { path } : {}) }); } /** * Blocking extension attention request. * * One dialog per session: title/options/countdown, or the pre-execution diff * review variant. A request resolved by another client disappears from * `session.attention` (reducer) and we close immediately + toast. */ export function AttentionDialog({ session, onRespond, onRemoteResolved }: AttentionDialogProps) { const { t } = useTranslation(); const [current, setCurrent] = useState(null); const [remaining, setRemaining] = useState(null); const responded = useRef(false); const dismissed = useRef(null); const timedOut = useRef>(new Set()); useEffect(() => { const next = session.attention[0] ?? null; if (next && dismissed.current === next.requestId) return; if (next) { if (!current || current.requestId !== next.requestId) { responded.current = false; setCurrent(next); } return; } if (current) { if (!responded.current) onRemoteResolved(current.requestId); responded.current = false; dismissed.current = null; setCurrent(null); } }, [session.attention, current, onRemoteResolved]); useEffect(() => { const timeoutSec = current?.timeoutSec; if (!current || !timeoutSec) { setRemaining(null); return; } const item = current; const deadline = item.receivedAt + timeoutSec * 1_000; const tick = () => { const left = Math.max(0, Math.ceil((deadline - Date.now()) / 1_000)); setRemaining(left); if (left <= 0 && !timedOut.current.has(item.requestId)) { timedOut.current.add(item.requestId); responded.current = true; dismissed.current = item.requestId; setCurrent(null); toast.error(t("chat.attentionTimedOut")); } }; tick(); const timer = setInterval(tick, 500); return () => clearInterval(timer); }, [current, t]); const files = useMemo(() => diffFilesFromPayload(current?.payload), [current]); const isDiff = current?.method === "diff" || files.length > 0; function respond(response: unknown, message: string, kind: "success" | "error" | "info" = "success") { if (!current) return; responded.current = true; onRespond(current, response); if (kind === "error") toast.error(message); else if (kind === "info") toast.info(message); else toast.success(message); } const options = current?.options?.length ? current.options : current?.method === "confirm" ? [ { id: "confirm", label: t("chat.allowOnce") }, { id: "deny", label: t("chat.deny") }, ] : [ { id: "allow-once", label: t("chat.allowOnce") }, { id: "always", label: t("chat.always") }, { id: "deny", label: t("chat.deny") }, ]; return ( { /* blocking: answer (or let it time out) before closing */ }} title={current?.title ?? t("chat.attentionTitle")} > {current ? (
{current.message ?

{current.message}

: null} {isDiff ? ( <>
{t("chat.diffReview")}
{files.length ? ( respond( diffResponse(decision === "accept" ? "Accepted" : "Rejected", path), decision === "accept" ? t("chat.acceptedAll", { n: files.length }) : t("chat.rejected", { path: path ?? "" }), decision === "accept" ? "success" : "error", ) } /> ) : (
{t("chat.noChanges")}
)} ) : (
{options.map((option, index) => ( ))}
)} {current.timeoutSec ? ( <>
{t("chat.attentionTimeout")} {remaining ?? current.timeoutSec}s
{current.method}
) : (
{current.method}
)}
) : null} ); }