diff --git a/client/src/hooks/Chat/ask/guards.ts b/client/src/hooks/Chat/ask/guards.ts index 136d6029f4..0919847a10 100644 --- a/client/src/hooks/Chat/ask/guards.ts +++ b/client/src/hooks/Chat/ask/guards.ts @@ -1,23 +1 @@ -import { QueryKeys } from 'librechat-data-provider'; -import type { QueryClient } from '@tanstack/react-query'; -import type { TMessage } from 'librechat-data-provider'; - -/** A revalidating cache younger than this is locally authoritative (the run - * that just streamed wrote it) and stays sendable; older ones wait for the - * refetch so a send can't fork from an outdated tail. */ -const STALE_SEND_REVALIDATION_MS = 5_000; - -/** Whether the cached history is mid-refetch and old enough that a send could fork from it. */ -export function isHistoryRevalidating( - queryClient: QueryClient, - conversationId: string | null, -): boolean { - const messagesQueryState = queryClient.getQueryState([ - QueryKeys.messages, - conversationId, - ]); - const isRevalidating = - messagesQueryState?.isInvalidated === true && messagesQueryState.fetchStatus === 'fetching'; - const cacheAgeMs = Date.now() - (messagesQueryState?.dataUpdatedAt ?? 0); - return isRevalidating && cacheAgeMs > STALE_SEND_REVALIDATION_MS; -} +export { isHistoryRevalidating } from '@librechat/chat/react'; diff --git a/client/src/hooks/Chat/services.ts b/client/src/hooks/Chat/services.ts index 6fdced9656..65c822b44c 100644 --- a/client/src/hooks/Chat/services.ts +++ b/client/src/hooks/Chat/services.ts @@ -1,4 +1,3 @@ -import { useStore } from 'jotai'; import { useNavigate } from 'react-router-dom'; import { QueryKeys } from 'librechat-data-provider'; import { useQueryClient } from '@tanstack/react-query'; @@ -15,8 +14,6 @@ import { withSubmittedCodeDecision, resolveSubmittedCodeApprovalMode, } from '~/hooks/Agents/codeDecision'; -import { activeUsageResponseIdFamily } from '~/components/Chat/Input/TokenUsage/store'; -import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse'; import useGetConversation from '~/hooks/Conversations/useGetConversation'; import useCodeApprovalMode from '~/hooks/Agents/useCodeApprovalMode'; import useSetFilesToDelete from '~/hooks/Files/useSetFilesToDelete'; @@ -25,7 +22,6 @@ import { useChatSettings } from '~/Providers/ChatSettingsContext'; import useCodeWorkspace from '~/hooks/Agents/useCodeWorkspace'; import useGetSender from '~/hooks/Conversations/useGetSender'; import { resolveSubmittedReasoning } from './ask/reasoning'; -import { isHistoryRevalidating } from './ask/guards'; import useUserKey from '~/hooks/Input/useUserKey'; /** The app's side of a pane's turns: its catalogs, router, composer files, code workspace and @@ -34,7 +30,6 @@ export default function useTurnServices({ index, conversation, }: Pick): TurnServices { - const store = useStore(); const navigate = useNavigate(); const getSender = useGetSender(); const queryClient = useQueryClient(); @@ -42,7 +37,6 @@ export default function useTurnServices({ const agentsMap = useAgentsMapContext(); const { isTemporary, config, auth, getEphemeralAgent, addedConversation } = useChatSettings(); const { getExpiry } = useUserKey(conversation?.endpoint ?? ''); - const focusResponse = useFocusRegeneratedResponse(); const getConversation = useGetConversation(index); const codeWorkspaceState = useCodeWorkspace(conversation, addedConversation); const { modes: codeApprovalModes, selected: fallbackCodeApprovalMode } = useCodeApprovalMode( @@ -61,7 +55,6 @@ export default function useTurnServices({ getSender, getUserKey: getExpiry, getRouteProjectId: getRouteChatProjectId, - isHistoryRevalidating: (conversationId) => isHistoryRevalidating(queryClient, conversationId), resolveCode: (submitted) => { const latest = getConversation(); const latestApprovalMode = latest?.codeApprovalMode; @@ -107,9 +100,6 @@ export default function useTurnServices({ }, releaseFiles: (fileIds) => markPasteSubmitted(...fileIds), clearFilesToDelete: () => setFilesToDelete({}), - bindResponse: (conversationKey, responseMessageId) => - store.set(activeUsageResponseIdFamily(conversationKey), responseMessageId), - focusResponse, bindLaunch: bindComposerLaunch, }; } diff --git a/client/test/turnServices.ts b/client/test/turnServices.ts index 93da3f75de..b06e287166 100644 --- a/client/test/turnServices.ts +++ b/client/test/turnServices.ts @@ -10,13 +10,10 @@ export const createTestTurnServices = (): TurnServices => ({ getSender: () => 'Assistant', getUserKey: () => undefined, getRouteProjectId: () => null, - isHistoryRevalidating: () => false, resolveCode: () => null, resolveReasoning: () => ({ reasoningOverride: undefined, retryable: false }), openNewChat: jest.fn(), releaseFiles: jest.fn(), clearFilesToDelete: jest.fn(), - bindResponse: jest.fn(), - focusResponse: jest.fn(), bindLaunch: jest.fn(), }); diff --git a/e2e/specs/mock/scenarios/turn-regenerate.spec.ts b/e2e/specs/mock/scenarios/turn-regenerate.spec.ts new file mode 100644 index 0000000000..4467877f8e --- /dev/null +++ b/e2e/specs/mock/scenarios/turn-regenerate.spec.ts @@ -0,0 +1,63 @@ +import { expect, test } from '@playwright/test'; +import type { Page } from '@playwright/test'; +import { + MOCK_ENDPOINTS, + NEW_CHAT_PATH, + isAgentGenerationStart, + selectMockEndpoint, + sendMessage, +} from '../helpers'; + +const uniqueLabel = (name: string) => `${name}-${Date.now()}-${Math.floor(Math.random() * 1e6)}`; + +const messagesView = (page: Page) => page.getByTestId('messages-view'); +const messageRender = (page: Page, text: string) => + page.locator('.message-render').filter({ hasText: text }).last(); + +async function regenerate(page: Page, reply: string) { + const row = messageRender(page, reply); + await row.hover(); + await Promise.all([ + page.waitForResponse(isAgentGenerationStart, { timeout: 30000 }), + row.getByRole('button', { name: 'Regenerate', exact: true }).last().click(), + ]); +} + +/** A regeneration appends the newest sibling. When the parent already has siblings and an older + * one is on screen, the turn must still bring the new response into view. */ +test.describe('turn regeneration', () => { + test.setTimeout(120000); + + test('regenerating from an older sibling shows the new response @scenario:regenerate-from-older-sibling-shows-new-response', async ({ + page, + }) => { + const label = uniqueLabel('turn-regen'); + const reply = (n: number) => `E2E counted reply ${label} #${n}`; + + await page.goto(NEW_CHAT_PATH, { timeout: 10000 }); + await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); + const response = await sendMessage(page, `E2E_COUNTED_REPLY:${label}`); + expect(response.ok()).toBeTruthy(); + await expect(messagesView(page).getByText(reply(1))).toBeVisible({ timeout: 30000 }); + + await regenerate(page, reply(1)); + await expect(messagesView(page).getByText(reply(2))).toBeVisible({ timeout: 30000 }); + + const second = messageRender(page, reply(2)); + await second.hover(); + await second + .getByRole('navigation', { name: 'Sibling message navigation' }) + .getByRole('button', { name: 'Previous sibling message' }) + .click(); + await expect(messagesView(page).getByText(reply(1))).toBeVisible(); + + await regenerate(page, reply(1)); + await expect(messagesView(page).getByText(reply(3))).toBeVisible({ timeout: 30000 }); + await expect(messagesView(page).getByText(reply(1))).toBeHidden(); + const third = messageRender(page, reply(3)); + await third.hover(); + await expect( + third.getByRole('navigation', { name: 'Sibling message navigation' }), + ).toContainText('3 / 3'); + }); +}); diff --git a/packages/chat/src/react/chat/__fixtures__/services.ts b/packages/chat/src/react/chat/__fixtures__/services.ts index bdd94cc09d..5f9de76388 100644 --- a/packages/chat/src/react/chat/__fixtures__/services.ts +++ b/packages/chat/src/react/chat/__fixtures__/services.ts @@ -9,7 +9,6 @@ export const createTurnServices = (overrides: Partial = {}): TurnS getSender: () => 'Assistant', getUserKey: () => undefined, getRouteProjectId: () => null, - isHistoryRevalidating: () => false, resolveCode: () => ({ code: { approvalMode: undefined, environmentMode: undefined, workspaces: undefined }, apply: (conversation) => conversation, @@ -18,8 +17,6 @@ export const createTurnServices = (overrides: Partial = {}): TurnS openNewChat: jest.fn(), releaseFiles: jest.fn(), clearFilesToDelete: jest.fn(), - bindResponse: jest.fn(), - focusResponse: jest.fn(), bindLaunch: jest.fn(), ...overrides, }); diff --git a/packages/chat/src/react/chat/__tests__/stop.spec.tsx b/packages/chat/src/react/chat/__tests__/stop.spec.tsx index 6e7dad77ac..87f9b2f97a 100644 --- a/packages/chat/src/react/chat/__tests__/stop.spec.tsx +++ b/packages/chat/src/react/chat/__tests__/stop.spec.tsx @@ -5,6 +5,7 @@ import React from 'react'; import { Constants } from 'librechat-data-provider'; import { useAtomValue, useSetAtom, getDefaultStore } from 'jotai'; import { act, renderHook, waitFor } from '@testing-library/react'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { TSubmission, TConversation } from 'librechat-data-provider'; import type { DrainAfterAbort, RunEnd } from '../../../core'; import type { ChatHost, ChatMessageCache } from '../host'; @@ -229,11 +230,14 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => { getDefaultStore().set(activeGenerationProtocolVersionByConvoId(conversationId), 2); } const host = createHost({ conversationId, endpoint: 'agents' } as TConversation); + const queryClient = new QueryClient(); const wrapper = ({ children }: { children: React.ReactNode }) => ( - - - {children} - + + + + {children} + + ); return { ...renderHook(() => useChatHelpers(INDEX), { wrapper }), diff --git a/packages/chat/src/react/chat/__tests__/turns.spec.tsx b/packages/chat/src/react/chat/__tests__/turns.spec.tsx index b086673c8c..e84191e2f3 100644 --- a/packages/chat/src/react/chat/__tests__/turns.spec.tsx +++ b/packages/chat/src/react/chat/__tests__/turns.spec.tsx @@ -4,7 +4,8 @@ import React from 'react'; import { createStore, Provider } from 'jotai'; import { renderHook } from '@testing-library/react'; -import { Constants, EModelEndpoint } from 'librechat-data-provider'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { QueryKeys, Constants, EModelEndpoint } from 'librechat-data-provider'; import type { TMessage, TSubmission, TConversation } from 'librechat-data-provider'; import type { ChatTurnsParams, TurnServices } from '../host'; import type { ExtendedFile } from '../../../core'; @@ -15,6 +16,8 @@ import { pendingReasoningOverrideFamily, } from '../../composer'; import { createTurnServices } from '../__fixtures__/services'; +import { siblingIdxFamily, siblingKey } from '../../thread'; +import { activeUsageResponseIdFamily } from '../../usage'; import { isSubmittingFamily } from '../../submission'; import { useTurns } from '../turns'; @@ -61,6 +64,7 @@ function setup({ files = new Map(), services = createTurnServices(), isSubmitting = false, + revalidating = false, }: { conversation?: TConversation | null; /** `null` stands for history that has not loaded. */ @@ -69,8 +73,18 @@ function setup({ files?: Map; services?: TurnServices; isSubmitting?: boolean; + /** The cached history is mid-refetch and older than a locally written tail. */ + revalidating?: boolean; } = {}) { const store = createStore(); + const queryClient = new QueryClient(); + if (revalidating) { + const key = [QueryKeys.messages, conversationId]; + queryClient.setQueryData(key, messages ?? [], { updatedAt: Date.now() - 60_000 }); + void queryClient.invalidateQueries({ queryKey: key, refetchType: 'none' }); + const query = queryClient.getQueryCache().find(key); + query?.setState({ fetchStatus: 'fetching' }); + } let submission: TSubmission | null = null; let current = conversation; const written: TMessage[][] = []; @@ -91,7 +105,11 @@ function setup({ }, }; const { result } = renderHook(() => useTurns(params, services), { - wrapper: ({ children }) => {children}, + wrapper: ({ children }) => ( + + {children} + + ), }); return { store, @@ -120,8 +138,7 @@ describe('useTurns', () => { submission?.initialResponse?.messageId, ]); expect(chat.store.get(isSubmittingFamily(0))).toBe(true); - expect(chat.services.bindResponse).toHaveBeenCalledWith( - conversationId, + expect(chat.store.get(activeUsageResponseIdFamily(conversationId))).toBe( submission?.initialResponse?.messageId, ); expect(chat.services.bindLaunch).toHaveBeenCalledWith(submission?.conversation, 0); @@ -139,10 +156,7 @@ describe('useTurns', () => { it.each<[string, Parameters[0]]>([ ['while submitting', { isSubmitting: true }], ['before the history loads', { messages: null }], - [ - 'while the history revalidates', - { services: createTurnServices({ isHistoryRevalidating: () => true }) }, - ], + ['while the history revalidates', { revalidating: true }], ['without a code workspace', { services: createTurnServices({ resolveCode: () => null }) }], [ 'onto a preliminary response', @@ -257,6 +271,8 @@ describe('useTurns', () => { it('regenerates a response with the replayed user message and focuses it', () => { const chat = setup(); chat.store.set(pendingManualSkillsByConvoId(conversationId), ['queued']); + /** An older sibling is on screen: the regeneration must bring the new one into view. */ + chat.store.set(siblingIdxFamily(siblingKey('user-1')), 1); chat.turns.regenerate(response); @@ -270,7 +286,7 @@ describe('useTurns', () => { }, }); expect(chat.written[0].map((message) => message.messageId)).toEqual(['user-1', 'response-1_']); - expect(chat.services.focusResponse).toHaveBeenCalledWith('user-1'); + expect(chat.store.get(siblingIdxFamily(siblingKey('user-1')))).toBe(0); expect(chat.store.get(pendingManualSkillsByConvoId(conversationId))).toEqual(['queued']); }); }); diff --git a/packages/chat/src/react/chat/guards.ts b/packages/chat/src/react/chat/guards.ts new file mode 100644 index 0000000000..136d6029f4 --- /dev/null +++ b/packages/chat/src/react/chat/guards.ts @@ -0,0 +1,23 @@ +import { QueryKeys } from 'librechat-data-provider'; +import type { QueryClient } from '@tanstack/react-query'; +import type { TMessage } from 'librechat-data-provider'; + +/** A revalidating cache younger than this is locally authoritative (the run + * that just streamed wrote it) and stays sendable; older ones wait for the + * refetch so a send can't fork from an outdated tail. */ +const STALE_SEND_REVALIDATION_MS = 5_000; + +/** Whether the cached history is mid-refetch and old enough that a send could fork from it. */ +export function isHistoryRevalidating( + queryClient: QueryClient, + conversationId: string | null, +): boolean { + const messagesQueryState = queryClient.getQueryState([ + QueryKeys.messages, + conversationId, + ]); + const isRevalidating = + messagesQueryState?.isInvalidated === true && messagesQueryState.fetchStatus === 'fetching'; + const cacheAgeMs = Date.now() - (messagesQueryState?.dataUpdatedAt ?? 0); + return isRevalidating && cacheAgeMs > STALE_SEND_REVALIDATION_MS; +} diff --git a/packages/chat/src/react/chat/host.tsx b/packages/chat/src/react/chat/host.tsx index 52234780ab..1e1e06e604 100644 --- a/packages/chat/src/react/chat/host.tsx +++ b/packages/chat/src/react/chat/host.tsx @@ -90,8 +90,6 @@ export type TurnServices = { getUserKey: () => string | undefined; /** The project the route scopes a new chat to. */ getRouteProjectId: () => string | null; - /** Whether cached history is mid-refetch and old enough that a send could fork from it. */ - isHistoryRevalidating: (conversationId: string | null) => boolean; /** The send's code decision; `null` refuses the send. */ resolveCode: (conversation: TConversation | null) => TurnCodeDecision | null; /** @@ -111,10 +109,6 @@ export type TurnServices = { releaseFiles: (fileIds: (string | undefined)[]) => void; /** Forgets the files queued for deletion once the composer is cleared. */ clearFilesToDelete: () => void; - /** Binds the conversation's usage display to the response it is waiting on. */ - bindResponse: (conversationKey: string, responseMessageId: string) => void; - /** Brings a regenerated response into view. */ - focusResponse: (parentMessageId?: string | null) => void; /** Ties the composer launch to the conversation the submission starts. */ bindLaunch: (conversation: TSubmission['conversation'], index: number) => void; }; diff --git a/packages/chat/src/react/chat/index.ts b/packages/chat/src/react/chat/index.ts index 1ba4d73243..07aa3a4b26 100644 --- a/packages/chat/src/react/chat/index.ts +++ b/packages/chat/src/react/chat/index.ts @@ -45,3 +45,4 @@ export type { ChatSubmissionContract, ChatConversationContract, } from './contract'; +export { isHistoryRevalidating } from './guards'; diff --git a/packages/chat/src/react/chat/turns.ts b/packages/chat/src/react/chat/turns.ts index 26688e4900..84094047f5 100644 --- a/packages/chat/src/react/chat/turns.ts +++ b/packages/chat/src/react/chat/turns.ts @@ -1,5 +1,6 @@ import { useCallback, useEffect, useRef } from 'react'; import { useStore, useSetAtom } from 'jotai'; +import { useQueryClient } from '@tanstack/react-query'; import { Constants, EModelEndpoint, isAssistantsEndpoint } from 'librechat-data-provider'; import type { TMessage } from 'librechat-data-provider'; import type { TAskFunction, TRegenerateFunction } from '../../core'; @@ -18,7 +19,10 @@ import { } from '../../core'; import { submissionStartFamily, showStopButtonByIndex } from '../generation'; import { revealedQueuedTurnFamily } from '../steer/atoms'; +import { siblingIdxFamily, siblingKey } from '../thread'; +import { activeUsageResponseIdFamily } from '../usage'; import { isSubmittingFamily } from '../submission'; +import { isHistoryRevalidating } from './guards'; /** * Submits and regenerates turns for one pane: refuses a send the chat cannot take yet, drains @@ -42,6 +46,7 @@ export function useTurns( services: TurnServices, ): { ask: TAskFunction; regenerate: TRegenerateFunction } { const store = useStore(); + const queryClient = useQueryClient(); const setIsSubmitting = useSetAtom(isSubmittingFamily(index)); const setSubmissionStart = useSetAtom(submissionStartFamily(index)); const setShowStopButton = useSetAtom(showStopButtonByIndex(index)); @@ -177,7 +182,7 @@ export function useTurns( if ( isExistingConversation && overrideMessages == null && - services.isHistoryRevalidating(conversationId) + isHistoryRevalidating(queryClient, conversationId) ) { logger.warn('[useTurns] Refusing to send while conversation history revalidates'); return false; @@ -298,11 +303,16 @@ export function useTurns( /** Bound before the optimistic tail is published, not after the first provider event: a * waiting request already owns a turn even when no usage exists. */ - services.bindResponse(conversationId ?? Constants.NEW_CONVO, initialResponse.messageId); + store.set( + activeUsageResponseIdFamily(conversationId ?? Constants.NEW_CONVO), + initialResponse.messageId, + ); if (turn.regenerateShaped) { setMessages([...submissionMessages, initialResponse]); - services.focusResponse(initialResponse.parentMessageId); + if (initialResponse.parentMessageId != null) { + store.set(siblingIdxFamily(siblingKey(initialResponse.parentMessageId)), 0); + } } else { setMessages([...submissionMessages, currentMsg, initialResponse]); }