Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

10.2 KB
Raw Blame
import { memo, useEffect, useMemo } from 'react';
import { useAtom, useAtomValue } from 'jotai';
import { isCancelKey } from '@librechat/chat';
import { Button, TooltipAnchor } from '@librechat/client';
import { buildApprovalPreview, buildApprovalPreviews } from '@librechat/chat';
import { ChevronDown, ChevronUp, ShieldQuestion, TriangleAlert } from 'lucide-react';
import {
  useComposerOverlay,
  approvalPanelOpenFamily,
  pendingApprovalActionFamily,
} from '@librechat/chat/react';
import type { Agents } from 'librechat-data-provider';
import type { KeyboardEvent, RefObject } from 'react';
import type { TranslationKeys } from '~/hooks';
import {
  useApprovalContext,
  useResumeSubmit,
} from '~/components/Chat/Messages/Content/ApprovalContext';
import ToolApproval from '~/components/Chat/Messages/Content/ToolApproval';
import { useLocalize } from '~/hooks';

function usePendingToolApproval(conversationId: string) {
  const pendingAction = useAtomValue(pendingApprovalActionFamily(conversationId));
  return pendingAction?.payload.type === 'tool_approval'
    ? (pendingAction as Agents.PendingAction & {
        payload: Agents.ToolApprovalInterruptPayload;
      })
    : null;
}

const KIND_LABELS: Record<ReturnType<typeof buildApprovalPreview>['kind'], TranslationKeys> = {
  command: 'com_ui_proposed_command',
  create: 'com_ui_proposed_contents',
  edit: 'com_ui_proposed_replacements',
  generic: 'com_ui_proposed_arguments',
};

function previewTitle(
  preview: ReturnType<typeof buildApprovalPreview>,
  localize: ReturnType<typeof useLocalize>,
): string {
  if (preview.kind === 'command') return localize('com_ui_review_run_command');
  if (preview.kind === 'create') {
    return preview.target
      ? localize('com_ui_review_create_file', { 0: preview.target })
      : localize('com_ui_review_create_file_generic');
  }
  if (preview.kind === 'edit') {
    return preview.target
      ? localize('com_ui_review_edit_file', { 0: preview.target })
      : localize('com_ui_review_edit_file_generic');
  }
  return preview.toolName;
}

export const PendingToolApprovalPanel = memo(function PendingToolApprovalPanel({
  conversationId,
  triggerRef,
}: {
  conversationId: string;
  /** The composer toggle that reopens the panel; collapsing returns focus to it. */
  triggerRef?: RefObject<HTMLButtonElement>;
}) {
  const localize = useLocalize();
  const pendingAction = usePendingToolApproval(conversationId);
  const [open, setOpen] = useAtom(approvalPanelOpenFamily(conversationId));
  const { getDecisions, getStatus, isReady } = useApprovalContext();
  const { submitToolApproval } = useResumeSubmit();
  const actionId = pendingAction?.actionId;

  useEffect(() => {
    if (actionId != null) {
      setOpen(true);
    }
  }, [actionId, setOpen]);

  useComposerOverlay(conversationId, open && pendingAction != null);

  const reviews = useMemo(() => {
    if (pendingAction == null) return [];
    const configById = new Map(
      pendingAction.payload.review_configs.map((config) => [config.tool_call_id, config]),
    );
    const previews = buildApprovalPreviews(pendingAction.payload.action_requests);
    return pendingAction.payload.action_requests.map((request, index) => ({
      request,
      config: configById.get(request.tool_call_id),
      preview: previews[index],
    }));
  }, [pendingAction]);

  if (!open || pendingAction == null) {
    return null;
  }

  /** Collapsing unmounts the focused control, so focus goes back to the
   *  toggle rather than falling to the page. Decisions and drafts are kept. */
  const collapse = () => {
    setOpen(false);
    triggerRef?.current?.focus();
  };

  const handleKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
    const isComposing = e.nativeEvent.isComposing || e.key === 'Process' || e.keyCode === 229;
    if (!isCancelKey(e.nativeEvent) || isComposing) {
      return;
    }
    e.preventDefault();
    collapse();
  };

  const decisions = getDecisions(pendingAction.actionId);
  const status = getStatus(pendingAction.actionId);
  const locked = status === 'submitting' || status === 'submitted' || status === 'expired';

  return (
    <div
      id="pending-tool-approval-panel"
      /* The absolute review layer spans the composer width, but only its card
       * is painted and interactive. Let the thread receive clicks through the
       * transparent remainder of the layer. */
      className="pointer-events-none absolute bottom-28 z-10 w-full"
      role="region"
      aria-labelledby="pending-tool-approval-title"
      aria-live="polite"
      onKeyDown={handleKeyDown}
    >
      <div className="popover border-border-light bg-surface-primary-alt rounded-theme-popover pointer-events-auto flex max-h-[70vh] flex-col border shadow-lg">
        <div className="border-border-light flex shrink-0 items-center justify-between gap-2 border-b px-3 py-2">
          <p id="pending-tool-approval-title" className="text-text-primary text-sm font-medium">
            {localize(reviews.length === 1 ? 'com_ui_review_action' : 'com_ui_review_actions', {
              0: reviews.length,
            })}
          </p>
          <TooltipAnchor
            description={localize('com_ui_collapse')}
            side="top"
            render={
              <Button
                type="button"
                variant="quiet"
                size="icon"
                aria-label={localize('com_ui_collapse')}
                className="size-auto rounded-md p-1"
                onClick={collapse}
              >
                <ChevronDown className="size-4" aria-hidden="true" />
              </Button>
            }
          />
        </div>
        <div className="min-h-0 flex-1 space-y-3 overflow-y-auto p-3">
          {reviews.map(({ request, config, preview }, index) => (
            <section
              key={request.tool_call_id}
              aria-labelledby={`approval-${request.tool_call_id}`}
            >
              <div className="mb-2">
                <h3
                  id={`approval-${request.tool_call_id}`}
                  className="text-text-primary text-sm font-medium [overflow-wrap:anywhere]"
                >
                  {reviews.length > 1
                    ? `${index + 1}. ${previewTitle(preview, localize)}`
                    : previewTitle(preview, localize)}
                </h3>
                <p className="text-text-secondary text-xs">{localize(KIND_LABELS[preview.kind])}</p>
              </div>
              <pre className="border-border-light bg-surface-tertiary text-text-primary max-h-52 overflow-auto rounded-lg border p-3 font-mono text-xs break-words whitespace-pre-wrap">
                {preview.body || localize('com_ui_no_arguments')}
              </pre>
              {preview.truncated && (
                <p className="text-text-secondary mt-1 text-xs">
                  {localize('com_ui_preview_truncated')}
                </p>
              )}
              {config != null ? (
                <ToolApproval
                  approval={{
                    actionId: pendingAction.actionId,
                    allowed_decisions: config.allowed_decisions,
                    remember_scope: config.remember_scope,
                    remember_unavailable: config.remember_unavailable,
                    description: preview.description,
                    allow_always: config.allow_always === true,
                  }}
                  toolCallId={request.tool_call_id}
                  args={request.arguments}
                  surface="composer"
                />
              ) : (
                <p className="text-text-warning mt-2 flex items-center text-xs" role="alert">
                  <TriangleAlert className="me-1.5 size-4" aria-hidden="true" />
                  {localize('com_ui_approval_unavailable')}
                </p>
              )}
            </section>
          ))}
        </div>
        <div className="border-border-light flex shrink-0 items-center justify-between gap-3 border-t px-3 py-2">
          <span className="text-text-secondary text-xs">
            {localize('com_ui_decisions_selected', {
              0: decisions.length,
              1: reviews.length,
            })}
          </span>
          <div className="flex items-center gap-2">
            {(status === 'expired' || status === 'error') && (
              <span className="text-text-warning flex items-center text-xs" role="alert">
                <TriangleAlert className="me-1.5 size-4" aria-hidden="true" />
                {localize(
                  status === 'expired' ? 'com_ui_approval_expired' : 'com_ui_approval_error',
                )}
              </span>
            )}
            <Button
              type="button"
              size="sm"
              variant="submit"
              disabled={!isReady(pendingAction.actionId) || locked}
              onClick={() => submitToolApproval(pendingAction.actionId)}
            >
              {status === 'submitting'
                ? localize('com_ui_submitting')
                : localize('com_ui_continue')}
            </Button>
          </div>
        </div>
      </div>
    </div>
  );
});

export const PendingToolApprovalButton = memo(function PendingToolApprovalButton({
  conversationId,
  triggerRef,
}: {
  conversationId: string;
  triggerRef?: RefObject<HTMLButtonElement>;
}) {
  const localize = useLocalize();
  const pendingAction = usePendingToolApproval(conversationId);
  const [open, setOpen] = useAtom(approvalPanelOpenFamily(conversationId));

  if (pendingAction == null) {
    return null;
  }

  const count = pendingAction.payload.action_requests.length;
  return (
    <Button
      ref={triggerRef}
      type="button"
      variant="quiet"
      size="sm"
      aria-expanded={open}
      aria-controls="pending-tool-approval-panel"
      onClick={() => setOpen((current) => !current)}
      className="h-8 gap-1.5 rounded-full px-2"
      data-testid="pending-tool-approval-button"
    >
      <ShieldQuestion className="size-4" aria-hidden="true" />
      <span>
        {localize(count === 1 ? 'com_ui_review_action' : 'com_ui_review_actions', { 0: count })}
      </span>
      {open ? (
        <ChevronDown className="size-3.5" aria-hidden="true" />
      ) : (
        <ChevronUp className="size-3.5" aria-hidden="true" />
      )}
    </Button>
  );
});