✏️ refactor: Read the Inline Editors' Messages View Through the Parts Host (#17172)

This commit is contained in:
Marco Beretta 2026-10-16 22:48:06 +02:00 • committed by GitHub
parent de9d46fcd4
commit dd570269a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 51 additions and 9 deletions

View file

@ -21,9 +21,17 @@ import type {
MessagePartMessage,
MessagePartsHost,
} from '~/hooks/Chat/contract';
import type { MessagesEditing } from './MessagesViewContext';
import type { Artifact } from '~/common';
import {
useMessageContext,
useFileMapContext,
useAgentsMapContext,
useMessagesOperations,
useMessagesConversation,
useOptionalMessagesOperations,
} from '~/Providers';
import store, { ptcTraceByToolCallId, ptcTraceKey, sandboxStartingByToolCallId } from '~/store';
import { useMessageContext, useFileMapContext, useAgentsMapContext } from '~/Providers';
import { getAppPartRenderers } from '~/components/Chat/Messages/Content/Parts/slots';
import useSmoothStreaming from '~/hooks/Messages/useSmoothStreaming';
import { showThinkingAtom } from '~/store/showThinking';
@ -35,6 +43,15 @@ function useAppMessage(): MessagePartMessage {
return { messageId, isSubmitting, isLatestMessage, nextType, conversationId, partIndex };
}
function useAppMessagesEditing(): MessagesEditing {
const { conversation } = useMessagesConversation();
const { ask, getMessages, setMessages } = useMessagesOperations();
return useMemo(
() => ({ conversation, ask, getMessages, setMessages }),
[conversation, ask, getMessages, setMessages],
);
}
function useAppAgent(agentId: string): Agent | undefined {
const agentsMap = useAgentsMapContext();
return useMemo(() => agentsMap?.[agentId], [agentsMap, agentId]);
@ -113,6 +130,8 @@ export const appMessagePartsHost: MessagePartsHost = {
useShowThinking: () => useAtomValue(showThinkingAtom),
useAutoExpandTools: () => useRecoilValue<boolean>(store.autoExpandTools),
useLaTeXParsing: () => useRecoilValue<boolean>(store.LaTeXParsing),
useChatDirection: () =>
useRecoilValue<string>(store.chatDirection).toLowerCase() === 'rtl' ? 'rtl' : 'ltr',
useUserTextPreferences: useAppUserTextPreferences,
/* The atom rather than the auth context: AuthContextProvider mirrors the user into it, and the
* public share route mounts outside that provider. */
@ -130,6 +149,8 @@ export const appMessagePartsHost: MessagePartsHost = {
useSteerEscalating: (conversationId) => useAtomValue(escalatingSteerFamily(conversationId)),
usePaneConversationId: (index) => useRecoilValue(store.conversationIdByIndex(index)),
useLiveAppliedSteer: useAppLiveAppliedSteer,
useMessagesEditing: useAppMessagesEditing,
useMessagesAsk: () => useOptionalMessagesOperations().ask,
};
setDefaultMessagePartsHost(appMessagePartsHost);

View file

@ -31,6 +31,12 @@ type MessagesOperations = Pick<
'ask' | 'regenerate' | 'handleContinue' | 'getMessages' | 'setMessages'
>;
/** The view's conversation with the operations an inline editor writes through. */
export type MessagesEditing = Pick<
MessagesViewContextValue,
'conversation' | 'ask' | 'getMessages' | 'setMessages'
>;
const MessagesViewContext = createContext<MessagesViewContextValue | undefined>(undefined);
/**

View file

@ -1,5 +1,4 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useRecoilValue } from 'recoil';
import { Alert, Button, TextareaAutosize } from '@librechat/client';
import { useUpdateMessageContentMutation } from 'librechat-data-provider/react-query';
import {
@ -9,13 +8,12 @@ import {
} from 'librechat-data-provider';
import type { TMessageContentParts } from 'librechat-data-provider';
import type { ReactNode } from 'react';
import { useMessagesConversation, useMessagesOperations } from '~/Providers';
import { getPartText, isEditablePart, withPartText } from './editableParts';
import { useMessagePartsHost } from '~/Providers/MessagePartsHostContext';
import useGetAddedConvo from '~/hooks/Conversations/useGetAddedConvo';
import { findRerunParent } from './rerunParent';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
import store from '~/store';
type EditableType = ContentTypes.TEXT | ContentTypes.THINK;
@ -48,9 +46,9 @@ export default function EditContentParts({
renderReadOnlyPart,
}: EditContentPartsProps) {
const localize = useLocalize();
const isRTL = useRecoilValue(store.chatDirection).toLowerCase() === 'rtl';
const { conversation } = useMessagesConversation();
const { ask, getMessages, setMessages } = useMessagesOperations();
const { useChatDirection, useMessagesEditing } = useMessagePartsHost();
const isRTL = useChatDirection() === 'rtl';
const { conversation, ask, getMessages, setMessages } = useMessagesEditing();
const getAddedConvo = useGetAddedConvo();
const firstEditorRef = useRef<HTMLTextAreaElement | null>(null);
const [saveError, setSaveError] = useState(false);

View file

@ -41,6 +41,7 @@ function createHost(overrides: Partial<MessagePartsHost> = {}): MessagePartsHost
useShowThinking: () => false,
useAutoExpandTools: () => false,
useLaTeXParsing: () => false,
useChatDirection: () => 'ltr',
useUserTextPreferences: () => ({
usernameDisplay: false,
enableUserMsgMarkdown: false,
@ -56,6 +57,13 @@ function createHost(overrides: Partial<MessagePartsHost> = {}): MessagePartsHost
useSteerEscalating: () => false,
usePaneConversationId: () => null,
useLiveAppliedSteer: () => [false, jest.fn()],
useMessagesEditing: () => ({
conversation: null,
ask: jest.fn(),
getMessages: () => undefined,
setMessages: jest.fn(),
}),
useMessagesAsk: () => jest.fn(),
...overrides,
};
}

View file

@ -3,7 +3,7 @@ import { Button } from '@librechat/client';
import { ChevronLeft, ChevronRight } from 'lucide-react';
import type { UIResource } from 'librechat-data-provider';
import UIResourceRenderer, { isSupportedUIResource } from '~/components/MCPUIResource/Renderer';
import { useOptionalMessagesOperations } from '~/Providers';
import { useMessagePartsHost } from '~/Providers/MessagePartsHostContext';
import { cn, handleUIAction } from '~/utils';
import { useLocalize } from '~/hooks';
@ -17,7 +17,7 @@ const UIResourceCarousel: React.FC<UIResourceCarouselProps> = React.memo(({ uiRe
const [showRightArrow, setShowRightArrow] = useState(true);
const [isContainerHovered, setIsContainerHovered] = useState(false);
const scrollContainerRef = React.useRef<HTMLDivElement>(null);
const { ask } = useOptionalMessagesOperations();
const ask = useMessagePartsHost().useMessagesAsk();
const supportedUIResources = React.useMemo(
() => uiResources.filter(isSupportedUIResource),
[uiResources],

View file

@ -31,6 +31,7 @@ import type {
Artifact,
PtcTrace,
} from '~/common';
import type { MessagesEditing } from '~/Providers/MessagesViewContext';
import type { PendingSteer } from './queue';
/** Options accepted by {@link ChatConversationContract.newConversation}: the shared params plus
@ -328,5 +329,9 @@ declare module '@librechat/chat/react' {
useArtifactPanel: (artifactId: string) => MessagePartArtifactPanel;
/** Steers sent to a conversation that the server has not confirmed yet. */
usePendingSteers: (conversationId: string) => PendingSteer[];
/** The messages view an inline editor reads and writes; only rendered inside one. */
useMessagesEditing: () => MessagesEditing;
/** The messages view's `ask`, or one that does nothing outside a messages view. */
useMessagesAsk: () => MessagesEditing['ask'];
}
}

View file

@ -30,6 +30,7 @@ function createHost(enableUserMsgMarkdown = false): MessagePartsHost {
useShowThinking: () => false,
useAutoExpandTools: () => false,
useLaTeXParsing: () => false,
useChatDirection: () => 'ltr',
useUserTextPreferences: () => ({
usernameDisplay: false,
enableUserMsgMarkdown,

View file

@ -15,6 +15,7 @@ const createHost = (fontSize: string): MessagePartsHost => ({
useShowThinking: () => false,
useAutoExpandTools: () => false,
useLaTeXParsing: () => false,
useChatDirection: () => 'ltr',
useUserTextPreferences: () => ({
usernameDisplay: false,
enableUserMsgMarkdown: false,

View file

@ -76,6 +76,8 @@ export interface MessagePartsHost {
useAutoExpandTools: () => boolean;
/** Whether the viewer wants LaTeX in markdown rendered as math. */
useLaTeXParsing: () => boolean;
/** The direction the viewer reads chat text in. */
useChatDirection: () => 'ltr' | 'rtl';
/** How the viewer prefers user-authored text to display. */
useUserTextPreferences: () => MessagePartsUserTextPreferences;
/** The signed-in user. */