Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

9.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"use client";

import { Button } from "@/components/ui/button";
import { AlertDialog } from "@/components/ui/alert-dialog";
import { getAuthSessionEpoch } from "@/features/auth";
import { useFullAccessAllowed } from "@/features/auth/account-session";
import { FullAccessConfirmContent } from "@/features/chat/permission-mode-select";
import { hasChatFullAccess, withChatFullAccess } from "@/features/chat/utils/chat-full-access";
import {
  resolveToolConfirmation,
  ToolApprovalGoneError,
} from "@/features/chat/api/chat-api";
import { useChatRuntimeStore } from "@/features/chat/stores/chat-runtime-store";
import {
  COMPOSER_INPUT_SELECTOR,
  isSurfaceBackgrounded,
  useShortcut,
} from "@/features/settings";
import type {
  ToolCallMessagePartComponent,
  ToolCallMessagePartStatus,
} from "@assistant-ui/react";
import { useCallback, useEffect, useState } from "react";
import { useChatActive, useChatNavigationStore } from "@/features/chat";

/** Shown only when the adapter registered a backend-gated pending call for this card. */
export function ToolConfirmationControls({
  toolCallId,
  toolName,
  result,
  status,
}: {
  toolCallId?: string;
  toolName: string;
  result: unknown;
  status?: ToolCallMessagePartStatus;
}) {
  const confirmation = useChatRuntimeStore((s) =>
    toolCallId &&
    Object.prototype.hasOwnProperty.call(s.toolConfirmations, toolCallId)
      ? s.toolConfirmations[toolCallId]
      : undefined,
  );
  const fullAccessAllowed = useFullAccessAllowed();
  const permissionMode = useChatRuntimeStore((s) =>
    withChatFullAccess(s, s.chatFullAccessGrants, confirmation?.threadId).permissionMode,
  );
  const grantChatFullAccess = useChatRuntimeStore((s) => s.grantChatFullAccess);
  const chatFullAccess = useChatRuntimeStore((s) =>
    fullAccessAllowed && confirmation && !confirmation.imageDisclosure &&
    hasChatFullAccess(s.chatFullAccessGrants, confirmation.threadId, confirmation.runId),
  );
  const allowToolAlways = useChatRuntimeStore((s) => s.allowToolAlways);
  const clearToolConfirmation = useChatRuntimeStore(
    (s) => s.clearToolConfirmation,
  );
  const autoAllowKey = confirmation?.autoAllowKey ?? "";
  // Sharing the user's image is asked every time: no Always allow, no keyboard chord.
  const disclosure = confirmation?.imageDisclosure;
  const autoAllowed = useChatRuntimeStore(
    (s) =>
      !disclosure &&
      (s.alwaysAllowToolsBySession.get(autoAllowKey)?.has(toolName) ?? false),
  );

  const [fullAccessOpen, setFullAccessOpen] = useState(false);
  const [decided, setDecided] = useState(false);
  const [pending, setPending] = useState<"allow" | "deny" | null>(null);
  // "retry" can succeed on another press; "gone" means the backend no longer holds the approval.
  const [failure, setFailure] = useState<"retry" | "gone" | null>(null);
  const failed = failure !== null;

  const awaiting =
    confirmation !== undefined &&
    result === undefined &&
    status?.type === "running";
  const showControls = awaiting && !decided;

  const resolve = useCallback(
    async (decision: "allow" | "deny", alsoAlways = false, fullAccess = false) => {
      if (!toolCallId || !confirmation) return;
      const epoch = getAuthSessionEpoch();
      const choiceVersion = useChatRuntimeStore.getState().chatPermissionChoiceVersion;
      setPending(decision);
      setFailure(null);
      try {
        const ok = await resolveToolConfirmation(
          confirmation.sessionId,
          confirmation.approvalId,
          decision,
          fullAccess,
        );
        if (ok && getAuthSessionEpoch() === epoch) {
          if (fullAccess && confirmation.runId &&
            useChatRuntimeStore.getState().chatPermissionChoiceVersion === choiceVersion) {
            // Autosave may adopt the first turn while the POST is in flight.
            const current = useChatRuntimeStore.getState().toolConfirmations[toolCallId];
            const target = current?.approvalId === confirmation.approvalId ? current : confirmation;
            grantChatFullAccess({
              runId: confirmation.runId,
              sessionId: confirmation.sessionId,
              threadId: target.threadId,
            });
          }
          // Record the session grant only after the backend accepted, or a failed press auto-approves.
          if (alsoAlways && autoAllowKey) allowToolAlways(autoAllowKey, toolName);
          // Hide only once the backend confirms, or the generation stays blocked with no retry.
          setDecided(true);
          clearToolConfirmation(toolCallId);
        } else {
          setFailure("retry");
        }
      } catch (err) {
        setFailure(err instanceof ToolApprovalGoneError ? "gone" : "retry");
      } finally {
        setPending(null);
      }
    },
    [
      toolCallId,
      confirmation,
      clearToolConfirmation,
      allowToolAlways,
      grantChatFullAccess,
      autoAllowKey,
      toolName,
    ],
  );

  useEffect(() => {
    if (showControls && (autoAllowed || chatFullAccess) && pending === null && !failed) {
      // A sibling loop in this chat needs its own backend policy upgrade.
      void resolve("allow", false, !!chatFullAccess);
    }
  }, [showControls, autoAllowed, chatFullAccess, pending, failed, resolve]);

  // Chords only while this card asks and the chat is visible; with a second request parked, both
  // cards fall back to buttons.
  const chatActive = useChatActive();
  const soleRequest = useChatRuntimeStore(
    (s) => Object.keys(s.toolConfirmations).length === 1,
  );
  // A sidebar selection also handles Escape without consuming it; defer so dismissing it cannot deny.
  const selectionActive = useChatNavigationStore((s) => s.selectionActive);
  const keyboardReady =
    !disclosure &&
    chatActive &&
    !fullAccessOpen &&
    soleRequest &&
    !selectionActive &&
    showControls &&
    pending === null &&
    !((autoAllowed || chatFullAccess) && !failed);
  // keyboardReady stays true under a dialog, so the chords claim only while chat is uncovered;
  // answering a tool call the user cannot see must never happen by accident.
  const chatUncovered = () => !isSurfaceBackgrounded(COMPOSER_INPUT_SELECTOR);
  useShortcut("approveToolRequest", () => void resolve("allow"), {
    enabled: keyboardReady,
    claims: chatUncovered,
    skipInTextFields: true,
  });
  useShortcut("declineToolRequest", () => void resolve("deny"), {
    enabled: keyboardReady,
    claims: chatUncovered,
    skipInTextFields: true,
    // Escape passes the gate from the composer (it types nothing there); Enter does not: it sends.
    textFieldException: COMPOSER_INPUT_SELECTOR,
  });

  if (!showControls) return null;
  // Auto-approved tools resolve silently unless the post fails.
  if ((autoAllowed || chatFullAccess) && !failed) return null;

  return (
    <div className="flex flex-wrap items-center gap-2 pt-1">
      {disclosure ? (
        <p className="w-full text-xs text-muted-foreground">
          Send your attached image ({Math.ceil(disclosure.size_bytes / 1024)}{" "}
          KB) to {disclosure.server} ({disclosure.tool}) at{" "}
          {disclosure.destination}? The server may keep it.
        </p>
      ) : null}
      <Button
        size="xs"
        disabled={pending !== null || failure === "gone"}
        onClick={() => void resolve("allow")}
      >
        {disclosure ? "Share image once" : "Allow"}
      </Button>
      {disclosure ? null : (
        <Button
          size="xs"
          variant="outline"
          disabled={pending !== null || failure === "gone"}
          onClick={() => void resolve("allow", true)}
        >
          Always allow
        </Button>
      )}
      <Button
        size="xs"
        variant="destructive"
        disabled={pending !== null || failure === "gone"}
        onClick={() => void resolve("deny")}
      >
        Deny
      </Button>
      {!disclosure && fullAccessAllowed && permissionMode !== "full" && confirmation?.sessionId && confirmation?.runId ? (
        <>
          <Button
            size="xs"
            variant="destructive"
            disabled={pending !== null || failure === "gone"}
            onClick={() => setFullAccessOpen(true)}
          >
            Full Access
          </Button>
          <AlertDialog open={fullAccessOpen} onOpenChange={setFullAccessOpen}>
            <FullAccessConfirmContent
              onConfirm={() => {
                setFullAccessOpen(false);
                void resolve("allow", false, true);
              }}
              onClose={() => setFullAccessOpen(false)}
            />
          </AlertDialog>
        </>
      ) : null}
      {failure !== null ? (
        <span className="text-xs text-destructive">
          {failure === "gone"
            ? "This request is no longer waiting for an answer."
            : "Could not send your decision. Try again."}
        </span>
      ) : null}
    </div>
  );
}

export function withToolConfirmation(
  Component: ToolCallMessagePartComponent,
): ToolCallMessagePartComponent {
  const WithToolConfirmation: ToolCallMessagePartComponent = (props) => (
    <>
      <Component {...props} />
      <ToolConfirmationControls
        toolCallId={props.toolCallId}
        toolName={props.toolName}
        result={props.result}
        status={props.status}
      />
    </>
  );
  return WithToolConfirmation;
}