LCOV - code coverage report
Current view: top level - src/scripts - app.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 75.1 % 963 723
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useEventListener, useResizeObserver } from '@vueuse/core'
       2              : import _ from 'es-toolkit/compat'
       3              : import { reactive, unref, shallowRef } from 'vue'
       4              : 
       5              : import { useToast } from '@/components/ui/toast/toastStore'
       6              : import { partnerRunGateBlocksAutoQueue } from '@/platform/billing/composables/usePartnerNodesRunGate'
       7              : import { useCanvasPositionConversion } from '@/composables/element/useCanvasPositionConversion'
       8              : import { normalizeCameraState } from '@/renderer/core/canvas/cameraState'
       9              : import {
      10              :   applyViewport,
      11              :   measureViewportFromElement
      12              : } from '@/renderer/core/canvas/canvasViewport'
      13              : import { useCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler'
      14              : import { selectedCanvasNodes } from '@/renderer/core/canvas/litegraph/selectionAdapter'
      15              : 
      16              : import { resolveDynamicInputSpec } from '@/core/graph/widgets/dynamicInputSpec'
      17              : import { setBackendNodeText, st, t } from '@/i18n'
      18              : import { normalizeI18nKey } from '@/utils/formatUtil'
      19              : import { ChangeTracker } from '@/scripts/changeTracker'
      20              : import { registerApp } from '@/scripts/appRegistry'
      21              : import {
      22              :   clipspace,
      23              :   copyToClipspace,
      24              :   pasteFromClipspace
      25              : } from '@/scripts/clipspace'
      26              : import type { Clipspace } from '@/scripts/clipspace'
      27              : import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu'
      28              : import { withGraphIntentSource } from '@/lib/litegraph/src/graphIntents'
      29              : import { createMutationView } from '@/lib/litegraph/src/infrastructure/createMutationView'
      30              : import {
      31              :   inputAsSerialisable,
      32              :   LGraph,
      33              :   LGraphCanvas,
      34              :   LGraphNode,
      35              :   LiteGraph
      36              : } from '@/lib/litegraph/src/litegraph'
      37              : import { snapPoint } from '@/lib/litegraph/src/measure'
      38              : import type { ISerialisedGraph, Vector2 } from '@/lib/litegraph/src/litegraph'
      39              : import type { IBaseWidget, TWidgetValue } from '@comfyorg/graph-model/widgets'
      40              : import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums'
      41              : import { useFreeTierQuota } from '@/platform/billing/subscription/composables/useFreeTierQuota'
      42              : import { isCloud } from '@/platform/distribution/types'
      43              : import { useKeybindingService } from '@/platform/keybindings/keybindingService'
      44              : import { useSettingStore } from '@/platform/settings/settingStore'
      45              : import { useTelemetry } from '@comfyorg/telemetry'
      46              : import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer'
      47              : import { installNodeAddedTelemetry } from '@/platform/telemetry/nodeAdded/installNodeAddedTelemetry'
      48              : import { reportError } from '@comfyorg/telemetry/reportError'
      49              : import { normalizeExecutionTriggerSource } from '@comfyorg/telemetry/types'
      50              : import { getExecutionContext } from '@/platform/telemetry/utils/getExecutionContext'
      51              : import { groupMissingNodesByPack } from '@/platform/telemetry/utils/groupMissingNodesByPack'
      52              : import { toWorkflowExecutionContext } from '@/platform/telemetry/utils/workflowExecutionContext'
      53              : import type {
      54              :   ExecutionContext,
      55              :   WorkflowExecutionContext,
      56              :   WorkflowExecutionIntent,
      57              :   WorkflowOpenSource,
      58              :   WorkflowQueueIntent
      59              : } from '@comfyorg/telemetry/types'
      60              : import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema'
      61              : import { restoreDynamicGroupInputs } from '@/platform/workflow/core/utils/restoreDynamicGroupInputs'
      62              : import { updatePendingWarnings } from '@/platform/workflow/core/utils/pendingWarnings'
      63              : import { useWorkflowService } from '@/platform/workflow/core/services/workflowService'
      64              : import {
      65              :   getDefaultWorkflow,
      66              :   initDefaultWorkflow
      67              : } from '@/platform/workflow/core/services/defaultWorkflowService'
      68              : import {
      69              :   ComfyWorkflow,
      70              :   useWorkflowStore
      71              : } from '@/platform/workflow/management/stores/workflowStore'
      72              : import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore'
      73              : import { useWorkflowValidation } from '@/platform/workflow/validation/composables/useWorkflowValidation'
      74              : import type {
      75              :   ComfyApiWorkflow,
      76              :   ComfyWorkflowJSON,
      77              :   WorkflowJSON04
      78              : } from '@comfyorg/graph-model/workflowSchema'
      79              : import { toNodeId } from '@comfyorg/graph-model/nodeId'
      80              : import { zNodePackMetadata } from '@comfyorg/graph-model/workflowSchema'
      81              : import type { NodeId, SerializedNodeId } from '@comfyorg/graph-model/nodeId'
      82              : import {
      83              :   buildSubgraphExecutionPaths,
      84              :   collectSubgraphDefinitions,
      85              :   parseFlattenableSubgraphDefinitions
      86              : } from '@/platform/workflow/core/utils/workflowFlattening'
      87              : import type { FlattenableWorkflowNode } from '@/platform/workflow/core/utils/workflowFlattening'
      88              : import type {
      89              :   ExecutionErrorWsMessage,
      90              :   NodeExecutionOutput
      91              : } from '@comfyorg/api-client/executionTypes'
      92              : import type { NodeError } from '@comfyorg/api-client/types'
      93              : import {
      94              :   isComboInputSpecV1,
      95              :   isComboInputSpecV2
      96              : } from '@comfyorg/object-info-parser'
      97              : import type { ComfyNodeDef as ComfyNodeDefV1 } from '@comfyorg/object-info-parser'
      98              : import { ComponentWidgetImpl, DOMWidgetImpl } from '@/scripts/domWidget'
      99              : import type { BaseDOMWidget } from '@/types/domWidget'
     100              : import { useAccountPreconditionDialog } from '@/platform/billing/subscription/composables/useAccountPreconditionDialog'
     101              : import {
     102              :   promptErrorHasLockedCredits,
     103              :   resolveAccountPrecondition
     104              : } from '@/platform/workflowErrors/errorCatalog/accountPreconditionRouting'
     105              : import { useJobModelDownloadStore } from '@/stores/jobModelDownloadStore'
     106              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
     107              : import { useDialogService } from '@/services/dialogService'
     108              : import {
     109              :   isGraphLoadHookOrigin,
     110              :   useExtensionService
     111              : } from '@/platform/extensions/services/extensionService'
     112              : import { beginChangeCaptureSuppression } from '@/services/changeCaptureSuppression'
     113              : import {
     114              :   isGraphLoadFailurePresented,
     115              :   markGraphLoadFailurePresented
     116              : } from '@/services/graphLoadFailure'
     117              : import { useLitegraphService } from '@/services/litegraphService'
     118              : import { useSubgraphService } from '@/services/subgraphService'
     119              : import { isDesktopHostSignedIn } from '@/platform/auth/desktopHost/desktopHostSession'
     120              : import { queueAuthToken } from '@/platform/auth/partnerNode/partnerNodeQueueToken'
     121              : import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore'
     122              : import { useCommandStore } from '@/platform/keybindings/commandStore'
     123              : import { createCanvasInteractionMode } from '@/renderer/core/canvas/interaction/canvasInteractionMode'
     124              : import { useDomWidgetStore } from '@/stores/domWidgetStore'
     125              : import { useExecutionStore } from '@/stores/executionStore'
     126              : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
     127              : import { useExtensionStore } from '@/platform/extensions/stores/extensionStore'
     128              : import { useAuthStore } from '@/stores/authStore'
     129              : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
     130              : import { useJobPreviewStore } from '@/stores/jobPreviewStore'
     131              : import {
     132              :   getAncestorExecutionIds,
     133              :   tryNormalizeNodeExecutionId
     134              : } from '@comfyorg/graph-model/nodeIdentification'
     135              : import { SYSTEM_NODE_DEFS, useNodeDefStore } from '@/stores/nodeDefStore'
     136              : import { useNodeReplacementStore } from '@/platform/workflowErrors/nodeReplacement/nodeReplacementStore'
     137              : import type { MissingNodeType } from '@/platform/workflowErrors/nodeReplacement/types'
     138              : 
     139              : import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore'
     140              : import { useWidgetStore } from '@/stores/widgetStore'
     141              : import { useWorkspaceStore } from '@/stores/workspaceStore'
     142              : import type {
     143              :   ComfyApp as IComfyApp,
     144              :   ComfyExtension,
     145              :   ComfyWidgetConstructor,
     146              :   CustomComfyWidgetConstructor,
     147              :   LoadGraphDataOptions,
     148              :   NodeDefsFetch,
     149              :   QueuePromptOptions
     150              : } from '@/types/comfy'
     151              : import type { ExtensionManager } from '@/types/extensionTypes'
     152              : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
     153              : import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil'
     154              : import { normalizePromptError } from '@/utils/executionErrorUtil'
     155              : import { graphToPrompt, unwrapExportedWidgetValue } from '@/utils/executionUtil'
     156              : import { parseJsonWithNonFinite } from '@/utils/jsonUtil'
     157              : import { getCnrIdFromProperties } from '@/platform/workflowErrors/nodeReplacement/cnrIdUtil'
     158              : import { useMissingNodesErrorStore } from '@/platform/workflowErrors/nodeReplacement/missingNodesErrorStore'
     159              : import { rescanAndSurfaceMissingNodes } from '@/platform/workflowErrors/nodeReplacement/missingNodeScan'
     160              : import {
     161              :   refreshMissingModelPipeline,
     162              :   runMissingModelPipeline
     163              : } from '@/platform/workflowErrors/missingModel/missingModelPipeline'
     164              : import { useMissingModelStore } from '@/platform/workflowErrors/missingModel/missingModelStore'
     165              : import type {
     166              :   MissingModelCandidate,
     167              :   MissingModelPipelineResult
     168              : } from '@/platform/workflowErrors/missingModel/types'
     169              : import type { MissingMediaCandidate } from '@/platform/workflowErrors/missingMedia/types'
     170              : import { runMissingMediaPipeline } from '@/platform/workflowErrors/missingMedia/missingMediaPipeline'
     171              : import { useMissingMediaStore } from '@/platform/workflowErrors/missingMedia/missingMediaStore'
     172              : 
     173              : import { getWorkflowMode } from '@comfyorg/graph-model/appMode'
     174              : import { anyItemOverlapsRect } from '@/utils/mathUtil'
     175              : import { ensureNonZeroUuid } from '@/utils/uuid'
     176              : import {
     177              :   collectAllNodes,
     178              :   forEachNode,
     179              :   getNodeByExecutionId,
     180              :   isAncestorPathActive,
     181              :   triggerCallbackOnAllNodes
     182              : } from '@/utils/graphTraversalUtil'
     183              : import {
     184              :   executeWidgetsCallback,
     185              :   isImageNode,
     186              :   isSelectOnly,
     187              :   isVideoNode
     188              : } from '@/utils/litegraphUtil'
     189              : import { createNode } from '@/utils/createNode'
     190              : import {
     191              :   createSharedObjectUrl,
     192              :   releaseSharedObjectUrl
     193              : } from '@/utils/objectUrlUtil'
     194              : import {
     195              :   findLegacyRerouteNodes,
     196              :   migrateLegacyRerouteNodes,
     197              :   noNativeReroutes
     198              : } from '@/utils/migration/migrateReroute'
     199              : import { deserialiseAndCreate } from '@/utils/vintageClipboard'
     200              : 
     201              : import type { ComfyApi } from '@comfyorg/api-client/ComfyApi'
     202              : import { PromptExecutionError } from '@comfyorg/api-client/ComfyApi'
     203              : import { api } from './api'
     204              : import { importA1111 } from './pnginfo'
     205              : import { applyPromotedWidgetControl } from '@/core/graph/subgraph/promotedWidgetControl'
     206              : import { syncPromotedComboHostOptions } from '@/core/graph/widgets/syncPromotedComboHostOptions'
     207              : import type { ComfyUI } from './ui'
     208              : import type { ComfyAppMenu } from './ui/menu/index'
     209              : import { clone } from './utils'
     210              : import { ensureCorrectLayoutScale } from '@/renderer/extensions/vueNodes/layout/ensureCorrectLayoutScale'
     211              : import {
     212              :   extractFilesFromDragEvent,
     213              :   hasAudioType,
     214              :   hasImageType,
     215              :   hasVideoType,
     216              :   isMediaFile
     217              : } from '@/utils/eventUtils'
     218              : import { getWorkflowDataFromFile } from '@/scripts/metadata/parser'
     219              : import { SUPPORTED_MESH_EXTENSIONS } from '@comfyorg/viewport3d/load3d/constants'
     220              : import Load3dUtils from '@/extensions/core/load3d/Load3dUtils'
     221              : import {
     222              :   pasteAudioNode,
     223              :   pasteAudioNodes,
     224              :   pasteImageNode,
     225              :   pasteImageNodes,
     226              :   pasteVideoNode,
     227              :   pasteVideoNodes
     228              : } from '@/composables/usePaste'
     229              : 
     230              : export { ANIM_PREVIEW_WIDGET } from '@/composables/node/useNodeAnimatedImage'
     231              : 
     232            0 : function isMeshModelFile(file: File): boolean {
     233            0 :   const name = file.name.toLowerCase()
     234            0 :   return SUPPORTED_MESH_EXTENSIONS.has(name.slice(name.lastIndexOf('.')))
     235              : }
     236              : 
     237          143 : export function sanitizeNodeName(string: string) {
     238          143 :   const entityMap = {
     239              :     '&': '',
     240              :     '<': '',
     241              :     '>': '',
     242              :     '"': '',
     243              :     "'": '',
     244              :     '`': '',
     245              :     '=': ''
     246              :   }
     247          143 :   return string.replace(/[&<>"'`=]/g, function fromEntityMap(s) {
     248            0 :     return entityMap[s as keyof typeof entityMap]
     249              :   })
     250              : }
     251              : 
     252         2356 : function createNodeOutputsMutationView(
     253              :   outputs: Partial<Record<string, NodeExecutionOutput>>,
     254              :   commit: (id: string, output: NodeExecutionOutput | undefined) => void
     255              : ): Partial<Record<string, NodeExecutionOutput>> {
     256         2356 :   const views = new WeakMap<object, Map<string, object>>()
     257         2356 :   const wrapNestedValue = (id: string, value: unknown): unknown => {
     258           68 :     if (value === null || typeof value !== 'object') return value
     259           68 :     const existing = views.get(value)?.get(id)
     260           68 :     if (existing) return existing
     261            7 :     const view = createMutationView(value, {
     262              :       commit: () => commit(id, outputs[id]),
     263              :       mapValue: (_property, nestedValue) => wrapNestedValue(id, nestedValue)
     264              :     })
     265           68 :     const viewsById = views.get(value) ?? new Map<string, object>()
     266           68 :     viewsById.set(id, view)
     267           68 :     views.set(value, viewsById)
     268           68 :     return view
     269              :   }
     270         2356 :   return new Proxy(outputs, {
     271              :     get(target, property) {
     272            4 :       const value = Reflect.get(target, property, target)
     273            4 :       if (typeof value === 'function') return value.bind(target)
     274            4 :       return wrapNestedValue(String(property), value)
     275              :     },
     276              :     set(target, property, value) {
     277          520 :       const previous = Reflect.get(target, property, target)
     278          520 :       const updated = Reflect.set(target, property, value, target)
     279          520 :       if (updated && previous !== value) {
     280          520 :         commit(String(property), value as NodeExecutionOutput)
     281              :       }
     282          520 :       return updated
     283              :     },
     284              :     deleteProperty(target, property) {
     285            0 :       const existed = Reflect.has(target, property)
     286            0 :       const deleted = Reflect.deleteProperty(target, property)
     287            0 :       if (deleted && existed) commit(String(property), undefined)
     288            0 :       return deleted
     289              :     }
     290              :   })
     291              : }
     292              : 
     293         2378 : function toNodeDefs(response: unknown): Record<string, ComfyNodeDefV1> {
     294         2378 :   const translateNodeDef = (def: ComfyNodeDefV1): ComfyNodeDefV1 => {
     295      2446581 :     return {
     296              :       ...def,
     297              :       category: (typeof def.category === 'string' ? def.category : '')
     298              :         .split('/')
     299              :         .map((category: string) =>
     300              :           st(`nodeCategories.${normalizeI18nKey(category)}`, category)
     301              :         )
     302              :         .join('/')
     303              :     }
     304              :   }
     305              : 
     306         2378 :   const isNodeDef = (value: unknown): value is ComfyNodeDefV1 =>
     307              :     typeof value === 'object' &&
     308              :     value !== null &&
     309              :     typeof (value as { name?: unknown }).name === 'string'
     310              : 
     311         2378 :   const entries =
     312              :     typeof response === 'object' &&
     313              :     response !== null &&
     314              :     !Array.isArray(response)
     315              :       ? Object.entries(response)
     316              :       : []
     317         2378 :   const defs: Record<string, ComfyNodeDefV1> = Object.fromEntries(
     318              :     entries.filter((entry): entry is [string, ComfyNodeDefV1] =>
     319              :       isNodeDef(entry[1])
     320              :     )
     321              :   )
     322         2378 :   setBackendNodeText(Object.values(defs))
     323         2378 :   return _.mapValues(defs, (def) => translateNodeDef(def))
     324              : }
     325              : 
     326         2356 : export class ComfyApp implements IComfyApp {
     327              :   /**
     328              :    * List of entries to queue
     329              :    */
     330              :   private queueItems: {
     331              :     number: number
     332              :     batchCount: number
     333              :     requestId: number
     334              :     queueNodeIds?: NodeExecutionId[]
     335              :     workflowQueueIntent?: WorkflowQueueIntent
     336              :   }[] = []
     337              :   private nextQueueRequestId = 1
     338              :   /**
     339              :    * If the queue is currently being processed
     340              :    */
     341              :   private processingQueue: boolean = false
     342              : 
     343              :   /**
     344              :    * Content Clipboard, kept as statics for custom-node compatibility.
     345              :    * State lives in `@/scripts/clipspace`.
     346              :    */
     347              :   static get clipspace(): Clipspace | null {
     348            0 :     return clipspace.current
     349              :   }
     350              :   static set clipspace(value: Clipspace | null) {
     351            0 :     clipspace.current = value
     352              :   }
     353              :   static get clipspace_invalidate_handler(): (() => void) | null {
     354            0 :     return clipspace.invalidateHandler
     355              :   }
     356              :   static set clipspace_invalidate_handler(value: (() => void) | null) {
     357            0 :     clipspace.invalidateHandler = value
     358              :   }
     359              :   static get clipspace_return_node(): LGraphNode | null {
     360         3774 :     return clipspace.returnNode
     361              :   }
     362              :   static set clipspace_return_node(value: LGraphNode | null) {
     363            0 :     clipspace.returnNode = value
     364              :   }
     365              :   static open_maskeditor: (() => void) | null = null
     366              :   static get maskeditor_is_opended(): (() => boolean) | null {
     367            0 :     return clipspace.maskEditorIsOpened
     368              :   }
     369              :   static set maskeditor_is_opended(value: (() => boolean) | null) {
     370            0 :     clipspace.maskEditorIsOpened = value
     371              :   }
     372              :   static copyToClipspace = copyToClipspace
     373              :   static pasteFromClipspace = pasteFromClipspace
     374              : 
     375              :   vueAppReady: boolean
     376              :   api: ComfyApi
     377              :   ui!: ComfyUI
     378              :   extensionManager!: ExtensionManager
     379              :   private readonly nodeOutputsData: Partial<
     380              :     Record<string, NodeExecutionOutput>
     381              :   > = {}
     382              :   private readonly _nodeOutputs = createNodeOutputsMutationView(
     383              :     this.nodeOutputsData,
     384              :     (id, output) => {
     385          520 :       if (!this.vueAppReady) return
     386          520 :       const store = useNodeOutputStore()
     387          520 :       if (output === undefined) store.removeOutputFromLegacy(id)
     388          520 :       else store.setOutputFromLegacy(id, output)
     389              :     }
     390              :   )
     391              :   nodePreviewImages: Partial<Record<string, string[]>>
     392              : 
     393              :   private readonly rootGraphRef = shallowRef<LGraph | undefined>(undefined)
     394              : 
     395              :   private get rootGraphInternal(): LGraph | undefined {
     396       940363 :     return this.rootGraphRef.value
     397              :   }
     398              : 
     399              :   private set rootGraphInternal(graph: LGraph | undefined) {
     400         2355 :     this.rootGraphRef.value = graph
     401              :   }
     402              : 
     403              :   // TODO: Migrate internal usage to the
     404              :   /** @deprecated Use {@link rootGraph} instead */
     405              :   get graph() {
     406         4760 :     return this.rootGraphInternal!
     407              :   }
     408              : 
     409              :   get rootGraph(): LGraph {
     410       231747 :     if (!this.rootGraphInternal) {
     411            1 :       console.error('ComfyApp graph accessed before initialization')
     412              :     }
     413       231747 :     return this.rootGraphInternal!
     414              :   }
     415              : 
     416              :   get rootGraphOrUndefined(): LGraph | undefined {
     417       362192 :     return this.rootGraphInternal
     418              :   }
     419              : 
     420              :   /**
     421              :    * Whether the root graph has been initialized. Safe to check without
     422              :    * triggering error logs, and reactive, so it can be watched.
     423              :    */
     424              :   get isGraphReady(): boolean {
     425       109917 :     return !!this.rootGraphInternal
     426              :   }
     427              : 
     428              :   private canvasInternal: LGraphCanvas | undefined
     429              : 
     430              :   /** The canvas, once {@link setup} has created it. Accessing it earlier is a bug. */
     431              :   get canvas(): LGraphCanvas {
     432       181076 :     return this.canvasInternal!
     433              :   }
     434              : 
     435              :   set canvas(value: LGraphCanvas) {
     436         2355 :     this.canvasInternal = value
     437              :   }
     438              : 
     439              :   /** Same as {@link canvas}, but `undefined` before {@link setup} creates it. */
     440              :   get canvasOrUndefined(): LGraphCanvas | undefined {
     441         6708 :     return this.canvasInternal
     442              :   }
     443              : 
     444              :   dragOverNode: Pick<LGraphNode, 'onDragDrop' | 'id'> | null = null
     445              :   readonly canvasElRef = shallowRef<HTMLCanvasElement>()
     446              :   get canvasEl() {
     447              :     // TODO: Fix possibly undefined reference
     448        20838 :     return unref(this.canvasElRef)!
     449              :   }
     450              : 
     451              :   private configuringGraphLevel: number = 0
     452              :   private graphLoadSequence = 0
     453              :   private graphLoadQueue: Promise<void> = Promise.resolve()
     454              :   private fileLoadDiscoveryQueue: Promise<void> = Promise.resolve()
     455              :   private pendingCamera:
     456              :     | { id: number; workflow: string | null | ComfyWorkflow }
     457              :     | undefined
     458              :   get configuringGraph() {
     459        23003 :     return this.configuringGraphLevel > 0
     460              :   }
     461              :   ctx!: CanvasRenderingContext2D
     462              :   bodyTop!: HTMLElement
     463              :   bodyLeft!: HTMLElement
     464              :   bodyRight!: HTMLElement
     465              :   bodyBottom!: HTMLElement
     466              :   canvasContainer!: HTMLElement
     467              :   menu!: ComfyAppMenu
     468              :   // Set by Comfy.Clipspace extension
     469              :   openClipspace: () => void = () => {}
     470              : 
     471              :   private positionConversion?: {
     472              :     clientPosToCanvasPos: (pos: Vector2) => Vector2
     473              :     canvasPosToClientPos: (pos: Vector2) => Vector2
     474              :   }
     475              : 
     476              :   /**
     477              :    * The node errors from the previous execution.
     478              :    * @deprecated Use app.extensionManager.lastNodeErrors instead
     479              :    */
     480              :   get lastNodeErrors(): Record<string, NodeError> | null {
     481            0 :     return useExecutionErrorStore().lastNodeErrors
     482              :   }
     483              : 
     484              :   /**
     485              :    * The error from the previous execution.
     486              :    * @deprecated Use app.extensionManager.lastExecutionError instead
     487              :    */
     488              :   get lastExecutionError(): ExecutionErrorWsMessage | null {
     489        65820 :     return useExecutionErrorStore().lastExecutionError
     490              :   }
     491              : 
     492              :   /**
     493              :    * @deprecated Use useExecutionStore().executingNodeId instead
     494              :    * TODO: Update to support multiple executing nodes. This getter returns only the first executing node.
     495              :    * Consider updating consumers to handle multiple nodes or use executingNodeIds array.
     496              :    */
     497              :   get runningNodeId(): SerializedNodeId | null {
     498            0 :     return useExecutionStore().executingNodeId
     499              :   }
     500              : 
     501              :   /**
     502              :    * @deprecated Use useWorkspaceStore().shiftDown instead
     503              :    */
     504              :   get shiftDown(): boolean {
     505            0 :     return useWorkspaceStore().shiftDown
     506              :   }
     507              : 
     508              :   /**
     509              :    * @deprecated Use useWidgetStore().widgets instead
     510              :    */
     511              :   get widgets(): Record<
     512              :     string,
     513              :     ComfyWidgetConstructor | CustomComfyWidgetConstructor
     514              :   > {
     515           71 :     const widgetStore = useWidgetStore()
     516           71 :     return Object.assign(
     517              :       Object.fromEntries(widgetStore.widgets.entries()),
     518              :       widgetStore.coreWidgets
     519              :     )
     520              :   }
     521              : 
     522              :   /**
     523              :    * @deprecated storageLocation is always 'server' since
     524              :    * https://github.com/comfyanonymous/ComfyUI/commit/53c8a99e6c00b5e20425100f6680cd9ea2652218
     525              :    */
     526              :   get storageLocation() {
     527            0 :     return 'server'
     528              :   }
     529              : 
     530              :   /**
     531              :    * @deprecated storage migration is no longer needed.
     532              :    */
     533              :   get isNewUserSession() {
     534            0 :     return false
     535              :   }
     536              : 
     537              :   /**
     538              :    * @deprecated Use useExtensionStore().extensions instead
     539              :    */
     540              :   get extensions(): ComfyExtension[] {
     541            0 :     return useExtensionStore().extensions
     542              :   }
     543              : 
     544              :   /**
     545              :    * The progress on the current executing node, if the node reports any.
     546              :    * @deprecated Use useExecutionStore().executingNodeProgress instead
     547              :    */
     548              :   get progress() {
     549            0 :     return useExecutionStore()._executingNodeProgress
     550              :   }
     551              : 
     552              :   /**
     553              :    * @deprecated Use {@link isImageNode} from @/utils/litegraphUtil instead
     554              :    */
     555              :   static isImageNode(node: LGraphNode) {
     556            0 :     return isImageNode(node)
     557              :   }
     558              : 
     559              :   /**
     560              :    * Resets the canvas view to the default
     561              :    * @deprecated Use {@link useLitegraphService().resetView} instead
     562              :    */
     563              :   resetView() {
     564            0 :     useLitegraphService().resetView()
     565              :   }
     566              : 
     567              :   constructor() {
     568         2356 :     this.vueAppReady = false
     569         2356 :     this.api = api
     570         2356 :     this.nodeOutputs = {}
     571         2356 :     this.nodePreviewImages = {}
     572              :   }
     573              : 
     574              :   get nodeOutputs() {
     575         2879 :     return this._nodeOutputs
     576              :   }
     577              : 
     578              :   set nodeOutputs(value) {
     579         6454 :     if (value !== this._nodeOutputs) {
     580         6454 :       for (const key of Object.keys(this.nodeOutputsData))
     581           26 :         delete this.nodeOutputsData[key]
     582         6454 :       Object.assign(this.nodeOutputsData, value)
     583              :     }
     584         6454 :     if (this.vueAppReady) {
     585         4098 :       useNodeOutputStore().replaceOutputsFromLegacy(this.nodeOutputsData)
     586         4098 :       useExtensionService().invokeExtensions('onNodeOutputsUpdated', value)
     587              :     }
     588              :   }
     589              : 
     590              :   /**
     591              :    * If the user has specified a preferred format to receive preview images in,
     592              :    * this function will return that format as a url query param.
     593              :    * If the node's outputs are not images, this param should not be used, as it will
     594              :    * force the server to load the output file as an image.
     595              :    */
     596              :   getPreviewFormatParam() {
     597          635 :     const preview_format = useSettingStore().get('Comfy.PreviewFormat')
     598          635 :     if (preview_format) return `&preview=${preview_format}`
     599          635 :     else return ''
     600              :   }
     601              : 
     602              :   getRandParam() {
     603              :     if (isCloud) return ''
     604          485 :     return '&rand=' + Math.random()
     605              :   }
     606              : 
     607              :   static onClipspaceEditorSave() {
     608            0 :     if (clipspace.returnNode) {
     609            0 :       ComfyApp.pasteFromClipspace(clipspace.returnNode)
     610              :     }
     611              :   }
     612              : 
     613              :   static onClipspaceEditorClosed() {
     614            0 :     clipspace.returnNode = null
     615              :   }
     616              : 
     617              :   /**
     618              :    * Adds a handler allowing drag+drop of files onto the window to load workflows
     619              :    */
     620              :   private addDropHandler() {
     621              :     // Get prompt from dropped PNG or json
     622         2355 :     useEventListener(document, 'drop', async (event: DragEvent) => {
     623           88 :       try {
     624              :         // Skip if already handled (e.g. file drop onto publish dialog tiles)
     625           88 :         if (event.defaultPrevented) return
     626              : 
     627           79 :         event.preventDefault()
     628           79 :         event.stopPropagation()
     629              : 
     630              :         // graph_mouse is only updated on mousemove, so when files are dragged
     631              :         // in from another window the canvas-space cursor is stale. Sync it
     632              :         // from the drop event so nodes created below land at the cursor.
     633           79 :         const canvas: LGraphCanvas = this.canvas
     634           79 :         canvas.adjustMouseEvent(event)
     635           79 :         canvas.graph_mouse[0] = event.canvasX
     636           79 :         canvas.graph_mouse[1] = event.canvasY
     637              : 
     638           79 :         const n = this.dragOverNode
     639           79 :         this.dragOverNode = null
     640           79 :         if (isSelectOnly(canvas)) return
     641              :         // Node handles file drop, we dont use the built in onDropFile handler as its buggy
     642              :         // If you drag multiple files it will call it multiple times with the same file
     643           79 :         if (await n?.onDragDrop?.(event)) return
     644              : 
     645           39 :         const files = await extractFilesFromDragEvent(event)
     646           45 :         if (files.length === 0) {
     647            1 :           if (event.dataTransfer?.types.includes(MIME_ASSET_INFO)) {
     648            0 :             reportError(new Error('Dropped asset card yielded no file'), {
     649              :               surface: 'graph',
     650              :               errorType: 'asset_drop_load_failure'
     651              :             })
     652            0 :             useToast().warning(t('toastMessages.assetDropFailed'))
     653              :           }
     654            1 :           return
     655              :         }
     656              : 
     657           38 :         const workspace = useWorkspaceStore()
     658           40 :         try {
     659           38 :           workspace.spinner = true
     660           38 :           const imageFiles = files.filter(hasImageType)
     661           38 :           const audioFiles = files.filter(hasAudioType)
     662           38 :           const videoFiles = files.filter(hasVideoType)
     663              :           const totalMedia =
     664              :             imageFiles.length + audioFiles.length + videoFiles.length
     665           38 :           const hasMultipleMedia = totalMedia > 1
     666              : 
     667           38 :           const createdNodes: LGraphNode[] = []
     668           38 :           const handleFileOptions = {
     669              :             deferWarnings: true,
     670              :             onNodeCreated: (node: LGraphNode) => createdNodes.push(node)
     671              :           }
     672              : 
     673           40 :           if (hasMultipleMedia) {
     674            0 :             if (imageFiles.length > 0) {
     675            0 :               await this.handleFileList(imageFiles)
     676              :             }
     677            0 :             if (audioFiles.length > 0) {
     678            0 :               await this.handleAudioFileList(audioFiles)
     679              :             }
     680            0 :             if (videoFiles.length > 0) {
     681            0 :               await this.handleVideoFileList(videoFiles)
     682              :             }
     683            0 :             for (const file of files.filter((f) => !isMediaFile(f))) {
     684            0 :               await this.handleFile(file, 'file_drop', handleFileOptions)
     685              :             }
     686              :           } else {
     687           38 :             for (const file of files) {
     688           38 :               await this.handleFile(file, 'file_drop', handleFileOptions)
     689              :             }
     690              :           }
     691              : 
     692           38 :           this.positionNodes(createdNodes)
     693              :         } finally {
     694           38 :           workspace.spinner = false
     695              :         }
     696           38 :         useWorkflowService().showPendingWarnings()
     697              :       } catch (error: unknown) {
     698            0 :         if (isGraphLoadFailurePresented(error)) return
     699            0 :         useToast().warning(
     700              :           t('toastMessages.dropFileError', {
     701              :             error: getErrorMessage(error) ?? t('g.unknownError')
     702              :           })
     703              :         )
     704              :       }
     705              :     })
     706              : 
     707              :     // Always clear over node on drag leave
     708         2355 :     useEventListener(this.canvasElRef, 'dragleave', async () => {
     709            0 :       if (!this.dragOverNode) return
     710            0 :       this.dragOverNode = null
     711            0 :       this.canvas.setDirty(false, true)
     712              :     })
     713              : 
     714              :     // Add handler for dropping onto a specific node
     715         2355 :     useEventListener(
     716              :       this.canvasElRef,
     717              :       'dragover',
     718              :       (event: DragEvent) => {
     719           80 :         const canvas: LGraphCanvas = this.canvas
     720           80 :         canvas.adjustMouseEvent(event)
     721           80 :         const node = canvas.graph?.getNodeOnPos(event.canvasX, event.canvasY)
     722              : 
     723           80 :         if (!node?.onDragOver?.(event)) {
     724           41 :           this.dragOverNode = null
     725           41 :           return
     726              :         }
     727              : 
     728           39 :         this.dragOverNode = node
     729              : 
     730              :         // dragover event is fired very frequently, run this on an animation frame
     731           39 :         requestAnimationFrame(() => {
     732           39 :           this.canvas.setDirty(false, true)
     733              :         })
     734              :       },
     735              :       false
     736              :     )
     737              :   }
     738              : 
     739              :   /**
     740              :    * Handle keypress
     741              :    */
     742              :   private addProcessKeyHandler() {
     743         2355 :     const origProcessKey = LGraphCanvas.prototype.processKey
     744         2355 :     LGraphCanvas.prototype.processKey = function (e: KeyboardEvent) {
     745         2085 :       if (!this.graph) return
     746              : 
     747         2085 :       if (e.target instanceof Element && e.target.localName == 'input') {
     748          503 :         return
     749              :       }
     750              : 
     751         1989 :       if (useKeybindingService().executeCanvasKeybinding(e)) {
     752          118 :         this.graph.change()
     753          118 :         return
     754              :       }
     755              : 
     756              :       // Fall through to Litegraph defaults
     757         1464 :       return origProcessKey.apply(this, [e])
     758              :     }
     759              :   }
     760              : 
     761              :   /**
     762              :    * Handles updates from the API socket
     763              :    */
     764              :   private addApiUpdateHandlers() {
     765         2355 :     api.addEventListener('status', ({ detail }) => {
     766         2639 :       this.ui.setStatus(detail)
     767              :     })
     768              : 
     769         2355 :     api.addEventListener('progress', () => {
     770           22 :       this.canvas.setDirty(true, false)
     771              :     })
     772              : 
     773         2355 :     api.addEventListener('executing', () => {
     774          192 :       this.canvas.setDirty(true, false)
     775              :     })
     776              : 
     777         2355 :     api.addEventListener('executed', ({ detail }) => {
     778          155 :       if (
     779              :         !useExecutionStore().rememberStampAndCheckActiveWorkflow(
     780              :           detail.prompt_id,
     781              :           detail.workflow_id
     782              :         )
     783              :       )
     784            1 :         return
     785          154 :       const nodeOutputStore = useNodeOutputStore()
     786          155 :       const executionId = tryNormalizeNodeExecutionId(
     787              :         detail.display_node || detail.node
     788              :       )
     789          155 :       if (!executionId) return
     790              : 
     791          154 :       nodeOutputStore.setNodeOutputsByExecutionId(executionId, detail.output, {
     792              :         merge: detail.merge
     793              :       })
     794              : 
     795          154 :       const node = getNodeByExecutionId(this.rootGraph, executionId)
     796          155 :       if (node && node.onExecuted) {
     797            1 :         node.onExecuted(detail.output)
     798              :       }
     799              :     })
     800              : 
     801         2355 :     api.addEventListener('execution_start', () => {
     802          125 :       triggerCallbackOnAllNodes(this.rootGraph, 'onExecutionStart')
     803              :     })
     804              : 
     805         2355 :     api.addEventListener('execution_error', ({ detail }) => {
     806           13 :       const precondition = resolveAccountPrecondition({
     807              :         exceptionType: detail.exception_type,
     808              :         exceptionMessage: detail.exception_message
     809              :       })
     810           13 :       if (precondition) {
     811            0 :         void useAccountPreconditionDialog().open(precondition, {
     812              :           nodeType: detail.node_type,
     813              :           runRefused: true
     814              :         })
     815              :       }
     816           13 :       this.canvas.draw(true, true)
     817              :     })
     818              : 
     819         2355 :     api.addEventListener('b_preview_with_metadata', ({ detail }) => {
     820            4 :       const { blob, displayNodeId, jobId, workflowId } = detail
     821            4 :       const { setNodePreviewsByExecutionId, revokePreviewsByExecutionId } =
     822              :         useNodeOutputStore()
     823            4 :       const displayNodeExecutionId = tryNormalizeNodeExecutionId(displayNodeId)
     824            4 :       if (!displayNodeExecutionId) return
     825            4 :       const blobUrl = createSharedObjectUrl(blob)
     826            4 :       useJobPreviewStore().setPreviewUrl(jobId, blobUrl, displayNodeId)
     827            4 :       if (
     828              :         !useExecutionStore().rememberStampAndCheckActiveWorkflow(
     829              :           jobId,
     830              :           workflowId
     831              :         )
     832              :       ) {
     833            0 :         releaseSharedObjectUrl(blobUrl)
     834            0 :         return
     835              :       }
     836            4 :       revokePreviewsByExecutionId(displayNodeExecutionId)
     837            4 :       for (const executionId of getAncestorExecutionIds(
     838              :         displayNodeExecutionId
     839              :       )) {
     840            5 :         setNodePreviewsByExecutionId(executionId, [blobUrl])
     841              :       }
     842            4 :       releaseSharedObjectUrl(blobUrl)
     843              :     })
     844              : 
     845         2355 :     api.addEventListener('feature_flags', () => {
     846         2344 :       void useNodeReplacementStore().load()
     847              :     })
     848              : 
     849         2355 :     useJobModelDownloadStore()
     850              : 
     851         2355 :     void api.init()
     852              :   }
     853              : 
     854              :   /** Flag that the graph is configuring to prevent nodes from running checks while its still loading */
     855              :   private addConfigureHandler() {
     856         2355 :     const configure = LGraph.prototype.configure
     857         2355 :     const trackConfiguring = <T>(run: () => T): T => {
     858         4648 :       this.configuringGraphLevel++
     859         4648 :       try {
     860         4648 :         return run()
     861              :       } finally {
     862         4648 :         this.configuringGraphLevel--
     863              :       }
     864              :     }
     865         2355 :     LGraph.prototype.configure = function (...args) {
     866         4648 :       return trackConfiguring(() => configure.apply(this, args))
     867              :     }
     868              :   }
     869              : 
     870              :   private addAfterConfigureHandler(graph: LGraph) {
     871         2355 :     const { onConfigure } = graph
     872         2355 :     graph.onConfigure = function (...args) {
     873              :       // Fire callbacks before the onConfigure, this is used by widget inputs to setup the config
     874         3881 :       triggerCallbackOnAllNodes(this, 'onGraphConfigured')
     875              : 
     876         3881 :       const r = onConfigure?.apply(this, args)
     877              : 
     878              :       // Fire after onConfigure, used by primitives to generate widget using input nodes config
     879         3881 :       triggerCallbackOnAllNodes(this, 'onAfterGraphConfigured')
     880              : 
     881         3881 :       return r
     882              :     }
     883              :   }
     884              : 
     885              :   /**
     886              :    * Set up the app on the page
     887              :    */
     888              :   async setup(canvasEl: HTMLCanvasElement) {
     889         2355 :     this.bodyTop = document.getElementById('comfyui-body-top')!
     890         2355 :     this.bodyLeft = document.getElementById('comfyui-body-left')!
     891         2355 :     this.bodyRight = document.getElementById('comfyui-body-right')!
     892         2355 :     this.bodyBottom = document.getElementById('comfyui-body-bottom')!
     893         2355 :     this.canvasContainer = document.getElementById('graph-canvas-container')!
     894              : 
     895         2355 :     this.canvasElRef.value = canvasEl
     896              : 
     897         2355 :     const defaultWorkflowReady = initDefaultWorkflow()
     898              : 
     899         2355 :     await bootstrapTracer.settle('bootstrap/sync-workflows', () =>
     900              :       useWorkspaceStore().workflow.syncWorkflows()
     901              :     )
     902              : 
     903         2355 :     this.addProcessKeyHandler()
     904         2355 :     this.addConfigureHandler()
     905         2355 :     this.addApiUpdateHandlers()
     906              : 
     907         2355 :     const graph = new LGraph()
     908              : 
     909              :     // Register the subgraph - adds type wrapper for Litegraph's `createNode` factory
     910         2355 :     graph.events.addEventListener('subgraph-created', (e) => {
     911          767 :       try {
     912          767 :         const { subgraph, data } = e.detail
     913          767 :         useSubgraphService().registerNewSubgraph(subgraph, data)
     914              :       } catch (err) {
     915            0 :         console.error('Failed to register subgraph', err)
     916            0 :         useToast().error(t('toastMessages.failedToRegisterSubgraph'), {
     917              :           description: getErrorMessage(err) ?? t('g.unknownError')
     918              :         })
     919              :       }
     920              :     })
     921              : 
     922         2355 :     this.addAfterConfigureHandler(graph)
     923              : 
     924         2355 :     this.rootGraphInternal = graph
     925         2355 :     installNodeAddedTelemetry(graph)
     926         2355 :     const interactionMode = createCanvasInteractionMode()
     927         2355 :     this.canvas = new LGraphCanvas(canvasEl, graph, { interactionMode })
     928         2355 :     useCommandStore().setInteractionMode(interactionMode)
     929              :     // Make canvas states reactive so we can observe changes on them.
     930         2355 :     this.canvas.state = reactive(this.canvas.state)
     931              : 
     932         2355 :     this.ctx = canvasEl.getContext('2d')!
     933              : 
     934         2355 :     LiteGraph.alt_drag_do_clone_nodes = true
     935         2355 :     LiteGraph.macGesturesRequireMac = false
     936              : 
     937         2355 :     this.canvas.canvas.addEventListener<'litegraph:set-graph'>(
     938              :       'litegraph:set-graph',
     939              :       (e) => {
     940          216 :         const { newGraph } = e.detail
     941              : 
     942          216 :         const widgetStore = useDomWidgetStore()
     943              : 
     944          216 :         const activeWidgets: Record<string, BaseDOMWidget> = Object.fromEntries(
     945              :           newGraph.nodes
     946              :             .flatMap((node) => node.widgets ?? [])
     947              :             .filter(
     948              :               (w) =>
     949              :                 w instanceof DOMWidgetImpl || w instanceof ComponentWidgetImpl
     950              :             )
     951              :             .map((w) => [w.id, w])
     952              :         )
     953              : 
     954          216 :         for (const [
     955              :           widgetId,
     956              :           widgetState
     957              :         ] of widgetStore.widgetStates.entries()) {
     958          356 :           if (widgetId in activeWidgets) {
     959          138 :             widgetState.active = true
     960          138 :             widgetState.widget = activeWidgets[widgetId]
     961              :           } else {
     962          218 :             widgetState.active = false
     963              :           }
     964              :         }
     965              :       }
     966              :     )
     967              : 
     968              :     // Ensure subgraphs are scaled when entering them
     969         2355 :     this.canvas.canvas.addEventListener<'litegraph:set-graph'>(
     970              :       'litegraph:set-graph',
     971              :       (e) => {
     972          216 :         const { newGraph, oldGraph } = e.detail
     973              :         // Only scale when switching between graphs (not during initial setup)
     974              :         // oldGraph is null/undefined during initial setup, so skip scaling then
     975          216 :         if (oldGraph) {
     976          216 :           ensureCorrectLayoutScale(
     977              :             newGraph.extra.workflowRendererVersion,
     978              :             newGraph
     979              :           )
     980              :         }
     981              :       }
     982              :     )
     983              : 
     984              :     // Ensure the canvas fills the window
     985         2355 :     useResizeObserver(this.canvasElRef, ([canvasEl]) => {
     986         2533 :       if (canvasEl.target instanceof HTMLCanvasElement) {
     987         2533 :         this.resizeCanvas(canvasEl.target)
     988              :       }
     989              :     })
     990              : 
     991         2355 :     await bootstrapTracer.settle('bootstrap/extensions-init', () =>
     992              :       useExtensionService().invokeExtensionsAsync('init')
     993              :     )
     994              : 
     995         2355 :     await this.registerNodes()
     996              : 
     997         2355 :     this.addDropHandler()
     998              : 
     999         2355 :     await bootstrapTracer.settle('bootstrap/extensions-setup', () =>
    1000              :       useExtensionService().invokeExtensionsAsync('setup')
    1001              :     )
    1002              : 
    1003         2355 :     await defaultWorkflowReady
    1004              : 
    1005         2355 :     this.positionConversion = useCanvasPositionConversion(
    1006              :       this.canvasContainer,
    1007              :       this.canvas
    1008              :     )
    1009              :   }
    1010              : 
    1011              :   private resizeCanvas(canvas: HTMLCanvasElement) {
    1012         2533 :     const viewport = measureViewportFromElement(canvas)
    1013         2533 :     applyViewport(viewport, canvas, this.canvas.bgcanvas, this.canvas)
    1014         2533 :     useCanvasScheduler().flush()
    1015         2533 :     this.canvas.draw(true, true)
    1016              :   }
    1017              : 
    1018              :   private updateVueAppNodeDefs(defs: Record<string, ComfyNodeDefV1>) {
    1019              :     // Frontend only nodes registered by custom nodes.
    1020              :     // Example: https://github.com/rgthree/rgthree-comfy/blob/dd534e5384be8cf0c0fa35865afe2126ba75ac55/src_web/comfyui/fast_groups_bypasser.ts#L10
    1021              : 
    1022              :     // Only create frontend_only definitions for nodes that don't have backend definitions
    1023         2371 :     const frontendOnlyDefs: Record<string, ComfyNodeDefV1> = {}
    1024         2371 :     for (const [name, node] of Object.entries(
    1025              :       LiteGraph.registered_node_types
    1026              :     )) {
    1027              :       // Skip if we already have a backend definition or system definition
    1028      2448850 :       if (name in defs || name in SYSTEM_NODE_DEFS || node.skip_list) {
    1029      2448850 :         continue
    1030              :       }
    1031              : 
    1032      2448850 :       frontendOnlyDefs[name] = {
    1033              :         name,
    1034              :         display_name: name,
    1035              :         category: node.category || '__frontend_only__',
    1036              :         input: { required: {}, optional: {} },
    1037              :         output: [],
    1038              :         output_name: [],
    1039              :         output_is_list: [],
    1040              :         output_node: false,
    1041              :         python_module: 'custom_nodes.frontend_only',
    1042              :         description: node.description ?? `Frontend only node for ${name}`
    1043              :       }
    1044              :     }
    1045              : 
    1046         2371 :     const allNodeDefs = {
    1047              :       ...frontendOnlyDefs,
    1048              :       ...defs,
    1049              :       ...SYSTEM_NODE_DEFS
    1050              :     }
    1051              : 
    1052         2371 :     const nodeDefStore = useNodeDefStore()
    1053         2371 :     const nodeDefArray: ComfyNodeDefV1[] = Object.values(allNodeDefs)
    1054         2371 :     useExtensionService().invokeExtensions(
    1055              :       'beforeRegisterVueAppNodeDefs',
    1056              :       nodeDefArray
    1057              :     )
    1058         2371 :     nodeDefStore.updateNodeDefs(nodeDefArray)
    1059              :   }
    1060              : 
    1061              :   async getNodeDefs(): Promise<Record<string, ComfyNodeDefV1>> {
    1062         2374 :     return toNodeDefs(await api.getNodeDefs())
    1063              :   }
    1064              : 
    1065              :   /** Fetches the node definitions, or the HTTP status when the server refused, instead of reading the refusal as definitions. */
    1066              :   async fetchNodeDefs(): Promise<NodeDefsFetch> {
    1067            8 :     const response = await api.fetchNodeDefs()
    1068            6 :     if (!response.ok) return { ok: false, status: response.status }
    1069            6 :     return { ok: true, defs: toNodeDefs(await response.json()) }
    1070              :   }
    1071              : 
    1072              :   /**
    1073              :    * Registers nodes with the graph
    1074              :    */
    1075              :   async registerNodes() {
    1076         2355 :     const defs = await bootstrapTracer.settle('bootstrap/object-info', () =>
    1077              :       this.getNodeDefs()
    1078              :     )
    1079              : 
    1080         2355 :     await bootstrapTracer.settle('bootstrap/extensions', async () => {
    1081         2355 :       await this.registerNodesFromDefs(defs)
    1082         2355 :       await useExtensionService().invokeExtensionsAsync('registerCustomNodes')
    1083              :     })
    1084         2355 :     if (this.vueAppReady) {
    1085         2355 :       const phase = bootstrapTracer.startPhase('bootstrap/vue-node-defs')
    1086         2355 :       try {
    1087         2355 :         this.updateVueAppNodeDefs(defs)
    1088              :       } finally {
    1089         2355 :         phase.stop()
    1090              :       }
    1091              :     }
    1092              :   }
    1093              : 
    1094              :   async registerNodeDef(nodeId: string, nodeDef: ComfyNodeDefV1) {
    1095      2440403 :     return await useLitegraphService().registerNodeDef(nodeId, nodeDef)
    1096              :   }
    1097              : 
    1098              :   async registerNodesFromDefs(defs: Record<string, ComfyNodeDefV1>) {
    1099         2355 :     await useExtensionService().invokeExtensionsAsync('addCustomNodeDefs', defs)
    1100              : 
    1101              :     // Register a node for each definition
    1102         2355 :     await Promise.all(
    1103              :       Object.keys(defs).map((nodeId) =>
    1104              :         this.registerNodeDef(nodeId, defs[nodeId])
    1105              :       )
    1106              :     )
    1107              :   }
    1108              : 
    1109              :   loadTemplateData(templateData: {
    1110              :     templates?: { name?: string; data?: string }[]
    1111              :   }): void {
    1112            0 :     if (!templateData.templates) {
    1113            0 :       return
    1114              :     }
    1115              : 
    1116            0 :     const old = localStorage.getItem('litegrapheditor_clipboard')
    1117              : 
    1118            0 :     for (const template of templateData.templates) {
    1119            0 :       if (!template.data) {
    1120            0 :         continue
    1121              :       }
    1122              : 
    1123              :       // Check for old clipboard format
    1124              :       const data = parseJsonWithNonFinite<{ reroutes?: unknown }>(template.data)
    1125            0 :       if (!data.reroutes) {
    1126            0 :         deserialiseAndCreate(template.data, app.canvas)
    1127              :       } else {
    1128            0 :         localStorage.setItem('litegrapheditor_clipboard', template.data)
    1129            0 :         app.canvas.pasteFromClipboard()
    1130              :       }
    1131              : 
    1132              :       // Move mouse position down to paste the next template below
    1133            0 :       let maxY: number | undefined
    1134              : 
    1135            0 :       for (const node of selectedCanvasNodes(app.canvas)) {
    1136            0 :         const nodeBottom = node.pos[1] + node.size[1]
    1137            0 :         if (maxY === undefined || nodeBottom > maxY) {
    1138            0 :           maxY = nodeBottom
    1139              :         }
    1140              :       }
    1141              : 
    1142            0 :       if (maxY !== undefined) {
    1143            0 :         app.canvas.graph_mouse[1] = maxY + 50
    1144              :       }
    1145              :     }
    1146              : 
    1147            0 :     if (old !== null) {
    1148            0 :       localStorage.setItem('litegrapheditor_clipboard', old)
    1149              :     }
    1150              :   }
    1151              : 
    1152              :   private showMissingNodesError(
    1153              :     missingNodeTypes: MissingNodeType[],
    1154              :     options?: { deferWarnings?: boolean }
    1155              :   ) {
    1156            2 :     const activeWorkflow = useWorkspaceStore().workflow.activeWorkflow
    1157            2 :     updatePendingWarnings(activeWorkflow, { missingNodeTypes })
    1158            2 :     if (!options?.deferWarnings) {
    1159            2 :       useWorkflowService().showPendingWarnings(activeWorkflow)
    1160              :     }
    1161              :   }
    1162              : 
    1163              :   private async reportGraphLoadFailure(
    1164              :     error: unknown,
    1165              :     options: { notifyExtensions?: boolean } = {}
    1166              :   ): Promise<Error> {
    1167            6 :     const { notifyExtensions = true } = options
    1168            6 :     const failure = markGraphLoadFailurePresented(error)
    1169            6 :     useDialogService().showErrorDialog(failure, {
    1170              :       title: t('errorDialog.loadWorkflowTitle'),
    1171              :       reportType: 'loadWorkflowError'
    1172              :     })
    1173            6 :     console.error(failure)
    1174            6 :     if (notifyExtensions)
    1175            6 :       await useExtensionService().invokeExtensionsAsync(
    1176              :         'onGraphLoadError',
    1177              :         failure
    1178              :       )
    1179            6 :     return failure
    1180              :   }
    1181              : 
    1182              :   private enqueueGraphLoad<T>(operation: () => Promise<T>): Promise<T> {
    1183         3903 :     const result = this.graphLoadQueue.then(operation)
    1184         3903 :     this.graphLoadQueue = result.then(
    1185              :       () => undefined,
    1186              :       () => undefined
    1187              :     )
    1188         3903 :     return result
    1189              :   }
    1190              : 
    1191              :   async loadGraphData(
    1192              :     graphData?: ComfyWorkflowJSON,
    1193              :     clean: boolean = true,
    1194              :     restore_view: boolean = true,
    1195              :     workflow: string | null | ComfyWorkflow = null,
    1196              :     options: LoadGraphDataOptions = {}
    1197              :   ): Promise<LoadedComfyWorkflow | boolean | undefined> {
    1198         3887 :     return this.enqueueGraphLoad(async () =>
    1199              :       options.isCurrent?.() === false
    1200              :         ? false
    1201              :         : this.loadGraphDataNow(
    1202              :             graphData,
    1203              :             clean,
    1204              :             restore_view,
    1205              :             workflow,
    1206              :             options
    1207              :           )
    1208              :     )
    1209              :   }
    1210              : 
    1211              :   private async loadGraphDataNow(
    1212              :     graphData: ComfyWorkflowJSON | undefined,
    1213              :     clean: boolean,
    1214              :     restore_view: boolean,
    1215              :     workflow: string | null | ComfyWorkflow,
    1216              :     options: LoadGraphDataOptions
    1217              :   ): Promise<LoadedComfyWorkflow | boolean | undefined> {
    1218         3887 :     const canvasScheduler = useCanvasScheduler()
    1219         3887 :     const loadId = ++this.graphLoadSequence
    1220              : 
    1221         3887 :     const {
    1222              :       checkForRerouteMigration = false,
    1223              :       openSource,
    1224              :       shareId,
    1225              :       deferWarnings = false,
    1226              :       skipAssetScans = false,
    1227              :       silentAssetErrors = false,
    1228              :       workflowNavigationId,
    1229              :       isCurrent
    1230              :     } = options
    1231         3887 :     await useExtensionService().invokeExtensionsAsync('beforeLoadGraph')
    1232         3887 :     let reset_invalid_values = false
    1233         3887 :     const missingNodeTypes: MissingNodeType[] = []
    1234         3887 :     try {
    1235              :       const replaceCanvas = useWorkflowService().beforeLoadNewGraph(clean)
    1236         3887 :       replaceCanvas()
    1237         3887 :       ChangeTracker.isLoadingGraph = true
    1238         3887 :       useExecutionErrorStore().setActiveGraph(null)
    1239              : 
    1240         3887 :       if (skipAssetScans) {
    1241              :         // Only reset candidates; preserve UI state (fileSizes, etc.)
    1242              :         // so cached results restored by showPendingWarnings still display sizes.
    1243              :         // Abort any in-flight verification from the outgoing workflow so a late
    1244              :         // result cannot repopulate the store after we've switched workflows.
    1245           96 :         useMissingModelStore().createVerificationAbortController().abort()
    1246           96 :         useMissingMediaStore().createVerificationAbortController().abort()
    1247           96 :         useMissingModelStore().setMissingModels([])
    1248           96 :         useMissingMediaStore().setMissingMedia([])
    1249              :       } else {
    1250         3791 :         useMissingModelStore().clearMissingModels()
    1251         3791 :         useMissingMediaStore().clearMissingMedia()
    1252              :       }
    1253              : 
    1254         3887 :       if (clean) {
    1255              :         // Reset canvas context before configuring a new graph so subgraph UI
    1256              :         // state from the previous workflow cannot leak into the newly loaded
    1257              :         // one, and so `clean()` can clear the root graph even when the user is
    1258              :         // currently inside a subgraph.
    1259         3811 :         this.canvas.setGraph(this.rootGraph)
    1260              : 
    1261         3811 :         withGraphIntentSource('load', () => this.clean())
    1262              :       }
    1263              : 
    1264              :       // Use explicit validation instead of falsy check to avoid replacing
    1265              :       // valid but falsy values (empty objects, 0, false, etc.)
    1266         3887 :       if (
    1267              :         !graphData ||
    1268              :         typeof graphData !== 'object' ||
    1269              :         Array.isArray(graphData)
    1270              :       ) {
    1271         2253 :         graphData = getDefaultWorkflow()
    1272         2253 :         reset_invalid_values = true
    1273              :       }
    1274              : 
    1275         3887 :       graphData = clone(graphData)
    1276              : 
    1277         3887 :       if (useSettingStore().get('Comfy.Validation.Workflows')) {
    1278            1 :         const { graphData: validatedGraphData } =
    1279              :           await useWorkflowValidation().validateWorkflow(graphData)
    1280              : 
    1281              :         // If the validation failed, use the original graph data.
    1282              :         // Ideally we should not block users from loading the workflow.
    1283            1 :         graphData = validatedGraphData ?? graphData
    1284              :       }
    1285              :       // Only show the reroute migration warning if the workflow does not have native
    1286              :       // reroutes. Merging reroute network has great complexity, and it is not supported
    1287              :       // for now.
    1288              :       // See: https://github.com/Comfy-Org/ComfyUI_frontend/issues/3317
    1289         3887 :       if (
    1290              :         checkForRerouteMigration &&
    1291              :         graphData.version === 0.4 &&
    1292              :         findLegacyRerouteNodes(graphData).length &&
    1293              :         noNativeReroutes(graphData)
    1294              :       ) {
    1295            0 :         const toast = useToast()
    1296            0 :         const migrationToastId = toast.warning(
    1297              :           t('toastMessages.migrateToLitegraphReroute'),
    1298              :           {
    1299              :             action: {
    1300              :               label: t('g.migrate'),
    1301              :               onClick: async () => {
    1302            0 :                 await this.loadGraphData(
    1303              :                   migrateLegacyRerouteNodes(
    1304              :                     this.rootGraph.serialize() as unknown as WorkflowJSON04
    1305              :                   ),
    1306              :                   false,
    1307              :                   false,
    1308              :                   useWorkflowStore().activeWorkflow
    1309              :                 )
    1310            0 :                 toast.dismiss(migrationToastId)
    1311              :               }
    1312              :             }
    1313              :           }
    1314              :         )
    1315              :       }
    1316              : 
    1317         3887 :       useSubgraphService().loadSubgraphs(graphData)
    1318              : 
    1319         3887 :       await useExtensionService().invokeExtensionsAsync(
    1320              :         'beforeConfigureGraph',
    1321              :         graphData,
    1322              :         missingNodeTypes
    1323              :       )
    1324              : 
    1325         3887 :       const nodeReplacementStore = useNodeReplacementStore()
    1326         3887 :       await nodeReplacementStore.load()
    1327              : 
    1328              :       // Collect missing node types from all nodes (root + subgraphs)
    1329         3887 :       const collectMissingNodes = (
    1330              :         nodes: readonly FlattenableWorkflowNode[],
    1331              :         pathPrefix: string = '',
    1332              :         displayName: string = ''
    1333              :       ) => {
    1334         4258 :         if (!Array.isArray(nodes)) {
    1335            0 :           console.warn(
    1336              :             'Workflow nodes data is missing or invalid, skipping node processing',
    1337              :             { nodes, pathPrefix }
    1338              :           )
    1339            0 :           return
    1340              :         }
    1341         4258 :         for (const n of nodes) {
    1342        22030 :           if (!(n.type in LiteGraph.registered_node_types)) {
    1343              :             // Always sanitize so configure() can handle unregistered types,
    1344              :             // but only report as missing if the node is active.
    1345              :             const isMuted =
    1346              :               n.mode === LGraphEventMode.NEVER ||
    1347              :               n.mode === LGraphEventMode.BYPASS
    1348           96 :             if (!isMuted) {
    1349           95 :               const replacement = nodeReplacementStore.getReplacementFor(n.type)
    1350           95 :               const cnrId = getCnrIdFromProperties(
    1351              :                 n.properties as Record<string, unknown> | undefined
    1352              :               )
    1353           95 :               const executionId = pathPrefix
    1354              :                 ? `${pathPrefix}:${n.id}`
    1355              :                 : String(n.id)
    1356              : 
    1357           95 :               missingNodeTypes.push({
    1358              :                 type: n.type,
    1359              :                 nodeId: executionId,
    1360              :                 cnrId,
    1361              :                 ...(displayName && {
    1362              :                   hint: t('g.inSubgraph', { name: displayName })
    1363              :                 }),
    1364              :                 isReplaceable: replacement !== null,
    1365              :                 replacement: replacement ?? undefined
    1366              :               })
    1367              :             }
    1368              : 
    1369           96 :             n.type = sanitizeNodeName(n.type)
    1370              :           }
    1371              :         }
    1372              :       }
    1373              : 
    1374         3887 :       collectMissingNodes(graphData.nodes)
    1375         3887 :       const subgraphDefs = collectSubgraphDefinitions(
    1376              :         parseFlattenableSubgraphDefinitions(
    1377              :           graphData.definitions?.subgraphs ?? []
    1378              :         )
    1379              :       )
    1380         3887 :       const subgraphContainerIdMap = buildSubgraphExecutionPaths(
    1381              :         graphData.nodes,
    1382              :         subgraphDefs
    1383              :       )
    1384         3887 :       for (const subgraph of subgraphDefs) {
    1385          356 :         const paths = subgraphContainerIdMap.get(subgraph.id) ?? []
    1386          356 :         for (const pathPrefix of paths) {
    1387          371 :           collectMissingNodes(
    1388              :             subgraph.nodes,
    1389              :             pathPrefix,
    1390              :             subgraph.name || subgraph.id
    1391              :           )
    1392              :         }
    1393              :       }
    1394              :     } catch (error) {
    1395              :       // This try wraps everything between `beforeLoadGraph` and
    1396              :       // `rootGraph.configure`: asset-scan resets, `clean()`, workflow
    1397              :       // cloning, `validateWorkflow`, reroute-migration inspection, a
    1398              :       // malformed subgraph definition, a `beforeConfigureGraph` extension
    1399              :       // hook throwing, or a node-replacement load failure. Any of those
    1400              :       // happens before `rootGraph.configure` and so reaches neither the
    1401              :       // `afterConfigureGraph` success path nor the `onGraphLoadError` catch
    1402              :       // below. Left unhandled, that would both reject silently and leak any
    1403              :       // suppression/loading-state a `beforeLoadGraph` listener opened for
    1404              :       // this load, since nothing ever notifies it the load ended.
    1405            0 :       try {
    1406            0 :         await this.reportGraphLoadFailure(error)
    1407              :       } finally {
    1408            0 :         ChangeTracker.isLoadingGraph = false
    1409              :       }
    1410            0 :       void useSubgraphNavigationStore().updateHash(
    1411              :         'workflow-load',
    1412              :         workflowNavigationId
    1413              :       )
    1414            0 :       return false
    1415              :     }
    1416              : 
    1417         3887 :     const cancelReplacedLoad = () =>
    1418              :       useExtensionService().invokeExtensionsAsync(
    1419              :         'onGraphLoadError',
    1420              :         new DOMException('Workflow load was replaced', 'AbortError')
    1421              :       )
    1422         3887 :     if (isCurrent?.() === false) {
    1423            0 :       ChangeTracker.isLoadingGraph = false
    1424            0 :       await cancelReplacedLoad()
    1425            0 :       void useSubgraphNavigationStore().updateHash(
    1426              :         'workflow-load',
    1427              :         workflowNavigationId
    1428              :       )
    1429            0 :       return false
    1430              :     }
    1431              : 
    1432         3887 :     const fitView = () => {
    1433         3805 :       const savedCameraState = normalizeCameraState(graphData.extra?.ds, {
    1434              :         minScale: this.canvas.ds.min_scale,
    1435              :         maxScale: this.canvas.ds.max_scale
    1436              :       })
    1437         3805 :       if (
    1438              :         restore_view &&
    1439              :         useSettingStore().get('Comfy.EnableWorkflowViewRestore')
    1440              :       ) {
    1441              :         // Always fit view for templates to ensure they're visible on load
    1442         3804 :         if (openSource === 'template') {
    1443           11 :           useLitegraphService().fitView()
    1444         3793 :         } else if (savedCameraState) {
    1445         3589 :           this.canvas.ds.offset = savedCameraState.offset
    1446         3589 :           this.canvas.ds.scale = savedCameraState.scale
    1447              : 
    1448              :           // Fit view if no nodes visible in restored viewport
    1449         3589 :           this.canvas.ds.computeVisibleArea(this.canvas.viewport)
    1450         3589 :           if (
    1451              :             this.canvas.visible_area.width &&
    1452              :             this.canvas.visible_area.height &&
    1453              :             !anyItemOverlapsRect(
    1454              :               this.rootGraph._nodes,
    1455              :               this.canvas.visible_area
    1456              :             )
    1457              :           ) {
    1458           25 :             useLitegraphService().fitView()
    1459              :           }
    1460              :         } else {
    1461          204 :           useLitegraphService().fitView()
    1462              :         }
    1463              :       }
    1464              :     }
    1465              : 
    1466         3887 :     let activatedWorkflow: LoadedComfyWorkflow | undefined
    1467         3887 :     let reconcileResourceErrors: (() => void) | undefined
    1468         3887 :     let resourceScanLoadCompleted = false
    1469         3887 :     try {
    1470         3887 :       try {
    1471         3887 :         this.rootGraph.configure(graphData as ISerialisedGraph)
    1472              : 
    1473              :         // Save original renderer version before scaling (it gets modified during scaling)
    1474         3887 :         const originalMainGraphRenderer =
    1475              :           this.rootGraph.extra.workflowRendererVersion
    1476              : 
    1477              :         // Scale main graph
    1478         3887 :         ensureCorrectLayoutScale(originalMainGraphRenderer, this.rootGraph)
    1479              : 
    1480              :         // Scale all subgraphs that were loaded with the workflow
    1481              :         // Use original main graph renderer as fallback (not the modified one)
    1482         3887 :         for (const subgraph of this.rootGraph.subgraphs.values()) {
    1483          356 :           ensureCorrectLayoutScale(
    1484              :             subgraph.extra.workflowRendererVersion || originalMainGraphRenderer,
    1485              :             subgraph
    1486              :           )
    1487              :         }
    1488              : 
    1489              :         const preservesPendingCamera =
    1490              :           !restore_view &&
    1491              :           workflow !== null &&
    1492              :           workflow === this.pendingCamera?.workflow
    1493         3887 :         if (!preservesPendingCamera) {
    1494         3881 :           canvasScheduler.cancel('graph-load-camera')
    1495         3881 :           this.pendingCamera = restore_view
    1496              :             ? { id: loadId, workflow }
    1497              :             : undefined
    1498         3881 :           if (restore_view) {
    1499         3805 :             canvasScheduler.schedule({
    1500              :               key: 'graph-load-camera',
    1501              :               element: this.canvasEl,
    1502              :               isCurrent: () => this.pendingCamera?.id === loadId,
    1503              :               run: () => {
    1504         3805 :                 const viewport = measureViewportFromElement(this.canvasEl)
    1505         3805 :                 applyViewport(
    1506              :                   viewport,
    1507              :                   this.canvasEl,
    1508              :                   this.canvas.bgcanvas,
    1509              :                   this.canvas
    1510              :                 )
    1511         3805 :                 fitView()
    1512         3805 :                 this.canvas.draw(true, true)
    1513         3805 :                 if (this.pendingCamera?.id === loadId) {
    1514         3805 :                   this.pendingCamera = undefined
    1515              :                 }
    1516              :               }
    1517              :             })
    1518              :           }
    1519              :         }
    1520              :       } catch (error) {
    1521            6 :         await this.reportGraphLoadFailure(error)
    1522              :         // Resolves rather than throws: the close/replacement guards read this outcome.
    1523            6 :         return false
    1524              :       }
    1525         3881 :       const configuredGraphState = this.rootGraph.state
    1526         3881 :       const isCurrentGraphLoad = () =>
    1527              :         configuredGraphState === this.rootGraph.state
    1528         3887 :       const snapTo = LiteGraph.alwaysSnapToGrid
    1529              :         ? this.rootGraph.getSnapToGridSize()
    1530              :         : 0
    1531         3887 :       withGraphIntentSource('load', () =>
    1532              :         forEachNode(this.rootGraph, (node) => {
    1533        21988 :           const size = node.computeSize()
    1534        21988 :           size[0] = Math.max(node.size[0], size[0])
    1535        21988 :           size[1] = Math.max(node.size[1], size[1])
    1536        21988 :           snapPoint(size, snapTo, 'ceil')
    1537        21988 :           node.setSize(size)
    1538        21988 :           if (node.widgets) {
    1539              :             // If you break something in the backend and want to patch workflows in the frontend
    1540              :             // This is the place to do this
    1541        21959 :             for (const widget of node.widgets) {
    1542        45555 :               if (node.type == 'KSampler' || node.type == 'KSamplerAdvanced') {
    1543        21979 :                 if (widget.name == 'sampler_name') {
    1544              :                   if (
    1545         3134 :                     typeof widget.value === 'string' &&
    1546              :                     widget.value.startsWith('sample_')
    1547              :                   ) {
    1548            0 :                     widget.value = widget.value.slice(7)
    1549              :                   }
    1550              :                 }
    1551              :               }
    1552        45555 :               if (
    1553              :                 node.type == 'KSampler' ||
    1554              :                 node.type == 'KSamplerAdvanced' ||
    1555              :                 node.type == 'PrimitiveNode'
    1556              :               ) {
    1557        22097 :                 if (widget.name == 'control_after_generate') {
    1558         3171 :                   if (widget.value === true) {
    1559         2509 :                     widget.value = 'randomize'
    1560          662 :                   } else if (widget.value === false) {
    1561            0 :                     widget.value = 'fixed'
    1562              :                   }
    1563              :                 }
    1564              :               }
    1565        45555 :               if (widget.type == 'combo') {
    1566        13012 :                 const values = widget.options.values as
    1567              :                   | (string | number | boolean)[]
    1568              :                   | undefined
    1569        13012 :                 if (
    1570              :                   values &&
    1571              :                   values.length > 0 &&
    1572              :                   (widget.value == null ||
    1573              :                     (reset_invalid_values &&
    1574              :                       !values.includes(
    1575              :                         widget.value as string | number | boolean
    1576              :                       )))
    1577              :                 ) {
    1578            7 :                   widget.value = values[0]
    1579              :                 }
    1580              :               }
    1581              :             }
    1582              :           }
    1583              : 
    1584        21988 :           useExtensionService().invokeExtensions('loadedGraphNode', node)
    1585              :         })
    1586              :       )
    1587              : 
    1588         3887 :       await withGraphIntentSource('load', () =>
    1589              :         useExtensionService().invokeExtensionsAsync(
    1590              :           'afterConfigureGraph',
    1591              :           missingNodeTypes
    1592              :         )
    1593              :       )
    1594         3887 :       if (!isCurrentGraphLoad()) {
    1595            0 :         await cancelReplacedLoad()
    1596            0 :         return undefined
    1597              :       }
    1598         3887 :       if (isCurrent?.() === false) {
    1599            0 :         await cancelReplacedLoad()
    1600            0 :         return false
    1601              :       }
    1602              : 
    1603         3887 :       const effectiveShareId =
    1604              :         shareId ??
    1605              :         (workflow instanceof ComfyWorkflow ? workflow.shareId : undefined)
    1606         3887 :       const telemetryPayload = {
    1607              :         missing_node_count: missingNodeTypes.length,
    1608              :         missing_node_types: missingNodeTypes.map((node) =>
    1609              :           typeof node === 'string' ? node : node.type
    1610              :         ),
    1611              :         missing_node_packs: groupMissingNodesByPack(missingNodeTypes),
    1612              :         open_source: openSource ?? 'unknown',
    1613              :         ...(effectiveShareId ? { share_id: effectiveShareId } : {})
    1614              :       }
    1615         3887 :       useTelemetry()?.trackWorkflowOpened(telemetryPayload)
    1616         3887 :       useTelemetry()?.trackWorkflowImported(telemetryPayload)
    1617         3887 :       try {
    1618         3887 :         await useWorkflowService().afterLoadNewGraph(
    1619              :           workflow,
    1620              :           this.rootGraph.serialize() as unknown as ComfyWorkflowJSON,
    1621              :           effectiveShareId
    1622              :         )
    1623              :       } catch (error) {
    1624            0 :         if (isCurrentGraphLoad()) throw error
    1625              :       }
    1626         3887 :       if (!isCurrentGraphLoad()) {
    1627            0 :         await cancelReplacedLoad()
    1628            0 :         return undefined
    1629              :       }
    1630         3881 :       await useExtensionService().invokeExtensionsAsync('afterLoadGraph')
    1631         3887 :       if (!isCurrentGraphLoad()) {
    1632            0 :         await cancelReplacedLoad()
    1633            0 :         return undefined
    1634              :       }
    1635         3881 :       ChangeTracker.isLoadingGraph = false
    1636              :       // Capture the workflow this load activated before the asset-scan awaits
    1637              :       // below can hand control back and let the user switch to another one.
    1638         3887 :       activatedWorkflow = useWorkflowStore().activeWorkflow ?? undefined
    1639              : 
    1640              :       // Drop missing-node entries whose enclosing subgraph is
    1641              :       // muted/bypassed. The initial JSON scan only checks each node's
    1642              :       // own mode; the cascade from an inactive container is applied here
    1643              :       // using the now-configured live graph.
    1644         3887 :       const activeMissingNodeTypes = missingNodeTypes.filter(
    1645              :         (n) =>
    1646              :           typeof n === 'string' ||
    1647              :           n.nodeId == null ||
    1648              :           isAncestorPathActive(this.rootGraph, String(n.nodeId))
    1649              :       )
    1650              : 
    1651         3887 :       if (!skipAssetScans) {
    1652         3785 :         const errorStore = useExecutionErrorStore()
    1653         3785 :         const resourceScanKey = errorStore.captureRunErrorKey()
    1654         3785 :         let models: MissingModelCandidate[] | undefined
    1655         3785 :         let media: MissingMediaCandidate[] | undefined
    1656         3785 :         const reconcile = () => {
    1657        11295 :           if (!resourceScanLoadCompleted || (!models && !media)) return
    1658         6473 :           errorStore.retireResolvedMissingResourceErrors(
    1659              :             { models, media },
    1660              :             resourceScanKey
    1661              :           )
    1662              :         }
    1663         3785 :         reconcileResourceErrors = reconcile
    1664         3785 :         await runMissingModelPipeline({
    1665              :           graph: this.rootGraph,
    1666              :           graphData,
    1667              :           missingModelStore: useMissingModelStore(),
    1668              :           missingNodeTypes: activeMissingNodeTypes,
    1669              :           silent: silentAssetErrors,
    1670              :           onVerified: (candidates) => {
    1671         3725 :             models = candidates
    1672         3725 :             reconcile()
    1673              :           }
    1674              :         })
    1675              : 
    1676         3785 :         await runMissingMediaPipeline({
    1677              :           rootGraph: this.rootGraph,
    1678              :           silent: silentAssetErrors,
    1679              :           onVerified: (candidates) => {
    1680         3785 :             media = candidates
    1681         3785 :             reconcile()
    1682              :           }
    1683              :         })
    1684              :       }
    1685              : 
    1686         3881 :       if (!deferWarnings) {
    1687         3675 :         useWorkflowService().showPendingWarnings(undefined, {
    1688              :           silent: silentAssetErrors
    1689              :         })
    1690              :       }
    1691         3881 :       resourceScanLoadCompleted = true
    1692              : 
    1693         3881 :       requestAnimationFrame(() => {
    1694         3881 :         this.canvas.setDirty(true, true)
    1695              :       })
    1696         3887 :       return activatedWorkflow ?? true
    1697              :     } finally {
    1698              :       // Finally: a throwing load still repairs the URL.
    1699         3887 :       void useSubgraphNavigationStore().updateHash(
    1700              :         'workflow-load',
    1701              :         workflowNavigationId
    1702              :       )
    1703         3887 :       ChangeTracker.isLoadingGraph = false
    1704              :       // The retirement watcher skips transitions made during the load.
    1705         3887 :       useExecutionErrorStore().retireResolvedMissingNodePromptError()
    1706         3887 :       reconcileResourceErrors?.()
    1707              :     }
    1708              :   }
    1709              : 
    1710              :   async refreshMissingModels(
    1711              :     options: { silent?: boolean; reloadDefs?: boolean } = {}
    1712              :   ): Promise<MissingModelPipelineResult> {
    1713           11 :     return refreshMissingModelPipeline({
    1714              :       graph: this.rootGraph,
    1715              :       reloadNodeDefs:
    1716              :         options.reloadDefs === false
    1717              :           ? undefined
    1718              :           : async () => {
    1719            2 :               await this.reloadNodeDefs()
    1720              :             },
    1721              :       missingModelStore: useMissingModelStore(),
    1722              :       silent: options.silent ?? true
    1723              :     })
    1724              :   }
    1725              : 
    1726              :   async graphToPrompt(graph = this.rootGraph) {
    1727          275 :     return graphToPrompt(graph, {
    1728              :       sortNodes: useSettingStore().get('Comfy.Workflow.SortNodeIdOnSave')
    1729              :     })
    1730              :   }
    1731              : 
    1732              :   async queuePrompt(
    1733              :     number: number,
    1734              :     batchCount?: number,
    1735              :     options?: QueuePromptOptions
    1736              :   ): Promise<boolean>
    1737              :   async queuePrompt(
    1738              :     number: number,
    1739              :     batchCount: number,
    1740              :     queueNodeIds: NodeExecutionId[]
    1741              :   ): Promise<boolean>
    1742              :   async queuePrompt(
    1743              :     number: number,
    1744              :     batchCount: number = 1,
    1745              :     optionsOrQueueNodeIds: QueuePromptOptions | NodeExecutionId[] = {}
    1746              :   ): Promise<boolean> {
    1747              :     const options = Array.isArray(optionsOrQueueNodeIds)
    1748              :       ? { queueNodeIds: optionsOrQueueNodeIds }
    1749              :       : optionsOrQueueNodeIds
    1750              :     const { queueNodeIds, intent } = options
    1751          193 :     if (
    1752              :       intent?.trigger_source === 'auto_queue' &&
    1753              :       partnerRunGateBlocksAutoQueue()
    1754              :     ) {
    1755            0 :       return false
    1756              :     }
    1757          193 :     const requestId = this.nextQueueRequestId++
    1758          193 :     this.queueItems.push({
    1759              :       number,
    1760              :       batchCount,
    1761              :       queueNodeIds,
    1762              :       requestId,
    1763              :       workflowQueueIntent: intent
    1764              :     })
    1765          193 :     api.dispatchCustomEvent('promptQueueing', {
    1766              :       requestId,
    1767              :       batchCount
    1768              :     })
    1769              : 
    1770          193 :     if (this.processingQueue) {
    1771            0 :       return false
    1772              :     }
    1773              : 
    1774          193 :     this.processingQueue = true
    1775          193 :     const executionStore = useExecutionStore()
    1776          193 :     const executionErrorStore = useExecutionErrorStore()
    1777          193 :     const telemetry = useTelemetry()
    1778          193 :     executionErrorStore.clearRunErrors()
    1779          193 :     let queueResultOverride: boolean | null = null
    1780              : 
    1781          193 :     const teamWorkspaceStore = useTeamWorkspaceStore()
    1782          193 :     try {
    1783          193 :       await teamWorkspaceStore.waitForWorkspaceSwitch()
    1784              :     } catch (error) {
    1785            0 :       useDialogService().showErrorDialog(error, {
    1786              :         title: t('errorDialog.promptExecutionError'),
    1787              :         reportType: 'promptExecutionError'
    1788              :       })
    1789            0 :       this.queueItems.length = 0
    1790            0 :       this.processingQueue = false
    1791            0 :       return false
    1792              :     }
    1793          193 :     const workspaceIdBeforeAuthentication = teamWorkspaceStore.activeWorkspaceId
    1794          193 :     const workspaceGenerationBeforeAuthentication =
    1795              :       teamWorkspaceStore.workspaceTransitionGeneration
    1796          193 :     const workspaceAuthToken = await useAuthStore().getWorkspaceAuthToken()
    1797          193 :     const executionWorkspaceId = teamWorkspaceStore.activeWorkspaceId
    1798          193 :     const executionWorkspaceGeneration =
    1799              :       teamWorkspaceStore.workspaceTransitionGeneration
    1800          193 :     const comfyOrgAuthToken = await queueAuthToken(
    1801              :       workspaceAuthToken,
    1802              :       executionWorkspaceId
    1803              :     )
    1804          193 :     const workspaceChangedWhileAuthenticating =
    1805              :       (workspaceIdBeforeAuthentication !== executionWorkspaceId ||
    1806              :         workspaceGenerationBeforeAuthentication !==
    1807              :           executionWorkspaceGeneration) &&
    1808              :       (isCloud || workspaceIdBeforeAuthentication !== null)
    1809          193 :     const desktopHostAuth = isDesktopHostSignedIn()
    1810          193 :     const comfyOrgApiKey = desktopHostAuth
    1811              :       ? null
    1812              :       : useApiKeyAuthStore().getApiKey()
    1813          193 :     const isApiKeySessionExecution =
    1814              :       !desktopHostAuth &&
    1815              :       !useAuthStore().currentUser &&
    1816              :       !useAuthStore().sessionOnlyUser &&
    1817              :       useApiKeyAuthStore().isAuthenticated
    1818          193 :     if (
    1819              :       executionWorkspaceId &&
    1820              :       !comfyOrgAuthToken &&
    1821              :       !isApiKeySessionExecution
    1822              :     ) {
    1823            0 :       useDialogService().showErrorDialog(
    1824              :         new Error(t('toastMessages.userNotAuthenticated')),
    1825              :         {
    1826              :           title: t('errorDialog.promptExecutionError'),
    1827              :           reportType: 'promptExecutionError'
    1828              :         }
    1829              :       )
    1830            0 :       this.queueItems.length = 0
    1831            0 :       this.processingQueue = false
    1832            0 :       return false
    1833              :     }
    1834              : 
    1835          193 :     try {
    1836          193 :       while (this.queueItems.length) {
    1837          193 :         const {
    1838              :           number,
    1839              :           batchCount,
    1840              :           queueNodeIds,
    1841              :           requestId,
    1842              :           workflowQueueIntent
    1843              :         } = this.queueItems.pop()!
    1844          193 :         let queuedCount = 0
    1845          193 :         const workflowExecutionIntent: WorkflowExecutionIntent = {
    1846              :           trigger_source: normalizeExecutionTriggerSource(
    1847              :             workflowQueueIntent?.trigger_source
    1848              :           )
    1849              :         }
    1850          193 :         const previewMethod = useSettingStore().get(
    1851              :           'Comfy.Execution.PreviewMethod'
    1852              :         )
    1853              : 
    1854          193 :         const isPartialExecution = !!queueNodeIds?.length
    1855          193 :         for (let i = 0; i < batchCount; i++) {
    1856          193 :           let executionContext: ExecutionContext | undefined
    1857          193 :           if (telemetry) {
    1858            0 :             try {
    1859            0 :               executionContext = getExecutionContext()
    1860              :             } catch (error) {
    1861            0 :               console.error(
    1862              :                 '[Telemetry] Workflow context collection failed',
    1863              :                 error
    1864              :               )
    1865              :             }
    1866              :           }
    1867              : 
    1868          193 :           forEachNode(this.rootGraph, (node) => {
    1869          761 :             for (const widget of node.widgets ?? []) {
    1870         1857 :               widget.beforeQueued?.({ isPartialExecution })
    1871              :             }
    1872          761 :             applyPromotedWidgetControl(node, 'beforeQueued')
    1873              :           })
    1874              : 
    1875          193 :           const queuedWorkflow = useWorkspaceStore().workflow
    1876              :             .activeWorkflow as ComfyWorkflow
    1877          193 :           const queuedRunErrorKey = executionErrorStore.captureRunErrorKey()
    1878          193 :           const queuedMode = getWorkflowMode(queuedWorkflow)
    1879          193 :           const startTime = performance.now()
    1880          193 :           const p = await this.graphToPrompt(this.rootGraph).catch(
    1881              :             (error: unknown) => {
    1882            2 :               telemetry?.trackExecutionOutcome({
    1883              :                 startTime,
    1884              :                 endTime: performance.now(),
    1885              :                 success: false,
    1886              :                 failureReason: 'prompt_build_failed',
    1887              :                 ...workflowExecutionIntent
    1888              :               })
    1889            2 :               throw error
    1890              :             }
    1891              :           )
    1892          191 :           const queuedNodes = collectAllNodes(this.rootGraph)
    1893          191 :           let workflowContext: WorkflowExecutionContext | undefined
    1894          191 :           if (executionContext) {
    1895            0 :             workflowContext = toWorkflowExecutionContext(executionContext, {
    1896              :               executableNodeCount: Object.keys(p.output).length,
    1897              :               executionScope: isPartialExecution ? 'partial' : 'full',
    1898              :               viewMode: queuedMode
    1899              :             })
    1900              :           }
    1901          193 :           if (
    1902              :             workspaceChangedWhileAuthenticating ||
    1903              :             executionWorkspaceId !== teamWorkspaceStore.activeWorkspaceId ||
    1904              :             executionWorkspaceGeneration !==
    1905              :               teamWorkspaceStore.workspaceTransitionGeneration
    1906              :           ) {
    1907            0 :             useDialogService().showErrorDialog(
    1908              :               new Error(t('errorDialog.workspaceChangedDuringExecution')),
    1909              :               {
    1910              :                 title: t('errorDialog.promptExecutionError'),
    1911              :                 reportType: 'promptExecutionError'
    1912              :               }
    1913              :             )
    1914            0 :             queueResultOverride = false
    1915            0 :             break
    1916              :           }
    1917          193 :           try {
    1918          191 :             api.authToken = comfyOrgAuthToken
    1919          193 :             api.apiKey = comfyOrgApiKey ?? undefined
    1920          193 :             executionErrorStore.recordNodeErrors(null, queuedRunErrorKey)
    1921          193 :             const res = await api.queuePrompt(number, p, {
    1922              :               partialExecutionTargets: queueNodeIds,
    1923              :               previewMethod
    1924              :             })
    1925          150 :             const responseReceivedAt = performance.now()
    1926          150 :             delete api.authToken
    1927          150 :             delete api.apiKey
    1928          151 :             if (!res.prompt_id) {
    1929            0 :               telemetry?.trackExecutionOutcome({
    1930              :                 startTime,
    1931              :                 endTime: responseReceivedAt,
    1932              :                 success: false,
    1933              :                 failureReason: 'submission_rejected',
    1934              :                 ...workflowExecutionIntent,
    1935              :                 ...(workflowContext && { workflowContext })
    1936              :               })
    1937              :             }
    1938          150 :             executionErrorStore.recordSubmissionNodeErrors(
    1939              :               res.node_errors,
    1940              :               queuedRunErrorKey,
    1941              :               res.prompt_id
    1942              :             )
    1943          150 :             queueResultOverride = null
    1944          193 :             try {
    1945          150 :               if (res.prompt_id) {
    1946          150 :                 executionStore.storeJob({
    1947              :                   id: res.prompt_id,
    1948              :                   nodes: Object.keys(p.output),
    1949              :                   promptOutput: p.output,
    1950              :                   startTime,
    1951              :                   submissionAcceptedAt: responseReceivedAt,
    1952              :                   workflow: queuedWorkflow,
    1953              :                   mode: queuedMode,
    1954              :                   workflowContext,
    1955              :                   workflowExecutionIntent
    1956              :                 })
    1957              :               }
    1958              :             } catch (error) {
    1959            0 :               console.warn('Failed to store queued job metadata', {
    1960              :                 promptId: res.prompt_id,
    1961              :                 error
    1962              :               })
    1963              :             }
    1964          151 :             if (executionErrorStore.hasNodeError) {
    1965            1 :               if (useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')) {
    1966            1 :                 executionErrorStore.showErrorOverlay()
    1967              :               }
    1968            1 :               this.canvas.draw(true, true)
    1969              :             }
    1970              :           } catch (error: unknown) {
    1971           36 :             telemetry?.trackExecutionOutcome({
    1972              :               startTime,
    1973              :               endTime: performance.now(),
    1974              :               success: false,
    1975              :               failureReason:
    1976              :                 error instanceof PromptExecutionError
    1977              :                   ? 'submission_rejected'
    1978              :                   : 'submission_failed',
    1979              :               ...workflowExecutionIntent,
    1980              :               ...(workflowContext && { workflowContext })
    1981              :             })
    1982           36 :             if (error instanceof PromptExecutionError) {
    1983           35 :               queueResultOverride = !error.response.node_errors
    1984              :             }
    1985           36 :             const hasPromptNodeErrors =
    1986              :               error instanceof PromptExecutionError &&
    1987              :               Object.keys(error.response.node_errors ?? {}).length > 0
    1988           36 :             const promptError =
    1989              :               error instanceof PromptExecutionError
    1990              :                 ? normalizePromptError(error.response.error)
    1991              :                 : null
    1992           36 :             const promptPrecondition = promptError
    1993              :               ? resolveAccountPrecondition({
    1994              :                   exceptionType: promptError.type,
    1995              :                   exceptionMessage: promptError.message
    1996              :                 })
    1997              :               : undefined
    1998           36 :             if (promptPrecondition) {
    1999              :               const opened = await useAccountPreconditionDialog().open(
    2000              :                 promptPrecondition,
    2001              :                 {
    2002              :                   runRefused: true,
    2003              :                   creditsLocked: promptErrorHasLockedCredits(
    2004              :                     error instanceof PromptExecutionError
    2005              :                       ? error.response.error
    2006              :                       : null
    2007              :                   ),
    2008              :                   ...(promptError?.type &&
    2009              :                     promptError.type !== 'error' && {
    2010              :                       refusalType: promptError.type
    2011              :                     })
    2012              :                 }
    2013              :               )
    2014            1 :               if (opened) {
    2015            0 :                 console.error(error)
    2016            0 :                 break
    2017              :               }
    2018              :             }
    2019           36 :             if (
    2020              :               error instanceof PromptExecutionError &&
    2021              :               promptError?.type === 'missing_node_type'
    2022              :             ) {
    2023            1 :               rescanAndSurfaceMissingNodes(this.rootGraph)
    2024           35 :             } else if (
    2025              :               error instanceof PromptExecutionError &&
    2026              :               error.status === 403 &&
    2027              :               !hasPromptNodeErrors
    2028              :             ) {
    2029            0 :               const raw: Record<string, unknown> = error.response
    2030            0 :               const rawError =
    2031              :                 raw.error && typeof raw.error === 'object'
    2032              :                   ? (raw.error as Record<string, unknown>)
    2033              :                   : undefined
    2034            0 :               const detail =
    2035              :                 typeof raw.message === 'string'
    2036              :                   ? raw.message
    2037              :                   : typeof rawError?.message === 'string'
    2038              :                     ? rawError.message
    2039              :                     : typeof raw.error === 'string'
    2040              :                       ? raw.error
    2041              :                       : t('errorDialog.accessRestrictedMessage')
    2042            0 :               useDialogService().showErrorDialog(new Error(detail), {
    2043              :                 title: t('errorDialog.accessRestrictedTitle'),
    2044              :                 reportType: 'accessRestrictedError'
    2045              :               })
    2046           35 :             } else if (
    2047              :               !useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab') ||
    2048              :               !(error instanceof PromptExecutionError)
    2049              :             ) {
    2050           14 :               useDialogService().showErrorDialog(error, {
    2051              :                 title: t('errorDialog.promptExecutionError'),
    2052              :                 reportType: 'promptExecutionError'
    2053              :               })
    2054              :             }
    2055           36 :             console.error(error)
    2056              : 
    2057           36 :             if (error instanceof PromptExecutionError) {
    2058           35 :               const nodeErrors = error.response.node_errors
    2059           35 :               executionErrorStore.recordNodeErrors(
    2060              :                 nodeErrors ?? null,
    2061              :                 queuedRunErrorKey
    2062              :               )
    2063              : 
    2064           35 :               if (
    2065              :                 (!nodeErrors || Object.keys(nodeErrors).length === 0) &&
    2066              :                 promptError
    2067              :               ) {
    2068            7 :                 executionErrorStore.recordPromptError(
    2069              :                   promptError,
    2070              :                   queuedRunErrorKey
    2071              :                 )
    2072              :               }
    2073              : 
    2074           35 :               if (useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')) {
    2075           22 :                 executionErrorStore.showErrorOverlay()
    2076              :               }
    2077           35 :               this.canvas.draw(true, true)
    2078              :             }
    2079           36 :             break
    2080              :           }
    2081              : 
    2082          150 :           queuedCount++
    2083              : 
    2084          150 :           executeWidgetsCallback(queuedNodes, 'afterQueued', {
    2085              :             isPartialExecution
    2086              :           })
    2087          151 :           for (const node of queuedNodes) {
    2088          599 :             applyPromotedWidgetControl(node, 'afterQueued')
    2089              :           }
    2090          150 :           useFreeTierQuota().trackRun()
    2091              :         }
    2092              : 
    2093          187 :         if (queuedCount > 0) {
    2094          150 :           this.canvas.draw(true, true)
    2095          150 :           void this.ui.queue.update().catch((error) => {
    2096            0 :             reportError(error, {
    2097              :               surface: 'graph',
    2098              :               errorType: 'queue_ui_update_failure'
    2099              :             })
    2100              :           })
    2101          150 :           api.dispatchCustomEvent('promptQueued', {
    2102              :             number,
    2103              :             batchCount: queuedCount,
    2104              :             requestId
    2105              :           })
    2106              :         }
    2107              :       }
    2108              :     } finally {
    2109          188 :       this.processingQueue = false
    2110              :     }
    2111          193 :     return queueResultOverride ?? !executionErrorStore.lastNodeErrors
    2112              :   }
    2113              : 
    2114              :   showErrorOnFileLoad(file: File) {
    2115            0 :     useToast().warning(
    2116              :       t('toastMessages.fileLoadError', { fileName: file.name })
    2117              :     )
    2118              :   }
    2119              : 
    2120              :   /**
    2121              :    * Loads workflow data from the specified file
    2122              :    * @param {File} file
    2123              :    */
    2124              :   async handleFile(
    2125              :     file: File,
    2126              :     openSource?: WorkflowOpenSource,
    2127              :     options?: {
    2128              :       deferWarnings?: boolean
    2129              :       onNodeCreated?: (node: LGraphNode) => void
    2130              :     }
    2131              :   ) {
    2132         1071 :     const fromGraphLoadHook = isGraphLoadHookOrigin(options)
    2133         1071 :     const previousDiscovery = this.fileLoadDiscoveryQueue
    2134         1071 :     let releaseDiscovery!: () => void
    2135         1071 :     this.fileLoadDiscoveryQueue = new Promise<void>((resolve) => {
    2136         1071 :       releaseDiscovery = resolve
    2137              :     })
    2138         1071 :     let workflowData: Awaited<ReturnType<typeof getWorkflowDataFromFile>>
    2139         1071 :     try {
    2140         1071 :       workflowData = await getWorkflowDataFromFile(file)
    2141              :     } catch (error) {
    2142            0 :       await previousDiscovery
    2143            0 :       releaseDiscovery()
    2144            0 :       throw error
    2145              :     }
    2146         1071 :     await previousDiscovery
    2147         1071 :     try {
    2148         1071 :       return this.handleParsedFile(
    2149              :         file,
    2150              :         openSource,
    2151              :         options,
    2152              :         workflowData,
    2153              :         fromGraphLoadHook
    2154              :       )
    2155              :     } finally {
    2156         1071 :       releaseDiscovery()
    2157              :     }
    2158              :   }
    2159              : 
    2160              :   private async handleParsedFile(
    2161              :     file: File,
    2162              :     openSource: WorkflowOpenSource | undefined,
    2163              :     options:
    2164              :       | {
    2165              :           deferWarnings?: boolean
    2166              :           onNodeCreated?: (node: LGraphNode) => void
    2167              :         }
    2168              :       | undefined,
    2169              :     workflowData: Awaited<ReturnType<typeof getWorkflowDataFromFile>>,
    2170              :     fromGraphLoadHook: boolean
    2171              :   ) {
    2172         1071 :     const fileName = file.name.replace(/\.\w+$/, '') // Strip file extension
    2173         1071 :     const { workflow, prompt, parameters, templates } = workflowData ?? {}
    2174              : 
    2175         1071 :     if (fromGraphLoadHook && Boolean(workflow || prompt || parameters))
    2176            0 :       return false
    2177              : 
    2178         1071 :     if (!(workflow || prompt || parameters || templates)) {
    2179            5 :       const mediaNodeTypes: Record<string, [string, typeof pasteImageNode]> = {
    2180              :         image: ['LoadImage', pasteImageNode],
    2181              :         audio: ['LoadAudio', pasteAudioNode],
    2182              :         video: ['LoadVideo', pasteVideoNode]
    2183              :       }
    2184              : 
    2185            5 :       const mediaType = Object.keys(mediaNodeTypes).find((t) =>
    2186              :         file.type.startsWith(t)
    2187              :       )
    2188            5 :       if (mediaType) {
    2189            5 :         const [nodeType, pasteFn] = mediaNodeTypes[mediaType]
    2190            5 :         const transfer = new DataTransfer()
    2191            5 :         transfer.items.add(file)
    2192            5 :         const node = await createNode(this.canvas, nodeType)
    2193            5 :         await pasteFn(this.canvas, transfer.items, node)
    2194            5 :         if (node) options?.onNodeCreated?.(node)
    2195            5 :         return
    2196              :       }
    2197              : 
    2198            0 :       if (isMeshModelFile(file)) {
    2199            0 :         const node = await this.handleMeshFile(file)
    2200            0 :         if (node) options?.onNodeCreated?.(node)
    2201            0 :         return
    2202              :       }
    2203              : 
    2204            0 :       this.showErrorOnFileLoad(file)
    2205            0 :       return
    2206              :     }
    2207              : 
    2208         1071 :     if (
    2209              :       templates &&
    2210              :       typeof templates === 'object' &&
    2211              :       Array.isArray(templates)
    2212              :     ) {
    2213            0 :       this.loadTemplateData({
    2214              :         templates: templates as { name?: string; data?: string }[]
    2215              :       })
    2216              :     }
    2217              : 
    2218              :     // Check workflow first - it should take priority over parameters
    2219              :     // when both are present (e.g., in ComfyUI-generated PNGs)
    2220         1066 :     if (workflow) {
    2221         1052 :       try {
    2222         1052 :         const workflowObj: unknown =
    2223              :           typeof workflow === 'string'
    2224              :             ? parseJsonWithNonFinite(workflow)
    2225              :             : workflow
    2226              : 
    2227              :         // Only load workflow if parsing succeeded AND validation passed
    2228         1052 :         if (
    2229              :           workflowObj &&
    2230              :           typeof workflowObj === 'object' &&
    2231              :           !Array.isArray(workflowObj)
    2232              :         ) {
    2233         1052 :           const graphData = workflowObj as ComfyWorkflowJSON
    2234              :           const outcome = await this.loadGraphData(
    2235              :             graphData,
    2236              :             true,
    2237              :             true,
    2238              :             fileName,
    2239              :             {
    2240              :               openSource,
    2241              :               deferWarnings: options?.deferWarnings
    2242              :             }
    2243              :           )
    2244         1052 :           return outcome === false ? false : undefined
    2245              :         } else {
    2246            0 :           console.error(
    2247              :             'Invalid workflow structure, trying parameters fallback'
    2248              :           )
    2249              :         }
    2250              :       } catch (err) {
    2251            0 :         console.error('Failed to parse workflow:', err)
    2252              :         // Fall through to check parameters as fallback
    2253              :       }
    2254              :     }
    2255              : 
    2256           14 :     if (prompt) {
    2257           14 :       try {
    2258           14 :         const promptObj =
    2259              :           typeof prompt === 'string'
    2260              :             ? parseJsonWithNonFinite<ComfyApiWorkflow>(prompt)
    2261              :             : prompt
    2262           14 :         if (this.isApiJson(promptObj)) {
    2263           14 :           try {
    2264              :             const outcome = await this.loadApiJson(promptObj, fileName, {
    2265              :               deferWarnings: options?.deferWarnings
    2266              :             })
    2267           14 :             return outcome === false ? false : undefined
    2268              :           } catch (err) {
    2269            0 :             reportError(err, {
    2270              :               surface: 'graph',
    2271              :               errorType: 'api_prompt_load_failure'
    2272              :             })
    2273            0 :             if (!isGraphLoadFailurePresented(err))
    2274            0 :               this.showErrorOnFileLoad(file)
    2275              :           }
    2276            0 :           return
    2277              :         }
    2278              :       } catch (err) {
    2279            0 :         console.error('Failed to parse prompt:', err)
    2280              :       }
    2281              :       // Fall through to parameters as a last resort
    2282              :     }
    2283              : 
    2284              :     // Use parameters strictly as the final fallback
    2285         1071 :     if (parameters && typeof parameters === 'string') {
    2286            0 :       return await this.enqueueGraphLoad(async () => {
    2287            0 :         const lifecycle = { started: false }
    2288            0 :         let releaseCaptureSuppression: (() => void) | undefined
    2289            0 :         try {
    2290            0 :           const outcome = await importA1111(
    2291              :             this.rootGraph,
    2292              :             parameters,
    2293              :             async () => {
    2294            0 :               try {
    2295            0 :                 await useExtensionService().invokeExtensionsAsync(
    2296              :                   'beforeLoadGraph'
    2297              :                 )
    2298            0 :                 lifecycle.started = true
    2299              :                 const replaceCanvas =
    2300              :                   useWorkflowService().beforeLoadNewGraph(false)
    2301            0 :                 replaceCanvas()
    2302              :               } finally {
    2303            0 :                 useMissingNodesErrorStore().setMissingNodeTypes([])
    2304              :               }
    2305            0 :               releaseCaptureSuppression = beginChangeCaptureSuppression()
    2306            0 :               this.canvas.setGraph(this.rootGraph)
    2307              :             }
    2308              :           )
    2309            0 :           switch (outcome) {
    2310              :             case 'core-nodes-unavailable':
    2311            0 :               useToast().warning(t('toastMessages.a1111CoreNodesUnavailable'))
    2312            0 :               return
    2313              :             case 'not-a1111':
    2314            0 :               this.showErrorOnFileLoad(file)
    2315            0 :               return
    2316              :             case 'imported-without-embeddings':
    2317            0 :               useToast().warning(t('g.warning'), {
    2318              :                 description: t('toastMessages.a1111EmbeddingsUnavailable')
    2319              :               })
    2320            0 :               break
    2321              :             case 'imported':
    2322            0 :               break
    2323              :             default: {
    2324              :               const unexpectedOutcome: never = outcome
    2325            0 :               throw new Error(
    2326              :                 `Unhandled A1111 import outcome: ${unexpectedOutcome}`
    2327              :               )
    2328              :             }
    2329              :           }
    2330            0 :           await withGraphIntentSource('load', () =>
    2331              :             useExtensionService().invokeExtensionsAsync(
    2332              :               'afterConfigureGraph',
    2333              :               []
    2334              :             )
    2335              :           )
    2336            0 :           await useWorkflowService().afterLoadNewGraph(
    2337              :             fileName,
    2338              :             this.rootGraph.serialize() as unknown as ComfyWorkflowJSON
    2339              :           )
    2340            0 :           await useExtensionService().invokeExtensionsAsync('afterLoadGraph')
    2341            0 :           return
    2342              :         } catch (error) {
    2343            0 :           if (!lifecycle.started) throw error
    2344            0 :           throw await this.reportGraphLoadFailure(error)
    2345              :         } finally {
    2346            0 :           releaseCaptureSuppression?.()
    2347              :         }
    2348              :       })
    2349              :     }
    2350              : 
    2351            0 :     this.showErrorOnFileLoad(file)
    2352              :   }
    2353              : 
    2354              :   /**
    2355              :    * Uploads a mesh model file and creates a Load3DAdvanced node displaying it
    2356              :    * @param {File} file
    2357              :    */
    2358              :   private async handleMeshFile(file: File): Promise<LGraphNode | null> {
    2359            0 :     const uploadedPath = await Load3dUtils.uploadFile(file, '3d')
    2360            0 :     if (!uploadedPath) return null
    2361              : 
    2362            0 :     const node = await createNode(this.canvas, 'Load3DAdvanced')
    2363            0 :     if (!node) return null
    2364              : 
    2365            0 :     const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
    2366            0 :     if (!modelWidget) return node
    2367              : 
    2368            0 :     const values = (modelWidget.options as { values?: string[] } | undefined)
    2369              :       ?.values
    2370            0 :     if (values && !values.includes(uploadedPath)) {
    2371            0 :       values.push(uploadedPath)
    2372              :     }
    2373            0 :     modelWidget.value = uploadedPath
    2374            0 :     return node
    2375              :   }
    2376              : 
    2377              :   /**
    2378              :    * Loads multiple files, connects to a batch node, and selects them
    2379              :    * @param {FileList} fileList
    2380              :    */
    2381              :   async handleFileList(fileList: File[]) {
    2382            0 :     if (fileList.length === 0) return
    2383            0 :     if (!fileList[0].type.startsWith('image')) return
    2384              : 
    2385            0 :     const imageNodes = await pasteImageNodes(this.canvas, fileList)
    2386            0 :     if (imageNodes.length === 0) return
    2387              : 
    2388            0 :     if (imageNodes.length > 1) {
    2389            0 :       const batchImagesNode = await createNode(this.canvas, 'BatchImagesNode')
    2390            0 :       if (!batchImagesNode) return
    2391              : 
    2392            0 :       this.positionBatchNodes(imageNodes, batchImagesNode)
    2393            0 :       this.canvas.selectItems([...imageNodes, batchImagesNode])
    2394              : 
    2395            0 :       imageNodes.forEach((imageNode, index) => {
    2396            0 :         imageNode.connect(0, batchImagesNode, index)
    2397              :       })
    2398              :     } else {
    2399            0 :       this.canvas.selectItems(imageNodes)
    2400              :     }
    2401              :   }
    2402              : 
    2403              :   async handleAudioFileList(fileList: File[]) {
    2404            0 :     const audioNodes = await pasteAudioNodes(this.canvas, fileList)
    2405            0 :     if (audioNodes.length === 0) return
    2406              : 
    2407            0 :     this.positionNodes(audioNodes)
    2408            0 :     this.canvas.selectItems(audioNodes)
    2409              :   }
    2410              : 
    2411              :   async handleVideoFileList(fileList: File[]) {
    2412            0 :     const videoNodes = await pasteVideoNodes(this.canvas, fileList)
    2413            0 :     if (videoNodes.length === 0) return
    2414              : 
    2415            0 :     this.positionNodes(videoNodes)
    2416            0 :     this.canvas.selectItems(videoNodes)
    2417              :   }
    2418              : 
    2419              :   /**
    2420              :    * Positions batched nodes in drag and drop
    2421              :    * @param nodes
    2422              :    * @param batchNode
    2423              :    */
    2424              :   positionNodes(nodes: LGraphNode[]): void {
    2425           38 :     if (nodes.length <= 1) return
    2426              : 
    2427            0 :     const [x, y] = nodes[0].getBounding()
    2428            0 :     const nodeHeight = 150
    2429              : 
    2430            0 :     nodes.forEach((node, index) => {
    2431            0 :       if (index > 0) {
    2432            0 :         node.pos = [x, y + nodeHeight * index + 25 * (index + 1)]
    2433              :       }
    2434              :     })
    2435              : 
    2436           38 :     this.canvas.graph?.change()
    2437              :   }
    2438              : 
    2439              :   positionBatchNodes(nodes: LGraphNode[], batchNode: LGraphNode): void {
    2440            0 :     const [x, y, width] = nodes[0].getBounding()
    2441            0 :     batchNode.pos = [x + width + 100, y + 30]
    2442              : 
    2443              :     // Retrieving Node Height is inconsistent
    2444            0 :     let height = 0
    2445            0 :     if (nodes[0].type === 'LoadImage') {
    2446            0 :       height = 344
    2447              :     }
    2448              : 
    2449            0 :     nodes.forEach((node, index) => {
    2450            0 :       if (index > 0) {
    2451            0 :         node.pos = [x, y + height * index + 25 * (index + 1)]
    2452              :       }
    2453              :     })
    2454              : 
    2455            0 :     this.canvas.graph?.change()
    2456              :   }
    2457              : 
    2458              :   // @deprecated
    2459              :   isApiJson(data: unknown): data is ComfyApiWorkflow {
    2460           16 :     if (!_.isObject(data) || Array.isArray(data)) {
    2461            0 :       return false
    2462              :     }
    2463           16 :     if (Object.keys(data).length === 0) return false
    2464              : 
    2465           16 :     return Object.values(data).every((node) => {
    2466           31 :       if (!node || typeof node !== 'object' || Array.isArray(node)) {
    2467            0 :         return false
    2468              :       }
    2469              : 
    2470           31 :       const { class_type: classType, inputs } = node as Record<string, unknown>
    2471           31 :       const inputsIsRecord = _.isObject(inputs) && !Array.isArray(inputs)
    2472           31 :       return typeof classType === 'string' && inputsIsRecord
    2473              :     })
    2474              :   }
    2475              : 
    2476              :   async loadApiJson(
    2477              :     apiData: ComfyApiWorkflow,
    2478              :     fileName: string,
    2479              :     options: { deferWarnings?: boolean } = {}
    2480              :   ): Promise<void | false> {
    2481           16 :     return this.enqueueGraphLoad(() =>
    2482              :       this.loadApiJsonNow(apiData, fileName, options)
    2483              :     )
    2484              :   }
    2485              : 
    2486              :   private async loadApiJsonNow(
    2487              :     apiData: ComfyApiWorkflow,
    2488              :     fileName: string,
    2489              :     options: { deferWarnings?: boolean }
    2490              :   ): Promise<void> {
    2491           16 :     let lifecycleStarted = false
    2492           16 :     let lifecycleCompleted = false
    2493           16 :     let releaseCaptureSuppression: (() => void) | undefined
    2494           16 :     try {
    2495           16 :       await useExtensionService().invokeExtensionsAsync('beforeLoadGraph')
    2496           16 :       lifecycleStarted = true
    2497              :       const replaceCanvas = useWorkflowService().beforeLoadNewGraph(false)
    2498           16 :       replaceCanvas()
    2499           16 :       releaseCaptureSuppression = beginChangeCaptureSuppression()
    2500           16 :       this.canvas.setGraph(this.rootGraph)
    2501           16 :       withGraphIntentSource('load', () => this.clean())
    2502              : 
    2503           16 :       const ids = Object.keys(apiData)
    2504           16 :       const localNodeIdsByApiId = new Map<string, NodeId>()
    2505              :       {
    2506           16 :         const taken = new Set(ids.filter((id) => !id.includes(':')))
    2507           16 :         for (const id of ids) {
    2508           27 :           if (!id.includes(':')) {
    2509           27 :             localNodeIdsByApiId.set(id, toNodeId(isNaN(+id) ? id : +id))
    2510           27 :             continue
    2511              :           }
    2512            0 :           let candidate = id.replaceAll(':', '_')
    2513            0 :           while (taken.has(candidate)) candidate = `${candidate}_`
    2514            0 :           taken.add(candidate)
    2515            0 :           localNodeIdsByApiId.set(id, toNodeId(candidate))
    2516              :         }
    2517              :       }
    2518           16 :       const missingNodeTypes: MissingNodeType[] = []
    2519           16 :       const nodeReplacementStore = useNodeReplacementStore()
    2520           16 :       await nodeReplacementStore.load()
    2521           16 :       withGraphIntentSource('load', () => {
    2522           16 :         for (const id of ids) {
    2523           27 :           const data = apiData[id]
    2524           27 :           const nodeId = localNodeIdsByApiId.get(id) ?? toNodeId(id)
    2525           27 :           let node = LiteGraph.createNode(data.class_type)
    2526           27 :           let placeholderEntry:
    2527              :             | Extract<MissingNodeType, { type: string }>
    2528              :             | undefined
    2529           27 :           if (!node) {
    2530            2 :             const cnrId = zNodePackMetadata.shape.cnr_id.safeParse(
    2531              :               data._meta?.cnr_id
    2532              :             ).data
    2533            2 :             const auxId = zNodePackMetadata.shape.aux_id.safeParse(
    2534              :               data._meta?.aux_id
    2535              :             ).data
    2536            2 :             const packVersion = zNodePackMetadata.shape.ver.safeParse(
    2537              :               data._meta?.ver
    2538              :             ).data
    2539            2 :             const missingNode = new LGraphNode(
    2540              :               data._meta?.title ?? data.class_type,
    2541              :               sanitizeNodeName(data.class_type)
    2542              :             )
    2543            2 :             node = missingNode
    2544            2 :             node.has_errors = true
    2545            2 :             const widgetValues: TWidgetValue[] = []
    2546            2 :             const widgetValuesNamed: Record<string, TWidgetValue> =
    2547              :               Object.create(null)
    2548            2 :             for (const [input, value] of Object.entries(data.inputs)) {
    2549            6 :               if (value instanceof Array) {
    2550            0 :                 node.addInput(input, '*')
    2551              :               } else {
    2552            6 :                 const widgetValue = unwrapExportedWidgetValue(
    2553              :                   value
    2554              :                 ) as TWidgetValue
    2555            6 :                 widgetValues.push(widgetValue)
    2556            6 :                 widgetValuesNamed[input] = widgetValue
    2557              :               }
    2558              :             }
    2559            2 :             if (cnrId) node.properties.cnr_id = cnrId
    2560            2 :             if (auxId) node.properties.aux_id = auxId
    2561            2 :             if (packVersion) node.properties.ver = packVersion
    2562            2 :             node.last_serialization = {
    2563              :               id: nodeId,
    2564              :               type: data.class_type,
    2565              :               pos: [node.pos[0], node.pos[1]],
    2566              :               size: [node.size[0], node.size[1]],
    2567              :               flags: {},
    2568              :               order: 0,
    2569              :               mode: node.mode,
    2570              :               title: data._meta?.title ?? data.class_type,
    2571              :               inputs: node.inputs.map((input, i) =>
    2572              :                 inputAsSerialisable(input, missingNode, i)
    2573              :               ),
    2574              :               properties: { ...node.properties },
    2575              :               widgets_values: widgetValues,
    2576              :               widgets_values_named: widgetValuesNamed
    2577              :             }
    2578            2 :             const replacement = nodeReplacementStore.getReplacementFor(
    2579              :               data.class_type
    2580              :             )
    2581            2 :             placeholderEntry = {
    2582              :               type: data.class_type,
    2583              :               cnrId: getCnrIdFromProperties(node.properties),
    2584              :               isReplaceable: replacement !== null,
    2585              :               replacement: replacement ?? undefined
    2586              :             }
    2587            2 :             missingNodeTypes.push(placeholderEntry)
    2588              :           }
    2589           27 :           node.id = nodeId
    2590           27 :           node.title = data._meta?.title ?? node.title
    2591           27 :           app.rootGraph.add(node)
    2592           27 :           if (placeholderEntry && node.last_serialization) {
    2593            2 :             node.last_serialization.id = node.id
    2594            2 :             placeholderEntry.nodeId = String(node.id)
    2595              :           }
    2596              :         }
    2597              : 
    2598           16 :         const unresolvedInputs: (() => boolean)[] = []
    2599           16 :         const processNodeInputs = (id: string) => {
    2600           27 :           const data = apiData[id]
    2601           27 :           const currentNodeId = localNodeIdsByApiId.get(id) ?? toNodeId(id)
    2602           27 :           const node = app.rootGraph.getNodeById(currentNodeId)
    2603           27 :           if (!node) return
    2604           27 :           const targetNode = node
    2605           27 :           const inputs = restoreDynamicGroupInputs(node, data.inputs)
    2606              : 
    2607           27 :           for (const input in inputs) {
    2608           84 :             const value = inputs[input]
    2609           84 :             if (value instanceof Array) {
    2610           11 :               function connectInput() {
    2611           11 :                 const [fromId, fromSlot] = value
    2612           11 :                 const fromNode = app.rootGraph.getNodeById(
    2613              :                   localNodeIdsByApiId.get(String(fromId)) ?? toNodeId(fromId)
    2614              :                 )
    2615           11 :                 if (!fromNode?.outputs[fromSlot]) return false
    2616              : 
    2617           11 :                 let toSlot = targetNode.inputs.findIndex(
    2618              :                   (inp) => inp.name === input
    2619              :                 )
    2620           11 :                 if (toSlot === -1) {
    2621            0 :                   try {
    2622            0 :                     const widget = targetNode.widgets?.find(
    2623              :                       (w) => w.name === input
    2624              :                     )
    2625            0 :                     const convertFn = (
    2626              :                       targetNode as LGraphNode & {
    2627              :                         convertWidgetToInput?: (w: IBaseWidget) => boolean
    2628              :                       }
    2629              :                     ).convertWidgetToInput
    2630            0 :                     if (widget && convertFn?.(widget)) {
    2631            0 :                       toSlot = targetNode.inputs.findIndex(
    2632              :                         (inp) => inp.name === input
    2633              :                       )
    2634              :                     }
    2635              :                   } catch {
    2636            0 :                     return false
    2637              :                   }
    2638              :                 }
    2639           11 :                 if (toSlot === -1) return false
    2640              : 
    2641           11 :                 fromNode.connect(fromSlot, targetNode, toSlot)
    2642           11 :                 return true
    2643              :               }
    2644           11 :               if (!connectInput()) unresolvedInputs.push(connectInput)
    2645              :             } else {
    2646           79 :               function applyWidgetValue() {
    2647           79 :                 const widget = targetNode.widgets?.find((w) => w.name === input)
    2648           79 :                 if (!widget) return false
    2649           67 :                 const widgetValue = unwrapExportedWidgetValue(
    2650              :                   value
    2651              :                 ) as TWidgetValue
    2652           67 :                 widget.value = widgetValue
    2653           79 :                 try {
    2654           79 :                   widget.callback?.(widgetValue)
    2655              :                 } catch (error) {
    2656            0 :                   reportError(error, {
    2657              :                     surface: 'graph',
    2658              :                     errorType: 'failure_invoking_api_workflow_widget_callback',
    2659              :                     tags: {
    2660              :                       node_type: targetNode.type,
    2661              :                       widget_name: input
    2662              :                     },
    2663              :                     context: { fileName }
    2664              :                   })
    2665              :                 }
    2666           67 :                 return true
    2667              :               }
    2668           73 :               if (!applyWidgetValue()) unresolvedInputs.push(applyWidgetValue)
    2669              :             }
    2670              :           }
    2671              :         }
    2672              : 
    2673           27 :         for (const id of ids) processNodeInputs(id)
    2674           16 :         let pendingInputs = unresolvedInputs
    2675           16 :         while (pendingInputs.length > 0) {
    2676            2 :           const remainingInputs = pendingInputs.filter(
    2677              :             (applyInput) => !applyInput()
    2678              :           )
    2679            2 :           if (remainingInputs.length === pendingInputs.length) break
    2680            0 :           pendingInputs = remainingInputs
    2681              :         }
    2682           16 :         for (const node of app.rootGraph.nodes) {
    2683           27 :           if (!node.last_serialization) continue
    2684            2 :           node.last_serialization.inputs = node.inputs.map((input, i) =>
    2685              :             inputAsSerialisable(input, node, i)
    2686              :           )
    2687              :         }
    2688           16 :         app.rootGraph.arrange()
    2689              :       })
    2690              : 
    2691           16 :       await withGraphIntentSource('load', () =>
    2692              :         useExtensionService().invokeExtensionsAsync(
    2693              :           'afterConfigureGraph',
    2694              :           missingNodeTypes
    2695              :         )
    2696              :       )
    2697           16 :       await useWorkflowService().afterLoadNewGraph(
    2698              :         fileName,
    2699              :         this.rootGraph.serialize() as unknown as ComfyWorkflowJSON
    2700              :       )
    2701           16 :       await useExtensionService().invokeExtensionsAsync('afterLoadGraph')
    2702           16 :       lifecycleCompleted = true
    2703           16 :       if (missingNodeTypes.length) {
    2704            2 :         this.showMissingNodesError(missingNodeTypes, options)
    2705              :       }
    2706              :     } catch (error) {
    2707            0 :       if (!lifecycleCompleted) {
    2708            0 :         throw await this.reportGraphLoadFailure(error, {
    2709              :           notifyExtensions: lifecycleStarted
    2710              :         })
    2711              :       }
    2712            0 :       throw error
    2713              :     } finally {
    2714           16 :       releaseCaptureSuppression?.()
    2715              :     }
    2716              :   }
    2717              : 
    2718              :   /**
    2719              :    * Registers a Comfy web extension with the app
    2720              :    * @param {ComfyExtension} extension
    2721              :    */
    2722              :   registerExtension(extension: ComfyExtension) {
    2723         9474 :     useExtensionService().registerExtension(extension)
    2724              :   }
    2725              : 
    2726              :   /**
    2727              :    * Collects context menu items from all extensions for canvas menus
    2728              :    * @param canvas The canvas instance
    2729              :    * @returns Array of context menu items from all extensions
    2730              :    */
    2731              :   collectCanvasMenuItems(canvas: LGraphCanvas): IContextMenuValue[] {
    2732           32 :     return useExtensionService()
    2733              :       .invokeExtensions('getCanvasMenuItems', canvas)
    2734              :       .flat() as IContextMenuValue[]
    2735              :   }
    2736              : 
    2737              :   /**
    2738              :    * Collects context menu items from all extensions for node menus
    2739              :    * @param node The node being right-clicked
    2740              :    * @returns Array of context menu items from all extensions
    2741              :    */
    2742              :   collectNodeMenuItems(node: LGraphNode): IContextMenuValue[] {
    2743         2340 :     return useExtensionService()
    2744              :       .invokeExtensions('getNodeMenuItems', node)
    2745              :       .flat() as IContextMenuValue[]
    2746              :   }
    2747              : 
    2748              :   /**
    2749              :    * Reload node definitions, or register `given` ones, and refresh combo lists on all nodes; definitions owed to `addCustomNodeDefs` go through it first.
    2750              :    * Resolves false, changing nothing, when the server refuses the definitions while they are owed.
    2751              :    */
    2752              :   async reloadNodeDefs(
    2753              :     given?: Record<string, ComfyNodeDefV1>
    2754              :   ): Promise<boolean> {
    2755           19 :     const defs = given ?? (await this.fetchReloadedNodeDefs())
    2756           17 :     if (!defs) return false
    2757           17 :     if (useExtensionStore().takeOwedNodeDefHooks())
    2758            0 :       await useExtensionService().invokeExtensionsAsync(
    2759              :         'addCustomNodeDefs',
    2760              :         defs
    2761              :       )
    2762           17 :     for (const nodeId in defs) {
    2763        17528 :       await this.registerNodeDef(nodeId, defs[nodeId])
    2764              :     }
    2765              :     // Refresh combo widgets in all nodes including those in subgraphs
    2766           17 :     const nodeOutputStore = useNodeOutputStore()
    2767           17 :     forEachNode(this.rootGraph, (node) => {
    2768           69 :       const def = defs[node.type] as ComfyNodeDefV1 | undefined
    2769              :       // Allow primitive nodes to handle refresh
    2770           69 :       node.refreshComboInNode?.(defs)
    2771              : 
    2772           69 :       if (!def?.input) return
    2773              : 
    2774           64 :       if (node.widgets) {
    2775           64 :         const nodeInputs = def.input
    2776           64 :         for (const widget of node.widgets) {
    2777          134 :           if (widget.type === 'combo') {
    2778              :             const resolved = resolveDynamicInputSpec(
    2779              :               nodeInputs,
    2780              :               widget.name,
    2781              :               (name) => node.widgets?.find((item) => item.name === name)?.value
    2782              :             )
    2783           41 :             const inputSpec = resolved?.spec
    2784           41 :             if (inputSpec && isComboInputSpecV2(inputSpec)) {
    2785            0 :               widget.options.values = inputSpec[1]?.options
    2786           41 :             } else if (inputSpec && isComboInputSpecV1(inputSpec)) {
    2787           32 :               widget.options.values = inputSpec[0]
    2788              :             }
    2789              :           }
    2790              :         }
    2791              :       }
    2792              : 
    2793              :       // Re-trigger previews on media nodes (e.g. LoadImage)
    2794              :       // to bust browser cache when files are edited externally
    2795           69 :       if (isImageNode(node) || isVideoNode(node)) {
    2796            1 :         nodeOutputStore.refreshNodeOutputs(node)
    2797              :       }
    2798              :     })
    2799              : 
    2800           17 :     await useExtensionService().invokeExtensionsAsync(
    2801              :       'refreshComboInNodes',
    2802              :       defs
    2803              :     )
    2804              : 
    2805              :     // Promoted widgets keep hosted option snapshots; sync them after source refresh hooks run.
    2806           16 :     syncPromotedComboHostOptions(this.rootGraph)
    2807              : 
    2808           16 :     if (this.vueAppReady) {
    2809           16 :       this.updateVueAppNodeDefs(defs)
    2810              :     }
    2811           16 :     return true
    2812              :   }
    2813              : 
    2814              :   private async fetchReloadedNodeDefs(): Promise<
    2815              :     Record<string, ComfyNodeDefV1> | undefined
    2816              :   > {
    2817           19 :     if (!useExtensionStore().areNodeDefHooksOwed())
    2818           19 :       return await this.getNodeDefs()
    2819            0 :     const fetched = await this.fetchNodeDefs()
    2820            0 :     if (fetched.ok) return fetched.defs
    2821            0 :     reportError(new Error(`GET /object_info answered ${fetched.status}`), {
    2822              :       errorType: 'late_extension_failed',
    2823              :       surface: 'platform',
    2824              :       level: 'warning',
    2825              :       tags: { step: 'reloadNodeDefs' }
    2826              :     })
    2827              :     return undefined
    2828              :   }
    2829              : 
    2830              :   /**
    2831              :    * Refresh combo list on whole nodes
    2832              :    */
    2833              :   async refreshComboInNodes() {
    2834           14 :     const requestToastId = this.vueAppReady
    2835              :       ? useToast().info(t('g.update'), {
    2836              :           description: t('toastMessages.updateRequested')
    2837              :         })
    2838              :       : undefined
    2839              : 
    2840           14 :     try {
    2841           14 :       const reloaded = await this.reloadNodeDefs()
    2842              : 
    2843           14 :       if (this.vueAppReady && reloaded) {
    2844           11 :         useToast().success(t('g.updated'), {
    2845              :           description: t('toastMessages.nodeDefinitionsUpdated'),
    2846              :           duration: 1000
    2847              :         })
    2848            0 :       } else if (this.vueAppReady) {
    2849            0 :         useToast().error(t('g.error'), {
    2850              :           description: t('toastMessages.nodeDefinitionsUpdateFailed')
    2851              :         })
    2852              :       }
    2853              :     } catch (error) {
    2854            1 :       if (this.vueAppReady) {
    2855            1 :         useToast().error(t('g.error'), {
    2856              :           description: t('toastMessages.nodeDefinitionsUpdateFailed')
    2857              :         })
    2858              :       }
    2859            1 :       throw error
    2860              :     } finally {
    2861           12 :       if (requestToastId !== undefined) {
    2862           12 :         useToast().dismiss(requestToastId)
    2863              :       }
    2864              :     }
    2865              :   }
    2866              : 
    2867              :   /**
    2868              :    * Clean current state
    2869              :    */
    2870              :   clean() {
    2871              :     const nodeOutputStore = useNodeOutputStore()
    2872         3915 :     nodeOutputStore.resetAllOutputsAndPreviews()
    2873         3915 :     const executionErrorStore = useExecutionErrorStore()
    2874         3915 :     executionErrorStore.setActiveGraph(null)
    2875         3915 :     useMissingNodesErrorStore().setMissingNodeTypes([])
    2876              : 
    2877         3915 :     useDomWidgetStore().clear()
    2878              : 
    2879              :     // Subgraph does not properly implement `clear` and the parent class's
    2880              :     // (`LGraph`) `clear` breaks the subgraph structure.
    2881         3915 :     if (!this.canvas.subgraph) {
    2882         3915 :       this.rootGraph.clear()
    2883         3915 :       ensureNonZeroUuid(this.rootGraph)
    2884              :     }
    2885              : 
    2886         3915 :     executionErrorStore.setActiveGraph(this.rootGraph.id)
    2887              :   }
    2888              : 
    2889              :   clientPosToCanvasPos(pos: Vector2): Vector2 {
    2890            0 :     if (!this.positionConversion) {
    2891            0 :       throw new Error('clientPosToCanvasPos called before setup')
    2892              :     }
    2893            0 :     return this.positionConversion.clientPosToCanvasPos(pos)
    2894              :   }
    2895              : 
    2896              :   canvasPosToClientPos(pos: Vector2): Vector2 {
    2897         1371 :     if (!this.positionConversion) {
    2898            0 :       throw new Error('canvasPosToClientPos called before setup')
    2899              :     }
    2900         1371 :     return this.positionConversion.canvasPosToClientPos(pos)
    2901              :   }
    2902              : }
    2903              : 
    2904         2356 : export const app = new ComfyApp()
    2905         2356 : registerApp(app)
        

Generated by: LCOV version 2.0-1