Repository navigation
🪟 fix: Wait for Side Chat Destination Readiness #18453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+3,639
−406
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
454b95e
Fix Side Chat Handoff Destination Readiness
dustinhealy e60d162
fix: Preserve Pending Side Chat Handoff Drafts
dustinhealy 30cfdc7
fix: Preserve Side Chat Handoff Ownership Through Lifecycle Changes
dustinhealy 5cf90ad
Preserve Pending Side Chat Admission and Withdrawn Drafts
dustinhealy 9fc1027
Complete Side Chat Intent Ownership and Query Readiness
dustinhealy 7285455
Preserve Driven Composer Ownership Until Actual Acceptance
dustinhealy fdb75af
Fence Deferred Cleanup and Expired Pause Availability
dustinhealy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| import { act, renderHook, waitFor } from '@testing-library/react'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { QueryKeys, EModelEndpoint, dataService } from 'librechat-data-provider'; | ||
| import type { Agent, TConversation } from 'librechat-data-provider'; | ||
| import type { ReactNode } from 'react'; | ||
| import useTargetReadiness, { getTargetReadiness } from '../ask/readiness'; | ||
| import { useGetAgentByIdQuery } from '~/data-provider/Agents/queries'; | ||
|
|
||
| jest.mock('librechat-data-provider', () => { | ||
| const actual = jest.requireActual('librechat-data-provider'); | ||
| return { ...actual, dataService: { ...actual.dataService, getAgentById: jest.fn() } }; | ||
| }); | ||
| const getAgent = jest.mocked(dataService.getAgentById); | ||
| beforeEach(() => getAgent.mockReset()); | ||
|
|
||
| const selected = (id = 'agent_1'): TConversation => | ||
| ({ | ||
| conversationId: `conversation-${id}`, | ||
| endpoint: EModelEndpoint.agents, | ||
| agent_id: id, | ||
| }) as TConversation; | ||
| const denied = Object.assign(new Error('denied'), { status: 403 }); | ||
| const detail = { id: 'agent_1' } as Agent; | ||
|
|
||
| function setupClient() { | ||
| const client = new QueryClient({ | ||
| defaultOptions: { queries: { retry: false } }, | ||
| logger: { log: jest.fn(), warn: jest.fn(), error: jest.fn() }, | ||
| }); | ||
| client.setQueryData([QueryKeys.endpoints], { | ||
| agents: {}, | ||
| openAI: {}, | ||
| assistants: { version: 'v2' }, | ||
| }); | ||
| const wrapper = ({ children }: { children: ReactNode }) => ( | ||
| <QueryClientProvider client={client}>{children}</QueryClientProvider> | ||
| ); | ||
| return { client, wrapper }; | ||
| } | ||
|
|
||
| it('shares one delayed request with the existing agent metadata loader', async () => { | ||
| let release!: (value: Agent) => void; | ||
| const request = getAgent.mockImplementation( | ||
| () => | ||
| new Promise((resolve) => { | ||
| release = resolve; | ||
| }), | ||
| ); | ||
| const { wrapper } = setupClient(); | ||
| const { result } = renderHook( | ||
| () => { | ||
| useGetAgentByIdQuery('agent_1'); | ||
| return useTargetReadiness(selected()); | ||
| }, | ||
| { wrapper }, | ||
| ); | ||
| expect(result.current).toBe('pending'); | ||
| expect(request).toHaveBeenCalledTimes(1); | ||
| await act(async () => release(detail)); | ||
| await waitFor(() => expect(result.current).toBe('ready')); | ||
| expect(request).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it.each(['success', 'denial'] as const)( | ||
| 'waits for the native mount refetch after a cached error: %s', | ||
| async (outcome) => { | ||
| const { client, wrapper } = setupClient(); | ||
| client.setQueryData([QueryKeys.agent, 'agent_1'], detail, { updatedAt: Date.now() - 10000 }); | ||
| await client | ||
| .fetchQuery([QueryKeys.agent, 'agent_1'], () => Promise.reject(denied)) | ||
| .catch(() => undefined); | ||
| let release!: (value: Agent) => void; | ||
| let reject!: (reason: unknown) => void; | ||
| const request = getAgent.mockImplementation( | ||
| () => | ||
| new Promise((resolve, fail) => { | ||
| release = resolve; | ||
| reject = fail; | ||
| }), | ||
| ); | ||
| const seen: string[] = []; | ||
| const { result } = renderHook( | ||
| () => { | ||
| useGetAgentByIdQuery('agent_1'); | ||
| const readiness = useTargetReadiness(selected()); | ||
| seen.push(readiness); | ||
| return readiness; | ||
| }, | ||
| { wrapper }, | ||
| ); | ||
| expect(seen).not.toContain('blocked'); | ||
| expect(result.current).toBe('pending'); | ||
| expect(request).toHaveBeenCalledTimes(1); | ||
| await act(async () => (outcome === 'success' ? release(detail) : reject(denied))); | ||
| await waitFor(() => expect(result.current).toBe(outcome === 'success' ? 'ready' : 'blocked')); | ||
| expect(request).toHaveBeenCalledTimes(1); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(['success', 'denial'] as const)( | ||
| 'waits for the native mount refetch after cached success: %s', | ||
| async (outcome) => { | ||
| const { client, wrapper } = setupClient(); | ||
| client.setQueryData([QueryKeys.agent, 'agent_1'], detail, { updatedAt: Date.now() - 10000 }); | ||
| let release!: (value: Agent) => void; | ||
| let reject!: (reason: unknown) => void; | ||
| const request = getAgent.mockImplementation( | ||
| () => | ||
| new Promise((resolve, fail) => { | ||
| release = resolve; | ||
| reject = fail; | ||
| }), | ||
| ); | ||
| const seen: string[] = []; | ||
| const { result } = renderHook( | ||
| () => { | ||
| useGetAgentByIdQuery('agent_1'); | ||
| const readiness = useTargetReadiness(selected()); | ||
| seen.push(readiness); | ||
| return readiness; | ||
| }, | ||
| { wrapper }, | ||
| ); | ||
| expect(seen).not.toContain('blocked'); | ||
| expect(seen).not.toContain('ready'); | ||
| expect(result.current).toBe('pending'); | ||
| expect(request).toHaveBeenCalledTimes(1); | ||
| await act(async () => (outcome === 'success' ? release(detail) : reject(denied))); | ||
| await waitFor(() => expect(result.current).toBe(outcome === 'success' ? 'ready' : 'blocked')); | ||
| expect(request).toHaveBeenCalledTimes(1); | ||
| }, | ||
| ); | ||
|
|
||
| it('uses cached endpoint configuration after refetch fails while the agent still loads', async () => { | ||
| const { client, wrapper } = setupClient(); | ||
| await client | ||
| .fetchQuery([QueryKeys.endpoints], () => Promise.reject(denied)) | ||
| .catch(() => undefined); | ||
| let release!: (value: Agent) => void; | ||
| getAgent.mockImplementation( | ||
| () => | ||
| new Promise((resolve) => { | ||
| release = resolve; | ||
| }), | ||
| ); | ||
| const { result } = renderHook(() => useTargetReadiness(selected()), { wrapper }); | ||
| expect(result.current).toBe('pending'); | ||
| await act(async () => release(detail)); | ||
| await waitFor(() => expect(result.current).toBe('ready')); | ||
| }); | ||
|
|
||
| it('adds no request for a fresh cached target or an inactive handoff', () => { | ||
| const request = getAgent; | ||
| const { client, wrapper } = setupClient(); | ||
| client.setQueryData([QueryKeys.agent, 'agent_1'], detail); | ||
| const { result, rerender } = renderHook( | ||
| ({ enabled, id }) => useTargetReadiness(selected(id), enabled), | ||
| { | ||
| wrapper, | ||
| initialProps: { enabled: true, id: 'agent_1' }, | ||
| }, | ||
| ); | ||
| expect(result.current).toBe('ready'); | ||
| rerender({ enabled: false, id: 'agent_2' }); | ||
| expect(result.current).toBe('ready'); | ||
| expect(request).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('fences a previous destination and ignores unrelated cache updates', async () => { | ||
| const { wrapper, client } = setupClient(); | ||
| const releases = new Map<string, (agent: Agent) => void>(); | ||
| getAgent.mockImplementation( | ||
| ({ agent_id }) => | ||
| new Promise((resolve) => { | ||
| releases.set(agent_id, resolve); | ||
| }), | ||
| ); | ||
| let renders = 0; | ||
| const { result, rerender } = renderHook( | ||
| ({ id }) => { | ||
| renders++; | ||
| return useTargetReadiness(selected(id)); | ||
| }, | ||
| { | ||
| wrapper, | ||
| initialProps: { id: 'agent_1' }, | ||
| }, | ||
| ); | ||
| rerender({ id: 'agent_2' }); | ||
| await act(async () => releases.get('agent_1')?.(detail)); | ||
| expect(result.current).toBe('pending'); | ||
| await act(async () => releases.get('agent_2')?.({ id: 'agent_2' } as Agent)); | ||
| await waitFor(() => expect(result.current).toBe('ready')); | ||
| const settledRenders = renders; | ||
| act(() => { | ||
| for (let i = 0; i < 20; i++) client.setQueryData(['unrelated', i], i); | ||
| }); | ||
| expect(result.current).toBe('ready'); | ||
| expect(renders).toBe(settledRenders); | ||
| }); | ||
|
|
||
| it('blocks unavailable endpoints and invalid saved targets', () => { | ||
| const { client } = setupClient(); | ||
| expect(getTargetReadiness(client, { ...selected(), endpoint: EModelEndpoint.google })).toBe( | ||
| 'blocked', | ||
| ); | ||
| expect(getTargetReadiness(client, { ...selected(), agent_id: undefined })).toBe('blocked'); | ||
| client.setQueryData([QueryKeys.agent, 'agent_1'], { id: 'different' }); | ||
| expect(getTargetReadiness(client, selected())).toBe('blocked'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { useCallback, useSyncExternalStore } from 'react'; | ||
| import { useQueryClient } from '@tanstack/react-query'; | ||
| import { | ||
| QueryKeys, | ||
| isAgentsEndpoint, | ||
| isEphemeralAgentId, | ||
| isAssistantsEndpoint, | ||
| defaultAssistantsVersion, | ||
| } from 'librechat-data-provider'; | ||
| import type { Agent, Assistant, TConversation, TEndpointsConfig } from 'librechat-data-provider'; | ||
| import type { QueryClient, QueryKey } from '@tanstack/react-query'; | ||
| import type { TurnReadiness } from '@librechat/chat/react'; | ||
| import { useGetAgentByIdQuery } from '~/data-provider/Agents/queries'; | ||
| import { assistantKey, isTargetReady } from './guards'; | ||
|
|
||
| /** Classifies the explicit target without selecting a default or changing send admission. */ | ||
| export function getTargetReadiness( | ||
| client: QueryClient, | ||
| conversation: TConversation | null, | ||
| ): TurnReadiness { | ||
| if (conversation == null) return 'pending'; | ||
| const ready = isTargetReady(client, conversation); | ||
| const endpoint = conversation.endpoint; | ||
| if (!endpoint) return 'blocked'; | ||
| const endpoints = client.getQueryState<TEndpointsConfig>([QueryKeys.endpoints]); | ||
| if (!endpoints?.data) return endpoints?.status === 'error' ? 'blocked' : 'pending'; | ||
| if (!endpoints.data[endpoint]) return 'blocked'; | ||
| if (isAgentsEndpoint(endpoint)) { | ||
| const id = conversation.agent_id; | ||
| if (!id || isEphemeralAgentId(id)) return ready ? 'ready' : 'blocked'; | ||
| const target = client.getQueryState<Agent>([QueryKeys.agent, id]); | ||
| if (target?.fetchStatus === 'fetching' || target?.fetchStatus === 'paused') return 'pending'; | ||
| if (ready) return 'ready'; | ||
| return target?.status === 'success' || target?.status === 'error' ? 'blocked' : 'pending'; | ||
| } | ||
| if (ready) return 'ready'; | ||
| if (isAssistantsEndpoint(endpoint)) { | ||
| if (!conversation.assistant_id) return 'blocked'; | ||
| const version = endpoints.data[endpoint].version ?? defaultAssistantsVersion[endpoint]; | ||
| const target = client.getQueryState<Assistant>( | ||
| assistantKey(endpoint, conversation.assistant_id, version, conversation.model), | ||
| ); | ||
| if (target?.status === 'error') return 'blocked'; | ||
| if (target?.isInvalidated) return 'pending'; | ||
| return target?.status === 'success' ? 'blocked' : 'pending'; | ||
| } | ||
| return 'blocked'; | ||
| } | ||
|
|
||
| function matches(key: QueryKey, target: TConversation | null): boolean { | ||
| return ( | ||
| target != null && | ||
| ((key[0] === QueryKeys.agent && key[1] === target.agent_id) || | ||
| (key[0] === QueryKeys.assistant && | ||
| key[1] === target.endpoint && | ||
| key[2] === target.assistant_id)) | ||
| ); | ||
| } | ||
|
|
||
| /** Only a driven handoff observes readiness. Canonical agent options give the optimistic | ||
| * mount/refetch result as well as request deduplication with the pane's tool metadata loaders. */ | ||
| export default function useTargetReadiness( | ||
| conversation: TConversation | null, | ||
| enabled = true, | ||
| ): TurnReadiness { | ||
| const client = useQueryClient(); | ||
| const agentId = | ||
| enabled && conversation?.endpoint && isAgentsEndpoint(conversation.endpoint) | ||
| ? conversation.agent_id | ||
| : undefined; | ||
| const agent = useGetAgentByIdQuery(agentId, { enabled }); | ||
| const snapshot = useCallback( | ||
| (): TurnReadiness => (enabled ? getTargetReadiness(client, conversation) : 'ready'), | ||
| [client, conversation, enabled], | ||
| ); | ||
| const subscribe = useCallback( | ||
| (notify: () => void) => { | ||
| if (!enabled) return () => undefined; | ||
| return client.getQueryCache().subscribe((event) => { | ||
| const key = event?.query.queryKey; | ||
| if (key && (key[0] === QueryKeys.endpoints || matches(key, conversation))) notify(); | ||
| }); | ||
| }, | ||
| [client, conversation, enabled], | ||
| ); | ||
| const readiness = useSyncExternalStore(subscribe, snapshot, snapshot); | ||
| /** A stale success or cached error can refetch on mount before the cache reports fetching. | ||
| * Wait for that native attempt; only its settled result may consume the handoff. */ | ||
| if (enabled && (agent.isFetching || agent.isPaused)) return 'pending'; | ||
| return readiness; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.