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 98977fb0b0..329c6e944f 100644 --- a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx +++ b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx @@ -53,12 +53,15 @@ function Harness({ routePending = false, targetReady = true, onAsk = ask, + refusesWhileSubmitting = false, speechSettingsInitialized = false, }: { conversation: TConversation; routePending?: boolean; targetReady?: boolean; onAsk?: typeof ask; + /** Mirrors the real `ask`, which reads `isSubmitting` from the render it was created in. */ + refusesWhileSubmitting?: boolean; speechSettingsInitialized?: boolean; }) { const [files, setFiles] = useAtom(filesFamily(0)); @@ -89,7 +92,7 @@ function Harness({ getMappingOptions: () => ({}), feedbackEnabled: false, setMessages: () => undefined, - ask: onAsk, + ask: refusesWhileSubmitting && isSubmitting ? () => false : onAsk, regenerate: () => undefined, setSiblingIdx: () => undefined, abortScroll: false, @@ -101,7 +104,7 @@ function Harness({ handleRegenerate: () => undefined, handleContinue: () => undefined, }), - [conversation, files, setFiles, isSubmitting, onAsk], + [conversation, files, setFiles, isSubmitting, onAsk, refusesWhileSubmitting], ); return ( @@ -224,7 +227,12 @@ function mountComposer( wrapper, }, ); - return { ...view, navigate: (to: string) => navigate(to), getLocation: () => location }; + return { + ...view, + navigate: (to: string) => navigate(to), + getLocation: () => location, + setSubmitting: (value: boolean) => jotaiStore.set(isSubmittingFamily(0), value), + }; } afterEach(() => { @@ -315,6 +323,32 @@ describe('ChatForm URL submission', () => { expect(ask).not.toHaveBeenCalled(); }); + it('sends a queued prompt in a new chat only once the source run has released the composer', async () => { + const source = { ...initialConversation, conversationId: 'source-chat' }; + const view = mountComposer(source, { query: '', liveRun: true }); + await act(async () => jest.advanceTimersByTime(100)); + const destinationAsk = jest.fn(); + const destination = ( + + ); + await act(async () => { + view.navigate('/c/new?prompt=queued+words&submit=true'); + view.rerender(destination); + }); + await act(async () => jest.advanceTimersByTime(100)); + expect(destinationAsk).not.toHaveBeenCalled(); + expect(view.getLocation().search).toBe('?prompt=queued+words&submit=true'); + expect(screen.getByTestId('text-input')).toHaveValue('queued words'); + + await act(async () => view.setSubmitting(false)); + expect(destinationAsk).toHaveBeenCalledTimes(1); + expect(destinationAsk).toHaveBeenCalledWith( + expect.objectContaining({ text: 'queued words' }), + expect.anything(), + ); + expect(view.getLocation().search).toBe(''); + }); + 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/Agents/__tests__/useSelectAgent.spec.ts b/client/src/hooks/Agents/__tests__/useSelectAgent.spec.ts index b1ce811340..2336336c1a 100644 --- a/client/src/hooks/Agents/__tests__/useSelectAgent.spec.ts +++ b/client/src/hooks/Agents/__tests__/useSelectAgent.spec.ts @@ -1,11 +1,14 @@ import { renderHook, act } from '@testing-library/react'; import { EModelEndpoint } from 'librechat-data-provider'; -import type { TConversation } from 'librechat-data-provider'; +import type { Agent, TAgentsMap, TConversation } from 'librechat-data-provider'; const mockNewConversation = jest.fn(); const mockFetchQuery = jest.fn(); const mockGetConversation = jest.fn(); const mockGetDefaultConversation = jest.fn(); +const listedAgent = { id: 'agent-1', name: 'Agent' } as Agent; +const loadedAgentsMap: TAgentsMap = { [listedAgent.id]: listedAgent }; +let mockAgentsMap: TAgentsMap | undefined = loadedAgentsMap; jest.mock('@tanstack/react-query', () => ({ useQueryClient: jest.fn(() => ({ fetchQuery: mockFetchQuery })), @@ -27,7 +30,7 @@ jest.mock('~/hooks/Conversations/useDefaultConvo', () => ({ })); jest.mock('~/Providers/AgentsMapContext', () => ({ - useAgentsMapContext: jest.fn(() => ({ 'agent-1': { id: 'agent-1', name: 'Agent' } })), + useAgentsMapContext: jest.fn(() => mockAgentsMap), })); jest.mock('~/utils', () => ({ @@ -40,11 +43,14 @@ jest.mock('~/utils', () => ({ }, })); +import { resetCatalogWarmup, useCatalogReady } from '~/hooks/useCatalogWarmup'; import useSelectAgent from '../useSelectAgent'; describe('useSelectAgent', () => { beforeEach(() => { jest.clearAllMocks(); + resetCatalogWarmup(); + mockAgentsMap = loadedAgentsMap; mockGetConversation.mockResolvedValue({ endpoint: EModelEndpoint.agents } as TConversation); mockGetDefaultConversation.mockImplementation( ({ @@ -168,4 +174,40 @@ describe('useSelectAgent', () => { expect(mockNewConversation.mock.calls[0][0].template.codeWorkspaces).toBeUndefined(); expect(mockFetchQuery).not.toHaveBeenCalled(); }); + + describe('agents catalog readiness', () => { + const renderSelection = () => + renderHook(() => ({ + selection: useSelectAgent(), + catalogReady: useCatalogReady('agents'), + })); + + it('selects an agent before the deferred catalog has loaded, and releases the catalog', async () => { + mockAgentsMap = undefined; + const { result } = renderSelection(); + expect(result.current.catalogReady).toBe(false); + + await act(async () => { + await result.current.selection.onSelect('agent-new'); + }); + + expect(mockNewConversation).toHaveBeenCalledTimes(1); + expect(mockNewConversation.mock.calls[0][0].template).toMatchObject({ + endpoint: EModelEndpoint.agents, + agent_id: 'agent-new', + }); + expect(result.current.catalogReady).toBe(true); + }); + + it('refuses an agent that a loaded catalog does not list', async () => { + const { result } = renderSelection(); + + await act(async () => { + await result.current.selection.onSelect('agent-missing'); + }); + + expect(mockNewConversation).not.toHaveBeenCalled(); + expect(result.current.catalogReady).toBe(false); + }); + }); }); diff --git a/client/src/hooks/Agents/useSelectAgent.ts b/client/src/hooks/Agents/useSelectAgent.ts index 9d132ba370..959485254e 100644 --- a/client/src/hooks/Agents/useSelectAgent.ts +++ b/client/src/hooks/Agents/useSelectAgent.ts @@ -4,6 +4,7 @@ import type { TConversation, TPreset, Agent } from 'librechat-data-provider'; import useGetConversation from '~/hooks/Conversations/useGetConversation'; import useDefaultConvo from '~/hooks/Conversations/useDefaultConvo'; import { useAgentsMapContext } from '~/Providers/AgentsMapContext'; +import { activateCatalog } from '~/hooks/useCatalogWarmup'; import { logger, specDisplayFieldReset } from '~/utils'; import useNewConvo from '~/hooks/useNewConvo'; @@ -48,23 +49,27 @@ export default function useSelectAgent() { [getConversation, getDefaultConversation, newConversation], ); + /** + * The agents map is a deferred catalog, so `undefined` means it has not loaded, not that the + * agent is gone. The caller already holds the agent it offers; only a loaded map may refuse it. + */ const onSelect = useCallback( async (value: string) => { - const agent = agentsMap?.[value]; - if (!agent) { + if (agentsMap != null && agentsMap[value] == null) { return; } + activateCatalog('agents'); const template: Partial = { endpoint: EModelEndpoint.agents, - agent_id: agent.id, + agent_id: value, conversationId: Constants.NEW_CONVO as string, codeEnvironmentMode: undefined, codeWorkspaces: undefined, ...specDisplayFieldReset, }; - await updateConversation({ id: agent.id }, template); + await updateConversation({ id: value }, template); }, [agentsMap, updateConversation], ); diff --git a/client/src/hooks/Input/useQueryParams.ts b/client/src/hooks/Input/useQueryParams.ts index f7bb257255..3199891877 100644 --- a/client/src/hooks/Input/useQueryParams.ts +++ b/client/src/hooks/Input/useQueryParams.ts @@ -137,7 +137,17 @@ export default function useQueryParams({ submitMessageRef.current = submitMessage; const queryClient = useQueryClient(); - const { conversation, newConversation, index = 0 } = useChatContext(); + const { conversation, newConversation, isSubmitting, index = 0 } = useChatContext(); + /** + * A new chat has no run of its own, so a busy composer there is still releasing the run of the + * conversation it just left (a queued message started in a new chat navigates mid-run). `ask` + * refuses until a commit without that run has flushed its effects, so the auto-submit waits for + * an effect of this hook to observe one instead of spending its only attempt on that refusal. + */ + const composerReleasedRef = useRef(!isSubmitting); + if (isSubmitting) { + composerReleasedRef.current = false; + } const urlAgentId = searchParams.get('agent_id') || ''; const { data: urlAgent } = useGetAgentByIdQuery(urlAgentId); @@ -166,6 +176,14 @@ export default function useQueryParams({ setSearchParamsRef.current(params, { replace: true }); }, [index, getPreservedSearchParams]); + const awaitsComposerRelease = useCallback(() => { + const conversationId = conversationRef.current?.conversationId; + return ( + !composerReleasedRef.current && + (conversationId == null || conversationId === Constants.NEW_CONVO) + ); + }, []); + const ownsComposer = useCallback(() => { const currentId = conversationRef.current?.conversationId; const current = routeRef.current; @@ -465,6 +483,7 @@ export default function useQueryParams({ if ( routePendingRef.current || !targetReadyRef.current || + awaitsComposerRelease() || submissionHandledRef.current || !pendingSubmitRef.current || !promptTextRef.current || @@ -531,6 +550,7 @@ export default function useQueryParams({ ownsRequest, isHistoryPending, areSettingsApplied, + awaitsComposerRelease, clearConsumedSearchParams, restoreUrlPrompt, settlePrompt, @@ -714,6 +734,13 @@ export default function useQueryParams({ cancelRequest, ]); + /** Declared ahead of the retry below, so that effect already sees the release it reacts to. */ + useEffect(() => { + if (!isSubmitting) { + composerReleasedRef.current = true; + } + }, [isSubmitting]); + useEffect(() => { if (routePending || !targetReady) { if (settingsTimeoutRef.current) clearTimeout(settingsTimeoutRef.current); @@ -742,6 +769,7 @@ export default function useQueryParams({ conversation, routePending, targetReady, + isSubmitting, processSubmission, armSettingsTimeout, areSettingsApplied, diff --git a/e2e/specs/mock/scenarios/builder-select-cold-catalog.spec.ts b/e2e/specs/mock/scenarios/builder-select-cold-catalog.spec.ts new file mode 100644 index 0000000000..105ab1d99c --- /dev/null +++ b/e2e/specs/mock/scenarios/builder-select-cold-catalog.spec.ts @@ -0,0 +1,75 @@ +import { expect, test } from '@playwright/test'; +import type { Request } from '@playwright/test'; +import type { AgentDetail } from '../agents.helpers'; +import { cleanupAgent, openAgentBuilder, uniqueAgentName } from '../agents.helpers'; +import { + MOCK_ENDPOINTS, + NEW_CHAT_PATH, + getAccessToken, + replyPrompt, + requestJson, +} from '../helpers'; + +type ChatStart = { endpoint?: string; agent_id?: string }; + +const isChatStart = (request: Request) => + request.method() === 'POST' && + /^\/api\/agents\/chat\/(?!abort$|resume$)[^/]+$/.test(new URL(request.url()).pathname); + +const isAgentsMapRequest = (request: Request) => { + const url = new URL(request.url()); + return url.pathname === '/api/agents' && url.searchParams.get('requiredPermission') === '1'; +}; + +test.describe('builder select on a cold agents catalog', () => { + test('Select starts a chat with the agent before the agents catalog has warmed @scenario:builder-select-applies-before-agents-catalog-warms', async ({ + page, + }) => { + test.setTimeout(60000); + let agentId: string | undefined; + + try { + await page.goto(NEW_CHAT_PATH, { timeout: 10000 }); + const token = await getAccessToken(page); + const agent = await requestJson(page, { + path: '/api/agents', + token, + method: 'POST', + body: { + name: uniqueAgentName('E2E Cold Catalog Agent'), + instructions: 'Reply to the deterministic end-to-end request.', + provider: MOCK_ENDPOINTS[0].label, + model: MOCK_ENDPOINTS[0].model, + }, + }); + agentId = agent.id; + + /** Catalog warmup waits for an idle main thread. Never granting one holds the agents map + * back for the whole test, the state a busy first load leaves a quick user in. */ + await page.addInitScript(() => { + window.requestIdleCallback = () => 0; + }); + let agentsMapRequested = false; + page.on('request', (request) => { + agentsMapRequested ||= isAgentsMapRequest(request); + }); + + const form = await openAgentBuilder(page); + await form.getByRole('combobox', { name: 'Agent', exact: true }).click(); + await page.getByRole('option', { name: agent.name! }).click(); + await expect(form.getByLabel('Agent name')).toHaveValue(agent.name!); + expect(agentsMapRequested, 'the agents catalog must still be cold').toBe(false); + await form.getByRole('button', { name: 'Select Agent' }).click(); + + const input = page.getByRole('textbox', { name: 'Message input' }); + await expect(input).toHaveAttribute('placeholder', `Message ${agent.name}`); + await input.fill(replyPrompt(`cold-catalog-${Date.now()}`)); + const [start] = await Promise.all([page.waitForRequest(isChatStart), input.press('Enter')]); + + expect(new URL(start.url()).pathname).toBe('/api/agents/chat/agents'); + expect((start.postDataJSON() as ChatStart).agent_id).toBe(agent.id); + } finally { + await cleanupAgent(page, agentId); + } + }); +}); diff --git a/e2e/specs/mock/subagent-activity.spec.ts b/e2e/specs/mock/subagent-activity.spec.ts index ab062fd7c7..16c180f5f2 100644 --- a/e2e/specs/mock/subagent-activity.spec.ts +++ b/e2e/specs/mock/subagent-activity.spec.ts @@ -1,7 +1,7 @@ import { rm } from 'node:fs/promises'; import { expect, test } from '@playwright/test'; import type { ParentSubagentIndex, SubagentThreadView, TMessage } from 'librechat-data-provider'; -import type { BrowserContext, Locator, Page } from '@playwright/test'; +import type { BrowserContext, Locator, Page, Request } from '@playwright/test'; import type { AgentDetail } from './agents.helpers'; import { MOCK_ENDPOINTS, @@ -56,6 +56,40 @@ const childActivityPanel = (page: Page) => .getByRole('region', { name: 'Child agent activity' }) .or(page.getByRole('dialog', { name: 'Child agent activity' })); +/** + * Records what the panel's activity stream delivers, read from the page's own request. + * + * Playwright exposes a response body only once the request finishes, and the panel may abort this + * stream first: it stops listening as soon as the durable thread reports the task terminal, which + * the server persists before it writes the stream's final frame. The longest text wins because the + * abort empties `responseText` before this listener, registered after the transport's, runs. + */ +async function recordActivityStreams(page: Page): Promise<() => Promise> { + await page.addInitScript((activityPath) => { + const streams: string[] = []; + Object.assign(window, { e2eActivityStreams: streams }); + const open = XMLHttpRequest.prototype.open; + XMLHttpRequest.prototype.open = function ( + this: XMLHttpRequest, + ...args: Parameters + ) { + if (new RegExp(activityPath).test(new URL(String(args[1]), window.location.href).pathname)) { + const index = streams.push('') - 1; + this.addEventListener('progress', () => { + if (this.responseText.length > streams[index].length) { + streams[index] = this.responseText; + } + }); + } + return open.apply(this, args); + } as XMLHttpRequest['open']; + }, ACTIVITY_PATH.source); + return () => + page.evaluate( + () => (window as Window & { e2eActivityStreams?: string[] }).e2eActivityStreams ?? [], + ); +} + async function selectAgent(page: Page, name: string): Promise { const form = await openAgentBuilder(page); await form.getByRole('combobox', { name: 'Agent', exact: true }).click(); @@ -78,19 +112,24 @@ test.describe('detached subagent activity', () => { const parentName = uniqueAgentName('E2E Activity Parent'); const createdAgentIds: string[] = []; const activityRequests: string[] = []; - const finishedActivityRequests: string[] = []; + const endedActivityRequests: string[] = []; page.on('request', (request) => { const url = new URL(request.url()); if (ACTIVITY_PATH.test(url.pathname)) { activityRequests.push(url.pathname); } }); - page.on('requestfinished', (request) => { + /** The stream ends either way once the child is terminal: the server closes it after the final + * frame, or the panel aborts it on the durable terminal status, whichever arrives first. */ + const recordEndedActivity = (request: Request) => { const url = new URL(request.url()); if (ACTIVITY_PATH.test(url.pathname)) { - finishedActivityRequests.push(url.pathname); + endedActivityRequests.push(url.pathname); } - }); + }; + page.on('requestfinished', recordEndedActivity); + page.on('requestfailed', recordEndedActivity); + const activityStreams = await recordActivityStreams(page); try { await page.goto('/c/new'); @@ -126,7 +165,7 @@ test.describe('detached subagent activity', () => { await cards.first().click(); const panel = childActivityPanel(page); - const activityResponse = await activityResponsePromise; + await activityResponsePromise; await expect(panel).toBeVisible(); await expect(panel).toContainText('child-1-phase-10'); await expect(panel.getByText('Running', { exact: true })).toHaveCount(0); @@ -136,8 +175,9 @@ test.describe('detached subagent activity', () => { timeout: 30_000, }); await expect(panel.getByText('Completed', { exact: true })).toHaveCount(0); - await expect.poll(() => finishedActivityRequests.length).toBe(1); - const activityStreamBody = await activityResponse.text(); + await expect.poll(() => endedActivityRequests.length).toBe(1); + const [activityStreamBody, ...otherStreams] = await activityStreams(); + expect(otherStreams).toHaveLength(0); expect(activityStreamBody).toContain('"event":"on_subagent_update"'); expect(activityStreamBody).toContain('"phase":"message_delta"'); await page.waitForTimeout(ACTIVITY_RECONNECT_GUARD_MS);