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

8.4 KB
Raw Blame
import { memo, useMemo, useState, useEffect, useCallback } from 'react';
import {
  SteerReceipt,
  FileContainer,
  MessageQuotes,
  CollapsibleText,
  MessageTimestamp,
} from '@librechat/chat/components';
import type { SteerReceiptState } from '@librechat/chat/components';
import type { TFile, TMessage } from 'librechat-data-provider';
import FilePreviewDialog from '~/components/Chat/Messages/Content/FilePreviewDialog';
import { cn, hydrateFileDeliveryMetadata, usesImagePreview } from '~/utils';
import MarkdownLite from '~/components/Chat/Messages/Content/MarkdownLite';
import { useMessagePartsHost } from '~/Providers/MessagePartsHostContext';
import Image from '~/components/Chat/Messages/Content/Image';
import { useShareContext } from '~/Providers';
import { useLocalize } from '~/hooks';

/**
 * A mid-run steering message rendered as a standard user message inside the
 * assistant response, with the same compact surface and text presentation as any
 * user turn, placed where the words enter the run so the visible order equals
 * what the next turn replays (`ContentTypes.STEER` splits back into a
 * HumanMessage server-side). Only the server-applied part renders here, at its
 * authoritative index; a steer still in flight lives in the composer's chip
 * stack. Renders identically live, on reload, and in shared/search views.
 */
const SteerPart = memo(function SteerPart({
  steer,
  files,
  quotes,
  steerId,
  createdAt,
  isSubmitting = false,
  receiptState = 'applied',
  hasMore = false,
  onExpand,
}: {
  steer: string;
  hasMore?: boolean;
  onExpand?: () => void;
  files?: TMessage['files'];
  /** Quoted excerpts steered with the message; rendered as the same reference
   *  blocks a user bubble shows for `message.quotes`. */
  quotes?: string[];
  /** Anchors the part for the message-nav rail (`#steer-<id>` rib target). */
  steerId?: string;
  createdAt?: number;
  /** The owning response is still generating: the receipt keeps its amber
   *  steering identity while it is the live thing at the end, then settles
   *  to timestamp gray (always settled on reload, share, and search). */
  isSubmitting?: boolean;
  /** Pending renderer state must never claim server application early. */
  receiptState?: SteerReceiptState | null;
}) {
  const localize = useLocalize();
  const { useUser, useFileMap, useUserTextPreferences, useLiveAppliedSteer } =
    useMessagePartsHost();
  const user = useUser();
  const { isSharedConvo } = useShareContext();
  const fileMap = useFileMap();
  const { usernameDisplay, enableUserMsgMarkdown, collapseLongUserMessages } =
    useUserTextPreferences();

  /** The share surface must never label the SHARER's steers with the
   *  viewer's identity; always the generic user label there. */
  const label = useMemo(() => {
    if (isSharedConvo === true || !usernameDisplay) {
      return localize('com_user_message');
    }
    return (user?.name ?? '') || user?.username || localize('com_user_message');
  }, [isSharedConvo, usernameDisplay, user?.name, user?.username, localize]);
  const timestamp = useMemo(
    () => (createdAt != null ? new Date(createdAt).toISOString() : null),
    [createdAt],
  );
  const hydratedFiles = useMemo(
    () => hydrateFileDeliveryMetadata(files, undefined, fileMap),
    [files, fileMap],
  );
  const imageFiles = useMemo(() => hydratedFiles?.filter(usesImagePreview) ?? [], [hydratedFiles]);
  const otherFiles = useMemo(
    () => hydratedFiles?.filter((file) => !usesImagePreview(file)) ?? [],
    [hydratedFiles],
  );
  useEffect(() => {
    if (hasMore && !collapseLongUserMessages) onExpand?.();
  }, [hasMore, collapseLongUserMessages, onExpand]);
  const [selectedFile, setSelectedFile] = useState<Partial<TFile> | null>(null);
  const handlePreviewClose = useCallback((open: boolean) => {
    if (!open) {
      setSelectedFile(null);
    }
  }, []);

  /** The receipt draw-in plays exactly once, at the live chip→inline hand-off:
   *  the applied handler stamps the id as it commits the part, the render that
   *  first sees each identity captures it, and the effect consumes it so a
   *  remount (conversation revisit, reload, share view) renders the settled
   *  checks without motion. Captured per IDENTITY, not per mount — a content
   *  slot can be overwritten with a different steer (`applySteerPart` permits
   *  replacement at an index) while React reuses this component — and every
   *  identity consumes its id whether it animated or not, so nothing lingers.
   *  The membership selector scopes the subscription to THIS id — stamping or
   *  consuming one steer never re-renders the other mounted parts. */
  const [isLiveApplied, consumeLiveApplied] = useLiveAppliedSteer(steerId ?? '');
  const [captured, setCaptured] = useState<{ id: string | undefined; animate: boolean }>({
    id: steerId,
    animate: isLiveApplied,
  });
  if (captured.id !== steerId) {
    setCaptured({ id: steerId, animate: isLiveApplied });
  }
  const animateIn = captured.id === steerId && captured.animate;
  useEffect(() => {
    if (steerId == null || steerId.length === 0) {
      return;
    }
    consumeLiveApplied(steerId);
  }, [steerId, consumeLiveApplied]);

  if (typeof steer !== 'string' || steer.length === 0) {
    return null;
  }

  return (
    <div
      id={steerId ? `steer-${steerId}` : undefined}
      data-steer-render
      className="group relative my-5 flex w-full justify-end"
      data-testid="steer-part"
    >
      <div className="user-turn relative flex w-fit max-w-[90%] flex-col items-end sm:max-w-[85%]">
        <h2 className="sr-only">{label}</h2>
        <div className="rounded-theme-surface rounded-ee-theme-control bg-surface-user-message px-theme-normal flex max-w-full flex-col items-start gap-2 py-2.5">
          <MessageQuotes quotes={quotes} />
          {(imageFiles.length > 0 || otherFiles.length > 0) && (
            <div className="flex w-full flex-wrap gap-2">
              {otherFiles.map((file) => (
                <FileContainer
                  key={file.file_id}
                  file={file as TFile}
                  onClick={() => setSelectedFile(file)}
                />
              ))}
              {imageFiles.length > 0 && (
                <div className="ms-auto flex max-w-full flex-wrap justify-end gap-2">
                  {imageFiles.map((file) => (
                    <Image
                      key={file.file_id}
                      imagePath={file.preview ?? file.filepath ?? ''}
                      height={file.height ?? 1920}
                      width={file.width ?? 1080}
                      altText={file.filename ?? localize('com_ui_attached_image')}
                    />
                  ))}
                </div>
              )}
            </div>
          )}
          <CollapsibleText
            enabled={collapseLongUserMessages || hasMore}
            hasMore={hasMore}
            onExpand={onExpand}
          >
            <div
              className={cn(
                'markdown prose message-content light w-full break-words',
                !enableUserMsgMarkdown && 'whitespace-pre-wrap',
                'text-text-primary',
              )}
            >
              {enableUserMsgMarkdown ? <MarkdownLite content={steer} /> : steer}
            </div>
          </CollapsibleText>
        </div>
        {/* The receipt trails the timestamp so the always-visible checks sit
         *  flush under the bubble's bottom-right corner. Leading them would
         *  park them wherever the hover-revealed time happens to end, and that
         *  width changes as the relative string ticks. */}
        <div className="text-text-secondary mt-1 flex min-h-8 items-center justify-end gap-2">
          <MessageTimestamp value={timestamp} className="ms-0" />
          {receiptState != null && (
            <SteerReceipt state={receiptState} live={isSubmitting} animateIn={animateIn} />
          )}
        </div>
      </div>
      {otherFiles.length > 0 && (
        <FilePreviewDialog
          open={selectedFile !== null}
          onOpenChange={handlePreviewClose}
          fileName={selectedFile?.filename ?? ''}
          fileId={selectedFile?.file_id}
          filePath={selectedFile?.filepath}
          fileType={selectedFile?.type ?? undefined}
          fileSource={selectedFile?.source}
          fileSize={(selectedFile as TFile | null)?.bytes}
          deliveryPath={selectedFile?.llmDeliveryPath}
        />
      )}
    </div>
  );
});

export default SteerPart;