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

🧻 fix: Clear Stale Draft Text After Sending (#16444)

lia-by-librechat[bot]a9b491608390
diff --git a/client/src/components/Chat/Input/ChatForm.tsx b/client/src/components/Chat/Input/ChatForm.tsx
index f23fb61ffa..b92787c0ad 100644
--- a/client/src/components/Chat/Input/ChatForm.tsx
+++ b/client/src/components/Chat/Input/ChatForm.tsx
@@ -7,6 +7,17 @@ import type { TChatProject, TMessage, TConversation } from 'librechat-data-provi
import type { SetterOrUpdater } from 'recoil';
import type { ExtendedFile, FileSetter, ConvoGenerator } from '~/common';
import type { QueuedMessageContext } from '~/hooks/Chat/useSteering';
+import {
+ cn,
+ getModelSpec,
+ hasIncompleteFiles,
+ removeFocusRings,
+ getComposerDraftId,
+ getPendingDraftId,
+ clearAllDrafts,
+ getFilesDraftCached,
+ isPastedTextFileMarked,
+} from '~/utils';
import {
useTextarea,
useAutoSave,
@@ -18,15 +29,6 @@ import {
useFocusChatEffect,
useCodeWorkspace,
} from '~/hooks';
-import {
- cn,
- getModelSpec,
- hasIncompleteFiles,
- removeFocusRings,
- getComposerDraftId,
- getFilesDraftCached,
- isPastedTextFileMarked,
-} from '~/utils';
import {
useChatContext,
useChatFormContext,
@@ -625,12 +627,18 @@ const ChatForm = memo(function ChatForm({
{
answerMode,
steering,
- submitMessage,
+ submitMessage: (message) => {
+ const result = submitMessage(message);
+ if (result !== false) {
+ clearAllDrafts(getPendingDraftId(index));
+ }
+ return result;
+ },
reset: () => methods.reset(),
},
data,
),
- [answerMode, steering, submitMessage, methods],
+ [answerMode, steering, submitMessage, methods, index],
);
return (
diff --git a/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx b/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx
index 2364da87c6..cf7d206569 100644
--- a/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx
+++ b/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx
@@ -10,13 +10,15 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { QueryKeys, FileSources, EModelEndpoint } from 'librechat-data-provider';
import { render, screen, within, waitFor, fireEvent } from '@testing-library/react';
import type { TFile, TFileUpload, TConversation } from 'librechat-data-provider';
-import type { ChatFormValues } from '~/common';
+import type { ChatFormValues, TAskFunction } from '~/common';
+import { getDraft, getPendingDraftId, setDraft } from '~/utils';
import ChatForm, { toRestoredComposerFile } from '../ChatForm';
import { ChatContext, ChatFormProvider } from '~/Providers';
import { AuthContextProvider } from '~/hooks/AuthContext';
import store from '~/store';
const mockUpload = jest.fn();
+const mockAsk = jest.fn();
jest.mock('librechat-data-provider', () => {
const actual = jest.requireActual('librechat-data-provider');
@@ -68,7 +70,7 @@ let commits = 0;
function Harness() {
const [files, setFiles] = useRecoilState(store.filesByIndex(0));
- const [isSubmitting] = useRecoilState(store.isSubmittingFamily(0));
+ const [isSubmitting, setIsSubmitting] = useRecoilState(store.isSubmittingFamily(0));
const [, setFilesLoading] = useState(false);
const methods = useForm<ChatFormValues>({ defaultValues: { text: '' } });
@@ -89,7 +91,13 @@ function Harness() {
stopGenerating: () => undefined,
getMessages: () => undefined,
setMessages: () => undefined,
- ask: () => undefined,
+ ask: (...args: Parameters<TAskFunction>) => {
+ const result = mockAsk(...args);
+ if (result !== false) {
+ setIsSubmitting(true);
+ }
+ return result;
+ },
regenerate: () => undefined,
setSiblingIdx: () => undefined,
showPopover: false,
@@ -103,7 +111,7 @@ function Harness() {
handleRegenerate: () => undefined,
handleContinue: () => undefined,
}) as unknown as React.ContextType<typeof ChatContext>,
- [files, setFiles, isSubmitting],
+ [files, setFiles, isSubmitting, setIsSubmitting],
);
return (
@@ -172,6 +180,7 @@ describe('ChatForm attachments', () => {
global.URL.revokeObjectURL = jest.fn();
(global as unknown as { Image: unknown }).Image = StubImage;
mockUpload.mockReset();
+ mockAsk.mockReset();
/** The server echoes the id the client sent back as `temp_file_id`. */
mockUpload.mockImplementation((body: FormData) =>
Promise.resolve({ ...uploadResponse, temp_file_id: body.get('file_id') as string }),
@@ -238,6 +247,42 @@ describe('ChatForm attachments', () => {
expect(textarea).toHaveFocus();
}, 20000);
+ test('does not restore an older fragment of the sent message while the run starts', async () => {
+ renderComposer();
+ const textarea = await screen.findByTestId('text-input');
+ const message = 'i would like to learn how to use it for a demo or is it too early days';
+ await userEvent.type(textarea, message);
+ setDraft({ id: getPendingDraftId(), value: 'i would like to learn how to use it for a dem' });
+
+ await userEvent.click(sendButton());
+
+ await waitFor(() =>
+ expect(mockAsk).toHaveBeenCalledWith(
+ expect.objectContaining({ text: message }),
+ expect.anything(),
+ ),
+ );
+ expect(textarea).toHaveValue('');
+ expect(getDraft(getPendingDraftId())).toBe('');
+
+ await userEvent.type(textarea, 'a different follow-up');
+ await waitFor(() => expect(getDraft(getPendingDraftId())).toBe('a different follow-up'));
+ }, 20000);
+
+ test('keeps the draft intact when the normal send is refused', async () => {
+ mockAsk.mockReturnValue(false);
+ renderComposer();
+ const textarea = await screen.findByTestId('text-input');
+ await userEvent.type(textarea, 'not sent');
+ setDraft({ id: getPendingDraftId(), value: 'earlier follow-up' });
+
+ await userEvent.click(sendButton());
+
+ await waitFor(() => expect(mockAsk).toHaveBeenCalled());
+ expect(textarea).toHaveValue('not sent');
+ expect(getDraft(getPendingDraftId())).toBe('earlier follow-up');
+ }, 20000);
+
test('still returns focus to the textarea after a plain control click', async () => {
renderComposer();
const textarea = await screen.findByTestId('text-input');