diff --git a/client/src/hooks/SSE/__tests__/frames.spec.ts b/client/src/hooks/SSE/__tests__/frames.spec.ts new file mode 100644 index 0000000000..10df6de5dd --- /dev/null +++ b/client/src/hooks/SSE/__tests__/frames.spec.ts @@ -0,0 +1,86 @@ +import { + StepEvents, + UsageEvents, + SteerEvents, + ContentTypes, + ApprovalEvents, + ActivityLabelEvents, + ReasoningLabelEvents, +} from 'librechat-data-provider'; +import type { ChatFrame } from 'librechat-data-provider'; +import { normalizeFrame } from '../transport'; + +/** Frames arrive as parsed JSON; the fixtures only carry the keys under test. */ +const frame = (value: object) => value as ChatFrame; + +describe('normalizeFrame', () => { + it('tags the control frames by their keys', () => { + const created = frame({ created: true, message: { messageId: 'user-1' }, streamId: 's-1' }); + const sync = frame({ sync: true, requestMessage: { messageId: 'user-1' } }); + const final = frame({ final: true, responseMessage: { messageId: 'response-1' } }); + + expect(normalizeFrame(created)).toEqual({ type: 'created', data: created }); + expect(normalizeFrame(sync)).toEqual({ type: 'sync', data: sync }); + expect(normalizeFrame(final)).toEqual({ type: 'final', data: final }); + }); + + it('keeps a reconcile frame final and leaves the decision to the consumer', () => { + const reconcile = frame({ final: true, reconcile: true, terminalStatus: 'error' }); + expect(normalizeFrame(reconcile)).toEqual({ type: 'final', data: reconcile }); + }); + + it('checks keys in the order the stream hooks always have', () => { + const finalWithEvent = frame({ final: true, event: StepEvents.ON_RUN_STEP }); + const createdWithSync = frame({ created: true, sync: true, message: {} }); + const eventWithType = frame({ event: 'title', type: ContentTypes.TEXT }); + + expect(normalizeFrame(finalWithEvent)?.type).toBe('final'); + expect(normalizeFrame(createdWithSync)?.type).toBe('created'); + expect(normalizeFrame(eventWithType)?.type).toBe('title'); + }); + + it('unwraps the named side channels to their payload', () => { + const cases: Array<[string, string]> = [ + ['attachment', 'attachment'], + [UsageEvents.ON_CONTEXT_USAGE, 'context_usage'], + [UsageEvents.ON_TOKEN_USAGE, 'token_usage'], + [ApprovalEvents.ON_PENDING_ACTION, 'pending_action'], + [SteerEvents.ON_STEER_APPLIED, 'steer_applied'], + [SteerEvents.ON_STEER_UPDATED, 'steer_updated'], + [ActivityLabelEvents.ON_ACTIVITY_LABEL, 'activity_label'], + [ReasoningLabelEvents.ON_REASONING_LABEL, 'reasoning_label'], + [ReasoningLabelEvents.ON_REASONING_LABEL_ATTEMPT, 'reasoning_label_attempt'], + ]; + + for (const [event, type] of cases) { + const data = { marker: event }; + expect(normalizeFrame(frame({ event, data }))).toEqual({ type, data }); + } + }); + + it('keeps the whole frame for titles, which the title handler reads as-is', () => { + const title = frame({ event: 'title', data: { conversationId: 'c-1', title: 'Hello' } }); + expect(normalizeFrame(title)).toEqual({ type: 'title', data: title }); + }); + + it('treats any other event as a step, including ones the client does not name yet', () => { + const delta = frame({ event: StepEvents.ON_MESSAGE_DELTA, data: { id: 'step-1' } }); + const unknown = frame({ event: 'on_future_event', data: {} }); + + expect(normalizeFrame(delta)).toEqual({ type: 'step', data: delta }); + expect(normalizeFrame(unknown)).toEqual({ type: 'step', data: unknown }); + }); + + it('tags legacy content-part and cumulative-text frames', () => { + const content = frame({ type: ContentTypes.TEXT, text: 'Hi', index: 0, messageId: 'r-1' }); + const text = frame({ message: true, text: 'Hi there', messageId: 'r-1' }); + + expect(normalizeFrame(content)).toEqual({ type: 'content', data: content }); + expect(normalizeFrame(text)).toEqual({ type: 'text', data: text }); + }); + + it('returns undefined for a frame with none of the keys', () => { + expect(normalizeFrame(frame({}))).toBeUndefined(); + expect(normalizeFrame(frame({ final: null, created: null }))).toBeUndefined(); + }); +}); diff --git a/client/src/hooks/SSE/__tests__/sse.spec.ts b/client/src/hooks/SSE/__tests__/sse.spec.ts new file mode 100644 index 0000000000..665545f298 --- /dev/null +++ b/client/src/hooks/SSE/__tests__/sse.spec.ts @@ -0,0 +1,262 @@ +import { request } from 'librechat-data-provider'; +import type { ChatEvent, TPayload } from 'librechat-data-provider'; +import { createSSETransport } from '../transport'; + +/** + * Drives the real `sse.js` module through a fake XHR, so the transport is + * tested against the library's actual framing and lifecycle. + */ +type XHRListener = (event: { currentTarget: FakeXHR }) => void; + +class FakeXHR { + static HEADERS_RECEIVED = 2; + status = 200; + readyState = 0; + response = ''; + responseText = ''; + withCredentials = false; + headers: Record = {}; + body: string | null = null; + private readonly listeners: Record = {}; + + addEventListener(type: string, listener: XHRListener) { + (this.listeners[type] ??= []).push(listener); + } + + open() {} + setRequestHeader(key: string, value: string) { + this.headers[key] = value; + } + + send(body: string | null) { + this.body = body; + } + + getAllResponseHeaders() { + return 'content-type: text/event-stream\r\n'; + } + + abort() { + this.emit('abort'); + } + + emit(type: string) { + for (const listener of this.listeners[type] ?? []) { + listener({ currentTarget: this }); + } + } + + receiveHeaders() { + this.readyState = FakeXHR.HEADERS_RECEIVED; + this.emit('readystatechange'); + } + + write(chunk: string) { + this.responseText += chunk; + this.response = this.responseText; + this.emit('progress'); + } +} + +const message = (data: object | string) => + `event: message\ndata: ${typeof data === 'string' ? data : JSON.stringify(data)}\n\n`; + +const turn = { + server: '/api/assistants/v2/chat', + payload: { text: 'Hello', conversationId: null, isContinued: false } as TPayload, +}; + +describe('createSSETransport', () => { + const OriginalXHR = global.XMLHttpRequest; + let xhrs: FakeXHR[]; + let events: ChatEvent[]; + let controller: AbortController; + + const current = () => xhrs[xhrs.length - 1]; + const send = (token = 'token-1') => + createSSETransport({ token }).send(turn, { + signal: controller.signal, + onEvent: (event) => events.push(event), + }); + + beforeEach(() => { + xhrs = []; + events = []; + controller = new AbortController(); + const FakeConstructor = jest.fn(() => { + const xhr = new FakeXHR(); + xhrs.push(xhr); + return xhr; + }); + global.XMLHttpRequest = Object.assign(FakeConstructor, { + HEADERS_RECEIVED: FakeXHR.HEADERS_RECEIVED, + }) as unknown as typeof XMLHttpRequest; + }); + + afterEach(() => { + global.XMLHttpRequest = OriginalXHR; + jest.restoreAllMocks(); + }); + + it('posts the payload with the bearer token', () => { + send(); + + expect(current().body).toBe(JSON.stringify(turn.payload)); + expect(current().headers).toEqual({ + 'Content-Type': 'application/json', + Authorization: 'Bearer token-1', + }); + }); + + it('emits open, then created, sync and final frames in wire order', () => { + send(); + current().receiveHeaders(); + current().write( + message({ created: true, message: { messageId: 'user-1' } }) + + message({ sync: true, responseMessage: { messageId: 'response-1' } }) + + message({ final: true, responseMessage: { messageId: 'response-1' } }), + ); + + expect(events.map((event) => event.type)).toEqual(['open', 'created', 'sync', 'final']); + expect(events[1]).toEqual({ + type: 'created', + data: { created: true, message: { messageId: 'user-1' } }, + }); + }); + + it('normalizes the named attachment event and the in-band attachment frame alike', () => { + const attachment = { file_id: 'file-1', filename: 'a.png', type: 'image/png' }; + send(); + current().write( + `event: attachment\ndata: ${JSON.stringify(attachment)}\n\n` + + message({ event: 'attachment', data: attachment }), + ); + + expect(events).toEqual([ + { type: 'attachment', data: attachment }, + { type: 'attachment', data: attachment }, + ]); + }); + + it('skips a malformed frame and keeps dispatching the frames behind it', () => { + const consoleError = jest.spyOn(console, 'error').mockImplementation(() => undefined); + send(); + current().write( + message('{"created": tru') + message({ final: true, responseMessage: { text: 'done' } }), + ); + + expect(events).toEqual([ + { type: 'final', data: { final: true, responseMessage: { text: 'done' } } }, + ]); + expect(consoleError).toHaveBeenCalledTimes(1); + }); + + it('emits the parsed body of a server error event', () => { + send(); + current().write(`event: error\ndata: ${JSON.stringify({ text: 'Rate limited' })}\n\n`); + + expect(events).toEqual([{ type: 'error', data: { text: 'Rate limited' } }]); + }); + + it('emits an error with undefined data when the failure body is not JSON', () => { + jest.spyOn(console, 'error').mockImplementation(() => undefined); + jest.spyOn(console, 'log').mockImplementation(() => undefined); + send(); + current().status = 500; + current().write('Internal Server Error'); + + expect(events).toEqual([{ type: 'error', data: undefined }]); + }); + + it('refreshes the token once on a 401 and retries on a new connection', async () => { + jest.spyOn(request, 'refreshToken').mockResolvedValue({ token: 'token-2' } as never); + const dispatchTokenUpdated = jest + .spyOn(request, 'dispatchTokenUpdatedEvent') + .mockImplementation(() => undefined); + send(); + current().status = 401; + current().write('Unauthorized'); + + await new Promise(process.nextTick); + + expect(xhrs).toHaveLength(2); + expect(current().headers.Authorization).toBe('Bearer token-2'); + expect(dispatchTokenUpdated).toHaveBeenCalledWith('token-2'); + expect(events).toEqual([]); + }); + + it('reports a second 401 instead of refreshing again', async () => { + const refreshToken = jest + .spyOn(request, 'refreshToken') + .mockResolvedValue({ token: 'token-2' } as never); + jest.spyOn(request, 'dispatchTokenUpdatedEvent').mockImplementation(() => undefined); + jest.spyOn(console, 'error').mockImplementation(() => undefined); + jest.spyOn(console, 'log').mockImplementation(() => undefined); + send(); + current().status = 401; + current().write('Unauthorized'); + await new Promise(process.nextTick); + + current().status = 401; + current().write('Unauthorized'); + await new Promise(process.nextTick); + + expect(refreshToken).toHaveBeenCalledTimes(1); + expect(xhrs).toHaveLength(2); + expect(events).toEqual([{ type: 'error', data: undefined }]); + }); + + it.each([ + ['succeeds', () => Promise.resolve({ token: 'token-2' })], + ['fails', () => Promise.reject(new Error('refresh failed'))], + ])( + 'cancels, and stays closed, when aborted while a 401 refresh %s', + async (_outcome, refresh) => { + jest.spyOn(console, 'log').mockImplementation(() => undefined); + jest.spyOn(request, 'refreshToken').mockImplementation(refresh as never); + const dispatchTokenUpdated = jest + .spyOn(request, 'dispatchTokenUpdatedEvent') + .mockImplementation(() => undefined); + send(); + current().status = 401; + current().write('Unauthorized'); + + controller.abort(); + await new Promise(process.nextTick); + + expect(xhrs).toHaveLength(1); + expect(dispatchTokenUpdated).not.toHaveBeenCalled(); + expect(events).toEqual([{ type: 'abort' }]); + }, + ); + + it('emits abort when the caller aborts mid-stream, then goes quiet', () => { + send(); + current().receiveHeaders(); + current().write(message({ created: true, message: {} })); + const xhr = current(); + + controller.abort(); + xhr.write(message({ final: true })); + + expect(events.map((event) => event.type)).toEqual(['open', 'created', 'abort']); + }); + + it('does not emit abort for a stream that already closed', () => { + send(); + current().write(message({ final: true })); + current().emit('load'); + + controller.abort(); + + expect(events.map((event) => event.type)).toEqual(['final']); + }); + + it('never opens a connection for an already-aborted signal', () => { + controller.abort(); + send(); + + expect(xhrs).toHaveLength(0); + expect(events).toEqual([]); + }); +}); diff --git a/client/src/hooks/SSE/transport/frames.ts b/client/src/hooks/SSE/transport/frames.ts new file mode 100644 index 0000000000..cbf6149482 --- /dev/null +++ b/client/src/hooks/SSE/transport/frames.ts @@ -0,0 +1,84 @@ +import { + UsageEvents, + SteerEvents, + ApprovalEvents, + ActivityLabelEvents, + ReasoningLabelEvents, +} from 'librechat-data-provider'; +import type { + ChatFrame, + ChatEvent, + ChatSyncFrame, + ChatTextFrame, + ChatEventFrame, + ChatFinalFrame, + ChatContentFrame, + ChatCreatedFrame, +} from 'librechat-data-provider'; + +/** The keys the server discriminates frames by, in the order they are checked. */ +type FrameKeys = { + final?: unknown; + created?: unknown; + event?: unknown; + sync?: unknown; + type?: unknown; + message?: unknown; +}; + +function normalizeEventFrame(frame: ChatEventFrame): ChatEvent { + switch (frame.event) { + case 'title': + return { type: 'title', data: frame }; + case 'attachment': + return { type: 'attachment', data: frame.data }; + case UsageEvents.ON_CONTEXT_USAGE: + return { type: 'context_usage', data: frame.data }; + case UsageEvents.ON_TOKEN_USAGE: + return { type: 'token_usage', data: frame.data }; + case ApprovalEvents.ON_PENDING_ACTION: + return { type: 'pending_action', data: frame.data }; + case SteerEvents.ON_STEER_APPLIED: + return { type: 'steer_applied', data: frame.data }; + case SteerEvents.ON_STEER_UPDATED: + return { type: 'steer_updated', data: frame.data }; + case ActivityLabelEvents.ON_ACTIVITY_LABEL: + return { type: 'activity_label', data: frame.data }; + case ReasoningLabelEvents.ON_REASONING_LABEL: + return { type: 'reasoning_label', data: frame.data }; + case ReasoningLabelEvents.ON_REASONING_LABEL_ATTEMPT: + return { type: 'reasoning_label_attempt', data: frame.data }; + default: + return { type: 'step', data: frame }; + } +} + +/** + * Tags one `message` frame by the keys the server wrote, checked in the order + * the stream hooks have always checked them: a frame carrying both `final` + * and `event` is final. An `event` the client does not name is a step event, + * so the step handler keeps seeing new graph events as it did before. A frame + * with none of the keys returns `undefined`. + */ +export function normalizeFrame(frame: ChatFrame): ChatEvent | undefined { + const keys: FrameKeys = frame; + if (keys.final != null) { + return { type: 'final', data: frame as ChatFinalFrame }; + } + if (keys.created != null) { + return { type: 'created', data: frame as ChatCreatedFrame }; + } + if (keys.event != null) { + return normalizeEventFrame(frame as ChatEventFrame); + } + if (keys.sync != null) { + return { type: 'sync', data: frame as ChatSyncFrame }; + } + if (keys.type != null) { + return { type: 'content', data: frame as ChatContentFrame }; + } + if (keys.message != null) { + return { type: 'text', data: frame as ChatTextFrame }; + } + return undefined; +} diff --git a/client/src/hooks/SSE/transport/index.ts b/client/src/hooks/SSE/transport/index.ts new file mode 100644 index 0000000000..95081978c2 --- /dev/null +++ b/client/src/hooks/SSE/transport/index.ts @@ -0,0 +1,2 @@ +export { normalizeFrame } from './frames'; +export { createSSETransport } from './sse'; diff --git a/client/src/hooks/SSE/transport/sse.ts b/client/src/hooks/SSE/transport/sse.ts new file mode 100644 index 0000000000..a0e4c7e1f5 --- /dev/null +++ b/client/src/hooks/SSE/transport/sse.ts @@ -0,0 +1,119 @@ +import { SSE } from 'sse.js'; +import { request } from 'librechat-data-provider'; +import type { ChatFrame, ChatTransport, ChatErrorData } from 'librechat-data-provider'; +import { normalizeFrame } from './frames'; + +type StreamErrorEvent = MessageEvent & { responseCode?: number }; + +const jsonHeaders = (token?: string) => ({ + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, +}); + +/** + * POSTs the turn with `sse.js` and normalizes what comes back. Owns the wire: + * frame parsing, named `attachment`/`error` events, and one token refresh and + * retry on a 401. A frame that is not JSON is logged and skipped, so the + * frames behind it in the same chunk still dispatch. + */ +export function createSSETransport({ token }: { token?: string }): ChatTransport { + return { + send({ server, payload }, { signal, onEvent }) { + if (signal.aborted) { + return; + } + + const sse = new SSE(server, { + payload: JSON.stringify(payload), + headers: jsonHeaders(token), + }); + + sse.addEventListener('open', () => { + onEvent({ type: 'open' }); + }); + + sse.addEventListener('attachment', (e: MessageEvent) => { + try { + onEvent({ type: 'attachment', data: JSON.parse(e.data) }); + } catch (error) { + console.error(error); + } + }); + + sse.addEventListener('message', (e: MessageEvent) => { + let frame: ChatFrame; + try { + frame = JSON.parse(e.data); + } catch (error) { + console.error('Skipping malformed stream frame:', error); + return; + } + const event = normalizeFrame(frame); + if (event) { + onEvent(event); + } + }); + + let refreshed = false; + /** sse.js marks the connection closed on a 401, but the turn is still + * live until the retry settles, so an abort meanwhile must still cancel. */ + let refreshing = false; + sse.addEventListener('error', async (e: StreamErrorEvent) => { + if (e.responseCode === 401 && !refreshed) { + refreshed = true; + refreshing = true; + try { + const refreshResponse = await request.refreshToken(); + refreshing = false; + if (signal.aborted) { + return; + } + const refreshedToken = refreshResponse?.token ?? ''; + if (!refreshedToken) { + throw new Error('Token refresh failed.'); + } + sse.headers = jsonHeaders(refreshedToken); + request.dispatchTokenUpdatedEvent(refreshedToken); + sse.stream(); + return; + } catch (error) { + refreshing = false; + /* token refresh failed, continue handling the original 401 */ + console.log(error); + } + if (signal.aborted) { + return; + } + } + + let data: ChatErrorData | null | undefined; + try { + data = JSON.parse(e.data); + } catch (error) { + console.error(error); + console.log(e); + } + onEvent({ type: 'error', data }); + }); + + sse.addEventListener('cancel', () => { + onEvent({ type: 'abort' }); + }); + + signal.addEventListener( + 'abort', + () => { + const wasOpen = sse.readyState <= 1 || refreshing; + sse.close(); + if (wasOpen) { + // @ts-expect-error sse.js declares dispatchEvent as (type, listener); it takes an event + sse.dispatchEvent(new Event('cancel')); + } + }, + { once: true }, + ); + + sse.stream(); + }, + }; +} diff --git a/client/src/hooks/SSE/useContentHandler.ts b/client/src/hooks/SSE/useContentHandler.ts index 78840fbd38..84309bf886 100644 --- a/client/src/hooks/SSE/useContentHandler.ts +++ b/client/src/hooks/SSE/useContentHandler.ts @@ -8,7 +8,7 @@ import type { ImageFile, ContentPart, PartMetadata, - TContentData, + ChatContentFrame, EventSubmission, TMessageContentParts, } from 'librechat-data-provider'; @@ -20,7 +20,7 @@ type TUseContentHandler = { }; type TContentHandler = { - data: TContentData; + data: ChatContentFrame; submission: EventSubmission; }; diff --git a/client/src/hooks/SSE/useEventHandlers.ts b/client/src/hooks/SSE/useEventHandlers.ts index f40b60d3f5..45d2fd1282 100644 --- a/client/src/hooks/SSE/useEventHandlers.ts +++ b/client/src/hooks/SSE/useEventHandlers.ts @@ -19,6 +19,7 @@ import type { TConversation, TStartupConfig, EventSubmission, + ChatCreatedFrame, TMessageContentParts, } from 'librechat-data-provider'; import type { InfiniteData } from '@tanstack/react-query'; @@ -270,8 +271,8 @@ const CONNECTION_ERROR_TEXT = 'Error connecting to server, try refreshing the pa /** * The parts the in-flight response has streamed so far: the transcript's tail (a user row there * means no response was placed), without the holes an interrupted stream leaves. Whether anything - * streamed is judged without the slots that never received content — a comparison run's - * `type: ''` placeholders, a text or think part opened before its first delta — but once something + * streamed is judged without the slots that never received content (a comparison run's + * `type: ''` placeholders, a text or think part opened before its first delta), but once something * did, those slots stay: a placeholder is what keeps a comparison lane's layout and attribution * when only the other lane produced output. The parts carry the render identity they streamed * under, as the final path stamps it, so the settled row does not remount. @@ -328,7 +329,7 @@ const createErrorMessage = ({ 'Error cancelling request'; const streamedContent = getStreamedContent(latestMessage); if (latestMessage?.conversationId && latestMessage.messageId && streamedContent.length > 0) { - /** The row keeps the envelope it streamed under — author, model, icon, creation time — and + /** The row keeps the envelope it streamed under (author, model, icon, creation time) and * takes from the failure only what describes the failure: a server payload names `System` * as its sender and the schema dates a fresh envelope now, neither of which applies to a * response that is merely gaining a part. */ @@ -575,7 +576,7 @@ export default function useEventHandlers({ /** Cleared on every terminal path below: the elapsed anchor must not outlive * its generation, or a later externally-started run attached at this index * would inherit a stale baseline. Navigation teardown deliberately does not - * clear it — a reattach to a still-live run keeps its original start. */ + * clear it: a reattach to a still-live run keeps its original start. */ const setSubmissionStart = useSetRecoilState(store.submissionStartFamily(runIndex)); const { mutate: reconcileCodeDecision } = useReconcileConversationCodeEnvironmentMutation(setConversation); @@ -620,7 +621,7 @@ export default function useEventHandlers({ const location = useLocation(); /** Re-queue the turn's quoted excerpts when an early abort restores the draft, - * so retrying the restored message still sends the references — the pending + * so retrying the restored message still sends the references; the pending * queue was already drained on submit. */ const restorePendingQuotes = useRecoilCallback( ({ set }) => @@ -637,7 +638,7 @@ export default function useEventHandlers({ const { token } = useAuthContext(); const { contentHandler, resetContentHandler } = useContentHandler({ setMessages, getMessages }); - /** `refetchType: 'all'` so cached-but-unmounted skill queries refresh too — + /** `refetchType: 'all'` so cached-but-unmounted skill queries refresh too: * they opt out of `refetchOnMount`, so a plain invalidation would leave * the Skills panel stale until a manual refresh. */ const onSkillAuthoringComplete = useCallback(() => { @@ -675,7 +676,7 @@ export default function useEventHandlers({ * Historical subagent dialogs rehydrate from the persisted * `subagent_content` on each `tool_call` (written by the backend * at message-save time), so clearing live atoms on switch - * doesn't lose any viewable history — it just keeps `atomFamily` + * doesn't lose any viewable history; it just keeps `atomFamily` * bounded across multi-conversation sessions. * * Rule: reset on real conversation switches, but preserve atoms for @@ -806,7 +807,7 @@ export default function useEventHandlers({ * Filtering it out and re-appending at the tail would order any of its * already-present children (abandoned responses from preempted * attempts) before their parent, and the message tree hoists such rows - * into phantom root branches — a folded thread. */ + * into phantom root branches, a folded thread. */ const userIndex = _messages.findIndex((msg) => msg.messageId === userMessage.messageId); const messages = userIndex >= 0 @@ -879,11 +880,11 @@ export default function useEventHandlers({ const focusRegeneratedResponse = useFocusRegeneratedResponse(); const createdHandler = useCallback( - (data: TResData, submission: EventSubmission) => { + (data: ChatCreatedFrame, submission: EventSubmission) => { const { messages, userMessage, isRegenerate = false, isTemporary = false } = submission; /** * The spread carries `manualSkills` through from - * `submission.initialResponse` — `useChatFunctions` seeds the field + * `submission.initialResponse`: `useChatFunctions` seeds the field * there at construction so the assistant placeholder already has it * by the time this handler fires. Subsequent `useStepHandler` * spreads and `updateContent` spreads preserve it, and @@ -1100,7 +1101,7 @@ export default function useEventHandlers({ const isNewConvo = conversation.conversationId !== submissionConvo.conversationId; - // Skip temporary conversations — the server never generates titles for them. + // Skip temporary conversations; the server never generates titles for them. if (isNewConvo && conversation.conversationId && !_isTemporary) { queueTitleGeneration(conversation.conversationId); } @@ -1200,7 +1201,7 @@ export default function useEventHandlers({ /** A title applied locally (e.g. an immediate-mode title fetched while the * response was still streaming) must survive the final event, whose * `conversation` was built before the title was saved and so carries no - * title yet — otherwise the chat reverts to "New Chat" until reload. This + * title yet, otherwise the chat reverts to "New Chat" until reload. This * holds for a stopped turn too: the server persists a title that finished * generating before the Stop, so the local one stays in sync. */ if (setConversation && isAddedRequest !== true) { @@ -1435,7 +1436,7 @@ export default function useEventHandlers({ }); /** A compaction has no user row: its `userMessage` slot names the leaf * the turn hangs off, which `messages` already holds. Writing it here - * would duplicate that id as an empty, self-parented user message — + * would duplicate that id as an empty, self-parented user message, * a phantom root the thread then folds into. */ setMessages( submission.compact === true diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.ts index ca3eeb0f2a..e7899b0fbc 100644 --- a/client/src/hooks/SSE/useSSE.ts +++ b/client/src/hooks/SSE/useSSE.ts @@ -1,31 +1,28 @@ import { useEffect, useState } from 'react'; import { v4 } from 'uuid'; -import { SSE } from 'sse.js'; import { useStore } from 'jotai'; import { useSetRecoilState } from 'recoil'; -import { - request, - UsageEvents, - StepEvents, - createPayload, - ApprovalEvents, - removeNullishValues, -} from 'librechat-data-provider'; +import { StepEvents, createPayload, removeNullishValues } from 'librechat-data-provider'; import type { Agents, TMessage, TPayload, + ChatEvent, + TAttachment, TSubmission, + ChatErrorData, + ChatFinalFrame, EventSubmission, } from 'librechat-data-provider'; import type { EventHandlerParams } from './useEventHandlers'; -import type { TResData } from '~/common'; +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 { useGetStartupConfig, useGetUserBalance } from '~/data-provider'; import { useAuthContext } from '~/hooks/AuthContext'; import useEventHandlers from './useEventHandlers'; +import { createSSETransport } from './transport'; import useUsageHandler from './useUsageHandler'; import store from '~/store'; @@ -34,6 +31,12 @@ type ChatHelpers = Pick< 'setMessages' | 'getMessages' | 'setConversation' | 'setIsSubmitting' | 'newConversation' >; +type EventHandlers = ReturnType; +type TSyncData = Parameters[0]; +/** The handlers predate the wire types and accept a narrower payload; these + * frames reached them unchanged before the transport existed. */ +type TStepEvent = Parameters[0]; + export default function useSSE( submission: TSubmission | null, chatHelpers: ChatHelpers, @@ -117,123 +120,50 @@ export default function useSSE( let { payload } = payloadData; payload = removeNullishValues(payload) as TPayload; - let textIndex = null; clearStepMaps(); bindResponse(currentSubmission); - const sse = new SSE(payloadData.server, { - payload: JSON.stringify(payload), - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, - }); - - sse.addEventListener('attachment', (e: MessageEvent) => { + const handleFinal = (data: ChatFinalFrame) => { + /** A queued delta flush reading the older streaming copy must never + * land on top of the server-final write. */ + cancelPendingDeltaFlush(); + clearComposerDrafts(runIndex, submission.conversation?.conversationId, { + includeNewChatDraft: startedAsNewConversation(submission), + }); try { - const data = JSON.parse(e.data); - attachmentHandler({ data, submission: currentSubmission }); + finalHandler(data as TFinalResData, currentSubmission); + finalizeUsage(data, currentSubmission); } catch (error) { - console.error(error); + console.error('Error in finalHandler:', error); + setIsSubmitting(false); + setShowStopButton(false); } - }); + (startupConfig?.balance?.enabled ?? false) && balanceQuery.refetch(); + console.log('final', data); + }; - sse.addEventListener('message', (e: MessageEvent) => { - const data = JSON.parse(e.data); - - if (data.final != null) { - /** A queued delta flush reading the older streaming copy must never - * land on top of the server-final write. */ - cancelPendingDeltaFlush(); - clearComposerDrafts(runIndex, submission.conversation?.conversationId, { - includeNewChatDraft: startedAsNewConversation(submission), - }); - try { - finalHandler(data, currentSubmission); - finalizeUsage(data, currentSubmission); - } catch (error) { - console.error('Error in finalHandler:', error); - setIsSubmitting(false); - setShowStopButton(false); - } - (startupConfig?.balance?.enabled ?? false) && balanceQuery.refetch(); - console.log('final', data); - return; - } else if (data.created != null) { - const runId = v4(); - setActiveRunId(runId); - const userMessage = { ...currentSubmission.userMessage, ...data.message }; - adoptResponse( - userMessage, - buildCreatedInitialResponse({ - ...currentSubmission, - userMessage, - }), - ); - createdHandler(data, currentSubmission); - } else if (data.event === 'title') { - titleHandler(data); - } else if (data.event === UsageEvents.ON_CONTEXT_USAGE) { - contextHandler(data.data, currentSubmission); - } else if (data.event === UsageEvents.ON_TOKEN_USAGE) { - usageHandler(data.data, currentSubmission); - } else if (data.event === ApprovalEvents.ON_PENDING_ACTION) { - /** The pause card must attach to the same message state the stream - * produced — apply any queued delta before reading the cache. */ - flushPendingDeltas(); - const pendingAction = data.data as Agents.PendingAction; - const pendingConversationId = - pendingAction.conversationId ?? submission.conversation?.conversationId; - if (pendingConversationId) { - jotaiStore.set(pendingApprovalActionFamily(pendingConversationId), pendingAction); - } - const messages = getMessages() ?? []; - const index = findPendingActionMessageIndex(messages, pendingAction); - if (index >= 0) { - const updated = applyPendingAction(messages[index], pendingAction); - if (updated !== messages[index]) { - const nextMessages = [...messages]; - nextMessages[index] = updated; - setMessages(nextMessages); - } - } - } else if (data.event != null) { - if ( - data.event === StepEvents.ON_MESSAGE_DELTA || - data.event === StepEvents.ON_REASONING_DELTA - ) { - tapStream(data.data, currentSubmission); - } - stepHandler(data, currentSubmission); - } else if (data.sync != null) { - const runId = v4(); - setActiveRunId(runId); - /* synchronize messages to Assistants API as well as with real DB ID's */ - adoptResponse(data.requestMessage, data.responseMessage); - syncHandler(data, currentSubmission); - } else if (data.type != null) { - const { text, index } = data; - if (text != null && index !== textIndex) { - textIndex = index; - } - - tapContent(text, currentSubmission); - contentHandler({ data, submission: currentSubmission }); - } else if (data.message != null) { - const text = data.text ?? data.response; - - adoptResponse({}, { parentMessageId: data.parentMessageId, messageId: data.messageId }); - - /** Legacy non-agent streams (handleText) send cumulative text here, - * not via the content path — feed it to the live estimate too */ - tapContent(text, currentSubmission); - messageHandler(text, currentSubmission); + const handlePendingAction = (pendingAction: Agents.PendingAction) => { + /** The pause card must attach to the same message state the stream + * produced, so apply any queued delta before reading the cache. */ + flushPendingDeltas(); + const pendingConversationId = + pendingAction.conversationId ?? submission.conversation?.conversationId; + if (pendingConversationId) { + jotaiStore.set(pendingApprovalActionFamily(pendingConversationId), pendingAction); } - }); + const messages = getMessages() ?? []; + const index = findPendingActionMessageIndex(messages, pendingAction); + if (index >= 0) { + const updated = applyPendingAction(messages[index], pendingAction); + if (updated !== messages[index]) { + const nextMessages = [...messages]; + nextMessages[index] = updated; + setMessages(nextMessages); + } + } + }; - sse.addEventListener('open', () => { - setAbortScroll(false); - console.log('connection is opened'); - }); - - sse.addEventListener('cancel', async () => { + const handleAbort = async () => { /** FLUSH (not cancel): the abort below synthesizes the partial response * from the cache, so the last queued tokens must land first. */ flushPendingDeltas(); @@ -251,7 +181,7 @@ export default function useSSE( const latestMessages = getMessages(); const conversationId = latestMessages?.[latestMessages.length - 1]?.conversationId; /** Attribute usage billed before the stop to the partial response (the - * branch tail), then reset pending — so it neither drops nor leaks into + * branch tail), then reset pending, so it neither drops nor leaks into * the next response. Falls back to a plain reset when no response exists. */ const tail = latestMessages?.[latestMessages.length - 1]; const partialResponseId = @@ -271,42 +201,13 @@ export default function useSSE( setIsSubmitting(false); setShowStopButton(false); } - }); - - sse.addEventListener('error', async (e: MessageEvent) => { - /* @ts-ignore */ - if (e.responseCode === 401) { - /* token expired, refresh and retry */ - try { - const refreshResponse = await request.refreshToken(); - const token = refreshResponse?.token ?? ''; - if (!token) { - throw new Error('Token refresh failed.'); - } - sse.headers = { - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}`, - }; - - request.dispatchTokenUpdatedEvent(token); - sse.stream(); - return; - } catch (error) { - /* token refresh failed, continue handling the original 401 */ - console.log(error); - } - } + }; + const handleError = (data: ChatErrorData | null | undefined) => { console.log('error in server stream.'); (startupConfig?.balance?.enabled ?? false) && balanceQuery.refetch(); resetLive(currentSubmission); - - let data: TResData | undefined = undefined; - try { - data = JSON.parse(e.data) as TResData; - } catch (error) { - console.error(error); - console.log(e); + if (data === undefined) { setIsSubmitting(false); } @@ -314,23 +215,105 @@ export default function useSSE( * the last queued tokens must land before it is synthesized. */ flushPendingDeltas(); errorHandler({ - data, + data: data as TResData | undefined, submission: currentSubmission, }); - }); + }; - setIsSubmitting(true); - sse.stream(); + const onEvent = (event: ChatEvent) => { + switch (event.type) { + case 'open': + setAbortScroll(false); + console.log('connection is opened'); + return; + case 'final': + handleFinal(event.data); + return; + case 'created': { + const runId = v4(); + setActiveRunId(runId); + const userMessage = { ...currentSubmission.userMessage, ...event.data.message }; + adoptResponse( + userMessage, + buildCreatedInitialResponse({ + ...currentSubmission, + userMessage, + }), + ); + createdHandler(event.data, currentSubmission); + return; + } + case 'title': + titleHandler(event.data); + return; + case 'attachment': + attachmentHandler({ data: event.data as TAttachment, submission: currentSubmission }); + return; + case 'context_usage': + contextHandler(event.data, currentSubmission); + return; + case 'token_usage': + usageHandler(event.data, currentSubmission); + return; + case 'pending_action': + handlePendingAction(event.data); + return; + case 'step': + if ( + event.data.event === StepEvents.ON_MESSAGE_DELTA || + event.data.event === StepEvents.ON_REASONING_DELTA + ) { + tapStream(event.data.data, currentSubmission); + } + stepHandler(event.data as TStepEvent, currentSubmission); + return; + case 'sync': { + const runId = v4(); + setActiveRunId(runId); + /* synchronize messages to Assistants API as well as with real DB ID's */ + adoptResponse(event.data.requestMessage ?? {}, event.data.responseMessage ?? {}); + syncHandler(event.data as TSyncData, currentSubmission); + return; + } + case 'content': { + const text = 'text' in event.data ? event.data.text : undefined; + tapContent(text, currentSubmission); + contentHandler({ data: event.data, submission: currentSubmission }); + return; + } + case 'text': { + const { data } = event; + const text = data.text ?? data.response; - return () => { - const isCancelled = sse.readyState <= 1; - sse.close(); - if (isCancelled) { - const e = new Event('cancel'); - /* @ts-ignore */ - sse.dispatchEvent(e); + adoptResponse({}, { parentMessageId: data.parentMessageId, messageId: data.messageId }); + + /** Legacy non-agent streams (handleText) send cumulative text here, + * not via the content path; feed it to the live estimate too */ + tapContent(text, currentSubmission); + messageHandler(text, currentSubmission); + return; + } + case 'error': + handleError(event.data); + return; + case 'abort': + void handleAbort(); + return; + default: + /** Steering and label events belong to agent runs, which stream + * through `useResumableSSE`; this path never receives them. */ + return; } }; + + const controller = new AbortController(); + setIsSubmitting(true); + createSSETransport({ token }).send( + { server: payloadData.server, payload }, + { signal: controller.signal, onEvent }, + ); + + return () => controller.abort(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [submission]); }