mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-27 21:58:51 +01:00
- Updated the abort endpoint to support aborting ongoing generation streams using either streamId or conversationId. - Introduced a new mutation hook `useAbortStreamMutation` for client-side integration. - Added `useStreamStatus` query to monitor stream status and facilitate resuming conversations. - Enhanced `useChatHelpers` to incorporate abort functionality when stopping generation. - Improved `useResumableSSE` to handle stream errors and token refresh seamlessly. - Updated `useResumeOnLoad` to check for active streams and resume conversations appropriately.
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import { request } from 'librechat-data-provider';
|
|
import type { Agents } from 'librechat-data-provider';
|
|
|
|
export interface StreamStatusResponse {
|
|
active: boolean;
|
|
streamId?: string;
|
|
status?: 'running' | 'complete' | 'error' | 'aborted';
|
|
chunkCount?: number;
|
|
aggregatedContent?: Array<{ type: string; text?: string }>;
|
|
createdAt?: number;
|
|
resumeState?: Agents.ResumeState;
|
|
}
|
|
|
|
/**
|
|
* Query key for stream status
|
|
*/
|
|
export const streamStatusQueryKey = (conversationId: string) => ['streamStatus', conversationId];
|
|
|
|
/**
|
|
* Fetch stream status for a conversation
|
|
*/
|
|
export const fetchStreamStatus = async (conversationId: string): Promise<StreamStatusResponse> => {
|
|
console.log('[fetchStreamStatus] Fetching status for:', conversationId);
|
|
const result = await request.get<StreamStatusResponse>(
|
|
`/api/agents/chat/status/${conversationId}`,
|
|
);
|
|
console.log('[fetchStreamStatus] Result:', result);
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* React Query hook for checking if a conversation has an active generation stream.
|
|
* Only fetches when conversationId is provided and resumable streams are enabled.
|
|
*/
|
|
export function useStreamStatus(conversationId: string | undefined, enabled = true) {
|
|
return useQuery({
|
|
queryKey: streamStatusQueryKey(conversationId || ''),
|
|
queryFn: () => fetchStreamStatus(conversationId!),
|
|
enabled: !!conversationId && enabled,
|
|
staleTime: 1000, // Consider stale after 1 second
|
|
refetchOnMount: true,
|
|
refetchOnWindowFocus: true,
|
|
retry: false,
|
|
});
|
|
}
|