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

14.2 KB
Raw Blame
import { memo, useRef, useMemo, useState, useLayoutEffect } from 'react';
import { useAtomValue } from 'jotai';
import * as Ariakit from '@ariakit/react';
import { ChevronDown } from 'lucide-react';
import { Constants } from 'librechat-data-provider';
import { getAppendParentMessageId } from '@librechat/chat';
import { queuedMessagesByConvoId } from '@librechat/chat/react';
import { Button, TooltipAnchor, useMediaQuery } from '@librechat/client';
import type {
  TMessage,
  TConversation,
  SettingDefinition,
  TReasoningOverride,
} from 'librechat-data-provider';
import { ReasoningControl, useComposerReasoning } from '../Reasoning';
import Effort, { effortRank, resolveEffortLabel } from './Effort';
import { useGetStartupConfig } from '~/data-provider';
import { useChatContext } from '~/Providers';
import { cn, getModelSpec } from '~/utils';
import { useLocalize } from '~/hooks';

interface ThinkingControlProps {
  index: number;
  setting: SettingDefinition;
  conversation: TConversation | null;
  value?: TReasoningOverride;
  disabled: boolean;
  onChange: (value: TReasoningOverride) => void;
}

function ThinkingControl({
  index,
  setting,
  conversation,
  value,
  disabled,
  onChange,
}: ThinkingControlProps) {
  const localize = useLocalize();
  /* Ariakit owns the open state rather than a controlled `open`/`setOpen` pair:
     with the controlled form, hide-on-interact-outside fired on mousedown and
     the disclosure's own click re-opened it, so a second click never closed the
     popup. */
  /* Above the composer on a phone, where the composer sits at the bottom of the
     screen and a popup below it would open off the edge or under the keyboard. */
  const isMobile = useMediaQuery('(max-width: 767px)');
  const popover = Ariakit.usePopoverStore({ placement: isMobile ? 'top' : 'bottom' });
  const open = popover.useState('open');
  const disclosureRef = useRef<HTMLButtonElement>(null);
  const ghostsRef = useRef<HTMLSpanElement>(null);
  const currentRef = useRef<HTMLSpanElement>(null);
  const [slotWidth, setSlotWidth] = useState<number>();

  /* The conversation only holds a value once the user touches the control; an
     admin can still override the setting's default (e.g. reasoning_effort:
     high), and labelling that state Auto would disagree with what actually
     runs, and with the Parameters panel. */
  const raw =
    value?.key === setting.key
      ? value.value
      : (conversation?.[setting.key as keyof TConversation] ?? setting.default);
  const currentValue = raw == null ? undefined : String(raw);
  const display =
    currentValue == null
      ? localize('com_ui_auto')
      : resolveEffortLabel(setting, currentValue, localize);

  /* The word on the button moves with the level: up for a higher one, down for
     a lower one, the outgoing word staying behind as a blurred ghost until it
     has cleared. `turn` restarts the animation for each change. */
  const rank = effortRank(setting, currentValue);
  const [label, setLabel] = useState({
    text: display,
    rank,
    previous: null as string | null,
    up: true,
    turn: 0,
  });
  if (label.text !== display) {
    setLabel({
      text: display,
      rank,
      previous: label.text,
      up: rank >= label.rank,
      turn: label.turn + 1,
    });
  }
  const clearPrevious = (turn: number) =>
    setLabel((state) => (state.turn === turn ? { ...state, previous: null } : state));

  /* Every label this button can show, in the active language. All of them get
     measured rather than picking by character count: the longest string is not
     the widest one in a proportional font, and even less so across languages. */
  const optionLabels = useMemo(() => {
    const seen = new Set<string>([localize('com_ui_auto')]);
    for (const option of setting.options ?? []) {
      const raw = String(option);
      seen.add(resolveEffortLabel(setting, raw, localize));
    }
    return [...seen];
  }, [setting, localize]);

  /* Both candidate widths come from out-of-flow ghosts. The visible label is a
     block that fills the slot, so measuring it just returned the slot's current
     width and the button never shrank back down.
     Measured after commit, not during render: a ref read mid-render returns the
     previous layout, so closing right after a level change animated to the old
     label's width. */
  useLayoutEffect(() => {
    const ghosts = ghostsRef.current;
    const widest =
      ghosts != null
        ? Math.max(0, ...Array.from(ghosts.children, (el) => (el as HTMLElement).offsetWidth))
        : 0;
    const measured = open ? widest : (currentRef.current?.offsetWidth ?? 0);
    if (measured > 0) {
      setSlotWidth(measured);
    }
  }, [open, display, optionLabels]);

  /* Opens downward on wider screens; Ariakit flips it above on its own once
     the composer sits low enough in the viewport that there is no room below. */
  return (
    <Ariakit.PopoverProvider store={popover}>
      {/* Named on hover like the mic and send buttons: closed, the trigger shows
          only the level, which says nothing about what it sets. */}
      <Ariakit.PopoverDisclosure
        ref={disclosureRef}
        data-testid="composer-thinking-button"
        onClick={(event) => event.stopPropagation()}
        disabled={disabled}
        aria-label={localize('com_ui_composer_thinking_value', { 0: display })}
        render={
          <TooltipAnchor
            description={localize('com_ui_composer_thinking')}
            render={<Button variant="composer-pill" />}
          />
        }
        className="flex shrink-0"
      >
        {/* Closed, the button hugs its label so it takes no more room in the bar
            than it needs. Open, it widens to the longest label and the text
            centres, so changing levels while the popup is up never shifts the
            row. A level change never dips the label to transparent (a plain
            keyed crossfade did, and read as a flicker): the new word arrives
            while the old one is still leaving, so one is always legible. */}
        <span
          /* `composer-slot-resize` runs on `animate-composer-popover`'s clock,
             so the button and the popup resize together. */
          className="composer-slot-resize relative block overflow-hidden"
          style={{ width: slotWidth }}
        >
          <span
            ref={ghostsRef}
            aria-hidden="true"
            className="pointer-events-none invisible absolute top-0 left-0 w-max"
          >
            {optionLabels.map((text) => (
              <span key={text} className="block whitespace-nowrap">
                {text}
              </span>
            ))}
          </span>
          <span
            ref={currentRef}
            aria-hidden="true"
            className="pointer-events-none invisible absolute top-0 left-0 w-max whitespace-nowrap"
          >
            {display}
          </span>
          {/* Always centred: flipping alignment as the width animated made the
              label jump sideways mid-transition. Closed the slot is exactly the
              label's width, so centred and left are identical anyway. */}
          <span className="grid text-center whitespace-nowrap">
            {label.previous != null && (
              <span
                key={`out-${label.turn}`}
                aria-hidden="true"
                onAnimationEnd={() => clearPrevious(label.turn)}
                className={cn(
                  'col-start-1 row-start-1',
                  label.up ? 'composer-label-out-up' : 'composer-label-out-down',
                )}
              >
                {label.previous}
              </span>
            )}
            <span
              key={`in-${label.turn}`}
              className={cn(
                'col-start-1 row-start-1',
                label.turn > 0 && (label.up ? 'composer-label-in-up' : 'composer-label-in-down'),
              )}
            >
              {display}
            </span>
          </span>
        </span>
        {/* Turns to point at the popup, which is the only cue that the button
            and the panel below it are one control. */}
        <ChevronDown
          className={cn('animate-composer-icon size-3 shrink-0 opacity-60', open && '-rotate-180')}
          aria-hidden="true"
        />
      </Ariakit.PopoverDisclosure>
      <Ariakit.Popover
        portal
        data-chat-pane-portal={index}
        gutter={8}
        unmountOnHide
        onClick={(event) => event.stopPropagation()}
        /* Without this the trigger could not close the popup: mousedown on it
           counts as "outside", so Ariakit hid the popup and the button's own
           click immediately re-opened it. Excluding the trigger leaves a single
           clean toggle. */
        hideOnInteractOutside={(event) => !disclosureRef.current?.contains(event.target as Node)}
        /* On a phone the button sits inside the composer, so opening above the
           button alone would cover the message field. It rises from the
           composer's top edge instead, still centred over its button. */
        getAnchorRect={
          isMobile
            ? (anchor) => {
                const button = anchor?.getBoundingClientRect();
                const composer = anchor?.closest('form')?.getBoundingClientRect();
                if (button == null || composer == null) {
                  return null;
                }
                return { x: button.x, y: composer.y, width: button.width, height: 0 };
              }
            : undefined
        }
        aria-label={localize('com_ui_composer_thinking_value', { 0: display })}
        /* `border-light` resolves to the same value as `surface-tertiary`, so
           the edge was invisible against the popup's own background. */
        className="animate-composer-popover border-border-medium bg-surface-tertiary rounded-theme-popover z-50 border shadow-lg outline-hidden"
      >
        <Effort setting={setting} conversation={conversation} value={value} onChange={onChange} />
      </Ariakit.Popover>
    </Ariakit.PopoverProvider>
  );
}

/**
 * Reasoning effort in the composer footer, the one model parameter worth
 * reaching for mid-conversation, which otherwise lives three clicks away in the
 * parameters panel under a different name per provider.
 *
 * Model and agent selection deliberately stay in the header; this owns effort
 * only. Renders nothing at all for models that expose no reasoning parameter,
 * and nothing when `interface.parameters` is off (the same gate as the
 * Parameters side panel; model specs default that flag to false).
 *
 * Split in two so the control's hooks are never behind that early return.
 */
/**
 * The level a branch runs at, by the server's rule: the override on the nearest user message at or
 * above `fromMessageId` that carries one.
 */
function getBranchReasoning(
  messages: TMessage[],
  fromMessageId: string,
): TMessage['reasoningOverride'] {
  const byId = new Map<string, TMessage>();
  for (const message of messages) {
    byId.set(message.messageId, message);
  }
  const visited = new Set<string>();
  let current = byId.get(fromMessageId);
  while (current != null && !visited.has(current.messageId)) {
    visited.add(current.messageId);
    if (current.isCreatedByUser === true && current.reasoningOverride != null) {
      return current.reasoningOverride;
    }
    current = current.parentMessageId != null ? byId.get(current.parentMessageId) : undefined;
  }
  return undefined;
}

interface ThinkingProps {
  index: number;
  disabled: boolean;
  hasAddedConversation: boolean;
}

function Thinking({ index, disabled, hasAddedConversation }: ThinkingProps) {
  const { conversation, latestMessageId, getMessages } = useChatContext();
  const { data: startupConfig } = useGetStartupConfig();
  const parametersEnabled = startupConfig?.interface?.parameters;
  const modelSpec = getModelSpec({ specName: conversation?.spec, startupConfig });
  const blockedReasoningKeys = useMemo(() => {
    if (startupConfig?.modelSpecs?.enforce !== true || modelSpec?.preset == null) {
      return undefined;
    }
    return new Set(Object.keys(modelSpec.preset));
  }, [modelSpec?.preset, startupConfig?.modelSpecs?.enforce]);
  const queueKey =
    conversation?.conversationId != null && conversation.conversationId !== ''
      ? conversation.conversationId
      : Constants.NEW_CONVO;
  const queued = useAtomValue(queuedMessagesByConvoId(queueKey));
  /* The server runs a turn without its own pick at the level its branch last ran at, so the
     button names that level: the latest pick still waiting in the queue, otherwise the one the
     branch the composer extends carries. */
  const queuedReasoning = useMemo(() => {
    for (let i = queued.length - 1; i >= 0; i--) {
      const row = queued[i];
      /* A held row waits for an explicit send, so it is not the turn that runs next. */
      if (row.heldByUser === true || row.needsExplicitSend === true) {
        continue;
      }
      if (row.reasoningOverride != null) {
        return row.reasoningOverride;
      }
    }
    return undefined;
  }, [queued]);
  const branchReasoning = useMemo(() => {
    if (queuedReasoning != null) {
      return queuedReasoning;
    }
    const messages = getMessages(conversation?.conversationId) ?? [];
    const latestMessage = messages.find((message) => message.messageId === latestMessageId);
    return getBranchReasoning(
      messages,
      getAppendParentMessageId({ latestMessage: latestMessage ?? null, currentMessages: messages }),
    );
  }, [conversation?.conversationId, getMessages, latestMessageId, queuedReasoning]);
  const reasoning = useComposerReasoning({
    conversation: conversation ?? null,
    index,
    hasAddedConversation,
    enabled: parametersEnabled,
    blockedReasoningKeys,
    branchReasoning,
  });

  if (parametersEnabled !== true || reasoning == null) {
    return null;
  }

  if (reasoning.setting.type !== 'enum' || reasoning.setting.options?.length === 0) {
    return (
      <ReasoningControl
        index={index}
        setting={reasoning.setting}
        value={reasoning.value}
        disabled={disabled}
        onChange={reasoning.setValue}
      />
    );
  }

  return (
    <ThinkingControl
      index={index}
      setting={reasoning.setting}
      conversation={conversation ?? null}
      value={reasoning.value}
      disabled={disabled}
      onChange={reasoning.setValue}
    />
  );
}

export default memo(Thinking);