From 2b863a37cead71a7eba1d3cdc635af948c869c2a Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Fri, 13 Nov 2026 16:35:47 -0800 Subject: [PATCH 1/3] feat(apps): let an app page run on a ComfyUI build on this computer An app page opened in the Apps view could run on Comfy Cloud only. Its "This computer" choice talked to a local ComfyUI the way a browser does, which needs CORS turned on by hand and was never tried inside Desktop. The page now gets window.comfyDesktopLocal (ComfyDesktopHostedLocalBridge, bridge types 0.10.0) next to window.comfyDesktopAuth: - getLocal / startLocal: Desktop picks one of the person's installed local builds that is new enough for the page (running first, then the one used last, then the newest) and starts it without a window through the existing launch path. With no fitting build it says so in words. - localFetch: Desktop forwards the eight calls a run needs and nothing else. History, result files, removal from the queue and stopping work only for runs the page started. The queue is handed over without other runs' ids or graphs. Uploads go to input/comfy-apps and never replace a file. - onLocalEvent: Desktop listens to the build's socket as one client and passes on updates for the page's own runs. - localDownload: a safetensors file on Hugging Face, saved under its own name in a models folder of that build through the download manager. The page still builds and follows its own runs. Desktop holds no app logic, so a change to an app stays a website change. A build Desktop started for apps is stopped after ten idle minutes with an empty queue, unless the person has opened its window. A build the person started is never stopped. Setting up a build when the person has none is not in this change. --- e2e/apps-local.test.ts | 279 +++++++ e2e/support/fakeComfyApi.ts | 6 + e2e/support/fakeComfyInstall.ts | 5 +- locales/en.json | 10 +- locales/zh.json | 10 +- .../comfyui-desktop-bridge-types/README.md | 24 +- .../comfyDesktopBridge.d.ts | 79 ++ .../comfyui-desktop-bridge-types/package.json | 2 +- src/main/apps/__fixtures__/fakeComfyApi.ts | 144 ++++ src/main/apps/localBuild.test.ts | 207 +++++ src/main/apps/localBuild.ts | 113 +++ src/main/apps/localCalls.test.ts | 282 +++++++ src/main/apps/localCalls.ts | 191 +++++ src/main/apps/localComfy.integration.test.ts | 173 +++++ src/main/apps/localComfy.test.ts | 704 ++++++++++++++++++ src/main/apps/localComfy.ts | 411 ++++++++++ src/main/apps/localModelRequest.test.ts | 74 ++ src/main/apps/localModelRequest.ts | 40 + src/main/apps/localRelay.test.ts | 177 +++++ src/main/apps/localRelay.ts | 113 +++ src/main/lib/ipc/index.ts | 2 + .../lib/ipc/registerAppsLocalHandlers.test.ts | 86 +++ src/main/lib/ipc/registerAppsLocalHandlers.ts | 49 ++ src/preload/appsPreload.ts | 18 +- src/types/comfyDesktopBridge.ts | 87 +++ 25 files changed, 3275 insertions(+), 11 deletions(-) create mode 100644 e2e/apps-local.test.ts create mode 100644 e2e/support/fakeComfyApi.ts create mode 100644 src/main/apps/__fixtures__/fakeComfyApi.ts create mode 100644 src/main/apps/localBuild.test.ts create mode 100644 src/main/apps/localBuild.ts create mode 100644 src/main/apps/localCalls.test.ts create mode 100644 src/main/apps/localCalls.ts create mode 100644 src/main/apps/localComfy.integration.test.ts create mode 100644 src/main/apps/localComfy.test.ts create mode 100644 src/main/apps/localComfy.ts create mode 100644 src/main/apps/localModelRequest.test.ts create mode 100644 src/main/apps/localModelRequest.ts create mode 100644 src/main/apps/localRelay.test.ts create mode 100644 src/main/apps/localRelay.ts create mode 100644 src/main/lib/ipc/registerAppsLocalHandlers.test.ts create mode 100644 src/main/lib/ipc/registerAppsLocalHandlers.ts diff --git a/e2e/apps-local.test.ts b/e2e/apps-local.test.ts new file mode 100644 index 000000000..17f7bc1af --- /dev/null +++ b/e2e/apps-local.test.ts @@ -0,0 +1,279 @@ +import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http' +import type { AddressInfo } from 'node:net' +import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +import { expect, test } from '@playwright/test' +import { launchApp, type AppContext } from './launchApp' +import { WebContentsPage } from './support/cdpPages' +import { getRunningSessionSnapshot } from './support/devHooks' +import { + FAKE_COMFY_API_SERVER, + FAKE_OUTPUT_TEXT, + SOMEONE_ELSES_RUN, + type FakeComfySeen +} from './support/fakeComfyApi' +import { reserveFreePort, writeFakeComfyInstall } from './support/fakeComfyInstall' + +const BUILD_ID = 'inst-apps-local' +const BUILD_NAME = 'Apps Test Build' +const SIDEBAR_APPS = '[data-testid="app-sidebar-apps"]' +const CUTOUT_CARD = '[data-testid="app-card-remove-background"]' +const CUTOUT_PAGE = '/apps/remove-background/desktop.html' +const LONG_WAIT = { timeout: 30_000 } + +const APP_PAGE = ` + + Remove Background page + + All apps +

pending

+ + +

+ +

+ + +` + +class FakeAppsSite { + port = 0 + private server: Server | null = null + + get url(): string { + return `http://127.0.0.1:${this.port}` + } + + async start(): Promise { + const server = createServer((req, res) => this.route(req, res)) + this.server = server + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) + this.port = (server.address() as AddressInfo).port + } + + async stop(): Promise { + const server = this.server + if (!server) return + this.server = null + server.closeAllConnections() + await new Promise((resolve) => server.close(resolve)) + } + + private route(req: IncomingMessage, res: ServerResponse): void { + const { pathname } = new URL(req.url ?? '/', this.url) + if (pathname === '/apps/apps.json') { + res.writeHead(200, { 'content-type': 'application/json' }) + res.end( + JSON.stringify([ + { id: 'remove-background', page: CUTOUT_PAGE, title: 'Remove Background', blurb: '' } + ]) + ) + return + } + res.writeHead(pathname === CUTOUT_PAGE ? 200 : 404, { + 'content-type': 'text/html; charset=utf-8' + }) + res.end(pathname === CUTOUT_PAGE ? APP_PAGE : 'Not found') + } +} + +let site: FakeAppsSite +let ctx: AppContext +let profileDir = '' +let installPath = '' +let comfyPort = 0 +let inheritedAppsUrl: string | undefined + +function appPage(): WebContentsPage { + return new WebContentsPage(ctx.app, CUTOUT_PAGE) +} + +function windowCount(): Promise { + return ctx.app.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows().length) +} + +async function whatComfySaw(): Promise { + const port = (await getRunningSessionSnapshot(ctx.app, BUILD_ID))?.port + return (await (await fetch(`http://127.0.0.1:${port}/e2e/seen`)).json()) as FakeComfySeen +} + +test.describe.configure({ mode: 'serial' }) +test.setTimeout(180_000) + +test.beforeAll(async () => { + inheritedAppsUrl = process.env['COMFY_APPS_URL'] + site = new FakeAppsSite() + await site.start() + process.env['COMFY_APPS_URL'] = `${site.url}/apps/` + profileDir = await mkdtemp(path.join(os.tmpdir(), 'comfy-apps-local-profile-')) + installPath = await mkdtemp(path.join(os.tmpdir(), 'comfy-apps-local-build-')) + comfyPort = await reserveFreePort() + await writeFakeComfyInstall({ installPath, port: comfyPort, serverScript: FAKE_COMFY_API_SERVER }) + await writeFile( + path.join(installPath, 'ComfyUI', 'comfyui_version.py'), + '__version__ = "0.42.1"\n' + ) + ctx = await launchApp( + { + profileDir, + settings: { firstUseCompleted: true, telemetryEnabled: false, language: 'en' }, + installations: [ + { + id: BUILD_ID, + name: BUILD_NAME, + sourceId: 'comfybuilder', + sourceLabel: 'ComfyBuilder', + installPath, + status: 'installed', + launchArgs: `--port ${comfyPort}`, + seen: true + } + ] + }, + { stayOnHome: true } + ) +}) + +test.afterAll(async () => { + await ctx?.panel.evaluate(`window.api.stopComfyUI(${JSON.stringify(BUILD_ID)})`).catch(() => {}) + await ctx?.cleanup() + await site?.stop() + if (profileDir) await rm(profileDir, { recursive: true, force: true }) + if (installPath) await rm(installPath, { recursive: true, force: true }) + if (inheritedAppsUrl === undefined) delete process.env['COMFY_APPS_URL'] + else process.env['COMFY_APPS_URL'] = inheritedAppsUrl +}) + +test('An app page is offered the build on this computer, stopped, by name and version @linux', async () => { + await ctx.panel.waitForVisible(SIDEBAR_APPS) + expect(await ctx.panel.click(SIDEBAR_APPS)).toBe(true) + await ctx.panel.waitForVisible(CUTOUT_CARD) + expect(await ctx.panel.click(CUTOUT_CARD)).toBe(true) + + await expect + .poll(() => appPage().textOf('#local'), LONG_WAIT) + .toBe(`stopped | ${BUILD_NAME} | 0.42.1`) + expect(await getRunningSessionSnapshot(ctx.app, BUILD_ID)).toBeNull() +}) + +test('Starting from the page runs that build with no ComfyUI window @linux', async () => { + const windowsBefore = await windowCount() + + expect(await appPage().click('#start')).toBe(true) + + await expect + .poll(() => appPage().textOf('#local'), { timeout: 120_000 }) + .toBe(`running | ${BUILD_NAME} | 0.42.1`) + expect(await getRunningSessionSnapshot(ctx.app, BUILD_ID)).not.toBeNull() + expect(await windowCount(), 'no window was opened for ComfyUI').toBe(windowsBefore) +}) + +test('A run goes from a picture to its result, with updates on the way, through Desktop only @linux', async () => { + expect(await appPage().click('#run')).toBe(true) + + await expect.poll(() => appPage().textOf('#result'), LONG_WAIT).toContain('"result"') + const result = JSON.parse((await appPage().textOf('#result')) ?? '{}') as Record + expect(result).toEqual({ + image: 'comfy-apps/photo.png', + result: FAKE_OUTPUT_TEXT, + type: 'image/png', + updates: ['execution_start', 'executing', 'execution_success'], + queue: { running: [{ n: 1, id: null, app: null, seconds: null }], pending: [] } + }) + + const seen = await whatComfySaw() + expect(seen.sockets, 'Desktop listens as one client').toHaveLength(1) + expect(seen.prompts).toEqual([ + { id: 'e2e-run-1', clientId: seen.sockets[0], app: 'Remove Background', nodes: ['load'] } + ]) + expect(seen.uploads).toEqual([ + { name: 'photo.png', subfolder: 'comfy-apps', overwrite: false, bytes: expect.any(Number) } + ]) +}) + +test('The page cannot reach anything that is not its own run @linux', async () => { + expect(await appPage().click('#pry')).toBe(true) + + await expect.poll(() => appPage().textOf('#pried'), LONG_WAIT).toBe('403,403,403,403,403') + const seen = await whatComfySaw() + expect(seen.asked).not.toContain('GET /system_stats') + expect(seen.asked).not.toContain(`GET /history/${SOMEONE_ELSES_RUN}`) + expect( + seen.asked.filter((asked) => asked === 'GET /view'), + 'only the run fetched its own file' + ).toHaveLength(1) + expect(seen.interrupted).toEqual([]) +}) diff --git a/e2e/support/fakeComfyApi.ts b/e2e/support/fakeComfyApi.ts new file mode 100644 index 000000000..7cb71fee5 --- /dev/null +++ b/e2e/support/fakeComfyApi.ts @@ -0,0 +1,6 @@ +export { + FAKE_COMFY_API_SERVER, + FAKE_OUTPUT_TEXT, + SOMEONE_ELSES_RUN +} from '../../src/main/apps/__fixtures__/fakeComfyApi' +export type { FakeComfySeen } from '../../src/main/apps/__fixtures__/fakeComfyApi' diff --git a/e2e/support/fakeComfyInstall.ts b/e2e/support/fakeComfyInstall.ts index b024dd1d5..ad786085d 100644 --- a/e2e/support/fakeComfyInstall.ts +++ b/e2e/support/fakeComfyInstall.ts @@ -116,13 +116,14 @@ export interface FakeComfyInstall { export async function writeFakeComfyInstall(opts: { installPath: string port: number + serverScript?: string }): Promise { - const { installPath, port } = opts + const { installPath, port, serverScript = SERVER_JS } = opts await mkdir(path.join(installPath, 'ComfyUI'), { recursive: true }) await writeFile(path.join(installPath, 'ComfyUI', 'main.py'), '# e2e stub\n') const serverPath = path.join(installPath, 'stub-server.cjs') - await writeFile(serverPath, SERVER_JS) + await writeFile(serverPath, serverScript) if (process.platform !== 'linux') { throw new Error( diff --git a/locales/en.json b/locales/en.json index 3a04d1660..f61869e9a 100644 --- a/locales/en.json +++ b/locales/en.json @@ -42,7 +42,15 @@ "noMatches": "No apps match “{query}”", "allApps": "All apps", "pageLoading": "Loading {app}", - "pageFailedTitle": "{app} didn't load" + "pageFailedTitle": "{app} didn't load", + "local": { + "noBuilds": "Add a local build in Comfy Desktop to run apps on this computer.", + "tooOld": "This app needs ComfyUI {version} or newer. Update a local build in Comfy Desktop.", + "couldNotStart": "Comfy Desktop could not start {name}.", + "notRunning": "ComfyUI is not running on this computer.", + "notForwarded": "Comfy Desktop does not pass this request to ComfyUI.", + "modelRefused": "Comfy Desktop only downloads safetensors models from Hugging Face." + } }, "assets": { "kindLabel": "Asset type", diff --git a/locales/zh.json b/locales/zh.json index 160650679..af34d1e45 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -42,7 +42,15 @@ "noMatches": "没有与“{query}”匹配的应用", "allApps": "全部应用", "pageLoading": "正在加载{app}", - "pageFailedTitle": "{app}未能加载" + "pageFailedTitle": "{app}未能加载", + "local": { + "noBuilds": "请先在 Comfy Desktop 中添加一个本地构建,才能在这台电脑上运行应用。", + "tooOld": "此应用需要 ComfyUI {version} 或更高版本。请在 Comfy Desktop 中更新一个本地构建。", + "couldNotStart": "Comfy Desktop 无法启动{name}。", + "notRunning": "这台电脑上的 ComfyUI 没有在运行。", + "notForwarded": "Comfy Desktop 不会把这个请求转给 ComfyUI。", + "modelRefused": "Comfy Desktop 只从 Hugging Face 下载 safetensors 模型。" + } }, "assets": { "kindLabel": "资产类型", diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index 57d38f99e..3ad762678 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -41,15 +41,20 @@ while omitting part of it. ## Comfy pages hosted in Desktop's Apps view -A Comfy page that Desktop shows in its Apps view gets `window.comfyDesktopAuth` -instead: Desktop's account session, and nothing else of the bridge above. +A Comfy page that Desktop shows in its Apps view gets two objects instead, and +nothing else of the bridge above: `window.comfyDesktopAuth`, Desktop's account +session, and `window.comfyDesktopLocal`, a ComfyUI on this computer. ```ts -import type { ComfyDesktopHostedAuthBridge } from '@comfyorg/comfyui-desktop-bridge-types' +import type { + ComfyDesktopHostedAuthBridge, + ComfyDesktopHostedLocalBridge +} from '@comfyorg/comfyui-desktop-bridge-types' declare global { interface Window { comfyDesktopAuth?: ComfyDesktopHostedAuthBridge + comfyDesktopLocal?: ComfyDesktopHostedLocalBridge } } ``` @@ -57,8 +62,17 @@ declare global { The page reads the session with `getState()`, follows it with `onChanged()`, asks for the workspace credential with `getWorkspaceToken(workspaceId)` each time it calls Comfy Cloud, and starts a sign-in with `requestSignIn()`. It -cannot sign Desktop out or switch workspace. The object is absent in a -browser, so a page keeps its own sign-in as the fallback. +cannot sign Desktop out or switch workspace. + +For a run on this computer the page asks `getLocal({ minVersion })` which of +the person's builds Desktop would use, starts it with `startLocal()`, and then +talks to it through `localFetch()`. Desktop forwards only the calls a run +needs, and only for runs the page started; `onLocalEvent()` carries ComfyUI's +updates for those runs. `localDownload()` saves a safetensors model from +Hugging Face into that build's models folder. + +Both objects are absent in a browser, so a page keeps its own sign-in and its +own way to reach a local ComfyUI as the fallback. ## Provenance diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index de1d49231..9eddd9d9b 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -187,6 +187,85 @@ export type ComfyDesktopHostedAuthBridge = Required< 'getState' | 'getWorkspaceToken' | 'getPartnerNodeToken' | 'requestSignIn' | 'onChanged' > > +/** What a hosted page needs from the ComfyUI on this computer. */ +export interface ComfyDesktopLocalRequirements { + /** The oldest ComfyUI the page can run on, such as `0.41.0`. */ + minVersion?: string +} +/** + * The ComfyUI on this computer that Desktop uses for a hosted page. + * `running`: it answers now. `stopped`: one of the person's builds fits and + * `startLocal` starts it. `unsupported`: no build here can run the page, and + * `error` says why in words for the person. + */ +export interface ComfyDesktopLocalComfy { + state: 'running' | 'stopped' | 'unsupported' + /** True when it is one of the person's own builds. */ + own?: boolean + name?: string + version?: string + /** The `client_id` Desktop listens with; updates for runs sent through `localFetch` arrive on `onLocalEvent`. */ + clientId?: string + error?: string +} +/** + * One call to that ComfyUI. Desktop forwards only what a run needs: + * `GET /queue`, `GET /history/`, `GET /view`, `GET /object_info/`, + * `POST /prompt`, `POST /upload/image`, `POST /queue` to take a run out, and + * `POST /interrupt` to stop one. History, files, removal and stopping work + * only for runs the page started through this bridge. + */ +export interface ComfyDesktopLocalRequest { + method?: 'GET' | 'POST' + path: string + /** JSON text, or a file's bytes for `/upload/image`. */ + body?: string | Uint8Array + /** The name to save an upload under. */ + fileName?: string +} +/** ComfyUI's answer, or status 403 for a call Desktop does not forward. */ +export interface ComfyDesktopLocalAnswer { + status: number + contentType: string + body: Uint8Array +} +/** A safetensors file on Hugging Face to save as `name` in the models folder `folder`. */ +export interface ComfyDesktopLocalModel { + url: string + folder: string + name: string +} +export interface ComfyDesktopLocalDownload { + state: 'done' | 'failed' | 'cancelled' + error?: string +} +/** + * A model download's progress (`type: 'download'`, with `name`, `done` and + * `total` bytes in `data`), or one ComfyUI socket message about a run the page + * started (`execution_start`, `executing`, `progress`, `progress_state`, + * `execution_success`, `execution_error`, `execution_interrupted`). + */ +export interface ComfyDesktopLocalEvent { + type: string + data?: Record +} +/** + * A ComfyUI on this computer, run by Desktop, for a Comfy page hosted in + * Desktop's Apps view. Desktop exposes it to that page as + * `window.comfyDesktopLocal`. The page builds and follows its own runs; + * Desktop finds or starts the ComfyUI, forwards the page's calls to it and + * downloads a model the page asks for. + */ +export interface ComfyDesktopHostedLocalBridge { + getLocal(want?: ComfyDesktopLocalRequirements): Promise + /** Starts the ComfyUI if it is stopped and resolves once it answers, or with the state and `error` if it could not start. */ + startLocal(want?: ComfyDesktopLocalRequirements): Promise + /** Rejects when no ComfyUI is running to forward to. */ + localFetch(request: ComfyDesktopLocalRequest): Promise + /** Downloads into the running ComfyUI's models folder and resolves when the file is there. */ + localDownload(model: ComfyDesktopLocalModel): Promise + onLocalEvent(callback: (event: ComfyDesktopLocalEvent) => void): () => void +} export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise diff --git a/packages/comfyui-desktop-bridge-types/package.json b/packages/comfyui-desktop-bridge-types/package.json index acdb84d2c..7ce9e477e 100644 --- a/packages/comfyui-desktop-bridge-types/package.json +++ b/packages/comfyui-desktop-bridge-types/package.json @@ -1,6 +1,6 @@ { "name": "@comfyorg/comfyui-desktop-bridge-types", - "version": "0.9.0", + "version": "0.10.0", "description": "TypeScript definitions for the Comfy Desktop hosted frontend bridge", "type": "module", "main": "./index.js", diff --git a/src/main/apps/__fixtures__/fakeComfyApi.ts b/src/main/apps/__fixtures__/fakeComfyApi.ts new file mode 100644 index 000000000..78b6bfe00 --- /dev/null +++ b/src/main/apps/__fixtures__/fakeComfyApi.ts @@ -0,0 +1,144 @@ +export const FAKE_OUTPUT_TEXT = 'e2e cutout bytes' +export const SOMEONE_ELSES_RUN = 'someone-elses-run' + +export interface FakeComfySeen { + asked: string[] + sockets: string[] + prompts: { id: string; clientId: string; app: string | null; nodes: string[] }[] + uploads: { name: string; subfolder: string; overwrite: boolean; bytes: number }[] + interrupted: string[] +} + +export const FAKE_COMFY_API_SERVER = ` +const http = require('node:http') +const crypto = require('node:crypto') +const args = process.argv.slice(2) +const portIndex = args.indexOf('--port') +const port = portIndex === -1 ? 8188 : Number(args[portIndex + 1]) +const seen = { asked: [], sockets: [], prompts: [], uploads: [], interrupted: [] } +const sockets = new Map() +const history = {} +const OUTPUT = Buffer.from(${JSON.stringify(FAKE_OUTPUT_TEXT)}) +const WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' + +function frame(text) { + const payload = Buffer.from(text) + const header = + payload.length < 126 + ? Buffer.from([0x81, payload.length]) + : Buffer.from([0x81, 126, payload.length >> 8, payload.length & 0xff]) + return Buffer.concat([header, payload]) +} + +function tell(clientId, message) { + const socket = sockets.get(clientId) + if (socket) socket.write(frame(JSON.stringify(message))) +} + +function readBody(req) { + return new Promise((resolve) => { + const chunks = [] + req.on('data', (chunk) => chunks.push(chunk)) + req.on('end', () => resolve(Buffer.concat(chunks))) + }) +} + +function json(res, value, status = 200) { + res.writeHead(status, { 'content-type': 'application/json' }) + res.end(JSON.stringify(value)) +} + +async function upload(req, res) { + const body = await readBody(req) + const text = body.toString('latin1') + const name = (/filename="([^"]+)"/.exec(text) || [])[1] || '' + const subfolder = (/name="subfolder"\\r\\n\\r\\n([^\\r]*)/.exec(text) || [])[1] || '' + seen.uploads.push({ name, subfolder, overwrite: /name="overwrite"/.test(text), bytes: body.length }) + json(res, { name, subfolder, type: 'input' }) +} + +async function prompt(req, res) { + const sent = JSON.parse((await readBody(req)).toString('utf8')) + const id = 'e2e-run-' + (seen.prompts.length + 1) + const tag = (sent.extra_data && sent.extra_data.comfy_app) || {} + seen.prompts.push({ + id, + clientId: sent.client_id, + app: typeof tag.name === 'string' ? tag.name : null, + nodes: Object.keys(sent.prompt || {}) + }) + json(res, { prompt_id: id, number: seen.prompts.length, node_errors: {} }) + setTimeout(() => { + tell(sent.client_id, { type: 'execution_start', data: { prompt_id: id, timestamp: Date.now() } }) + tell(sent.client_id, { type: 'executing', data: { prompt_id: id, node: 'cut' } }) + tell(sent.client_id, { type: 'executing', data: { prompt_id: ${JSON.stringify(SOMEONE_ELSES_RUN)}, node: 'private' } }) + history[id] = { + status: { status_str: 'success', completed: true }, + outputs: { save: { images: [{ filename: 'cutout_00001_.png', subfolder: 'apps', type: 'output' }] } } + } + tell(sent.client_id, { type: 'execution_success', data: { prompt_id: id } }) + }, 150) +} + +const server = http.createServer((req, res) => { + const url = new URL(req.url, 'http://127.0.0.1') + if (url.pathname === '/e2e/seen') return json(res, seen) + seen.asked.push(req.method + ' ' + url.pathname) + if (url.pathname === '/queue' && req.method === 'GET') { + return json(res, { + queue_running: [[1, ${JSON.stringify(SOMEONE_ELSES_RUN)}, { 3: { class_type: 'KSampler' } }, { client_id: 'their-window' }]], + queue_pending: [] + }) + } + if (url.pathname === '/upload/image' && req.method === 'POST') return void upload(req, res) + if (url.pathname === '/prompt' && req.method === 'POST') return void prompt(req, res) + if (url.pathname === '/interrupt' && req.method === 'POST') { + return void readBody(req).then((body) => { + seen.interrupted.push(JSON.parse(body.toString('utf8')).prompt_id) + json(res, {}) + }) + } + const run = /^\\/history\\/([\\w-]+)$/.exec(url.pathname) + if (run) return json(res, history[run[1]] ? { [run[1]]: history[run[1]] } : {}) + if (url.pathname === '/view') { + res.writeHead(200, { 'content-type': 'image/png' }) + return res.end(OUTPUT) + } + if (url.pathname.startsWith('/object_info/')) return json(res, {}) + if (url.pathname === '/system_stats') return json(res, { system: { comfyui_version: '0.42.1' } }) + res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' }) + res.end('ComfyUI (e2e stub)

ComfyUI stub

') +}) + +server.on('upgrade', (req, socket) => { + const url = new URL(req.url, 'http://127.0.0.1') + const accept = crypto + .createHash('sha1') + .update(req.headers['sec-websocket-key'] + WS_GUID) + .digest('base64') + socket.write( + 'HTTP/1.1 101 Switching Protocols\\r\\nUpgrade: websocket\\r\\nConnection: Upgrade\\r\\n' + + 'Sec-WebSocket-Accept: ' + accept + '\\r\\n\\r\\n' + ) + const clientId = url.searchParams.get('clientId') || '' + sockets.set(clientId, socket) + seen.sockets.push(clientId) + socket.on('data', () => {}) + socket.on('error', () => {}) + socket.on('close', () => { + if (sockets.get(clientId) === socket) sockets.delete(clientId) + }) +}) + +let attemptsLeft = 40 +server.on('error', (err) => { + if (err.code !== 'EADDRINUSE' || attemptsLeft <= 0) throw err + attemptsLeft -= 1 + setTimeout(() => server.listen(port, '127.0.0.1'), 250) +}) +server.on('listening', () => { + console.log('Starting server\\n') + console.log('To see the GUI go to: http://127.0.0.1:' + port) +}) +server.listen(port, '127.0.0.1') +` diff --git a/src/main/apps/localBuild.test.ts b/src/main/apps/localBuild.test.ts new file mode 100644 index 000000000..082321219 --- /dev/null +++ b/src/main/apps/localBuild.test.ts @@ -0,0 +1,207 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const mocks = vi.hoisted(() => ({ + records: [] as Record[], + sessions: new Map>(), + launching: new Set(), + operations: new Map(), + versionFiles: new Map() +})) + +vi.mock('fs', () => ({ + promises: { + readFile: async (file: string) => { + const text = mocks.versionFiles.get(file) + if (text === undefined) throw new Error('ENOENT') + return text + } + } +})) +vi.mock('../installations', () => ({ list: async () => mocks.records })) +vi.mock('../lib/ipc/shared', () => ({ + _hasActiveLaunch: (id: string) => mocks.launching.has(id), + _operationAborts: mocks.operations, + _runningSessions: mocks.sessions, + sourceMap: { + standalone: { category: 'local' }, + portable: { category: 'local' }, + desktop: { category: 'local' }, + cloud: { category: 'cloud' }, + remote: { category: 'remote' } + } +})) +vi.mock('../lib/models', () => ({ + resolveComfyDir: (installPath: string) => `${installPath}/ComfyUI` +})) + +import { chooseLocalBuild, runningPortOf, usableLocalBuilds, wantedVersion } from './localBuild' +import type { LocalBuild } from './localBuild' + +function record(over: Record = {}): Record { + return { + id: 'studio', + name: 'Studio', + sourceId: 'standalone', + installPath: '/builds/studio', + status: 'installed', + version: 'v0.42.1', + lastLaunchedAt: 100, + ...over + } +} + +function build(over: Partial = {}): LocalBuild { + return { + id: 'studio', + name: 'Studio', + version: '0.42.1', + port: null, + starting: false, + lastLaunchedAt: 0, + ...over + } +} + +beforeEach(() => { + mocks.records = [] + mocks.sessions.clear() + mocks.launching.clear() + mocks.operations.clear() + mocks.versionFiles.clear() +}) + +describe('the ComfyUI version a page asks for', () => { + it.each([ + [{ minVersion: '0.41.0' }, '0.41.0'], + [{ minVersion: 'v0.37.0' }, '0.37.0'], + [{ minVersion: '0.21' }, '0.21.0'], + [{ minVersion: '' }, null], + [{ minVersion: 41 }, null], + [{}, null], + [undefined, null], + ['0.41.0', null] + ])('reads %j as %j', (want, version) => { + expect(wantedVersion(want)).toBe(version) + }) +}) + +describe('the builds on this computer an app can run on', () => { + it('are the installed local builds, each with its version and whether it is running', async () => { + mocks.records = [record(), record({ id: 'lab', name: 'Lab', installPath: '/builds/lab' })] + mocks.sessions.set('lab', { port: 8190 }) + + expect(await usableLocalBuilds()).toEqual([ + build({ lastLaunchedAt: 100 }), + build({ id: 'lab', name: 'Lab', port: 8190, lastLaunchedAt: 100 }) + ]) + }) + + it('leave out Comfy Cloud, remote servers, the legacy app and unfinished installs', async () => { + mocks.records = [ + record({ id: 'cloud', sourceId: 'cloud' }), + record({ id: 'remote', sourceId: 'remote' }), + record({ id: 'legacy', sourceId: 'desktop' }), + record({ id: 'unknown-kind', sourceId: 'mystery' }), + record({ id: 'installing', status: 'installing' }), + record({ id: 'updating', status: 'updating' }), + record({ id: 'nowhere', installPath: '' }) + ] + + expect(await usableLocalBuilds()).toEqual([]) + }) + + it('leave out a build that is being updated or copied, but keep one that is starting', async () => { + mocks.records = [record({ id: 'updating-now' }), record({ id: 'starting-now' })] + mocks.operations.set('updating-now', {}) + mocks.operations.set('starting-now', {}) + mocks.launching.add('starting-now') + + expect((await usableLocalBuilds()).map(({ id, starting }) => ({ id, starting }))).toEqual([ + { id: 'starting-now', starting: true } + ]) + }) + + it('read the version from the ComfyUI on disk before the one recorded at install', async () => { + mocks.records = [record({ version: 'v0.30.0' })] + mocks.versionFiles.set( + '/builds/studio/ComfyUI/comfyui_version.py', + '# generated\n__version__ = "0.42.1"\n' + ) + + expect((await usableLocalBuilds())[0]?.version).toBe('0.42.1') + }) + + it('have no version when neither the disk nor the record gives one', async () => { + mocks.records = [record({ version: undefined })] + + expect((await usableLocalBuilds())[0]?.version).toBeNull() + }) +}) + +describe('a running build', () => { + it('is one with a port on this computer', () => { + mocks.sessions.set('studio', { port: 8188 }) + + expect(runningPortOf('studio')).toBe(8188) + expect(runningPortOf('lab')).toBeNull() + }) + + it('is not a remote session, one with no port yet, or another session of the same build', () => { + mocks.sessions.set('remote', { port: 443, url: 'https://comfy.example' }) + mocks.sessions.set('external', { port: 0 }) + mocks.sessions.set('performance-test:studio', { port: 8200, sourceInstallationId: 'studio' }) + + expect(runningPortOf('remote')).toBeNull() + expect(runningPortOf('external')).toBeNull() + expect(runningPortOf('performance-test:studio')).toBeNull() + }) +}) + +describe('choosing the build for a run', () => { + it('says there are no builds', () => { + expect(chooseLocalBuild([], '0.41.0')).toEqual({ kind: 'no-builds' }) + }) + + it('says every build is too old, or of unknown age, for the page', () => { + const builds = [build({ version: '0.37.0' }), build({ id: 'lab', version: null })] + + expect(chooseLocalBuild(builds, '0.41.0')).toEqual({ kind: 'too-old', wanted: '0.41.0' }) + }) + + it('takes any build when the page names no version', () => { + expect(chooseLocalBuild([build({ version: null })], null)).toMatchObject({ kind: 'build' }) + }) + + it('prefers the build that is already running', () => { + const builds = [ + build({ id: 'newest', version: '0.45.0', lastLaunchedAt: 900 }), + build({ id: 'running', version: '0.41.0', port: 8188, lastLaunchedAt: 1 }) + ] + + expect(chooseLocalBuild(builds, '0.41.0')).toMatchObject({ build: { id: 'running' } }) + }) + + it('then the one that is starting, then the one used last, then the newest', () => { + const starting = build({ id: 'starting', starting: true }) + const usedLast = build({ id: 'used-last', lastLaunchedAt: 500 }) + const newest = build({ id: 'newest', version: '0.45.0' }) + const older = build({ id: 'older', version: '0.41.0' }) + + expect(chooseLocalBuild([older, usedLast, starting], null)).toMatchObject({ + build: { id: 'starting' } + }) + expect(chooseLocalBuild([older, newest, usedLast], null)).toMatchObject({ + build: { id: 'used-last' } + }) + expect(chooseLocalBuild([older, newest], null)).toMatchObject({ build: { id: 'newest' } }) + }) + + it('never takes a running build that is too old over a stopped one that fits', () => { + const builds = [ + build({ id: 'old-running', version: '0.30.0', port: 8188 }), + build({ id: 'fits', version: '0.42.0' }) + ] + + expect(chooseLocalBuild(builds, '0.41.0')).toMatchObject({ build: { id: 'fits' } }) + }) +}) diff --git a/src/main/apps/localBuild.ts b/src/main/apps/localBuild.ts new file mode 100644 index 000000000..2641be3d5 --- /dev/null +++ b/src/main/apps/localBuild.ts @@ -0,0 +1,113 @@ +import { promises as fsp } from 'fs' +import path from 'path' +import semver from 'semver' + +import * as installations from '../installations' +import type { InstallationRecord } from '../installations' +import { _hasActiveLaunch, _operationAborts, _runningSessions, sourceMap } from '../lib/ipc/shared' +import { resolveComfyDir } from '../lib/models' +import { coreSemver, coreVerifiedSemver } from '../lib/version' + +const LEGACY_DESKTOP_SOURCE = 'desktop' +const VERSION_FILE = 'comfyui_version.py' +const VERSION_LINE = /^__version__\s*=\s*['"]([^'"]+)['"]/m + +export interface LocalBuild { + id: string + name: string + version: string | null + port: number | null + starting: boolean + lastLaunchedAt: number +} + +export type LocalChoice = + | { kind: 'build'; build: LocalBuild } + | { kind: 'no-builds' } + | { kind: 'too-old'; wanted: string } + +function releaseVersion(value: unknown): string | null { + if (typeof value !== 'string') return null + return semver.valid(semver.coerce(value.trim().replace(/^v/, ''), { includePrerelease: false })) +} + +export function wantedVersion(want: unknown): string | null { + if (typeof want !== 'object' || want === null) return null + return releaseVersion((want as { minVersion?: unknown }).minVersion) +} + +async function versionOnDisk(inst: InstallationRecord): Promise { + const file = path.join(resolveComfyDir(inst.installPath), VERSION_FILE) + const text = await fsp.readFile(file, 'utf8').catch(() => null) + return text === null ? null : releaseVersion(VERSION_LINE.exec(text)?.[1]) +} + +async function installedVersion(inst: InstallationRecord): Promise { + return (await versionOnDisk(inst)) ?? coreVerifiedSemver(inst) ?? coreSemver(inst) +} + +export function runningPortOf(installationId: string): number | null { + const session = _runningSessions.get(installationId) + if (!session || session.url || session.port <= 0) return null + return (session.sourceInstallationId ?? installationId) === installationId ? session.port : null +} + +function runsOnThisComputer(inst: InstallationRecord): boolean { + return ( + sourceMap[inst.sourceId]?.category === 'local' && + inst.sourceId !== LEGACY_DESKTOP_SOURCE && + inst.status === 'installed' && + typeof inst.installPath === 'string' && + inst.installPath !== '' + ) +} + +function isBeingChanged(installationId: string): boolean { + if (runningPortOf(installationId) !== null || _hasActiveLaunch(installationId)) return false + return _operationAborts.has(installationId) +} + +export async function usableLocalBuilds(): Promise { + const usable = (await installations.list()).filter( + (inst) => runsOnThisComputer(inst) && !isBeingChanged(inst.id) + ) + return Promise.all( + usable.map(async (inst) => ({ + id: inst.id, + name: inst.name, + version: await installedVersion(inst), + port: runningPortOf(inst.id), + starting: _hasActiveLaunch(inst.id), + lastLaunchedAt: typeof inst.lastLaunchedAt === 'number' ? inst.lastLaunchedAt : 0 + })) + ) +} + +function fits(build: LocalBuild, wanted: string | null): boolean { + if (wanted === null) return true + return build.version !== null && semver.gte(build.version, wanted) +} + +function newerFirst(one: LocalBuild, other: LocalBuild): number { + if (one.version === null || other.version === null) { + return Number(other.version !== null) - Number(one.version !== null) + } + return semver.rcompare(one.version, other.version) +} + +export function chooseLocalBuild( + builds: readonly LocalBuild[], + wanted: string | null +): LocalChoice { + if (builds.length === 0) return { kind: 'no-builds' } + const fitting = builds.filter((build) => fits(build, wanted)) + if (fitting.length === 0) return { kind: 'too-old', wanted: wanted ?? '' } + const [build] = [...fitting].sort( + (one, other) => + Number(other.port !== null) - Number(one.port !== null) || + Number(other.starting) - Number(one.starting) || + other.lastLaunchedAt - one.lastLaunchedAt || + newerFirst(one, other) + ) + return build ? { kind: 'build', build } : { kind: 'no-builds' } +} diff --git a/src/main/apps/localCalls.test.ts b/src/main/apps/localCalls.test.ts new file mode 100644 index 000000000..e4b71c0a6 --- /dev/null +++ b/src/main/apps/localCalls.test.ts @@ -0,0 +1,282 @@ +import { describe, expect, it } from 'vitest' + +import { + MAX_JSON_BODY_CHARS, + MAX_UPLOAD_BYTES, + outputFileKey, + outputFilesOf, + queueViewOf, + readLocalCall +} from './localCalls' + +const RUN = '0b1c2d3e-aaaa-bbbb-cccc-0123456789ab' +const picture = new Uint8Array([1, 2, 3]) + +describe('what an app page may ask the ComfyUI on this computer', () => { + it('reads the queue', () => { + expect(readLocalCall({ path: '/queue' })).toEqual({ kind: 'queue' }) + expect(readLocalCall({ method: 'GET', path: '/queue' })).toEqual({ kind: 'queue' }) + }) + + it('reads how one run went', () => { + expect(readLocalCall({ path: `/history/${RUN}` })).toEqual({ kind: 'history', promptId: RUN }) + }) + + it('reads one result file, in a folder or not', () => { + const query = new URLSearchParams({ + filename: 'cutout_00001_.png', + subfolder: 'apps/remove-background', + type: 'output' + }) + + expect(readLocalCall({ path: `/view?${query}` })).toEqual({ + kind: 'view', + file: { filename: 'cutout_00001_.png', subfolder: 'apps/remove-background', type: 'output' } + }) + expect(readLocalCall({ path: '/view?filename=preview.webm&type=temp' })).toEqual({ + kind: 'view', + file: { filename: 'preview.webm', subfolder: '', type: 'temp' } + }) + }) + + it('reads what one node offers, such as its model files', () => { + expect(readLocalCall({ path: '/object_info/LoadBackgroundRemovalModel' })).toEqual({ + kind: 'object-info', + node: 'LoadBackgroundRemovalModel' + }) + }) + + it('reads a run to start, with the tag that names the app in the queue', () => { + const prompt = { load: { class_type: 'LoadImage', inputs: { image: 'a.png' } } } + const extraData = { comfy_app: { name: 'Remove Background', seconds: 12 } } + const body = JSON.stringify({ prompt, client_id: 'the-page', extra_data: extraData }) + + expect(readLocalCall({ method: 'POST', path: '/prompt', body })).toEqual({ + kind: 'prompt', + prompt, + extraData + }) + expect( + readLocalCall({ method: 'POST', path: '/prompt', body: JSON.stringify({ prompt }) }) + ).toEqual({ kind: 'prompt', prompt, extraData: {} }) + }) + + it('reads a picture or clip to send', () => { + expect( + readLocalCall({ + method: 'POST', + path: '/upload/image', + body: picture, + fileName: 'My_photo-1.PNG' + }) + ).toEqual({ kind: 'upload', bytes: picture, fileName: 'My_photo-1.PNG' }) + expect( + readLocalCall({ + method: 'POST', + path: '/upload/image', + body: picture.buffer, + fileName: 'clip.mp4' + }) + ).toEqual({ kind: 'upload', bytes: picture, fileName: 'clip.mp4' }) + }) + + it('reads a run to take out of the queue and a run to stop', () => { + expect( + readLocalCall({ method: 'POST', path: '/queue', body: JSON.stringify({ delete: [RUN] }) }) + ).toEqual({ kind: 'leave-queue', promptIds: [RUN] }) + expect( + readLocalCall({ + method: 'POST', + path: '/interrupt', + body: JSON.stringify({ prompt_id: RUN }) + }) + ).toEqual({ kind: 'interrupt', promptId: RUN }) + }) +}) + +describe('what an app page may not ask', () => { + it.each([ + '/system_stats', + '/free', + '/history', + '/object_info', + '/userdata/workflows%2Fmine.json', + '/api/userdata', + '/internal/logs', + '/manager/reboot', + '/extensions', + '/', + '', + 'queue', + '//127.0.0.1:9000/queue', + 'http://127.0.0.1:9000/queue', + '/history/../userdata', + '/history/a/b', + '/object_info/..', + '/object_info/%2e%2e', + '/view/../queue', + '/queue\\..\\free', + `/history/${RUN}#/free` + ])('refuses GET %s', (path) => { + expect(readLocalCall({ path })).toBeNull() + }) + + it.each(['/free', '/history', '/userdata/x', '/upload/mask', '/view', '/object_info/Node'])( + 'refuses POST %s', + (path) => { + expect( + readLocalCall({ + method: 'POST', + path, + body: JSON.stringify({ prompt: {}, delete: [RUN], prompt_id: RUN, clear: true }) + }) + ).toBeNull() + } + ) + + it.each(['PUT', 'DELETE', 'PATCH', 'get', 42, null])('refuses the method %j', (method) => { + expect(readLocalCall({ method, path: '/queue' })).toBeNull() + }) + + it.each([null, undefined, 'queue', 7, ['/queue'], { path: 7 }, {}])( + 'refuses the malformed request %j', + (raw) => { + expect(readLocalCall(raw)).toBeNull() + } + ) + + it.each([ + '/view', + '/view?filename=', + '/view?filename=a%2Fb.png', + '/view?filename=a.png&subfolder=..%2F..%2Fuser', + '/view?filename=a.png&subfolder=ok%5C..', + '/view?filename=a.png&type=input', + '/view?filename=a.png&type=..' + ])('refuses the file request %s', (path) => { + expect(readLocalCall({ path })).toBeNull() + }) + + it('refuses a run that is not a graph', () => { + const post = (body: unknown) => readLocalCall({ method: 'POST', path: '/prompt', body }) + + expect(post(JSON.stringify({ prompt: 'run this' }))).toBeNull() + expect(post(JSON.stringify({ prompt: [] }))).toBeNull() + expect(post(JSON.stringify([{ prompt: {} }]))).toBeNull() + expect(post('{ not json')).toBeNull() + expect(post({ prompt: {} })).toBeNull() + expect(post(undefined)).toBeNull() + expect(post(`{"prompt":{},"pad":"${'x'.repeat(MAX_JSON_BODY_CHARS)}"}`)).toBeNull() + }) + + it('refuses an upload that is empty, too big, or not a picture, clip or sound by name', () => { + const isRefused = (body: unknown, fileName: unknown): boolean => + readLocalCall({ method: 'POST', path: '/upload/image', body, fileName }) === null + + expect(isRefused(picture, 'a.png')).toBe(false) + expect(isRefused(new Uint8Array(MAX_UPLOAD_BYTES), 'a.png')).toBe(false) + expect(isRefused(new Uint8Array(MAX_UPLOAD_BYTES + 1), 'a.png')).toBe(true) + expect(isRefused(new Uint8Array(0), 'a.png')).toBe(true) + expect(isRefused('not bytes', 'a.png')).toBe(true) + expect(isRefused(picture, undefined)).toBe(true) + expect(isRefused(picture, 'run.py')).toBe(true) + expect(isRefused(picture, 'model.safetensors')).toBe(true) + expect(isRefused(picture, '../a.png')).toBe(true) + expect(isRefused(picture, 'folder/a.png')).toBe(true) + expect(isRefused(picture, '.hidden.png')).toBe(true) + expect(isRefused(picture, 'a b.png')).toBe(true) + }) + + it('refuses to clear the whole queue or to stop whatever is running', () => { + const post = (path: string, body: unknown) => + readLocalCall({ method: 'POST', path, body: JSON.stringify(body) }) + + expect(post('/queue', { clear: true })).toBeNull() + expect(post('/queue', { delete: [] })).toBeNull() + expect(post('/queue', { delete: [RUN, 7] })).toBeNull() + expect(post('/queue', { delete: [RUN, '../x'] })).toBeNull() + expect(post('/interrupt', {})).toBeNull() + expect(post('/interrupt', { prompt_id: '' })).toBeNull() + expect(readLocalCall({ method: 'POST', path: '/interrupt' })).toBeNull() + }) +}) + +describe('the files a finished run made', () => { + it('are every file its nodes list, with the folder and kind each is in', () => { + const entry = { + status: { completed: true }, + outputs: { + save: { + images: [{ filename: 'cutout_00001_.png', subfolder: 'apps', type: 'output' }], + animated: [false] + }, + preview: { images: [{ filename: 'p.png', type: 'temp' }, { filename: 7 }, 'junk'] }, + text: { text: ['hello'] }, + broken: null + } + } + + expect(outputFilesOf(entry)).toEqual([ + { filename: 'cutout_00001_.png', subfolder: 'apps', type: 'output' }, + { filename: 'p.png', subfolder: '', type: 'temp' } + ]) + }) + + it('are none for a run with no outputs yet', () => { + expect(outputFilesOf(undefined)).toEqual([]) + expect(outputFilesOf({ status: {} })).toEqual([]) + expect(outputFilesOf({ outputs: [] })).toEqual([]) + }) + + it('tell two files apart by kind, folder and name', () => { + const file = { filename: 'a.png', subfolder: 'x', type: 'output' } + + expect(outputFileKey(file)).toBe(outputFileKey({ ...file })) + expect(outputFileKey(file)).not.toBe(outputFileKey({ ...file, subfolder: '' })) + expect(outputFileKey(file)).not.toBe(outputFileKey({ ...file, type: 'temp' })) + expect(outputFileKey({ filename: 'b', subfolder: 'a/', type: 'output' })).not.toBe( + outputFileKey({ filename: '/b', subfolder: 'a', type: 'output' }) + ) + }) +}) + +describe('the queue as an app page sees it', () => { + const someoneElsesGraph = { 3: { class_type: 'KSampler', inputs: { seed: 1 } } } + const queue = { + queue_running: [[4, 'other-run', someoneElsesGraph, { client_id: 'their-window' }, ['9']]], + queue_pending: [ + [6, RUN, { 1: {} }, { comfy_app: { name: 'Remove Background', seconds: 12 } }, []], + [5, 'tagged-run', { 1: {} }, { comfy_app: { name: 'Upscale', seconds: 90 } }, []], + 'junk' + ] + } + + it('shows where its own run stands and what is ahead of it, in order', () => { + expect(queueViewOf(queue, (id) => id === RUN)).toEqual({ + running: [{ n: 4, id: null, app: null, seconds: null }], + pending: [ + { n: 5, id: null, app: 'Upscale', seconds: 90 }, + { n: 6, id: RUN, app: 'Remove Background', seconds: 12 } + ] + }) + }) + + it("never carries another run's id, graph or settings", () => { + const seen = JSON.stringify(queueViewOf(queue, (id) => id === RUN)) + + expect(seen).not.toContain('other-run') + expect(seen).not.toContain('tagged-run') + expect(seen).not.toContain('KSampler') + expect(seen).not.toContain('their-window') + }) + + it('drops an app name too long to be one, and reads an unreadable queue as empty', () => { + const long = { queue_running: [[1, 'x', {}, { comfy_app: { name: 'n'.repeat(41) } }]] } + + expect(queueViewOf(long, () => false).running).toEqual([ + { n: 1, id: null, app: null, seconds: null } + ]) + expect(queueViewOf(null, () => true)).toEqual({ running: [], pending: [] }) + expect(queueViewOf({ queue_running: 'no' }, () => true)).toEqual({ running: [], pending: [] }) + }) +}) diff --git a/src/main/apps/localCalls.ts b/src/main/apps/localCalls.ts new file mode 100644 index 000000000..161b36925 --- /dev/null +++ b/src/main/apps/localCalls.ts @@ -0,0 +1,191 @@ +export const MAX_UPLOAD_BYTES = 50_000_000 +export const MAX_JSON_BODY_CHARS = 4_000_000 +export const UPLOAD_SUBFOLDER = 'comfy-apps' + +const PATHNAME = /^(\/[A-Za-z0-9_.+-]+){1,2}$/ +const PROMPT_ID = /^[A-Za-z0-9-]{1,64}$/ +const NODE_CLASS = /^[A-Za-z0-9_][A-Za-z0-9_.+-]{0,127}$/ +const FILE_NAME = /^[^\\/\0]{1,255}$/ +const SUBFOLDER = /^[^\\\0]{0,255}$/ +const UPLOAD_NAME = + /^[A-Za-z0-9][A-Za-z0-9._-]{0,119}\.(png|jpe?g|webp|gif|mp4|mov|m4v|webm|mp3|wav|flac|ogg|m4a)$/i +const FILE_TYPES: ReadonlySet = new Set(['output', 'temp']) +const APP_NAME_MAX_CHARS = 40 + +export interface OutputFile { + filename: string + subfolder: string + type: string +} + +export type LocalCall = + | { kind: 'queue' } + | { kind: 'history'; promptId: string } + | { kind: 'view'; file: OutputFile } + | { kind: 'object-info'; node: string } + | { kind: 'prompt'; prompt: Record; extraData: Record } + | { kind: 'upload'; bytes: Uint8Array; fileName: string } + | { kind: 'leave-queue'; promptIds: string[] } + | { kind: 'interrupt'; promptId: string } + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +function jsonBody(body: unknown): Record | null { + if (typeof body !== 'string' || body.length > MAX_JSON_BODY_CHARS) return null + try { + const parsed: unknown = JSON.parse(body) + return isRecord(parsed) ? parsed : null + } catch { + return null + } +} + +function bytesOf(body: unknown): Uint8Array | null { + if (body instanceof Uint8Array) return body + if (body instanceof ArrayBuffer) return new Uint8Array(body) + return null +} + +function hasParentStep(subfolder: string): boolean { + return subfolder.split('/').includes('..') +} + +function viewCall(query: URLSearchParams): LocalCall | null { + const filename = query.get('filename') ?? '' + const subfolder = query.get('subfolder') ?? '' + const type = query.get('type') ?? 'output' + if (!FILE_NAME.test(filename) || !SUBFOLDER.test(subfolder) || hasParentStep(subfolder)) { + return null + } + return FILE_TYPES.has(type) ? { kind: 'view', file: { filename, subfolder, type } } : null +} + +function readGet(pathname: string, query: URLSearchParams): LocalCall | null { + if (pathname === '/queue') return { kind: 'queue' } + if (pathname === '/view') return viewCall(query) + const [, area, name = ''] = pathname.split('/') + if (area === 'history' && PROMPT_ID.test(name)) return { kind: 'history', promptId: name } + if (area === 'object_info' && NODE_CLASS.test(name)) return { kind: 'object-info', node: name } + return null +} + +function promptCall(body: unknown): LocalCall | null { + const sent = jsonBody(body) + if (!sent || !isRecord(sent['prompt'])) return null + const extraData = isRecord(sent['extra_data']) ? sent['extra_data'] : {} + return { kind: 'prompt', prompt: sent['prompt'], extraData } +} + +function uploadCall(body: unknown, fileName: unknown): LocalCall | null { + const bytes = bytesOf(body) + if (!bytes || bytes.byteLength === 0 || bytes.byteLength > MAX_UPLOAD_BYTES) return null + if (typeof fileName !== 'string' || !UPLOAD_NAME.test(fileName)) return null + return { kind: 'upload', bytes, fileName } +} + +function leaveQueueCall(body: unknown): LocalCall | null { + const ids = jsonBody(body)?.['delete'] + if (!Array.isArray(ids) || ids.length === 0) return null + const promptIds = ids.filter((id): id is string => typeof id === 'string' && PROMPT_ID.test(id)) + return promptIds.length === ids.length ? { kind: 'leave-queue', promptIds } : null +} + +function interruptCall(body: unknown): LocalCall | null { + const promptId = jsonBody(body)?.['prompt_id'] + return typeof promptId === 'string' && PROMPT_ID.test(promptId) + ? { kind: 'interrupt', promptId } + : null +} + +function readPost(pathname: string, body: unknown, fileName: unknown): LocalCall | null { + if (pathname === '/prompt') return promptCall(body) + if (pathname === '/upload/image') return uploadCall(body, fileName) + if (pathname === '/queue') return leaveQueueCall(body) + if (pathname === '/interrupt') return interruptCall(body) + return null +} + +export function readLocalCall(raw: unknown): LocalCall | null { + if (!isRecord(raw)) return null + const { method = 'GET', path, body, fileName } = raw + if (typeof path !== 'string') return null + const queryAt = path.indexOf('?') + const pathname = queryAt < 0 ? path : path.slice(0, queryAt) + if (!PATHNAME.test(pathname)) return null + const query = new URLSearchParams(queryAt < 0 ? '' : path.slice(queryAt + 1)) + if (method === 'GET') return readGet(pathname, query) + if (method === 'POST') return readPost(pathname, body, fileName) + return null +} + +export function outputFilesOf(historyEntry: unknown): OutputFile[] { + if (!isRecord(historyEntry) || !isRecord(historyEntry['outputs'])) return [] + const files: OutputFile[] = [] + for (const nodeOutput of Object.values(historyEntry['outputs'])) { + if (!isRecord(nodeOutput)) continue + for (const list of Object.values(nodeOutput)) { + if (!Array.isArray(list)) continue + for (const file of list) { + if (!isRecord(file) || typeof file['filename'] !== 'string') continue + files.push({ + filename: file['filename'], + subfolder: typeof file['subfolder'] === 'string' ? file['subfolder'] : '', + type: typeof file['type'] === 'string' ? file['type'] : 'output' + }) + } + } + } + return files +} + +export function outputFileKey(file: OutputFile): string { + return JSON.stringify([file.type, file.subfolder, file.filename]) +} + +export interface QueuedRun { + n: number + id: string | null + app: string | null + seconds: number | null +} + +export interface QueueView { + running: QueuedRun[] + pending: QueuedRun[] +} + +function appTagOf(extraData: unknown): Pick { + const tag = isRecord(extraData) ? extraData['comfy_app'] : null + if (!isRecord(tag)) return { app: null, seconds: null } + const name = tag['name'] + const seconds = tag['seconds'] + return { + app: typeof name === 'string' && name.length <= APP_NAME_MAX_CHARS ? name : null, + seconds: typeof seconds === 'number' && seconds > 0 ? seconds : null + } +} + +function queuedRuns(list: unknown, isOwn: (promptId: string) => boolean): QueuedRun[] { + if (!Array.isArray(list)) return [] + return list + .filter((entry): entry is unknown[] => Array.isArray(entry)) + .map((entry) => { + const promptId = typeof entry[1] === 'string' ? entry[1] : null + return { + n: typeof entry[0] === 'number' ? entry[0] : 0, + id: promptId !== null && isOwn(promptId) ? promptId : null, + ...appTagOf(entry[3]) + } + }) + .sort((one, other) => one.n - other.n) +} + +export function queueViewOf(queue: unknown, isOwn: (promptId: string) => boolean): QueueView { + const body = isRecord(queue) ? queue : {} + return { + running: queuedRuns(body['queue_running'], isOwn), + pending: queuedRuns(body['queue_pending'], isOwn) + } +} diff --git a/src/main/apps/localComfy.integration.test.ts b/src/main/apps/localComfy.integration.test.ts new file mode 100644 index 000000000..2fa6c83a1 --- /dev/null +++ b/src/main/apps/localComfy.integration.test.ts @@ -0,0 +1,173 @@ +import { spawn } from 'node:child_process' +import type { ChildProcess } from 'node:child_process' +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { createServer } from 'node:net' +import type { AddressInfo } from 'node:net' +import os from 'node:os' +import path from 'node:path' +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' + +import { + FAKE_COMFY_API_SERVER, + FAKE_OUTPUT_TEXT, + SOMEONE_ELSES_RUN +} from './__fixtures__/fakeComfyApi' +import type { FakeComfySeen } from './__fixtures__/fakeComfyApi' + +const mocks = vi.hoisted(() => ({ + sessions: new Map>(), + pages: [] as { send: (channel: string, event: unknown) => void }[] +})) + +vi.mock('../host/registry', () => ({ getEntryByInstallationId: () => undefined })) +vi.mock('../installations', () => ({ + list: async () => [ + { + id: 'studio', + name: 'Studio', + sourceId: 'standalone', + installPath: '/builds/studio', + status: 'installed', + version: 'v0.42.1' + } + ], + get: async () => null +})) +vi.mock('../lib/comfyDownloadManager', () => ({ startManagedModelJob: vi.fn() })) +vi.mock('../lib/comfyProcessRecord', () => ({ probeQueue: async () => null })) +vi.mock('../lib/i18n', () => ({ t: (key: string) => key })) +vi.mock('../lib/ipc/sessionActions/launch', () => ({ handleLaunch: vi.fn() })) +vi.mock('../lib/ipc/shared', async () => { + const { EventEmitter } = await import('node:events') + return { + _hasActiveLaunch: () => false, + _operationAborts: new Map(), + _runningSessions: mocks.sessions, + sessionLifecycleEvents: new EventEmitter(), + sourceMap: { standalone: { category: 'local' } }, + stopRunning: vi.fn() + } +}) +vi.mock('../lib/models', () => ({ resolveComfyDir: (installPath: string) => installPath })) +vi.mock('../lib/process', () => ({ COMFY_BOOT_TIMEOUT_MS: 300_000 })) +vi.mock('./appsPages', () => ({ liveAppsPages: () => mocks.pages })) + +import { _resetForTest, localFetch, localState } from './localComfy' + +const WANT = { minVersion: '0.41.0' } + +let comfy: ChildProcess +let scriptDir = '' +let port = 0 + +function freePort(): Promise { + return new Promise((resolve) => { + const probe = createServer() + probe.listen(0, '127.0.0.1', () => { + const { port: free } = probe.address() as AddressInfo + probe.close(() => resolve(free)) + }) + }) +} + +function textOf(answer: { body: Uint8Array }): Record { + return JSON.parse(new TextDecoder().decode(answer.body)) as Record +} + +async function whatComfySaw(): Promise { + return (await (await fetch(`http://127.0.0.1:${port}/e2e/seen`)).json()) as FakeComfySeen +} + +beforeAll(async () => { + port = await freePort() + scriptDir = mkdtempSync(path.join(os.tmpdir(), 'comfy-apps-local-')) + const script = path.join(scriptDir, 'fake-comfy.cjs') + writeFileSync(script, FAKE_COMFY_API_SERVER) + comfy = spawn(process.execPath, [script, '--port', String(port)], { + stdio: ['ignore', 'pipe', 'inherit'] + }) + await new Promise((resolve) => { + comfy.stdout?.on('data', (line: Buffer) => { + if (String(line).includes('To see the GUI')) resolve() + }) + }) + mocks.sessions.set('studio', { port }) +}) + +afterAll(() => { + _resetForTest() + comfy?.kill() + if (scriptDir) rmSync(scriptDir, { recursive: true, force: true }) +}) + +describe('a run on a ComfyUI that really answers and really talks over its socket', () => { + it("goes from a picture to its result, with ComfyUI's updates reaching the page on the way", async () => { + const updates: string[] = [] + mocks.pages = [{ send: (_channel, event) => updates.push((event as { type: string }).type) }] + + const state = await localState(WANT) + const uploaded = textOf( + await localFetch({ + method: 'POST', + path: '/upload/image', + body: new Uint8Array([1, 2, 3, 4]), + fileName: 'photo.png' + }) + ) + const started = textOf( + await localFetch({ + method: 'POST', + path: '/prompt', + body: JSON.stringify({ + prompt: { load: { class_type: 'LoadImage', inputs: { image: 'comfy-apps/photo.png' } } }, + client_id: 'the-page', + extra_data: { comfy_app: { name: 'Remove Background' } } + }) + }) + ) + const run = String(started['prompt_id']) + await vi.waitFor(() => expect(updates).toContain('execution_success'), { timeout: 10_000 }) + const history = textOf(await localFetch({ path: `/history/${run}` })) + const entry = history[run] as { outputs: { save: { images: Record[] } } } + const file = entry.outputs.save.images[0] ?? {} + const result = await localFetch({ path: `/view?${new URLSearchParams(file)}` }) + + expect(state).toMatchObject({ state: 'running', name: 'Studio', version: '0.42.1' }) + expect(uploaded).toEqual({ name: 'photo.png', subfolder: 'comfy-apps', type: 'input' }) + expect(updates).toEqual(['execution_start', 'executing', 'execution_success']) + expect(new TextDecoder().decode(result.body)).toBe(FAKE_OUTPUT_TEXT) + expect(result.contentType).toBe('image/png') + + const seen = await whatComfySaw() + expect(seen.sockets).toEqual([state.clientId]) + expect(seen.prompts).toEqual([ + { id: run, clientId: state.clientId, app: 'Remove Background', nodes: ['load'] } + ]) + expect(seen.uploads).toEqual([ + { name: 'photo.png', subfolder: 'comfy-apps', overwrite: false, bytes: expect.any(Number) } + ]) + }) + + it('shows the queue without the other run in it, and can stop only its own run', async () => { + const queue = textOf(await localFetch({ path: '/queue' })) + const own = (await whatComfySaw()).prompts[0]?.id ?? '' + const stopOf = (promptId: string) => + localFetch({ + method: 'POST', + path: '/interrupt', + body: JSON.stringify({ prompt_id: promptId }) + }) + + expect(queue).toEqual({ running: [{ n: 1, id: null, app: null, seconds: null }], pending: [] }) + expect((await stopOf(SOMEONE_ELSES_RUN)).status).toBe(403) + expect((await stopOf(own)).status).toBe(200) + expect((await whatComfySaw()).interrupted).toEqual([own]) + }) + + it('never lets a request outside the list reach ComfyUI', async () => { + const refused = await localFetch({ path: '/system_stats' }) + + expect(refused.status).toBe(403) + expect((await whatComfySaw()).asked).not.toContain('GET /system_stats') + }) +}) diff --git a/src/main/apps/localComfy.test.ts b/src/main/apps/localComfy.test.ts new file mode 100644 index 000000000..31f2696f5 --- /dev/null +++ b/src/main/apps/localComfy.test.ts @@ -0,0 +1,704 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const mocks = vi.hoisted(() => ({ + records: [] as Record[], + sessions: new Map>(), + launching: new Set(), + windows: new Map(), + pages: [] as { send: (channel: string, event: unknown) => void }[], + handleLaunch: (_ctx: unknown): Promise> => Promise.resolve({ ok: false }), + stopRunning: (_id: string): Promise => Promise.resolve(), + probeQueue: (_port: number): Promise => Promise.resolve({ running: 0, pending: 0 }), + startModelJob: (_options: Record): Promise => Promise.resolve(null), + relay: null as unknown, + hearFromComfy: (_message: unknown): void => {} +})) + +vi.mock('fs', () => ({ + promises: { readFile: async () => Promise.reject(new Error('ENOENT')) } +})) +vi.mock('../host/registry', () => ({ + getEntryByInstallationId: (id: string) => mocks.windows.get(id) +})) +vi.mock('../installations', () => ({ + list: async () => mocks.records, + get: async (id: string) => mocks.records.find((record) => record['id'] === id) ?? null +})) +vi.mock('../lib/comfyDownloadManager', () => ({ + startManagedModelJob: (options: Record) => mocks.startModelJob(options) +})) +vi.mock('../lib/comfyProcessRecord', () => ({ + probeQueue: (port: number) => mocks.probeQueue(port) +})) +vi.mock('../lib/i18n', () => ({ + t: (key: string, params?: Record) => + params ? `${key} ${Object.values(params).join(' ')}` : key +})) +vi.mock('../lib/ipc/sessionActions/launch', () => ({ + handleLaunch: (ctx: unknown) => mocks.handleLaunch(ctx) +})) +vi.mock('../lib/ipc/shared', async () => { + const { EventEmitter: Emitter } = await import('node:events') + return { + _hasActiveLaunch: (id: string) => mocks.launching.has(id), + _operationAborts: new Map(), + _runningSessions: mocks.sessions, + sessionLifecycleEvents: new Emitter(), + sourceMap: { standalone: { category: 'local' } }, + stopRunning: (id: string) => mocks.stopRunning(id) + } +}) +vi.mock('../lib/models', () => ({ + resolveComfyDir: (installPath: string) => `${installPath}/ComfyUI` +})) +vi.mock('../lib/process', () => ({ COMFY_BOOT_TIMEOUT_MS: 300_000 })) +vi.mock('./appsPages', () => ({ liveAppsPages: () => mocks.pages })) +vi.mock('./localRelay', () => ({ + createLocalRelay: (onMessage: (message: unknown) => void) => { + mocks.hearFromComfy = onMessage + return mocks.relay + } +})) + +import { sessionLifecycleEvents } from '../lib/ipc/shared' +import { + LOCAL_EVENT_CHANNEL, + LOCAL_IDLE_STOP_MS, + _resetForTest, + localDownload, + localFetch, + localState, + startLocal +} from './localComfy' + +const STUDIO = 'studio' +const PORT = 8190 +const RUN = '0b1c2d3e-aaaa-bbbb-cccc-0123456789ab' +const OTHER_RUN = 'ffffffff-aaaa-bbbb-cccc-0123456789ab' +const WANT = { minVersion: '0.41.0' } +const CUTOUT = { filename: 'cutout_00001_.png', subfolder: 'apps', type: 'output' } +const BIREFNET = { + url: 'https://huggingface.co/Comfy-Org/BiRefNet/resolve/main/background_removal/birefnet.safetensors', + folder: 'background_removal', + name: 'birefnet.safetensors' +} + +interface Asked { + method: string + path: string + body: unknown +} + +class FakeComfy { + asked: Asked[] = [] + queue = { + queue_running: [[4, OTHER_RUN, { 3: { class_type: 'KSampler' } }, { client_id: 'theirs' }]], + queue_pending: [[5, RUN, { 1: {} }, { comfy_app: { name: 'Remove Background' } }]] + } + promptAnswer: { status: number; body: unknown } = { + status: 200, + body: { prompt_id: RUN, number: 5, node_errors: {} } + } + + answer = async (input: string | URL | Request, init: RequestInit = {}): Promise => { + const url = new URL(String(input)) + const path = url.pathname + url.search + const body = + typeof init.body === 'string' ? (JSON.parse(init.body) as unknown) : (init.body ?? null) + this.asked.push({ method: init.method ?? 'GET', path, body }) + const json = (value: unknown, status = 200) => + new Response(JSON.stringify(value), { + status, + headers: { 'content-type': 'application/json' } + }) + if (url.port !== String(PORT)) throw new TypeError('fetch failed') + if (url.pathname === '/queue' && init.method !== 'POST') return json(this.queue) + if (url.pathname === '/prompt') return json(this.promptAnswer.body, this.promptAnswer.status) + if (url.pathname === `/history/${RUN}`) { + return json({ + [RUN]: { status: { completed: true }, outputs: { save: { images: [CUTOUT] } } } + }) + } + if (url.pathname === '/view') { + return new Response(new Uint8Array([137, 80, 78, 71]), { + headers: { 'content-type': 'image/png' } + }) + } + if (url.pathname === '/upload/image') { + return json({ name: 'photo.png', subfolder: 'comfy-apps', type: 'input' }) + } + return json({}) + } + + calls(path: string): Asked[] { + return this.asked.filter((asked) => asked.path === path) + } +} + +interface FakeRelay { + clientId: string + follow: ReturnType + whenOpen: ReturnType + stop: ReturnType +} + +let comfy: FakeComfy +let relay: FakeRelay +let page: { send: ReturnType void>> } +let launches: Record[] +let stopped: string[] + +function build(over: Record = {}): Record { + return { + id: STUDIO, + name: 'Studio', + sourceId: 'standalone', + installPath: '/builds/studio', + status: 'installed', + version: 'v0.42.1', + ...over + } +} + +function running(id = STUDIO, port = PORT): void { + mocks.sessions.set(id, { port }) +} + +function startsWhenLaunched(port = PORT): void { + mocks.handleLaunch = async (ctx) => { + const launch = ctx as { installationId: string } + launches.push(ctx as Record) + running(launch.installationId, port) + return { ok: true, mode: 'console', port } + } +} + +function textOf(answer: { body: Uint8Array }): unknown { + return JSON.parse(new TextDecoder().decode(answer.body)) +} + +function post(path: string, body: unknown): Promise<{ status: number; body: Uint8Array }> { + return localFetch({ method: 'POST', path, body: JSON.stringify(body) }) +} + +async function startARun(): Promise { + await post('/prompt', { prompt: { 1: { class_type: 'LoadImage', inputs: {} } } }) +} + +function sentToPage(): unknown[] { + return page.send.mock.calls + .filter(([channel]) => channel === LOCAL_EVENT_CHANNEL) + .map(([, event]) => event) +} + +beforeEach(() => { + vi.useFakeTimers() + _resetForTest() + comfy = new FakeComfy() + vi.stubGlobal('fetch', vi.fn(comfy.answer)) + relay = { + clientId: 'desktop-client', + follow: vi.fn(), + whenOpen: vi.fn(async () => true), + stop: vi.fn() + } + page = { send: vi.fn<(channel: string, event: unknown) => void>() } + launches = [] + stopped = [] + mocks.relay = relay + mocks.pages = [page] + mocks.records = [build()] + mocks.sessions.clear() + mocks.launching.clear() + mocks.windows.clear() + mocks.handleLaunch = async () => ({ ok: false }) + mocks.stopRunning = async (id) => { + stopped.push(id) + mocks.sessions.delete(id) + } + mocks.probeQueue = async () => ({ running: 0, pending: 0 }) + mocks.startModelJob = async () => null +}) + +afterEach(() => { + _resetForTest() + vi.unstubAllGlobals() + vi.useRealTimers() +}) + +describe('finding a ComfyUI on this computer for an app', () => { + it('says to add a build when there is none', async () => { + mocks.records = [] + + expect(await localState(WANT)).toEqual({ + state: 'unsupported', + error: 'apps.local.noBuilds' + }) + }) + + it('says which ComfyUI the app needs when every build is older', async () => { + mocks.records = [build({ version: 'v0.37.0' })] + + expect(await localState(WANT)).toEqual({ + state: 'unsupported', + error: 'apps.local.tooOld 0.41.0' + }) + }) + + it('offers a stopped build that fits, by its name and version', async () => { + expect(await localState(WANT)).toEqual({ + state: 'stopped', + own: true, + name: 'Studio', + version: '0.42.1' + }) + expect(relay.follow).not.toHaveBeenCalled() + }) + + it('says a build is running and follows it, without starting anything', async () => { + running() + + expect(await localState(WANT)).toEqual({ + state: 'running', + own: true, + name: 'Studio', + version: '0.42.1', + clientId: 'desktop-client' + }) + expect(relay.follow).toHaveBeenCalledWith(PORT) + expect(launches).toEqual([]) + }) +}) + +describe('starting it', () => { + it('starts the build without a window and answers once Desktop is listening to it', async () => { + startsWhenLaunched() + + expect(await startLocal(WANT)).toEqual({ + state: 'running', + own: true, + name: 'Studio', + version: '0.42.1', + clientId: 'desktop-client' + }) + expect(launches).toHaveLength(1) + expect(launches[0]).toMatchObject({ + installationId: STUDIO, + inst: { id: STUDIO }, + actionData: { launchModeOverride: 'console', autoPortOnConflict: true } + }) + expect(relay.follow).toHaveBeenCalledWith(PORT) + expect(relay.whenOpen).toHaveBeenCalled() + }) + + it('starts it once when two pages ask together', async () => { + startsWhenLaunched() + + const [one, other] = await Promise.all([startLocal(WANT), startLocal(WANT)]) + + expect(one.state).toBe('running') + expect(other.state).toBe('running') + expect(launches).toHaveLength(1) + }) + + it('leaves a running build as it is', async () => { + running() + startsWhenLaunched() + + expect((await startLocal(WANT)).state).toBe('running') + expect(launches).toEqual([]) + }) + + it('waits for a start the person already began instead of starting it again', async () => { + mocks.launching.add(STUDIO) + startsWhenLaunched() + + const starting = startLocal(WANT) + await vi.advanceTimersByTimeAsync(1_200) + running() + mocks.launching.delete(STUDIO) + await vi.advanceTimersByTimeAsync(600) + + expect((await starting).state).toBe('running') + expect(launches).toEqual([]) + }) + + it('says why when the build does not start', async () => { + mocks.handleLaunch = async () => ({ ok: false, message: 'Port 8188 is in use.' }) + + expect(await startLocal(WANT)).toEqual({ + state: 'stopped', + own: true, + name: 'Studio', + version: '0.42.1', + error: 'Port 8188 is in use.' + }) + }) + + it('names the build when the launch gives no reason, or throws', async () => { + mocks.handleLaunch = async () => ({ ok: false }) + expect((await startLocal(WANT)).error).toBe('apps.local.couldNotStart Studio') + + mocks.handleLaunch = async () => { + throw new Error('spawn failed') + } + expect((await startLocal(WANT)).error).toBe('spawn failed') + }) + + it('starts nothing when no build fits', async () => { + mocks.records = [] + startsWhenLaunched() + + expect((await startLocal(WANT)).state).toBe('unsupported') + expect(launches).toEqual([]) + }) +}) + +describe('a run from an app page', () => { + beforeEach(async () => { + running() + await localState(WANT) + }) + + it("is sent as Desktop's client, whatever client the page named", async () => { + const prompt = { 1: { class_type: 'LoadImage', inputs: { image: 'a.png' } } } + const extraData = { comfy_app: { name: 'Remove Background', seconds: 12 } } + + const answer = await post('/prompt', { + prompt, + client_id: 'the-page', + extra_data: extraData + }) + + expect(answer.status).toBe(200) + expect(textOf(answer)).toMatchObject({ prompt_id: RUN }) + expect(comfy.calls('/prompt')).toEqual([ + { + method: 'POST', + path: '/prompt', + body: { prompt, client_id: 'desktop-client', extra_data: extraData } + } + ]) + expect(relay.whenOpen).toHaveBeenCalled() + }) + + it('can then be read, and its result files fetched', async () => { + await startARun() + + const history = await localFetch({ path: `/history/${RUN}` }) + const file = await localFetch({ path: `/view?${new URLSearchParams(CUTOUT)}` }) + + expect(textOf(history)).toMatchObject({ [RUN]: { status: { completed: true } } }) + expect(file).toMatchObject({ status: 200, contentType: 'image/png' }) + expect([...file.body]).toEqual([137, 80, 78, 71]) + expect(comfy.calls(`/view?${new URLSearchParams(CUTOUT)}`)).toHaveLength(1) + }) + + it('can be taken out of the queue and stopped', async () => { + await startARun() + + expect((await post('/queue', { delete: [RUN] })).status).toBe(200) + expect((await post('/interrupt', { prompt_id: RUN })).status).toBe(200) + expect(comfy.asked.slice(-2)).toEqual([ + { method: 'POST', path: '/queue', body: { delete: [RUN] } }, + { method: 'POST', path: '/interrupt', body: { prompt_id: RUN } } + ]) + }) + + it("passes ComfyUI's own refusal of a graph back to the page", async () => { + comfy.promptAnswer = { status: 400, body: { error: { type: 'prompt_no_outputs' } } } + + const answer = await post('/prompt', { prompt: {} }) + + expect(answer.status).toBe(400) + expect(textOf(answer)).toEqual({ error: { type: 'prompt_no_outputs' } }) + }) +}) + +describe("what is not the page's own", () => { + beforeEach(async () => { + running() + await localState(WANT) + }) + + it('cannot be read, fetched, removed or stopped', async () => { + await startARun() + const before = comfy.asked.length + + const refused = [ + await localFetch({ path: `/history/${OTHER_RUN}` }), + await localFetch({ path: '/view?filename=someone-elses.png&type=output' }), + await post('/queue', { delete: [RUN, OTHER_RUN] }), + await post('/interrupt', { prompt_id: OTHER_RUN }) + ] + + expect(refused.map((answer) => answer.status)).toEqual([403, 403, 403, 403]) + expect(textOf(refused[0]!)).toEqual({ error: 'apps.local.notForwarded' }) + expect(comfy.asked).toHaveLength(before) + }) + + it('cannot be fetched before the run that made it was read', async () => { + await startARun() + + const early = await localFetch({ path: `/view?${new URLSearchParams(CUTOUT)}` }) + + expect(early.status).toBe(403) + }) + + it('shows in the queue only as something ahead, with no id or graph', async () => { + await startARun() + + const queue = await localFetch({ path: '/queue' }) + + expect(textOf(queue)).toEqual({ + running: [{ n: 4, id: null, app: null, seconds: null }], + pending: [{ n: 5, id: RUN, app: 'Remove Background', seconds: null }] + }) + expect(new TextDecoder().decode(queue.body)).not.toContain('KSampler') + }) + + it('is out of reach through any other request, which never gets to ComfyUI', async () => { + const refused = [ + await localFetch({ path: '/system_stats' }), + await localFetch({ path: '/history' }), + await localFetch({ path: '/userdata/workflows' }), + await post('/free', { unload_models: true }), + await post('/queue', { clear: true }), + await localFetch({ method: 'DELETE', path: '/queue' }), + await localFetch(null) + ] + + expect(refused.map((answer) => answer.status)).toEqual([403, 403, 403, 403, 403, 403, 403]) + expect(comfy.asked).toEqual([]) + }) +}) + +describe('sending a picture', () => { + it("puts it in the apps folder of ComfyUI's input, without replacing a file of that name", async () => { + running() + await localState(WANT) + + const answer = await localFetch({ + method: 'POST', + path: '/upload/image', + body: new Uint8Array([1, 2, 3]), + fileName: 'photo.png' + }) + + const [sent] = comfy.calls('/upload/image') + const form = sent?.body as FormData + const file = form.get('image') as File + expect(textOf(answer)).toEqual({ name: 'photo.png', subfolder: 'comfy-apps', type: 'input' }) + expect(file.name).toBe('photo.png') + expect([...new Uint8Array(await file.arrayBuffer())]).toEqual([1, 2, 3]) + expect(form.get('subfolder')).toBe('comfy-apps') + expect(form.get('type')).toBe('input') + expect(form.has('overwrite')).toBe(false) + }) +}) + +describe('when ComfyUI is not there', () => { + it('rejects, so the page can say Desktop could not reach it', async () => { + await localState(WANT) + + await expect(localFetch({ path: '/queue' })).rejects.toThrow('apps.local.notRunning') + expect(comfy.asked).toEqual([]) + }) + + it('rejects before any build was chosen', async () => { + await expect(localFetch({ path: '/queue' })).rejects.toThrow('apps.local.notRunning') + }) + + it('rejects when the build stopped answering', async () => { + running(STUDIO, 9999) + await localState(WANT) + + await expect(localFetch({ path: '/queue' })).rejects.toThrow() + }) + + it('stops following a build that has stopped', async () => { + running() + await localState(WANT) + + mocks.sessions.delete(STUDIO) + sessionLifecycleEvents.emit('changed') + + expect(relay.stop).toHaveBeenCalled() + }) +}) + +describe('what ComfyUI says about a run', () => { + beforeEach(async () => { + running() + await localState(WANT) + await startARun() + }) + + it("reaches the app page for the page's own run", () => { + mocks.hearFromComfy({ type: 'execution_start', data: { prompt_id: RUN, timestamp: 1 } }) + mocks.hearFromComfy({ + type: 'progress', + data: { prompt_id: RUN, node: 'cut', value: 2, max: 8 } + }) + + expect(sentToPage()).toEqual([ + { type: 'execution_start', data: { prompt_id: RUN, timestamp: 1 } }, + { type: 'progress', data: { prompt_id: RUN, node: 'cut', value: 2, max: 8 } } + ]) + }) + + it('does not reach it for another run, or for anything that is not a run update', () => { + mocks.hearFromComfy({ type: 'executing', data: { prompt_id: OTHER_RUN, node: '3' } }) + mocks.hearFromComfy({ type: 'status', data: { status: { exec_info: { queue_remaining: 2 } } } }) + mocks.hearFromComfy({ type: 'logs', data: { prompt_id: RUN, entries: ['secret path'] } }) + mocks.hearFromComfy({ type: 'executing' }) + mocks.hearFromComfy('junk') + mocks.hearFromComfy(null) + + expect(sentToPage()).toEqual([]) + }) +}) + +describe('downloading a model for an app', () => { + function modelJob(outcome: unknown) { + const release = vi.fn() + const started: Record[] = [] + mocks.startModelJob = async (options) => { + started.push(options) + return { id: 'job', url: BIREFNET.url, savePath: '/models/x', release, completion: outcome } + } + return { release, started } + } + + beforeEach(async () => { + running() + await localState(WANT) + }) + + it('saves it into the build the app runs on, and says when it is there', async () => { + const job = modelJob(Promise.resolve({ status: 'completed', savePath: '/models/x' })) + + expect(await localDownload(BIREFNET)).toEqual({ state: 'done' }) + expect(job.started).toEqual([ + expect.objectContaining({ + url: BIREFNET.url, + filename: 'birefnet.safetensors', + directory: 'background_removal', + installationId: STUDIO + }) + ]) + expect(job.release).toHaveBeenCalledTimes(1) + }) + + it('tells the app page how far along it is, without flooding it', async () => { + const job = modelJob(new Promise(() => {})) + void localDownload(BIREFNET) + await vi.advanceTimersByTimeAsync(0) + const progress = job.started[0]?.['onProgress'] as (done: number, total: number) => void + + progress(10, 400) + progress(20, 400) + await vi.advanceTimersByTimeAsync(300) + progress(200, 400) + progress(400, 400) + + expect(sentToPage()).toEqual([ + { type: 'download', data: { name: 'birefnet.safetensors', done: 10, total: 400 } }, + { type: 'download', data: { name: 'birefnet.safetensors', done: 200, total: 400 } }, + { type: 'download', data: { name: 'birefnet.safetensors', done: 400, total: 400 } } + ]) + }) + + it('says why a download failed, and when the person cancelled it', async () => { + const failed = modelJob(Promise.resolve({ status: 'error', error: 'Disk is full.' })) + expect(await localDownload(BIREFNET)).toEqual({ state: 'failed', error: 'Disk is full.' }) + expect(failed.release).toHaveBeenCalledTimes(1) + + modelJob(Promise.resolve({ status: 'cancelled' })) + expect(await localDownload(BIREFNET)).toEqual({ state: 'cancelled' }) + }) + + it('downloads nothing that is not a safetensors file on Hugging Face', async () => { + const job = modelJob(Promise.resolve({ status: 'completed', savePath: '/x' })) + + expect( + await localDownload({ ...BIREFNET, url: 'https://evil.example/birefnet.safetensors' }) + ).toEqual({ state: 'failed', error: 'apps.local.modelRefused' }) + expect(await localDownload({ ...BIREFNET, folder: '../custom_nodes' })).toMatchObject({ + state: 'failed' + }) + expect(job.started).toEqual([]) + }) + + it('downloads nothing while no ComfyUI is running for apps', async () => { + const job = modelJob(Promise.resolve({ status: 'completed', savePath: '/x' })) + mocks.sessions.clear() + + expect(await localDownload(BIREFNET)).toEqual({ + state: 'failed', + error: 'apps.local.notRunning' + }) + expect(job.started).toEqual([]) + }) +}) + +describe('a ComfyUI that Desktop started for apps', () => { + beforeEach(async () => { + startsWhenLaunched() + await startLocal(WANT) + }) + + it('is stopped once it has sat unused for ten minutes with nothing queued', async () => { + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS - 1) + expect(stopped).toEqual([]) + + await vi.advanceTimersByTimeAsync(1) + + expect(stopped).toEqual([STUDIO]) + }) + + it('keeps running while an app page keeps using it', async () => { + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS - 1_000) + await localFetch({ path: '/queue' }) + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS - 1_000) + + expect(stopped).toEqual([]) + }) + + it('keeps running while it has work queued, and is stopped after that', async () => { + mocks.probeQueue = async () => ({ running: 1, pending: 0 }) + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS) + expect(stopped).toEqual([]) + + mocks.probeQueue = async () => ({ running: 0, pending: 0 }) + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS) + + expect(stopped).toEqual([STUDIO]) + }) + + it('keeps running when its queue cannot be read', async () => { + mocks.probeQueue = async () => null + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS) + + expect(stopped).toEqual([]) + }) + + it('is left alone for good once the person opens its window', async () => { + mocks.windows.set(STUDIO, { window: {} }) + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS * 3) + + expect(stopped).toEqual([]) + }) +}) + +describe('a ComfyUI the person started themselves', () => { + it('is never stopped by apps, however long it sits unused', async () => { + running() + await startLocal(WANT) + await localFetch({ path: '/queue' }) + + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS * 3) + + expect(stopped).toEqual([]) + }) +}) diff --git a/src/main/apps/localComfy.ts b/src/main/apps/localComfy.ts new file mode 100644 index 000000000..4177963d2 --- /dev/null +++ b/src/main/apps/localComfy.ts @@ -0,0 +1,411 @@ +import type { IpcMainInvokeEvent, WebContents } from 'electron' + +import type { + ComfyDesktopLocalAnswer, + ComfyDesktopLocalComfy, + ComfyDesktopLocalDownload, + ComfyDesktopLocalEvent +} from '../../types/comfyDesktopBridge' +import { getEntryByInstallationId } from '../host/registry' +import * as installations from '../installations' +import { startManagedModelJob } from '../lib/comfyDownloadManager' +import { probeQueue } from '../lib/comfyProcessRecord' +import * as i18n from '../lib/i18n' +import { handleLaunch } from '../lib/ipc/sessionActions/launch' +import type { ActionResult } from '../lib/ipc/sessionActions/types' +import { _hasActiveLaunch, sessionLifecycleEvents, stopRunning } from '../lib/ipc/shared' +import { COMFY_BOOT_TIMEOUT_MS } from '../lib/process' +import { liveAppsPages } from './appsPages' +import { chooseLocalBuild, runningPortOf, usableLocalBuilds, wantedVersion } from './localBuild' +import type { LocalBuild, LocalChoice } from './localBuild' +import { + outputFileKey, + outputFilesOf, + queueViewOf, + readLocalCall, + UPLOAD_SUBFOLDER +} from './localCalls' +import type { LocalCall } from './localCalls' +import { readModelRequest } from './localModelRequest' +import { createLocalRelay } from './localRelay' +import type { LocalRelay } from './localRelay' + +export const LOCAL_EVENT_CHANNEL = 'apps-local:event' +export const LOCAL_IDLE_STOP_MS = 10 * 60_000 + +const CALL_TIMEOUT_MS = 60_000 +const FILE_TIMEOUT_MS = 5 * 60_000 +const MAX_ANSWER_BYTES = 512 * 1024 * 1024 +const MAX_RUNS_REMEMBERED = 200 +const MAX_FILES_REMEMBERED = 2_000 +const RELAY_READY_MS = 5_000 +const OTHER_LAUNCH_POLL_MS = 500 +const DOWNLOAD_EVENT_GAP_MS = 250 +const REFUSED = 403 +const JSON_TYPE = 'application/json' +const RUN_UPDATES: ReadonlySet = new Set([ + 'execution_start', + 'executing', + 'progress', + 'progress_state', + 'execution_success', + 'execution_error', + 'execution_interrupted' +]) + +interface Target { + id: string + name: string + version: string | null +} + +let target: Target | null = null +let relay: LocalRelay | null = null +let idleTimer: ReturnType | null = null +let watchingSessions = false +const startedHere = new Set() +const launches = new Map>() +const ownRuns = new Set() +const ownFiles = new Set() + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +function rememberBounded(set: Set, value: string, limit: number): void { + set.delete(value) + set.add(value) + for (const oldest of set) { + if (set.size <= limit) break + set.delete(oldest) + } +} + +function sendToPages(event: ComfyDesktopLocalEvent): void { + for (const page of liveAppsPages()) page.send(LOCAL_EVENT_CHANNEL, event) +} + +function relayRunUpdate(message: unknown): void { + if (!isRecord(message) || typeof message['type'] !== 'string') return + if (!RUN_UPDATES.has(message['type']) || !isRecord(message['data'])) return + const promptId = message['data']['prompt_id'] + if (typeof promptId !== 'string' || !ownRuns.has(promptId)) return + sendToPages({ type: message['type'], data: message['data'] }) +} + +function theRelay(): LocalRelay { + relay ??= createLocalRelay(relayRunUpdate) + return relay +} + +function stopFollowingStoppedBuilds(): void { + for (const id of startedHere) if (runningPortOf(id) === null) startedHere.delete(id) + if (target && runningPortOf(target.id) === null) relay?.stop() +} + +function watchSessions(): void { + if (watchingSessions) return + watchingSessions = true + sessionLifecycleEvents.on('changed', stopFollowingStoppedBuilds) +} + +async function stopIdleBuilds(): Promise { + idleTimer = null + for (const id of [...startedHere]) { + const port = runningPortOf(id) + if (port === null || getEntryByInstallationId(id)) { + startedHere.delete(id) + continue + } + const queue = await probeQueue(port) + const isIdle = queue !== null && queue !== 'not_queue' && queue.running + queue.pending === 0 + if (!isIdle) continue + startedHere.delete(id) + await stopRunning(id) + } + if (startedHere.size > 0) noteUse() +} + +function noteUse(): void { + if (idleTimer) clearTimeout(idleTimer) + idleTimer = + startedHere.size > 0 ? setTimeout(() => void stopIdleBuilds(), LOCAL_IDLE_STOP_MS) : null +} + +function whyNoBuild(choice: Exclude): string { + return choice.kind === 'too-old' + ? i18n.t('apps.local.tooOld', { version: choice.wanted }) + : i18n.t('apps.local.noBuilds') +} + +function describe(build: LocalBuild): ComfyDesktopLocalComfy { + return { + state: build.port === null ? 'stopped' : 'running', + own: true, + name: build.name, + ...(build.version ? { version: build.version } : {}), + ...(build.port === null ? {} : { clientId: theRelay().clientId }) + } +} + +async function chooseTarget(want: unknown): Promise { + const choice = chooseLocalBuild(await usableLocalBuilds(), wantedVersion(want)) + if (choice.kind !== 'build') return whyNoBuild(choice) + target = { id: choice.build.id, name: choice.build.name, version: choice.build.version } + return choice.build +} + +export async function localState(want: unknown): Promise { + const build = await chooseTarget(want) + if (typeof build === 'string') return { state: 'unsupported', error: build } + watchSessions() + if (build.port !== null) theRelay().follow(build.port) + return describe(build) +} + +function pause(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +async function waitForOtherLaunch(installationId: string): Promise { + const giveUpAt = Date.now() + COMFY_BOOT_TIMEOUT_MS + while (_hasActiveLaunch(installationId) && Date.now() < giveUpAt) { + await pause(OTHER_LAUNCH_POLL_MS) + } + return { ok: runningPortOf(installationId) !== null } +} + +const QUIET_SENDER = { isDestroyed: () => false, send: () => {} } as unknown as WebContents +const QUIET_EVENT = { sender: QUIET_SENDER } as unknown as IpcMainInvokeEvent + +async function launchWithoutWindow(installationId: string): Promise { + if (_hasActiveLaunch(installationId)) return waitForOtherLaunch(installationId) + const inst = await installations.get(installationId) + if (!inst) return { ok: false } + const result = await handleLaunch({ + event: QUIET_EVENT, + installationId, + inst, + actionData: { launchModeOverride: 'console', autoPortOnConflict: true } + }) + if (result.ok) startedHere.add(installationId) + return result +} + +function launchOnce(installationId: string): Promise { + const pending = launches.get(installationId) + if (pending) return pending + const launch = launchWithoutWindow(installationId) + .catch((err: unknown): ActionResult => ({ ok: false, message: (err as Error)?.message })) + .finally(() => launches.delete(installationId)) + launches.set(installationId, launch) + return launch +} + +export async function startLocal(want: unknown): Promise { + const build = await chooseTarget(want) + if (typeof build === 'string') return { state: 'unsupported', error: build } + watchSessions() + const launched = build.port === null ? await launchOnce(build.id) : null + const port = runningPortOf(build.id) + if (port === null) { + return { + ...describe({ ...build, port: null }), + error: launched?.message || i18n.t('apps.local.couldNotStart', { name: build.name }) + } + } + theRelay().follow(port) + await theRelay().whenOpen(RELAY_READY_MS) + noteUse() + return describe({ ...build, port }) +} + +function jsonAnswer(status: number, value: unknown): ComfyDesktopLocalAnswer { + return { status, contentType: JSON_TYPE, body: new TextEncoder().encode(JSON.stringify(value)) } +} + +function refusal(): ComfyDesktopLocalAnswer { + return jsonAnswer(REFUSED, { error: i18n.t('apps.local.notForwarded') }) +} + +async function readCapped(response: Response): Promise { + if (Number(response.headers.get('content-length') ?? 0) > MAX_ANSWER_BYTES) { + throw new Error('The answer is too large to hand to the page.') + } + if (!response.body) return new Uint8Array(0) + const chunks: Uint8Array[] = [] + let size = 0 + for await (const chunk of response.body as unknown as AsyncIterable) { + size += chunk.byteLength + if (size > MAX_ANSWER_BYTES) throw new Error('The answer is too large to hand to the page.') + chunks.push(chunk) + } + const body = new Uint8Array(size) + let offset = 0 + for (const chunk of chunks) { + body.set(chunk, offset) + offset += chunk.byteLength + } + return body +} + +interface Forwarded { + method?: 'POST' + json?: unknown + form?: FormData + timeoutMs?: number +} + +async function forward( + port: number, + path: string, + { method, json, form, timeoutMs = CALL_TIMEOUT_MS }: Forwarded = {} +): Promise { + const response = await fetch(`http://127.0.0.1:${port}${path}`, { + method: method ?? 'GET', + redirect: 'error', + signal: AbortSignal.timeout(timeoutMs), + ...(json === undefined + ? {} + : { headers: { 'Content-Type': JSON_TYPE }, body: JSON.stringify(json) }), + ...(form ? { body: form } : {}) + }) + return { + status: response.status, + contentType: response.headers.get('content-type') ?? 'application/octet-stream', + body: await readCapped(response) + } +} + +function parsed(answer: ComfyDesktopLocalAnswer): unknown { + if (answer.status !== 200) return null + try { + return JSON.parse(new TextDecoder().decode(answer.body)) + } catch { + return null + } +} + +function uploadForm(call: Extract): FormData { + const form = new FormData() + form.append('image', new Blob([call.bytes as BlobPart]), call.fileName) + form.append('type', 'input') + form.append('subfolder', UPLOAD_SUBFOLDER) + return form +} + +async function sendRun( + port: number, + call: Extract +): Promise { + await theRelay().whenOpen(RELAY_READY_MS) + const answer = await forward(port, '/prompt', { + method: 'POST', + json: { prompt: call.prompt, client_id: theRelay().clientId, extra_data: call.extraData } + }) + const started = parsed(answer) + if (isRecord(started) && typeof started['prompt_id'] === 'string') { + rememberBounded(ownRuns, started['prompt_id'], MAX_RUNS_REMEMBERED) + } + return answer +} + +async function readRun(port: number, promptId: string): Promise { + const answer = await forward(port, `/history/${promptId}`) + const history = parsed(answer) + for (const file of outputFilesOf(isRecord(history) ? history[promptId] : null)) { + rememberBounded(ownFiles, outputFileKey(file), MAX_FILES_REMEMBERED) + } + return answer +} + +async function answerCall(port: number, call: LocalCall): Promise { + switch (call.kind) { + case 'queue': + return jsonAnswer( + 200, + queueViewOf(parsed(await forward(port, '/queue')), (id) => ownRuns.has(id)) + ) + case 'object-info': + return forward(port, `/object_info/${call.node}`) + case 'upload': + return forward(port, '/upload/image', { method: 'POST', form: uploadForm(call) }) + case 'prompt': + return sendRun(port, call) + case 'history': + return ownRuns.has(call.promptId) ? readRun(port, call.promptId) : refusal() + case 'view': + return ownFiles.has(outputFileKey(call.file)) + ? forward(port, `/view?${new URLSearchParams({ ...call.file })}`, { + timeoutMs: FILE_TIMEOUT_MS + }) + : refusal() + case 'leave-queue': + return call.promptIds.every((id) => ownRuns.has(id)) + ? forward(port, '/queue', { method: 'POST', json: { delete: call.promptIds } }) + : refusal() + case 'interrupt': + return ownRuns.has(call.promptId) + ? forward(port, '/interrupt', { method: 'POST', json: { prompt_id: call.promptId } }) + : refusal() + } +} + +export async function localFetch(raw: unknown): Promise { + const call = readLocalCall(raw) + if (!call) return refusal() + const port = target ? runningPortOf(target.id) : null + if (port === null) throw new Error(i18n.t('apps.local.notRunning')) + theRelay().follow(port) + noteUse() + return answerCall(port, call) +} + +function downloadProgress(name: string): (done: number, total: number) => void { + let lastSentAt = 0 + return (done, total) => { + const now = Date.now() + if (now - lastSentAt < DOWNLOAD_EVENT_GAP_MS && done < total) return + lastSentAt = now + sendToPages({ type: 'download', data: { name, done, total } }) + } +} + +export async function localDownload(raw: unknown): Promise { + const model = readModelRequest(raw) + if (!model) return { state: 'failed', error: i18n.t('apps.local.modelRefused') } + if (!target || runningPortOf(target.id) === null) { + return { state: 'failed', error: i18n.t('apps.local.notRunning') } + } + noteUse() + const job = await startManagedModelJob({ + url: model.url, + filename: model.name, + directory: model.folder, + installationId: target.id, + onProgress: downloadProgress(model.name) + }) + try { + const outcome = await job.completion + if (outcome.status === 'completed') return { state: 'done' } + if (outcome.status === 'cancelled') return { state: 'cancelled' } + return { state: 'failed', error: outcome.error } + } finally { + job.release() + noteUse() + } +} + +export function _resetForTest(): void { + if (idleTimer) clearTimeout(idleTimer) + idleTimer = null + relay?.stop() + relay = null + target = null + if (watchingSessions) sessionLifecycleEvents.off('changed', stopFollowingStoppedBuilds) + watchingSessions = false + startedHere.clear() + launches.clear() + ownRuns.clear() + ownFiles.clear() +} diff --git a/src/main/apps/localModelRequest.test.ts b/src/main/apps/localModelRequest.test.ts new file mode 100644 index 000000000..27650e9f0 --- /dev/null +++ b/src/main/apps/localModelRequest.test.ts @@ -0,0 +1,74 @@ +import { describe, expect, it } from 'vitest' + +import { readModelRequest } from './localModelRequest' + +const BIREFNET = { + url: 'https://huggingface.co/Comfy-Org/BiRefNet/resolve/main/background_removal/birefnet.safetensors', + folder: 'background_removal', + name: 'birefnet.safetensors' +} + +describe('a model an app page may ask Desktop to download', () => { + it('is a safetensors file on Hugging Face, saved under its own name in a models folder', () => { + expect(readModelRequest(BIREFNET)).toEqual(BIREFNET) + expect( + readModelRequest({ + url: 'https://huggingface.co/Comfy-Org/Qwen-Image-2.1/resolve/main/text_encoders/qwen3vl_8b_int8_convrot.safetensors?download=true', + folder: 'text_encoders', + name: 'qwen3vl_8b_int8_convrot.safetensors' + }) + ).toMatchObject({ folder: 'text_encoders', name: 'qwen3vl_8b_int8_convrot.safetensors' }) + }) + + it.each([ + [ + 'another host', + { url: BIREFNET.url.replace('huggingface.co', 'huggingface.co.evil.example') } + ], + [ + 'a look-alike host', + { url: BIREFNET.url.replace('huggingface.co', 'evil.example/huggingface.co') } + ], + ['plain http', { url: BIREFNET.url.replace('https:', 'http:') }], + ['a file on this computer', { url: 'file:///models/birefnet.safetensors' }], + ['sign-in details in the address', { url: BIREFNET.url.replace('https://', 'https://me:pw@') }], + ['another port', { url: BIREFNET.url.replace('huggingface.co', 'huggingface.co:8443') }], + ['a page that is not a file', { url: 'https://huggingface.co/Comfy-Org/birefnet.safetensors' }], + ['an address that is not one', { url: 'birefnet.safetensors' }] + ])('refuses %s', (_what, change) => { + expect(readModelRequest({ ...BIREFNET, ...change })).toBeNull() + }) + + it.each([ + 'birefnet.ckpt', + 'birefnet.pt', + 'birefnet.safetensors.py', + 'run.py', + '../birefnet.safetensors', + 'sub/birefnet.safetensors', + '.safetensors', + 'bire fnet.safetensors' + ])('refuses to save it as %s', (name) => { + expect( + readModelRequest({ ...BIREFNET, name, url: BIREFNET.url.replace(BIREFNET.name, name) }) + ).toBeNull() + }) + + it('refuses a name that is not the file the address points at', () => { + expect(readModelRequest({ ...BIREFNET, name: 'other.safetensors' })).toBeNull() + }) + + it.each(['', '..', '../custom_nodes', 'checkpoints/sub', '/abs', 'Check Points', 'custom-nodes'])( + 'refuses the folder %j', + (folder) => { + expect(readModelRequest({ ...BIREFNET, folder })).toBeNull() + } + ) + + it.each([null, undefined, 'birefnet', 7, {}, { url: 7, folder: 'vae', name: 'a.safetensors' }])( + 'refuses the malformed request %j', + (raw) => { + expect(readModelRequest(raw)).toBeNull() + } + ) +}) diff --git a/src/main/apps/localModelRequest.ts b/src/main/apps/localModelRequest.ts new file mode 100644 index 000000000..fcbef2369 --- /dev/null +++ b/src/main/apps/localModelRequest.ts @@ -0,0 +1,40 @@ +const MODEL_HOST = 'huggingface.co' +const MODEL_FOLDER = /^[a-z0-9][a-z0-9_]{0,39}$/ +const MODEL_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,150}\.safetensors$/ +const RESOLVE_PATH = /^\/[^/]+\/[^/]+\/resolve\/[^/]+\/.+$/ + +export interface ModelRequest { + url: string + folder: string + name: string +} + +function parseUrl(value: string): URL | null { + try { + return new URL(value) + } catch { + return null + } +} + +function isPlainHuggingFaceFile(url: URL): boolean { + return ( + url.protocol === 'https:' && + url.hostname === MODEL_HOST && + url.port === '' && + url.username === '' && + url.password === '' && + RESOLVE_PATH.test(url.pathname) + ) +} + +export function readModelRequest(raw: unknown): ModelRequest | null { + if (typeof raw !== 'object' || raw === null) return null + const { url, folder, name } = raw as Record + if (typeof url !== 'string' || typeof folder !== 'string' || typeof name !== 'string') return null + if (!MODEL_FOLDER.test(folder) || !MODEL_NAME.test(name)) return null + const source = parseUrl(url) + if (!source || !isPlainHuggingFaceFile(source)) return null + if (!source.pathname.endsWith(`/${name}`)) return null + return { url: source.href, folder, name } +} diff --git a/src/main/apps/localRelay.test.ts b/src/main/apps/localRelay.test.ts new file mode 100644 index 000000000..67e1ac581 --- /dev/null +++ b/src/main/apps/localRelay.test.ts @@ -0,0 +1,177 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { createLocalRelay } from './localRelay' +import type { RelaySocket } from './localRelay' + +class FakeSocket implements RelaySocket { + closed = false + private listeners = new Map void)[]>() + + constructor(readonly url: string) {} + + addEventListener(type: string, listener: (event: { data: unknown }) => void): void { + this.listeners.set(type, [...(this.listeners.get(type) ?? []), listener]) + } + + close(): void { + this.closed = true + } + + emit(type: 'open' | 'close' | 'error' | 'message', data?: unknown): void { + for (const listener of this.listeners.get(type) ?? []) listener({ data }) + } +} + +let sockets: FakeSocket[] +let heard: unknown[] + +function relayUnderTest() { + return createLocalRelay( + (message) => heard.push(message), + (url) => { + const socket = new FakeSocket(url) + sockets.push(socket) + return socket + } + ) +} + +function lastSocket(): FakeSocket { + const socket = sockets.at(-1) + if (!socket) throw new Error('No socket was opened') + return socket +} + +beforeEach(() => { + vi.useFakeTimers() + sockets = [] + heard = [] +}) + +afterEach(() => { + vi.useRealTimers() +}) + +describe("following a ComfyUI's socket", () => { + it('connects as one client and hands over each message it can read', () => { + const relay = relayUnderTest() + + relay.follow(8188) + lastSocket().emit('open') + lastSocket().emit('message', '{"type":"executing","data":{"node":"3"}}') + lastSocket().emit('message', 'not json') + lastSocket().emit('message', new ArrayBuffer(8)) + + expect(sockets.map((socket) => socket.url)).toEqual([ + `ws://127.0.0.1:8188/ws?clientId=${relay.clientId}` + ]) + expect(heard).toEqual([{ type: 'executing', data: { node: '3' } }]) + }) + + it('keeps one connection while it is asked to follow the same ComfyUI again', () => { + const relay = relayUnderTest() + + relay.follow(8188) + relay.follow(8188) + lastSocket().emit('open') + relay.follow(8188) + + expect(sockets).toHaveLength(1) + }) + + it('connects again, waiting longer each time, while the ComfyUI is away', () => { + const relay = relayUnderTest() + relay.follow(8188) + + lastSocket().emit('error') + lastSocket().emit('close') + vi.advanceTimersByTime(999) + expect(sockets).toHaveLength(1) + vi.advanceTimersByTime(1) + expect(sockets).toHaveLength(2) + + lastSocket().emit('close') + vi.advanceTimersByTime(1_999) + expect(sockets).toHaveLength(2) + vi.advanceTimersByTime(1) + expect(sockets).toHaveLength(3) + + lastSocket().emit('open') + lastSocket().emit('close') + vi.advanceTimersByTime(1_000) + expect(sockets).toHaveLength(4) + }) + + it('moves to another ComfyUI and ignores what the old connection still says', () => { + const relay = relayUnderTest() + relay.follow(8188) + const old = lastSocket() + old.emit('open') + + relay.follow(8190) + old.emit('message', '{"type":"executing"}') + old.emit('close') + vi.advanceTimersByTime(60_000) + + expect(old.closed).toBe(true) + expect(sockets.map((socket) => socket.url)).toEqual([ + `ws://127.0.0.1:8188/ws?clientId=${relay.clientId}`, + `ws://127.0.0.1:8190/ws?clientId=${relay.clientId}` + ]) + expect(heard).toEqual([]) + }) + + it('stops for good when told to', () => { + const relay = relayUnderTest() + relay.follow(8188) + const socket = lastSocket() + + relay.stop() + socket.emit('close') + vi.advanceTimersByTime(60_000) + + expect(socket.closed).toBe(true) + expect(sockets).toHaveLength(1) + }) +}) + +describe('waiting for the socket to be open', () => { + it('answers at once when it is', async () => { + const relay = relayUnderTest() + relay.follow(8188) + lastSocket().emit('open') + + await expect(relay.whenOpen(5_000)).resolves.toBe(true) + }) + + it('answers as soon as it opens', async () => { + const relay = relayUnderTest() + relay.follow(8188) + const waiting = relay.whenOpen(5_000) + + lastSocket().emit('open') + + await expect(waiting).resolves.toBe(true) + }) + + it('gives up after the wait, and when nothing is being followed', async () => { + const relay = relayUnderTest() + await expect(relay.whenOpen(5_000)).resolves.toBe(false) + + relay.follow(8188) + const waiting = relay.whenOpen(5_000) + vi.advanceTimersByTime(5_000) + + await expect(waiting).resolves.toBe(false) + }) + + it('gives up when the relay stops', async () => { + const relay = relayUnderTest() + relay.follow(8188) + const waiting = relay.whenOpen(5_000) + + relay.stop() + + await expect(waiting).resolves.toBe(false) + }) +}) diff --git a/src/main/apps/localRelay.ts b/src/main/apps/localRelay.ts new file mode 100644 index 000000000..8e131c7c0 --- /dev/null +++ b/src/main/apps/localRelay.ts @@ -0,0 +1,113 @@ +import { randomUUID } from 'crypto' + +const FIRST_RETRY_MS = 1_000 +const LONGEST_RETRY_MS = 30_000 + +export interface RelaySocket { + addEventListener(type: 'open' | 'close' | 'error', listener: () => void): void + addEventListener(type: 'message', listener: (event: { data: unknown }) => void): void + close(): void +} + +export interface LocalRelay { + readonly clientId: string + follow(port: number): void + whenOpen(timeoutMs: number): Promise + stop(): void +} + +function parseMessage(data: unknown): unknown { + if (typeof data !== 'string') return null + try { + return JSON.parse(data) + } catch { + return null + } +} + +export function createLocalRelay( + onMessage: (message: unknown) => void, + openSocket: (url: string) => RelaySocket = (url) => new WebSocket(url) +): LocalRelay { + const clientId = randomUUID() + let followedPort: number | null = null + let socket: RelaySocket | null = null + let isOpen = false + let retryMs = FIRST_RETRY_MS + let retryTimer: ReturnType | null = null + let waiters: ((open: boolean) => void)[] = [] + + function tellWaiters(open: boolean): void { + const waiting = waiters + waiters = [] + for (const tell of waiting) tell(open) + } + + function forget(): void { + if (retryTimer) clearTimeout(retryTimer) + retryTimer = null + const closing = socket + socket = null + isOpen = false + closing?.close() + } + + function connect(port: number): void { + const connecting = openSocket(`ws://127.0.0.1:${port}/ws?clientId=${clientId}`) + socket = connecting + connecting.addEventListener('open', () => { + if (socket !== connecting) return + isOpen = true + retryMs = FIRST_RETRY_MS + tellWaiters(true) + }) + connecting.addEventListener('message', (event) => { + if (socket !== connecting) return + const message = parseMessage(event.data) + if (message !== null) onMessage(message) + }) + const retryLater = (): void => { + if (socket !== connecting) return + socket = null + isOpen = false + retryTimer = setTimeout(() => { + retryTimer = null + if (followedPort === port) connect(port) + }, retryMs) + retryMs = Math.min(retryMs * 2, LONGEST_RETRY_MS) + } + connecting.addEventListener('close', retryLater) + connecting.addEventListener('error', retryLater) + } + + return { + clientId, + follow(port) { + if (followedPort === port) return + forget() + followedPort = port + retryMs = FIRST_RETRY_MS + connect(port) + }, + whenOpen(timeoutMs) { + if (isOpen) return Promise.resolve(true) + if (followedPort === null) return Promise.resolve(false) + return new Promise((resolve) => { + const tell = (open: boolean): void => { + clearTimeout(timer) + resolve(open) + } + const timer = setTimeout(() => { + waiters = waiters.filter((waiter) => waiter !== tell) + resolve(false) + }, timeoutMs) + waiters.push(tell) + }) + }, + stop() { + followedPort = null + forget() + tellWaiters(false) + } + } +} diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index 4bfd4c3e7..62f7dcd5b 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -39,6 +39,7 @@ import { registerAssetsHandlers } from '../assets' import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandlers' import { registerCatalogHandlers } from './registerCatalogHandlers' import { registerAppsHandlers } from './registerAppsHandlers' +import { registerAppsLocalHandlers } from './registerAppsLocalHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' import { finalizeComfyBuilderRecovery, @@ -285,5 +286,6 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerEmbeddedSessionHandlers() registerCatalogHandlers() registerAppsHandlers() + registerAppsLocalHandlers() return startupRecovery.then(() => undefined) } diff --git a/src/main/lib/ipc/registerAppsLocalHandlers.test.ts b/src/main/lib/ipc/registerAppsLocalHandlers.test.ts new file mode 100644 index 000000000..0f0fb0bc9 --- /dev/null +++ b/src/main/lib/ipc/registerAppsLocalHandlers.test.ts @@ -0,0 +1,86 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +type Handler = (event: unknown, ...args: unknown[]) => Promise + +const mocks = vi.hoisted(() => ({ + handlers: new Map(), + isAppsPage: true, + appsEnabled: true, + localState: vi.fn(), + startLocal: vi.fn(), + localFetch: vi.fn(), + localDownload: vi.fn() +})) + +vi.mock('electron', () => ({ + ipcMain: { + handle: (channel: string, handler: Handler) => mocks.handlers.set(channel, handler) + } +})) +vi.mock('../../apps/appsConfig', () => ({ areAppsEnabled: async () => mocks.appsEnabled })) +vi.mock('../../apps/appsPages', () => ({ isAppsPageSender: () => mocks.isAppsPage })) +vi.mock('../../apps/localComfy', () => ({ + localState: mocks.localState, + startLocal: mocks.startLocal, + localFetch: mocks.localFetch, + localDownload: mocks.localDownload +})) +vi.mock('../i18n', () => ({ t: (key: string) => key })) + +import { APPS_LOCAL_CHANNELS, registerAppsLocalHandlers } from './registerAppsLocalHandlers' + +const EVENT = { sender: {}, senderFrame: {} } +const WANT = { minVersion: '0.41.0' } +const RUNNING = { state: 'running', own: true, name: 'Studio', clientId: 'desktop-client' } + +function call(channel: string, ...args: unknown[]): Promise { + const handler = mocks.handlers.get(channel) + if (!handler) throw new Error(`No handler for ${channel}`) + return handler(EVENT, ...args) +} + +beforeEach(() => { + vi.clearAllMocks() + mocks.handlers.clear() + mocks.isAppsPage = true + mocks.appsEnabled = true + mocks.localState.mockResolvedValue({ state: 'stopped', own: true, name: 'Studio' }) + mocks.startLocal.mockResolvedValue(RUNNING) + mocks.localFetch.mockResolvedValue({ status: 200, contentType: 'application/json', body: null }) + mocks.localDownload.mockResolvedValue({ state: 'done' }) + registerAppsLocalHandlers() +}) + +describe('the ComfyUI on this computer, for the hosted Apps page', () => { + it('is found, started, called and given a model on behalf of the page', async () => { + const request = { path: '/queue' } + const model = { url: 'https://huggingface.co/a/b/resolve/main/vae/x.safetensors' } + + expect(await call(APPS_LOCAL_CHANNELS.get, WANT)).toMatchObject({ state: 'stopped' }) + expect(await call(APPS_LOCAL_CHANNELS.start, WANT)).toEqual(RUNNING) + expect(await call(APPS_LOCAL_CHANNELS.fetch, request)).toMatchObject({ status: 200 }) + expect(await call(APPS_LOCAL_CHANNELS.download, model)).toEqual({ state: 'done' }) + + expect(mocks.localState).toHaveBeenCalledWith(WANT) + expect(mocks.startLocal).toHaveBeenCalledWith(WANT) + expect(mocks.localFetch).toHaveBeenCalledWith(request) + expect(mocks.localDownload).toHaveBeenCalledWith(model) + }) + + it.each([ + ['a sender that is not the hosted Apps page', { isAppsPage: false, appsEnabled: true }], + ['the Apps page while Apps is switched off', { isAppsPage: true, appsEnabled: false }] + ])('does nothing for %s', async (_who, state) => { + Object.assign(mocks, state) + + expect(await call(APPS_LOCAL_CHANNELS.get, WANT)).toMatchObject({ state: 'unsupported' }) + expect(await call(APPS_LOCAL_CHANNELS.start, WANT)).toMatchObject({ state: 'unsupported' }) + await expect(call(APPS_LOCAL_CHANNELS.fetch, { path: '/queue' })).rejects.toThrow() + expect(await call(APPS_LOCAL_CHANNELS.download, {})).toMatchObject({ state: 'failed' }) + + expect(mocks.localState).not.toHaveBeenCalled() + expect(mocks.startLocal).not.toHaveBeenCalled() + expect(mocks.localFetch).not.toHaveBeenCalled() + expect(mocks.localDownload).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/lib/ipc/registerAppsLocalHandlers.ts b/src/main/lib/ipc/registerAppsLocalHandlers.ts new file mode 100644 index 000000000..a2fd47074 --- /dev/null +++ b/src/main/lib/ipc/registerAppsLocalHandlers.ts @@ -0,0 +1,49 @@ +import { ipcMain } from 'electron' +import type { IpcMainInvokeEvent } from 'electron' + +import { areAppsEnabled } from '../../apps/appsConfig' +import { isAppsPageSender } from '../../apps/appsPages' +import { localDownload, localFetch, localState, startLocal } from '../../apps/localComfy' +import type { + ComfyDesktopLocalComfy, + ComfyDesktopLocalDownload +} from '../../../types/comfyDesktopBridge' +import * as i18n from '../i18n' + +export const APPS_LOCAL_CHANNELS = { + get: 'apps-local:get', + start: 'apps-local:start', + fetch: 'apps-local:fetch', + download: 'apps-local:download' +} as const + +async function isFromAppsPage(event: IpcMainInvokeEvent): Promise { + return isAppsPageSender(event.sender, event.senderFrame) && (await areAppsEnabled()) +} + +function notOffered(): ComfyDesktopLocalComfy { + return { state: 'unsupported', error: i18n.t('apps.local.notRunning') } +} + +export function registerAppsLocalHandlers(): void { + ipcMain.handle(APPS_LOCAL_CHANNELS.get, async (event, want: unknown) => + (await isFromAppsPage(event)) ? localState(want) : notOffered() + ) + + ipcMain.handle(APPS_LOCAL_CHANNELS.start, async (event, want: unknown) => + (await isFromAppsPage(event)) ? startLocal(want) : notOffered() + ) + + ipcMain.handle(APPS_LOCAL_CHANNELS.fetch, async (event, request: unknown) => { + if (!(await isFromAppsPage(event))) throw new Error(i18n.t('apps.local.notRunning')) + return localFetch(request) + }) + + ipcMain.handle( + APPS_LOCAL_CHANNELS.download, + async (event, model: unknown): Promise => + (await isFromAppsPage(event)) + ? localDownload(model) + : { state: 'failed', error: i18n.t('apps.local.notRunning') } + ) +} diff --git a/src/preload/appsPreload.ts b/src/preload/appsPreload.ts index 7015af8d0..30ff5f225 100644 --- a/src/preload/appsPreload.ts +++ b/src/preload/appsPreload.ts @@ -2,7 +2,9 @@ import { contextBridge, ipcRenderer } from 'electron' import type { IpcRendererEvent } from 'electron' import type { ComfyDesktop2AuthState, - ComfyDesktopHostedAuthBridge + ComfyDesktopHostedAuthBridge, + ComfyDesktopHostedLocalBridge, + ComfyDesktopLocalEvent } from '../types/comfyDesktopBridge' const auth: ComfyDesktopHostedAuthBridge = { @@ -20,4 +22,18 @@ const auth: ComfyDesktopHostedAuthBridge = { } } +const local: ComfyDesktopHostedLocalBridge = { + getLocal: (want) => ipcRenderer.invoke('apps-local:get', want), + startLocal: (want) => ipcRenderer.invoke('apps-local:start', want), + localFetch: (request) => ipcRenderer.invoke('apps-local:fetch', request), + localDownload: (model) => ipcRenderer.invoke('apps-local:download', model), + onLocalEvent: (callback) => { + const handler = (_event: IpcRendererEvent, update: ComfyDesktopLocalEvent): void => + callback(update) + ipcRenderer.on('apps-local:event', handler) + return () => ipcRenderer.removeListener('apps-local:event', handler) + } +} + contextBridge.exposeInMainWorld('comfyDesktopAuth', auth) +contextBridge.exposeInMainWorld('comfyDesktopLocal', local) diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index b5d71c456..b2ef44388 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -180,6 +180,93 @@ export type ComfyDesktopHostedAuthBridge = Required< > > +/** What a hosted page needs from the ComfyUI on this computer. */ +export interface ComfyDesktopLocalRequirements { + /** The oldest ComfyUI the page can run on, such as `0.41.0`. */ + minVersion?: string +} + +/** + * The ComfyUI on this computer that Desktop uses for a hosted page. + * `running`: it answers now. `stopped`: one of the person's builds fits and + * `startLocal` starts it. `unsupported`: no build here can run the page, and + * `error` says why in words for the person. + */ +export interface ComfyDesktopLocalComfy { + state: 'running' | 'stopped' | 'unsupported' + /** True when it is one of the person's own builds. */ + own?: boolean + name?: string + version?: string + /** The `client_id` Desktop listens with; updates for runs sent through `localFetch` arrive on `onLocalEvent`. */ + clientId?: string + error?: string +} + +/** + * One call to that ComfyUI. Desktop forwards only what a run needs: + * `GET /queue`, `GET /history/`, `GET /view`, `GET /object_info/`, + * `POST /prompt`, `POST /upload/image`, `POST /queue` to take a run out, and + * `POST /interrupt` to stop one. History, files, removal and stopping work + * only for runs the page started through this bridge. + */ +export interface ComfyDesktopLocalRequest { + method?: 'GET' | 'POST' + path: string + /** JSON text, or a file's bytes for `/upload/image`. */ + body?: string | Uint8Array + /** The name to save an upload under. */ + fileName?: string +} + +/** ComfyUI's answer, or status 403 for a call Desktop does not forward. */ +export interface ComfyDesktopLocalAnswer { + status: number + contentType: string + body: Uint8Array +} + +/** A safetensors file on Hugging Face to save as `name` in the models folder `folder`. */ +export interface ComfyDesktopLocalModel { + url: string + folder: string + name: string +} + +export interface ComfyDesktopLocalDownload { + state: 'done' | 'failed' | 'cancelled' + error?: string +} + +/** + * A model download's progress (`type: 'download'`, with `name`, `done` and + * `total` bytes in `data`), or one ComfyUI socket message about a run the page + * started (`execution_start`, `executing`, `progress`, `progress_state`, + * `execution_success`, `execution_error`, `execution_interrupted`). + */ +export interface ComfyDesktopLocalEvent { + type: string + data?: Record +} + +/** + * A ComfyUI on this computer, run by Desktop, for a Comfy page hosted in + * Desktop's Apps view. Desktop exposes it to that page as + * `window.comfyDesktopLocal`. The page builds and follows its own runs; + * Desktop finds or starts the ComfyUI, forwards the page's calls to it and + * downloads a model the page asks for. + */ +export interface ComfyDesktopHostedLocalBridge { + getLocal(want?: ComfyDesktopLocalRequirements): Promise + /** Starts the ComfyUI if it is stopped and resolves once it answers, or with the state and `error` if it could not start. */ + startLocal(want?: ComfyDesktopLocalRequirements): Promise + /** Rejects when no ComfyUI is running to forward to. */ + localFetch(request: ComfyDesktopLocalRequest): Promise + /** Downloads into the running ComfyUI's models folder and resolves when the file is there. */ + localDownload(model: ComfyDesktopLocalModel): Promise + onLocalEvent(callback: (event: ComfyDesktopLocalEvent) => void): () => void +} + export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise From 0f3a3ef57bc6fe93963561135d36990ad19de929 Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Fri, 13 Nov 2026 16:45:15 -0800 Subject: [PATCH 2/3] test(apps): wait for the app page to exist before reading it in the local-run journey The first step read the page's text inside expect.poll right after the card was clicked. The hosted view is created a moment later, and the page helper throws when no page has that address yet, which ends a poll at once instead of trying again. That failed E2E (linux 1/3) on its first step. The step now waits for the page first, as the other Apps journey does. --- e2e/apps-local.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/e2e/apps-local.test.ts b/e2e/apps-local.test.ts index 17f7bc1af..5460573ee 100644 --- a/e2e/apps-local.test.ts +++ b/e2e/apps-local.test.ts @@ -5,7 +5,7 @@ import os from 'node:os' import path from 'node:path' import { expect, test } from '@playwright/test' import { launchApp, type AppContext } from './launchApp' -import { WebContentsPage } from './support/cdpPages' +import { waitForWebContents, WebContentsPage } from './support/cdpPages' import { getRunningSessionSnapshot } from './support/devHooks' import { FAKE_COMFY_API_SERVER, @@ -222,6 +222,7 @@ test('An app page is offered the build on this computer, stopped, by name and ve expect(await ctx.panel.click(SIDEBAR_APPS)).toBe(true) await ctx.panel.waitForVisible(CUTOUT_CARD) expect(await ctx.panel.click(CUTOUT_CARD)).toBe(true) + await waitForWebContents(ctx.app, CUTOUT_PAGE) await expect .poll(() => appPage().textOf('#local'), LONG_WAIT) From 58e1246540db2d349a85d8b84a5ff13c8173f66e Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Fri, 13 Nov 2026 16:48:16 -0800 Subject: [PATCH 3/3] fix(apps): keep each app page on its own build, and a build alive while it downloads a model Two things a re-read of the local bridge turned up: - The build chosen for a run was one value shared by every app page. Two app pages in two windows, given different builds, would have sent their calls to whichever build was chosen last. Each page now keeps the build it was given, and each build is followed by its own socket client. - A model download counted as use only when it began and ended. A download longer than ten minutes let the idle timer stop the build Desktop had started, so the run failed after the download. A download under way now keeps the build running. --- src/main/apps/localComfy.integration.test.ts | 8 +- src/main/apps/localComfy.test.ts | 104 +++++++++++++---- src/main/apps/localComfy.ts | 105 ++++++++++++------ .../lib/ipc/registerAppsLocalHandlers.test.ts | 8 +- src/main/lib/ipc/registerAppsLocalHandlers.ts | 8 +- 5 files changed, 164 insertions(+), 69 deletions(-) diff --git a/src/main/apps/localComfy.integration.test.ts b/src/main/apps/localComfy.integration.test.ts index 2fa6c83a1..0d1599982 100644 --- a/src/main/apps/localComfy.integration.test.ts +++ b/src/main/apps/localComfy.integration.test.ts @@ -5,6 +5,7 @@ import { createServer } from 'node:net' import type { AddressInfo } from 'node:net' import os from 'node:os' import path from 'node:path' +import type { WebContents } from 'electron' import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' import { @@ -52,9 +53,12 @@ vi.mock('../lib/models', () => ({ resolveComfyDir: (installPath: string) => inst vi.mock('../lib/process', () => ({ COMFY_BOOT_TIMEOUT_MS: 300_000 })) vi.mock('./appsPages', () => ({ liveAppsPages: () => mocks.pages })) -import { _resetForTest, localFetch, localState } from './localComfy' +import * as local from './localComfy' const WANT = { minVersion: '0.41.0' } +const PAGE = {} as WebContents +const localState = (want: unknown) => local.localState(PAGE, want) +const localFetch = (raw: unknown) => local.localFetch(PAGE, raw) let comfy: ChildProcess let scriptDir = '' @@ -95,7 +99,7 @@ beforeAll(async () => { }) afterAll(() => { - _resetForTest() + local._resetForTest() comfy?.kill() if (scriptDir) rmSync(scriptDir, { recursive: true, force: true }) }) diff --git a/src/main/apps/localComfy.test.ts b/src/main/apps/localComfy.test.ts index 31f2696f5..e38f12e2f 100644 --- a/src/main/apps/localComfy.test.ts +++ b/src/main/apps/localComfy.test.ts @@ -1,3 +1,4 @@ +import type { WebContents } from 'electron' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const mocks = vi.hoisted(() => ({ @@ -10,7 +11,7 @@ const mocks = vi.hoisted(() => ({ stopRunning: (_id: string): Promise => Promise.resolve(), probeQueue: (_port: number): Promise => Promise.resolve({ running: 0, pending: 0 }), startModelJob: (_options: Record): Promise => Promise.resolve(null), - relay: null as unknown, + makeRelay: (): unknown => null, hearFromComfy: (_message: unknown): void => {} })) @@ -56,23 +57,19 @@ vi.mock('./appsPages', () => ({ liveAppsPages: () => mocks.pages })) vi.mock('./localRelay', () => ({ createLocalRelay: (onMessage: (message: unknown) => void) => { mocks.hearFromComfy = onMessage - return mocks.relay + return mocks.makeRelay() } })) import { sessionLifecycleEvents } from '../lib/ipc/shared' -import { - LOCAL_EVENT_CHANNEL, - LOCAL_IDLE_STOP_MS, - _resetForTest, - localDownload, - localFetch, - localState, - startLocal -} from './localComfy' +import * as local from './localComfy' + +const { LOCAL_EVENT_CHANNEL, LOCAL_IDLE_STOP_MS, _resetForTest } = local const STUDIO = 'studio' const PORT = 8190 +const LAB = 'lab' +const LAB_PORT = 8191 const RUN = '0b1c2d3e-aaaa-bbbb-cccc-0123456789ab' const OTHER_RUN = 'ffffffff-aaaa-bbbb-cccc-0123456789ab' const WANT = { minVersion: '0.41.0' } @@ -84,6 +81,7 @@ const BIREFNET = { } interface Asked { + port: number method: string path: string body: unknown @@ -105,13 +103,13 @@ class FakeComfy { const path = url.pathname + url.search const body = typeof init.body === 'string' ? (JSON.parse(init.body) as unknown) : (init.body ?? null) - this.asked.push({ method: init.method ?? 'GET', path, body }) + this.asked.push({ port: Number(url.port), method: init.method ?? 'GET', path, body }) const json = (value: unknown, status = 200) => new Response(JSON.stringify(value), { status, headers: { 'content-type': 'application/json' } }) - if (url.port !== String(PORT)) throw new TypeError('fetch failed') + if (![PORT, LAB_PORT].includes(Number(url.port))) throw new TypeError('fetch failed') if (url.pathname === '/queue' && init.method !== 'POST') return json(this.queue) if (url.pathname === '/prompt') return json(this.promptAnswer.body, this.promptAnswer.status) if (url.pathname === `/history/${RUN}`) { @@ -142,9 +140,24 @@ interface FakeRelay { stop: ReturnType } +type FakePage = { send: ReturnType void>> } + let comfy: FakeComfy let relay: FakeRelay -let page: { send: ReturnType void>> } +let page: FakePage + +const asContents = (from: FakePage): WebContents => from as unknown as WebContents +const localState = (want: unknown, from: FakePage = page) => + local.localState(asContents(from), want) +const startLocal = (want: unknown, from: FakePage = page) => + local.startLocal(asContents(from), want) +const localFetch = (raw: unknown, from: FakePage = page) => local.localFetch(asContents(from), raw) +const localDownload = (raw: unknown, from: FakePage = page) => + local.localDownload(asContents(from), raw) + +function fakeRelay(clientId: string): FakeRelay { + return { clientId, follow: vi.fn(), whenOpen: vi.fn(async () => true), stop: vi.fn() } +} let launches: Record[] let stopped: string[] @@ -196,16 +209,11 @@ beforeEach(() => { _resetForTest() comfy = new FakeComfy() vi.stubGlobal('fetch', vi.fn(comfy.answer)) - relay = { - clientId: 'desktop-client', - follow: vi.fn(), - whenOpen: vi.fn(async () => true), - stop: vi.fn() - } + relay = fakeRelay('desktop-client') page = { send: vi.fn<(channel: string, event: unknown) => void>() } launches = [] stopped = [] - mocks.relay = relay + mocks.makeRelay = () => relay mocks.pages = [page] mocks.records = [build()] mocks.sessions.clear() @@ -374,6 +382,7 @@ describe('a run from an app page', () => { expect(textOf(answer)).toMatchObject({ prompt_id: RUN }) expect(comfy.calls('/prompt')).toEqual([ { + port: PORT, method: 'POST', path: '/prompt', body: { prompt, client_id: 'desktop-client', extra_data: extraData } @@ -400,8 +409,8 @@ describe('a run from an app page', () => { expect((await post('/queue', { delete: [RUN] })).status).toBe(200) expect((await post('/interrupt', { prompt_id: RUN })).status).toBe(200) expect(comfy.asked.slice(-2)).toEqual([ - { method: 'POST', path: '/queue', body: { delete: [RUN] } }, - { method: 'POST', path: '/interrupt', body: { prompt_id: RUN } } + { port: PORT, method: 'POST', path: '/queue', body: { delete: [RUN] } }, + { port: PORT, method: 'POST', path: '/interrupt', body: { prompt_id: RUN } } ]) }) @@ -473,6 +482,35 @@ describe("what is not the page's own", () => { }) }) +describe('two app pages open in two windows', () => { + it('each reach the ComfyUI their own app was given, as its own client', async () => { + const other: FakePage = { send: vi.fn<(channel: string, event: unknown) => void>() } + const relays = [fakeRelay('client-for-studio'), fakeRelay('client-for-lab')] + mocks.makeRelay = () => relays.shift() + mocks.records = [ + build({ version: 'v0.30.0', lastLaunchedAt: 900 }), + build({ id: LAB, name: 'Lab', installPath: '/builds/lab', lastLaunchedAt: 100 }) + ] + running(STUDIO, PORT) + running(LAB, LAB_PORT) + + const forOlderApp = await localState({ minVersion: '0.21.0' }) + const forNewerApp = await localState({ minVersion: '0.41.0' }, other) + await post('/prompt', { prompt: { 1: {} } }) + await localFetch( + { method: 'POST', path: '/prompt', body: JSON.stringify({ prompt: { 2: {} } }) }, + other + ) + + expect(forOlderApp).toMatchObject({ name: 'Studio', clientId: 'client-for-studio' }) + expect(forNewerApp).toMatchObject({ name: 'Lab', clientId: 'client-for-lab' }) + expect(comfy.calls('/prompt').map(({ port, body }) => [port, body])).toEqual([ + [PORT, { prompt: { 1: {} }, client_id: 'client-for-studio', extra_data: {} }], + [LAB_PORT, { prompt: { 2: {} }, client_id: 'client-for-lab', extra_data: {} }] + ]) + }) +}) + describe('sending a picture', () => { it("puts it in the apps folder of ComfyUI's input, without replacing a file of that name", async () => { running() @@ -510,7 +548,7 @@ describe('when ComfyUI is not there', () => { }) it('rejects when the build stopped answering', async () => { - running(STUDIO, 9999) + running(STUDIO, 9_999) await localState(WANT) await expect(localFetch({ path: '/queue' })).rejects.toThrow() @@ -683,6 +721,24 @@ describe('a ComfyUI that Desktop started for apps', () => { expect(stopped).toEqual([]) }) + it('keeps running while a model is still downloading for an app, however long that takes', async () => { + let finish: (outcome: unknown) => void = () => {} + mocks.startModelJob = async () => ({ + id: 'job', + release: vi.fn(), + completion: new Promise((resolve) => (finish = resolve)) + }) + const downloading = localDownload(BIREFNET) + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS * 3) + expect(stopped).toEqual([]) + + finish({ status: 'completed', savePath: '/models/x' }) + await downloading + await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS) + + expect(stopped).toEqual([STUDIO]) + }) + it('is left alone for good once the person opens its window', async () => { mocks.windows.set(STUDIO, { window: {} }) await vi.advanceTimersByTimeAsync(LOCAL_IDLE_STOP_MS * 3) diff --git a/src/main/apps/localComfy.ts b/src/main/apps/localComfy.ts index 4177963d2..d1439bd2b 100644 --- a/src/main/apps/localComfy.ts +++ b/src/main/apps/localComfy.ts @@ -27,6 +27,7 @@ import { } from './localCalls' import type { LocalCall } from './localCalls' import { readModelRequest } from './localModelRequest' +import type { ModelRequest } from './localModelRequest' import { createLocalRelay } from './localRelay' import type { LocalRelay } from './localRelay' @@ -56,13 +57,13 @@ const RUN_UPDATES: ReadonlySet = new Set([ interface Target { id: string name: string - version: string | null } -let target: Target | null = null -let relay: LocalRelay | null = null let idleTimer: ReturnType | null = null let watchingSessions = false +let downloadsUnderWay = 0 +const targetByPage = new WeakMap() +const relayByBuild = new Map() const startedHere = new Set() const launches = new Map>() const ownRuns = new Set() @@ -93,14 +94,20 @@ function relayRunUpdate(message: unknown): void { sendToPages({ type: message['type'], data: message['data'] }) } -function theRelay(): LocalRelay { - relay ??= createLocalRelay(relayRunUpdate) +function relayFor(installationId: string, port: number): LocalRelay { + const relay = relayByBuild.get(installationId) ?? createLocalRelay(relayRunUpdate) + relayByBuild.set(installationId, relay) + relay.follow(port) return relay } function stopFollowingStoppedBuilds(): void { for (const id of startedHere) if (runningPortOf(id) === null) startedHere.delete(id) - if (target && runningPortOf(target.id) === null) relay?.stop() + for (const [id, relay] of relayByBuild) { + if (runningPortOf(id) !== null) continue + relay.stop() + relayByBuild.delete(id) + } } function watchSessions(): void { @@ -111,6 +118,7 @@ function watchSessions(): void { async function stopIdleBuilds(): Promise { idleTimer = null + if (downloadsUnderWay > 0) return noteUse() for (const id of [...startedHere]) { const port = runningPortOf(id) if (port === null || getEntryByInstallationId(id)) { @@ -144,22 +152,24 @@ function describe(build: LocalBuild): ComfyDesktopLocalComfy { own: true, name: build.name, ...(build.version ? { version: build.version } : {}), - ...(build.port === null ? {} : { clientId: theRelay().clientId }) + ...(build.port === null ? {} : { clientId: relayFor(build.id, build.port).clientId }) } } -async function chooseTarget(want: unknown): Promise { +async function chooseTarget(page: WebContents, want: unknown): Promise { const choice = chooseLocalBuild(await usableLocalBuilds(), wantedVersion(want)) if (choice.kind !== 'build') return whyNoBuild(choice) - target = { id: choice.build.id, name: choice.build.name, version: choice.build.version } + targetByPage.set(page, { id: choice.build.id, name: choice.build.name }) return choice.build } -export async function localState(want: unknown): Promise { - const build = await chooseTarget(want) +export async function localState( + page: WebContents, + want: unknown +): Promise { + const build = await chooseTarget(page, want) if (typeof build === 'string') return { state: 'unsupported', error: build } watchSessions() - if (build.port !== null) theRelay().follow(build.port) return describe(build) } @@ -202,8 +212,11 @@ function launchOnce(installationId: string): Promise { return launch } -export async function startLocal(want: unknown): Promise { - const build = await chooseTarget(want) +export async function startLocal( + page: WebContents, + want: unknown +): Promise { + const build = await chooseTarget(page, want) if (typeof build === 'string') return { state: 'unsupported', error: build } watchSessions() const launched = build.port === null ? await launchOnce(build.id) : null @@ -214,8 +227,7 @@ export async function startLocal(want: unknown): Promise error: launched?.message || i18n.t('apps.local.couldNotStart', { name: build.name }) } } - theRelay().follow(port) - await theRelay().whenOpen(RELAY_READY_MS) + await relayFor(build.id, port).whenOpen(RELAY_READY_MS) noteUse() return describe({ ...build, port }) } @@ -296,12 +308,13 @@ function uploadForm(call: Extract): FormData { async function sendRun( port: number, + relay: LocalRelay, call: Extract ): Promise { - await theRelay().whenOpen(RELAY_READY_MS) + await relay.whenOpen(RELAY_READY_MS) const answer = await forward(port, '/prompt', { method: 'POST', - json: { prompt: call.prompt, client_id: theRelay().clientId, extra_data: call.extraData } + json: { prompt: call.prompt, client_id: relay.clientId, extra_data: call.extraData } }) const started = parsed(answer) if (isRecord(started) && typeof started['prompt_id'] === 'string') { @@ -319,7 +332,11 @@ async function readRun(port: number, promptId: string): Promise { +async function answerCall( + port: number, + relay: LocalRelay, + call: LocalCall +): Promise { switch (call.kind) { case 'queue': return jsonAnswer( @@ -331,7 +348,7 @@ async function answerCall(port: number, call: LocalCall): Promise { +export async function localFetch( + page: WebContents, + raw: unknown +): Promise { const call = readLocalCall(raw) if (!call) return refusal() + const target = targetByPage.get(page) const port = target ? runningPortOf(target.id) : null - if (port === null) throw new Error(i18n.t('apps.local.notRunning')) - theRelay().follow(port) + if (!target || port === null) throw new Error(i18n.t('apps.local.notRunning')) noteUse() - return answerCall(port, call) + return answerCall(port, relayFor(target.id, port), call) } function downloadProgress(name: string): (done: number, total: number) => void { @@ -371,18 +391,15 @@ function downloadProgress(name: string): (done: number, total: number) => void { } } -export async function localDownload(raw: unknown): Promise { - const model = readModelRequest(raw) - if (!model) return { state: 'failed', error: i18n.t('apps.local.modelRefused') } - if (!target || runningPortOf(target.id) === null) { - return { state: 'failed', error: i18n.t('apps.local.notRunning') } - } - noteUse() +async function downloadInto( + installationId: string, + model: ModelRequest +): Promise { const job = await startManagedModelJob({ url: model.url, filename: model.name, directory: model.folder, - installationId: target.id, + installationId, onProgress: downloadProgress(model.name) }) try { @@ -392,6 +409,24 @@ export async function localDownload(raw: unknown): Promise { + const model = readModelRequest(raw) + if (!model) return { state: 'failed', error: i18n.t('apps.local.modelRefused') } + const target = targetByPage.get(page) + if (!target || runningPortOf(target.id) === null) { + return { state: 'failed', error: i18n.t('apps.local.notRunning') } + } + downloadsUnderWay += 1 + try { + return await downloadInto(target.id, model) + } finally { + downloadsUnderWay -= 1 noteUse() } } @@ -399,9 +434,9 @@ export async function localDownload(raw: unknown): Promise { expect(await call(APPS_LOCAL_CHANNELS.fetch, request)).toMatchObject({ status: 200 }) expect(await call(APPS_LOCAL_CHANNELS.download, model)).toEqual({ state: 'done' }) - expect(mocks.localState).toHaveBeenCalledWith(WANT) - expect(mocks.startLocal).toHaveBeenCalledWith(WANT) - expect(mocks.localFetch).toHaveBeenCalledWith(request) - expect(mocks.localDownload).toHaveBeenCalledWith(model) + expect(mocks.localState).toHaveBeenCalledWith(EVENT.sender, WANT) + expect(mocks.startLocal).toHaveBeenCalledWith(EVENT.sender, WANT) + expect(mocks.localFetch).toHaveBeenCalledWith(EVENT.sender, request) + expect(mocks.localDownload).toHaveBeenCalledWith(EVENT.sender, model) }) it.each([ diff --git a/src/main/lib/ipc/registerAppsLocalHandlers.ts b/src/main/lib/ipc/registerAppsLocalHandlers.ts index a2fd47074..db3a2d198 100644 --- a/src/main/lib/ipc/registerAppsLocalHandlers.ts +++ b/src/main/lib/ipc/registerAppsLocalHandlers.ts @@ -27,23 +27,23 @@ function notOffered(): ComfyDesktopLocalComfy { export function registerAppsLocalHandlers(): void { ipcMain.handle(APPS_LOCAL_CHANNELS.get, async (event, want: unknown) => - (await isFromAppsPage(event)) ? localState(want) : notOffered() + (await isFromAppsPage(event)) ? localState(event.sender, want) : notOffered() ) ipcMain.handle(APPS_LOCAL_CHANNELS.start, async (event, want: unknown) => - (await isFromAppsPage(event)) ? startLocal(want) : notOffered() + (await isFromAppsPage(event)) ? startLocal(event.sender, want) : notOffered() ) ipcMain.handle(APPS_LOCAL_CHANNELS.fetch, async (event, request: unknown) => { if (!(await isFromAppsPage(event))) throw new Error(i18n.t('apps.local.notRunning')) - return localFetch(request) + return localFetch(event.sender, request) }) ipcMain.handle( APPS_LOCAL_CHANNELS.download, async (event, model: unknown): Promise => (await isFromAppsPage(event)) - ? localDownload(model) + ? localDownload(event.sender, model) : { state: 'failed', error: i18n.t('apps.local.notRunning') } ) }