From da84398a44b91e54fa8d55cbb719399705516e44 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Mon, 26 Oct 2026 07:40:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=90=20refactor:=20Split=20History=20Pl?= =?UTF-8?q?acement=20Out=20of=20the=20Resumable=20resume.ts=20(#17428)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/chat/src/core/resumable/index.ts | 1 + packages/chat/src/core/resumable/placement.ts | 226 ++++++++++++++++++ packages/chat/src/core/resumable/resume.ts | 225 ----------------- packages/chat/src/core/resumable/sync.ts | 3 +- 4 files changed, 229 insertions(+), 226 deletions(-) create mode 100644 packages/chat/src/core/resumable/placement.ts diff --git a/packages/chat/src/core/resumable/index.ts b/packages/chat/src/core/resumable/index.ts index 80824b9132..412d1416c7 100644 --- a/packages/chat/src/core/resumable/index.ts +++ b/packages/chat/src/core/resumable/index.ts @@ -2,6 +2,7 @@ export * from './start'; export * from './errors'; export * from './events'; export * from './resume'; +export * from './placement'; export * from './messages'; export * from './run'; export * from './host'; diff --git a/packages/chat/src/core/resumable/placement.ts b/packages/chat/src/core/resumable/placement.ts new file mode 100644 index 0000000000..1619e7cabb --- /dev/null +++ b/packages/chat/src/core/resumable/placement.ts @@ -0,0 +1,226 @@ +import type { Agents, TMessage, TSubmission } from 'librechat-data-provider'; + +export type ResumeMessageIndexes = { + userIndex: number; + responseIndex: number; + preliminaryUserIndex: number; + preliminaryResponseIndex: number; +}; + +export const getResumeMessageIndexes = ( + messages: TMessage[], + userMessageId: string, + responseMessageId: string, + preliminaryUserMessageId?: string, + preliminaryResponseMessageId?: string, +): ResumeMessageIndexes => { + let userIndex = -1; + let responseIndex = -1; + let preliminaryUserIndex = -1; + let preliminaryResponseIndex = -1; + const hasPreliminaryResponse = preliminaryResponseMessageId?.endsWith('_') === true; + const eligiblePreliminaryUserId = + hasPreliminaryResponse && preliminaryUserMessageId && preliminaryUserMessageId !== userMessageId + ? preliminaryUserMessageId + : undefined; + const eligiblePreliminaryResponseId = + hasPreliminaryResponse && preliminaryResponseMessageId !== responseMessageId + ? preliminaryResponseMessageId + : undefined; + + for (let index = 0; index < messages.length; index++) { + const messageId = messages[index]?.messageId; + if (userIndex < 0 && messageId === userMessageId) { + userIndex = index; + } + if (responseIndex < 0 && messageId === responseMessageId) { + responseIndex = index; + } + if ( + preliminaryUserIndex < 0 && + eligiblePreliminaryUserId && + messageId === eligiblePreliminaryUserId + ) { + preliminaryUserIndex = index; + } + if ( + preliminaryResponseIndex < 0 && + eligiblePreliminaryResponseId && + messageId === eligiblePreliminaryResponseId + ) { + preliminaryResponseIndex = index; + } + } + + return { userIndex, responseIndex, preliminaryUserIndex, preliminaryResponseIndex }; +}; + +export const mergeResumeMessages = ( + messages: TMessage[], + userMessage: TMessage, + responseMessage: TMessage, + indexes: ResumeMessageIndexes, + /** + * An anchored run (a compaction) owns no user row. Its user-message slot + * holds the leaf the summary hangs off, a row the transcript already has: + * merging the slot onto it would replace that answer with an empty user + * message, and inserting it would duplicate its id. Either way the row loses + * its parent and `buildTree` files it as a phantom root, folding the thread. + */ + anchoredUserMessage = false, +): TMessage[] => { + const nextMessages = [...messages]; + let { userIndex, responseIndex, preliminaryResponseIndex } = indexes; + const { preliminaryUserIndex } = indexes; + + if (preliminaryUserIndex >= 0 && !anchoredUserMessage) { + if (userIndex >= 0) { + nextMessages.splice(preliminaryUserIndex, 1); + if (userIndex > preliminaryUserIndex) { + userIndex -= 1; + } + if (responseIndex > preliminaryUserIndex) { + responseIndex -= 1; + } + if (preliminaryResponseIndex > preliminaryUserIndex) { + preliminaryResponseIndex -= 1; + } + } else { + nextMessages[preliminaryUserIndex] = { + ...nextMessages[preliminaryUserIndex], + ...userMessage, + }; + userIndex = preliminaryUserIndex; + } + } + + if (preliminaryResponseIndex >= 0) { + if (responseIndex >= 0) { + nextMessages.splice(preliminaryResponseIndex, 1); + if (userIndex > preliminaryResponseIndex) { + userIndex -= 1; + } + if (responseIndex > preliminaryResponseIndex) { + responseIndex -= 1; + } + } else { + nextMessages[preliminaryResponseIndex] = { + ...nextMessages[preliminaryResponseIndex], + ...responseMessage, + }; + responseIndex = preliminaryResponseIndex; + } + } + + if (userIndex >= 0 && !anchoredUserMessage) { + nextMessages[userIndex] = { ...nextMessages[userIndex], ...userMessage }; + } + + if (responseIndex >= 0) { + nextMessages[responseIndex] = { ...nextMessages[responseIndex], ...responseMessage }; + } + + if (anchoredUserMessage) { + return responseIndex >= 0 ? nextMessages : [...nextMessages, responseMessage]; + } + + if (userIndex >= 0 && responseIndex >= 0) { + return nextMessages; + } + + if (userIndex >= 0) { + return [...nextMessages, responseMessage]; + } + + if (responseIndex >= 0) { + nextMessages.splice(responseIndex, 0, userMessage); + return nextMessages; + } + + return [...nextMessages, userMessage, responseMessage]; +}; + +export function hasSubmissionUserMessage( + submission: TSubmission | null, + messages: TMessage[] | undefined, + conversationId: string | undefined, +): boolean { + const userMessageId = submission?.userMessage?.messageId; + if (!userMessageId || !conversationId || !messages?.length) { + return false; + } + + return messages.some( + (message) => + message.isCreatedByUser === true && + message.messageId === userMessageId && + message.conversationId === conversationId, + ); +} + +export function resumeStateMatchesSubmission( + streamStatus: { resumeState?: Agents.ResumeState } | undefined, + submission: TSubmission | null, +): boolean { + const resumeState = streamStatus?.resumeState; + if (!resumeState || !submission) { + return false; + } + + const userMessageId = submission.userMessage?.messageId; + if (userMessageId && resumeState.userMessage?.messageId === userMessageId) { + return true; + } + + const responseMessageId = submission.initialResponse?.messageId; + return !!responseMessageId && resumeState.responseMessageId === responseMessageId; +} + +/** + * The row that names the branch a resumed run belongs to when its own response + * is not in the loaded history yet. A compaction's user-message slot is the leaf + * it summarizes up to and carries no parent (`projectCompactionAnchor`), so + * there the anchor itself names the branch; without this the pane restores no + * sibling selection and a compaction started on an older branch comes back on + * whichever branch the default lands on. + */ +function getResumeBranchFallbackMessageId( + resumeState: Agents.ResumeState, +): string | null | undefined { + return resumeState.userMessage?.parentMessageId ?? resumeState.userMessage?.messageId; +} + +export function getResumeBranchTargetMessageId( + resumeState: Agents.ResumeState, + messages: TMessage[], +): string | null | undefined { + const responseMessageId = resumeState.responseMessageId; + if (!responseMessageId) { + return getResumeBranchFallbackMessageId(resumeState); + } + + const unpaddedResponseMessageId = responseMessageId.replace(/_+$/, ''); + let hasResponseMessage = false; + let hasUnpaddedResponseMessage = false; + + for (const message of messages) { + if (message.messageId === responseMessageId) { + hasResponseMessage = true; + break; + } + + if (message.messageId === unpaddedResponseMessageId) { + hasUnpaddedResponseMessage = true; + } + } + + if (hasResponseMessage) { + return responseMessageId; + } + + if (hasUnpaddedResponseMessage) { + return unpaddedResponseMessageId; + } + + return getResumeBranchFallbackMessageId(resumeState); +} diff --git a/packages/chat/src/core/resumable/resume.ts b/packages/chat/src/core/resumable/resume.ts index 9d058374d6..af82855b7a 100644 --- a/packages/chat/src/core/resumable/resume.ts +++ b/packages/chat/src/core/resumable/resume.ts @@ -117,231 +117,6 @@ export const buildResumeEventSubmission = ( } as EventSubmission; }; -export type ResumeMessageIndexes = { - userIndex: number; - responseIndex: number; - preliminaryUserIndex: number; - preliminaryResponseIndex: number; -}; - -export const getResumeMessageIndexes = ( - messages: TMessage[], - userMessageId: string, - responseMessageId: string, - preliminaryUserMessageId?: string, - preliminaryResponseMessageId?: string, -): ResumeMessageIndexes => { - let userIndex = -1; - let responseIndex = -1; - let preliminaryUserIndex = -1; - let preliminaryResponseIndex = -1; - const hasPreliminaryResponse = preliminaryResponseMessageId?.endsWith('_') === true; - const eligiblePreliminaryUserId = - hasPreliminaryResponse && preliminaryUserMessageId && preliminaryUserMessageId !== userMessageId - ? preliminaryUserMessageId - : undefined; - const eligiblePreliminaryResponseId = - hasPreliminaryResponse && preliminaryResponseMessageId !== responseMessageId - ? preliminaryResponseMessageId - : undefined; - - for (let index = 0; index < messages.length; index++) { - const messageId = messages[index]?.messageId; - if (userIndex < 0 && messageId === userMessageId) { - userIndex = index; - } - if (responseIndex < 0 && messageId === responseMessageId) { - responseIndex = index; - } - if ( - preliminaryUserIndex < 0 && - eligiblePreliminaryUserId && - messageId === eligiblePreliminaryUserId - ) { - preliminaryUserIndex = index; - } - if ( - preliminaryResponseIndex < 0 && - eligiblePreliminaryResponseId && - messageId === eligiblePreliminaryResponseId - ) { - preliminaryResponseIndex = index; - } - } - - return { userIndex, responseIndex, preliminaryUserIndex, preliminaryResponseIndex }; -}; - -export const mergeResumeMessages = ( - messages: TMessage[], - userMessage: TMessage, - responseMessage: TMessage, - indexes: ResumeMessageIndexes, - /** - * An anchored run (a compaction) owns no user row. Its user-message slot - * holds the leaf the summary hangs off, a row the transcript already has: - * merging the slot onto it would replace that answer with an empty user - * message, and inserting it would duplicate its id. Either way the row loses - * its parent and `buildTree` files it as a phantom root, folding the thread. - */ - anchoredUserMessage = false, -): TMessage[] => { - const nextMessages = [...messages]; - let { userIndex, responseIndex, preliminaryResponseIndex } = indexes; - const { preliminaryUserIndex } = indexes; - - if (preliminaryUserIndex >= 0 && !anchoredUserMessage) { - if (userIndex >= 0) { - nextMessages.splice(preliminaryUserIndex, 1); - if (userIndex > preliminaryUserIndex) { - userIndex -= 1; - } - if (responseIndex > preliminaryUserIndex) { - responseIndex -= 1; - } - if (preliminaryResponseIndex > preliminaryUserIndex) { - preliminaryResponseIndex -= 1; - } - } else { - nextMessages[preliminaryUserIndex] = { - ...nextMessages[preliminaryUserIndex], - ...userMessage, - }; - userIndex = preliminaryUserIndex; - } - } - - if (preliminaryResponseIndex >= 0) { - if (responseIndex >= 0) { - nextMessages.splice(preliminaryResponseIndex, 1); - if (userIndex > preliminaryResponseIndex) { - userIndex -= 1; - } - if (responseIndex > preliminaryResponseIndex) { - responseIndex -= 1; - } - } else { - nextMessages[preliminaryResponseIndex] = { - ...nextMessages[preliminaryResponseIndex], - ...responseMessage, - }; - responseIndex = preliminaryResponseIndex; - } - } - - if (userIndex >= 0 && !anchoredUserMessage) { - nextMessages[userIndex] = { ...nextMessages[userIndex], ...userMessage }; - } - - if (responseIndex >= 0) { - nextMessages[responseIndex] = { ...nextMessages[responseIndex], ...responseMessage }; - } - - if (anchoredUserMessage) { - return responseIndex >= 0 ? nextMessages : [...nextMessages, responseMessage]; - } - - if (userIndex >= 0 && responseIndex >= 0) { - return nextMessages; - } - - if (userIndex >= 0) { - return [...nextMessages, responseMessage]; - } - - if (responseIndex >= 0) { - nextMessages.splice(responseIndex, 0, userMessage); - return nextMessages; - } - - return [...nextMessages, userMessage, responseMessage]; -}; - -export function hasSubmissionUserMessage( - submission: TSubmission | null, - messages: TMessage[] | undefined, - conversationId: string | undefined, -): boolean { - const userMessageId = submission?.userMessage?.messageId; - if (!userMessageId || !conversationId || !messages?.length) { - return false; - } - - return messages.some( - (message) => - message.isCreatedByUser === true && - message.messageId === userMessageId && - message.conversationId === conversationId, - ); -} - -export function resumeStateMatchesSubmission( - streamStatus: { resumeState?: Agents.ResumeState } | undefined, - submission: TSubmission | null, -): boolean { - const resumeState = streamStatus?.resumeState; - if (!resumeState || !submission) { - return false; - } - - const userMessageId = submission.userMessage?.messageId; - if (userMessageId && resumeState.userMessage?.messageId === userMessageId) { - return true; - } - - const responseMessageId = submission.initialResponse?.messageId; - return !!responseMessageId && resumeState.responseMessageId === responseMessageId; -} - -/** - * The row that names the branch a resumed run belongs to when its own response - * is not in the loaded history yet. A compaction's user-message slot is the leaf - * it summarizes up to and carries no parent (`projectCompactionAnchor`), so - * there the anchor itself names the branch; without this the pane restores no - * sibling selection and a compaction started on an older branch comes back on - * whichever branch the default lands on. - */ -function getResumeBranchFallbackMessageId( - resumeState: Agents.ResumeState, -): string | null | undefined { - return resumeState.userMessage?.parentMessageId ?? resumeState.userMessage?.messageId; -} - -export function getResumeBranchTargetMessageId( - resumeState: Agents.ResumeState, - messages: TMessage[], -): string | null | undefined { - const responseMessageId = resumeState.responseMessageId; - if (!responseMessageId) { - return getResumeBranchFallbackMessageId(resumeState); - } - - const unpaddedResponseMessageId = responseMessageId.replace(/_+$/, ''); - let hasResponseMessage = false; - let hasUnpaddedResponseMessage = false; - - for (const message of messages) { - if (message.messageId === responseMessageId) { - hasResponseMessage = true; - break; - } - - if (message.messageId === unpaddedResponseMessageId) { - hasUnpaddedResponseMessage = true; - } - } - - if (hasResponseMessage) { - return responseMessageId; - } - - if (hasUnpaddedResponseMessage) { - return unpaddedResponseMessageId; - } - - return getResumeBranchFallbackMessageId(resumeState); -} - /** * Build a submission object from resume state for reconnected streams. * This provides the minimum data needed for useResumableSSE to subscribe. diff --git a/packages/chat/src/core/resumable/sync.ts b/packages/chat/src/core/resumable/sync.ts index 2cfa28913e..d242a0eb0d 100644 --- a/packages/chat/src/core/resumable/sync.ts +++ b/packages/chat/src/core/resumable/sync.ts @@ -20,8 +20,9 @@ import type { } from 'librechat-data-provider'; import type { StreamContext } from './terminal'; import type { StepEvent } from '../reducer'; -import { getResumeMessageIndexes, buildResumeEventSubmission, mergeResumeMessages } from './resume'; +import { getResumeMessageIndexes, mergeResumeMessages } from './placement'; import { getStepEventId, isOAuthStepEvent } from './events'; +import { buildResumeEventSubmission } from './resume'; import { preferDefinedString } from './messages'; /**