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(mac): the chat agent actually sees the image it just generated

purplea8720304ed8d
diff --git a/src/api/__tests__/vision-feedback-source.test.ts b/src/api/__tests__/vision-feedback-source.test.ts
new file mode 100644
index 00000000..e2342614
--- /dev/null
+++ b/src/api/__tests__/vision-feedback-source.test.ts
@@ -0,0 +1,52 @@
+/**
+ * Vision feedback needs the bytes of the image the agent just made — that is
+ * what stops the model describing its own render from the prompt alone.
+ *
+ * On Windows the render is a ComfyUI /view URL and the localhost proxy fetches
+ * it. On macOS the MLX lane hands back a `blob:` URL (an in-memory PNG, no
+ * server at all), which the proxy cannot fetch: it threw, buildVisionFeedback
+ * swallowed the error, and the step silently did nothing. These pin the source
+ * dispatch so a Mac render reaches the model.
+ */
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+
+const fetchLocalhostBytes = vi.fn()
+vi.mock('../backend', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('../backend')>()),
+ fetchLocalhostBytes: (...a: unknown[]) => fetchLocalhostBytes(...a),
+}))
+
+import { fetchComfyImageBase64 } from '../comfyui'
+
+const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47])
+const PNG_B64 = 'iVBORw=='
+
+beforeEach(() => {
+ fetchLocalhostBytes.mockReset()
+ vi.stubGlobal('fetch', vi.fn(async () => ({ arrayBuffer: async () => PNG.buffer })))
+})
+
+describe('fetchComfyImageBase64 — where the bytes come from', () => {
+ it('reads a blob: URL directly, never through the localhost proxy', async () => {
+ // The Mac path. Routing this through the proxy is what made the agent
+ // hallucinate its own picture.
+ const b64 = await fetchComfyImageBase64('blob:http://localhost/9f2a-1')
+ expect(b64).toBe(PNG_B64)
+ expect(fetchLocalhostBytes).not.toHaveBeenCalled()
+ })
+
+ it('reads a data: URL directly too', async () => {
+ const b64 = await fetchComfyImageBase64('data:image/png;base64,iVBORw==')
+ expect(b64).toBe(PNG_B64)
+ expect(fetchLocalhostBytes).not.toHaveBeenCalled()
+ })
+
+ it('still proxies a real ComfyUI /view URL', async () => {
+ // Windows must be untouched: that URL is a server the WebView cannot read
+ // cross-origin, which is the whole reason the proxy exists.
+ fetchLocalhostBytes.mockResolvedValueOnce(PNG)
+ const url = 'http://127.0.0.1:8188/view?filename=out.png'
+ expect(await fetchComfyImageBase64(url)).toBe(PNG_B64)
+ expect(fetchLocalhostBytes).toHaveBeenCalledWith(url)
+ })
+})
diff --git a/src/api/comfyui.ts b/src/api/comfyui.ts
index b06d9470..3c554ec7 100644
--- a/src/api/comfyui.ts
+++ b/src/api/comfyui.ts
@@ -1285,7 +1285,20 @@ export function getImageUrl(filename: string, subfolder: string = '', type: stri
* SEES the picture it made and can comment on it.
*/
export async function fetchComfyImageBase64(url: string): Promise<string> {
- const bytes = await fetchLocalhostBytes(url)
+ // Not every generated image is a ComfyUI /view URL any more. The macOS MLX
+ // lane hands the chat agent a `blob:` URL (an in-memory PNG, no server), and
+ // the localhost proxy below cannot fetch one — it threw, the caller swallowed
+ // it, and the vision-feedback step silently gave up. The model then described
+ // the picture it had just made from the prompt alone, i.e. hallucinated it:
+ // exactly the failure the provider-aware fix cured on Windows.
+ if (url.startsWith('blob:') || url.startsWith('data:')) {
+ const buf = await (await fetch(url)).arrayBuffer()
+ return bytesToBase64(new Uint8Array(buf))
+ }
+ return bytesToBase64(await fetchLocalhostBytes(url))
+}
+
+function bytesToBase64(bytes: Uint8Array): string {
let binary = ''
const CHUNK = 0x8000 // avoid arg-count limits on String.fromCharCode
for (let i = 0; i < bytes.length; i += CHUNK) {