Repository navigation
fix(core): Reduce multimodal history payload size #6045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
558e71f
fix(core): reduce multimodal history payload size
LaZzyMan 3dac22e
fix(core): use kebab-case image payload filenames
LaZzyMan b833184
fix(core): address image payload review blockers
LaZzyMan fb90b66
fix(core): preserve current request image payloads
LaZzyMan b3b6a1b
ci: disable implicit actionlint pyflakes integration
LaZzyMan 768787b
fix(ci): merge main to disable both actionlint pyflakes and shellcheck
qwen-code-dev-bot 5e1bcd6
fix(core): reattach recent unique image payloads
LaZzyMan 6fbb4e7
fix(core): preserve referenced image payloads
LaZzyMan 04ce964
fix(core): tolerate partial config mocks in MCP discovery
LaZzyMan 9e220e6
fix(core): preserve current images during recovery
LaZzyMan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/core/src/services/image-payload-references.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { Content, Part } from '@google/genai'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| InMemoryImagePayloadStore, | ||
| prepareImagePayloadsForRequest, | ||
| } from './image-payload-references.js'; | ||
|
|
||
| function toolImageTurn(data: string): Content { | ||
| return { | ||
| role: 'user', | ||
| parts: [ | ||
| { | ||
| functionResponse: { | ||
| id: `call-${data}`, | ||
| name: 'screenshot', | ||
| response: { output: `captured ${data}` }, | ||
| parts: [{ inlineData: { mimeType: 'image/png', data } }], | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| function imageParts(contents: Content[]): Part[] { | ||
| const result: Part[] = []; | ||
| for (const content of contents) { | ||
| for (const part of content.parts ?? []) { | ||
| if (part.inlineData?.mimeType?.startsWith('image/')) { | ||
| result.push(part); | ||
| } | ||
| const nested = part.functionResponse?.parts as Part[] | undefined; | ||
| for (const inner of nested ?? []) { | ||
| if (inner.inlineData?.mimeType?.startsWith('image/')) { | ||
| result.push(inner); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| describe('prepareImagePayloadsForRequest', () => { | ||
| it('replaces historical image positions with stable refs and reattaches only the most recent images', () => { | ||
| const store = new InMemoryImagePayloadStore(); | ||
| const history: Content[] = [ | ||
| toolImageTurn('old-shot'), | ||
| toolImageTurn('new-shot'), | ||
| { role: 'user', parts: [{ text: 'continue' }] }, | ||
| ]; | ||
|
|
||
| const prepared = prepareImagePayloadsForRequest(history, { | ||
| maxRecentImages: 1, | ||
| store, | ||
| }); | ||
|
|
||
| const serialized = JSON.stringify(prepared); | ||
| expect(serialized).toMatch( | ||
| /\[Image #[a-f0-9]{12}: image\/png, \d+ bytes\]/, | ||
| ); | ||
| expect(serialized).not.toContain('"data":"old-shot"'); | ||
| expect(imageParts(prepared).map((part) => part.inlineData?.data)).toEqual([ | ||
| 'new-shot', | ||
| ]); | ||
| expect(prepared).toHaveLength(history.length); | ||
| expect(prepared.at(-1)?.role).toBe('user'); | ||
| expect(prepared.at(-1)?.parts?.[0]?.text).toBe('continue'); | ||
| expect(prepared.at(-1)?.parts?.[1]?.text).toContain( | ||
| 'Recent images reattached', | ||
| ); | ||
| }); | ||
|
|
||
| it('reattaches an older image when the current request explicitly references its stable id', () => { | ||
| const store = new InMemoryImagePayloadStore(); | ||
| const oldImage = toolImageTurn('old-shot'); | ||
| const firstPass = prepareImagePayloadsForRequest( | ||
| [oldImage, { role: 'model', parts: [{ text: 'ok' }] }], | ||
| { | ||
| maxRecentImages: 0, | ||
| store, | ||
| }, | ||
| ); | ||
| const id = JSON.stringify(firstPass).match(/Image #([a-f0-9]{12})/)?.[1]; | ||
| expect(id).toBeDefined(); | ||
|
|
||
| const prepared = prepareImagePayloadsForRequest( | ||
| [ | ||
| oldImage, | ||
| toolImageTurn('new-shot'), | ||
| { role: 'user', parts: [{ text: `inspect Image #${id}` }] }, | ||
| ], | ||
| { | ||
| maxRecentImages: 0, | ||
| store, | ||
| }, | ||
| ); | ||
|
|
||
| expect(imageParts(prepared).map((part) => part.inlineData?.data)).toEqual([ | ||
| 'old-shot', | ||
| ]); | ||
| }); | ||
|
|
||
| it('preserves images in the current user request when maxRecentImages is zero', () => { | ||
| const store = new InMemoryImagePayloadStore(); | ||
| const prepared = prepareImagePayloadsForRequest( | ||
| [ | ||
| toolImageTurn('old-shot'), | ||
| { | ||
| role: 'user', | ||
| parts: [ | ||
| { text: 'inspect this' }, | ||
| { inlineData: { mimeType: 'image/png', data: 'current-shot' } }, | ||
| ], | ||
| }, | ||
| ], | ||
| { | ||
| maxRecentImages: 0, | ||
| preserveLastUserImagePartCount: 2, | ||
| store, | ||
| }, | ||
| ); | ||
|
|
||
| const serialized = JSON.stringify(prepared); | ||
| expect(serialized).not.toContain('"data":"old-shot"'); | ||
| expect(imageParts(prepared).map((part) => part.inlineData?.data)).toEqual([ | ||
| 'current-shot', | ||
| ]); | ||
| }); | ||
| }); | ||
|
LaZzyMan marked this conversation as resolved.
Resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.