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 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { act, renderHook, waitFor } from '@testing-library/react'; | ||
| import { QueryKeys, EModelEndpoint } from 'librechat-data-provider'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import type { Agent, TConversation } from 'librechat-data-provider'; | ||
| import type { ReactNode } from 'react'; | ||
| import useTargetReadiness, { getTargetReadiness } from '../ask/readiness'; | ||
|
|
||
| const selected = (id: string): TConversation => | ||
| ({ | ||
| conversationId: `conversation-${id}`, | ||
| endpoint: EModelEndpoint.agents, | ||
| agent_id: id, | ||
| }) as TConversation; | ||
|
|
||
| function setup(id = 'agent_1') { | ||
| 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> | ||
| ); | ||
| const hook = renderHook(({ conversation }) => useTargetReadiness(conversation, null), { | ||
| wrapper, | ||
| initialProps: { conversation: selected(id) }, | ||
| }); | ||
| return { client, ...hook }; | ||
| } | ||
|
|
||
| it('observes delayed destination details without starting another query', async () => { | ||
| const { client, result } = setup(); | ||
| let release: (agent: Agent) => void = () => undefined; | ||
| const request = jest.fn( | ||
| () => | ||
| new Promise<Agent>((resolve) => { | ||
| release = resolve; | ||
| }), | ||
| ); | ||
| let fetched: Promise<Agent>; | ||
| act(() => { | ||
| fetched = client.fetchQuery([QueryKeys.agent, 'agent_1'], request); | ||
| }); | ||
| expect(result.current).toBe('pending'); | ||
| await act(async () => { | ||
| release({ id: 'agent_1' } as Agent); | ||
| await fetched; | ||
| }); | ||
| await waitFor(() => expect(result.current).toBe('ready')); | ||
| expect(request).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('exposes rejected and missing destinations as blocked, not pending', async () => { | ||
| const { client, result } = setup(); | ||
| await act(async () => { | ||
| await client | ||
| .fetchQuery([QueryKeys.agent, 'agent_1'], () => Promise.reject(new Error('denied'))) | ||
| .catch(() => undefined); | ||
| }); | ||
| await waitFor(() => expect(result.current).toBe('blocked')); | ||
| act(() => { | ||
| client.setQueryData([QueryKeys.agent, 'agent_1'], null); | ||
| }); | ||
| expect(result.current).toBe('blocked'); | ||
| }); | ||
|
|
||
| it('ignores a late previous destination and unrelated query updates', async () => { | ||
| const { client, result, rerender } = setup(); | ||
| rerender({ conversation: selected('agent_2') }); | ||
| act(() => { | ||
| client.setQueryData([QueryKeys.agent, 'agent_1'], { id: 'agent_1' }); | ||
| }); | ||
| expect(result.current).toBe('pending'); | ||
| act(() => { | ||
| client.setQueryData([QueryKeys.agent, 'agent_2'], { id: 'agent_2' }); | ||
| }); | ||
| await waitFor(() => expect(result.current).toBe('ready')); | ||
| act(() => { | ||
| client.setQueryData(['unrelated'], { data: true }); | ||
| }); | ||
| expect(result.current).toBe('ready'); | ||
| }); | ||
|
|
||
| it('blocks unavailable endpoints and invalid saved targets', () => { | ||
| const { client } = setup(); | ||
| expect( | ||
| getTargetReadiness(client, { ...selected('agent_1'), endpoint: EModelEndpoint.google }), | ||
| ).toBe('blocked'); | ||
| expect(getTargetReadiness(client, { ...selected('agent_1'), agent_id: undefined })).toBe( | ||
| 'blocked', | ||
| ); | ||
| act(() => { | ||
| client.setQueryData([QueryKeys.agent, 'agent_1'], { id: 'different' }); | ||
| }); | ||
| expect(getTargetReadiness(client, selected('agent_1'))).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
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,80 @@ | ||
| 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 { assistantKey, isTargetReady } from './guards'; | ||
|
|
||
| /** Observes the existing target queries; it never starts a request or selects a default. */ | ||
| export function getTargetReadiness( | ||
| client: QueryClient, | ||
| conversation: TConversation | null, | ||
| ): TurnReadiness { | ||
| if (conversation == null) return 'pending'; | ||
| if (isTargetReady(client, conversation)) return 'ready'; | ||
| const endpoint = conversation.endpoint; | ||
| if (!endpoint) return 'blocked'; | ||
| const endpoints = client.getQueryState<TEndpointsConfig>([QueryKeys.endpoints]); | ||
| if (endpoints?.status === 'error') return 'blocked'; | ||
| if (endpoints?.status !== 'success') return 'pending'; | ||
| if (!endpoints.data?.[endpoint]) return 'blocked'; | ||
| if (isAgentsEndpoint(endpoint)) { | ||
| const id = conversation.agent_id; | ||
| if (!id || isEphemeralAgentId(id)) return 'blocked'; | ||
| const target = client.getQueryState<Agent>([QueryKeys.agent, id]); | ||
| return target?.status === 'success' || target?.status === 'error' ? 'blocked' : 'pending'; | ||
|
dustinhealy marked this conversation as resolved.
Resolved
|
||
| } | ||
| 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)) | ||
| ); | ||
| } | ||
|
|
||
| export default function useTargetReadiness( | ||
| conversation: TConversation | null, | ||
| added: TConversation | null, | ||
| ): TurnReadiness { | ||
| const client = useQueryClient(); | ||
| const snapshot = useCallback((): TurnReadiness => { | ||
| const primary = getTargetReadiness(client, conversation); | ||
| const secondary = added == null ? 'ready' : getTargetReadiness(client, added); | ||
| if (primary === 'blocked' || secondary === 'blocked') return 'blocked'; | ||
| return primary === 'pending' || secondary === 'pending' ? 'pending' : 'ready'; | ||
| }, [client, conversation, added]); | ||
| const subscribe = useCallback( | ||
| (notify: () => void) => | ||
| client.getQueryCache().subscribe((event) => { | ||
| const key = event?.query.queryKey; | ||
| if (!key) return; | ||
| const relevant = | ||
| key[0] === QueryKeys.endpoints || matches(key, conversation) || matches(key, added); | ||
| if (relevant) notify(); | ||
| }), | ||
| [client, conversation, added], | ||
| ); | ||
| return useSyncExternalStore(subscribe, snapshot, snapshot); | ||
| } | ||
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
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.