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; };