diff --git a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx
index 329c6e944f..4795aa986d 100644
--- a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx
+++ b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx
@@ -1,4 +1,4 @@
-import React, { useMemo, useState } from 'react';
+import React, { useLayoutEffect, useMemo, useState } from 'react';
import { RecoilRoot } from 'recoil';
import { DndProvider } from 'react-dnd';
import { useForm } from 'react-hook-form';
@@ -34,6 +34,13 @@ const initialConversation = {
title: 'New Chat',
} as TConversation;
+const agentConversation = {
+ ...initialConversation,
+ endpoint: EModelEndpoint.agents,
+ agent_id: 'agent_test',
+ model: undefined,
+} as TConversation;
+
const ask = jest.fn();
const newConversation = jest.fn();
const stopGeneration = jest.fn();
@@ -55,6 +62,7 @@ function Harness({
onAsk = ask,
refusesWhileSubmitting = false,
speechSettingsInitialized = false,
+ onCommit,
}: {
conversation: TConversation;
routePending?: boolean;
@@ -63,7 +71,10 @@ function Harness({
/** Mirrors the real `ask`, which reads `isSubmitting` from the render it was created in. */
refusesWhileSubmitting?: boolean;
speechSettingsInitialized?: boolean;
+ /** Runs in the commit phase, ahead of the passive effects of the same commit. */
+ onCommit?: () => void;
}) {
+ useLayoutEffect(() => onCommit?.());
const [files, setFiles] = useAtom(filesFamily(0));
const [isSubmitting] = useAtom(isSubmittingFamily(0));
const [, setFilesLoading] = useState(false);
@@ -349,6 +360,126 @@ describe('ChatForm URL submission', () => {
expect(view.getLocation().search).toBe('');
});
+ it.each([500, 3500])(
+ 'keeps applied URL settings pending until composer release after %i ms',
+ async (releaseDelay) => {
+ const view = mountComposer(initialConversation, { liveRun: true });
+ await act(async () => jest.advanceTimersByTime(100));
+ view.rerender();
+ await act(async () => jest.advanceTimersByTime(releaseDelay));
+ expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument();
+ expect(screen.getByTestId('text-input')).toHaveValue('hi');
+ expect(view.getLocation().search).toContain('submit=true');
+ expect(ask).not.toHaveBeenCalled();
+
+ await act(async () => view.setSubmitting(false));
+ expect(ask).toHaveBeenCalledTimes(1);
+ expect(ask).toHaveBeenCalledWith(expect.objectContaining({ text: 'hi' }), expect.anything());
+ expect(view.getLocation().search).toBe('');
+ await act(async () => jest.advanceTimersByTime(4000));
+ expect(ask).toHaveBeenCalledTimes(1);
+ },
+ );
+
+ it('still expires unapplied URL settings while the composer is busy', async () => {
+ const view = mountComposer(initialConversation, { liveRun: true });
+ await act(async () => jest.advanceTimersByTime(100));
+ await act(async () => jest.advanceTimersByTime(3000));
+ expect(screen.getByText(/Chat settings could not be applied/)).toBeInTheDocument();
+ expect(screen.getByTestId('text-input')).toHaveValue('hi');
+ expect(view.getLocation().search).toBe('');
+ await act(async () => view.setSubmitting(false));
+ expect(ask).not.toHaveBeenCalled();
+ });
+
+ it('restarts the settings wait when applied URL settings are replaced before composer release', async () => {
+ const view = mountComposer(initialConversation, { liveRun: true });
+ await act(async () => jest.advanceTimersByTime(100));
+ view.rerender();
+ await act(async () => jest.advanceTimersByTime(1000));
+ view.rerender();
+ await act(async () => jest.advanceTimersByTime(2999));
+ expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument();
+ expect(view.getLocation().search).toContain('submit=true');
+ await act(async () => jest.advanceTimersByTime(1));
+ expect(screen.getByText(/Chat settings could not be applied/)).toBeInTheDocument();
+ expect(screen.getByTestId('text-input')).toHaveValue('hi');
+ expect(view.getLocation().search).toBe('');
+ await act(async () => view.setSubmitting(false));
+ expect(ask).not.toHaveBeenCalled();
+ });
+
+ it.each([false, true])(
+ 'sends once when the settings wait elapses before the effects of the commit that applied them (busy composer: %p)',
+ async (liveRun) => {
+ const view = mountComposer(initialConversation, { liveRun });
+ await act(async () => jest.advanceTimersByTime(100));
+ await act(async () => jest.advanceTimersByTime(2900));
+ expect(ask).not.toHaveBeenCalled();
+ let elapsed = false;
+ const elapseSettingsWait = () => {
+ if (elapsed) {
+ return;
+ }
+ elapsed = true;
+ jest.advanceTimersByTime(100);
+ };
+ await act(async () => {
+ view.rerender(
+ ,
+ );
+ });
+ expect(elapsed).toBe(true);
+ expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument();
+ if (liveRun) {
+ expect(ask).not.toHaveBeenCalled();
+ expect(view.getLocation().search).toContain('submit=true');
+ await act(async () => view.setSubmitting(false));
+ }
+ expect(ask).toHaveBeenCalledTimes(1);
+ expect(ask).toHaveBeenCalledWith(expect.objectContaining({ text: 'hi' }), expect.anything());
+ expect(view.getLocation().search).toBe('');
+ await act(async () => jest.advanceTimersByTime(4000));
+ expect(ask).toHaveBeenCalledTimes(1);
+ },
+ );
+
+ it('submits to an existing conversation during its own run without waiting for the composer', async () => {
+ const view = mountComposer(
+ { ...agentConversation, conversationId: 'existing-live-chat' },
+ { liveRun: true },
+ );
+ await act(async () => jest.advanceTimersByTime(100));
+ expect(ask).toHaveBeenCalledTimes(1);
+ expect(ask).toHaveBeenCalledWith(expect.objectContaining({ text: 'hi' }), expect.anything());
+ expect(view.getLocation().search).toBe('');
+ await act(async () => view.setSubmitting(false));
+ await act(async () => jest.advanceTimersByTime(4000));
+ expect(ask).toHaveBeenCalledTimes(1);
+ });
+
+ it('cancels an applied URL submission if its route changes before composer release', async () => {
+ const view = mountComposer(initialConversation, { liveRun: true });
+ await act(async () => jest.advanceTimersByTime(100));
+ view.rerender();
+ await act(async () => jest.advanceTimersByTime(3500));
+ await act(async () => {
+ view.navigate('/c/other-chat');
+ view.rerender(
+ ,
+ );
+ });
+ await act(async () => view.setSubmitting(false));
+ await act(async () => jest.advanceTimersByTime(4000));
+ expect(ask).not.toHaveBeenCalled();
+ expect(view.getLocation().pathname).toBe('/c/other-chat');
+ expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument();
+ });
+
it('processes a new agent URL once after a previous URL request has completed', async () => {
const view = mountComposer(initialConversation, { query: 'q=first&submit=true' });
await act(async () => jest.advanceTimersByTime(100));
diff --git a/client/src/hooks/Input/useQueryParams.ts b/client/src/hooks/Input/useQueryParams.ts
index 3199891877..69fc0bc26c 100644
--- a/client/src/hooks/Input/useQueryParams.ts
+++ b/client/src/hooks/Input/useQueryParams.ts
@@ -478,12 +478,15 @@ export default function useQueryParams({
);
}, [methods, ownsRequest]);
- /** Consumes an auto-submit once, leaving a refused submission in the composer. */
+ /**
+ * Consumes an auto-submit once, leaving a refused submission in the composer. Callers reach it
+ * only with the requested settings in place, so the settings wait ends here even when the send
+ * itself still waits for the composer or the history.
+ */
const processSubmission = useCallback(() => {
if (
routePendingRef.current ||
!targetReadyRef.current ||
- awaitsComposerRelease() ||
submissionHandledRef.current ||
!pendingSubmitRef.current ||
!promptTextRef.current ||
@@ -497,6 +500,9 @@ export default function useQueryParams({
clearTimeout(settingsTimeoutRef.current);
settingsTimeoutRef.current = null;
}
+ if (awaitsComposerRelease()) {
+ return;
+ }
awaitingHistoryRef.current = isHistoryPending();
if (awaitingHistoryRef.current) {
@@ -556,6 +562,11 @@ export default function useQueryParams({
settlePrompt,
]);
+ /**
+ * Bounds the wait for the requested settings, never the wait for the composer. The timer can
+ * fire between the commit that applied them and that commit's effects, so it reads them again
+ * before reporting a failure.
+ */
const armSettingsTimeout = useCallback(() => {
if (settingsTimeoutRef.current || routePendingRef.current || !targetReadyRef.current) return;
const requestId = requestIdRef.current;
@@ -570,6 +581,10 @@ export default function useQueryParams({
return;
}
if (!submissionHandledRef.current && pendingSubmitRef.current) {
+ if (areSettingsApplied()) {
+ processSubmission();
+ return;
+ }
restoreUrlPrompt();
pendingSubmitRef.current = false;
submissionHandledRef.current = true;
@@ -579,7 +594,15 @@ export default function useQueryParams({
clearConsumedSearchParams();
}
}, MAX_SETTINGS_WAIT_MS);
- }, [ownsRequest, cancelRequest, restoreUrlPrompt, settlePrompt, clearConsumedSearchParams]);
+ }, [
+ ownsRequest,
+ cancelRequest,
+ areSettingsApplied,
+ processSubmission,
+ restoreUrlPrompt,
+ settlePrompt,
+ clearConsumedSearchParams,
+ ]);
/** Observes the history the composer sends from, so a retry runs after the
* render that derives the latest message from it, not during the cache write. */