import { ResumeIcon, } from "@/components/icons/product-icons"; import { useEffect, useState } from "react"; import { AlertTriangle, X } from "lucide-react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import type { RecoveryState } from "@/lib/types"; import { resolveModelRequestFailureCopy } from "@/lib/model-request-failure"; interface RecoveryNoticeProps { state: RecoveryState; onContinue: () => Promise; onDismiss: () => Promise; onOpenSettings?: () => void; } export function RecoveryNotice({ state, onContinue, onDismiss, onOpenSettings }: RecoveryNoticeProps) { const { t } = useTranslation(); const [pending, setPending] = useState<"continue" | "dismiss" | null>(null); const [error, setError] = useState(null); const [hiddenRecoveryId, setHiddenRecoveryId] = useState(null); useEffect(() => { // A continuation can be interrupted again with the same recovery ID. // Reveal the decision surface when the server returns to a waiting state. if (state.status === "awaiting_user" || state.status === "failed") { setHiddenRecoveryId(null); } }, [state.recovery_id, state.status]); const waiting = state.status === "awaiting_user" || state.status === "failed"; // Recovery metadata stays resuming until the continued turn commits, but // execution progress belongs to the normal running-turn UI, even after reload. if (!waiting || hiddenRecoveryId === state.recovery_id) return null; const contextUnavailable = state.can_continue === false; const modelFailure = state.reason === "model_request_failed" ? resolveModelRequestFailureCopy({ errorKind: state.failure_error_kind }, t) : null; const title = modelFailure?.title ?? (state.status === "failed" ? t("recovery.failed", { defaultValue: "Task recovery failed" }) : t("recovery.interrupted", { defaultValue: "Task interrupted" })); const detail = contextUnavailable || state.status === "failed" ? t("recovery.failedHelp", { defaultValue: "The saved task could not be restored safely. Review it before continuing.", }) : modelFailure && state.reply_preserved === true ? t("recovery.replyPreserved", { defaultValue: "Reply saved. You can continue." }) : modelFailure?.body ?? t("recovery.review", { defaultValue: "Review the task before continuing. Tools will not be replayed automatically." }); const run = (action: "continue" | "dismiss") => { setPending(action); setError(null); // ``resuming`` is an internal transition, not another task for the user // to monitor. Hide the notice optimistically and only bring it back if // the explicit action is rejected. if (action === "continue") setHiddenRecoveryId(state.recovery_id); const operation = action === "continue" ? onContinue() : onDismiss(); void operation.catch(() => { if (action === "continue") setHiddenRecoveryId(null); setError(t("recovery.actionFailed", { defaultValue: "Recovery action failed. Try again." })); }).finally(() => setPending(null)); }; return (

{title}

{modelFailure && onOpenSettings ? ( ) : null}

{error ?? detail}

{!contextUnavailable ? ( ) : null}
); }