Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions client/src/hooks/Chat/__tests__/readiness.spec.tsx
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');
});
14 changes: 10 additions & 4 deletions client/src/hooks/Chat/__tests__/services.readCodeInputs.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from '@testing-library/react';
import { act, renderHook } from '@testing-library/react';
import { QueryKeys, EModelEndpoint } from 'librechat-data-provider';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { CodeApprovalModeInputs, CodeWorkspaceInputs } from '@librechat/chat';
Expand Down Expand Up @@ -129,9 +129,13 @@ describe('useTurnServices readCodeInputs', () => {
expect(canSubmit?.(null)).toBe(false);
expect(canSubmit?.(rendered)).toBe(true);
expect(canSubmit?.(submitted)).toBe(false);
queryClient.setQueryData([QueryKeys.agent, 'agent_primary'], { id: 'agent_primary' });
act(() => {
queryClient.setQueryData([QueryKeys.agent, 'agent_primary'], { id: 'agent_primary' });
});
expect(canSubmit?.(submitted)).toBe(true);
queryClient.removeQueries([QueryKeys.endpoints]);
act(() => {
queryClient.removeQueries([QueryKeys.endpoints]);
});
expect(canSubmit?.(submitted)).toBe(false);
});

Expand All @@ -156,7 +160,9 @@ describe('useTurnServices readCodeInputs', () => {
};
rerender();
expect(canSubmit?.(submitted)).toBe(false);
queryClient.setQueryData([QueryKeys.agent, 'agent_added'], { id: 'agent_added' });
act(() => {
queryClient.setQueryData([QueryKeys.agent, 'agent_added'], { id: 'agent_added' });
});
expect(canSubmit?.(submitted)).toBe(true);

settings = { ...settings, addedConversation: { ...added, agent_id: 'agent_other' } };
Expand Down
80 changes: 80 additions & 0 deletions client/src/hooks/Chat/ask/readiness.ts
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';
Comment thread
dustinhealy marked this conversation as resolved.
Resolved
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';
Comment thread
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);
}
3 changes: 3 additions & 0 deletions client/src/hooks/Chat/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useChatSettings } from '~/Providers/ChatSettingsContext';
import useCodeWorkspace from '~/hooks/Agents/useCodeWorkspace';
import { cancelUploads } from '~/hooks/Files/useFileHandling';
import useUserKey from '~/hooks/Input/useUserKey';
import useTargetReadiness from './ask/readiness';
import { isTargetReady } from './ask/guards';

/** The app's side of a pane's turns: its catalogs, router, composer files, code workspace and
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function useTurnServices({
approval: codeApprovalState.readInputs,
};

const targetReadiness = useTargetReadiness(conversation, addedConversation);
const readiness = useRef({ config, addedConversation });
readiness.current = { config, addedConversation };

Expand All @@ -85,6 +87,7 @@ export default function useTurnServices({
getEphemeralAgent,
getUserKey: getExpiry,
getRouteProjectId: getRouteChatProjectId,
readiness: config.loaded === false ? 'pending' : targetReadiness,
canSubmit: (submitted) => {
const { config, addedConversation } = readiness.current;
return (
Expand Down
19 changes: 19 additions & 0 deletions e2e/specs/mock/subagent-activity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ test.describe('detached subagent activity', () => {
];
const parentName = uniqueAgentName('E2E Side Chat Parent');
const createdAgentIds: string[] = [];
let releaseAgent: () => void = () => undefined;
const agentReady = new Promise<void>((resolve) => {
releaseAgent = resolve;
});
let observedAgent: () => void = () => undefined;
const agentRequested = new Promise<void>((resolve) => {
observedAgent = resolve;
});
/** Every generation the page starts, by the conversation it is posted for. */
const starts: Array<string | null> = [];
page.on('request', (request) => {
Expand Down Expand Up @@ -380,6 +388,13 @@ test.describe('detached subagent activity', () => {
});
createdAgentIds.push(parent.id);

/** The destination's history can load before its selected agent details. */
await page.route(`**/api/agents/${children[0].id}`, async (route) => {
observedAgent();
await agentReady;
await route.continue();
});

await selectAgent(page, parentName);
const response = await sendMessageAndWaitForCompletion(
page,
Expand Down Expand Up @@ -416,6 +431,9 @@ test.describe('detached subagent activity', () => {
/** The child's history came with it, the reader's words are its next turn, and the
* reply streams into the same list. */
await expect(sideChat).toContainText(`E2E detached child 1 complete ${label}`);
await agentRequested;
expect(starts.slice(startsBefore)).toEqual([]);
releaseAgent();
await expect(sideChat).toContainText('What did you find?');
await expect(sideChat).toContainText(MOCK_REPLY_TEXT, { timeout: 30_000 });
expect(starts.slice(startsBefore)).toEqual([sideConversationId]);
Expand All @@ -441,6 +459,7 @@ test.describe('detached subagent activity', () => {
await expect(main).toContainText('What did you find?');
await expect(main).toContainText('And then?');
} finally {
releaseAgent();
for (const agentId of createdAgentIds.reverse()) {
await cleanupAgent(page, agentId);
}
Expand Down
Loading
Loading