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
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
✂️ fix: Keep Parts After a Summary in the Summary-Bounded History Read (#16434)
Marco Beretta0a85ff5c5955
diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.jsindex 4a4f6975bd..643fd796ec 100644--- a/api/app/clients/BaseClient.js+++ b/api/app/clients/BaseClient.js@@ -25,6 +25,7 @@ const {withBalanceReservations,findCheckpointSummaryPart,getSummaryPartText,+ resolveCheckpointMessage,runAfterSeed,saveTurnConversation,seedTurnConversation,@@ -1419,6 +1420,7 @@ class BaseClient {* - The message's 'role' is set to 'system'.* - The message's 'text' is set to its 'summary'.* - If the message has a 'summaryTokenCount', the message's 'tokenCount' is set to 'summaryTokenCount'.+ * - A message with a summary content block keeps its content from that block on, for the SDK formatter to promote.* The traversal stops at the message with the 'summary' property.** Each message object should have an 'id' or 'messageId' property and may have a 'parentMessageId' property.@@ -1468,35 +1470,13 @@ class BaseClient {break;}- let resolved = message;- let hasSummary = false;- if (summary) {- const summaryBlock = findCheckpointSummaryPart(message.content);- if (summaryBlock) {- const summaryText = getSummaryPartText(summaryBlock);- resolved = {- ...message,- role: 'system',- content: [{ type: ContentTypes.TEXT, text: summaryText }],- tokenCount: summaryBlock.tokenCount,- };- hasSummary = true;- } else if (message.summary) {- resolved = {- ...message,- role: 'system',- content: [{ type: ContentTypes.TEXT, text: message.summary }],- tokenCount: message.summaryTokenCount ?? message.tokenCount,- };- hasSummary = true;- }- }-+ const checkpoint = summary ? resolveCheckpointMessage(message) : null;+ const resolved = checkpoint ?? message;const shouldMap = mapMethod != null && (mapCondition != null ? mapCondition(resolved) : true);const processedMessage = shouldMap ? mapMethod(resolved) : resolved;orderedMessages.push(processedMessage);- if (hasSummary) {+ if (checkpoint) {break;}diff --git a/api/app/clients/specs/BaseClient.test.js b/api/app/clients/specs/BaseClient.test.jsindex 2c675aa5dc..375f5f2d27 100644--- a/api/app/clients/specs/BaseClient.test.js+++ b/api/app/clients/specs/BaseClient.test.js@@ -548,9 +548,10 @@ describe('BaseClient', () => {summary: true,});expect(result).toHaveLength(2);- expect(result[0].role).toBe('system');- expect(result[0].content).toEqual([{ type: 'text', text: 'Content block summary' }]);- expect(result[0].tokenCount).toBe(42);+ expect(result[0].role).toBeUndefined();+ expect(result[0].content).toEqual([+ { type: 'summary', text: 'Content block summary', tokenCount: 42 },+ ]);});it('should prefer content block summary over legacy summary field', () => {@@ -571,8 +572,9 @@ describe('BaseClient', () => {summary: true,});expect(result).toHaveLength(2);- expect(result[0].content).toEqual([{ type: 'text', text: 'Content block summary' }]);- expect(result[0].tokenCount).toBe(20);+ expect(result[0].content).toEqual([+ { type: 'summary', text: 'Content block summary', tokenCount: 20 },+ ]);});it('should fallback to legacy summary when no content block exists', () => {@@ -596,6 +598,34 @@ describe('BaseClient', () => {expect(result[0].tokenCount).toBe(15);});+ it('keeps the parts a response produced after its summary (summary mode)', () => {+ const trailingText = { type: 'text', text: 'Answer after summarizing' };+ const messagesWithTrailingParts = [+ { id: '1', parentMessageId: null, text: 'Message 1' },+ {+ id: '2',+ parentMessageId: '1',+ text: 'Before summarizing',+ content: [+ { type: 'text', text: 'Before summarizing' },+ { type: 'summary', text: 'Earlier context', tokenCount: 6 },+ trailingText,+ ],+ },+ { id: '3', parentMessageId: '2', text: 'Message 3' },+ ];+ const result = TestClient.constructor.getMessagesForConversation({+ messages: messagesWithTrailingParts,+ parentMessageId: '3',+ summary: true,+ });+ expect(result.map((message) => message.id)).toEqual(['2', '3']);+ expect(result[0].content).toEqual([+ { type: 'summary', text: 'Earlier context', tokenCount: 6 },+ trailingText,+ ]);+ });+it('should not stop traversal at a failed summary, keeping the prior history', () => {/** A summarize round that errored keeps the deltas it streamed, so its* text is a truncated prefix; treating it as the checkpoint would senddiff --git a/api/server/controllers/agents/__tests__/client.retainedAnswers.spec.js b/api/server/controllers/agents/__tests__/client.retainedAnswers.spec.jsindex aa0f035dbf..27a308a303 100644--- a/api/server/controllers/agents/__tests__/client.retainedAnswers.spec.js+++ b/api/server/controllers/agents/__tests__/client.retainedAnswers.spec.js@@ -222,6 +222,11 @@ describe('AgentClient retained answers', () => {client.user = 'user-123';client.processMemory = jest.fn();const rows = compactedBranch();+ /** The stored count covers the whole response, including what preceded its summary. */+ const storedResponseTokens = 5000;+ const response = rows.find((row) => row.messageId === 'a2');+ response.tokenCount = storedResponseTokens;+ response.content.unshift({ type: ContentTypes.TEXT, text: 'Checking the pipeline.' });getMessages.mockResolvedValue(rows);/** The real loader: one read of the conversation, then the summary-bounded walk. */const cut = await client.loadHistory('convo-123', 'u3');@@ -239,6 +244,10 @@ describe('AgentClient retained answers', () => {expect(text.indexOf(ANSWER_LINE)).toBeLessThan(text.indexOf(LATEST_TEXT));expect(text.endsWith(LATEST_TEXT)).toBe(true);expect(client.options.agent.additional_instructions ?? '').not.toContain(ANSWER_LINE);+ expect(text).toContain('Deployed.');+ expect(text).not.toContain('Checking the pipeline.');+ expect(tokenCountMap.a2).toBeGreaterThan(0);+ expect(tokenCountMap.a2).toBeLessThan(storedResponseTokens);expect(cut[1].text).toBe(LATEST_TEXT);expect(cut[1].content).toBeUndefined();expect(counts[prompt.length - 1]).toBe(tokenCountMap.u3);diff --git a/e2e/specs/mock/scenarios/failed-summary-history.spec.ts b/e2e/specs/mock/scenarios/failed-summary-history.spec.tsindex 50765dac37..fbe1da623e 100644--- a/e2e/specs/mock/scenarios/failed-summary-history.spec.ts+++ b/e2e/specs/mock/scenarios/failed-summary-history.spec.ts@@ -26,12 +26,12 @@ async function startConversation(page: Page) {}/**- * Appends a turn whose only content is a summary part, cloning the endpoint and+ * Appends a turn whose content is the given parts, cloning the endpoint and* model of the turn it hangs off. A summarize round that streamed deltas and* then errored cannot be produced through the composer, and the behavior under* test is what the *next* turn sends once such a turn is persisted.*/-async function appendSummaryTurn(conversationId: string, part: Record<string, unknown>) {+async function appendSummaryTurn(conversationId: string, ...parts: Record<string, unknown>[]) {await withMongo(async (db) => {const rows = await db.collection('messages')@@ -53,7 +53,7 @@ async function appendSummaryTurn(conversationId: string, part: Record<string, unparentMessageId: leaf.messageId,isCreatedByUser: false,text: '',- content: [part],+ content: parts,createdAt: now,updatedAt: now,});@@ -170,4 +170,32 @@ test.describe('failed summary history', () => {messagesView(page).getByText(`E2E history assertion absent: ${token}`),).toBeVisible({ timeout: 30000 });});++ /**+ * A response that summarized mid-run keeps producing after the summary, and+ * those parts are not covered by it. The text the turn wrote after its summary+ * has to reach the model on the next turn.+ */+ test('a turn after a mid-response summary still sends what the response wrote after it @scenario:parts-after-summary-reach-next-turn', async ({+ page,+ }) => {+ const { conversationId } = await startConversation(page);+ conversationIds.push(conversationId);+ const laterFact = `LATERFACT-${randomUUID().slice(0, 8)}`;+ await appendSummaryTurn(+ conversationId,+ summaryPart('The user shared a passphrase and it was acknowledged.', {+ boundary: completedBoundary,+ }),+ { type: 'text', text: `The deploy code is ${laterFact}.` },+ );++ await page.goto(`/c/${conversationId}`);+ await expect(messagesView(page).getByText(laterFact)).toBeVisible();+ await sendMessageAndWaitForCompletion(page, `E2E_ASSERT_HISTORY:${laterFact}`);++ await expect(+ messagesView(page).getByText(`E2E history assertion present: ${laterFact}`),+ ).toBeVisible({ timeout: 30000 });+ });});diff --git a/packages/api/src/agents/compaction.spec.ts b/packages/api/src/agents/compaction.spec.tsindex e5460ca7f7..a8910eeb3e 100644--- a/packages/api/src/agents/compaction.spec.ts+++ b/packages/api/src/agents/compaction.spec.ts@@ -17,6 +17,7 @@ import {getSummaryPartText,markCompactionOutcome,resolveFailedTurnContent,+ resolveCheckpointMessage,restoreCompactionSemanticIndex,restoreCompactionSemanticIndexSnapshot,stripUnusableSummaryParts,@@ -246,6 +247,76 @@ describe('resolveFailedTurnContent', () => {});});+describe('resolveCheckpointMessage', () => {+ const summaryPart = {+ type: ContentTypes.SUMMARY,+ content: [{ type: ContentTypes.TEXT, text: 'Earlier context' }],+ boundary: completedBoundary,+ tokenCount: 7,+ };++ it('keeps the parts a response produced after its summary', () => {+ const trailingText = { type: ContentTypes.TEXT, text: 'Answer after summarizing' };+ const message = {+ messageId: 'response',+ tokenCount: 90,+ content: [{ type: ContentTypes.TEXT, text: 'Before summarizing' }, summaryPart, trailingText],+ };++ const resolved = resolveCheckpointMessage(message);++ expect(resolved).toEqual({ messageId: 'response', content: [summaryPart, trailingText] });+ expect(resolved?.tokenCount).toBeUndefined();+ });++ it('starts at the last usable summary', () => {+ const between = { type: ContentTypes.TEXT, text: 'Between summaries' };+ const after = { type: ContentTypes.TEXT, text: 'After the latest' };+ const latest = {+ ...summaryPart,+ content: [{ type: ContentTypes.TEXT, text: 'Latest context' }],+ };++ expect(resolveCheckpointMessage({ content: [summaryPart, between, latest, after] })).toEqual({+ content: [latest, after],+ });+ });++ it('keeps the parts after a completed summary when a later one failed', () => {+ const after = { type: ContentTypes.TEXT, text: 'After the completed summary' };+ const failed = { ...summaryPart, failed: true };++ expect(resolveCheckpointMessage({ content: [summaryPart, after, failed] })).toEqual({+ content: [summaryPart, after, failed],+ });+ });++ it('returns the row itself when it already starts at its summary', () => {+ const message = { content: [summaryPart], tokenCount: 12 };++ expect(resolveCheckpointMessage(message)).toBe(message);+ });++ it('replaces the whole row for a legacy summary field', () => {+ expect(+ resolveCheckpointMessage({ summary: 'Legacy', summaryTokenCount: 4, tokenCount: 30 }),+ ).toEqual({+ summary: 'Legacy',+ summaryTokenCount: 4,+ role: 'system',+ content: [{ type: ContentTypes.TEXT, text: 'Legacy' }],+ tokenCount: 4,+ });+ });++ it('is null for a row that is no checkpoint', () => {+ expect(resolveCheckpointMessage({ content: [{ ...summaryPart, failed: true }] })).toBeNull();+ expect(+ resolveCheckpointMessage({ content: [{ type: ContentTypes.TEXT, text: 'x' }] }),+ ).toBeNull();+ });+});+describe('findCheckpointSummaryPart', () => {const legacySummary = { type: ContentTypes.SUMMARY, text: 'Summary of conversation' };diff --git a/packages/api/src/agents/compaction.ts b/packages/api/src/agents/compaction.tsindex e89311178f..241c394965 100644--- a/packages/api/src/agents/compaction.ts+++ b/packages/api/src/agents/compaction.ts@@ -96,6 +96,45 @@ export function findCheckpointSummaryPart(content: unknown): SummaryContentPartreturn checkpoint;}+/** The fields a history row offers when it may be a summary checkpoint. */+export interface CheckpointCandidate {+ content?: unknown;+ summary?: string | null;+ summaryTokenCount?: number | null;+ tokenCount?: number | null;+}++/**+ * The row a history read stops at, as it enters the prompt, or null when the+ * row is no checkpoint and the read continues past it. A content-block summary+ * keeps the row from that summary on: a response that summarized mid-run keeps+ * producing after it, and the SDK formatter (`applySummaryBoundary`) promotes+ * the summary and keeps the parts that follow, exactly as it does when the full+ * history is sent. A sliced row drops its stored token count, which still covers+ * the parts before the summary, so the prompt is charged for what it sends. A+ * legacy `summary` field replaces the whole row.+ */+export function resolveCheckpointMessage<T extends CheckpointCandidate>(+ message: T,+): (T & { role?: 'system' }) | null {+ const summaryPart = findCheckpointSummaryPart(message.content);+ if (summaryPart != null && Array.isArray(message.content)) {+ const summaryIndex = message.content.lastIndexOf(summaryPart);+ return summaryIndex === 0+ ? message+ : { ...message, content: message.content.slice(summaryIndex), tokenCount: undefined };+ }+ if (!message.summary) {+ return null;+ }+ return {+ ...message,+ role: 'system',+ content: [{ type: ContentTypes.TEXT, text: message.summary }],+ tokenCount: message.summaryTokenCount ?? message.tokenCount,+ };+}+/*** The summary a warm event-actor continuation carries forward: the last usable* one in the run's content parts, stamped as actor state. A failed or