Skip to content

Automate browser based workflows with AI

4.9 KB
Raw Blame
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import {
  AuthStatusValue,
  useAuthDiagnostics,
} from "@/hooks/useAuthDiagnostics";
import { useAuthIssueStore } from "@/store/AuthIssueStore";

type BannerStatus =
  | Exclude<AuthStatusValue, "ok">
  | "ui_session_failed"
  | "request_auth_error"
  | "error";

function getCopy(status: BannerStatus): { title: string; description: string } {
  switch (status) {
    case "missing_api_key":
      return {
        title: "UI credential missing",
        description:
          "The UI has no runtime credential. Authenticated requests will fail until the UI server can mint a session or a runtime key is stored.",
      };
    case "invalid_format":
      return {
        title: "Skyvern API key is invalid",
        description: "The configured key cannot be decoded.",
      };
    case "invalid":
      return {
        title: "Skyvern API key not recognized",
        description: "The backend rejected the configured key.",
      };
    case "expired":
      return {
        title: "Skyvern API key expired",
        description: "The current key is no longer valid.",
      };
    case "not_found":
      return {
        title: "Local organization missing",
        description: "The backend could not find the local organization.",
      };
    case "ui_session_failed":
      return {
        title: "The UI server could not mint a browser session",
        description:
          "The UI holds a short-lived session token rather than the organization API key, and the UI server was unable to issue one. Until it can, every request from this page is unauthenticated.",
      };
    case "request_auth_error":
      return {
        title: "Skyvern API requests are unauthorized",
        description:
          "The backend rejected a UI request. This usually means UI session minting, the backend API key, and the local database are out of sync.",
      };
    case "error":
    default:
      return {
        title: "Unable to verify Skyvern API key",
        description:
          "The UI could not reach the diagnostics endpoint. Ensure the backend is running locally.",
      };
  }
}

function SelfHealApiKeyBanner() {
  const diagnosticsQuery = useAuthDiagnostics();
  const authIssue = useAuthIssueStore((state) => state.issue);
  const uiSessionFailure = useAuthIssueStore((state) => state.uiSessionFailure);

  const { data, error } = diagnosticsQuery;

  const rawStatus = data?.status;
  // A failed mint is the cause and a rejected request is only its symptom, so it outranks
  // the generic unauthorized banner.
  const bannerStatus: BannerStatus | null = error
    ? "error"
    : rawStatus && rawStatus !== "ok"
      ? rawStatus
      : uiSessionFailure
        ? "ui_session_failed"
        : authIssue
          ? "request_auth_error"
          : null;

  if (!bannerStatus) {
    return null;
  }

  const copy = getCopy(bannerStatus);
  const queryErrorMessage = error?.message ?? null;
  // The diagnostics endpoint only exists in a local backend, so its statuses are the only
  // ones where local-development advice is the right next step.
  const isLocalDiagnosticsStatus =
    bannerStatus !== "error" &&
    bannerStatus !== "ui_session_failed" &&
    bannerStatus !== "request_auth_error";

  return (
    <div className="px-4 pt-4">
      <Alert className="flex flex-col items-center gap-2 border-slate-700 bg-slate-900 text-slate-50">
        <AlertTitle className="text-center text-base font-semibold tracking-wide">
          {copy.title}
        </AlertTitle>
        <AlertDescription className="space-y-3 text-center text-sm leading-6">
          <p>
            {copy.description}
            {isLocalDiagnosticsStatus ? (
              <>
                {" "}
                For local development, set <code>SKYVERN_API_KEY</code> in{" "}
                <code className="mx-1">skyvern-frontend/.env</code> and restart{" "}
                <code>npm run dev</code>. Otherwise run{" "}
                <code>skyvern doctor --fix</code> in the project directory, then
                restart local services if prompted.
              </>
            ) : null}
          </p>
          {data?.detail ? (
            <p className="text-xs text-slate-300">{data.detail}</p>
          ) : null}
          {uiSessionFailure?.detail ? (
            <p className="text-xs text-slate-300">{uiSessionFailure.detail}</p>
          ) : null}
          {authIssue ? (
            <p className="text-xs text-slate-300">
              Last failed request: HTTP {authIssue.statusCode}
              {authIssue.path ? ` at ${authIssue.path}` : ""}
              {authIssue.detail ? ` (${authIssue.detail})` : ""}
            </p>
          ) : null}
          {queryErrorMessage ? (
            <p className="text-xs text-rose-200">{queryErrorMessage}</p>
          ) : null}
        </AlertDescription>
      </Alert>
    </div>
  );
}

export { SelfHealApiKeyBanner };