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

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

Marco Berettadd570269a905
diff --git a/client/src/Providers/MessagePartsHostContext.tsx b/client/src/Providers/MessagePartsHostContext.tsx
index 7c7cada637..6b00fa3e44 100644
--- a/client/src/Providers/MessagePartsHostContext.tsx
+++ b/client/src/Providers/MessagePartsHostContext.tsx
@@ -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);
diff --git a/client/src/Providers/MessagesViewContext.tsx b/client/src/Providers/MessagesViewContext.tsx
index 47c56a8cdf..79d9bcb12f 100644
--- a/client/src/Providers/MessagesViewContext.tsx
+++ b/client/src/Providers/MessagesViewContext.tsx
@@ -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);
/**
diff --git a/client/src/components/Chat/Messages/Content/EditContentParts.tsx b/client/src/components/Chat/Messages/Content/EditContentParts.tsx
index 5399d642fb..ef25f38c1f 100644
--- a/client/src/components/Chat/Messages/Content/EditContentParts.tsx
+++ b/client/src/components/Chat/Messages/Content/EditContentParts.tsx
@@ -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);
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/host.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/host.test.tsx
index 3705f276dd..d553d93eae 100644
--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/host.test.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/host.test.tsx
@@ -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,
};
}
diff --git a/client/src/components/Chat/Messages/Content/UIResourceCarousel.tsx b/client/src/components/Chat/Messages/Content/UIResourceCarousel.tsx
index dc51602b0a..4b784bc34a 100644
--- a/client/src/components/Chat/Messages/Content/UIResourceCarousel.tsx
+++ b/client/src/components/Chat/Messages/Content/UIResourceCarousel.tsx
@@ -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],
diff --git a/client/src/hooks/Chat/contract.ts b/client/src/hooks/Chat/contract.ts
index 87b568a723..f75c90604b 100644
--- a/client/src/hooks/Chat/contract.ts
+++ b/client/src/hooks/Chat/contract.ts
@@ -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'];
}
}
diff --git a/packages/chat/src/components/__tests__/host.spec.tsx b/packages/chat/src/components/__tests__/host.spec.tsx
index 228bb480b3..2f01f665ae 100644
--- a/packages/chat/src/components/__tests__/host.spec.tsx
+++ b/packages/chat/src/components/__tests__/host.spec.tsx
@@ -30,6 +30,7 @@ function createHost(enableUserMsgMarkdown = false): MessagePartsHost {
useShowThinking: () => false,
useAutoExpandTools: () => false,
useLaTeXParsing: () => false,
+ useChatDirection: () => 'ltr',
useUserTextPreferences: () => ({
usernameDisplay: false,
enableUserMsgMarkdown,
diff --git a/packages/chat/src/react/__tests__/host.spec.tsx b/packages/chat/src/react/__tests__/host.spec.tsx
index 194a5a292b..fbfc4e836e 100644
--- a/packages/chat/src/react/__tests__/host.spec.tsx
+++ b/packages/chat/src/react/__tests__/host.spec.tsx
@@ -15,6 +15,7 @@ const createHost = (fontSize: string): MessagePartsHost => ({
useShowThinking: () => false,
useAutoExpandTools: () => false,
useLaTeXParsing: () => false,
+ useChatDirection: () => 'ltr',
useUserTextPreferences: () => ({
usernameDisplay: false,
enableUserMsgMarkdown: false,
diff --git a/packages/chat/src/react/host.tsx b/packages/chat/src/react/host.tsx
index 58296c54ad..5bb2e699d9 100644
--- a/packages/chat/src/react/host.tsx
+++ b/packages/chat/src/react/host.tsx
@@ -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. */