Skip to content

The agent that grows with you

fix(desktop): keep a generated image visible after a remembered failure

teknium1ee8ffffd4535
diff --git a/apps/desktop/src/components/chat/image-geometry.test.tsx b/apps/desktop/src/components/chat/image-geometry.test.tsx
index b1a213c75ba..9ccc41b489a 100644
--- a/apps/desktop/src/components/chat/image-geometry.test.tsx
+++ b/apps/desktop/src/components/chat/image-geometry.test.tsx
@@ -93,7 +93,9 @@ it.each(cases)('reserves $kind frames through cold decode, warm return and failu
expect(frame(warm.container).style.cssText).toBe(warmSize)
// A broken image collapses to its text line instead of an empty frame, and
- // the next mount of that source does not reserve a frame just to collapse it.
+ // the next markdown mount of that source does not reserve a frame just to
+ // collapse it. A generated image's children are all absolutely positioned,
+ // so it keeps its hinted frame: a successful retry must still be visible.
fireEvent.error(warm.container.querySelector('img')!)
expect(warm.container.textContent).toMatch(/Open image/i)
expect(frame(warm.container)?.style.aspectRatio ?? '').toBe('')
@@ -101,14 +103,25 @@ it.each(cases)('reserves $kind frames through cold decode, warm return and failu
const retry = render(content(kind, path, hint))
- if (hint !== 'intrinsic') {
+ if (kind !== 'generated' && hint !== 'intrinsic') {
expect(frame(retry.container)?.style.aspectRatio ?? '').toBe('')
}
+
+ await decode(retry.container)
+ expect(retry.container.querySelector('img')).not.toBeNull()
+
+ if (kind === 'generated') {
+ expect(parseFloat(frame(retry.container).style.aspectRatio)).toBeGreaterThan(0)
+ }
})
it('keeps the pending generated-image frame when its result arrives', async () => {
const path = '/geometry/pending.svg'
- const mounted = render(<GeneratedImage aspectRatio="landscape" />)
+ const mounted = render(<GeneratedImage aspectRatio="square" />)
+ expect(parseFloat(frame(mounted.container).style.aspectRatio)).toBe(1)
+ // The hint can arrive after the pending card mounted; nothing is shown yet.
+ mounted.rerender(<GeneratedImage aspectRatio="landscape" />)
+ expect(parseFloat(frame(mounted.container).style.aspectRatio)).toBeCloseTo(16 / 9)
const pendingStyle = frame(mounted.container).style.cssText
mounted.rerender(<GeneratedImage aspectRatio="landscape" result={{ success: true, image: path, pixel_size: '900x600' }} />)
diff --git a/apps/desktop/src/hooks/use-media-image.ts b/apps/desktop/src/hooks/use-media-image.ts
index bdb5c2e5f45..818d7efc6ba 100644
--- a/apps/desktop/src/hooks/use-media-image.ts
+++ b/apps/desktop/src/hooks/use-media-image.ts
@@ -16,7 +16,12 @@ import { $connection } from '@/store/session'
/** Keep a frame for one source/owner for its mounted lifetime. Hints are not
* intrinsic dimensions: a cold image is contained in that frame, not allowed
- * to resize it at decode. The next mount can use the measured dimensions. */
+ * to resize it at decode. The next mount can use the measured dimensions.
+ *
+ * `preservePendingFrame` is for a caller whose content is all absolutely
+ * positioned (a generated image): it always gets a frame, since without one a
+ * successful retry renders into a zero-height box, and it keeps the frame
+ * shown while its tool was pending. */
export function useMediaImage(
path: string,
fallbackRatio: number,
@@ -44,9 +49,10 @@ export function useMediaImage(
key,
ownerKey,
path,
+ fallbackRatio,
// A source that just failed gets no frame: it would only collapse again.
frameStyle:
- !dimensions && isKnownBrokenMediaImage(key)
+ !dimensions && !preservePendingFrame && isKnownBrokenMediaImage(key)
? undefined
: ({
aspectRatio: ratio,
@@ -62,7 +68,8 @@ export function useMediaImage(
// Source changes must not paint the previous image/geometry even for one
// commit. React retries this component before committing its children.
- if (state.key !== key) {
+ // With no source yet, nothing is painted, so a hint arriving late reshapes.
+ if (state.key !== key || (!path && state.fallbackRatio !== fallbackRatio)) {
const next = initialState()
// A generated result fills the frame already visible while its tool was
// pending. Media state still resets; geometry is never inherited across
@@ -83,13 +90,13 @@ export function useMediaImage(
rememberMediaImageFailure(key)
}
- setState(current => ({ ...current, src, failed: !src }))
+ setState(current => (current.key === key ? { ...current, src, failed: !src } : current))
}
},
() => {
if (!cancelled) {
rememberMediaImageFailure(key)
- setState(current => ({ ...current, failed: true }))
+ setState(current => (current.key === key ? { ...current, failed: true } : current))
}
}
)