mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 21:08:17 +00:00
🎯 refactor: Let useTurns Own Response Binding, Regenerated Focus and the History Guard (#17750)
* refactor: Move the History Revalidation Guard into @librechat/chat * refactor: Let useTurns Own Response Binding, Regenerated Focus and the History Guard
This commit is contained in:
parent
6c53f258e4
commit
e64ef7ec25
11 changed files with 134 additions and 61 deletions
|
|
@ -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<TMessage[]>([
|
||||
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';
|
||||
|
|
|
|||
|
|
@ -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<ChatTurnsParams, 'index' | 'conversation'>): 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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
});
|
||||
|
|
|
|||
63
e2e/specs/mock/scenarios/turn-regenerate.spec.ts
Normal file
63
e2e/specs/mock/scenarios/turn-regenerate.spec.ts
Normal file
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
@ -9,7 +9,6 @@ export const createTurnServices = (overrides: Partial<TurnServices> = {}): 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<TurnServices> = {}): TurnS
|
|||
openNewChat: jest.fn(),
|
||||
releaseFiles: jest.fn(),
|
||||
clearFilesToDelete: jest.fn(),
|
||||
bindResponse: jest.fn(),
|
||||
focusResponse: jest.fn(),
|
||||
bindLaunch: jest.fn(),
|
||||
...overrides,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 }) => (
|
||||
<ChatHostProvider host={host}>
|
||||
<RunEndProbe />
|
||||
{children}
|
||||
</ChatHostProvider>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatHostProvider host={host}>
|
||||
<RunEndProbe />
|
||||
{children}
|
||||
</ChatHostProvider>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
return {
|
||||
...renderHook(() => useChatHelpers(INDEX), { wrapper }),
|
||||
|
|
|
|||
|
|
@ -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<string, ExtendedFile>(),
|
||||
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<string, ExtendedFile>;
|
||||
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 }) => <Provider store={store}>{children}</Provider>,
|
||||
wrapper: ({ children }) => (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<Provider store={store}>{children}</Provider>
|
||||
</QueryClientProvider>
|
||||
),
|
||||
});
|
||||
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<typeof setup>[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']);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
23
packages/chat/src/react/chat/guards.ts
Normal file
23
packages/chat/src/react/chat/guards.ts
Normal file
|
|
@ -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<TMessage[]>([
|
||||
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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -45,3 +45,4 @@ export type {
|
|||
ChatSubmissionContract,
|
||||
ChatConversationContract,
|
||||
} from './contract';
|
||||
export { isHistoryRevalidating } from './guards';
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue