diff --git a/client/src/Providers/ChatSettingsContext.tsx b/client/src/Providers/ChatSettingsContext.tsx index e096431a1b..e299f1c3e5 100644 --- a/client/src/Providers/ChatSettingsContext.tsx +++ b/client/src/Providers/ChatSettingsContext.tsx @@ -9,6 +9,8 @@ export const defaultChatSettings: ChatSettings = { saveDrafts: true, isTemporary: false, setIsTemporary: () => undefined, + getEphemeralAgent: () => null, + addedConversation: null, config: { feedbackEnabled: false, canRenameRunningChat: false }, auth: {}, getPaneConversations: () => [], diff --git a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx index 5ea98a3850..984f0061b1 100644 --- a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx @@ -24,6 +24,7 @@ import { activeUsageResponseIdFamily, pendingUsageFamily, } from '~/components/Chat/Input/TokenUsage/store'; +import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext'; import { pendingReasoningOverrideFamily } from '~/components/Chat/Input/Composer/state'; import { submissionStartFamily, showStopButtonByIndex } from '~/store/generation'; import useChatFunctions from '../useChatFunctions'; @@ -127,7 +128,6 @@ jest.mock('~/store', () => ({ messagesSiblingIdxFamily: () => 'messagesSiblingIdx', conversationByKeySelector: () => 'conversation', }, - useGetEphemeralAgent: () => mockGetEphemeralAgent, })); jest.mock('~/utils', () => ({ ...jest.requireActual('~/utils'), @@ -169,6 +169,14 @@ const conversation = (conversationId: string) => agent_id: 'agent-1', }) as TConversation; +const settingsWrapper = ({ children }: { children: ReactNode }) => ( + + {children} + +); + function renderAsk( messages: TMessage[] | undefined, conversationId = 'conversation-1', @@ -198,7 +206,11 @@ function renderAsk( const reasoningStore = createStore(); reasoningStore.set(pendingReasoningOverrideFamily(conversationId), options.reasoningOverride); const wrapper = ({ children }: { children: ReactNode }) => ( - {children} + + {children} + ); const hook = renderHook( () => @@ -1144,18 +1156,20 @@ describe('useChatFunctions ask attachments', () => { }, ], ]) as unknown as Parameters[0]['files']; - const { result } = renderHook(() => - useChatFunctions({ - isSubmitting: false, - latestMessage: null, - conversation: conversation(Constants.NEW_CONVO as string), - getMessages: () => [], - setMessages, - setSubmission, - setConversation: mockSetConversation, - files, - setFiles, - }), + const { result } = renderHook( + () => + useChatFunctions({ + isSubmitting: false, + latestMessage: null, + conversation: conversation(Constants.NEW_CONVO as string), + getMessages: () => [], + setMessages, + setSubmission, + setConversation: mockSetConversation, + files, + setFiles, + }), + { wrapper: settingsWrapper }, ); const selectedConversationTools: TEphemeralAgent = { skills: true, diff --git a/client/src/hooks/Chat/ask/guards.ts b/client/src/hooks/Chat/ask/guards.ts new file mode 100644 index 0000000000..136d6029f4 --- /dev/null +++ b/client/src/hooks/Chat/ask/guards.ts @@ -0,0 +1,23 @@ +import { QueryKeys } from 'librechat-data-provider'; +import type { QueryClient } from '@tanstack/react-query'; +import type { TMessage } from 'librechat-data-provider'; + +/** A revalidating cache younger than this is locally authoritative (the run + * that just streamed wrote it) and stays sendable; older ones wait for the + * refetch so a send can't fork from an outdated tail. */ +const STALE_SEND_REVALIDATION_MS = 5_000; + +/** Whether the cached history is mid-refetch and old enough that a send could fork from it. */ +export function isHistoryRevalidating( + queryClient: QueryClient, + conversationId: string | null, +): boolean { + const messagesQueryState = queryClient.getQueryState([ + QueryKeys.messages, + conversationId, + ]); + const isRevalidating = + messagesQueryState?.isInvalidated === true && messagesQueryState.fetchStatus === 'fetching'; + const cacheAgeMs = Date.now() - (messagesQueryState?.dataUpdatedAt ?? 0); + return isRevalidating && cacheAgeMs > STALE_SEND_REVALIDATION_MS; +} diff --git a/client/src/hooks/Chat/ask/reasoning.ts b/client/src/hooks/Chat/ask/reasoning.ts new file mode 100644 index 0000000000..4caf957413 --- /dev/null +++ b/client/src/hooks/Chat/ask/reasoning.ts @@ -0,0 +1,121 @@ +import { + QueryKeys, + ReasoningEffort, + getEndpointField, + resolveModelReasoning, + isAgentsEndpoint, + isReasoningOverrideSupported, + resolveReasoningSettingForTarget, +} from 'librechat-data-provider'; +import type { + Agent, + TMessage, + TConversation, + TEndpointsConfig, + TReasoningCapabilitiesResponse, +} from 'librechat-data-provider'; +import type { QueryClient } from '@tanstack/react-query'; +import { usesReasoningCapabilities } from '~/hooks/Endpoint/useModelReasoning'; +import { resolveAgentTarget } from '~/utils'; + +type ReasoningOverride = NonNullable; + +type ResolveReasoningArgs = { + queryClient: QueryClient; + agentsMap?: Record | null; + endpoint?: string | null; + conversation?: TConversation | null; + endpointsConfig?: TEndpointsConfig; + reasoningOverride: ReasoningOverride; + /** The override came out of the pending atom rather than from the caller. */ + drainedReasoning: boolean; + stagedReasoning?: TMessage['reasoningOverride']; +}; + +/** + * Checks a request-scoped reasoning override against the endpoint's loaded capabilities. An + * unsupported override is dropped; `restoreStaged` asks the caller to put a drained one back + * when it was refused only because the catalog is not loaded yet. + */ +export function resolveSubmittedReasoning({ + queryClient, + agentsMap, + endpoint, + conversation, + endpointsConfig, + reasoningOverride, + drainedReasoning, + stagedReasoning, +}: ResolveReasoningArgs): { + reasoningOverride: TMessage['reasoningOverride']; + restoreStaged: boolean; +} { + const isAgent = isAgentsEndpoint(endpoint); + const agentId = isAgent ? conversation?.agent_id : undefined; + const savedAgent = + agentId != null + ? (queryClient.getQueryData([QueryKeys.agent, agentId]) ?? agentsMap?.[agentId]) + : undefined; + const agentTarget = isAgent ? resolveAgentTarget(agentId, savedAgent) : undefined; + const effectiveEndpoint = isAgent ? agentTarget?.provider : endpoint; + const effectiveModel = isAgent ? agentTarget?.model : conversation?.model; + const effectiveEndpointType = getEndpointField(endpointsConfig, effectiveEndpoint, 'type'); + const customParams = + effectiveEndpoint == null ? undefined : endpointsConfig?.[effectiveEndpoint]?.customParams; + const capabilitiesKey = [QueryKeys.reasoningCapabilities, effectiveEndpoint]; + /* A failed or running refresh leaves the previous data cached, and so does an entry that is + past its lifetime while its refetch has not started (a throttled background tab): the + efforts are unknown in all three, as they are in the editors. */ + const capabilitiesState = queryClient.getQueryState(capabilitiesKey); + const cachedCapabilities = + queryClient.getQueryData(capabilitiesKey); + const capabilitiesExpired = + cachedCapabilities != null && + Date.now() - (capabilitiesState?.dataUpdatedAt ?? 0) >= cachedCapabilities.expiresInMs; + const capabilitiesData = + capabilitiesState?.status === 'error' || + capabilitiesState?.fetchStatus === 'fetching' || + capabilitiesExpired + ? undefined + : cachedCapabilities?.capabilities; + /* Auto sends no effort, so no catalog can refuse it and a restored one is always kept. */ + const clearsEffort = + reasoningOverride.key === 'reasoning_effort' && + reasoningOverride.value === ReasoningEffort.unset && + usesReasoningCapabilities(endpointsConfig, effectiveEndpoint ?? ''); + /* A replayed override is checked against the loaded per-model efforts. While they are + unknown (never requested, running or failed) `resolveModelReasoning` offers nothing, so + the override is omitted rather than sent and refused by the server. */ + const supportedSetting = + effectiveEndpoint == null + ? undefined + : resolveReasoningSettingForTarget({ + endpoint: effectiveEndpointType ?? effectiveEndpoint, + model: effectiveModel, + isAgent, + defaultParamsEndpoint: customParams?.defaultParamsEndpoint, + reasoningFormat: customParams?.reasoningFormat, + paramDefinitions: customParams?.paramDefinitions, + modelReasoning: + effectiveModel == null || clearsEffort + ? undefined + : resolveModelReasoning({ + capabilities: capabilitiesData, + endpoint: effectiveEndpoint, + model: effectiveModel, + paramDefinitions: customParams?.paramDefinitions, + }), + }); + + if (isReasoningOverrideSupported(reasoningOverride, supportedSetting)) { + return { reasoningOverride, restoreStaged: false }; + } + /* Refused only because the catalog is not known yet, not because it said no: the user's + one-shot choice is put back instead of being lost, and applies once it can be checked. */ + const restoreStaged = + drainedReasoning && + stagedReasoning != null && + capabilitiesData == null && + usesReasoningCapabilities(endpointsConfig, effectiveEndpoint ?? ''); + return { reasoningOverride: undefined, restoreStaged }; +} diff --git a/client/src/hooks/Chat/ask/regenerate.ts b/client/src/hooks/Chat/ask/regenerate.ts new file mode 100644 index 0000000000..5ac7b8d5b0 --- /dev/null +++ b/client/src/hooks/Chat/ask/regenerate.ts @@ -0,0 +1,43 @@ +import type { TMessage, TConversation } from 'librechat-data-provider'; +import type { TAskFunction } from '~/common'; + +/** Builds `regenerate`: replays the user message a response answers, with its skills, quotes and reasoning. */ +export const createRegenerate = + (ask: TAskFunction, getMessages: (conversationId?: string | null) => TMessage[] | undefined) => + ( + message: Partial>, + options?: { addedConvo?: TConversation | null }, + ) => { + const messages = getMessages(); + const parentMessageId = + message.isCreatedByUser === true ? message.messageId : message.parentMessageId; + const targetResponseMessageId = + message.isCreatedByUser === true ? undefined : message.messageId; + const parentMessage = messages?.find((element) => element.messageId == parentMessageId); + + if (parentMessage && parentMessage.isCreatedByUser) { + ask( + { ...parentMessage }, + { + isRegenerate: true, + addedConvo: options?.addedConvo ?? undefined, + targetResponseMessageId, + /** Carry the original user message's manual skill picks forward + * so the regenerated response is primed with the same skills. + * The compose-time atom was drained on the first submit; without + * this the model sees an unprimed turn even though the pills + * still show on the user bubble. */ + overrideManualSkills: parentMessage.manualSkills, + /** Carry the original user message's quoted excerpts forward so the + * regenerated response is sent the same referenced context. */ + overrideQuotes: parentMessage.quotes, + /** Replay the exact request-scoped reasoning selection used by this turn. */ + overrideReasoning: parentMessage.reasoningOverride ?? null, + }, + ); + } else { + console.error( + 'Failed to regenerate the message: parentMessage not found or not created by user.', + ); + } + }; diff --git a/client/src/hooks/Chat/ask/turn.ts b/client/src/hooks/Chat/ask/turn.ts new file mode 100644 index 0000000000..532bfefd08 --- /dev/null +++ b/client/src/hooks/Chat/ask/turn.ts @@ -0,0 +1,491 @@ +import { v4 } from 'uuid'; +import { cloneDeep } from 'lodash'; +import { + Constants, + ContentTypes, + EModelEndpoint, + getEndpointField, + getDefaultParamsEndpoint, + isAgentsEndpoint, + parseCompactConvo, + replaceSpecialVars, + isAssistantsEndpoint, +} from 'librechat-data-provider'; +import { + createDualMessageContent, + stripStreamedIndexStamps, + getAppendParentMessageId, + getRegenerateTargetResponseMessage, + getRegenerateSubmissionMessages, + getPreliminaryRegenerateResponseMessageId, +} from '@librechat/chat'; +import type { + TUser, + TMessage, + TModelSpec, + TSubmission, + TConversation, + TEphemeralAgent, + TEndpointOption, + TEndpointsConfig, + EndpointSchemaKey, +} from 'librechat-data-provider'; +import type { TAskProps, TOptions, ExtendedFile } from '~/common'; +import { getRouteChatProjectId, logger } from '~/utils'; + +/** What `ask` hands the turn builder once it has decided the send may go ahead. */ +export type BuildTurnInput = { + text: string; + params: TAskProps; + options: TOptions; + /** Resolved by the caller: the explicit id, else the conversation's, else `null`. */ + conversationId: string | null; + index: number; + conversation: TConversation | null | undefined; + latestMessage: TMessage | null; + cachedMessages?: TMessage[]; + files?: Map; + /** Whether the host supplied a composer file setter, so consumed files can be cleared. */ + canClearFiles: boolean; + endpointsConfig?: TEndpointsConfig; + modelSpecs?: TModelSpec[]; + user?: TUser; + isTemporary: boolean; + ephemeralAgent: TEphemeralAgent | null; + getSender: (endpointOption: TEndpointOption) => string; + userKey: string | undefined; + manualSkills: string[]; + quotes: string[]; + reasoningOverride: TMessage['reasoningOverride']; + code: { + approvalMode: TSubmission['codeApprovalMode']; + latestApprovalMode?: TConversation['codeApprovalMode']; + environmentMode: TSubmission['codeEnvironmentMode']; + workspaces: TSubmission['codeWorkspaces']; + }; +}; + +/** Composer files the turn consumed, for the caller to mark as submitted and clear. */ +export type TurnFileEffects = { + submittedFileIds: (string | undefined)[]; + clearComposer: boolean; +}; + +export type BuiltTurn = { + submission: TSubmission; + currentMsg: TMessage; + initialResponse: TMessage; + submissionMessages: TMessage[]; + /** `null` for a new chat: the server assigns the id. */ + conversationId: string | null; + regenerateShaped: boolean; + isEditOrContinue: boolean; + /** Set when the send starts from the new-chat route, which the caller navigates to. */ + newChatPath?: string; + fileEffects?: TurnFileEffects; + /** Whether the host should mark the chat as submitting now (Assistants flip it from the stream). */ + startsSubmitting: boolean; +}; + +const logChatRequest = (request: Record) => { + logger.log('=====================================\nAsk function called with:'); + logger.dir({ + conversationId: request.conversationId, + messageId: request.messageId, + parentMessageId: request.parentMessageId, + isEdited: request.isEdited, + isContinued: request.isContinued, + isRegenerate: request.isRegenerate, + }); + logger.log('====================================='); +}; + +/** + * Builds the turn `ask` submits: the optimistic user message and response placeholder, the + * message list to render, and the `TSubmission` the stream starts from. Reads nothing from the + * app and writes nothing; the caller applies the navigation, composer-file and state effects the + * result names. + */ +export function buildTurn({ + text, + params, + options, + index, + conversation, + latestMessage, + cachedMessages, + files, + canClearFiles, + endpointsConfig, + modelSpecs, + user, + isTemporary, + ephemeralAgent, + getSender, + userKey, + manualSkills, + quotes, + reasoningOverride, + code, + conversationId: resolvedConversationId, +}: BuildTurnInput): BuiltTurn { + const { overrideConvoId, overrideUserMessageId, messageId = null } = params; + let parentMessageId = params.parentMessageId ?? null; + let conversationId = resolvedConversationId; + const { + editedContent = null, + editedMessageId = null, + isRegenerate = false, + isContinued = false, + isEdited = false, + compact = false, + overrideMessages, + overrideFiles, + targetResponseMessageId, + addedConvo, + overrideClientRequestId, + overrideRecoverySteerId, + overrideExpectedPredecessorCreatedAt, + overrideQueuedMessageOrigin, + } = options; + const regenerateShaped = isRegenerate || compact; + const isEditOrContinue = isEdited || isContinued; + const endpoint = conversation?.endpoint; + const endpointType = getEndpointField(endpointsConfig, endpoint, 'type'); + const defaultParamsEndpoint = getDefaultParamsEndpoint(endpointsConfig, endpoint); + const { latestApprovalMode: latestCodeApprovalMode } = code; + const { approvalMode: codeApprovalMode, environmentMode: codeEnvironmentMode } = code; + const codeWorkspaces = code.workspaces; + + let currentMessages: TMessage[] = overrideMessages ?? cachedMessages ?? []; + + if (conversation?.promptPrefix) { + conversation.promptPrefix = replaceSpecialVars({ + text: conversation.promptPrefix, + user, + }); + } + + const chatProjectId = + conversationId === Constants.NEW_CONVO + ? getRouteChatProjectId() + : (conversation?.chatProjectId ?? null); + const conversationForPayload = + chatProjectId != null ? { ...(conversation ?? {}), chatProjectId } : (conversation ?? {}); + + // construct the query message + // this is not a real messageId, it is used as placeholder before real messageId returned + const intermediateId = overrideUserMessageId ?? v4(); + /** Stable idempotency key for this submission: fresh per `ask()` (so regenerate differs) + * but reused across the client's start-generation network retries, letting the server + * dedup a retried request instead of starting a second billed generation. */ + const clientRequestId = overrideClientRequestId ?? v4(); + if (parentMessageId == null) { + parentMessageId = getAppendParentMessageId({ latestMessage, currentMessages }); + } + + logChatRequest({ + index, + conversation, + latestMessage, + conversationId, + intermediateId, + parentMessageId, + currentMessages, + }); + + let newChatPath: string | undefined; + if (conversationId == Constants.NEW_CONVO) { + parentMessageId = Constants.NO_PARENT; + currentMessages = []; + conversationId = null; + const projectSearch = chatProjectId ? `?projectId=${encodeURIComponent(chatProjectId)}` : ''; + newChatPath = `/c/new${projectSearch}`; + } + + const targetParentMessageId = regenerateShaped ? messageId : latestMessage?.parentMessageId; + /** + * If the user regenerated or resubmitted the message, the current parent is technically + * the latest user message, which is passed into `ask`; otherwise, we can rely on the + * latestMessage to find the parent. + */ + const targetParentMessage = currentMessages.find( + (msg) => msg.messageId === targetParentMessageId, + ); + const targetResponseMessage = isRegenerate + ? getRegenerateTargetResponseMessage({ + messages: currentMessages, + parentMessageId: messageId, + targetResponseMessageId, + latestMessage, + }) + : null; + + let thread_id = targetParentMessage?.thread_id ?? latestMessage?.thread_id; + if (thread_id == null) { + thread_id = currentMessages.find((message) => message.thread_id)?.thread_id; + } + + const iconURL = conversation?.iconURL; + + /** This becomes part of the `endpointOption` */ + const convo = parseCompactConvo({ + endpoint: endpoint as EndpointSchemaKey, + endpointType: endpointType as EndpointSchemaKey, + conversation: conversationForPayload, + defaultParamsEndpoint, + }); + + const { modelDisplayLabel } = endpointsConfig?.[endpoint ?? ''] ?? {}; + const endpointOption = Object.assign( + { + endpoint, + endpointType, + overrideConvoId, + overrideUserMessageId: + overrideUserMessageId ?? + (endpoint === EModelEndpoint.agents && !regenerateShaped && !isContinued + ? `${intermediateId}${Constants.COMMON_DIVIDER}0` + : undefined), + }, + convo, + chatProjectId ? { chatProjectId } : {}, + ) as TEndpointOption; + if (endpoint !== EModelEndpoint.agents) { + endpointOption.key = userKey; + endpointOption.thread_id = thread_id; + endpointOption.modelDisplayLabel = modelDisplayLabel; + } else { + endpointOption.key = new Date(Date.now() + 60 * 60 * 1000).toISOString(); + } + const responseSender = getSender({ model: conversation?.model, ...endpointOption }); + + const currentMsg: TMessage = { + text, + sender: 'User', + clientTimestamp: new Date().toLocaleString('sv').replace(' ', 'T'), + isCreatedByUser: true, + parentMessageId, + conversationId, + /** A compaction's "user message" is the leaf itself, so an error lands under it. */ + messageId: + (isContinued || compact) && messageId != null && messageId ? messageId : intermediateId, + thread_id, + error: false, + /** + * UI-only metadata. Survives reload because the backend persists the + * field on the message schema, and `SkillPills` reads straight + * off the message so there's no Recoil state to clean up. Runtime + * skill resolution reads the top-level `manualSkills` payload field. + */ + manualSkills: manualSkills.length > 0 ? manualSkills : undefined, + /** + * Quoted excerpts the user referenced this turn. Persisted on the + * message (backend echoes it back on `req.body.quotes`) so `MessageQuotes` + * renders the references on the user bubble after reload. The backend + * also merges these into the model-facing user text at request time. + */ + quotes: quotes.length > 0 ? quotes : undefined, + reasoningOverride, + }; + + /** The leaf's files already sit in history; a compaction re-attaches nothing. */ + const submissionFiles = compact ? undefined : (overrideFiles ?? targetParentMessage?.files); + const reuseFiles = + (isRegenerate || (overrideFiles != null && overrideFiles.length)) && + submissionFiles && + submissionFiles.length > 0; + + let fileEffects: TurnFileEffects | undefined; + if (canClearFiles && reuseFiles === true) { + currentMsg.files = [...submissionFiles]; + /** Queued override files were consumed just like composer files, so mark their identities + * as submitted before later draft cleanup can classify the restored paste as unsent. */ + const submittedFileIds: (string | undefined)[] = []; + for (const file of submissionFiles) { + submittedFileIds.push(file.file_id, file.temp_file_id); + } + // Caller-supplied overrideFiles were consumed elsewhere (queued + // during-run messages take theirs out of the composer at queue time); + // clearing here would eat attachments staged for the user's NEXT send. + fileEffects = { submittedFileIds, clearComposer: isRegenerate }; + } else if (!compact && canClearFiles && files && files.size > 0 && overrideFiles == null) { + /** A compaction attaches nothing and must not consume files the user + * staged in the composer for their next message. */ + // `overrideFiles` (even empty) is authoritative for the submission: + // auto-drained queued messages must never vacuum up attachments the + // user has staged in the composer for their NEXT message. + const submittedFileIds: (string | undefined)[] = []; + currentMsg.files = []; + for (const [key, file] of files) { + currentMsg.files.push({ + file_id: file.file_id, + filepath: file.filepath, + filename: file.filename, + type: file.type ?? '', + llmDeliveryPath: file.llmDeliveryPath, + height: file.height, + width: file.width, + }); + submittedFileIds.push(key, file.file_id, file.temp_file_id); + } + fileEffects = { submittedFileIds, clearComposer: true }; + } + + const responseMessageId = + editedMessageId ?? + (isRegenerate + ? getPreliminaryRegenerateResponseMessageId( + targetResponseMessage?.messageId ?? targetResponseMessageId, + ) + : null) ?? + null; + /** Set only for edited resubmissions; see `TSubmission.editPrefixLength`. */ + let editPrefixLength: number | undefined; + const initialResponseId = + responseMessageId ?? + `${regenerateShaped ? messageId : intermediateId}`.replace(/_+$/, '') + '_'; + + const initialResponse: TMessage = { + sender: responseSender, + text: '', + endpoint: endpoint ?? '', + parentMessageId: regenerateShaped ? messageId : intermediateId, + messageId: initialResponseId, + thread_id, + conversationId, + unfinished: false, + isCreatedByUser: false, + model: convo?.model, + error: false, + iconURL, + clientQueueParentMessageId: regenerateShaped ? (messageId ?? undefined) : intermediateId, + /** + * Seed the assistant placeholder with the turn's manually-invoked + * skill names so `ContentParts` can render interim `SkillCall` cards + * from the very first render: no round-trip through the `created` + * SSE event required. Rides along with every subsequent spread + * (`useStepHandler` response construction, `updateContent` result + * spreads) and drops out naturally at `finalHandler` when the + * server-backed `responseMessage` replacement takes over. + */ + manualSkills: manualSkills.length > 0 ? manualSkills : undefined, + }; + + if (isAssistantsEndpoint(endpoint)) { + initialResponse.model = conversation?.assistant_id ?? ''; + initialResponse.text = ''; + initialResponse.content = [ + { + type: ContentTypes.TEXT, + [ContentTypes.TEXT]: { + value: '', + }, + }, + ]; + } else if (endpoint != null) { + initialResponse.model = isAgentsEndpoint(endpoint) + ? (conversation?.agent_id ?? '') + : (conversation?.model ?? ''); + initialResponse.text = ''; + + if (editedContent && latestMessage?.content) { + /** Stamps off: the rerun appends provider parts at the prefix LENGTH, + * and a retained `streamedIndex` at or above it would collide with an + * appended part's render key (see `stripStreamedIndexStamps`). */ + initialResponse.content = stripStreamedIndexStamps(cloneDeep(latestMessage.content)); + /** Captured now, while it is still the retained prefix: a later resume + * sync replaces this array with the server's completion-local + * snapshot, after which its length no longer describes the offset. */ + editPrefixLength = initialResponse.content.length; + const { index, type, ...part } = editedContent; + if (initialResponse.content && index >= 0 && index < initialResponse.content.length) { + const contentPart = initialResponse.content[index]; + if (type === ContentTypes.THINK && contentPart.type === ContentTypes.THINK) { + contentPart[ContentTypes.THINK] = part[ContentTypes.THINK]; + delete contentPart.reasoning_label; + delete contentPart.reasoning_label_step_id; + delete contentPart.reasoning_label_attempts; + delete contentPart.reasoning_label_submitted_chars; + delete contentPart.reasoning_label_revision; + delete contentPart.reasoning_label_status; + } else if (type === ContentTypes.TEXT && contentPart.type === ContentTypes.TEXT) { + contentPart[ContentTypes.TEXT] = part[ContentTypes.TEXT]; + } + } + } else if (addedConvo && conversation) { + // Pre-populate placeholders for smooth UI - these will be overridden/extended + // as SSE events arrive with actual content, preserving the agent-based agentId + initialResponse.content = createDualMessageContent( + conversation, + addedConvo, + endpointsConfig, + modelSpecs, + ); + } else { + initialResponse.content = []; + } + } + + if (isContinued) { + currentMessages = currentMessages.filter((msg) => msg.messageId !== responseMessageId); + } + + const submissionMessages = regenerateShaped + ? getRegenerateSubmissionMessages({ + messages: currentMessages, + targetResponseMessage, + initialResponseId: initialResponse.messageId, + }) + : currentMessages; + const regenerateMessages = regenerateShaped ? [...currentMessages] : undefined; + + logger.log('message_state', initialResponse); + const submission: TSubmission = { + conversation: { + ...conversation, + ...(chatProjectId ? { chatProjectId } : {}), + ...(latestCodeApprovalMode != null ? { codeApprovalMode: latestCodeApprovalMode } : {}), + conversationId, + }, + endpointOption, + userMessage: { + ...currentMsg, + responseMessageId, + overrideParentMessageId: isRegenerate ? messageId : null, + }, + messages: submissionMessages, + regenerateMessages, + isEdited: isEditOrContinue, + isContinued, + isRegenerate: regenerateShaped, + ...(compact && { compact: true }), + initialResponse, + isTemporary, + ephemeralAgent, + editedContent, + editPrefixLength, + addedConvo, + manualSkills: manualSkills.length > 0 ? manualSkills : undefined, + codeApprovalMode, + codeEnvironmentMode, + codeWorkspaces, + clientRequestId, + recoverySteerId: overrideRecoverySteerId, + expectedPredecessorCreatedAt: overrideExpectedPredecessorCreatedAt, + queuedMessageOrigin: overrideQueuedMessageOrigin, + }; + + return { + submission, + currentMsg, + initialResponse, + submissionMessages, + conversationId, + regenerateShaped, + isEditOrContinue, + newChatPath, + fileEffects, + startsSubmitting: endpoint != null && !isAssistantsEndpoint(endpoint), + }; +} diff --git a/client/src/hooks/Chat/contract.ts b/client/src/hooks/Chat/contract.ts index 983a92ef8f..87b568a723 100644 --- a/client/src/hooks/Chat/contract.ts +++ b/client/src/hooks/Chat/contract.ts @@ -6,6 +6,7 @@ import type { TAttachment, RetentionMode, TConversation, + TEphemeralAgent, ChatTransport, ChatTransportRequest, } from 'librechat-data-provider'; @@ -221,6 +222,10 @@ export type ChatSettings = { isTemporary: boolean; /** Turns temporary chat on or off for the next conversation. */ setIsTemporary: Dispatch>; + /** Reads the ephemeral agent (tool toggles) staged for a conversation, `null` when none. */ + getEphemeralAgent: (conversationId: string) => TEphemeralAgent | null; + /** The conversation open in the added (multi-convo) pane, `null` when there is none. */ + addedConversation: TConversation | null; config: ChatConfig; auth: ChatAuth; /** The conversation each mounted chat pane shows, read when called rather than at render: diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.ts index 45e6cecc3d..d5e5719a33 100644 --- a/client/src/hooks/Chat/useChatFunctions.ts +++ b/client/src/hooks/Chat/useChatFunctions.ts @@ -1,50 +1,25 @@ import { useCallback, useEffect, useRef } from 'react'; -import { v4 } from 'uuid'; import { cloneDeep } from 'lodash'; +import { useSetRecoilState } from 'recoil'; import { useStore, useSetAtom } from 'jotai'; import { useNavigate } from 'react-router-dom'; import { useQueryClient } from '@tanstack/react-query'; -import { useSetRecoilState, useRecoilValue } from 'recoil'; +import { hasPendingAssistantParent } from '@librechat/chat'; import { revealedQueuedTurnFamily } from '@librechat/chat/react'; import { Constants, QueryKeys, - ContentTypes, - ReasoningEffort, EModelEndpoint, - getEndpointField, - resolveModelReasoning, - isAgentsEndpoint, - parseCompactConvo, - replaceSpecialVars, isAssistantsEndpoint, - getDefaultParamsEndpoint, - isReasoningOverrideSupported, - resolveReasoningSettingForTarget, } from 'librechat-data-provider'; import type { - Agent, TMessage, TSubmission, TConversation, - TEndpointOption, TEndpointsConfig, - EndpointSchemaKey, - TReasoningCapabilitiesResponse, } from 'librechat-data-provider'; import type { SetterOrUpdater } from 'recoil'; import type { TAskFunction, ExtendedFile } from '~/common'; -import { - logger, - requestChatFocus, - resolveAgentTarget, - markPasteSubmitted, - hasStreamStartFailed, - isSubmittableMessage, - createDualMessageContent, - getRouteChatProjectId, - stripStreamedIndexStamps, -} from '~/utils'; import { getReasoningStateKey, pendingQuotesByConvoId, @@ -55,10 +30,10 @@ import { withSubmittedCodeDecision, resolveSubmittedCodeApprovalMode, } from '~/hooks/Agents/codeDecision'; +import { logger, requestChatFocus, markPasteSubmitted, isSubmittableMessage } from '~/utils'; import { activeUsageResponseIdFamily } from '~/components/Chat/Input/TokenUsage/store'; import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse'; import { submissionStartFamily, showStopButtonByIndex } from '~/store/generation'; -import { usesReasoningCapabilities } from '~/hooks/Endpoint/useModelReasoning'; import useGetConversation from '~/hooks/Conversations/useGetConversation'; import useCodeApprovalMode from '~/hooks/Agents/useCodeApprovalMode'; import useSetFilesToDelete from '~/hooks/Files/useSetFilesToDelete'; @@ -66,161 +41,18 @@ import { useAgentsMapContext } from '~/Providers/AgentsMapContext'; import { useChatSettings } from '~/Providers/ChatSettingsContext'; import useCodeWorkspace from '~/hooks/Agents/useCodeWorkspace'; import useGetSender from '~/hooks/Conversations/useGetSender'; -import store, { useGetEphemeralAgent } from '~/store'; +import { resolveSubmittedReasoning } from './ask/reasoning'; +import { isHistoryRevalidating } from './ask/guards'; +import { createRegenerate } from './ask/regenerate'; import useUserKey from '~/hooks/Input/useUserKey'; +import { buildTurn } from './ask/turn'; +import store from '~/store'; -/** A revalidating cache younger than this is locally authoritative (the run - * that just streamed wrote it) and stays sendable; older ones wait for the - * refetch so a send can't fork from an outdated tail. */ -const STALE_SEND_REVALIDATION_MS = 5_000; - -const logChatRequest = (request: Record) => { - logger.log('=====================================\nAsk function called with:'); - logger.dir({ - conversationId: request.conversationId, - messageId: request.messageId, - parentMessageId: request.parentMessageId, - isEdited: request.isEdited, - isContinued: request.isContinued, - isRegenerate: request.isRegenerate, - }); - logger.log('====================================='); -}; - -const getAppendParentMessageId = ({ - latestMessage, - currentMessages, -}: { - latestMessage: TMessage | null; - currentMessages: TMessage[]; -}) => { - if (!latestMessage) { - return Constants.NO_PARENT; - } - - if (!hasStreamStartFailed(latestMessage)) { - return latestMessage.messageId; - } - - const failedUserMessage = currentMessages.find( - (message) => message.messageId === latestMessage.parentMessageId, - ); - if (failedUserMessage?.isCreatedByUser !== true) { - return latestMessage.messageId; - } - - return failedUserMessage.parentMessageId ?? Constants.NO_PARENT; -}; - -const hasPendingAssistantParent = (message: TMessage | null) => - !!message?.messageId && - message.isCreatedByUser !== true && - message.messageId.endsWith('_') && - message.createdAt == null && - message.updatedAt == null && - !hasStreamStartFailed(message); - -type RegenerateTargetResponseArgs = { - messages: TMessage[]; - parentMessageId?: string | null; - targetResponseMessageId?: string | null; - latestMessage?: TMessage | null; -}; - -const isAssistantResponseForParent = ( - message: TMessage | null | undefined, - parentMessageId?: string | null, -): message is TMessage => - !!message && - !message.isCreatedByUser && - !!parentMessageId && - message.parentMessageId === parentMessageId; - -export function getPreliminaryRegenerateResponseMessageId( - responseMessageId?: string | null, -): string | null { - if (typeof responseMessageId !== 'string' || responseMessageId.length === 0) { - return null; - } - - return `${responseMessageId.replace(/_+$/, '')}_`; -} - -export function getRegenerateTargetResponseMessage({ - messages, - parentMessageId, - targetResponseMessageId, - latestMessage, -}: RegenerateTargetResponseArgs): TMessage | null { - if (!parentMessageId) { - return null; - } - - if (targetResponseMessageId) { - const targetResponse = messages.find( - (message) => - message.messageId === targetResponseMessageId && - isAssistantResponseForParent(message, parentMessageId), - ); - if (targetResponse) { - return targetResponse; - } - } - - if (isAssistantResponseForParent(latestMessage, parentMessageId)) { - return latestMessage; - } - - for (let i = messages.length - 1; i >= 0; i--) { - const message = messages[i]; - if (isAssistantResponseForParent(message, parentMessageId)) { - return message; - } - } - - return null; -} - -export function getRegenerateSubmissionMessages({ - messages, - targetResponseMessage, - initialResponseId, -}: { - messages: TMessage[]; - targetResponseMessage?: TMessage | null; - initialResponseId?: string | null; -}): TMessage[] { - if (targetResponseMessage?.messageId) { - /** - * Remove the response being regenerated and its descendants only: NOT a - * flat `slice(0, targetIndex)`, which also drops unrelated sibling branches - * that merely sit later in the array. That collapse made the optimistic - * render briefly lose other branches mid-regenerate (visible flash, and the - * scroll jumping to the shrunken content). Keeping them holds the thread - * (and scroll) steady. This array is render-only; the server regenerates - * from `parentMessageId`, so removing by subtree never affects the payload. - */ - const removed = new Set([targetResponseMessage.messageId]); - let grew = true; - while (grew) { - grew = false; - for (const message of messages) { - const parentMessageId = message.parentMessageId; - if ( - parentMessageId != null && - removed.has(parentMessageId) && - !removed.has(message.messageId) - ) { - removed.add(message.messageId); - grew = true; - } - } - } - return messages.filter((message) => !removed.has(message.messageId)); - } - - return messages.filter((msg) => msg.messageId !== initialResponseId); -} +export { + getRegenerateTargetResponseMessage, + getRegenerateSubmissionMessages, + getPreliminaryRegenerateResponseMessageId, +} from '@librechat/chat'; export default function useChatFunctions({ index = 0, @@ -253,9 +85,8 @@ export default function useChatFunctions({ const getSender = useGetSender(); const queryClient = useQueryClient(); const setFilesToDelete = useSetFilesToDelete(); - const getEphemeralAgent = useGetEphemeralAgent(); const agentsMap = useAgentsMapContext(); - const { isTemporary, config, auth } = useChatSettings(); + const { isTemporary, config, auth, getEphemeralAgent, addedConversation } = useChatSettings(); const { getExpiry } = useUserKey(immutableConversation?.endpoint ?? ''); const setIsSubmitting = useSetRecoilState(store.isSubmittingFamily(index)); const setSubmissionStart = useSetAtom(submissionStartFamily(index)); @@ -263,7 +94,6 @@ export default function useChatFunctions({ const focusRegeneratedResponse = useFocusRegeneratedResponse(); const jotaiStore = useStore(); const getConversation = useGetConversation(index); - const addedConversation = useRecoilValue(store.conversationByKeySelector(1)); const codeWorkspaceState = useCodeWorkspace(immutableConversation, addedConversation); const { modes: codeApprovalModes, selected: fallbackCodeApprovalMode } = useCodeApprovalMode( immutableConversation, @@ -442,14 +272,8 @@ export default function useChatFunctions({ * rapid follow-ups during the post-run reconcile. */ if (isExistingConversation && overrideMessages == null) { - const messagesQueryState = queryClient.getQueryState([ - QueryKeys.messages, - conversationId, - ]); - const isRevalidating = - messagesQueryState?.isInvalidated === true && messagesQueryState.fetchStatus === 'fetching'; - const cacheAgeMs = Date.now() - (messagesQueryState?.dataUpdatedAt ?? 0); - if (isRevalidating && cacheAgeMs > STALE_SEND_REVALIDATION_MS) { + const isStale = isHistoryRevalidating(queryClient, conversationId); + if (isStale) { logger.warn('[useChatFunctions] Refusing to send while conversation history revalidates'); return false; } @@ -472,8 +296,6 @@ export default function useChatFunctions({ const ephemeralAgent = getEphemeralAgent(conversationId ?? Constants.NEW_CONVO); const endpointsConfig = queryClient.getQueryData([QueryKeys.endpoints]); - const endpointType = getEndpointField(endpointsConfig, endpoint, 'type'); - const defaultParamsEndpoint = getDefaultParamsEndpoint(endpointsConfig, endpoint); /** * Manual skill selection resolution: * - Explicit `overrideManualSkills` wins (regenerate / save-and-submit @@ -522,405 +344,93 @@ export default function useChatFunctions({ } const stagedReasoning = reasoningOverride; if (reasoningOverride != null) { - const isAgent = isAgentsEndpoint(endpoint); - const agentId = isAgent ? conversation?.agent_id : undefined; - const savedAgent = - agentId != null - ? (queryClient.getQueryData([QueryKeys.agent, agentId]) ?? agentsMap?.[agentId]) - : undefined; - const agentTarget = isAgent ? resolveAgentTarget(agentId, savedAgent) : undefined; - const effectiveEndpoint = isAgent ? agentTarget?.provider : endpoint; - const effectiveModel = isAgent ? agentTarget?.model : conversation?.model; - const effectiveEndpointType = getEndpointField(endpointsConfig, effectiveEndpoint, 'type'); - const customParams = - effectiveEndpoint == null ? undefined : endpointsConfig?.[effectiveEndpoint]?.customParams; - const capabilitiesKey = [QueryKeys.reasoningCapabilities, effectiveEndpoint]; - /* A failed or running refresh leaves the previous data cached, and so does an entry that is - past its lifetime while its refetch has not started (a throttled background tab): the - efforts are unknown in all three, as they are in the editors. */ - const capabilitiesState = queryClient.getQueryState(capabilitiesKey); - const cachedCapabilities = - queryClient.getQueryData(capabilitiesKey); - const capabilitiesExpired = - cachedCapabilities != null && - Date.now() - (capabilitiesState?.dataUpdatedAt ?? 0) >= cachedCapabilities.expiresInMs; - const capabilitiesData = - capabilitiesState?.status === 'error' || - capabilitiesState?.fetchStatus === 'fetching' || - capabilitiesExpired - ? undefined - : cachedCapabilities?.capabilities; - /* Auto sends no effort, so no catalog can refuse it and a restored one is always kept. */ - const clearsEffort = - reasoningOverride.key === 'reasoning_effort' && - reasoningOverride.value === ReasoningEffort.unset && - usesReasoningCapabilities(endpointsConfig, effectiveEndpoint ?? ''); - /* A replayed override is checked against the loaded per-model efforts. While they are - unknown (never requested, running or failed) `resolveModelReasoning` offers nothing, so - the override is omitted rather than sent and refused by the server. */ - const supportedSetting = - effectiveEndpoint == null - ? undefined - : resolveReasoningSettingForTarget({ - endpoint: effectiveEndpointType ?? effectiveEndpoint, - model: effectiveModel, - isAgent, - defaultParamsEndpoint: customParams?.defaultParamsEndpoint, - reasoningFormat: customParams?.reasoningFormat, - paramDefinitions: customParams?.paramDefinitions, - modelReasoning: - effectiveModel == null || clearsEffort - ? undefined - : resolveModelReasoning({ - capabilities: capabilitiesData, - endpoint: effectiveEndpoint, - model: effectiveModel, - paramDefinitions: customParams?.paramDefinitions, - }), - }); - if (!isReasoningOverrideSupported(reasoningOverride, supportedSetting)) { - reasoningOverride = undefined; - /* Refused only because the catalog is not known yet, not because it said no: the user's - one-shot choice is put back instead of being lost, and applies once it can be checked. */ - if ( - drainedReasoning && - stagedReasoning != null && - capabilitiesData == null && - usesReasoningCapabilities(endpointsConfig, effectiveEndpoint ?? '') - ) { - reasoningStore.set(pendingReasoningOverrideFamily(reasoningStateKey), stagedReasoning); - } + const resolved = resolveSubmittedReasoning({ + queryClient, + agentsMap, + endpoint, + conversation, + endpointsConfig, + reasoningOverride, + drainedReasoning, + stagedReasoning, + }); + reasoningOverride = resolved.reasoningOverride; + if (resolved.restoreStaged && stagedReasoning != null) { + reasoningStore.set(pendingReasoningOverrideFamily(reasoningStateKey), stagedReasoning); } } - const isEditOrContinue = isEdited || isContinued; - let currentMessages: TMessage[] = overrideMessages ?? cachedMessages ?? []; - - if (conversation?.promptPrefix) { - conversation.promptPrefix = replaceSpecialVars({ - text: conversation.promptPrefix, - user: auth.user, - }); - } - - const chatProjectId = - conversationId === Constants.NEW_CONVO - ? getRouteChatProjectId() - : (conversation?.chatProjectId ?? null); - const conversationForPayload = - chatProjectId != null ? { ...(conversation ?? {}), chatProjectId } : (conversation ?? {}); - - // construct the query message - // this is not a real messageId, it is used as placeholder before real messageId returned - const intermediateId = overrideUserMessageId ?? v4(); - /** Stable idempotency key for this submission: fresh per `ask()` (so regenerate differs) - * but reused across the client's start-generation network retries, letting the server - * dedup a retried request instead of starting a second billed generation. */ - const clientRequestId = overrideClientRequestId ?? v4(); - if (parentMessageId == null) { - parentMessageId = getAppendParentMessageId({ latestMessage, currentMessages }); - } - - logChatRequest({ + const turn = buildTurn({ + text, + params: { text, overrideConvoId, overrideUserMessageId, parentMessageId, messageId }, + options: { + editedContent: editedContent ?? undefined, + editedMessageId, + isRegenerate, + isContinued, + isEdited, + compact, + overrideMessages, + overrideFiles, + targetResponseMessageId, + addedConvo, + overrideClientRequestId, + overrideRecoverySteerId, + overrideExpectedPredecessorCreatedAt, + overrideQueuedMessageOrigin, + }, + conversationId, index, conversation, latestMessage, - conversationId, - intermediateId, - parentMessageId, - currentMessages, - }); - - if (conversationId == Constants.NEW_CONVO) { - parentMessageId = Constants.NO_PARENT; - currentMessages = []; - conversationId = null; - const projectSearch = chatProjectId ? `?projectId=${encodeURIComponent(chatProjectId)}` : ''; - requestChatFocus(); - navigate(`/c/new${projectSearch}`); - } - - const targetParentMessageId = regenerateShaped ? messageId : latestMessage?.parentMessageId; - /** - * If the user regenerated or resubmitted the message, the current parent is technically - * the latest user message, which is passed into `ask`; otherwise, we can rely on the - * latestMessage to find the parent. - */ - const targetParentMessage = currentMessages.find( - (msg) => msg.messageId === targetParentMessageId, - ); - const targetResponseMessage = isRegenerate - ? getRegenerateTargetResponseMessage({ - messages: currentMessages, - parentMessageId: messageId, - targetResponseMessageId, - latestMessage, - }) - : null; - - let thread_id = targetParentMessage?.thread_id ?? latestMessage?.thread_id; - if (thread_id == null) { - thread_id = currentMessages.find((message) => message.thread_id)?.thread_id; - } - - const iconURL = conversation?.iconURL; - - /** This becomes part of the `endpointOption` */ - const convo = parseCompactConvo({ - endpoint: endpoint as EndpointSchemaKey, - endpointType: endpointType as EndpointSchemaKey, - conversation: conversationForPayload, - defaultParamsEndpoint, - }); - - const { modelDisplayLabel } = endpointsConfig?.[endpoint ?? ''] ?? {}; - const endpointOption = Object.assign( - { - endpoint, - endpointType, - overrideConvoId, - overrideUserMessageId: - overrideUserMessageId ?? - (endpoint === EModelEndpoint.agents && !regenerateShaped && !isContinued - ? `${intermediateId}${Constants.COMMON_DIVIDER}0` - : undefined), - }, - convo, - chatProjectId ? { chatProjectId } : {}, - ) as TEndpointOption; - if (endpoint !== EModelEndpoint.agents) { - endpointOption.key = getExpiry(); - endpointOption.thread_id = thread_id; - endpointOption.modelDisplayLabel = modelDisplayLabel; - } else { - endpointOption.key = new Date(Date.now() + 60 * 60 * 1000).toISOString(); - } - const responseSender = getSender({ model: conversation?.model, ...endpointOption }); - - const currentMsg: TMessage = { - text, - sender: 'User', - clientTimestamp: new Date().toLocaleString('sv').replace(' ', 'T'), - isCreatedByUser: true, - parentMessageId, - conversationId, - /** A compaction's "user message" is the leaf itself, so an error lands under it. */ - messageId: - (isContinued || compact) && messageId != null && messageId ? messageId : intermediateId, - thread_id, - error: false, - /** - * UI-only metadata. Survives reload because the backend persists the - * field on the message schema, and `SkillPills` reads straight - * off the message so there's no Recoil state to clean up. Runtime - * skill resolution reads the top-level `manualSkills` payload field. - */ - manualSkills: manualSkills.length > 0 ? manualSkills : undefined, - /** - * Quoted excerpts the user referenced this turn. Persisted on the - * message (backend echoes it back on `req.body.quotes`) so `MessageQuotes` - * renders the references on the user bubble after reload. The backend - * also merges these into the model-facing user text at request time. - */ - quotes: quotes.length > 0 ? quotes : undefined, + cachedMessages, + files, + canClearFiles: setFiles != null, + endpointsConfig, + modelSpecs: config.modelSpecs, + user: auth.user, + isTemporary, + ephemeralAgent, + getSender, + userKey: endpoint !== EModelEndpoint.agents ? getExpiry() : undefined, + manualSkills, + quotes, reasoningOverride, - }; + code: { + approvalMode: codeApprovalMode, + latestApprovalMode: latestCodeApprovalMode, + environmentMode: codeEnvironmentMode, + workspaces: codeWorkspaces, + }, + }); + const { + submission, + currentMsg, + initialResponse, + submissionMessages, + regenerateShaped: isRegenerateShaped, + isEditOrContinue, + newChatPath, + fileEffects, + } = turn; + conversationId = turn.conversationId; - /** The leaf's files already sit in history; a compaction re-attaches nothing. */ - const submissionFiles = compact ? undefined : (overrideFiles ?? targetParentMessage?.files); - const reuseFiles = - (isRegenerate || (overrideFiles != null && overrideFiles.length)) && - submissionFiles && - submissionFiles.length > 0; - - if (setFiles && reuseFiles === true) { - currentMsg.files = [...submissionFiles]; - /** Queued override files were consumed just like composer files, so mark their identities - * as submitted before later draft cleanup can classify the restored paste as unsent. */ - const submittedFileIds: (string | undefined)[] = []; - for (const file of submissionFiles) { - submittedFileIds.push(file.file_id, file.temp_file_id); - } - markPasteSubmitted(...submittedFileIds); - // Caller-supplied overrideFiles were consumed elsewhere (queued - // during-run messages take theirs out of the composer at queue time); - // clearing here would eat attachments staged for the user's NEXT send. - if (isRegenerate) { + if (newChatPath != null) { + requestChatFocus(); + navigate(newChatPath); + } + if (fileEffects != null && setFiles) { + markPasteSubmitted(...fileEffects.submittedFileIds); + if (fileEffects.clearComposer) { setFiles(new Map()); setFilesToDelete({}); } - } else if (!compact && setFiles && files && files.size > 0 && overrideFiles == null) { - /** A compaction attaches nothing and must not consume files the user - * staged in the composer for their next message. */ - // `overrideFiles` (even empty) is authoritative for the submission: - // auto-drained queued messages must never vacuum up attachments the - // user has staged in the composer for their NEXT message. - const submittedFileIds: (string | undefined)[] = []; - currentMsg.files = []; - for (const [key, file] of files) { - currentMsg.files.push({ - file_id: file.file_id, - filepath: file.filepath, - filename: file.filename, - type: file.type ?? '', - llmDeliveryPath: file.llmDeliveryPath, - height: file.height, - width: file.width, - }); - submittedFileIds.push(key, file.file_id, file.temp_file_id); - } - // Publish every alias before clearing the composer or its draft. - markPasteSubmitted(...submittedFileIds); - setFiles(new Map()); - setFilesToDelete({}); } - - const responseMessageId = - editedMessageId ?? - (isRegenerate - ? getPreliminaryRegenerateResponseMessageId( - targetResponseMessage?.messageId ?? targetResponseMessageId, - ) - : null) ?? - null; - /** Set only for edited resubmissions; see `TSubmission.editPrefixLength`. */ - let editPrefixLength: number | undefined; - const initialResponseId = - responseMessageId ?? - `${regenerateShaped ? messageId : intermediateId}`.replace(/_+$/, '') + '_'; - - const initialResponse: TMessage = { - sender: responseSender, - text: '', - endpoint: endpoint ?? '', - parentMessageId: regenerateShaped ? messageId : intermediateId, - messageId: initialResponseId, - thread_id, - conversationId, - unfinished: false, - isCreatedByUser: false, - model: convo?.model, - error: false, - iconURL, - clientQueueParentMessageId: regenerateShaped ? (messageId ?? undefined) : intermediateId, - /** - * Seed the assistant placeholder with the turn's manually-invoked - * skill names so `ContentParts` can render interim `SkillCall` cards - * from the very first render: no round-trip through the `created` - * SSE event required. Rides along with every subsequent spread - * (`useStepHandler` response construction, `updateContent` result - * spreads) and drops out naturally at `finalHandler` when the - * server-backed `responseMessage` replacement takes over. - */ - manualSkills: manualSkills.length > 0 ? manualSkills : undefined, - }; - - if (isAssistantsEndpoint(endpoint)) { - initialResponse.model = conversation?.assistant_id ?? ''; - initialResponse.text = ''; - initialResponse.content = [ - { - type: ContentTypes.TEXT, - [ContentTypes.TEXT]: { - value: '', - }, - }, - ]; - } else if (endpoint != null) { - initialResponse.model = isAgentsEndpoint(endpoint) - ? (conversation?.agent_id ?? '') - : (conversation?.model ?? ''); - initialResponse.text = ''; - - if (editedContent && latestMessage?.content) { - /** Stamps off: the rerun appends provider parts at the prefix LENGTH, - * and a retained `streamedIndex` at or above it would collide with an - * appended part's render key (see `stripStreamedIndexStamps`). */ - initialResponse.content = stripStreamedIndexStamps(cloneDeep(latestMessage.content)); - /** Captured now, while it is still the retained prefix: a later resume - * sync replaces this array with the server's completion-local - * snapshot, after which its length no longer describes the offset. */ - editPrefixLength = initialResponse.content.length; - const { index, type, ...part } = editedContent; - if (initialResponse.content && index >= 0 && index < initialResponse.content.length) { - const contentPart = initialResponse.content[index]; - if (type === ContentTypes.THINK && contentPart.type === ContentTypes.THINK) { - contentPart[ContentTypes.THINK] = part[ContentTypes.THINK]; - delete contentPart.reasoning_label; - delete contentPart.reasoning_label_step_id; - delete contentPart.reasoning_label_attempts; - delete contentPart.reasoning_label_submitted_chars; - delete contentPart.reasoning_label_revision; - delete contentPart.reasoning_label_status; - } else if (type === ContentTypes.TEXT && contentPart.type === ContentTypes.TEXT) { - contentPart[ContentTypes.TEXT] = part[ContentTypes.TEXT]; - } - } - } else if (addedConvo && conversation) { - // Pre-populate placeholders for smooth UI - these will be overridden/extended - // as SSE events arrive with actual content, preserving the agent-based agentId - initialResponse.content = createDualMessageContent( - conversation, - addedConvo, - endpointsConfig, - config.modelSpecs, - ); - } else { - initialResponse.content = []; - } + if (turn.startsSubmitting) { setIsSubmitting(true); setShowStopButton(true); } - if (isContinued) { - currentMessages = currentMessages.filter((msg) => msg.messageId !== responseMessageId); - } - - const submissionMessages = regenerateShaped - ? getRegenerateSubmissionMessages({ - messages: currentMessages, - targetResponseMessage, - initialResponseId: initialResponse.messageId, - }) - : currentMessages; - const regenerateMessages = regenerateShaped ? [...currentMessages] : undefined; - - logger.log('message_state', initialResponse); - const submission: TSubmission = { - conversation: { - ...conversation, - ...(chatProjectId ? { chatProjectId } : {}), - ...(latestCodeApprovalMode != null ? { codeApprovalMode: latestCodeApprovalMode } : {}), - conversationId, - }, - endpointOption, - userMessage: { - ...currentMsg, - responseMessageId, - overrideParentMessageId: isRegenerate ? messageId : null, - }, - messages: submissionMessages, - regenerateMessages, - isEdited: isEditOrContinue, - isContinued, - isRegenerate: regenerateShaped, - ...(compact && { compact: true }), - initialResponse, - isTemporary, - ephemeralAgent, - editedContent, - editPrefixLength, - addedConvo, - manualSkills: manualSkills.length > 0 ? manualSkills : undefined, - codeApprovalMode, - codeEnvironmentMode, - codeWorkspaces, - clientRequestId, - recoverySteerId: overrideRecoverySteerId, - expectedPredecessorCreatedAt: overrideExpectedPredecessorCreatedAt, - queuedMessageOrigin: overrideQueuedMessageOrigin, - }; - /** Bind before publishing the optimistic tail, not after the first provider * event: a waiting request already owns a turn even when no usage exists. */ jotaiStore.set( @@ -928,7 +438,7 @@ export default function useChatFunctions({ initialResponse.messageId, ); - if (regenerateShaped) { + if (isRegenerateShaped) { setMessages([...submissionMessages, initialResponse]); focusRegeneratedResponse(initialResponse.parentMessageId); } else { @@ -954,48 +464,12 @@ export default function useChatFunctions({ messageId: currentMsg.messageId, parentMessageId: currentMsg.parentMessageId, isEdited: isEditOrContinue, - isRegenerate: regenerateShaped, + isRegenerate: isRegenerateShaped, isContinued, }); }; - const regenerate = ( - message: Partial>, - options?: { addedConvo?: TConversation | null }, - ) => { - const messages = getMessages(); - const parentMessageId = - message.isCreatedByUser === true ? message.messageId : message.parentMessageId; - const targetResponseMessageId = - message.isCreatedByUser === true ? undefined : message.messageId; - const parentMessage = messages?.find((element) => element.messageId == parentMessageId); - - if (parentMessage && parentMessage.isCreatedByUser) { - ask( - { ...parentMessage }, - { - isRegenerate: true, - addedConvo: options?.addedConvo ?? undefined, - targetResponseMessageId, - /** Carry the original user message's manual skill picks forward - * so the regenerated response is primed with the same skills. - * The compose-time atom was drained on the first submit; without - * this the model sees an unprimed turn even though the pills - * still show on the user bubble. */ - overrideManualSkills: parentMessage.manualSkills, - /** Carry the original user message's quoted excerpts forward so the - * regenerated response is sent the same referenced context. */ - overrideQuotes: parentMessage.quotes, - /** Replay the exact request-scoped reasoning selection used by this turn. */ - overrideReasoning: parentMessage.reasoningOverride ?? null, - }, - ); - } else { - console.error( - 'Failed to regenerate the message: parentMessage not found or not created by user.', - ); - } - }; + const regenerate = createRegenerate(ask, getMessages); return { ask, diff --git a/client/src/routes/ChatSettings.tsx b/client/src/routes/ChatSettings.tsx index 5e0f9318fc..131c74a469 100644 --- a/client/src/routes/ChatSettings.tsx +++ b/client/src/routes/ChatSettings.tsx @@ -8,8 +8,8 @@ import { ChatTransportContext, defaultChatTransport } from '~/Providers/ChatTran import { ChatSettingsContext } from '~/Providers/ChatSettingsContext'; import { duringRunActionAtom } from '~/store/duringRun'; import { useGetStartupConfig } from '~/data-provider'; +import store, { useGetEphemeralAgent } from '~/store'; import { useAuthContext } from '~/hooks/AuthContext'; -import store from '~/store'; /** Reads the conversation each mounted pane shows from the app's pane store, at call time. */ export function usePaneConversations(): ChatSettings['getPaneConversations'] { @@ -36,6 +36,8 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode const resetVisibleArtifacts = useResetRecoilState(store.visibleArtifacts); const saveDrafts = useRecoilValue(store.saveDrafts); const [isTemporary, setIsTemporary] = useRecoilState(store.isTemporary); + const getEphemeralAgent = useGetEphemeralAgent(); + const addedConversation = useRecoilValue(store.conversationByKeySelector(1)); const { data: startupConfig } = useGetStartupConfig(); const { user } = useAuthContext(); @@ -62,6 +64,8 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode saveDrafts, isTemporary, setIsTemporary, + getEphemeralAgent, + addedConversation, config, auth, getPaneConversations, @@ -73,6 +77,8 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode saveDrafts, isTemporary, setIsTemporary, + getEphemeralAgent, + addedConversation, config, auth, getPaneConversations, diff --git a/client/src/utils/messages.ts b/client/src/utils/messages.ts index 0236e588d3..8f79277dca 100644 --- a/client/src/utils/messages.ts +++ b/client/src/utils/messages.ts @@ -1,24 +1,21 @@ +import { QueryKeys, Constants, buildTree, ContentTypes } from 'librechat-data-provider'; import { - QueryKeys, - Constants, - buildTree, - ContentTypes, - isEphemeralAgentId, - getEphemeralSender, - appendAgentIdSuffix, - encodeEphemeralAgentId, -} from 'librechat-data-provider'; -import type { - Agents, - TMessage, - TConversation, - TEndpointsConfig, - TMessageContentParts, -} from 'librechat-data-provider'; + hasStreamStartFailed, + stripStreamedIndexStamps, + createDualMessageContent, + STREAM_START_FAILED_METADATA_KEY, +} from '@librechat/chat'; +import type { Agents, TMessage, TMessageContentParts } from 'librechat-data-provider'; import type { QueryClient } from '@tanstack/react-query'; import type { LocalizeFunction, TMessageProps } from '~/common'; -export const STREAM_START_FAILED_METADATA_KEY = 'streamStartFailed'; +export { + hasStreamStartFailed, + stripStreamedIndexStamps, + createDualMessageContent, + STREAM_START_FAILED_METADATA_KEY, +}; +export const TEXT_KEY_DIVIDER = '|||'; /** A locally submitted user row is not canonical until the server acknowledges it. */ export function isUnacknowledgedUserMessage(message: TMessage): boolean { @@ -367,32 +364,6 @@ export const preserveStreamedContentIdentity = ( return stamped ?? finalContent; }; -/** - * Drops the client-only `streamedIndex` stamps from a content array. An - * edited resubmission retains the settled prefix and appends the rerun's - * parts at the prefix LENGTH — a stamp at or above that length would collide - * with an appended part's key — so the retained prefix reverts to physical - * identity for the rerun. Returns the input untouched when nothing is - * stamped. - */ -export function stripStreamedIndexStamps(content: TMessageContentParts[]): TMessageContentParts[]; -export function stripStreamedIndexStamps(content: TMessage['content']): TMessage['content']; -export function stripStreamedIndexStamps(content: TMessage['content']): TMessage['content'] { - if (!content?.length) { - return content; - } - let changed = false; - const next = content.map((part) => { - if (part == null || part.streamedIndex === undefined) { - return part; - } - changed = true; - const { streamedIndex: _streamedIndex, ...rest } = part; - return rest as TMessageContentParts; - }); - return changed ? next : content; -} - /** Render-identity index for content-part keys: the streamed position stamped * by the final handler survives the sparse→compact swap; everything else keys * by the live index. Coordinate logic (edit indexes, phase bounds, cursor) @@ -408,9 +379,6 @@ export const getPartKeyIndex = (part: TMessageContentParts | undefined, idx: num export const isSubmittableMessage = (text?: string | null, fileCount = 0): boolean => (text ?? '').trim() !== '' || fileCount > 0; -export const hasStreamStartFailed = (message?: Pick | null): boolean => - message?.metadata?.[STREAM_START_FAILED_METADATA_KEY] === true; - export const markStreamStartFailedMetadata = ( metadata?: TMessage['metadata'], ): TMessage['metadata'] => ({ @@ -657,102 +625,6 @@ export const getMessageTimestamp = ( }; }; -/** - * Creates initial content parts for dual message display with agent-based grouping. - * Sets up primary and added agent content parts with agentId for column rendering. - * - * @param primaryConvo - The primary conversation configuration - * @param addedConvo - The added conversation configuration - * @param endpointsConfig - Endpoints configuration for getting model display labels - * @param modelSpecs - Model specs list for getting spec labels - * @returns Array of content parts with agentId for side-by-side rendering - */ -export const createDualMessageContent = ( - primaryConvo: TConversation, - addedConvo: TConversation, - endpointsConfig?: TEndpointsConfig, - modelSpecs?: { name: string; label?: string }[], -): TMessageContentParts[] => { - // For real agents (agent_id starts with "agent_"), use agent_id directly - // Otherwise create ephemeral ID from endpoint/model - let primaryAgentId: string; - if (primaryConvo.agent_id && !isEphemeralAgentId(primaryConvo.agent_id)) { - primaryAgentId = primaryConvo.agent_id; - } else { - const primaryEndpoint = primaryConvo.endpoint; - const primaryModel = primaryConvo.model ?? ''; - // Look up model spec for label fallback - const primarySpec = - primaryConvo.spec != null && primaryConvo.spec !== '' - ? modelSpecs?.find((s) => s.name === primaryConvo.spec) - : undefined; - const primarySender = getEphemeralSender({ - modelLabel: primaryConvo.modelLabel, - specLabel: primarySpec?.label, - modelDisplayLabel: primaryEndpoint - ? endpointsConfig?.[primaryEndpoint]?.modelDisplayLabel - : undefined, - }); - primaryAgentId = encodeEphemeralAgentId({ - endpoint: primaryEndpoint ?? '', - model: primaryModel, - sender: primarySender, - }); - } - - // Both agents run in parallel, so they share the same groupId - const parallelGroupId = 1; - - // Use empty type - these are just placeholders to establish agentId/groupId - // The actual type will be set when real content arrives from the server - const primaryContent = { - type: '' as const, - agentId: primaryAgentId, - groupId: parallelGroupId, - }; - - // For added agent, use agent_id if it's a real agent (starts with "agent_") - // Otherwise create ephemeral ID with index suffix - // Always append index suffix for added agent to distinguish from primary (even if same agent_id) - let addedAgentId: string; - if (addedConvo.agent_id && !isEphemeralAgentId(addedConvo.agent_id)) { - // Append suffix to distinguish from primary agent (matches ephemeral format) - addedAgentId = appendAgentIdSuffix(addedConvo.agent_id, 1); - } else { - const addedEndpoint = addedConvo.endpoint; - const addedModel = addedConvo.model ?? ''; - // Look up model spec for label fallback - const addedSpec = - addedConvo.spec != null && addedConvo.spec !== '' - ? modelSpecs?.find((s) => s.name === addedConvo.spec) - : undefined; - const addedSender = getEphemeralSender({ - modelLabel: addedConvo.modelLabel, - specLabel: addedSpec?.label, - modelDisplayLabel: addedEndpoint - ? endpointsConfig?.[addedEndpoint]?.modelDisplayLabel - : undefined, - }); - addedAgentId = encodeEphemeralAgentId({ - endpoint: addedEndpoint ?? '', - model: addedModel, - sender: addedSender, - index: 1, - }); - } - - // Use empty type - placeholder to establish agentId/groupId - const addedContent = { - type: '' as const, - agentId: addedAgentId, - groupId: parallelGroupId, - }; - - // Cast through unknown since these are placeholder objects with empty type - // that will be replaced by real content with proper types from the server - return [primaryContent, addedContent] as unknown as TMessageContentParts[]; -}; - export function areMessageFilesEqual(prevFiles?: TMessage['files'], nextFiles?: TMessage['files']) { if (prevFiles === nextFiles) { return true; diff --git a/packages/chat/src/core/__tests__/regenerate.spec.ts b/packages/chat/src/core/__tests__/regenerate.spec.ts new file mode 100644 index 0000000000..2f930a0704 --- /dev/null +++ b/packages/chat/src/core/__tests__/regenerate.spec.ts @@ -0,0 +1,78 @@ +import { Constants } from 'librechat-data-provider'; +import type { TMessage } from 'librechat-data-provider'; +import { + hasStreamStartFailed, + hasPendingAssistantParent, + getAppendParentMessageId, + getRegenerateTargetResponseMessage, + getRegenerateSubmissionMessages, + getPreliminaryRegenerateResponseMessageId, +} from '../regenerate'; + +const msg = (fields: Partial): TMessage => ({ text: '', ...fields }) as TMessage; + +describe('regenerate helpers', () => { + it('detects a message whose stream never started', () => { + expect(hasStreamStartFailed(msg({ metadata: { streamStartFailed: true } as never }))).toBe( + true, + ); + expect(hasStreamStartFailed(msg({}))).toBe(false); + expect(hasStreamStartFailed(null)).toBe(false); + }); + + it('appends under the user message before a failed start', () => { + const user = msg({ messageId: 'u', isCreatedByUser: true, parentMessageId: 'p' }); + const failed = msg({ + messageId: 'a', + parentMessageId: 'u', + metadata: { streamStartFailed: true } as never, + }); + expect(getAppendParentMessageId({ latestMessage: null, currentMessages: [] })).toBe( + Constants.NO_PARENT, + ); + expect( + getAppendParentMessageId({ latestMessage: failed, currentMessages: [user, failed] }), + ).toBe('p'); + expect(getAppendParentMessageId({ latestMessage: user, currentMessages: [user] })).toBe('u'); + }); + + it('treats an unsaved underscore response as a pending assistant parent', () => { + expect(hasPendingAssistantParent(msg({ messageId: 'r_' }))).toBe(true); + expect(hasPendingAssistantParent(msg({ messageId: 'r_', createdAt: 'x' }))).toBe(false); + expect(hasPendingAssistantParent(null)).toBe(false); + }); + + it('normalizes the preliminary response id', () => { + expect(getPreliminaryRegenerateResponseMessageId('abc__')).toBe('abc_'); + expect(getPreliminaryRegenerateResponseMessageId('')).toBeNull(); + }); + + it('prefers the targeted response, then the latest, then the newest sibling', () => { + const first = msg({ messageId: 'a1', parentMessageId: 'u' }); + const second = msg({ messageId: 'a2', parentMessageId: 'u' }); + const messages = [first, second]; + expect( + getRegenerateTargetResponseMessage({ + messages, + parentMessageId: 'u', + targetResponseMessageId: 'a1', + }), + ).toBe(first); + expect( + getRegenerateTargetResponseMessage({ messages, parentMessageId: 'u', latestMessage: first }), + ).toBe(first); + expect(getRegenerateTargetResponseMessage({ messages, parentMessageId: 'u' })).toBe(second); + expect(getRegenerateTargetResponseMessage({ messages })).toBeNull(); + }); + + it('removes only the regenerated subtree from the rendered list', () => { + const target = msg({ messageId: 'a1', parentMessageId: 'u' }); + const child = msg({ messageId: 'c1', parentMessageId: 'a1' }); + const sibling = msg({ messageId: 'a2', parentMessageId: 'u' }); + const result = getRegenerateSubmissionMessages({ + messages: [target, sibling, child], + targetResponseMessage: target, + }); + expect(result.map((m) => m.messageId)).toEqual(['a2']); + }); +}); diff --git a/packages/chat/src/core/content.ts b/packages/chat/src/core/content.ts new file mode 100644 index 0000000000..0d2be69f5b --- /dev/null +++ b/packages/chat/src/core/content.ts @@ -0,0 +1,134 @@ +import { + isEphemeralAgentId, + getEphemeralSender, + appendAgentIdSuffix, + encodeEphemeralAgentId, +} from 'librechat-data-provider'; +import type { + TMessage, + TConversation, + TEndpointsConfig, + TMessageContentParts, +} from 'librechat-data-provider'; + +/** + * Drops the client-only `streamedIndex` stamps from a content array. An + * edited resubmission retains the settled prefix and appends the rerun's + * parts at the prefix LENGTH; a stamp at or above that length would collide + * with an appended part's key, so the retained prefix reverts to physical + * identity for the rerun. Returns the input untouched when nothing is + * stamped. + */ +export function stripStreamedIndexStamps(content: TMessageContentParts[]): TMessageContentParts[]; +export function stripStreamedIndexStamps(content: TMessage['content']): TMessage['content']; +export function stripStreamedIndexStamps(content: TMessage['content']): TMessage['content'] { + if (!content?.length) { + return content; + } + let changed = false; + const next = content.map((part) => { + if (part == null || part.streamedIndex === undefined) { + return part; + } + changed = true; + const { streamedIndex: _streamedIndex, ...rest } = part; + return rest as TMessageContentParts; + }); + return changed ? next : content; +} + +/** + * Creates initial content parts for dual message display with agent-based grouping. + * Sets up primary and added agent content parts with agentId for column rendering. + * + * @param primaryConvo - The primary conversation configuration + * @param addedConvo - The added conversation configuration + * @param endpointsConfig - Endpoints configuration for getting model display labels + * @param modelSpecs - Model specs list for getting spec labels + * @returns Array of content parts with agentId for side-by-side rendering + */ +export const createDualMessageContent = ( + primaryConvo: TConversation, + addedConvo: TConversation, + endpointsConfig?: TEndpointsConfig, + modelSpecs?: { name: string; label?: string }[], +): TMessageContentParts[] => { + // For real agents (agent_id starts with "agent_"), use agent_id directly + // Otherwise create ephemeral ID from endpoint/model + let primaryAgentId: string; + if (primaryConvo.agent_id && !isEphemeralAgentId(primaryConvo.agent_id)) { + primaryAgentId = primaryConvo.agent_id; + } else { + const primaryEndpoint = primaryConvo.endpoint; + const primaryModel = primaryConvo.model ?? ''; + // Look up model spec for label fallback + const primarySpec = + primaryConvo.spec != null && primaryConvo.spec !== '' + ? modelSpecs?.find((s) => s.name === primaryConvo.spec) + : undefined; + const primarySender = getEphemeralSender({ + modelLabel: primaryConvo.modelLabel, + specLabel: primarySpec?.label, + modelDisplayLabel: primaryEndpoint + ? endpointsConfig?.[primaryEndpoint]?.modelDisplayLabel + : undefined, + }); + primaryAgentId = encodeEphemeralAgentId({ + endpoint: primaryEndpoint ?? '', + model: primaryModel, + sender: primarySender, + }); + } + + // Both agents run in parallel, so they share the same groupId + const parallelGroupId = 1; + + // Use empty type - these are just placeholders to establish agentId/groupId + // The actual type will be set when real content arrives from the server + const primaryContent = { + type: '' as const, + agentId: primaryAgentId, + groupId: parallelGroupId, + }; + + // For added agent, use agent_id if it's a real agent (starts with "agent_") + // Otherwise create ephemeral ID with index suffix + // Always append index suffix for added agent to distinguish from primary (even if same agent_id) + let addedAgentId: string; + if (addedConvo.agent_id && !isEphemeralAgentId(addedConvo.agent_id)) { + // Append suffix to distinguish from primary agent (matches ephemeral format) + addedAgentId = appendAgentIdSuffix(addedConvo.agent_id, 1); + } else { + const addedEndpoint = addedConvo.endpoint; + const addedModel = addedConvo.model ?? ''; + // Look up model spec for label fallback + const addedSpec = + addedConvo.spec != null && addedConvo.spec !== '' + ? modelSpecs?.find((s) => s.name === addedConvo.spec) + : undefined; + const addedSender = getEphemeralSender({ + modelLabel: addedConvo.modelLabel, + specLabel: addedSpec?.label, + modelDisplayLabel: addedEndpoint + ? endpointsConfig?.[addedEndpoint]?.modelDisplayLabel + : undefined, + }); + addedAgentId = encodeEphemeralAgentId({ + endpoint: addedEndpoint ?? '', + model: addedModel, + sender: addedSender, + index: 1, + }); + } + + // Use empty type - placeholder to establish agentId/groupId + const addedContent = { + type: '' as const, + agentId: addedAgentId, + groupId: parallelGroupId, + }; + + // Cast through unknown since these are placeholder objects with empty type + // that will be replaced by real content with proper types from the server + return [primaryContent, addedContent] as unknown as TMessageContentParts[]; +}; diff --git a/packages/chat/src/core/index.ts b/packages/chat/src/core/index.ts index fe752ab853..7695032540 100644 --- a/packages/chat/src/core/index.ts +++ b/packages/chat/src/core/index.ts @@ -11,3 +11,5 @@ export * from './reducer'; export * from './protocol'; export * from './transport'; export * from './usage'; +export * from './regenerate'; +export * from './content'; diff --git a/packages/chat/src/core/regenerate.ts b/packages/chat/src/core/regenerate.ts new file mode 100644 index 0000000000..2934eb15c5 --- /dev/null +++ b/packages/chat/src/core/regenerate.ts @@ -0,0 +1,143 @@ +import { Constants } from 'librechat-data-provider'; +import type { TMessage } from 'librechat-data-provider'; + +export const STREAM_START_FAILED_METADATA_KEY = 'streamStartFailed'; + +/** Whether the message records a start-generation request that never opened a stream. */ +export const hasStreamStartFailed = (message?: Pick | null): boolean => + message?.metadata?.[STREAM_START_FAILED_METADATA_KEY] === true; + +export const getAppendParentMessageId = ({ + latestMessage, + currentMessages, +}: { + latestMessage: TMessage | null; + currentMessages: TMessage[]; +}): string => { + if (!latestMessage) { + return Constants.NO_PARENT; + } + + if (!hasStreamStartFailed(latestMessage)) { + return latestMessage.messageId; + } + + const failedUserMessage = currentMessages.find( + (message) => message.messageId === latestMessage.parentMessageId, + ); + if (failedUserMessage?.isCreatedByUser !== true) { + return latestMessage.messageId; + } + + return failedUserMessage.parentMessageId ?? Constants.NO_PARENT; +}; + +export const hasPendingAssistantParent = (message: TMessage | null): boolean => + !!message?.messageId && + message.isCreatedByUser !== true && + message.messageId.endsWith('_') && + message.createdAt == null && + message.updatedAt == null && + !hasStreamStartFailed(message); + +type RegenerateTargetResponseArgs = { + messages: TMessage[]; + parentMessageId?: string | null; + targetResponseMessageId?: string | null; + latestMessage?: TMessage | null; +}; + +const isAssistantResponseForParent = ( + message: TMessage | null | undefined, + parentMessageId?: string | null, +): message is TMessage => + !!message && + !message.isCreatedByUser && + !!parentMessageId && + message.parentMessageId === parentMessageId; + +export function getPreliminaryRegenerateResponseMessageId( + responseMessageId?: string | null, +): string | null { + if (typeof responseMessageId !== 'string' || responseMessageId.length === 0) { + return null; + } + + return `${responseMessageId.replace(/_+$/, '')}_`; +} + +export function getRegenerateTargetResponseMessage({ + messages, + parentMessageId, + targetResponseMessageId, + latestMessage, +}: RegenerateTargetResponseArgs): TMessage | null { + if (!parentMessageId) { + return null; + } + + if (targetResponseMessageId) { + const targetResponse = messages.find( + (message) => + message.messageId === targetResponseMessageId && + isAssistantResponseForParent(message, parentMessageId), + ); + if (targetResponse) { + return targetResponse; + } + } + + if (isAssistantResponseForParent(latestMessage, parentMessageId)) { + return latestMessage; + } + + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i]; + if (isAssistantResponseForParent(message, parentMessageId)) { + return message; + } + } + + return null; +} + +export function getRegenerateSubmissionMessages({ + messages, + targetResponseMessage, + initialResponseId, +}: { + messages: TMessage[]; + targetResponseMessage?: TMessage | null; + initialResponseId?: string | null; +}): TMessage[] { + if (targetResponseMessage?.messageId) { + /** + * Remove the response being regenerated and its descendants only: NOT a + * flat `slice(0, targetIndex)`, which also drops unrelated sibling branches + * that merely sit later in the array. That collapse made the optimistic + * render briefly lose other branches mid-regenerate (visible flash, and the + * scroll jumping to the shrunken content). Keeping them holds the thread + * (and scroll) steady. This array is render-only; the server regenerates + * from `parentMessageId`, so removing by subtree never affects the payload. + */ + const removed = new Set([targetResponseMessage.messageId]); + let grew = true; + while (grew) { + grew = false; + for (const message of messages) { + const parentMessageId = message.parentMessageId; + if ( + parentMessageId != null && + removed.has(parentMessageId) && + !removed.has(message.messageId) + ) { + removed.add(message.messageId); + grew = true; + } + } + } + return messages.filter((message) => !removed.has(message.messageId)); + } + + return messages.filter((msg) => msg.messageId !== initialResponseId); +}