Skip to content

Your lightweight open-source personal agent.

4.9 KB
Raw Blame
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<void>;
  onDismiss: () => Promise<void>;
  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<string | null>(null);
  const [hiddenRecoveryId, setHiddenRecoveryId] = useState<string | null>(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 (
    <div
      role="alert"
      aria-live="assertive"
      data-recovery-status={state.status}
      className="thread-composer-notice mb-2 flex flex-wrap items-center gap-3 rounded-control border border-border/70 bg-muted/35 px-3 py-2 text-sm transition-[background-color,border-color,opacity,transform] duration-200 ease-out motion-reduce:transition-none animate-in fade-in-0 slide-in-from-bottom-1 duration-200 motion-reduce:animate-none"
    >
      <AlertTriangle className="h-4 w-4 shrink-0 text-amber-600 dark:text-amber-400" aria-hidden />
      <div className="min-w-0 flex-1 basis-48">
        <p className="font-medium">
          {title}
        </p>
        {modelFailure && onOpenSettings ? (
          <Button variant="link" size="sm" className="h-auto px-0 py-1" onClick={onOpenSettings}>
            {t("thread.composer.reauthSettings")}
          </Button>
        ) : null}
        <p className={cn(
          "mt-0.5 text-xs",
          error ? "text-destructive" : "text-muted-foreground",
        )}>
          {error ?? detail}
        </p>
      </div>
      <div className="ms-auto flex shrink-0 items-center gap-1.5">
        <Button
          type="button"
          size="sm"
          variant="outline"
          disabled={pending !== null}
          onClick={() => run("dismiss")}
        >
          <X className="mr-1 h-3.5 w-3.5" aria-hidden />
          {t("recovery.dismiss", { defaultValue: "Dismiss" })}
        </Button>
        {!contextUnavailable ? (
          <Button
            type="button"
            size="sm"
            disabled={pending !== null}
            onClick={() => run("continue")}
          >
            <ResumeIcon className="mr-1 h-3.5 w-3.5" aria-hidden />
            {t("recovery.continue", { defaultValue: "Continue" })}
          </Button>
        ) : null}
      </div>
    </div>
  );
}