Skip to content

The agent that grows with you

fix(desktop): remember the size of inline data-URL chat images

teknium1d113cfc20851
diff --git a/apps/desktop/src/components/chat/image-geometry.test.tsx b/apps/desktop/src/components/chat/image-geometry.test.tsx
index b78f070cbe9..c82c629a91f 100644
--- a/apps/desktop/src/components/chat/image-geometry.test.tsx
+++ b/apps/desktop/src/components/chat/image-geometry.test.tsx
@@ -108,6 +108,19 @@ it('keeps the pending generated-image frame when its result arrives', async () =
expect(frame(mounted.container).style.cssText).toBe(pendingStyle)
})
+it('keys inline data sources by a bounded digest so they are remembered', async () => {
+ const big = (fill: string) => `data:image/png;base64,${fill.repeat(200_000)}`
+ const first = render(<MarkdownImage alt="inline" src={big('A')} />)
+ expect(first.container.querySelector('img')).not.toBeNull()
+ const img = first.container.querySelector('img')!
+ Object.defineProperties(img, { naturalWidth: { value: 900 }, naturalHeight: { value: 600 } })
+ fireEvent.load(img)
+ first.unmount()
+
+ expect(parseFloat(frame(render(<MarkdownImage alt="inline" src={big('A')} />).container).style.aspectRatio)).toBe(1.5)
+ expect(parseFloat(frame(render(<MarkdownImage alt="inline" src={big('B')} />).container).style.aspectRatio)).not.toBe(1.5)
+})
+
it.each(['markdown', 'generated'])('%s dimensions follow owner and source, not stale reads or hints', async kind => {
const path = `${paths.second}?${kind}`
diff --git a/apps/desktop/src/lib/media-dimensions.test.ts b/apps/desktop/src/lib/media-dimensions.test.ts
index bc0c4c9a997..1f9052a04d9 100644
--- a/apps/desktop/src/lib/media-dimensions.test.ts
+++ b/apps/desktop/src/lib/media-dimensions.test.ts
@@ -43,3 +43,12 @@ it('shares proven path aliases only within an owner and bounds regenerated metad
forgetMediaImageDimensions(key)
expect(getMediaImageDimensions(key)).toBeUndefined()
})
+
+it('keeps multi-megabyte inline sources to a small, distinct key', () => {
+ const a = { connectionId: 'a', profile: 'work', mode: 'local' } as HermesConnection
+ const inline = (tail: string) => `data:image/png;base64,${'A'.repeat(4_000_000)}${tail}`
+
+ expect(mediaImageKey(inline('x'), a).length).toBeLessThan(1024)
+ expect(mediaImageKey(inline('x'), a)).toBe(mediaImageKey(inline('x'), a))
+ expect(mediaImageKey(inline('x'), a)).not.toBe(mediaImageKey(inline('y'), a))
+})
diff --git a/apps/desktop/src/lib/media.ts b/apps/desktop/src/lib/media.ts
index a9e0b041090..8d32a720f37 100644
--- a/apps/desktop/src/lib/media.ts
+++ b/apps/desktop/src/lib/media.ts
@@ -143,11 +143,21 @@ export function mediaImageKey(path: string, connection: HermesConnection | null,
connection?.connectionId ||
desktopFsCacheKey(connection && { ...connection, profile: owner?.profile ?? connection.profile }),
owner?.profile ?? connection?.profile ?? '',
- filePathFromMediaPath(path),
+ mediaSourceIdentity(path),
revision
])
}
+// Inline sources are the bytes themselves (often megabytes) and this key is
+// rebuilt every render. Length plus head (format header, usually the size)
+// and tail keeps it small and cacheable; a collision can only borrow another
+// image's frame shape, never its pixels.
+function mediaSourceIdentity(path: string): string {
+ return /^data:/i.test(path) && path.length > 512
+ ? `data#${path.length}:${path.slice(0, 160)}:${path.slice(-96)}`
+ : filePathFromMediaPath(path)
+}
+
export function validImageDimensions(width: unknown, height: unknown): MediaImageDimensions | undefined {
const w = Number(width)
const h = Number(height)