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
2 changes: 2 additions & 0 deletions browser_tests/types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '@/types'

import type { LGraph } from '@/lib/litegraph/src/LGraph'
import type { LGraphBadge as LGraphBadgeClass } from '@/lib/litegraph/src/LGraphBadge'
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
Expand Down
4 changes: 4 additions & 0 deletions src/components/graph/GraphCanvas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ vi.mock<unknown>(import('@/scripts/changeTracker'), () => ({
ChangeTracker: { init: vi.fn() }
}))

vi.mock(import('@/services/extensionLoader'), () => ({
loadExtensions: vi.fn(async () => {})
}))

vi.mock<unknown>(import('@/services/useNewUserService'), () => ({
useNewUserService: () => ({
initializeIfNewUser: vi.fn(),
Expand Down
3 changes: 3 additions & 0 deletions src/components/graph/GraphCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ import { UnauthorizedError } from '@/scripts/api'
import { app as comfyApp } from '@/scripts/app'
import { ChangeTracker } from '@/scripts/changeTracker'
import { IS_CONTROL_WIDGET, updateControlWidgetLabel } from '@/scripts/widgets'
import { loadExtensions } from '@/services/extensionLoader'
import { useColorPaletteService } from '@/services/colorPaletteService'
import { useNewUserService } from '@/services/useNewUserService'
import {
Expand Down Expand Up @@ -563,6 +564,8 @@ onMounted(async () => {
)
}

await bootstrapTracer.settle('bootstrap/extensions-load', loadExtensions)

const canvas = canvasRef.value
if (!canvas) throw new TypeError('GraphCanvas mounted without a canvas')
await comfyApp.setup(canvas)
Expand Down
2 changes: 0 additions & 2 deletions src/components/graph/SelectionToolbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as nodeFilterUtil from '@/utils/nodeFilterUtil'
function createMockExtensionService(): ReturnType<typeof useExtensionService> {
return {
extensionCommands: { value: new Map() },
loadExtensions: vi.fn(),
registerExtension: vi.fn(),
invokeExtensions: vi.fn(() => []),
invokeExtensionsAsync: vi.fn()
Expand Down Expand Up @@ -409,7 +408,6 @@ describe('SelectionToolbox', () => {
['test-command', { id: 'test-command', title: 'Test Command' }]
])
},
loadExtensions: vi.fn(),
registerExtension: vi.fn(),
invokeExtensions: vi.fn(() => ['test-command']),
invokeExtensionsAsync: vi.fn()
Expand Down
3 changes: 0 additions & 3 deletions src/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1003,9 +1003,6 @@ export class ComfyApp {
await useWorkspaceStore().workflow.syncWorkflows()
//Doesn't need to block. Blueprints will load async
void useSubgraphStore().fetchSubgraphs()
await bootstrapTracer.settle('bootstrap/extensions-load', () =>
useExtensionService().loadExtensions()
)

this.addProcessKeyHandler()
this.addConfigureHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { assert, describe, expect, it, vi } from 'vitest'
import { reportError } from '@/platform/telemetry/reportError'
import { api } from '@/scripts/api'

import type { ExtensionLoadFailure } from './extensionService'
import type { ExtensionLoadFailure } from './extensionLoader'
import {
loadExtensions,
reportExtensionLoadFailures,
shouldLoadExtension,
useExtensionService
} from './extensionService'
shouldLoadExtension
} from './extensionLoader'

vi.mock(import('@/platform/telemetry/reportError'))

Expand Down Expand Up @@ -113,7 +113,7 @@ describe('extension loading', () => {
'/extensions/pack-b/main.js'
])

await useExtensionService().loadExtensions()
await loadExtensions()

expect(reportError).toHaveBeenCalledOnce()
const [cause, options] = vi.mocked(reportError).mock.calls[0]
Expand All @@ -128,7 +128,7 @@ describe('extension loading', () => {
it('stays silent when no custom extensions are listed', async () => {
vi.spyOn(api, 'getExtensions').mockResolvedValue([])

await useExtensionService().loadExtensions()
await loadExtensions()

expect(reportError).not.toHaveBeenCalled()
})
Expand All @@ -139,7 +139,7 @@ describe('extension loading', () => {
'/extensions/pack-a/main.js'
])

await useExtensionService().loadExtensions()
await loadExtensions()

const [cause] = vi.mocked(reportError).mock.calls[0]
assert(cause instanceof Error)
Expand All @@ -154,7 +154,7 @@ describe('extension loading', () => {
'/extensions/pack-a/main.js'
])

await useExtensionService().loadExtensions()
await loadExtensions()

expect(mark.mock.calls.map(([name]) => name)).toEqual([
'bootstrap/extensions-load-core:start',
Expand Down
116 changes: 116 additions & 0 deletions src/services/extensionLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { useSettingStore } from '@/platform/settings/settingStore'
import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer'
import { reportError } from '@/platform/telemetry/reportError'
import { api } from '@/scripts/api'
import { useExtensionStore } from '@/stores/extensionStore'
import { getErrorMessage } from '@/utils/errorUtil'

const INLINED_CLOUD_EXTENSIONS = new Set([
'/extensions/cloud/rum.js',
'/extensions/cloud/sentry.js'
])

export function shouldLoadExtension(
extension: string,
isCloudBuild: boolean
): boolean {
if (extension.includes('extensions/core')) return false
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return !isCloudBuild || !INLINED_CLOUD_EXTENSIONS.has(extension)
}

const MAX_NAMED_FAILED_EXTENSIONS = 10

export interface ExtensionLoadFailure {
ext: string
error: unknown
}

/**
* Import one backend-provided extension, returning the failure rather than
* throwing, so one broken pack cannot abort the rest of the parallel load.
*
* Reporting deliberately does not happen here. A systemic failure — a backend
* restart, or a proxy serving HTML for every `.js` — fails every import in the
* list, and one report per extension is then unbounded. Off cloud that is
* actively harmful: with no sink live, each report is held in `reportError`'s
* 25-entry pending buffer, so a handful of broken packs during bootstrap would
* silently crowd out every later error in the session. The batch is reported
* once, by `reportExtensionLoadFailures`.
*/
async function importCustomExtension(
ext: string
): Promise<ExtensionLoadFailure | undefined> {
try {
await import(/* @vite-ignore */ api.fileURL(ext))
} catch (error) {
return { ext, error }
}
}

/**
* The paths go in the message because `reportError` writes its console line
* from the error, not from `options.tags`. The count is tagged; backend paths
* are unbounded and belong in context instead of an indexed facet.
*/
export function reportExtensionLoadFailures(
failures: ExtensionLoadFailure[]
): void {
if (failures.length === 0) return

const named = failures.slice(0, MAX_NAMED_FAILED_EXTENSIONS)
const elided = failures.length - named.length
const paths = named.map(({ ext }) => ext).join(', ')
const noun = failures.length === 1 ? 'extension' : 'extensions'

reportError(
new Error(
`Error loading ${failures.length} ${noun}: ${paths}` +
(elided > 0 ? ` (+${elided} more)` : ''),
{ cause: failures[0].error }
),
{
errorType: 'error_loading_extension',
surface: 'platform',
level: 'warning',
tags: { failed_extension_count: failures.length },
context: {
failures: named.map(({ ext, error }) => ({
ext,
message: getErrorMessage(error)
}))
}
}
)
}

/**
* Loads the core extensions, then every extension served by the backend, in
* parallel. Core extensions load first because custom extensions may depend
* on them.
*/
export async function loadExtensions() {
const extensionStore = useExtensionStore()
extensionStore.loadDisabledExtensionNames(
useSettingStore().get('Comfy.Extension.Disabled')
)

const extensions = await api.getExtensions()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

await bootstrapTracer.settle(
'bootstrap/extensions-load-core',
() => import('@/extensions/core/index')
)
extensionStore.captureCoreExtensions()
const outcomes = await bootstrapTracer.settle(
'bootstrap/extensions-load-custom',
() =>
Promise.all(
extensions
.filter((extension) =>
shouldLoadExtension(extension, __DISTRIBUTION__ === 'cloud')
)
.map((ext) => importCustomExtension(ext))
)
)
reportExtensionLoadFailures(outcomes.filter((outcome) => !!outcome))
}
114 changes: 0 additions & 114 deletions src/services/extensionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { useCurrentUser } from '@/composables/auth/useCurrentUser'
import { useErrorHandling } from '@/composables/useErrorHandling'
import { legacyMenuCompat } from '@/lib/litegraph/src/contextMenuCompat'
import { useSettingStore } from '@/platform/settings/settingStore'
import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer'
import { reportError } from '@/platform/telemetry/reportError'
import { api } from '@/scripts/api'
import { useCommandStore } from '@/stores/commandStore'
import { useExtensionStore } from '@/stores/extensionStore'
import { KeybindingImpl } from '@/platform/keybindings/keybinding'
Expand All @@ -14,88 +11,9 @@ import { useWidgetStore } from '@/stores/widgetStore'
import { useBottomPanelStore } from '@/stores/workspace/bottomPanelStore'
import type { ComfyExtension } from '@/types/comfy'
import type { AuthUserInfo } from '@/types/authTypes'
import { getErrorMessage } from '@/utils/errorUtil'
import { app } from '@/scripts/app'
import type { ComfyApp } from '@/scripts/app'

const INLINED_CLOUD_EXTENSIONS = new Set([
'/extensions/cloud/rum.js',
'/extensions/cloud/sentry.js'
])

export function shouldLoadExtension(
extension: string,
isCloudBuild: boolean
): boolean {
if (extension.includes('extensions/core')) return false
return !isCloudBuild || !INLINED_CLOUD_EXTENSIONS.has(extension)
}

const MAX_NAMED_FAILED_EXTENSIONS = 10

export interface ExtensionLoadFailure {
ext: string
error: unknown
}

/**
* Import one backend-provided extension, returning the failure rather than
* throwing, so one broken pack cannot abort the rest of the parallel load.
*
* Reporting deliberately does not happen here. A systemic failure — a backend
* restart, or a proxy serving HTML for every `.js` — fails every import in the
* list, and one report per extension is then unbounded. Off cloud that is
* actively harmful: with no sink live, each report is held in `reportError`'s
* 25-entry pending buffer, so a handful of broken packs during bootstrap would
* silently crowd out every later error in the session. The batch is reported
* once, by `reportExtensionLoadFailures`.
*/
async function importCustomExtension(
ext: string
): Promise<ExtensionLoadFailure | undefined> {
try {
await import(/* @vite-ignore */ api.fileURL(ext))
} catch (error) {
return { ext, error }
}
}

/**
* The paths go in the message because `reportError` writes its console line
* from the error, not from `options.tags`. The count is tagged; backend paths
* are unbounded and belong in context instead of an indexed facet.
*/
export function reportExtensionLoadFailures(
failures: ExtensionLoadFailure[]
): void {
if (failures.length === 0) return

const named = failures.slice(0, MAX_NAMED_FAILED_EXTENSIONS)
const elided = failures.length - named.length
const paths = named.map(({ ext }) => ext).join(', ')
const noun = failures.length === 1 ? 'extension' : 'extensions'

reportError(
new Error(
`Error loading ${failures.length} ${noun}: ${paths}` +
(elided > 0 ? ` (+${elided} more)` : ''),
{ cause: failures[0].error }
),
{
errorType: 'error_loading_extension',
surface: 'platform',
level: 'warning',
tags: { failed_extension_count: failures.length },
context: {
failures: named.map(({ ext, error }) => ({
ext,
message: getErrorMessage(error)
}))
}
}
)
}

export const useExtensionService = () => {
const extensionStore = useExtensionStore()
const settingStore = useSettingStore()
Expand All @@ -106,37 +24,6 @@ export const useExtensionService = () => {
toastErrorHandler
} = useErrorHandling()

/**
* Loads all extensions from the API into the window in parallel
*/
const loadExtensions = async () => {
extensionStore.loadDisabledExtensionNames(
settingStore.get('Comfy.Extension.Disabled')
)

const extensions = await api.getExtensions()

// Need to load core extensions first as some custom extensions
// may depend on them.
await bootstrapTracer.settle(
'bootstrap/extensions-load-core',
() => import('../extensions/core/index')
)
extensionStore.captureCoreExtensions()
const outcomes = await bootstrapTracer.settle(
'bootstrap/extensions-load-custom',
() =>
Promise.all(
extensions
.filter((extension) =>
shouldLoadExtension(extension, __DISTRIBUTION__ === 'cloud')
)
.map((ext) => importCustomExtension(ext))
)
)
reportExtensionLoadFailures(outcomes.filter((outcome) => !!outcome))
}

/**
* Register an extension with the app
* @param extension The extension to register
Expand Down Expand Up @@ -320,7 +207,6 @@ export const useExtensionService = () => {
}

return {
loadExtensions,
registerExtension,
invokeExtensions,
invokeExtensionsAsync
Expand Down
Loading