diff --git a/client/src/hooks/Agents/codeDecision.ts b/client/src/hooks/Agents/codeDecision.ts index 1b544093d6..a1fb7aef85 100644 --- a/client/src/hooks/Agents/codeDecision.ts +++ b/client/src/hooks/Agents/codeDecision.ts @@ -1,66 +1,11 @@ -import { Constants, canonicalizeCodeWorkspaceSelections } from 'librechat-data-provider'; +import { Constants } from 'librechat-data-provider'; import type { CodeApprovalMode, - CodeEnvironmentMode, EventSubmission, - CodeWorkspaceSelection, - TConversation, + CodeEnvironmentMode, } from 'librechat-data-provider'; -function sameSelections( - left?: CodeWorkspaceSelection[], - right?: CodeWorkspaceSelection[], -): boolean { - if (!left?.length || !right?.length) return !left?.length && !right?.length; - if (left.length !== right.length) return false; - return ( - JSON.stringify(canonicalizeCodeWorkspaceSelections(left)) === - JSON.stringify(canonicalizeCodeWorkspaceSelections(right)) - ); -} - -/** - * Folds the decision a submitted run establishes into the conversation that run belongs to. The - * server seals this pair for the rest of the chat, so the conversation has to carry it from the - * moment it is sent: a chat that becomes saved mid-run would otherwise hold no decision while the - * stored one is already sealed, and the composer would re-derive an agent default or a remembered - * preference that the sealed decision refuses — reporting "choose a workspace", with Send - * disabled, for a workspace the run is already using. Returns the same conversation when it - * already holds the decision, so an unchanged send never re-renders the composer. - */ -export function withSubmittedCodeDecision( - conversation: TConversation | null, - submitted: { - codeEnvironmentMode?: CodeEnvironmentMode; - codeWorkspaces?: CodeWorkspaceSelection[]; - }, -): TConversation | null { - const { codeWorkspaces } = submitted; - // Older replicas accept selections without a mode. Preserve that submitted selection locally - // too, or the first saved-chat event drops an implicit default back to "Choose workspace". - const codeEnvironmentMode = - submitted.codeEnvironmentMode ?? ((codeWorkspaces?.length ?? 0) > 0 ? 'attached' : undefined); - if (conversation == null || codeEnvironmentMode == null) return conversation; - if ( - conversation.codeEnvironmentMode === codeEnvironmentMode && - sameSelections(conversation.codeWorkspaces, codeWorkspaces) - ) { - return conversation; - } - return { ...conversation, codeEnvironmentMode, codeWorkspaces }; -} - -/** Compare the pair, including absence: a failed first send may leave no persisted decision. */ -export function hasSameCodeDecision( - left: Pick, - right: Pick, -): boolean { - return ( - (left.codeEnvironmentMode ?? (left.codeWorkspaces?.length ? 'attached' : undefined)) === - (right.codeEnvironmentMode ?? (right.codeWorkspaces?.length ? 'attached' : undefined)) && - sameSelections(left.codeWorkspaces, right.codeWorkspaces) - ); -} +export { withSubmittedCodeDecision, hasSameCodeDecision } from '@librechat/chat'; /** The submission snapshot may still be `new` after creation/streaming assigned a durable id. */ export function getFailedCodeDecisionRequest(submission: EventSubmission, resolvedId?: string) { diff --git a/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts b/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts index 14507b4655..ddc2caeb0c 100644 --- a/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts +++ b/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts @@ -1,4 +1,5 @@ import { Constants, ContentTypes } from 'librechat-data-provider'; +import { mergeErrorMessages, buildRecoveryPreset } from '@librechat/chat'; import type { TMessageContentParts, EventSubmission, @@ -6,363 +7,11 @@ import type { TMessage, } from 'librechat-data-provider'; import type { TResData } from '~/common'; -import { - buildCreatedInitialResponse, - getExistingConversationAbortMessages, - isInitialNewConversationSubmission, - keepLocalCodeApprovalMode, - buildRecoveryPreset, - mergeErrorMessages, - mergeRegenerateFinalMessages, - resolveErrorTurn, - startedAsNewConversation, -} from '~/hooks/SSE/useEventHandlers'; import { getFailedCodeDecisionRequest } from '~/hooks/Agents/codeDecision'; import { stripStreamedIndexStamps, getPartKeyIndex } from '~/utils'; +import { resolveErrorTurn } from '~/hooks/SSE/useEventHandlers'; import buildDefaultConvo from '~/utils/buildDefaultConvo'; -describe('buildCreatedInitialResponse', () => { - const userMessage = { - messageId: 'server-user-message', - conversationId: 'conversation-1', - isCreatedByUser: true, - text: 'Hello', - sender: 'User', - } as TMessage; - - const initialResponse = { - messageId: 'prelim-response', - parentMessageId: 'original-user-message', - conversationId: 'conversation-1', - isCreatedByUser: false, - text: '', - sender: 'Assistant', - } as TMessage; - - it('uses the created user message id for new turns', () => { - expect( - buildCreatedInitialResponse({ - initialResponse, - userMessage, - isRegenerate: false, - }), - ).toEqual( - expect.objectContaining({ - messageId: 'server-user-message_', - parentMessageId: 'server-user-message', - }), - ); - }); - - it('preserves the regenerated prelim response id and parent', () => { - expect( - buildCreatedInitialResponse({ - initialResponse, - userMessage, - isRegenerate: true, - }), - ).toEqual( - expect.objectContaining({ - messageId: 'prelim-response', - parentMessageId: 'original-user-message', - }), - ); - }); -}); - -describe('isInitialNewConversationSubmission', () => { - it('treats a root user message as an optimistic new chat', () => { - expect( - isInitialNewConversationSubmission({ - userMessage: { - messageId: 'user-1', - parentMessageId: Constants.NO_PARENT, - } as TMessage, - } as EventSubmission), - ).toBe(true); - }); - - it('does not treat follow-up turns as optimistic new chats', () => { - expect( - isInitialNewConversationSubmission({ - userMessage: { - messageId: 'user-2', - parentMessageId: 'assistant-1', - } as TMessage, - } as EventSubmission), - ).toBe(false); - }); -}); - -describe('startedAsNewConversation', () => { - const rootUserMessage = { - messageId: 'user-1', - parentMessageId: Constants.NO_PARENT, - } as TMessage; - - it('treats an unsaved conversation as a new chat', () => { - for (const conversationId of [undefined, Constants.NEW_CONVO, Constants.PENDING_CONVO]) { - expect( - startedAsNewConversation({ - conversation: { conversationId }, - userMessage: rootUserMessage, - } as EventSubmission), - ).toBe(true); - } - }); - - it('treats a first turn without a saved id as a new chat', () => { - expect( - startedAsNewConversation({ - conversation: {}, - userMessage: rootUserMessage, - } as EventSubmission), - ).toBe(true); - }); - - it('does not treat a regenerated first reply of a saved conversation as a new chat', () => { - expect( - startedAsNewConversation({ - conversation: { conversationId: 'conversation-1' }, - userMessage: rootUserMessage, - isRegenerate: true, - } as EventSubmission), - ).toBe(false); - }); - - it('does not treat a resubmitted first message of a saved conversation as a new chat', () => { - expect( - startedAsNewConversation({ - conversation: { conversationId: 'conversation-1' }, - userMessage: rootUserMessage, - isEdited: true, - } as EventSubmission), - ).toBe(false); - }); - - it('does not treat a follow-up turn of a saved conversation as a new chat', () => { - expect( - startedAsNewConversation({ - conversation: { conversationId: 'conversation-1' }, - userMessage: { messageId: 'user-2', parentMessageId: 'assistant-1' } as TMessage, - } as EventSubmission), - ).toBe(false); - }); -}); - -describe('mergeRegenerateFinalMessages', () => { - const userMessage = (messageId: string, parentMessageId: string = Constants.NO_PARENT) => - ({ - messageId, - parentMessageId, - conversationId: 'conversation-1', - isCreatedByUser: true, - sender: 'User', - text: messageId, - }) as TMessage; - - const assistantMessage = (messageId: string, parentMessageId: string) => - ({ - messageId, - parentMessageId, - conversationId: 'conversation-1', - isCreatedByUser: false, - sender: 'Assistant', - text: messageId, - }) as TMessage; - - it('keeps the original branch siblings when a non-tail regenerate finalizes', () => { - const rootUser = userMessage('user-1'); - const originalResponse = assistantMessage('assistant-1', rootUser.messageId); - const followUpUser = userMessage('user-2', originalResponse.messageId); - const followUpResponse = assistantMessage('assistant-2', followUpUser.messageId); - const finalResponse = assistantMessage('assistant-3', rootUser.messageId); - - expect( - mergeRegenerateFinalMessages({ - messages: [rootUser, originalResponse, followUpUser, followUpResponse], - responseMessage: finalResponse, - initialResponseId: 'assistant-1_', - }).map((message) => message.messageId), - ).toEqual([ - rootUser.messageId, - originalResponse.messageId, - followUpUser.messageId, - followUpResponse.messageId, - finalResponse.messageId, - ]); - }); - - it('replaces the streamed preliminary response when it is present', () => { - const rootUser = userMessage('user-1'); - const preliminaryResponse = assistantMessage('assistant-1_', rootUser.messageId); - const finalResponse = assistantMessage('assistant-3', rootUser.messageId); - - expect( - mergeRegenerateFinalMessages({ - messages: [rootUser, preliminaryResponse], - responseMessage: finalResponse, - initialResponseId: preliminaryResponse.messageId, - }).map((message) => message.messageId), - ).toEqual([rootUser.messageId, finalResponse.messageId]); - }); -}); - -describe('getExistingConversationAbortMessages', () => { - const message = (messageId: string) => - ({ - messageId, - conversationId: 'conversation-1', - text: messageId, - }) as TMessage; - - it('restores the full pre-regenerate branch on early abort', () => { - const originalMessages = [message('user-1'), message('assistant-1'), message('user-2')]; - const scopedRegenerateMessages = [message('user-1')]; - const currentStreamMessages = [message('user-1'), message('assistant-1_')]; - - expect( - getExistingConversationAbortMessages({ - messages: scopedRegenerateMessages, - currentMessages: currentStreamMessages, - regenerateMessages: originalMessages, - isRegenerate: true, - }).map(({ messageId }) => messageId), - ).toEqual(['user-1', 'assistant-1', 'user-2']); - }); - - it('keeps the existing non-regenerate abort rollback behavior', () => { - const submissionMessages = [message('user-1')]; - const currentMessages = [message('user-1'), message('assistant-1')]; - - expect( - getExistingConversationAbortMessages({ - messages: submissionMessages, - currentMessages, - }).map(({ messageId }) => messageId), - ).toEqual(['user-1']); - }); -}); - -describe('mergeErrorMessages', () => { - const message = (messageId: string, isCreatedByUser = false) => - ({ - messageId, - conversationId: 'conversation-1', - isCreatedByUser, - text: messageId, - }) as TMessage; - - it('adds the request and error for a normal submission', () => { - const userMessage = message('user-1', true); - const errorMessage = message('assistant-error'); - - expect( - mergeErrorMessages({ - messages: [message('previous-response')], - userMessage, - errorMessage, - }).map(({ messageId }) => messageId), - ).toEqual(['previous-response', 'user-1', 'assistant-error']); - }); - - it('preserves regeneration history without duplicating its user', () => { - const userMessage = message('user-1', true); - const originalResponse = message('assistant-1'); - const laterUser = message('user-2', true); - const laterResponse = message('assistant-2'); - const errorMessage = message('assistant-1_'); - - expect( - mergeErrorMessages({ - messages: [userMessage], - regenerateMessages: [userMessage, originalResponse, laterUser, laterResponse], - userMessage, - errorMessage, - isRegenerate: true, - }).map(({ messageId }) => messageId), - ).toEqual(['user-1', 'assistant-1', 'user-2', 'assistant-2', 'assistant-1_']); - }); - - it('replaces an edited response error that intentionally reuses its id', () => { - const userMessage = message('user-1', true); - const originalResponse = message('assistant-1'); - const errorMessage = { ...originalResponse, text: 'Regeneration failed', error: true }; - - const merged = mergeErrorMessages({ - messages: [userMessage], - regenerateMessages: [userMessage, originalResponse], - userMessage, - errorMessage, - isRegenerate: true, - }); - - expect(merged.map(({ messageId }) => messageId)).toEqual(['user-1', 'assistant-1']); - expect(merged[1]).toEqual(errorMessage); - }); -}); - -describe('keepLocalCodeApprovalMode', () => { - const server = { conversationId: 'conversation-1', codeApprovalMode: 'ask' } as TConversation; - const local = { - conversationId: 'conversation-1', - codeApprovalMode: 'acceptEdits', - } as TConversation; - - it('keeps a locally picked mode over the server copy', () => { - expect(keepLocalCodeApprovalMode(server, local, 'conversation-1').codeApprovalMode).toBe( - 'acceptEdits', - ); - }); - - it('lets the server copy win when nothing was picked locally', () => { - const unset = { conversationId: 'conversation-1' } as TConversation; - expect(keepLocalCodeApprovalMode(server, unset, 'conversation-1')).toBe(server); - expect(keepLocalCodeApprovalMode(server, null, 'conversation-1')).toBe(server); - }); - - it('ignores the mode of a conversation the user navigated to mid-run', () => { - const elsewhere = { conversationId: 'conversation-2', codeApprovalMode: 'fullAccess' }; - expect(keepLocalCodeApprovalMode(server, elsewhere as TConversation, 'conversation-1')).toBe( - server, - ); - }); - - it('follows a new chat to the id the server assigned', () => { - const assigned = { conversationId: 'server-id', codeApprovalMode: 'acceptEdits' }; - expect( - keepLocalCodeApprovalMode( - { conversationId: 'server-id' } as TConversation, - assigned as TConversation, - 'server-id', - ).codeApprovalMode, - ).toBe('acceptEdits'); - expect(keepLocalCodeApprovalMode(server, assigned as TConversation, Constants.NEW_CONVO)).toBe( - server, - ); - }); -}); - -describe('buildRecoveryPreset', () => { - const sent = { - conversationId: 'conversation-1', - endpoint: 'agents', - agent_id: 'agent-1', - codeApprovalMode: 'ask', - } as TConversation; - - it('carries the mode the detail cache holds for the rebuilt conversation', () => { - const cached = { ...sent, codeApprovalMode: 'acceptEdits' } as TConversation; - const preset = buildRecoveryPreset(sent, cached, 'conversation-1'); - expect(preset.codeApprovalMode).toBe('acceptEdits'); - expect(preset.agent_id).toBe('agent-1'); - }); - - it('falls back to the sent mode when no record exists', () => { - expect(buildRecoveryPreset(sent, undefined, '_fresh').codeApprovalMode).toBe('ask'); - }); -}); - describe('resolveErrorTurn', () => { const userMessage = { messageId: 'user-1', diff --git a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts index 9ae21fbd3d..8f8213eb9c 100644 --- a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts +++ b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts @@ -263,12 +263,6 @@ jest.mock('~/hooks/SSE/useEventHandlers', () => { return { __esModule: true, ...actual, - // Read at call time: the hooks barrel imports useSSE during this factory. - startedAsNewConversation: (submission: TSubmission) => - jest.requireActual('~/hooks/SSE/useEventHandlers').startedAsNewConversation(submission), - buildCreatedInitialResponse: ( - submission: Pick, - ) => jest.requireActual('~/hooks/SSE/useEventHandlers').buildCreatedInitialResponse(submission), default: jest.fn(() => ({ errorHandler: mockErrorHandler, finalHandler: mockFinalHandler, diff --git a/client/src/hooks/SSE/useEventHandlers.ts b/client/src/hooks/SSE/useEventHandlers.ts index 613153fdf6..4565478cc8 100644 --- a/client/src/hooks/SSE/useEventHandlers.ts +++ b/client/src/hooks/SSE/useEventHandlers.ts @@ -8,13 +8,21 @@ import { QueryKeys, Constants, ContentTypes, - tPresetSchema, tMessageSchema, tConvoUpdateSchema, isAssistantsEndpoint, } from 'librechat-data-provider'; +import { + mergeErrorMessages, + buildRecoveryPreset, + responseParentMessageId, + keepLocalCodeApprovalMode, + buildCreatedInitialResponse, + mergeRegenerateFinalMessages, + isInitialNewConversationSubmission, + getExistingConversationAbortMessages, +} from '@librechat/chat'; import type { - TPreset, TMessage, TConversation, TStartupConfig, @@ -57,11 +65,8 @@ import { pendingQuotesByConvoId, pendingReasoningOverrideFamily, } from '~/components/Chat/Input/Composer/state'; -import { - getFailedCodeDecisionRequest, - withSubmittedCodeDecision, -} from '~/hooks/Agents/codeDecision'; import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse'; +import { getFailedCodeDecisionRequest } from '~/hooks/Agents/codeDecision'; import { shouldResetSubagentAtomsOnConversationChange } from './cleanup'; import useAttachmentHandler from '~/hooks/SSE/useAttachmentHandler'; import { useChatTransport } from '~/Providers/ChatTransportContext'; @@ -101,30 +106,6 @@ const SKILL_QUERY_KEYS = [ QueryKeys.skillNodeContent, ] as const; -/** A regenerated response is parented to the saved turn, never its unsaved replay copy. */ -const responseParentMessageId = ( - initialResponse: TMessage, - userMessage: TMessage, - isRegenerate?: boolean, -) => - isRegenerate && initialResponse.parentMessageId - ? initialResponse.parentMessageId - : userMessage.messageId; - -export const buildCreatedInitialResponse = ({ - initialResponse, - userMessage, - isRegenerate = false, -}: Pick): TMessage => ({ - ...initialResponse, - parentMessageId: responseParentMessageId(initialResponse, userMessage, isRegenerate), - messageId: - isRegenerate && initialResponse.messageId - ? initialResponse.messageId - : `${userMessage.messageId}_`, - conversationId: userMessage.conversationId ?? initialResponse.conversationId, -}); - /** Apply the resolved request choice only after acknowledgement. A saved decision may have * moved since this event was emitted, so replay must never replace it. */ function acknowledgedCodeEnvironment( @@ -144,123 +125,6 @@ function acknowledgedCodeEnvironment( }; } -export const isInitialNewConversationSubmission = ({ - userMessage, -}: Pick): boolean => - userMessage?.parentMessageId === Constants.NO_PARENT; - -/** - * Whether the run was sent from the unsaved-chat composer, which is the only case where - * finishing it may drop the pane's new-chat draft. Regenerating or resubmitting the first turn - * of a saved conversation keeps the root parent id, so the root parent alone cannot decide it. - */ -export const startedAsNewConversation = ({ - conversation, - userMessage, - isEdited, - isRegenerate, -}: Pick< - EventSubmission, - 'conversation' | 'userMessage' | 'isEdited' | 'isRegenerate' ->): boolean => { - const conversationId = conversation?.conversationId; - if ( - !conversationId || - conversationId === Constants.NEW_CONVO || - conversationId === Constants.PENDING_CONVO - ) { - return true; - } - - return ( - isEdited !== true && - isRegenerate !== true && - isInitialNewConversationSubmission({ userMessage }) - ); -}; - -export const mergeRegenerateFinalMessages = ({ - messages, - responseMessage, - initialResponseId, -}: { - messages: TMessage[]; - responseMessage: TMessage; - initialResponseId?: string | null; -}): TMessage[] => { - const finalMessages: TMessage[] = []; - let inserted = false; - - for (const message of messages) { - if (!message?.messageId || message.messageId === initialResponseId) { - continue; - } - - if (message.messageId === responseMessage.messageId) { - finalMessages.push(responseMessage); - inserted = true; - continue; - } - - finalMessages.push(message); - } - - if (!inserted) { - finalMessages.push(responseMessage); - } - - return finalMessages; -}; - -export const getExistingConversationAbortMessages = ({ - messages, - currentMessages, - regenerateMessages, - isRegenerate = false, -}: Pick & { - currentMessages?: TMessage[]; -}): TMessage[] => { - if (!isRegenerate) { - return [...messages]; - } - - if (regenerateMessages?.length) { - return [...regenerateMessages]; - } - - const sourceMessages = currentMessages?.length ? currentMessages : messages; - return [...sourceMessages]; -}; - -export const mergeErrorMessages = ({ - messages, - regenerateMessages, - userMessage, - errorMessage, - isRegenerate = false, -}: Pick & { - errorMessage: TMessage; -}): TMessage[] => { - if (isRegenerate) { - const finalMessages: TMessage[] = []; - let replaced = false; - for (const message of regenerateMessages ?? messages) { - if (message.messageId === errorMessage.messageId) { - finalMessages.push(errorMessage); - replaced = true; - } else { - finalMessages.push(message); - } - } - if (!replaced) { - finalMessages.push(errorMessage); - } - return finalMessages; - } - - return [...messages, userMessage, errorMessage]; -}; - export type EventHandlerParams = { isAddedRequest?: boolean; runIndex?: number; @@ -493,47 +357,6 @@ export const resolveErrorTurn = ({ }; }; -/** - * A code approval mode picked locally is newer than any server copy of the - * conversation: the final and abort events return the mode the run STARTED - * with, and error recovery rebuilds from the pre-run capture. The local mode - * therefore survives every merge and the next send carries it. Identity is - * checked because the conversation atom is index-global: after navigating - * elsewhere mid-run, that conversation's mode must not land in this one. - */ -export const keepLocalCodeApprovalMode = >( - merged: T, - local: Partial | null | undefined, - conversationId: string | null | undefined, -): T => { - const localMode = local?.codeApprovalMode; - if (localMode == null || local?.conversationId !== conversationId) { - return merged; - } - return merged.codeApprovalMode === localMode - ? merged - : { ...merged, codeApprovalMode: localMode }; -}; - -/** Preset for a conversation rebuilt after a failed or aborted run. The detail - * cache holds a mode picked while that run streamed; the capture only the - * pre-run one. */ -export const buildRecoveryPreset = ( - submissionConvo: Partial, - cachedConvo: TConversation | null | undefined, - conversationId: string, - submittedDecision?: Pick, -): TPreset => - tPresetSchema.parse( - keepLocalCodeApprovalMode( - submittedDecision == null - ? submissionConvo - : withSubmittedCodeDecision(submissionConvo as TConversation, submittedDecision)!, - cachedConvo, - conversationId, - ), - ); - export const getConvoTitle = ({ queryClient, currentTitle, diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.ts index 2f20fe0fe2..2c3eaefb81 100644 --- a/client/src/hooks/SSE/useResumableSSE.ts +++ b/client/src/hooks/SSE/useResumableSSE.ts @@ -49,6 +49,14 @@ import { GENERATION_PROTOCOL_VERSION, getGenerationProtocolVersion, supportsGenerationProtocolV2, + isInitialNewConversation, + completedResponseMessageId, + preferDefinedString, + getOptimisticMessages, + buildOptimisticConversation, + hydrateSubmissionMessages, + buildCreatedInitialResponse, + keepLocalCodeApprovalMode, } from '@librechat/chat'; import type { Agents, @@ -122,17 +130,13 @@ import { canRestoreRecovery, blockRecovery, } from '~/components/Chat/Steering/recovery'; -import useEventHandlers, { - buildCreatedInitialResponse, - keepLocalCodeApprovalMode, -} from './useEventHandlers'; import { pendingApprovalActionFamily } from '~/components/Chat/approval/state'; import { abortScrollFamily, showStopButtonByIndex } from '~/store/generation'; -import { withSubmittedCodeDecision } from '~/hooks/Agents/codeDecision'; import { useChatTransport } from '~/Providers/ChatTransportContext'; import { reopenTermsOnRefusal } from '~/utils/terms'; import { useAuthContext } from '~/hooks/AuthContext'; import { fetchConvoSnapshot } from '~/utils/convos'; +import useEventHandlers from './useEventHandlers'; import { useFileMapContext } from '~/Providers'; import useUsageHandler from './useUsageHandler'; import useLocalize from '~/hooks/useLocalize'; @@ -170,16 +174,6 @@ const getStreamStartFailureData = (errorData?: unknown, termsRefusalText?: strin metadata: markStreamStartFailedMetadata(), }) as unknown as TResData; -const hasConcreteConversationId = (conversationId?: string | null) => - !!conversationId && - conversationId !== Constants.NEW_CONVO && - conversationId !== Constants.PENDING_CONVO; - -const isInitialNewConversation = (submission: TSubmission) => { - const conversationId = submission.conversation?.conversationId; - return !hasConcreteConversationId(conversationId); -}; - const replaceNewConversationUrl = (conversationId: string) => { if (window.location.pathname !== `/c/${Constants.NEW_CONVO}`) { return; @@ -192,104 +186,6 @@ const replaceNewConversationUrl = (conversationId: string) => { ); }; -const shouldHydrateMessage = (message: TMessage) => - !hasConcreteConversationId(message.conversationId); - -/** Recovery must identify the response itself: regenerations share a user - * parent, and array order says nothing about which sibling just completed. - * A fresh-turn placeholder has no server response ID yet; only an unambiguous - * persisted child can stand in for it. */ -const completedResponseMessageId = ( - messages: TMessage[] | undefined, - userMessageId: string | undefined, - responseMessageId: string | undefined, -): string | undefined => { - if (messages == null || userMessageId == null) { - return undefined; - } - const target = responseMessageId?.replace(/_+$/, ''); - const hasResponseIdentity = target != null && target !== userMessageId; - let candidate: string | undefined; - let ambiguous = false; - for (const message of messages) { - if (message.isCreatedByUser !== false || message.parentMessageId !== userMessageId) { - continue; - } - if (hasResponseIdentity) { - if (message.messageId === target) { - return message.messageId; - } - continue; - } - ambiguous ||= candidate != null; - candidate = message.messageId; - } - return hasResponseIdentity || ambiguous ? undefined : candidate; -}; - -const hydrateMessageConversationId = (message: TMessage, conversationId: string): TMessage => - shouldHydrateMessage(message) ? { ...message, conversationId } : message; - -const preferDefinedString = (value?: string | null, fallback?: string): string | undefined => - value != null && value !== '' ? value : fallback; - -const getOptimisticMessages = ( - submission: TSubmission, - conversationId: string, - messages?: TMessage[], -): TMessage[] => { - const sourceMessages = - messages && messages.length > 0 - ? messages - : [submission.userMessage, submission.initialResponse].filter( - (message): message is TMessage => message != null, - ); - - return sourceMessages.map((message) => hydrateMessageConversationId(message, conversationId)); -}; - -const buildOptimisticConversation = ( - submission: TSubmission, - conversationId: string, -): TConversation => { - const now = new Date().toISOString(); - const messageIds = [ - submission.userMessage?.messageId, - submission.initialResponse?.messageId, - ].filter((messageId): messageId is string => typeof messageId === 'string' && messageId !== ''); - - const conversation = { - ...submission.conversation, - conversationId, - endpoint: submission.conversation.endpoint ?? null, - title: submission.conversation.title ?? 'New Chat', - messages: messageIds.length > 0 ? messageIds : submission.conversation.messages, - createdAt: submission.conversation.createdAt ?? now, - updatedAt: now, - /** Temporary mode lives on the submission, not on the draft conversation, so - * the optimistic record has to carry it forward or every consumer of this - * cache entry reads a new temporary chat as an ordinary one. Only stamped - * when true, leaving the legacy `expiredAt` inference untouched otherwise. */ - ...(submission.isTemporary === true ? { isTemporary: true } : {}), - } as TConversation; - return withSubmittedCodeDecision(conversation, submission)!; -}; - -const hydrateSubmissionMessages = ( - submission: TSubmission, - conversationId: string, -): TSubmission => ({ - ...submission, - conversation: { - ...submission.conversation, - conversationId, - }, - userMessage: hydrateMessageConversationId(submission.userMessage, conversationId), - initialResponse: submission.initialResponse - ? hydrateMessageConversationId(submission.initialResponse, conversationId) - : submission.initialResponse, -}); - const buildResumeEventSubmission = ( currentSubmission: TSubmission, currentUserMessage: TMessage, diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.ts index 2eff3360a9..c6022d90e5 100644 --- a/client/src/hooks/SSE/useSSE.ts +++ b/client/src/hooks/SSE/useSSE.ts @@ -3,6 +3,7 @@ import { v4 } from 'uuid'; import { useSetRecoilState } from 'recoil'; import { useStore, useSetAtom } from 'jotai'; import { useQueryClient } from '@tanstack/react-query'; +import { startedAsNewConversation, buildCreatedInitialResponse } from '@librechat/chat'; import { StepEvents, createPayload, removeNullishValues } from 'librechat-data-provider'; import type { Agents, @@ -18,7 +19,6 @@ import type { import type { EventHandlerParams } from './useEventHandlers'; import type { TResData, TFinalResData } from '~/common'; import { clearComposerDrafts, applyPendingAction, findPendingActionMessageIndex } from '~/utils'; -import { startedAsNewConversation, buildCreatedInitialResponse } from './useEventHandlers'; import { pendingApprovalActionFamily } from '~/components/Chat/approval/state'; import { abortScrollFamily, showStopButtonByIndex } from '~/store/generation'; import { useGetStartupConfig, useGetUserBalance } from '~/data-provider'; diff --git a/packages/chat/src/core/__tests__/messages.spec.ts b/packages/chat/src/core/__tests__/messages.spec.ts new file mode 100644 index 0000000000..dbee5f9d78 --- /dev/null +++ b/packages/chat/src/core/__tests__/messages.spec.ts @@ -0,0 +1,354 @@ +import { Constants } from 'librechat-data-provider'; +import type { EventSubmission, TConversation, TMessage } from 'librechat-data-provider'; +import { + mergeErrorMessages, + buildRecoveryPreset, + startedAsNewConversation, + keepLocalCodeApprovalMode, + buildCreatedInitialResponse, + mergeRegenerateFinalMessages, + isInitialNewConversationSubmission, + getExistingConversationAbortMessages, +} from '../resumable'; + +describe('buildCreatedInitialResponse', () => { + const userMessage = { + messageId: 'server-user-message', + conversationId: 'conversation-1', + isCreatedByUser: true, + text: 'Hello', + sender: 'User', + } as TMessage; + + const initialResponse = { + messageId: 'prelim-response', + parentMessageId: 'original-user-message', + conversationId: 'conversation-1', + isCreatedByUser: false, + text: '', + sender: 'Assistant', + } as TMessage; + + it('uses the created user message id for new turns', () => { + expect( + buildCreatedInitialResponse({ + initialResponse, + userMessage, + isRegenerate: false, + }), + ).toEqual( + expect.objectContaining({ + messageId: 'server-user-message_', + parentMessageId: 'server-user-message', + }), + ); + }); + + it('preserves the regenerated prelim response id and parent', () => { + expect( + buildCreatedInitialResponse({ + initialResponse, + userMessage, + isRegenerate: true, + }), + ).toEqual( + expect.objectContaining({ + messageId: 'prelim-response', + parentMessageId: 'original-user-message', + }), + ); + }); +}); + +describe('isInitialNewConversationSubmission', () => { + it('treats a root user message as an optimistic new chat', () => { + expect( + isInitialNewConversationSubmission({ + userMessage: { + messageId: 'user-1', + parentMessageId: Constants.NO_PARENT, + } as TMessage, + } as EventSubmission), + ).toBe(true); + }); + + it('does not treat follow-up turns as optimistic new chats', () => { + expect( + isInitialNewConversationSubmission({ + userMessage: { + messageId: 'user-2', + parentMessageId: 'assistant-1', + } as TMessage, + } as EventSubmission), + ).toBe(false); + }); +}); + +describe('startedAsNewConversation', () => { + const rootUserMessage = { + messageId: 'user-1', + parentMessageId: Constants.NO_PARENT, + } as TMessage; + + it('treats an unsaved conversation as a new chat', () => { + for (const conversationId of [undefined, Constants.NEW_CONVO, Constants.PENDING_CONVO]) { + expect( + startedAsNewConversation({ + conversation: { conversationId }, + userMessage: rootUserMessage, + } as EventSubmission), + ).toBe(true); + } + }); + + it('treats a first turn without a saved id as a new chat', () => { + expect( + startedAsNewConversation({ + conversation: {}, + userMessage: rootUserMessage, + } as EventSubmission), + ).toBe(true); + }); + + it('does not treat a regenerated first reply of a saved conversation as a new chat', () => { + expect( + startedAsNewConversation({ + conversation: { conversationId: 'conversation-1' }, + userMessage: rootUserMessage, + isRegenerate: true, + } as EventSubmission), + ).toBe(false); + }); + + it('does not treat a resubmitted first message of a saved conversation as a new chat', () => { + expect( + startedAsNewConversation({ + conversation: { conversationId: 'conversation-1' }, + userMessage: rootUserMessage, + isEdited: true, + } as EventSubmission), + ).toBe(false); + }); + + it('does not treat a follow-up turn of a saved conversation as a new chat', () => { + expect( + startedAsNewConversation({ + conversation: { conversationId: 'conversation-1' }, + userMessage: { messageId: 'user-2', parentMessageId: 'assistant-1' } as TMessage, + } as EventSubmission), + ).toBe(false); + }); +}); + +describe('mergeRegenerateFinalMessages', () => { + const userMessage = (messageId: string, parentMessageId: string = Constants.NO_PARENT) => + ({ + messageId, + parentMessageId, + conversationId: 'conversation-1', + isCreatedByUser: true, + sender: 'User', + text: messageId, + }) as TMessage; + + const assistantMessage = (messageId: string, parentMessageId: string) => + ({ + messageId, + parentMessageId, + conversationId: 'conversation-1', + isCreatedByUser: false, + sender: 'Assistant', + text: messageId, + }) as TMessage; + + it('keeps the original branch siblings when a non-tail regenerate finalizes', () => { + const rootUser = userMessage('user-1'); + const originalResponse = assistantMessage('assistant-1', rootUser.messageId); + const followUpUser = userMessage('user-2', originalResponse.messageId); + const followUpResponse = assistantMessage('assistant-2', followUpUser.messageId); + const finalResponse = assistantMessage('assistant-3', rootUser.messageId); + + expect( + mergeRegenerateFinalMessages({ + messages: [rootUser, originalResponse, followUpUser, followUpResponse], + responseMessage: finalResponse, + initialResponseId: 'assistant-1_', + }).map((message) => message.messageId), + ).toEqual([ + rootUser.messageId, + originalResponse.messageId, + followUpUser.messageId, + followUpResponse.messageId, + finalResponse.messageId, + ]); + }); + + it('replaces the streamed preliminary response when it is present', () => { + const rootUser = userMessage('user-1'); + const preliminaryResponse = assistantMessage('assistant-1_', rootUser.messageId); + const finalResponse = assistantMessage('assistant-3', rootUser.messageId); + + expect( + mergeRegenerateFinalMessages({ + messages: [rootUser, preliminaryResponse], + responseMessage: finalResponse, + initialResponseId: preliminaryResponse.messageId, + }).map((message) => message.messageId), + ).toEqual([rootUser.messageId, finalResponse.messageId]); + }); +}); + +describe('getExistingConversationAbortMessages', () => { + const message = (messageId: string) => + ({ + messageId, + conversationId: 'conversation-1', + text: messageId, + }) as TMessage; + + it('restores the full pre-regenerate branch on early abort', () => { + const originalMessages = [message('user-1'), message('assistant-1'), message('user-2')]; + const scopedRegenerateMessages = [message('user-1')]; + const currentStreamMessages = [message('user-1'), message('assistant-1_')]; + + expect( + getExistingConversationAbortMessages({ + messages: scopedRegenerateMessages, + currentMessages: currentStreamMessages, + regenerateMessages: originalMessages, + isRegenerate: true, + }).map(({ messageId }) => messageId), + ).toEqual(['user-1', 'assistant-1', 'user-2']); + }); + + it('keeps the existing non-regenerate abort rollback behavior', () => { + const submissionMessages = [message('user-1')]; + const currentMessages = [message('user-1'), message('assistant-1')]; + + expect( + getExistingConversationAbortMessages({ + messages: submissionMessages, + currentMessages, + }).map(({ messageId }) => messageId), + ).toEqual(['user-1']); + }); +}); + +describe('mergeErrorMessages', () => { + const message = (messageId: string, isCreatedByUser = false) => + ({ + messageId, + conversationId: 'conversation-1', + isCreatedByUser, + text: messageId, + }) as TMessage; + + it('adds the request and error for a normal submission', () => { + const userMessage = message('user-1', true); + const errorMessage = message('assistant-error'); + + expect( + mergeErrorMessages({ + messages: [message('previous-response')], + userMessage, + errorMessage, + }).map(({ messageId }) => messageId), + ).toEqual(['previous-response', 'user-1', 'assistant-error']); + }); + + it('preserves regeneration history without duplicating its user', () => { + const userMessage = message('user-1', true); + const originalResponse = message('assistant-1'); + const laterUser = message('user-2', true); + const laterResponse = message('assistant-2'); + const errorMessage = message('assistant-1_'); + + expect( + mergeErrorMessages({ + messages: [userMessage], + regenerateMessages: [userMessage, originalResponse, laterUser, laterResponse], + userMessage, + errorMessage, + isRegenerate: true, + }).map(({ messageId }) => messageId), + ).toEqual(['user-1', 'assistant-1', 'user-2', 'assistant-2', 'assistant-1_']); + }); + + it('replaces an edited response error that intentionally reuses its id', () => { + const userMessage = message('user-1', true); + const originalResponse = message('assistant-1'); + const errorMessage = { ...originalResponse, text: 'Regeneration failed', error: true }; + + const merged = mergeErrorMessages({ + messages: [userMessage], + regenerateMessages: [userMessage, originalResponse], + userMessage, + errorMessage, + isRegenerate: true, + }); + + expect(merged.map(({ messageId }) => messageId)).toEqual(['user-1', 'assistant-1']); + expect(merged[1]).toEqual(errorMessage); + }); +}); + +describe('keepLocalCodeApprovalMode', () => { + const server = { conversationId: 'conversation-1', codeApprovalMode: 'ask' } as TConversation; + const local = { + conversationId: 'conversation-1', + codeApprovalMode: 'acceptEdits', + } as TConversation; + + it('keeps a locally picked mode over the server copy', () => { + expect(keepLocalCodeApprovalMode(server, local, 'conversation-1').codeApprovalMode).toBe( + 'acceptEdits', + ); + }); + + it('lets the server copy win when nothing was picked locally', () => { + const unset = { conversationId: 'conversation-1' } as TConversation; + expect(keepLocalCodeApprovalMode(server, unset, 'conversation-1')).toBe(server); + expect(keepLocalCodeApprovalMode(server, null, 'conversation-1')).toBe(server); + }); + + it('ignores the mode of a conversation the user navigated to mid-run', () => { + const elsewhere = { conversationId: 'conversation-2', codeApprovalMode: 'fullAccess' }; + expect(keepLocalCodeApprovalMode(server, elsewhere as TConversation, 'conversation-1')).toBe( + server, + ); + }); + + it('follows a new chat to the id the server assigned', () => { + const assigned = { conversationId: 'server-id', codeApprovalMode: 'acceptEdits' }; + expect( + keepLocalCodeApprovalMode( + { conversationId: 'server-id' } as TConversation, + assigned as TConversation, + 'server-id', + ).codeApprovalMode, + ).toBe('acceptEdits'); + expect(keepLocalCodeApprovalMode(server, assigned as TConversation, Constants.NEW_CONVO)).toBe( + server, + ); + }); +}); + +describe('buildRecoveryPreset', () => { + const sent = { + conversationId: 'conversation-1', + endpoint: 'agents', + agent_id: 'agent-1', + codeApprovalMode: 'ask', + } as TConversation; + + it('carries the mode the detail cache holds for the rebuilt conversation', () => { + const cached = { ...sent, codeApprovalMode: 'acceptEdits' } as TConversation; + const preset = buildRecoveryPreset(sent, cached, 'conversation-1'); + expect(preset.codeApprovalMode).toBe('acceptEdits'); + expect(preset.agent_id).toBe('agent-1'); + }); + + it('falls back to the sent mode when no record exists', () => { + expect(buildRecoveryPreset(sent, undefined, '_fresh').codeApprovalMode).toBe('ask'); + }); +}); diff --git a/packages/chat/src/core/decision.ts b/packages/chat/src/core/decision.ts new file mode 100644 index 0000000000..49e8efc0cd --- /dev/null +++ b/packages/chat/src/core/decision.ts @@ -0,0 +1,61 @@ +import { canonicalizeCodeWorkspaceSelections } from 'librechat-data-provider'; +import type { + TConversation, + CodeEnvironmentMode, + CodeWorkspaceSelection, +} from 'librechat-data-provider'; + +function sameSelections( + left?: CodeWorkspaceSelection[], + right?: CodeWorkspaceSelection[], +): boolean { + if (!left?.length || !right?.length) return !left?.length && !right?.length; + if (left.length !== right.length) return false; + return ( + JSON.stringify(canonicalizeCodeWorkspaceSelections(left)) === + JSON.stringify(canonicalizeCodeWorkspaceSelections(right)) + ); +} + +/** + * Folds the decision a submitted run establishes into the conversation that run belongs to. The + * server seals this pair for the rest of the chat, so the conversation has to carry it from the + * moment it is sent: a chat that becomes saved mid-run would otherwise hold no decision while the + * stored one is already sealed, and the composer would re-derive an agent default or a remembered + * preference that the sealed decision refuses, reporting "choose a workspace", with Send + * disabled, for a workspace the run is already using. Returns the same conversation when it + * already holds the decision, so an unchanged send never re-renders the composer. + */ +export function withSubmittedCodeDecision( + conversation: TConversation | null, + submitted: { + codeEnvironmentMode?: CodeEnvironmentMode; + codeWorkspaces?: CodeWorkspaceSelection[]; + }, +): TConversation | null { + const { codeWorkspaces } = submitted; + // Older replicas accept selections without a mode. Preserve that submitted selection locally + // too, or the first saved-chat event drops an implicit default back to "Choose workspace". + const codeEnvironmentMode = + submitted.codeEnvironmentMode ?? ((codeWorkspaces?.length ?? 0) > 0 ? 'attached' : undefined); + if (conversation == null || codeEnvironmentMode == null) return conversation; + if ( + conversation.codeEnvironmentMode === codeEnvironmentMode && + sameSelections(conversation.codeWorkspaces, codeWorkspaces) + ) { + return conversation; + } + return { ...conversation, codeEnvironmentMode, codeWorkspaces }; +} + +/** Compare the pair, including absence: a failed first send may leave no persisted decision. */ +export function hasSameCodeDecision( + left: Pick, + right: Pick, +): boolean { + return ( + (left.codeEnvironmentMode ?? (left.codeWorkspaces?.length ? 'attached' : undefined)) === + (right.codeEnvironmentMode ?? (right.codeWorkspaces?.length ? 'attached' : undefined)) && + sameSelections(left.codeWorkspaces, right.codeWorkspaces) + ); +} diff --git a/packages/chat/src/core/index.ts b/packages/chat/src/core/index.ts index 542d3ab76a..8950a72d07 100644 --- a/packages/chat/src/core/index.ts +++ b/packages/chat/src/core/index.ts @@ -1,4 +1,5 @@ export * from './ask'; +export * from './decision'; export * from './resumable'; export * from './steps'; export * from './steer'; diff --git a/packages/chat/src/core/resumable/index.ts b/packages/chat/src/core/resumable/index.ts index 94f7190768..c05bda0329 100644 --- a/packages/chat/src/core/resumable/index.ts +++ b/packages/chat/src/core/resumable/index.ts @@ -1,3 +1,4 @@ export * from './start'; export * from './errors'; export * from './events'; +export * from './messages'; diff --git a/packages/chat/src/core/resumable/messages.ts b/packages/chat/src/core/resumable/messages.ts new file mode 100644 index 0000000000..162d301dd5 --- /dev/null +++ b/packages/chat/src/core/resumable/messages.ts @@ -0,0 +1,301 @@ +import { Constants, tPresetSchema } from 'librechat-data-provider'; +import type { + TPreset, + TMessage, + TSubmission, + TConversation, + EventSubmission, +} from 'librechat-data-provider'; +import { withSubmittedCodeDecision } from '../decision'; + +export const hasConcreteConversationId = (conversationId?: string | null): boolean => + !!conversationId && + conversationId !== Constants.NEW_CONVO && + conversationId !== Constants.PENDING_CONVO; + +export const isInitialNewConversation = (submission: TSubmission): boolean => { + const conversationId = submission.conversation?.conversationId; + return !hasConcreteConversationId(conversationId); +}; + +const shouldHydrateMessage = (message: TMessage): boolean => + !hasConcreteConversationId(message.conversationId); + +/** Recovery must identify the response itself: regenerations share a user + * parent, and array order says nothing about which sibling just completed. + * A fresh-turn placeholder has no server response ID yet; only an unambiguous + * persisted child can stand in for it. */ +export const completedResponseMessageId = ( + messages: TMessage[] | undefined, + userMessageId: string | undefined, + responseMessageId: string | undefined, +): string | undefined => { + if (messages == null || userMessageId == null) { + return undefined; + } + const target = responseMessageId?.replace(/_+$/, ''); + const hasResponseIdentity = target != null && target !== userMessageId; + let candidate: string | undefined; + let ambiguous = false; + for (const message of messages) { + if (message.isCreatedByUser !== false || message.parentMessageId !== userMessageId) { + continue; + } + if (hasResponseIdentity) { + if (message.messageId === target) { + return message.messageId; + } + continue; + } + ambiguous ||= candidate != null; + candidate = message.messageId; + } + return hasResponseIdentity || ambiguous ? undefined : candidate; +}; + +const hydrateMessageConversationId = (message: TMessage, conversationId: string): TMessage => + shouldHydrateMessage(message) ? { ...message, conversationId } : message; + +export const preferDefinedString = ( + value?: string | null, + fallback?: string, +): string | undefined => (value != null && value !== '' ? value : fallback); + +export const getOptimisticMessages = ( + submission: TSubmission, + conversationId: string, + messages?: TMessage[], +): TMessage[] => { + const sourceMessages = + messages && messages.length > 0 + ? messages + : [submission.userMessage, submission.initialResponse].filter( + (message): message is TMessage => message != null, + ); + + return sourceMessages.map((message) => hydrateMessageConversationId(message, conversationId)); +}; + +export const buildOptimisticConversation = ( + submission: TSubmission, + conversationId: string, +): TConversation => { + const now = new Date().toISOString(); + const messageIds = [ + submission.userMessage?.messageId, + submission.initialResponse?.messageId, + ].filter((messageId): messageId is string => typeof messageId === 'string' && messageId !== ''); + + const conversation = { + ...submission.conversation, + conversationId, + endpoint: submission.conversation.endpoint ?? null, + title: submission.conversation.title ?? 'New Chat', + messages: messageIds.length > 0 ? messageIds : submission.conversation.messages, + createdAt: submission.conversation.createdAt ?? now, + updatedAt: now, + /** Temporary mode lives on the submission, not on the draft conversation, so + * the optimistic record has to carry it forward or every consumer of this + * cache entry reads a new temporary chat as an ordinary one. Only stamped + * when true, leaving the legacy `expiredAt` inference untouched otherwise. */ + ...(submission.isTemporary === true ? { isTemporary: true } : {}), + } as TConversation; + return withSubmittedCodeDecision(conversation, submission)!; +}; + +export const hydrateSubmissionMessages = ( + submission: TSubmission, + conversationId: string, +): TSubmission => ({ + ...submission, + conversation: { + ...submission.conversation, + conversationId, + }, + userMessage: hydrateMessageConversationId(submission.userMessage, conversationId), + initialResponse: submission.initialResponse + ? hydrateMessageConversationId(submission.initialResponse, conversationId) + : submission.initialResponse, +}); + +/** A regenerated response is parented to the saved turn, never its unsaved replay copy. */ +export const responseParentMessageId = ( + initialResponse: TMessage, + userMessage: TMessage, + isRegenerate?: boolean, +): string => + isRegenerate && initialResponse.parentMessageId + ? initialResponse.parentMessageId + : userMessage.messageId; + +export const buildCreatedInitialResponse = ({ + initialResponse, + userMessage, + isRegenerate = false, +}: Pick): TMessage => ({ + ...initialResponse, + parentMessageId: responseParentMessageId(initialResponse, userMessage, isRegenerate), + messageId: + isRegenerate && initialResponse.messageId + ? initialResponse.messageId + : `${userMessage.messageId}_`, + conversationId: userMessage.conversationId ?? initialResponse.conversationId, +}); + +export const isInitialNewConversationSubmission = ({ + userMessage, +}: Pick): boolean => + userMessage?.parentMessageId === Constants.NO_PARENT; + +/** + * Whether the run was sent from the unsaved-chat composer, which is the only case where + * finishing it may drop the pane's new-chat draft. Regenerating or resubmitting the first turn + * of a saved conversation keeps the root parent id, so the root parent alone cannot decide it. + */ +export const startedAsNewConversation = ({ + conversation, + userMessage, + isEdited, + isRegenerate, +}: Pick< + EventSubmission, + 'conversation' | 'userMessage' | 'isEdited' | 'isRegenerate' +>): boolean => { + const conversationId = conversation?.conversationId; + if ( + !conversationId || + conversationId === Constants.NEW_CONVO || + conversationId === Constants.PENDING_CONVO + ) { + return true; + } + + return ( + isEdited !== true && + isRegenerate !== true && + isInitialNewConversationSubmission({ userMessage }) + ); +}; + +export const mergeRegenerateFinalMessages = ({ + messages, + responseMessage, + initialResponseId, +}: { + messages: TMessage[]; + responseMessage: TMessage; + initialResponseId?: string | null; +}): TMessage[] => { + const finalMessages: TMessage[] = []; + let inserted = false; + + for (const message of messages) { + if (!message?.messageId || message.messageId === initialResponseId) { + continue; + } + + if (message.messageId === responseMessage.messageId) { + finalMessages.push(responseMessage); + inserted = true; + continue; + } + + finalMessages.push(message); + } + + if (!inserted) { + finalMessages.push(responseMessage); + } + + return finalMessages; +}; + +export const getExistingConversationAbortMessages = ({ + messages, + currentMessages, + regenerateMessages, + isRegenerate = false, +}: Pick & { + currentMessages?: TMessage[]; +}): TMessage[] => { + if (!isRegenerate) { + return [...messages]; + } + + if (regenerateMessages?.length) { + return [...regenerateMessages]; + } + + const sourceMessages = currentMessages?.length ? currentMessages : messages; + return [...sourceMessages]; +}; + +export const mergeErrorMessages = ({ + messages, + regenerateMessages, + userMessage, + errorMessage, + isRegenerate = false, +}: Pick & { + errorMessage: TMessage; +}): TMessage[] => { + if (isRegenerate) { + const finalMessages: TMessage[] = []; + let replaced = false; + for (const message of regenerateMessages ?? messages) { + if (message.messageId === errorMessage.messageId) { + finalMessages.push(errorMessage); + replaced = true; + } else { + finalMessages.push(message); + } + } + if (!replaced) { + finalMessages.push(errorMessage); + } + return finalMessages; + } + + return [...messages, userMessage, errorMessage]; +}; + +/** + * A code approval mode picked locally is newer than any server copy of the + * conversation: the final and abort events return the mode the run STARTED + * with, and error recovery rebuilds from the pre-run capture. The local mode + * therefore survives every merge and the next send carries it. Identity is + * checked because the conversation atom is index-global: after navigating + * elsewhere mid-run, that conversation's mode must not land in this one. + */ +export const keepLocalCodeApprovalMode = >( + merged: T, + local: Partial | null | undefined, + conversationId: string | null | undefined, +): T => { + const localMode = local?.codeApprovalMode; + if (localMode == null || local?.conversationId !== conversationId) { + return merged; + } + return merged.codeApprovalMode === localMode + ? merged + : { ...merged, codeApprovalMode: localMode }; +}; + +/** Preset for a conversation rebuilt after a failed or aborted run. The detail + * cache holds a mode picked while that run streamed; the capture only the + * pre-run one. */ +export const buildRecoveryPreset = ( + submissionConvo: Partial, + cachedConvo: TConversation | null | undefined, + conversationId: string, + submittedDecision?: Pick, +): TPreset => + tPresetSchema.parse( + keepLocalCodeApprovalMode( + submittedDecision == null + ? submissionConvo + : withSubmittedCodeDecision(submissionConvo as TConversation, submittedDecision)!, + cachedConvo, + conversationId, + ), + );