Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ export type ComfyDesktopHostedAuthBridge = Required<
export interface ComfyDesktopLocalRequirements {
/** The oldest ComfyUI the page can run on, such as `0.41.0`. */
minVersion?: string
/**
* The build the person picked to run on, an id from `localBuilds`. Desktop
* uses it when it is installed and new enough, and otherwise chooses as if
* none was named: a running build first, then one that is starting, then
* the one used last. The answer names the build Desktop chose.
*/
build?: string
}
/**
* The ComfyUI on this computer that Desktop uses for a hosted page.
Expand Down Expand Up @@ -278,6 +285,10 @@ export type ComfyDesktopOpenInComfy =
export interface ComfyDesktopLocalBuild {
id: string
name: string
/** Its ComfyUI version, such as `0.42.1`, when Desktop knows it. Absent on Desktop builds that predate it. */
version?: string
/** True when it is running now. Absent on Desktop builds that predate it. */
running?: boolean
}
/**
* A ComfyUI on this computer, run by Desktop, for a Comfy page hosted in
Expand Down
2 changes: 1 addition & 1 deletion packages/comfyui-desktop-bridge-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comfyorg/comfyui-desktop-bridge-types",
"version": "0.13.0",
"version": "0.14.0",
"description": "TypeScript definitions for the Comfy Desktop hosted frontend bridge",
"type": "module",
"main": "./index.js",
Expand Down
34 changes: 34 additions & 0 deletions src/main/apps/localBuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
installingLocalBuilds,
isStillInstalling,
modelTargetOf,
pickedBuild,
runningPortOf,
usableLocalBuilds,
wantedVersion
Expand Down Expand Up @@ -292,4 +293,37 @@ describe('choosing the build for a run', () => {

expect(chooseLocalBuild(builds, '0.41.0')).toMatchObject({ build: { id: 'fits' } })
})

it('takes the build the person picked over a running one', () => {
const builds = [
build({ id: 'running', port: 8188 }),
build({ id: 'picked', version: '0.43.0' })
]

expect(chooseLocalBuild(builds, '0.41.0', 'picked')).toMatchObject({
build: { id: 'picked' }
})
})

it('chooses as usual when the picked build is too old or gone', () => {
const builds = [build({ id: 'running', port: 8188 }), build({ id: 'old', version: '0.30.0' })]

expect(chooseLocalBuild(builds, '0.41.0', 'old')).toMatchObject({ build: { id: 'running' } })
expect(chooseLocalBuild(builds, '0.41.0', 'deleted')).toMatchObject({
build: { id: 'running' }
})
})
})

describe('the build a page picked', () => {
it('is the build id it sent', () => {
expect(pickedBuild({ minVersion: '0.41.0', build: 'lab' })).toBe('lab')
})

it('is none when the page sent none, or something that is not an id', () => {
expect(pickedBuild(undefined)).toBeNull()
expect(pickedBuild({ minVersion: '0.41.0' })).toBeNull()
expect(pickedBuild({ build: '' })).toBeNull()
expect(pickedBuild({ build: 7 })).toBeNull()
})
})
11 changes: 10 additions & 1 deletion src/main/apps/localBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export function wantedVersion(want: unknown): string | null {
return releaseVersion((want as { minVersion?: unknown }).minVersion)
}

export function pickedBuild(want: unknown): string | null {
if (typeof want !== 'object' || want === null) return null
const build = (want as { build?: unknown }).build
return typeof build === 'string' && build !== '' ? build : null
}

async function versionOnDisk(inst: InstallationRecord): Promise<string | null> {
const file = path.join(resolveComfyDir(inst.installPath), VERSION_FILE)
const text = await fsp.readFile(file, 'utf8').catch(() => null)
Expand Down Expand Up @@ -145,11 +151,14 @@ export function chooseInstallingBuild(

export function chooseLocalBuild(
builds: readonly LocalBuild[],
wanted: string | null
wanted: string | null,
picked: string | null = 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 named = fitting.find((build) => build.id === picked)
if (named) return { kind: 'build', build: named }
const [build] = [...fitting].sort(
(one, other) =>
Number(other.port !== null) - Number(one.port !== null) ||
Expand Down
37 changes: 37 additions & 0 deletions src/main/apps/localComfy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,43 @@ describe('finding a ComfyUI on this computer for an app', () => {
})
})

describe('a build the person picked on the app page', () => {
const LAB = 'inst-lab'

beforeEach(() => {
mocks.records = [
build(),
build({ id: LAB, name: 'Lab', installPath: '/builds/lab', version: 'v0.43.0' })
]
})

it('is the one the app runs on, even when another build is running', async () => {
running()

expect(await localState({ ...WANT, build: LAB })).toMatchObject({
state: 'stopped',
name: 'Lab'
})
})

it('is started for the run, and the running build is left alone', async () => {
running(STUDIO, PORT)
startsWhenLaunched(PORT + 1)

const started = await startLocal({ ...WANT, build: LAB })

expect(started).toMatchObject({ state: 'running', name: 'Lab' })
expect(launches.map((launch) => launch['installationId'])).toEqual([LAB])
expect(stopped).toEqual([])
})

it('gives way to the usual choice when it is too old for the app', async () => {
mocks.records = [build(), build({ id: LAB, name: 'Lab', version: 'v0.30.0' })]

expect(await localState({ ...WANT, build: LAB })).toMatchObject({ name: 'Studio' })
})
})

describe('starting it', () => {
it('starts the build without a window and answers once Desktop is listening to it', async () => {
startsWhenLaunched()
Expand Down
6 changes: 4 additions & 2 deletions src/main/apps/localComfy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
installingLocalBuilds,
isStillInstalling,
modelTargetOf,
pickedBuild,
runningPortOf,
usableLocalBuilds,
wantedVersion
Expand Down Expand Up @@ -198,11 +199,12 @@ function rememberTarget(page: WebContents, build: LocalBuild, installing: boolea

async function chooseTarget(page: WebContents, want: unknown): Promise<ChosenBuild | string> {
const wanted = wantedVersion(want)
const choice = chooseLocalBuild(await usableLocalBuilds(), wanted)
const picked = pickedBuild(want)
const choice = chooseLocalBuild(await usableLocalBuilds(), wanted, picked)
if (choice.kind === 'build') return rememberTarget(page, choice.build, false)
const installing = chooseInstallingBuild(await installingLocalBuilds(), wanted)
if (installing) return rememberTarget(page, installing, true)
const recheck = chooseLocalBuild(await usableLocalBuilds(), wanted)
const recheck = chooseLocalBuild(await usableLocalBuilds(), wanted, picked)
return recheck.kind === 'build' ? rememberTarget(page, recheck.build, false) : whyNoBuild(recheck)
}

Expand Down
19 changes: 16 additions & 3 deletions src/main/apps/openInComfy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface FakeBuild {
id: string
name: string
port: number | null
version?: string | null
lastLaunchedAt?: number
}

Expand Down Expand Up @@ -508,10 +509,22 @@ describe('when the workflow does not open', () => {
})

describe('the builds a page can pick from', () => {
it('are this computer builds by id and name', async () => {
it('are this computer builds by id and name, with whether each is running', async () => {
expect(await localBuilds()).toEqual([
{ id: STUDIO.id, name: STUDIO.name },
{ id: LAB.id, name: LAB.name }
{ id: STUDIO.id, name: STUDIO.name, running: false },
{ id: LAB.id, name: LAB.name, running: false }
])
})

it('give the ComfyUI version of each build Desktop knows it for', async () => {
mocks.builds = [
{ ...STUDIO, version: '0.42.1', port: 8188 },
{ ...LAB, version: null }
]

expect(await localBuilds()).toEqual([
{ id: STUDIO.id, name: STUDIO.name, version: '0.42.1', running: true },
{ id: LAB.id, name: LAB.name, running: false }
])
})

Expand Down
7 changes: 6 additions & 1 deletion src/main/apps/openInComfy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ function refusalText(refusal: OpenRefusal): string {
export async function localBuilds(): Promise<ComfyDesktopLocalBuild[]> {
return (await usableLocalBuilds())
.sort((a, b) => b.lastLaunchedAt - a.lastLaunchedAt)
.map((build) => ({ id: build.id, name: build.name }))
.map((build) => ({
id: build.id,
name: build.name,
...(build.version === null ? {} : { version: build.version }),
running: build.port !== null
}))
}

function pickLocal(builds: LocalBuild[], wanted: string | null): LocalBuild | null {
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/src/components/ui/Tooltip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ describe('Tooltip shared adapter', () => {
document.addEventListener('keydown', hostListener)
try {
target().dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
await flushPromises()
await expect.poll(() => document.querySelector('.tooltip-bubble')).toBeNull()
expect(hostListener).not.toHaveBeenCalled()
expect(document.querySelector('.tooltip-bubble')).toBeNull()
} finally {
window.removeEventListener('keydown', hostListener)
document.removeEventListener('keydown', hostListener)
Expand Down
11 changes: 11 additions & 0 deletions src/types/comfyDesktopBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ export type ComfyDesktopHostedAuthBridge = Required<
export interface ComfyDesktopLocalRequirements {
/** The oldest ComfyUI the page can run on, such as `0.41.0`. */
minVersion?: string
/**
* The build the person picked to run on, an id from `localBuilds`. Desktop
* uses it when it is installed and new enough, and otherwise chooses as if
* none was named: a running build first, then one that is starting, then
* the one used last. The answer names the build Desktop chose.
*/
build?: string
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/**
Expand Down Expand Up @@ -271,6 +278,10 @@ export type ComfyDesktopOpenInComfy = { opened: true } | { opened: false; error:
export interface ComfyDesktopLocalBuild {
id: string
name: string
/** Its ComfyUI version, such as `0.42.1`, when Desktop knows it. Absent on Desktop builds that predate it. */
version?: string
/** True when it is running now. Absent on Desktop builds that predate it. */
running?: boolean
}

/**
Expand Down
Loading