Skip to content
Closed
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
33 changes: 29 additions & 4 deletions src/main/apps/appsBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {
MARKER_FILE,
sourceMap
} from '../lib/ipc/shared'
import { installModelsDir } from '../lib/models'
import { installModelsDir, resolveComfyDir } from '../lib/models'
import { dataDir, defaultDataRoot, defaultInstallDir } from '../lib/paths'
import { findAvailablePort } from '../lib/process'
import { coreSemver, coreVerifiedSemver } from '../lib/version'
import * as settings from '../settings'
import { buildPinnedVariant } from '../sources/standalone'
import { DEFAULT_LAUNCH_ARGS, variantMatchesHost } from '../sources/standalone/envPaths'
Expand All @@ -33,10 +34,12 @@ import {
appsLaunchArgs,
appsModelDirs,
appsOutputDir,
comfyuiFileVersion,
downloadBytes,
findAppsBuild,
localOff,
pickVariant,
reusableInstalls,
type SetupEvent,
setupEnd,
setupEvent
Expand Down Expand Up @@ -94,6 +97,21 @@ export async function findApps(): Promise<InstallationRecord | null> {
return inst
}

export async function reusableLocal(): Promise<InstallationRecord[]> {
return reusableInstalls(await installations.list(), categoryOf)
}

export function sharedInputDir(): string {
return settings.get('inputDir') || settings.defaults.inputDir
}

export async function installVersion(inst: InstallationRecord): Promise<string | null> {
const file = path.join(resolveComfyDir(inst.installPath), 'comfyui_version.py')
const text = await fsp.readFile(file, 'utf8').catch(() => null)
const onDisk = text === null ? null : comfyuiFileVersion(text)
return onDisk ?? coreVerifiedSemver(inst) ?? coreSemver(inst)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

interface Plan {
data: Record<string, unknown>
bytes: number
Expand Down Expand Up @@ -275,11 +293,10 @@ export function cancelAppsSetup(): { cancelled: boolean } {
return { cancelled: true }
}

export async function launchAppsBuild(
export async function launchConsole(
inst: InstallationRecord,
withWindow = false
): Promise<{ ok: boolean; message?: string }> {
const fresh = (await installations.update(inst.id, await storageFor(inst))) ?? inst
const taken = new Set<number>([
..._pendingPorts.keys(),
...[..._runningSessions.values()].map((s) => s.port)
Expand All @@ -291,12 +308,20 @@ export async function launchAppsBuild(
taken
).catch(() => null)
if (!port) return { ok: false, message: 'No free port for ComfyUI for apps.' }
return launchInstall(fresh, {
return launchInstall(inst, {
portOverride: port,
...(withWindow ? {} : { launchModeOverride: 'console' })
})
}

export async function launchAppsBuild(
inst: InstallationRecord,
withWindow = false
): Promise<{ ok: boolean; message?: string }> {
const fresh = (await installations.update(inst.id, await storageFor(inst))) ?? inst
return launchConsole(fresh, withWindow)
}

export function showComfyWindow(inst: InstallationRecord): boolean {
const session = _runningSessions.get(inst.id)
if (!session || !_onLaunch) return false
Expand Down
203 changes: 202 additions & 1 deletion src/main/apps/appsBuildRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { describe, expect, it } from 'vitest'
import {
APPS_BUILD_IDLE_MS,
APPS_BUILD_RELEASE,
APPS_BUILD_VERSION,
APPS_SETUP_STAGE,
appsLaunchArgs,
appsModelDirs,
appsOutputDir,
comfyuiFileVersion,
downloadBytes,
FREE_IDLE_MS,
FREE_JSON,
Expand All @@ -20,12 +22,19 @@ import {
MAC_GPU_ONLY_ARG,
MAC_GPU_ONLY_MIN_BYTES,
MAC_NO_GPU,
pickComfy,
pickOpenTarget,
pickVariant,
queueEmpty,
releaseVersion,
reusableInstalls,
reuseMinimum,
setupEnd,
setupEvent,
shouldStopIdle
shouldStopIdle,
statsVersion,
TARGETED_INTERRUPT_VERSION,
wantedVersion
} from './appsBuildRules'
import { localInstalls } from './guards'

Expand Down Expand Up @@ -366,3 +375,195 @@ describe('which build Open in ComfyUI picks', () => {
expect(pickOpenTarget([], new Map([['apps', 50]]))).toBeNull()
})
})

describe('the ComfyUI version an app needs', () => {
it('is the Apps build version when the page names none', () => {
expect(APPS_BUILD_VERSION).toBe('0.42.1')
expect(wantedVersion(undefined)).toBe(APPS_BUILD_VERSION)
expect(wantedVersion({})).toBe(APPS_BUILD_VERSION)
})

it('takes the minimum the page names', () => {
expect(wantedVersion({ minVersion: '0.37.0' })).toBe('0.37.0')
expect(wantedVersion({ minVersion: 'v0.36.0' })).toBe('0.36.0')
})

it('falls back to the Apps build version for anything that is not a version', () => {
for (const minVersion of ['', 'latest', '0.37', '1.2.3.4', 37, null, ['0.37.0']]) {
expect(wantedVersion({ minVersion })).toBe(APPS_BUILD_VERSION)
}
expect(wantedVersion('0.37.0')).toBe(APPS_BUILD_VERSION)
})

it('reads a release tag as its ComfyUI version', () => {
expect(releaseVersion('v0.42.1-env1')).toBe('0.42.1')
expect(releaseVersion('0.44.0')).toBe('0.44.0')
expect(releaseVersion('abc1234')).toBeNull()
})
})

describe('the lowest ComfyUI an app run may reuse', () => {
it('is the first release whose interrupt stops only the named run', () => {
expect(TARGETED_INTERRUPT_VERSION).toBe('0.3.57')
expect(reuseMinimum('0.3.31')).toBe('0.3.57')
expect(reuseMinimum('0.37.0')).toBe('0.37.0')
})

it('keeps an older install out of the pick even when the page allows it', () => {
const old = { id: 'old', version: '0.3.56', running: true, busy: false }
expect(pickComfy([old], reuseMinimum('0.3.31'))).toBeNull()
})
})

describe('reading the ComfyUI version a running process reports', () => {
it('reads system_stats', () => {
expect(statsVersion({ system: { comfyui_version: '0.36.0' } })).toBe('0.36.0')
})

it('keeps a prerelease, which is older than its release', () => {
expect(statsVersion({ system: { comfyui_version: '0.42.1-rc1' } })).toBe('0.42.1-rc1')
const rc = { id: 'rc', version: '0.42.1-rc1', running: true, busy: false }
expect(pickComfy([rc], '0.42.1')).toBeNull()
})

it('gives no version when system_stats names none', () => {
expect(statsVersion({ system: {} })).toBeNull()
expect(statsVersion(null)).toBeNull()
expect(statsVersion({ system: { comfyui_version: 'dev' } })).toBeNull()
})
})

describe('reading the ComfyUI version of an install', () => {
it('reads comfyui_version.py', () => {
expect(comfyuiFileVersion('# generated\n__version__ = "0.44.0"\n')).toBe('0.44.0')
expect(comfyuiFileVersion("__version__='0.3.40'")).toBe('0.3.40')
})

it('keeps a prerelease in comfyui_version.py', () => {
expect(comfyuiFileVersion('__version__ = "0.42.1-rc1"')).toBe('0.42.1-rc1')
})

it('gives no version when the file names none', () => {
expect(comfyuiFileVersion('')).toBeNull()
expect(comfyuiFileVersion('version = "0.44.0"')).toBeNull()
expect(comfyuiFileVersion('__version__ = "dev"')).toBeNull()
})
})

describe('which of the person own installs an app run may use', () => {
const kinds: Record<string, string> = {
standalone: 'local',
git: 'local',
portable: 'local',
desktop: 'local',
cloud: 'cloud',
remote: 'remote'
}
const category = (inst: { sourceId: string }) => kinds[inst.sourceId]
const base = { installPath: '/x', status: 'installed' }

it('keeps finished local installs', () => {
const list = [
{ id: 'a', sourceId: 'standalone', ...base },
{ id: 'b', sourceId: 'git', ...base },
{ id: 'c', sourceId: 'portable', ...base }
]
expect(reusableInstalls(list, category).map((i) => i.id)).toEqual(['a', 'b', 'c'])
})

it('leaves out Cloud, remote, legacy Desktop and the Apps build', () => {
const list = [
{ id: 'cloud', sourceId: 'cloud', ...base },
{ id: 'remote', sourceId: 'remote', ...base },
{ id: 'legacy', sourceId: 'desktop', ...base },
{ id: 'apps', sourceId: 'standalone', appsBuild: true, ...base }
]
expect(reusableInstalls(list, category)).toEqual([])
})

it('leaves out installs that are not finished or have no folder', () => {
const list = [
{ id: 'installing', sourceId: 'standalone', installPath: '/x', status: 'installing' },
{ id: 'failed', sourceId: 'standalone', installPath: '/x', status: 'failed' },
{ id: 'unknown', sourceId: 'standalone', installPath: '/x' },
{ id: 'nowhere', sourceId: 'standalone', installPath: '', status: 'installed' }
]
expect(reusableInstalls(list, category)).toEqual([])
})

it('leaves out installs that keep their own input folder instead of the shared one apps use', () => {
const list = [
{ id: 'shared', sourceId: 'standalone', useSharedInput: true, ...base },
{ id: 'default', sourceId: 'standalone', ...base },
{ id: 'private', sourceId: 'standalone', useSharedInput: false, ...base }
]
expect(reusableInstalls(list, category).map((i) => i.id)).toEqual(['shared', 'default'])
})
})

describe('which ComfyUI an app run uses', () => {
const at = (id: string, version: string | null, running = false, busy = false) => ({
id,
version,
running,
busy
})

it('uses one that is already running over a newer stopped one', () => {
const list = [at('stopped', '0.45.0'), at('running', '0.43.0', true)]
expect(pickComfy(list, '0.42.1')?.id).toBe('running')
})

it('picks the newest compatible one', () => {
const list = [at('old', '0.43.0'), at('new', '0.44.2'), at('mid', '0.44.0')]
expect(pickComfy(list, '0.42.1')?.id).toBe('new')
})

it('picks the newest of several running ones', () => {
const list = [at('a', '0.43.0', true), at('b', '0.44.0', true)]
expect(pickComfy(list, '0.42.1')?.id).toBe('b')
})

it('keeps list order between equal versions', () => {
const list = [at('first', '0.44.0'), at('second', '0.44.0')]
expect(pickComfy(list, '0.42.1')?.id).toBe('first')
})

it('accepts exactly the minimum version', () => {
expect(pickComfy([at('a', '0.37.0')], '0.37.0')?.id).toBe('a')
})

it('skips installs older than the app needs, even running ones', () => {
const list = [at('old', '0.36.9', true), at('ok', '0.37.0')]
expect(pickComfy(list, '0.37.0')?.id).toBe('ok')
expect(pickComfy([at('old', '0.41.0', true)], '0.42.1')).toBeNull()
})

it('treats an install whose version is unknown as not compatible', () => {
expect(pickComfy([at('unknown', null, true)], '0.30.0')).toBeNull()
})

it('skips installs Desktop is busy with', () => {
const list = [at('updating', '0.45.0', false, true), at('ok', '0.43.0')]
expect(pickComfy(list, '0.42.1')?.id).toBe('ok')
})

it('stays on the running one apps already use while it still fits', () => {
const list = [at('newer', '0.45.0', true), at('used', '0.43.0', true)]
expect(pickComfy(list, '0.42.1', 'used')?.id).toBe('used')
expect(pickComfy(list, '0.44.0', 'used')?.id).toBe('newer')
expect(
pickComfy([at('newer', '0.45.0', true), at('used', '0.43.0')], '0.42.1', 'used')?.id
).toBe('newer')
})

it('finds none so the Apps build is used', () => {
expect(pickComfy([], '0.42.1')).toBeNull()
})

it('lets a page that needs less use an older install', () => {
const list = [at('song', '0.36.0')]
expect(pickComfy(list, '0.36.0')?.id).toBe('song')
expect(pickComfy(list, APPS_BUILD_VERSION)).toBeNull()
})
})
Loading
Loading