diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.ts index aa8afd77c0..ef62bac868 100644 --- a/client/src/hooks/SSE/useResumableSSE.ts +++ b/client/src/hooks/SSE/useResumableSSE.ts @@ -27,36 +27,27 @@ import { recoveryDispositionsFamily, } from '@librechat/chat/react'; import { - toStartGenerationError, getOptimisticMessages, isInitialNewConversation, getStreamStartFailureText, - keepLocalCodeApprovalMode, hydrateSubmissionMessages, buildOptimisticConversation, - GENERATION_PROTOCOL_VERSION, - supportsGenerationProtocolV2, ResumableRun, documentVisibility, attachResumableStream, - startGeneration, -} from '@librechat/chat'; -import type { - ResumableHost, - ResumableCache, - SubscriptionTarget, - GenerationProtocolVersion, + mountSubmission, + releaseSubmission, } from '@librechat/chat'; import type { TMessage, TSubmission, TConversation, - TPendingSteer, EventSubmission, } from 'librechat-data-provider'; import type { QueuedMessageOrigin, DrainAfterAbort, RunEnd } from '@librechat/chat'; -import type { ActiveJobsResponse, StreamStatusResponse } from '~/data-provider'; +import type { ResumableHost, ResumableCache } from '@librechat/chat'; import type { EventHandlerParams } from './useEventHandlers'; +import type { ActiveJobsResponse } from '~/data-provider'; import type { TResData, TFinalResData } from '~/common'; import { logger, @@ -196,6 +187,38 @@ export default function useResumableSSE( [queryClient], ); + const addOptimisticConversation = useCallback( + (conversationId: string, currentSubmission: TSubmission): TSubmission => { + if (!isInitialNewConversation(currentSubmission)) { + return currentSubmission; + } + + const optimisticConversation = buildOptimisticConversation(currentSubmission, conversationId); + const optimisticMessages = getOptimisticMessages( + currentSubmission, + conversationId, + getMessages(), + ); + + queryClient.setQueryData( + [QueryKeys.conversation, conversationId], + (current) => current ?? optimisticConversation, + ); + queryClient.setQueryData( + [QueryKeys.messages, conversationId], + optimisticMessages, + ); + queryClient.setQueryData( + [QueryKeys.messages, Constants.NEW_CONVO], + optimisticMessages, + ); + upsertConvoInAllQueries(queryClient, optimisticConversation); + + return hydrateSubmissionMessages(currentSubmission, conversationId); + }, + [getMessages, queryClient], + ); + const resumableCache = useMemo( () => ({ invalidateMessages: (conversationId, refetch) => @@ -237,40 +260,16 @@ export default function useResumableSSE( upsertConvoInAllQueries(queryClient, conversation, true, 'snapshot'), removeConversation: (conversationId) => removeConvoFromAllQueries(queryClient, conversationId), + getConversation: (conversationId) => + queryClient.getQueryData([QueryKeys.conversation, conversationId]), + removeConversationQueries: (conversationId) => { + queryClient.removeQueries({ queryKey: [QueryKeys.conversation, conversationId] }); + queryClient.removeQueries({ queryKey: [QueryKeys.messages, conversationId] }); + }, + queueTitleGeneration, + addOptimisticConversation, }), - [addActiveJob, queryClient, removeActiveJob], - ); - - const addOptimisticConversation = useCallback( - (conversationId: string, currentSubmission: TSubmission): TSubmission => { - if (!isInitialNewConversation(currentSubmission)) { - return currentSubmission; - } - - const optimisticConversation = buildOptimisticConversation(currentSubmission, conversationId); - const optimisticMessages = getOptimisticMessages( - currentSubmission, - conversationId, - getMessages(), - ); - - queryClient.setQueryData( - [QueryKeys.conversation, conversationId], - (current) => current ?? optimisticConversation, - ); - queryClient.setQueryData( - [QueryKeys.messages, conversationId], - optimisticMessages, - ); - queryClient.setQueryData( - [QueryKeys.messages, Constants.NEW_CONVO], - optimisticMessages, - ); - upsertConvoInAllQueries(queryClient, optimisticConversation); - - return hydrateSubmissionMessages(currentSubmission, conversationId); - }, - [getMessages, queryClient], + [addActiveJob, addOptimisticConversation, queryClient, removeActiveJob], ); const [_completed, setCompleted] = useState(new Set()); const [streamId, setStreamId] = useState(null); @@ -419,6 +418,24 @@ export default function useResumableSSE( fetchStatus: fetchStreamStatus, subscribe: (next) => attachResumableStream(run, buildHost(), next), cache: resumableCache, + queue: { + clearSupersededMarkers: (conversationId, isSameGeneration) => { + const stopFamily = stopRequestedByConvoId(conversationId); + const stopped = jotaiStore.get(stopFamily); + if (stopped != null && !isSameGeneration(stopped.generationCreatedAt)) { + jotaiStore.set(stopFamily, null); + } + const detachedFamily = detachedRunByConvoId(conversationId); + const detached = jotaiStore.get(detachedFamily); + if (detached != null && !isSameGeneration(detached.generationCreatedAt)) { + jotaiStore.set(detachedFamily, null); + } + }, + isStopped: (conversationId, generationCreatedAt) => + isStoppedRun(jotaiStore.get(stopRequestedByConvoId(conversationId)), generationCreatedAt), + rememberDetachedRun: (conversationId, detachedRun) => + jotaiStore.set(detachedRunByConvoId(conversationId), detachedRun), + }, pane: { setIsSubmitting, setShowStopButton, @@ -431,6 +448,11 @@ export default function useResumableSSE( clearComposerDrafts(runIndex, conversationId, options), publishRunEnd: setRunEnd, setAbortScroll, + setConversation, + startNewConversation: newConversation + ? () => newConversation({ template: { conversationId: String(Constants.NEW_CONVO) } }) + : undefined, + replaceNewConversationUrl, renewActiveRun: () => setActiveRunId(v4()), runEndTarget: resolveRunEndTarget, clearDrainAfterAbort, @@ -540,6 +562,7 @@ export default function useResumableSSE( }), [ run, + newConversation, localize, queryClient, restoreQueuedSubmission, @@ -594,617 +617,13 @@ export default function useResumableSSE( ], ); - const subscribeToStream = useCallback( - (target: SubscriptionTarget) => attachResumableStream(run, buildHost(), target), - [buildHost, run], - ); - useEffect(() => { + const host = buildHost(); if (!submission || Object.keys(submission).length === 0) { - logger.log('ResumableSSE', 'No submission, cleaning up'); - // Clear reconnect timeout if submission is cleared - run.clearReconnectTimeout(); - run.stopForegroundReattach?.(); - run.stopForegroundReattach = null; - // Close SSE but do NOT dispatch cancel - navigation should not abort - run.stream?.abort(); - run.stream = null; - setStreamId(null); - run.reconnectAttempt = 0; - run.submission = null; + releaseSubmission(run, host); return; } - - const resumableSubmission = submission as TSubmission & { - resumeStreamId?: string; - resumeGenerationCreatedAt?: number; - resumeGenerationProtocolVersion?: GenerationProtocolVersion; - }; - const resumeStreamId = resumableSubmission.resumeStreamId; - const resumeGenerationCreatedAt = resumableSubmission.resumeGenerationCreatedAt; - const resumeGenerationProtocolVersion = - resumableSubmission.resumeGenerationProtocolVersion ?? 1; - logger.log('ResumableSSE', 'Effect triggered', { - conversationId: submission.conversation?.conversationId, - hasResumeStreamId: !!resumeStreamId, - resumeStreamId, - userMessageId: submission.userMessage?.messageId, - }); - - run.submission = submission; - run.runEndPublished = false; - run.identityConfirmed = false; - run.subscribedEpoch = null; - if (submission.conversation?.conversationId != null) { - /** A different run starting here owns the conversation's end from now on: an earlier run - * this pane left is superseded, and a Stop belonged to that earlier run. Reattaching to - * the same generation keeps both, since that run's end is still owed and it was stopped. */ - const startingConvoId = submission.conversation.conversationId; - const sameGeneration = (generationCreatedAt?: number) => - resumeStreamId != null && - resumeGenerationCreatedAt != null && - generationCreatedAt === resumeGenerationCreatedAt; - const stopFamily = stopRequestedByConvoId(startingConvoId); - const stopped = jotaiStore.get(stopFamily); - if (stopped != null && !sameGeneration(stopped.generationCreatedAt)) { - jotaiStore.set(stopFamily, null); - } - const detachedFamily = detachedRunByConvoId(startingConvoId); - const detached = jotaiStore.get(detachedFamily); - if (detached != null && !sameGeneration(detached.generationCreatedAt)) { - jotaiStore.set(detachedFamily, null); - } - } - const startController = new AbortController(); - const { signal } = startController; - const isCurrentEffect = () => !signal.aborted && run.submission === submission; - - const initStream = async () => { - if (!isCurrentEffect()) { - return; - } - - bindResponse(submission); - setIsSubmitting(true); - /** Starting a new generation is the one interval where `isSubmitting` - * is true but no generation epoch exists yet. Clear any prior epoch and - * withhold live Stop/steer controls; the composer can still queue input. - * subscribeToStream exposes controls once the POST/resume installs the - * server-provided epoch. */ - const submissionConversationId = - submission.conversation?.conversationId ?? Constants.NEW_CONVO; - if (!resumeStreamId || resumeGenerationCreatedAt == null) { - updateActiveGenerationCreatedAt(resumeStreamId ?? submissionConversationId, null); - } - setShowStopButton(false); - - if (resumeStreamId) { - if (!isCurrentEffect()) { - return; - } - // Resume: just subscribe to existing stream, don't start new generation - logger.log('ResumableSSE', 'Resuming existing stream:', resumeStreamId); - setStreamId(resumeStreamId); - // Optimistically add to active jobs (in case it's not already there) - addActiveJob(resumeStreamId); - subscribeToStream({ - streamId: resumeStreamId, - submission, - isResume: true, - generationCreatedAt: resumeGenerationCreatedAt, - generationProtocolVersion: resumeGenerationProtocolVersion, - signal, - }); - } else { - // New generation: start and then subscribe - logger.log('ResumableSSE', 'Starting NEW generation'); - const startResult = await startGeneration(run, buildHost(), submission, signal); - if (!isCurrentEffect()) { - return; - } - if (startResult) { - let replacementStart = startResult.status === 'replaced' ? startResult : null; - let knownReplacementStatus: StreamStatusResponse | null = null; - - if (startResult.status === 'predecessor_mismatch') { - const expectedPredecessorCreatedAt = submission.expectedPredecessorCreatedAt; - let refreshedStatus: StreamStatusResponse | null = null; - try { - refreshedStatus = await fetchStreamStatus(startResult.conversationId); - if (!isCurrentEffect()) { - return; - } - } catch (error) { - if (!isCurrentEffect()) { - return; - } - logger.warn('ResumableSSE', 'Could not refresh predecessor mismatch status', { - conversationId: startResult.conversationId, - error, - }); - } - if (!isCurrentEffect()) { - return; - } - - const reportedReplacement = - startResult.active === true && - startResult.generationCreatedAt != null && - startResult.generationCreatedAt !== expectedPredecessorCreatedAt; - - if ( - refreshedStatus?.active === true && - supportsGenerationProtocolV2(refreshedStatus) && - typeof refreshedStatus.streamId === 'string' && - refreshedStatus.streamId.length > 0 && - refreshedStatus.createdAt != null && - refreshedStatus.createdAt !== expectedPredecessorCreatedAt - ) { - replacementStart = { - status: 'replaced', - streamId: refreshedStatus.streamId, - conversationId: startResult.conversationId, - generationCreatedAt: refreshedStatus.createdAt, - generationProtocolVersion: GENERATION_PROTOCOL_VERSION, - }; - knownReplacementStatus = refreshedStatus; - restoreQueuedSubmission(submission, refreshedStatus.createdAt); - } else if (reportedReplacement) { - /** The atomic 409 is itself authoritative at admission time. - * Status publication can lag that response, so retain its exact - * epoch as a minimal handoff snapshot; the fenced SSE attach can - * reconcile if the replacement finishes in the meantime. */ - replacementStart = { - status: 'replaced', - streamId: startResult.streamId, - conversationId: startResult.conversationId, - generationCreatedAt: startResult.generationCreatedAt!, - generationProtocolVersion: GENERATION_PROTOCOL_VERSION, - }; - knownReplacementStatus = { - active: true, - status: 'running', - streamId: startResult.streamId, - createdAt: startResult.generationCreatedAt, - generationProtocolVersion: GENERATION_PROTOCOL_VERSION, - }; - restoreQueuedSubmission(submission, startResult.generationCreatedAt!); - } else { - /** No different live epoch is proven. Reconcile away the - * optimistic C rows, but publish no run-end: doing so would - * immediately drain and re-send the just-restored queue item. */ - const terminalWinnerCreatedAt = - refreshedStatus?.createdAt ?? startResult.generationCreatedAt; - if (terminalWinnerCreatedAt != null) { - restoreQueuedSubmission(submission, terminalWinnerCreatedAt); - } - if (refreshedStatus != null) { - queryClient.setQueryData( - streamStatusQueryKey(startResult.conversationId), - refreshedStatus, - ); - } else { - queryClient.invalidateQueries({ - queryKey: streamStatusQueryKey(startResult.conversationId), - }); - } - try { - await queryClient.invalidateQueries({ - queryKey: [QueryKeys.messages, startResult.conversationId], - refetchType: 'all', - }); - if (!isCurrentEffect()) { - return; - } - await queryClient.invalidateQueries({ - queryKey: [QueryKeys.conversation, startResult.conversationId], - refetchType: 'all', - }); - if (!isCurrentEffect()) { - return; - } - await invalidateConversationLists(queryClient); - if (!isCurrentEffect()) { - return; - } - await queryClient.invalidateQueries({ queryKey: [QueryKeys.pinnedConversations] }); - if (!isCurrentEffect()) { - return; - } - } catch (error) { - if (!isCurrentEffect()) { - return; - } - logger.warn('ResumableSSE', 'Could not reconcile predecessor mismatch history', { - conversationId: startResult.conversationId, - error, - }); - } - clearDrainAfterAbort(startResult.conversationId, expectedPredecessorCreatedAt); - run.submission = null; - setIsSubmitting(false); - setShowStopButton(false); - setSubmission(null); - return; - } - } - - if (replacementStart != null) { - /** This POST belongs to an older optimistic submission, while the - * conversation-scoped stream id now belongs to a newer generation. - * Never attach A's submission directly to B. Discover B's - * authoritative resume snapshot, refresh durable history, then let - * useResumeOnLoad construct a new epoch-fenced submission. */ - const replacementStatus = - knownReplacementStatus ?? - (await fetchStreamStatus(replacementStart.conversationId).catch((error) => { - if (!isCurrentEffect()) { - return null; - } - logger.warn('ResumableSSE', 'Could not discover replacement start generation', { - conversationId: replacementStart.conversationId, - generationCreatedAt: replacementStart.generationCreatedAt, - error, - }); - return null; - })); - if (!isCurrentEffect()) { - return; - } - if ( - replacementStatus?.active === true && - supportsGenerationProtocolV2(replacementStatus) && - replacementStatus.streamId === replacementStart.streamId && - replacementStatus.createdAt === replacementStart.generationCreatedAt - ) { - const startedAsNewConvo = isInitialNewConversation(submission); - try { - await queryClient.invalidateQueries({ - queryKey: [QueryKeys.messages, replacementStart.conversationId], - refetchType: 'all', - }); - if (!isCurrentEffect()) { - return; - } - await queryClient.invalidateQueries({ - queryKey: [QueryKeys.conversation, replacementStart.conversationId], - refetchType: 'all', - }); - if (!isCurrentEffect()) { - return; - } - await invalidateConversationLists(queryClient); - if (!isCurrentEffect()) { - return; - } - await queryClient.invalidateQueries({ queryKey: [QueryKeys.pinnedConversations] }); - if (!isCurrentEffect()) { - return; - } - } catch (error) { - if (!isCurrentEffect()) { - return; - } - logger.warn('ResumableSSE', 'Could not refresh replacement generation history', { - conversationId: replacementStart.conversationId, - error, - }); - } - const authoritativeConversation = queryClient.getQueryData([ - QueryKeys.conversation, - replacementStart.conversationId, - ]); - if (authoritativeConversation?.conversationId === replacementStart.conversationId) { - const localConversationId = isInitialNewConversation(submission) - ? Constants.NEW_CONVO - : replacementStart.conversationId; - setConversation?.((current) => - keepLocalCodeApprovalMode( - authoritativeConversation, - current, - localConversationId, - ), - ); - } - queryClient.setQueryData(streamStatusQueryKey(replacementStart.conversationId), { - ...replacementStatus, - generationHandoff: true, - }); - updateActiveGenerationCreatedAt( - replacementStart.conversationId, - replacementStart.generationCreatedAt, - undefined, - replacementStart.generationProtocolVersion, - ); - addActiveJob(replacementStart.streamId); - if (startedAsNewConvo) { - run.optimisticStreamIds.add(replacementStart.streamId); - updateActiveGenerationCreatedAt( - Constants.NEW_CONVO, - replacementStart.generationCreatedAt, - undefined, - replacementStart.generationProtocolVersion, - ); - replaceNewConversationUrl(replacementStart.conversationId); - } - seedSteerChips( - replacementStart.conversationId, - (replacementStatus.resumeState?.pendingSteers ?? []) as TPendingSteer[], - replacementStart.generationCreatedAt, - replacementStart.generationProtocolVersion, - ); - settleAppliedSteerParts( - replacementStart.conversationId, - replacementStatus.resumeState?.aggregatedContent ?? - replacementStatus.aggregatedContent, - ); - run.replacementHandoff = true; - run.submission = null; - setIsSubmitting(true); - setShowStopButton(true); - setSubmission(null); - return; - } - - /** The server reported a replacement but its authoritative status - * could not be confirmed. Drop A without ever attaching it to B; - * leave history/status invalidated so navigation/reload can recover, - * and keep any queued text parked (non-completed outcome). */ - queryClient.invalidateQueries({ - queryKey: streamStatusQueryKey(replacementStart.conversationId), - }); - queryClient.invalidateQueries({ - queryKey: [QueryKeys.messages, replacementStart.conversationId], - }); - clearDrainAfterAbort( - replacementStart.conversationId, - replacementStart.generationCreatedAt, - ); - setIsSubmitting(false); - setShowStopButton(false); - setRunEnd({ - conversationId: replacementStart.conversationId, - outcome: 'error', - endedAt: run.scheduler.now(), - generationCreatedAt: replacementStart.generationCreatedAt, - }); - setSubmission(null); - return; - } - if (startResult.status === 'predecessor_mismatch') { - // Every mismatch path above either hands off or reconciles. - return; - } - if (startResult.status === 'settled') { - const settledConversationId = startResult.conversationId; - const startedAsNewConvo = isInitialNewConversation(submission); - let persistedConversation: TConversation | undefined; - let conversationLookup: 'found' | 'not_found' | 'inconclusive' = 'inconclusive'; - try { - persistedConversation = await fetchConvoSnapshot( - queryClient, - settledConversationId, - () => dataService.getConversationById(settledConversationId), - ); - if (!isCurrentEffect()) { - return; - } - conversationLookup = 'found'; - } catch (error) { - if (!isCurrentEffect()) { - return; - } - conversationLookup = - toStartGenerationError(error)?.response?.status === 404 - ? 'not_found' - : 'inconclusive'; - logger.warn('ResumableSSE', 'Could not load settled generation conversation', { - conversationId: settledConversationId, - conversationLookup, - error, - }); - } - - if (persistedConversation != null) { - /** The persisted copy carries the mode the settled turn started - * with; a pick made while the start was pending is newer. The - * live conversation still holds the new-chat id here because no - * stream event ran for this turn. */ - const settledCopy = persistedConversation; - const localConversationId = startedAsNewConvo - ? Constants.NEW_CONVO - : settledConversationId; - const conversationRecord = keepLocalCodeApprovalMode( - settledCopy, - queryClient.getQueryData([ - QueryKeys.conversation, - settledConversationId, - ]), - settledConversationId, - ); - queryClient.setQueryData( - [QueryKeys.conversation, settledConversationId], - conversationRecord, - ); - upsertConvoInAllQueries(queryClient, conversationRecord, true, 'snapshot'); - setConversation?.((current) => - keepLocalCodeApprovalMode(settledCopy, current, localConversationId), - ); - if (startedAsNewConvo) { - replaceNewConversationUrl(settledConversationId); - } - } - - try { - await queryClient.invalidateQueries({ - queryKey: [QueryKeys.messages, settledConversationId], - refetchType: 'all', - }); - if (!isCurrentEffect()) { - return; - } - await queryClient.invalidateQueries({ - queryKey: [QueryKeys.conversation, settledConversationId], - refetchType: 'all', - }); - if (!isCurrentEffect()) { - return; - } - await invalidateConversationLists(queryClient); - if (!isCurrentEffect()) { - return; - } - await queryClient.invalidateQueries({ queryKey: [QueryKeys.pinnedConversations] }); - if (!isCurrentEffect()) { - return; - } - } catch (error) { - if (!isCurrentEffect()) { - return; - } - logger.warn('ResumableSSE', 'Could not refresh settled generation history', { - conversationId: settledConversationId, - error, - }); - } - - if (conversationLookup === 'not_found' && startedAsNewConvo) { - /** A failed/early-aborted first turn can settle without ever - * creating a conversation. Never mint A's optimistic rows here: - * prune any partial cache and return to `/c/new` instead of - * leaving a phantom chat/URL. */ - removeConvoFromAllQueries(queryClient, settledConversationId); - queryClient.removeQueries({ - queryKey: [QueryKeys.conversation, settledConversationId], - }); - queryClient.removeQueries({ - queryKey: [QueryKeys.messages, settledConversationId], - }); - newConversation?.({ - template: { conversationId: String(Constants.NEW_CONVO) }, - }); - } - if (conversationLookup === 'inconclusive') { - /** A network/5xx lookup cannot prove the conversation is absent. - * Preserve its optimistic/cache state for a later refetch and do - * not navigate or mint a fresh `/c/new` conversation. */ - clearDrainAfterAbort(settledConversationId, submission.expectedPredecessorCreatedAt); - } - clearStepMaps(); - setIsSubmitting(false); - setShowStopButton(false); - setRunEnd({ - conversationId: settledConversationId, - outcome: conversationLookup === 'inconclusive' ? 'error' : 'aborted', - startedAsNewConvo, - endedAt: run.scheduler.now(), - }); - setSubmission(null); - return; - } - if (startResult.status !== 'stream') { - return; - } - const { - streamId: newStreamId, - resumed, - generationCreatedAt, - generationProtocolVersion, - } = startResult; - setStreamId(newStreamId); - // Optimistically add to active jobs - addActiveJob(newStreamId); - // Queue title generation if this is a new conversation (first message). - // Skip temporary conversations: the server never generates titles for - // them, so polling would 404 indefinitely. - const isNewConvo = isInitialNewConversation(submission); - if (isNewConvo && !submission.isTemporary) { - queueTitleGeneration(newStreamId); - } - if (isInitialNewConversation(submission)) { - run.optimisticStreamIds.add(newStreamId); - replaceNewConversationUrl(newStreamId); - } - const streamSubmission = addOptimisticConversation(newStreamId, submission); - run.submission = streamSubmission; - // A deduped retry (status: 'resumed') attaches to an already-running stream, so - // subscribe with resume=true to replay its state instead of only live events. - subscribeToStream({ - streamId: newStreamId, - submission: streamSubmission, - isResume: resumed, - generationCreatedAt, - generationProtocolVersion, - signal, - }); - } else { - logger.error('ResumableSSE', 'Failed to get streamId from startGeneration'); - } - } - }; - - /* Fire-and-forget, but not silent: this sets the submitting flags before - it does any work, so a throw would leave the composer generating with no - stream, no final event and no way back but a reload. */ - initStream().catch((error: unknown) => { - logger.error('[useResumableSSE] Failed to start the stream', error); - setIsSubmitting(false); - setShowStopButton(false); - setSubmission(null); - }); - - return () => { - logger.log('ResumableSSE', 'Cleanup - closing SSE, resetting UI state'); - startController.abort(); - // Cleanup on unmount/navigation - close connection but DO NOT abort backend - // Reset UI state so it doesn't leak to other conversations - // If user returns to this conversation, useResumeOnLoad will restore the state - run.clearReconnectTimeout(); - run.cancelRetryFrames(); - run.stopForegroundReattach?.(); - run.stopForegroundReattach = null; - // Reset reconnect counter before closing (so abort handler doesn't think we're reconnecting) - run.reconnectAttempt = 0; - /** The pane is moving off a run that is still generating (another chat, a new chat, or - * an unmount): the server keeps going with no subscriber and deletes the job when done, - * so no terminal event will reach the queue drain. Remember the run; on return its end is - * read from history. Only a run whose identity the server confirmed (its epoch, and the - * user message `created` or a resume's sync named) is remembered, and not a regeneration, - * a run that already published its end, or one the user stopped. */ - const closing = run.submission; - const closingConvoId = closing?.conversation?.conversationId; - const closingUserMessageId = closing?.userMessage?.messageId; - const generationCreatedAt = run.subscribedEpoch; - if ( - run.stream != null && - run.identityConfirmed && - !run.runEndPublished && - closing?.isRegenerate !== true && - closingConvoId != null && - closingConvoId !== Constants.NEW_CONVO && - closingUserMessageId != null && - generationCreatedAt != null && - !isStoppedRun(jotaiStore.get(stopRequestedByConvoId(closingConvoId)), generationCreatedAt) - ) { - jotaiStore.set(detachedRunByConvoId(closingConvoId), { - userMessageId: closingUserMessageId, - responseMessageId: closing?.initialResponse?.messageId, - generationCreatedAt, - }); - } - run.stream?.abort(); - run.stream = null; - // Clear handler maps to prevent memory leaks and stale state - clearStepMaps(); - // Reset UI state on ordinary cleanup. A generation handoff already - // cached the replacement and must remain non-idle until resume-on-load - // installs its epoch-fenced submission. - if (!run.replacementHandoff) { - setIsSubmitting(false); - setShowStopButton(false); - } - run.replacementHandoff = false; - }; + return mountSubmission(run, host, submission); // eslint-disable-next-line react-hooks/exhaustive-deps }, [submission]); diff --git a/packages/chat/src/core/__fixtures__/resumable.ts b/packages/chat/src/core/__fixtures__/resumable.ts index d164237cfb..1c6bd0d85b 100644 --- a/packages/chat/src/core/__fixtures__/resumable.ts +++ b/packages/chat/src/core/__fixtures__/resumable.ts @@ -85,6 +85,15 @@ export const createHost = ( setConversation: jest.fn(), upsertConversation: jest.fn(), removeConversation: jest.fn(), + getConversation: jest.fn(() => undefined), + removeConversationQueries: jest.fn(), + queueTitleGeneration: jest.fn(), + addOptimisticConversation: jest.fn((_conversationId, submission) => submission), + }, + queue: { + clearSupersededMarkers: jest.fn(), + isStopped: jest.fn(() => false), + rememberDetachedRun: jest.fn(), }, pane: { setIsSubmitting: jest.fn(), @@ -100,6 +109,9 @@ export const createHost = ( updateConversation: jest.fn(), resolveDetachedEnd: jest.fn(() => ({ resolvable: false, end: null })), setAbortScroll: jest.fn(), + setConversation: jest.fn(), + startNewConversation: jest.fn(), + replaceNewConversationUrl: jest.fn(), renewActiveRun: jest.fn(), runEndTarget: jest.fn((params) => ({ conversationId: params.conversationId, diff --git a/packages/chat/src/core/__tests__/mount.spec.ts b/packages/chat/src/core/__tests__/mount.spec.ts new file mode 100644 index 0000000000..4d879611ab --- /dev/null +++ b/packages/chat/src/core/__tests__/mount.spec.ts @@ -0,0 +1,161 @@ +import { Constants } from 'librechat-data-provider'; +import type { TMessage, TSubmission, TConversation } from 'librechat-data-provider'; +import type { ResumableHost, StreamStatusResponse } from '../resumable'; +import { ResumableRun, mountSubmission, releaseSubmission } from '../resumable'; +import { createHost, createScheduler } from '../__fixtures__/resumable'; + +const submission = { + userMessage: { messageId: 'u-1', conversationId: 'c-1', isCreatedByUser: true, text: 'Hi' }, + initialResponse: { messageId: 'u-1_', conversationId: 'c-1' } as TMessage, + conversation: { conversationId: 'c-1', endpoint: 'agents' }, + endpointOption: { endpoint: 'agents' }, +} as unknown as TSubmission; + +const newChat = { + ...submission, + userMessage: { ...submission.userMessage, conversationId: null }, + conversation: { conversationId: Constants.NEW_CONVO, endpoint: 'agents' }, +} as unknown as TSubmission; + +const flush = () => new Promise((resolve) => setImmediate(resolve)); + +const setup = () => { + const fetchStatus = jest.fn, [string]>(); + const host: ResumableHost = createHost(fetchStatus); + const run = new ResumableRun(createScheduler().scheduler); + return { host, run, fetchStatus }; +}; + +describe('mountSubmission', () => { + it('re-attaches a resume submission without starting a generation', () => { + const { host, run } = setup(); + mountSubmission(run, host, { + ...submission, + resumeStreamId: 'c-1', + resumeGenerationCreatedAt: 2000, + resumeGenerationProtocolVersion: 2, + } as TSubmission); + expect(host.queue.clearSupersededMarkers).toHaveBeenCalledWith('c-1', expect.any(Function)); + const [, isSameGeneration] = (host.queue.clearSupersededMarkers as jest.Mock).mock.calls[0]; + expect(isSameGeneration(2000)).toBe(true); + expect(isSameGeneration(1000)).toBe(false); + expect(host.start).not.toHaveBeenCalled(); + expect(host.cache.addActiveJob).toHaveBeenCalledWith('c-1'); + expect(host.stream.reconnectToStream).toHaveBeenCalledWith( + expect.objectContaining({ url: expect.stringContaining('resume=true') }), + expect.anything(), + ); + }); + + it('starts a new chat, creates its optimistic records and attaches', async () => { + const { host, run } = setup(); + (host.start as jest.Mock).mockResolvedValue({ streamId: 'c-9', generationCreatedAt: 5 }); + mountSubmission(run, host, newChat); + await flush(); + expect(host.cache.queueTitleGeneration).toHaveBeenCalledWith('c-9'); + expect(host.pane.replaceNewConversationUrl).toHaveBeenCalledWith('c-9'); + expect(host.cache.addOptimisticConversation).toHaveBeenCalledWith('c-9', newChat); + expect(run.optimisticStreamIds.has('c-9')).toBe(true); + expect(host.stream.reconnectToStream).toHaveBeenCalledTimes(1); + }); + + it('ends a settled start from the persisted conversation without attaching', async () => { + const { host, run } = setup(); + (host.start as jest.Mock).mockResolvedValue({ + status: 'settled', + conversationId: 'c-1', + generationProtocolVersion: 2, + }); + const persisted = { conversationId: 'c-1', title: 'Saved' } as TConversation; + (host.cache.fetchConversation as jest.Mock).mockResolvedValue(persisted); + mountSubmission(run, host, submission); + await flush(); + expect(host.cache.setConversation).toHaveBeenCalledWith('c-1', persisted); + expect(host.cache.invalidateMessages).toHaveBeenCalledWith('c-1', 'all'); + expect(host.pane.publishRunEnd).toHaveBeenCalledWith( + expect.objectContaining({ conversationId: 'c-1', outcome: 'aborted' }), + ); + expect(host.stream.reconnectToStream).not.toHaveBeenCalled(); + }); + + it('hands a lost predecessor fence to the live replacement', async () => { + const { host, run, fetchStatus } = setup(); + const error = Object.assign(new Error('mismatch'), { + response: { + status: 409, + headers: {}, + data: { + status: 'predecessor_mismatch', + code: 'GENERATION_PREDECESSOR_MISMATCH', + streamId: 'c-1', + conversationId: 'c-1', + generationCreatedAt: 3000, + active: true, + }, + }, + }); + (host.start as jest.Mock).mockRejectedValue(error); + fetchStatus.mockResolvedValue({ + active: true, + streamId: 'c-1', + createdAt: 3000, + generationProtocolVersion: 2, + }); + mountSubmission(run, host, { + ...submission, + expectedPredecessorCreatedAt: 1000, + } as TSubmission); + await flush(); + await flush(); + expect(host.steers.restoreQueued).toHaveBeenCalledWith(expect.anything(), 3000); + expect(run.replacementHandoff).toBe(true); + expect(host.cache.setStreamStatus).toHaveBeenCalledWith( + 'c-1', + expect.objectContaining({ generationHandoff: true, createdAt: 3000 }), + ); + expect(host.pane.setSubmission).toHaveBeenCalledWith(null); + }); + + it('remembers a confirmed run the pane leaves mid-stream', () => { + const { host, run } = setup(); + const cleanup = mountSubmission(run, host, { + ...submission, + resumeStreamId: 'c-1', + resumeGenerationCreatedAt: 2000, + } as TSubmission); + run.identityConfirmed = true; + cleanup(); + expect(host.queue.rememberDetachedRun).toHaveBeenCalledWith('c-1', { + userMessageId: 'u-1', + responseMessageId: 'u-1_', + generationCreatedAt: 2000, + }); + expect(run.stream).toBeNull(); + expect(host.pane.setIsSubmitting).toHaveBeenLastCalledWith(false); + }); + + it('keeps the pane busy through a generation handoff', () => { + const { host, run } = setup(); + const cleanup = mountSubmission(run, host, submission); + run.replacementHandoff = true; + (host.pane.setIsSubmitting as jest.Mock).mockClear(); + cleanup(); + expect(host.pane.setIsSubmitting).not.toHaveBeenCalled(); + expect(run.replacementHandoff).toBe(false); + }); +}); + +describe('releaseSubmission', () => { + it('closes the connection without aborting the generation', () => { + const { host, run } = setup(); + const stream = new AbortController(); + run.stream = stream; + run.submission = submission; + run.reconnectAttempt = 3; + releaseSubmission(run, host); + expect(stream.signal.aborted).toBe(true); + expect(run.submission).toBeNull(); + expect(run.reconnectAttempt).toBe(0); + expect(host.pane.setStreamId).toHaveBeenCalledWith(null); + }); +}); diff --git a/packages/chat/src/core/resumable/host.ts b/packages/chat/src/core/resumable/host.ts index 4397c8c4f3..23a5902abf 100644 --- a/packages/chat/src/core/resumable/host.ts +++ b/packages/chat/src/core/resumable/host.ts @@ -22,6 +22,7 @@ import type { import type { GenerationProtocolVersion } from '../protocol'; import type { RunEnd } from '../steer/types'; import type { StepEvent } from '../reducer'; +import type { ResumableRun } from './run'; /** Where a resumable run reports what it is doing. The app passes its own logger. */ export interface ResumableLogger { @@ -166,7 +167,7 @@ export interface ResumableSteers { export interface ResumableCache { /** Marks the conversation's messages stale; `all` refetches them now, `none` leaves that to * the next read. */ - invalidateMessages: (conversationId: string, refetch: 'all' | 'none') => Promise; + invalidateMessages: (conversationId: string, refetch: 'all' | 'active' | 'none') => Promise; /** Fetches the conversation's messages through its query. `fallback` reads them directly * when no query for the conversation is mounted yet. */ fetchMessages: (conversationId: string, fallback?: boolean) => Promise; @@ -187,6 +188,33 @@ export interface ResumableCache { upsertConversation: (conversation: TConversation) => void; /** Drops the conversation from every conversation list. */ removeConversation: (conversationId: string) => void; + getConversation: (conversationId: string) => TConversation | undefined; + /** Drops the conversation's own record and messages from the cache. */ + removeConversationQueries: (conversationId: string) => void; + /** Polls for the title the server generates for a new conversation. */ + queueTitleGeneration: (conversationId: string) => void; + /** + * Gives a new chat its conversation record and messages under the id the server assigned, + * before any stream event names it; returns the submission rewritten onto that id. + */ + addOptimisticConversation: (conversationId: string, submission: TSubmission) => TSubmission; +} + +/** The run markers the queue drain reads for a conversation this pane may leave mid-run. */ +export interface ResumableQueue { + /** Clears the stop and detached-run markers an earlier generation left, keeping the ones + * `isSameGeneration` says belong to the generation being resumed. */ + clearSupersededMarkers: ( + conversationId: string, + isSameGeneration: (generationCreatedAt?: number) => boolean, + ) => void; + /** Whether the user stopped this generation. */ + isStopped: (conversationId: string, generationCreatedAt: number) => boolean; + /** Records a run the pane left while it was still generating. */ + rememberDetachedRun: ( + conversationId: string, + run: { userMessageId: string; responseMessageId?: string; generationCreatedAt: number }, + ) => void; } /** The pane's controls and run bookkeeping a resumable run drives. */ @@ -223,6 +251,12 @@ export interface ResumablePane { earlyAbort: boolean; startedAsNewConvo: boolean; }) => { conversationId: string; startedAsNewConvo: boolean }; + /** Sets the pane's conversation, whatever response the pane adds. */ + setConversation?: (update: (current: TConversation | null) => TConversation | null) => void; + /** Resets the pane to a fresh new chat. */ + startNewConversation?: () => void; + /** Moves the address bar off `/c/new` onto the conversation the server assigned. */ + replaceNewConversationUrl: (conversationId: string) => void; /** Updates the pane's conversation; absent for a pane that only adds a comparison response. */ updateConversation?: (update: (current: TConversation | null) => TConversation | null) => void; /** @@ -314,6 +348,7 @@ export interface ResumableParts { /** Everything a resumable run needs from the app it runs in. */ export interface ResumableHost extends SubscriptionHost { cache: ResumableCache; + queue: ResumableQueue; pane: ResumablePane; steers: ResumableSteers; events: ResumableEvents; @@ -327,3 +362,14 @@ export interface ResumableHost extends SubscriptionHost { /** Receives the dev-only controls that simulate a dropped or a clean close. */ exposeDebugControls?: (controls: { killNetwork: () => void; closeClean: () => void }) => void; } + +/** One submission the pane serves, from its start (or resume) until it is replaced. */ +export interface MountContext { + run: ResumableRun; + host: ResumableHost; + submission: TSubmission; + /** Aborted once the pane moves to another submission or unmounts. */ + signal: AbortSignal; + /** Whether this submission is still the one the pane serves. */ + isCurrent: () => boolean; +} diff --git a/packages/chat/src/core/resumable/index.ts b/packages/chat/src/core/resumable/index.ts index db73151282..80824b9132 100644 --- a/packages/chat/src/core/resumable/index.ts +++ b/packages/chat/src/core/resumable/index.ts @@ -16,3 +16,6 @@ export * from './frames'; export * from './sync'; export * from './attach'; export * from './launch'; +export * from './mount'; +export * from './outcomes'; +export * from './settled'; diff --git a/packages/chat/src/core/resumable/mount.ts b/packages/chat/src/core/resumable/mount.ts new file mode 100644 index 0000000000..76a5f60ea6 --- /dev/null +++ b/packages/chat/src/core/resumable/mount.ts @@ -0,0 +1,278 @@ +import { Constants } from 'librechat-data-provider'; +import type { TSubmission } from 'librechat-data-provider'; +import type { ResumableHost, MountContext } from './host'; +import type { StartGenerationResult } from './start'; +import type { ResumeSubmission } from './resume'; +import type { ResumableRun } from './run'; +import { resolvePredecessorMismatch, handoffStartToReplacement } from './outcomes'; +import { isInitialNewConversation } from './messages'; +import { attachResumableStream } from './attach'; +import { startGeneration } from './launch'; +import { settleStart } from './settled'; + +/** + * The pane no longer serves a submission: close the connection without dispatching a cancel + * (navigation must not abort the generation) and drop the pending re-attach. + */ +export function releaseSubmission(run: ResumableRun, host: ResumableHost): void { + host.logger.log('ResumableSSE', 'No submission, cleaning up'); + // Clear reconnect timeout if submission is cleared + run.clearReconnectTimeout(); + run.stopForegroundReattach?.(); + run.stopForegroundReattach = null; + // Close SSE but do NOT dispatch cancel - navigation should not abort + run.stream?.abort(); + run.stream = null; + host.pane.setStreamId(null); + run.reconnectAttempt = 0; + run.submission = null; +} + +/** Attaches the stream a start named, creating the new chat's optimistic records first. */ +function attachStartedStream( + { run, host, submission, signal }: MountContext, + startResult: Extract, +): void { + const { + streamId: newStreamId, + resumed, + generationCreatedAt, + generationProtocolVersion, + } = startResult; + host.pane.setStreamId(newStreamId); + // Optimistically add to active jobs + host.cache.addActiveJob(newStreamId); + // Queue title generation if this is a new conversation (first message). + // Skip temporary conversations: the server never generates titles for + // them, so polling would 404 indefinitely. + const isNewConvo = isInitialNewConversation(submission); + if (isNewConvo && !submission.isTemporary) { + host.cache.queueTitleGeneration(newStreamId); + } + if (isInitialNewConversation(submission)) { + run.optimisticStreamIds.add(newStreamId); + host.pane.replaceNewConversationUrl(newStreamId); + } + const streamSubmission = host.cache.addOptimisticConversation(newStreamId, submission); + run.submission = streamSubmission; + // A deduped retry (status: 'resumed') attaches to an already-running stream, so + // subscribe with resume=true to replay its state instead of only live events. + attachResumableStream(run, host, { + streamId: newStreamId, + submission: streamSubmission, + isResume: resumed, + generationCreatedAt, + generationProtocolVersion, + signal, + }); +} + +/** Acts on what the generation start reported. */ +async function settleStartResult( + mount: MountContext, + startResult: StartGenerationResult, +): Promise { + let replacementStart = startResult.status === 'replaced' ? startResult : null; + let knownReplacementStatus = null; + + if (startResult.status === 'predecessor_mismatch') { + const resolved = await resolvePredecessorMismatch(mount, startResult); + if (resolved == null) { + return; + } + ({ replacementStart, knownReplacementStatus } = resolved); + } + + if (replacementStart != null) { + await handoffStartToReplacement(mount, replacementStart, knownReplacementStatus); + return; + } + if (startResult.status === 'predecessor_mismatch') { + // Every mismatch path above either hands off or reconciles. + return; + } + if (startResult.status === 'settled') { + await settleStart(mount, startResult); + return; + } + if (startResult.status !== 'stream') { + return; + } + attachStartedStream(mount, startResult); +} + +/** Starts the submission's generation, or re-attaches to the one it resumes. */ +async function initStream( + mount: MountContext, + resume: Partial> & + Pick, +): Promise { + const { run, host, submission, signal, isCurrent } = mount; + const { resumeStreamId, resumeGenerationCreatedAt, resumeGenerationProtocolVersion } = resume; + if (!isCurrent()) { + return; + } + + host.usage.bindResponse(submission); + host.pane.setIsSubmitting(true); + /** Starting a new generation is the one interval where `isSubmitting` + * is true but no generation epoch exists yet. Clear any prior epoch and + * withhold live Stop/steer controls; the composer can still queue input. + * The attach exposes controls once the POST/resume installs the + * server-provided epoch. */ + const submissionConversationId = submission.conversation?.conversationId ?? Constants.NEW_CONVO; + if (!resumeStreamId || resumeGenerationCreatedAt == null) { + host.pane.setGenerationEpoch(resumeStreamId ?? submissionConversationId, null); + } + host.pane.setShowStopButton(false); + + if (resumeStreamId) { + if (!isCurrent()) { + return; + } + // Resume: just subscribe to existing stream, don't start new generation + host.logger.log('ResumableSSE', 'Resuming existing stream:', resumeStreamId); + host.pane.setStreamId(resumeStreamId); + // Optimistically add to active jobs (in case it's not already there) + host.cache.addActiveJob(resumeStreamId); + attachResumableStream(run, host, { + streamId: resumeStreamId, + submission, + isResume: true, + generationCreatedAt: resumeGenerationCreatedAt, + generationProtocolVersion: resumeGenerationProtocolVersion, + signal, + }); + return; + } + // New generation: start and then subscribe + host.logger.log('ResumableSSE', 'Starting NEW generation'); + const startResult = await startGeneration(run, host, submission, signal); + if (!isCurrent()) { + return; + } + if (startResult) { + await settleStartResult(mount, startResult); + } else { + host.logger.error('ResumableSSE', 'Failed to get streamId from startGeneration'); + } +} + +/** + * The pane is moving off a run that is still generating (another chat, a new chat, or an + * unmount): the server keeps going with no subscriber and deletes the job when done, so no + * terminal event will reach the queue drain. Remember the run; on return its end is read from + * history. Only a run whose identity the server confirmed (its epoch, and the user message + * `created` or a resume's sync named) is remembered, and not a regeneration, a run that + * already published its end, or one the user stopped. + */ +function rememberDetachedRun(run: ResumableRun, host: ResumableHost): void { + const closing = run.submission; + const closingConvoId = closing?.conversation?.conversationId; + const closingUserMessageId = closing?.userMessage?.messageId; + const generationCreatedAt = run.subscribedEpoch; + if ( + run.stream != null && + run.identityConfirmed && + !run.runEndPublished && + closing?.isRegenerate !== true && + closingConvoId != null && + closingConvoId !== Constants.NEW_CONVO && + closingUserMessageId != null && + generationCreatedAt != null && + !host.queue.isStopped(closingConvoId, generationCreatedAt) + ) { + host.queue.rememberDetachedRun(closingConvoId, { + userMessageId: closingUserMessageId, + responseMessageId: closing?.initialResponse?.messageId, + generationCreatedAt, + }); + } +} + +/** + * Serves `submission` in the pane: starts its generation (or resumes the one it names) and + * attaches to the stream. Returns the cleanup that closes the connection without aborting the + * generation server-side and resets the pane's controls. + */ +export function mountSubmission( + run: ResumableRun, + host: ResumableHost, + submission: TSubmission, +): () => void { + const { resumeStreamId, resumeGenerationCreatedAt, resumeGenerationProtocolVersion } = + submission as Partial; + host.logger.log('ResumableSSE', 'Effect triggered', { + conversationId: submission.conversation?.conversationId, + hasResumeStreamId: !!resumeStreamId, + resumeStreamId, + userMessageId: submission.userMessage?.messageId, + }); + + run.submission = submission; + run.runEndPublished = false; + run.identityConfirmed = false; + run.subscribedEpoch = null; + if (submission.conversation?.conversationId != null) { + /** A different run starting here owns the conversation's end from now on: an earlier run + * this pane left is superseded, and a Stop belonged to that earlier run. Reattaching to + * the same generation keeps both, since that run's end is still owed and it was stopped. */ + host.queue.clearSupersededMarkers( + submission.conversation.conversationId, + (generationCreatedAt) => + resumeStreamId != null && + resumeGenerationCreatedAt != null && + generationCreatedAt === resumeGenerationCreatedAt, + ); + } + const startController = new AbortController(); + const { signal } = startController; + const mount: MountContext = { + run, + host, + submission, + signal, + isCurrent: () => !signal.aborted && run.submission === submission, + }; + + /* Fire-and-forget, but not silent: this sets the submitting flags before + it does any work, so a throw would leave the composer generating with no + stream, no final event and no way back but a reload. */ + initStream(mount, { + resumeStreamId, + resumeGenerationCreatedAt, + resumeGenerationProtocolVersion: resumeGenerationProtocolVersion ?? 1, + }).catch((error: unknown) => { + host.logger.error('[useResumableSSE] Failed to start the stream', error); + host.pane.setIsSubmitting(false); + host.pane.setShowStopButton(false); + host.pane.setSubmission(null); + }); + + return () => { + host.logger.log('ResumableSSE', 'Cleanup - closing SSE, resetting UI state'); + startController.abort(); + // Cleanup on unmount/navigation - close connection but DO NOT abort backend + // Reset UI state so it doesn't leak to other conversations + // If user returns to this conversation, useResumeOnLoad will restore the state + run.clearReconnectTimeout(); + run.cancelRetryFrames(); + run.stopForegroundReattach?.(); + run.stopForegroundReattach = null; + // Reset reconnect counter before closing (so abort handler doesn't think we're reconnecting) + run.reconnectAttempt = 0; + rememberDetachedRun(run, host); + run.stream?.abort(); + run.stream = null; + // Clear handler maps to prevent memory leaks and stale state + host.events.clearStepMaps(); + // Reset UI state on ordinary cleanup. A generation handoff already + // cached the replacement and must remain non-idle until resume-on-load + // installs its epoch-fenced submission. + if (!run.replacementHandoff) { + host.pane.setIsSubmitting(false); + host.pane.setShowStopButton(false); + } + run.replacementHandoff = false; + }; +} diff --git a/packages/chat/src/core/resumable/outcomes.ts b/packages/chat/src/core/resumable/outcomes.ts new file mode 100644 index 0000000000..e677abd0bf --- /dev/null +++ b/packages/chat/src/core/resumable/outcomes.ts @@ -0,0 +1,270 @@ +import { Constants } from 'librechat-data-provider'; +import type { TPendingSteer } from 'librechat-data-provider'; +import type { StreamStatusResponse, MountContext } from './host'; +import type { StartGenerationResult } from './start'; +import { GENERATION_PROTOCOL_VERSION, supportsGenerationProtocolV2 } from '../protocol'; +import { isInitialNewConversation, keepLocalCodeApprovalMode } from './messages'; + +export type ReplacedStart = Extract; +type MismatchStart = Extract; + +/** + * A queued turn lost its predecessor fence. Decides from a fresh status read (or the atomic + * 409 itself) whether a different live epoch owns the conversation, restoring the queued turn + * either way. Resolves to the replacement to hand off to, or null once it reconciled history + * without one (or the pane moved on). + */ +export async function resolvePredecessorMismatch( + mount: MountContext, + startResult: MismatchStart, +): Promise<{ + replacementStart: ReplacedStart | null; + knownReplacementStatus: StreamStatusResponse | null; +} | null> { + const { run, host, submission, isCurrent } = mount; + let replacementStart: ReplacedStart | null = null; + let knownReplacementStatus: StreamStatusResponse | null = null; + const expectedPredecessorCreatedAt = submission.expectedPredecessorCreatedAt; + let refreshedStatus: StreamStatusResponse | null = null; + try { + refreshedStatus = await host.fetchStatus(startResult.conversationId); + if (!isCurrent()) { + return null; + } + } catch (error) { + if (!isCurrent()) { + return null; + } + host.logger.warn('ResumableSSE', 'Could not refresh predecessor mismatch status', { + conversationId: startResult.conversationId, + error, + }); + } + if (!isCurrent()) { + return null; + } + + const reportedReplacement = + startResult.active === true && + startResult.generationCreatedAt != null && + startResult.generationCreatedAt !== expectedPredecessorCreatedAt; + + if ( + refreshedStatus?.active === true && + supportsGenerationProtocolV2(refreshedStatus) && + typeof refreshedStatus.streamId === 'string' && + refreshedStatus.streamId.length > 0 && + refreshedStatus.createdAt != null && + refreshedStatus.createdAt !== expectedPredecessorCreatedAt + ) { + replacementStart = { + status: 'replaced', + streamId: refreshedStatus.streamId, + conversationId: startResult.conversationId, + generationCreatedAt: refreshedStatus.createdAt, + generationProtocolVersion: GENERATION_PROTOCOL_VERSION, + }; + knownReplacementStatus = refreshedStatus; + host.steers.restoreQueued(submission, refreshedStatus.createdAt); + } else if (reportedReplacement) { + /** The atomic 409 is itself authoritative at admission time. + * Status publication can lag that response, so retain its exact + * epoch as a minimal handoff snapshot; the fenced SSE attach can + * reconcile if the replacement finishes in the meantime. */ + replacementStart = { + status: 'replaced', + streamId: startResult.streamId, + conversationId: startResult.conversationId, + generationCreatedAt: startResult.generationCreatedAt!, + generationProtocolVersion: GENERATION_PROTOCOL_VERSION, + }; + knownReplacementStatus = { + active: true, + status: 'running', + streamId: startResult.streamId, + createdAt: startResult.generationCreatedAt, + generationProtocolVersion: GENERATION_PROTOCOL_VERSION, + }; + host.steers.restoreQueued(submission, startResult.generationCreatedAt!); + } else { + /** No different live epoch is proven. Reconcile away the + * optimistic C rows, but publish no run-end: doing so would + * immediately drain and re-send the just-restored queue item. */ + const terminalWinnerCreatedAt = refreshedStatus?.createdAt ?? startResult.generationCreatedAt; + if (terminalWinnerCreatedAt != null) { + host.steers.restoreQueued(submission, terminalWinnerCreatedAt); + } + if (refreshedStatus != null) { + host.cache.setStreamStatus(startResult.conversationId, refreshedStatus); + } else { + host.cache.invalidateStreamStatus(startResult.conversationId); + } + try { + await host.cache.invalidateMessages(startResult.conversationId, 'all'); + if (!isCurrent()) { + return null; + } + await host.cache.refreshConversation(startResult.conversationId); + if (!isCurrent()) { + return null; + } + await host.cache.refreshConversationLists(); + if (!isCurrent()) { + return null; + } + await host.cache.refreshPinnedConversations(); + if (!isCurrent()) { + return null; + } + } catch (error) { + if (!isCurrent()) { + return null; + } + host.logger.warn('ResumableSSE', 'Could not reconcile predecessor mismatch history', { + conversationId: startResult.conversationId, + error, + }); + } + host.pane.clearDrainAfterAbort(startResult.conversationId, expectedPredecessorCreatedAt); + run.submission = null; + host.pane.setIsSubmitting(false); + host.pane.setShowStopButton(false); + host.pane.setSubmission(null); + return null; + } + return { replacementStart, knownReplacementStatus }; +} + +/** + * This POST belongs to an older optimistic submission, while the conversation-scoped stream + * now belongs to a newer generation. Hands the pane to that generation without ever attaching + * the older submission to it. + */ +export async function handoffStartToReplacement( + mount: MountContext, + replacementStart: ReplacedStart, + knownReplacementStatus: StreamStatusResponse | null, +): Promise { + const { run, host, submission, isCurrent } = mount; + /** This POST belongs to an older optimistic submission, while the + * conversation-scoped stream id now belongs to a newer generation. + * Never attach A's submission directly to B. Discover B's + * authoritative resume snapshot, refresh durable history, then let + * useResumeOnLoad construct a new epoch-fenced submission. */ + const replacementStatus = + knownReplacementStatus ?? + (await host.fetchStatus(replacementStart.conversationId).catch((error) => { + if (!isCurrent()) { + return null; + } + host.logger.warn('ResumableSSE', 'Could not discover replacement start generation', { + conversationId: replacementStart.conversationId, + generationCreatedAt: replacementStart.generationCreatedAt, + error, + }); + return null; + })); + if (!isCurrent()) { + return; + } + if ( + replacementStatus?.active === true && + supportsGenerationProtocolV2(replacementStatus) && + replacementStatus.streamId === replacementStart.streamId && + replacementStatus.createdAt === replacementStart.generationCreatedAt + ) { + const startedAsNewConvo = isInitialNewConversation(submission); + try { + await host.cache.invalidateMessages(replacementStart.conversationId, 'all'); + if (!isCurrent()) { + return; + } + await host.cache.refreshConversation(replacementStart.conversationId); + if (!isCurrent()) { + return; + } + await host.cache.refreshConversationLists(); + if (!isCurrent()) { + return; + } + await host.cache.refreshPinnedConversations(); + if (!isCurrent()) { + return; + } + } catch (error) { + if (!isCurrent()) { + return; + } + host.logger.warn('ResumableSSE', 'Could not refresh replacement generation history', { + conversationId: replacementStart.conversationId, + error, + }); + } + const authoritativeConversation = host.cache.getConversation(replacementStart.conversationId); + if (authoritativeConversation?.conversationId === replacementStart.conversationId) { + const localConversationId = isInitialNewConversation(submission) + ? Constants.NEW_CONVO + : replacementStart.conversationId; + host.pane.setConversation?.((current) => + keepLocalCodeApprovalMode(authoritativeConversation, current, localConversationId), + ); + } + host.cache.setStreamStatus(replacementStart.conversationId, { + ...replacementStatus, + generationHandoff: true, + }); + host.pane.setGenerationEpoch( + replacementStart.conversationId, + replacementStart.generationCreatedAt, + undefined, + replacementStart.generationProtocolVersion, + ); + host.cache.addActiveJob(replacementStart.streamId); + if (startedAsNewConvo) { + run.optimisticStreamIds.add(replacementStart.streamId); + host.pane.setGenerationEpoch( + Constants.NEW_CONVO, + replacementStart.generationCreatedAt, + undefined, + replacementStart.generationProtocolVersion, + ); + host.pane.replaceNewConversationUrl(replacementStart.conversationId); + } + host.steers.seed( + replacementStart.conversationId, + (replacementStatus.resumeState?.pendingSteers ?? []) as TPendingSteer[], + replacementStart.generationCreatedAt, + replacementStart.generationProtocolVersion, + ); + host.steers.settleApplied( + replacementStart.conversationId, + replacementStatus.resumeState?.aggregatedContent ?? replacementStatus.aggregatedContent, + ); + run.replacementHandoff = true; + run.submission = null; + host.pane.setIsSubmitting(true); + host.pane.setShowStopButton(true); + host.pane.setSubmission(null); + return; + } + + /** The server reported a replacement but its authoritative status + * could not be confirmed. Drop A without ever attaching it to B; + * leave history/status invalidated so navigation/reload can recover, + * and keep any queued text parked (non-completed outcome). */ + host.cache.invalidateStreamStatus(replacementStart.conversationId); + host.cache.invalidateMessages(replacementStart.conversationId, 'active'); + host.pane.clearDrainAfterAbort( + replacementStart.conversationId, + replacementStart.generationCreatedAt, + ); + host.pane.setIsSubmitting(false); + host.pane.setShowStopButton(false); + host.pane.publishRunEnd({ + conversationId: replacementStart.conversationId, + outcome: 'error', + endedAt: run.scheduler.now(), + generationCreatedAt: replacementStart.generationCreatedAt, + }); + host.pane.setSubmission(null); +} diff --git a/packages/chat/src/core/resumable/settled.ts b/packages/chat/src/core/resumable/settled.ts new file mode 100644 index 0000000000..95cdb94fda --- /dev/null +++ b/packages/chat/src/core/resumable/settled.ts @@ -0,0 +1,114 @@ +import { Constants } from 'librechat-data-provider'; +import type { TConversation } from 'librechat-data-provider'; +import type { StartGenerationResult } from './start'; +import type { MountContext } from './host'; +import { isInitialNewConversation, keepLocalCodeApprovalMode } from './messages'; +import { toStartGenerationError } from './start'; + +/** + * The server reports the turn already settled (a duplicate start of a finished generation). + * Adopts the persisted conversation, refreshes history, and ends the run without attaching. + */ +export async function settleStart( + mount: MountContext, + startResult: Extract, +): Promise { + const { run, host, submission, isCurrent } = mount; + const settledConversationId = startResult.conversationId; + const startedAsNewConvo = isInitialNewConversation(submission); + let persistedConversation: TConversation | undefined; + let conversationLookup: 'found' | 'not_found' | 'inconclusive' = 'inconclusive'; + try { + persistedConversation = await host.cache.fetchConversation(settledConversationId); + if (!isCurrent()) { + return; + } + conversationLookup = 'found'; + } catch (error) { + if (!isCurrent()) { + return; + } + conversationLookup = + toStartGenerationError(error)?.response?.status === 404 ? 'not_found' : 'inconclusive'; + host.logger.warn('ResumableSSE', 'Could not load settled generation conversation', { + conversationId: settledConversationId, + conversationLookup, + error, + }); + } + + if (persistedConversation != null) { + /** The persisted copy carries the mode the settled turn started + * with; a pick made while the start was pending is newer. The + * live conversation still holds the new-chat id here because no + * stream event ran for this turn. */ + const settledCopy = persistedConversation; + const localConversationId = startedAsNewConvo ? Constants.NEW_CONVO : settledConversationId; + const conversationRecord = keepLocalCodeApprovalMode( + settledCopy, + host.cache.getConversation(settledConversationId), + settledConversationId, + ); + host.cache.setConversation(settledConversationId, conversationRecord); + host.cache.upsertConversation(conversationRecord); + host.pane.setConversation?.((current) => + keepLocalCodeApprovalMode(settledCopy, current, localConversationId), + ); + if (startedAsNewConvo) { + host.pane.replaceNewConversationUrl(settledConversationId); + } + } + + try { + await host.cache.invalidateMessages(settledConversationId, 'all'); + if (!isCurrent()) { + return; + } + await host.cache.refreshConversation(settledConversationId); + if (!isCurrent()) { + return; + } + await host.cache.refreshConversationLists(); + if (!isCurrent()) { + return; + } + await host.cache.refreshPinnedConversations(); + if (!isCurrent()) { + return; + } + } catch (error) { + if (!isCurrent()) { + return; + } + host.logger.warn('ResumableSSE', 'Could not refresh settled generation history', { + conversationId: settledConversationId, + error, + }); + } + + if (conversationLookup === 'not_found' && startedAsNewConvo) { + /** A failed/early-aborted first turn can settle without ever + * creating a conversation. Never mint A's optimistic rows here: + * prune any partial cache and return to `/c/new` instead of + * leaving a phantom chat/URL. */ + host.cache.removeConversation(settledConversationId); + host.cache.removeConversationQueries(settledConversationId); + host.pane.startNewConversation?.(); + } + if (conversationLookup === 'inconclusive') { + /** A network/5xx lookup cannot prove the conversation is absent. + * Preserve its optimistic/cache state for a later refetch and do + * not navigate or mint a fresh `/c/new` conversation. */ + host.pane.clearDrainAfterAbort(settledConversationId, submission.expectedPredecessorCreatedAt); + } + host.events.clearStepMaps(); + host.pane.setIsSubmitting(false); + host.pane.setShowStopButton(false); + host.pane.publishRunEnd({ + conversationId: settledConversationId, + outcome: conversationLookup === 'inconclusive' ? 'error' : 'aborted', + startedAsNewConvo, + endedAt: run.scheduler.now(), + }); + host.pane.setSubmission(null); +}