Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🧩 fix: Apply Every Entry of Multi-Part Stream Deltas (#14502)

Danny Avila3d2f3a6a18c9
diff --git a/client/src/hooks/SSE/__tests__/useStepHandler.spec.ts b/client/src/hooks/SSE/__tests__/useStepHandler.spec.ts
index 288e82ad3c..ac8c7c6240 100644
--- a/client/src/hooks/SSE/__tests__/useStepHandler.spec.ts
+++ b/client/src/hooks/SSE/__tests__/useStepHandler.spec.ts
@@ -1197,6 +1197,45 @@ describe('useStepHandler', () => {
);
});
+ it('applies every entry of a multi-part text delta in order', () => {
+ const responseMessage = createResponseMessage();
+ mockGetMessages.mockReturnValue([responseMessage]);
+
+ const { result } = renderHook(() => useStepHandler(createHookParams()));
+
+ const runStep = createRunStep();
+ const submission = createSubmission();
+
+ act(() => {
+ result.current.stepHandler({ event: StepEvents.ON_RUN_STEP, data: runStep }, submission);
+ });
+
+ act(() => {
+ result.current.stepHandler(
+ {
+ event: StepEvents.ON_MESSAGE_DELTA,
+ data: {
+ id: 'step-1',
+ delta: {
+ content: [
+ { type: ContentTypes.TEXT, text: 'Hello ' },
+ { type: ContentTypes.TEXT, text: 'streaming ' },
+ { type: ContentTypes.TEXT, text: 'world' },
+ ],
+ },
+ },
+ },
+ submission,
+ );
+ });
+
+ const lastCall = mockSetMessages.mock.calls[mockSetMessages.mock.calls.length - 1][0];
+ const responseMsg = lastCall[lastCall.length - 1];
+ expect(responseMsg.content).toContainEqual(
+ expect.objectContaining({ type: ContentTypes.TEXT, text: 'Hello streaming world' }),
+ );
+ });
+
it('coalesces multiple deltas into a single flush per animation frame', () => {
const rafQueue: FrameRequestCallback[] = [];
(window.requestAnimationFrame as jest.Mock).mockImplementation((cb: FrameRequestCallback) => {
@@ -1405,6 +1444,47 @@ describe('useStepHandler', () => {
expect.objectContaining({ type: ContentTypes.THINK, think: 'First thought' }),
);
});
+
+ it('applies every entry of a multi-part reasoning delta in order', () => {
+ const responseMessage = createResponseMessage();
+ mockGetMessages.mockReturnValue([responseMessage]);
+
+ const { result } = renderHook(() => useStepHandler(createHookParams()));
+
+ const runStep = createRunStep();
+ const submission = createSubmission();
+
+ act(() => {
+ result.current.stepHandler({ event: StepEvents.ON_RUN_STEP, data: runStep }, submission);
+ });
+
+ act(() => {
+ result.current.stepHandler(
+ {
+ event: StepEvents.ON_REASONING_DELTA,
+ data: {
+ id: 'step-1',
+ delta: {
+ content: [
+ { type: ContentTypes.THINK, think: 'First reasoning block. ' },
+ { type: ContentTypes.THINK, think: 'Second reasoning block.' },
+ ],
+ },
+ },
+ },
+ submission,
+ );
+ });
+
+ const lastCall = mockSetMessages.mock.calls[mockSetMessages.mock.calls.length - 1][0];
+ const responseMsg = lastCall[lastCall.length - 1];
+ expect(responseMsg.content).toContainEqual(
+ expect.objectContaining({
+ type: ContentTypes.THINK,
+ think: 'First reasoning block. Second reasoning block.',
+ }),
+ );
+ });
});
describe('on_run_step_delta event', () => {
diff --git a/client/src/hooks/SSE/useStepHandler.ts b/client/src/hooks/SSE/useStepHandler.ts
index 2969501a62..0055ee9664 100644
--- a/client/src/hooks/SSE/useStepHandler.ts
+++ b/client/src/hooks/SSE/useStepHandler.ts
@@ -894,29 +894,38 @@ export default function useStepHandler({
const response = messageMap.current.get(responseMessageId);
if (response && messageDelta.delta.content) {
- const contentPart = Array.isArray(messageDelta.delta.content)
- ? messageDelta.delta.content[0]
- : messageDelta.delta.content;
-
- if (contentPart == null) {
- return;
+ /** A delta may carry several parts (e.g. Google server-side tool
+ * chunks) — every entry must be applied, in order, or streamed
+ * text is silently dropped. */
+ const contentParts = Array.isArray(messageDelta.delta.content)
+ ? messageDelta.delta.content
+ : [messageDelta.delta.content];
+
+ let updatedResponse = response;
+ let hasUpdate = false;
+ for (const contentPart of contentParts) {
+ if (contentPart == null) {
+ continue;
+ }
+ const currentIndex = calculateContentIndex(
+ runStep.index,
+ initialContent,
+ contentPart.type || '',
+ updatedResponse.content,
+ );
+ updatedResponse = updateContent(
+ updatedResponse,
+ currentIndex,
+ contentPart,
+ false,
+ getStepMetadata(runStep),
+ );
+ hasUpdate = true;
+ }
+ if (hasUpdate) {
+ messageMap.current.set(responseMessageId, updatedResponse);
+ scheduleCoalescedMessagesFlush(responseMessageId);
}
-
- const currentIndex = calculateContentIndex(
- runStep.index,
- initialContent,
- contentPart.type || '',
- response.content,
- );
- const updatedResponse = updateContent(
- response,
- currentIndex,
- contentPart,
- false,
- getStepMetadata(runStep),
- );
- messageMap.current.set(responseMessageId, updatedResponse);
- scheduleCoalescedMessagesFlush(responseMessageId);
}
} else if (stepEvent.event === StepEvents.ON_REASONING_DELTA) {
const reasoningDelta = stepEvent.data;
@@ -936,29 +945,37 @@ export default function useStepHandler({
const response = messageMap.current.get(responseMessageId);
if (response && reasoningDelta.delta.content != null) {
- const contentPart = Array.isArray(reasoningDelta.delta.content)
- ? reasoningDelta.delta.content[0]
- : reasoningDelta.delta.content;
-
- if (contentPart == null) {
- return;
+ /** Same multi-part contract as message deltas: Google server-side
+ * tool chunks emit several think entries in one delta. */
+ const contentParts = Array.isArray(reasoningDelta.delta.content)
+ ? reasoningDelta.delta.content
+ : [reasoningDelta.delta.content];
+
+ let updatedResponse = response;
+ let hasUpdate = false;
+ for (const contentPart of contentParts) {
+ if (contentPart == null) {
+ continue;
+ }
+ const currentIndex = calculateContentIndex(
+ runStep.index,
+ initialContent,
+ contentPart.type || '',
+ updatedResponse.content,
+ );
+ updatedResponse = updateContent(
+ updatedResponse,
+ currentIndex,
+ contentPart,
+ false,
+ getStepMetadata(runStep),
+ );
+ hasUpdate = true;
+ }
+ if (hasUpdate) {
+ messageMap.current.set(responseMessageId, updatedResponse);
+ scheduleCoalescedMessagesFlush(responseMessageId);
}
-
- const currentIndex = calculateContentIndex(
- runStep.index,
- initialContent,
- contentPart.type || '',
- response.content,
- );
- const updatedResponse = updateContent(
- response,
- currentIndex,
- contentPart,
- false,
- getStepMetadata(runStep),
- );
- messageMap.current.set(responseMessageId, updatedResponse);
- scheduleCoalescedMessagesFlush(responseMessageId);
}
} else if (stepEvent.event === StepEvents.ON_RUN_STEP_DELTA) {
const runStepDelta = stepEvent.data;