Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
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
6 changes: 6 additions & 0 deletions client/src/Providers/ChatHostContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '~/data-provider';
import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';
import { useChatSettings } from '~/Providers/ChatSettingsContext';
import useTurnReadiness from '~/hooks/Chat/ask/readiness';
import useTurnServices from '~/hooks/Chat/services';
import useNewConvo from '~/hooks/useNewConvo';

Expand Down Expand Up @@ -114,6 +115,11 @@ export const appChatHost: ChatHost = {
usePaneConversation: (index) => useChatSettings().usePaneConversationState(index),
useNewConversation: (index) => useNewConvo(index).newConversation,
useTurnServices,
useTurnReadiness: (conversation, enabled) => {
const { config } = useChatSettings();
const readiness = useTurnReadiness(conversation, enabled);
return config.loaded === false ? 'pending' : readiness;
},
/** `mutateAsync` is bound once per observer; the mutation result object is fresh every
* render and would defeat the contract's memo. */
useAbortStream: () => useAbortStreamMutation().mutateAsync,
Expand Down
210 changes: 210 additions & 0 deletions client/src/hooks/Chat/__tests__/readiness.spec.tsx
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');
});
91 changes: 91 additions & 0 deletions client/src/hooks/Chat/ask/readiness.ts
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';
Comment thread
dustinhealy marked this conversation as resolved.
Resolved
}
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;
}
Loading
Loading