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

6.5 KB
Raw Blame
import * as Ariakit from '@ariakit/react';
import { TooltipAnchor } from '@librechat/client';
import { useQueryClient } from '@tanstack/react-query';
import { QueryKeys, Constants } from 'librechat-data-provider';
import { Check, ChevronDown, FilePen, FileQuestionMark, FileTerminal } from 'lucide-react';
import type { TConversation, CodeApprovalMode, CodeEnvironmentMode } from 'librechat-data-provider';
import type { LucideIcon } from 'lucide-react';
import type { SetterOrUpdater } from 'recoil';
import type { TranslationKeys } from '~/hooks';
import { chipClasses, chipMenuClasses, chipMenuItemClasses, chipMenuHeadingClasses } from './chip';
import { useCodeApprovalModePreference } from '~/hooks/Agents/codeApprovalPreference';
import { useCodeApprovalMode, useLocalize } from '~/hooks';
import { cn } from '~/utils';

/** The modes answer one question — what may happen to the workspace without the
 *  reader — so they share the document glyph and differ only in what is drawn on
 *  it, escalating with what each mode permits: a question the reader answers, a
 *  pen for writes that need no answer, a prompt for commands that need none
 *  either. The shield stays with the pending review button, which sits in this
 *  same row. */
const modeOptions: Record<
  CodeApprovalMode,
  { icon: LucideIcon; label: TranslationKeys; description: TranslationKeys }
> = {
  ask: {
    icon: FileQuestionMark,
    label: 'com_ui_code_approval_ask',
    description: 'com_ui_code_approval_ask_description',
  },
  acceptEdits: {
    icon: FilePen,
    label: 'com_ui_code_approval_accept_edits',
    description: 'com_ui_code_approval_accept_edits_description',
  },
  fullAccess: {
    icon: FileTerminal,
    label: 'com_ui_code_approval_full_access',
    description: 'com_ui_code_approval_full_access_description',
  },
};

export default function CodeApprovalMenu({
  conversation,
  addedConversation,
  codeEnvironmentMode,
  setConversation,
  disabled,
}: {
  conversation: TConversation | null;
  addedConversation?: TConversation | null;
  /** The workspace mode the composer resolved for the next turn. */
  codeEnvironmentMode?: CodeEnvironmentMode;
  setConversation: SetterOrUpdater<TConversation | null>;
  disabled: boolean;
}) {
  const localize = useLocalize();
  const queryClient = useQueryClient();
  const { available, modes, selected } = useCodeApprovalMode(
    conversation,
    addedConversation,
    codeEnvironmentMode,
  );
  const preference = useCodeApprovalModePreference();
  const menuStore = Ariakit.useMenuStore({ focusLoop: true, placement: 'top-start' });
  const isOpen = menuStore.useState('open');

  if (!available || selected == null) {
    return null;
  }

  /** Navigation and run recovery rebuild the conversation from its detail
   *  cache, so the pick lands there too, seeding the record from the live
   *  conversation when none exists yet (the resumable transport seeds the same
   *  key optimistically). A chat that has no id yet keeps the pick in
   *  conversation state alone until the run assigns one. The pick is also this
   *  browser's remembered default, so the next chat opens on it rather than back
   *  at `ask`; policy is re-checked before it is ever shown or submitted. */
  const selectMode = (mode: CodeApprovalMode) => {
    if (!modes.includes(mode)) {
      return;
    }
    preference.remember(mode);
    setConversation((current) =>
      current == null ? current : { ...current, codeApprovalMode: mode },
    );
    const record = conversation;
    const conversationId = record?.conversationId;
    if (record == null || conversationId == null || conversationId === Constants.NEW_CONVO) {
      return;
    }
    queryClient.setQueryData<TConversation>([QueryKeys.conversation, conversationId], (cached) => ({
      ...(cached ?? record),
      codeApprovalMode: mode,
    }));
  };

  const SelectedIcon = modeOptions[selected].icon;

  return (
    <Ariakit.MenuProvider store={menuStore}>
      <TooltipAnchor
        description={localize('com_ui_code_approval_mode')}
        /** `disabled` here would mark the open chip itself `aria-disabled`; only the hover
         *  tooltip has to stand down while the menu covers it. */
        showOnHover={!isOpen}
        render={
          <Ariakit.MenuButton
            disabled={disabled}
            data-testid="code-approval-mode"
            aria-label={`${localize('com_ui_code_approval_mode')}: ${localize(
              modeOptions[selected].label,
            )}`}
            className={cn(
              chipClasses,
              isOpen && 'bg-surface-hover',
              disabled && 'cursor-not-allowed opacity-50',
            )}
          />
        }
      >
        <SelectedIcon className="text-text-secondary size-4 shrink-0" aria-hidden="true" />
        <span className="max-w-[12rem] min-w-0 truncate">
          {localize(modeOptions[selected].label)}
        </span>
        <ChevronDown
          className={cn(
            'text-text-secondary size-3 shrink-0 transition-transform',
            isOpen && 'rotate-180',
          )}
          aria-hidden="true"
        />
      </TooltipAnchor>
      <Ariakit.Menu portal={true} gutter={8} unmountOnHide={true} className={chipMenuClasses}>
        {/* Names the menu without adding an `h1` to the page outline. */}
        <Ariakit.MenuHeading render={<div />} className={chipMenuHeadingClasses}>
          {localize('com_ui_code_approval_mode')}
        </Ariakit.MenuHeading>
        {modes.map((mode) => {
          const { icon: Icon, label, description } = modeOptions[mode];
          const isSelected = mode === selected;
          return (
            <Ariakit.MenuItemRadio
              key={mode}
              name="codeApprovalMode"
              value={mode}
              checked={isSelected}
              hideOnClick={true}
              onChange={() => selectMode(mode)}
              className={cn(chipMenuItemClasses(isSelected), 'items-start')}
            >
              <Icon className="text-text-secondary mt-0.5 size-4 shrink-0" aria-hidden="true" />
              <div className="min-w-0 flex-1 text-start">
                <div className="text-text-primary text-sm font-medium">{localize(label)}</div>
                <p className="text-text-secondary text-xs">{localize(description)}</p>
              </div>
              {isSelected && (
                <Check className="text-text-primary mt-0.5 size-4 shrink-0" aria-hidden="true" />
              )}
            </Ariakit.MenuItemRadio>
          );
        })}
      </Ariakit.Menu>
    </Ariakit.MenuProvider>
  );
}