Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(chat): a scene background reads the real render result (C58)

purple370f4ebdce7b
diff --git a/src/lib/__tests__/chat-background.test.ts b/src/lib/__tests__/chat-background.test.ts
index dca0f7d4..0f330788 100644
--- a/src/lib/__tests__/chat-background.test.ts
+++ b/src/lib/__tests__/chat-background.test.ts
@@ -1,5 +1,11 @@
-import { describe, it, expect } from 'vitest'
-import { generatedOk, resultUrl } from '../chat-background'
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+
+const backendCall = vi.fn()
+const fetchComfyImageBase64 = vi.fn()
+vi.mock('../../api/backend', () => ({ backendCall: (...a: unknown[]) => backendCall(...a) }))
+vi.mock('../../api/comfyui', () => ({ fetchComfyImageBase64: (...a: unknown[]) => fetchComfyImageBase64(...a) }))
+
+import { generatedOk, readResultPicture, resultUrl } from '../chat-background'
describe('reading the image_generate result for a scene background', () => {
it('finds the address on the last line', () => {
@@ -7,13 +13,39 @@ describe('reading the image_generate result for a scene background', () => {
expect(resultUrl(r)).toBe('http://127.0.0.1:8188/view?filename=LU_00012_.png&type=output')
expect(generatedOk(r)).toBe(true)
})
- it('a Mac render hands a blob address', () => {
- expect(generatedOk('Image generated: mflux.png (prompt: "x")\nblob:http://localhost/abc')).toBe(true)
+ // Found in the real render against a test ComfyUI (10.11.2026): in a browser
+ // the address is the dev proxy's relative one, and the picture was made but
+ // the panel called it a failure.
+ it('takes the relative /comfyui/view address of the browser build', () => {
+ const r = 'Image generated: wide_background_00001_.png (prompt: "x")\n/comfyui/view?filename=wide_background_00001_.png&subfolder=&type=output'
+ expect(generatedOk(r)).toBe(true)
+ })
+ it('takes the file a Mac render wrote', () => {
+ expect(generatedOk('Image generated: mlx-1.png (prompt: "x")\nfile:///Users/a/Pictures/mlx-1.png')).toBe(true)
+ })
+ it('a prompt that quotes an address is not the result', () => {
+ expect(generatedOk('Image generated: a.png (prompt: "like http://localhost:8188/view?filename=x.png")')).toBe(false)
})
it.each([
'Error: No local image model is installed yet.',
'Cannot generate: width must be a number from 64 to 4096 pixels',
'Image generation cancelled.',
'Image generated: but no address',
+ 'Image generated: clip.mp4 (prompt: "x")\ndata:video/mp4;base64,AAAA',
])('refuses %s', (r) => expect(generatedOk(r)).toBe(false))
})
+
+describe('reading the picture behind the address', () => {
+ beforeEach(() => { backendCall.mockReset(); fetchComfyImageBase64.mockReset() })
+ it('a Mac file is read through read_media_file', async () => {
+ backendCall.mockResolvedValue('QUJD')
+ expect(await readResultPicture('file:///Users/a/My%20Pictures/mlx-1.png')).toBe('QUJD')
+ expect(backendCall).toHaveBeenCalledWith('read_media_file', { path: '/Users/a/My Pictures/mlx-1.png' })
+ expect(fetchComfyImageBase64).not.toHaveBeenCalled()
+ })
+ it('a ComfyUI address is fetched from ComfyUI', async () => {
+ fetchComfyImageBase64.mockResolvedValue('REVG')
+ expect(await readResultPicture('/comfyui/view?filename=a.png&type=output')).toBe('REVG')
+ expect(backendCall).not.toHaveBeenCalled()
+ })
+})
diff --git a/src/lib/chat-background.ts b/src/lib/chat-background.ts
index a16ad09f..a59275d3 100644
--- a/src/lib/chat-background.ts
+++ b/src/lib/chat-background.ts
@@ -14,6 +14,8 @@
* and the file is a few hundred kB, not the 10 MB a render can be.
*/
import { storeAttachment } from './chat-attachments'
+import { resultMediaUrl } from './chat-media-refs'
+import { fileUrlToPath } from './local-media-url'
export const BG_EDGE = 1920
const MAX_INPUT_BYTES = 40 * 1024 * 1024
@@ -49,14 +51,13 @@ export async function storeBackground(blob: Blob): Promise<string> {
}
/** The address of the picture in an image_generate result
- * (`Image generated: <file> (prompt: "...")\n<url>`), or null. */
+ * (`Image generated: <file> (prompt: "...")\n<url>`), or null. The same
+ * reading as the chat's own media (lib/chat-media-refs): a ComfyUI /view
+ * address, absolute or the dev proxy's relative `/comfyui/view?…`, a
+ * `file://` the Mac wrote, or a `data:` picture. */
export function resultUrl(result: string): string | null {
- const lines = result.split('\n').map((l) => l.trim()).filter(Boolean)
- for (let i = lines.length - 1; i >= 0; i--) {
- const m = /^(https?:\/\/\S+|blob:\S+|data:image\/\S+)$/.exec(lines[i])
- if (m) return m[1]
- }
- return null
+ const url = resultMediaUrl(result)
+ return url && !/^data:video\//i.test(url) ? url : null
}
/** Whether the tool said it made a picture. Its refusals and failures are
@@ -65,6 +66,19 @@ export function generatedOk(result: string): boolean {
return /^Image generated:/i.test(result.trim()) && resultUrl(result) !== null
}
+/** The bytes behind a result address, base64. A Mac render is a file the
+ * app wrote; it is read the way the vision loop reads it (read_media_file). */
+export async function readResultPicture(url: string): Promise<string> {
+ if (/^file:\/\//i.test(url)) {
+ const path = fileUrlToPath(url)
+ if (!path) throw new Error('The picture file could not be found.')
+ const { backendCall } = await import('../api/backend')
+ return backendCall<string>('read_media_file', { path })
+ }
+ const { fetchComfyImageBase64 } = await import('../api/comfyui')
+ return fetchComfyImageBase64(url)
+}
+
/**
* A new background from `prompt`, rendered locally through the chat's own
* image path, owned by `conversationId` so Stop ends exactly this render.
@@ -73,11 +87,9 @@ export async function renderSceneBackground(prompt: string, conversationId: stri
const { generateMediaForRemote } = await import('../api/mcp/builtin-tools')
const result = await generateMediaForRemote('image', { prompt, aspectRatio: '16:9' }, conversationId)
if (!generatedOk(result)) throw new Error(result.trim().split('\n')[0] || 'The picture was not made.')
- const url = resultUrl(result)!
- const { fetchComfyImageBase64 } = await import('../api/comfyui')
- const b64 = await fetchComfyImageBase64(url)
+ const b64 = await readResultPicture(resultUrl(result)!)
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))
- return storeBackground(new Blob([bytes], { type: 'image/png' }))
+ return storeBackground(new Blob([bytes]))
}
/** Stop a render started by renderSceneBackground. */