An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(chat): replace a historical paste that alone exceeds the budget (#1081)
Yoshiki Miura1803668a5cbf
diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.mdindex cc918702..e5712ba9 100644--- a/docs/CONFIGURATION.md+++ b/docs/CONFIGURATION.md@@ -480,9 +480,8 @@ visible in the conversation, and the model is told to ask for it again if it nee```bash# Optional: characters of pasted content replayed from earlier turns, excluding the# newest message. Dropped in whole blocks so the prompt prefix stays cacheable, so the-# amount replayed can reach about twice the budget. The most recent earlier paste is-# always kept, even when it alone exceeds the budget. Set to 0 to replay every pasted-# block (default: 200000)+# amount replayed can reach about twice the budget. A single earlier paste larger than+# the budget is always replaced. Set to 0 to replay every pasted block (default: 200000)HISTORY_PASTED_CONTENT_CHAR_BUDGET=200000```diff --git a/lib/streaming/helpers/__tests__/cap-historical-pasted-content.test.ts b/lib/streaming/helpers/__tests__/cap-historical-pasted-content.test.tsindex dead0974..d74e917d 100644--- a/lib/streaming/helpers/__tests__/cap-historical-pasted-content.test.ts+++ b/lib/streaming/helpers/__tests__/cap-historical-pasted-content.test.ts@@ -86,8 +86,8 @@ describe('capHistoricalPastedContent', () => {expect(after.slice(0, before.length - 1)).toEqual(before.slice(0, -1))})- it('keeps only the newest historical paste when each exceeds the budget', () => {- const dropped = [1, 2, 3, 4].map(count =>+ it('replaces every historical paste that alone exceeds the budget', () => {+ const dropped = [1, 2, 3].map(count =>omittedIds(capHistoricalPastedContent([...thread(Array(count).fill(500)), textTurn('next', 'user')],@@ -96,17 +96,44 @@ describe('capHistoricalPastedContent', () => {))- expect(dropped).toEqual([[], ['u0'], ['u0', 'u1'], ['u0', 'u1', 'u2']])+ expect(dropped).toEqual([['u0'], ['u0', 'u1'], ['u0', 'u1', 'u2']])+ })++ it('keeps a paste exactly at the budget', () => {+ const messages = [...thread([BUDGET]), textTurn('u1', 'user')]+ expect(capHistoricalPastedContent(messages, BUDGET)).toBe(messages)})it('keeps a small paste that follows an oversized one', () => {const messages = [...thread([500, 500, 10]), textTurn('u3', 'user')]expect(omittedIds(capHistoricalPastedContent(messages, BUDGET))).toEqual([- 'u0'+ 'u0',+ 'u1'])})+ it('does not let an oversized paste close a block of smaller ones', () => {+ const messages = [...thread([40, 500, 40]), textTurn('u3', 'user')]++ expect(omittedIds(capHistoricalPastedContent(messages, BUDGET))).toEqual([+ 'u1'+ ])+ })++ it('keeps the placeholder for an oversized paste stable across turns', () => {+ const before = capHistoricalPastedContent(+ [...thread([500]), textTurn('u1', 'user')],+ BUDGET+ )+ const after = capHistoricalPastedContent(+ [...thread([500, 10]), textTurn('u2', 'user')],+ BUDGET+ )++ expect(after.slice(0, before.length - 1)).toEqual(before.slice(0, -1))+ })+it('never caps the newest user message', () => {const messages = [...thread([60, 60, 60, 60]), pasteTurn('u4', 5000)]const capped = capHistoricalPastedContent(messages, BUDGET)diff --git a/lib/streaming/helpers/cap-historical-pasted-content.ts b/lib/streaming/helpers/cap-historical-pasted-content.tsindex c76e9a2b..8a74867a 100644--- a/lib/streaming/helpers/cap-historical-pasted-content.ts+++ b/lib/streaming/helpers/cap-historical-pasted-content.ts@@ -33,10 +33,10 @@ function getPastedLength(part: UIMessage['parts'][number]): number {** Older blocks become a text placeholder giving their size. They are dropped* by the same weight quantization as attachments, so the prompt prefix stays- * cacheable between block crossings. A block may be a single paste: when one- * paste exceeds the budget, moving the boundary on each new paste rewrites far- * less than carrying a second copy would. The newest user message and the- * newest historical paste are never capped.+ * cacheable between block crossings. A single paste larger than the budget is+ * replaced as soon as it leaves the newest message, so it is replayed at most+ * once and its placeholder never moves. The newest user message is never+ * capped.*/export function capHistoricalPastedContent(messages: UIMessage[],@@ -51,27 +51,31 @@ export function capHistoricalPastedContent(currentTurnIndex === -1 ? messages.length : currentTurnIndexconst weights: number[] = []+ let oversizedCount = 0for (let i = 0; i < historyEnd; i++) {for (const part of messages[i].parts) {const length = getPastedLength(part)- if (length > 0) weights.push(length)+ if (length > charBudget) oversizedCount += 1+ else if (length > 0) weights.push(length)}}const dropCount = getWeightDroppedCount(weights, charBudget, 1)- if (dropCount === 0) return messages+ if (dropCount === 0 && oversizedCount === 0) return messageslet seen = 0return messages.map((message, index) => {- if (index >= historyEnd || seen >= dropCount) return message+ if (index >= historyEnd) return messageif (!message.parts.some(part => getPastedLength(part) > 0)) return messageconst parts = message.parts.map(part => {const length = getPastedLength(part)if (length === 0) return part- seen += 1- if (seen > dropCount) return part+ if (length <= charBudget) {+ seen += 1+ if (seen > dropCount) return part+ }return {type: 'text' as const,@@ -79,6 +83,8 @@ export function capHistoricalPastedContent(}})- return { ...message, parts }+ return parts.every((part, i) => part === message.parts[i])+ ? message+ : { ...message, parts }})}