From c83336c7bf936f4b45d16bbd4450da8f501fed57 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Sun, 15 Nov 2026 08:53:22 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A1=20fix:=20Stream=20Compaction=20Sum?= =?UTF-8?q?maries=20as=20They=20Are=20Written=20(#18174)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🧽 fix: Replace an Abandoned Summary Attempt's Text When Its Retry Streams Summary deltas append to the in-flight summary part. Once the agents SDK streams them while the provider is still responding, an attempt that is then abandoned (a retry, a fallback provider) has already put text in the part, and the next attempt's deltas would follow it until the completion event replaced the lot. A delta may now carry `reset`: the part's content is replaced instead of extended, and a reset with no content empties it. The part keeps its slot and stays in flight. Deltas without the field behave as before, so this is inert until the SDK sends it. * 🦿 chore: Bump Agents SDK to 4.2.3 4.2.3 dispatches each summary delta while the provider is still responding and marks the first delta after an abandoned attempt `reset`, which the chat reducer now honors. It also stops sending reasoning as summary text, which is where the `incoming.content is not iterable` handler errors during compaction came from. The release changed only the SDK's own dev dependencies and overrides, so the lockfile moves the package's version, URL and integrity and nothing else. --- api/package.json | 2 +- package-lock.json | 10 +++--- packages/api/package.json | 2 +- .../chat/src/core/__tests__/steps.spec.ts | 36 +++++++++++++++++++ packages/chat/src/core/steps/content.ts | 7 ++-- packages/chat/src/core/steps/text.ts | 7 ++-- packages/data-provider/src/types/agents.ts | 7 ++++ 7 files changed, 60 insertions(+), 11 deletions(-) diff --git a/api/package.json b/api/package.json index c762e415a8..1affaca307 100644 --- a/api/package.json +++ b/api/package.json @@ -40,7 +40,7 @@ "@azure/storage-blob": "^12.30.0", "@google/genai": "^2.8.0", "@keyv/redis": "5.1.6", - "@librechat/agents": "^4.2.2", + "@librechat/agents": "^4.2.3", "@librechat/api": "*", "@librechat/data-schemas": "*", "@microsoft/microsoft-graph-client": "^3.0.7", diff --git a/package-lock.json b/package-lock.json index d855afa22b..088d388668 100644 --- a/package-lock.json +++ b/package-lock.json @@ -70,7 +70,7 @@ "@azure/storage-blob": "^12.30.0", "@google/genai": "^2.8.0", "@keyv/redis": "5.1.6", - "@librechat/agents": "^4.2.2", + "@librechat/agents": "^4.2.3", "@librechat/api": "*", "@librechat/data-schemas": "*", "@microsoft/microsoft-graph-client": "^3.0.7", @@ -9479,9 +9479,9 @@ } }, "node_modules/@librechat/agents": { - "version": "4.2.2", - "resolved": "https://registry.npmjs.org/@librechat/agents/-/agents-4.2.2.tgz", - "integrity": "sha512-Yx248RpKQo0UAGNpPny5Y5anmYVXTbg5UhGJXeeYdxX7wKn+cBvM691jCWPTBWgps5D0Q4p5gj0T4Y2kyUTksw==", + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/@librechat/agents/-/agents-4.2.3.tgz", + "integrity": "sha512-nLZlff0YAXi26a1P9V5ydSEEjKsyMZi0WtEW2fo0xBipDvK9n5uvFExU9WK5z6YinLzJD6MY7gcpl2wQYh69Uw==", "license": "MIT", "dependencies": { "@anthropic-ai/sdk": "^0.115.0", @@ -42444,7 +42444,7 @@ "@azure/identity": "^4.14.0", "@azure/storage-blob": "^12.30.0", "@keyv/redis": "5.1.6", - "@librechat/agents": "^4.2.2", + "@librechat/agents": "^4.2.3", "@librechat/data-schemas": "*", "@modelcontextprotocol/sdk": "^1.32.1", "@opentelemetry/api": "^1.9.0", diff --git a/packages/api/package.json b/packages/api/package.json index c946da54ae..a5f20a1f0c 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -120,7 +120,7 @@ "@azure/identity": "^4.14.0", "@azure/storage-blob": "^12.30.0", "@keyv/redis": "5.1.6", - "@librechat/agents": "^4.2.2", + "@librechat/agents": "^4.2.3", "@librechat/data-schemas": "*", "@modelcontextprotocol/sdk": "^1.32.1", "@opentelemetry/api": "^1.9.0", diff --git a/packages/chat/src/core/__tests__/steps.spec.ts b/packages/chat/src/core/__tests__/steps.spec.ts index 1e317440f9..ac5e79940e 100644 --- a/packages/chat/src/core/__tests__/steps.spec.ts +++ b/packages/chat/src/core/__tests__/steps.spec.ts @@ -906,6 +906,42 @@ describe('steps', () => { ).toBeUndefined(); }); + it('replaces what an abandoned attempt streamed when a delta is marked reset', () => { + const text = (message: TMessage) => + ((message.content?.[1] as SummaryContentPart).content ?? []) + .map((block) => ('text' in block ? block.text : '')) + .join(''); + const abandoned = streamSummary(); + expect(text(abandoned)).toBe('Earlier, we talked.'); + + let retried = applySummarizeDelta( + abandoned, + summarize, + { ...summaryDelta('Second '), reset: true }, + 0, + ); + retried = applySummarizeDelta(retried, summarize, summaryDelta('attempt.'), 0); + expect(text(retried)).toBe('Second attempt.'); + expect(retried.content?.[1]).toMatchObject({ summarizing: true }); + expectStepPositions(retried, [[summarize, ContentTypes.SUMMARY]]); + + const withdrawn = applySummarizeDelta( + retried, + summarize, + { + id: summarize.id, + reset: true, + delta: { summary: { type: ContentTypes.SUMMARY, content: [] } as SummaryContentPart }, + }, + 0, + ); + expect(text(withdrawn)).toBe(''); + expect(withdrawn.content?.[1]).toMatchObject({ + type: ContentTypes.SUMMARY, + summarizing: true, + }); + }); + it('carries agent and group metadata onto the finalized summary', () => { const parallel = messageStep('step-summary-p', 1, { agentId: 'agent-b', diff --git a/packages/chat/src/core/steps/content.ts b/packages/chat/src/core/steps/content.ts index 938dcf1c1d..c720f3ca53 100644 --- a/packages/chat/src/core/steps/content.ts +++ b/packages/chat/src/core/steps/content.ts @@ -236,7 +236,8 @@ export function updateContent( /** * Writes one incoming part into `message.content[index]` and returns the next message. Text and * reasoning append, tool calls merge (args concatenate until they arrive as an object), summaries - * concatenate their content, and a part of a different type never overwrites an occupied slot. + * concatenate their content (or replace it on a final update), and a part of a different type + * never overwrites an occupied slot. * Returns the input message unchanged when the part is rejected. */ function writeContent( @@ -354,9 +355,11 @@ function writeContent( } else if (contentType === ContentTypes.SUMMARY) { const currentSummary = updatedContent[index] as SummaryContentPart | undefined; const incoming = contentPart as SummaryContentPart; + /** A final update carries the whole content: the summary so far is replaced, not extended. */ + const streamed = finalUpdate ? [] : (currentSummary?.content ?? []); updatedContent[index] = { ...incoming, - content: [...(currentSummary?.content ?? []), ...(incoming.content ?? [])], + content: [...streamed, ...(incoming.content ?? [])], }; } else if (contentType === ContentTypes.TOOL_CALL && 'tool_call' in contentPart) { const existingContent = updatedContent[index] as Agents.ToolCallContent | undefined; diff --git a/packages/chat/src/core/steps/text.ts b/packages/chat/src/core/steps/text.ts index 32e7cae06b..efad3f8022 100644 --- a/packages/chat/src/core/steps/text.ts +++ b/packages/chat/src/core/steps/text.ts @@ -151,7 +151,10 @@ export function applySummaryStep( ); } -/** Appends a summary delta to its step's in-flight summary part. */ +/** + * Appends a summary delta to its step's in-flight summary part. A delta marked `reset` replaces + * what the part held: the attempt that streamed it was abandoned. + */ export function applySummarizeDelta( message: TMessage, runStep: Agents.RunStep, @@ -163,7 +166,7 @@ export function applySummarizeDelta( message, runStep.index + editPrefixOffset, contentPart, - false, + delta.reset === true, getStepMetadata(runStep), ); } diff --git a/packages/data-provider/src/types/agents.ts b/packages/data-provider/src/types/agents.ts index 2127186af8..8f27671390 100644 --- a/packages/data-provider/src/types/agents.ts +++ b/packages/data-provider/src/types/agents.ts @@ -773,6 +773,13 @@ export namespace Agents { export interface SummarizeDeltaEvent { id: string; + /** + * Discard what the step has streamed so far, then apply `delta`. Deltas arrive while the + * provider is still responding, so an attempt that is then abandoned (a retry, a fallback + * provider) has already shown text; the next attempt's first delta carries this, or a delta + * with no content when no attempt follows. + */ + reset?: boolean; delta: { summary: SummaryContentPart; };