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(agent): the input picture of an edit or a start image is read through the proxy (D4)

purplef68266167df3
diff --git a/src/api/__tests__/input-image-through-the-proxy.test.ts b/src/api/__tests__/input-image-through-the-proxy.test.ts
new file mode 100644
index 00000000..28faae06
--- /dev/null
+++ b/src/api/__tests__/input-image-through-the-proxy.test.ts
@@ -0,0 +1,84 @@
+/**
+ * D4 (Discord 07.11.2026, "i can't edit image, ref2image in qwen image 2.1"):
+ * in the agent chat every edit and every start or reference picture ended as
+ * "Image generation failed: Failed to fetch". resolveInputImage read the
+ * picture from ComfyUI with the browser's own `fetch`, a cross-origin request
+ * from the app's window that a ComfyUI without a CORS header for it refuses.
+ * It now reads through the app's localhost proxy like every other ComfyUI
+ * read, and a missing file is still told apart from an unreachable one.
+ *
+ * Run: npx vitest run src/api/__tests__/input-image-through-the-proxy.test.ts
+ */
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
+
+const io = vi.hoisted(() => ({
+ files: new Map<string, Uint8Array>(),
+ proxied: [] as string[],
+ uploads: [] as string[],
+}))
+
+vi.mock('../backend', async (orig) => ({
+ ...(await orig<typeof import('../backend')>()),
+ isTauri: () => true,
+ // proxy_localhost_stream: bytes for a file ComfyUI has, "HTTP 404: …" otherwise.
+ fetchLocalhostBytes: vi.fn(async (url: string) => {
+ io.proxied.push(url)
+ const q = new URL(url, 'http://comfy.test').searchParams
+ const hit = io.files.get(`${q.get('type')}/${q.get('filename')}`)
+ if (!hit) throw new Error('HTTP 404: Not Found')
+ return hit
+ }),
+ backendCall: vi.fn(async (cmd: string, args: { filename?: string }) => {
+ if (cmd === 'comfy_upload_image') {
+ io.uploads.push(String(args.filename))
+ return JSON.stringify({ name: args.filename, subfolder: '', type: 'input' })
+ }
+ return {}
+ }),
+}))
+
+import { resolveInputImage } from '../vram-handoff'
+
+const PIXELS = new Uint8Array([0x89, 0x50, 0x4e, 0x47])
+let browserFetch: ReturnType<typeof vi.fn>
+
+beforeEach(() => {
+ io.files.clear()
+ io.proxied = []
+ io.uploads = []
+ // The browser's fetch is what failed in the app; it must not be asked.
+ browserFetch = vi.fn(async () => { throw new TypeError('Failed to fetch') })
+ vi.stubGlobal('fetch', browserFetch)
+})
+afterEach(() => vi.unstubAllGlobals())
+
+describe('D4: the input picture of an edit or a start image is read through the proxy', () => {
+ it('an earlier result (output folder) is read by the proxy and handed to the input folder', async () => {
+ io.files.set('output/ComfyUI_00012_.png', PIXELS)
+ const out = await resolveInputImage('/comfyui/view?filename=ComfyUI_00012_.png&subfolder=&type=output')
+ expect(out.name).toBe('ComfyUI_00012_.png')
+ expect(io.proxied[0]).toContain('filename=ComfyUI_00012_.png')
+ expect(io.uploads).toEqual(['ComfyUI_00012_.png'])
+ expect(browserFetch).not.toHaveBeenCalled()
+ })
+
+ it('a staged chat attachment (input folder) is found after the 404 in the output folder', async () => {
+ io.files.set('input/lu-chat-1xc1.png', PIXELS)
+ const out = await resolveInputImage('lu-chat-1xc1.png')
+ expect(out.name).toBe('lu-chat-1xc1.png')
+ expect(io.proxied.map((u) => new URL(u, 'http://x').searchParams.get('type'))).toEqual(['output', 'input'])
+ expect(io.uploads).toEqual([])
+ expect(browserFetch).not.toHaveBeenCalled()
+ })
+
+ it('a name in neither folder still says so, not "Failed to fetch"', async () => {
+ await expect(resolveInputImage('image.png')).rejects.toThrow(/no file of that name in ComfyUI's output or input folder/)
+ expect(browserFetch).not.toHaveBeenCalled()
+ })
+
+ it('an unreachable ComfyUI names the cause of the proxy', async () => {
+ const { fetchLocalhostBytes } = await import('../backend')
+ vi.mocked(fetchLocalhostBytes).mockRejectedValueOnce(new Error('proxy_localhost_stream: connection refused'))
+ await expect(resolveInputImage('/comfyui/view?filename=a.png&type=output')).rejects.toThrow(/from ComfyUI: proxy_localhost_stream: connection refused/)
+ })
+})
diff --git a/src/api/vram-handoff.ts b/src/api/vram-handoff.ts
index 5c52d16f..6b1e06d3 100644
--- a/src/api/vram-handoff.ts
+++ b/src/api/vram-handoff.ts
@@ -46,7 +46,7 @@
import { setChatBackendsRestore, holdChatBackends } from '../lib/chat-backends-gate'
import { gpuRolesSeparate } from '../lib/gpu-roles'
-import { backendCall, ollamaUrl, localFetch, isOllamaLocal, isWindows } from './backend'
+import { backendCall, ollamaUrl, localFetch, isOllamaLocal, isWindows, fetchLocalhostBytes } from './backend'
import type { ComfyApiGraph } from '../types/comfy-graph'
import { asNumber, asRecordArray, asString, prop } from '../types/json-guards'
import { listRunningModels, loadModel, unloadModel } from './ollama'
@@ -2276,21 +2276,18 @@ export async function resolveInputImage(ref: string): Promise<ResolvedInputImage
if (VIDEO_FILE_RE.test(name)) {
throw new Error(`"${name}" is a video clip, and inputImage takes a still image. Pass a picture (a generated image or "attachment N"); continuing a clip is not something this tool does.`)
}
- const resp = await fetch(url)
+ const read = await readInputFile(url, name, view !== null || !/^https?:\/\//i.test(ref))
// A12 (20.10.2026): a bare name that is not in the output folder may
// already sit in ComfyUI's input folder, where a chat attachment is staged
// (builtin-tools, stageChatAttachment) and where a user puts a picture by
// hand. LoadImage reads from there, so it needs no second upload.
- if (resp.status === 404 && (view || !/^https?:\/\//i.test(ref))) {
- const fromInput = await fetch(getImageUrl(name, '', 'input'))
- if (fromInput.ok) {
- const blob = await fromInput.blob()
- if (blob && blob.size > 0) return { name, ...(await probeSize(blob)) }
- }
+ if (read === 404 && (view || !/^https?:\/\//i.test(ref))) {
+ const fromInput = await readInputFile(getImageUrl(name, '', 'input'), name, true)
+ if (fromInput !== 404 && fromInput.size > 0) return { name, ...(await probeSize(fromInput)) }
throw new Error(`could not find input image "${ref}": there is no file of that name in ComfyUI's output or input folder. A picture the user attached in the chat is passed as "attachment 1" (its number in the chat), not by a filename.`)
}
- if (!resp.ok) throw new Error(`could not read input image "${name}": ComfyUI answered HTTP ${resp.status}.`)
- const blob = await resp.blob()
+ if (read === 404) throw new Error(`could not read input image "${name}": the address answered HTTP 404.`)
+ const blob = read
if (!blob || blob.size === 0) {
throw new Error(`could not read input image "${ref}", ComfyUI returned an empty file`)
}
@@ -2304,6 +2301,32 @@ export async function resolveInputImage(ref: string): Promise<ResolvedInputImage
return { name: uploaded, ...size }
}
+/**
+ * One input picture, read as a Blob, or 404. A file of ComfyUI goes through
+ * the app's localhost proxy, like every other ComfyUI read (D4, Discord
+ * 07.11.2026): the plain `fetch` this used before is a cross-origin request
+ * from the app's window, and against a ComfyUI that sends no CORS header for
+ * it (one LU did not start, or started without `--enable-cors-header`) it
+ * ended as "Image generation failed: Failed to fetch" on every edit and every
+ * start or reference image in the chat. A web address keeps the browser fetch.
+ */
+async function readInputFile(url: string, name: string, comfyFile: boolean): Promise<Blob | 404> {
+ const type = /\.jpe?g$/i.test(name) ? 'image/jpeg' : /\.webp$/i.test(name) ? 'image/webp' : 'image/png'
+ if (!comfyFile) {
+ const resp = await fetch(url)
+ if (resp.status === 404) return 404
+ if (!resp.ok) throw new Error(`could not read input image "${name}": the address answered HTTP ${resp.status}.`)
+ return resp.blob()
+ }
+ try {
+ return new Blob([await fetchLocalhostBytes(url)], { type })
+ } catch (e) {
+ const msg = e instanceof Error ? e.message : String(e)
+ if (/^HTTP 404\b/.test(msg)) return 404
+ throw new Error(`could not read input image "${name}" from ComfyUI: ${msg}`)
+ }
+}
+
async function probeSize(blob: Blob): Promise<{ width: number; height: number }> {
try {
const bmp = await createImageBitmap(blob)