LCOV - code coverage report
Current view: top level - src/platform/extensions/stores - extensionStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 69.2 % 104 72
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { computed, markRaw, ref, shallowRef } from 'vue'
       3              : 
       4              : import { useSettingStore } from '@/platform/settings/settingStore'
       5              : import { reportError } from '@comfyorg/telemetry/reportError'
       6              : import type { ComfyExtension } from '@/types/comfy'
       7              : 
       8              : /**
       9              :  * These extensions are always active, even if they are disabled in the setting.
      10              :  */
      11         2356 : const ALWAYS_ENABLED_EXTENSIONS: readonly string[] = []
      12              : 
      13         2356 : const ALWAYS_DISABLED_EXTENSIONS: readonly string[] = [
      14              :   // pysssss.Locking is replaced by pin/unpin in ComfyUI core.
      15              :   // https://github.com/Comfy-Org/litegraph.js/pull/117
      16              :   'pysssss.Locking',
      17              :   // pysssss.SnapToGrid is replaced by Comfy.Graph.AlwaysSnapToGrid in ComfyUI core.
      18              :   // pysssss.SnapToGrid tries to write global app.shiftDown state, which is no longer
      19              :   // allowed since v1.3.12.
      20              :   // https://github.com/Comfy-Org/ComfyUI_frontend/issues/1176
      21              :   'pysssss.SnapToGrid',
      22              :   // Favicon status is implemented in ComfyUI core in v1.20.
      23              :   // https://github.com/Comfy-Org/ComfyUI_frontend/pull/3880
      24              :   'pysssss.FaviconStatus',
      25              :   'KJNodes.browserstatus'
      26              : ]
      27              : 
      28              : type Registration =
      29              :   | { mode: 'immediate' }
      30              :   | { mode: 'holding'; held: readonly ComfyExtension[] }
      31              :   | { mode: 'starting' }
      32              : 
      33              : interface ExtensionPathState {
      34              :   paths: string[]
      35              :   disabledAtBoot: Set<string>
      36              : }
      37              : 
      38         2356 : export const useExtensionStore = defineStore('extension', () => {
      39              :   // For legacy reasons, the name uniquely identifies an extension
      40         2355 :   const extensionByName = ref<Record<string, ComfyExtension>>({})
      41         2355 :   const extensions = computed(() => Object.values(extensionByName.value))
      42              :   // Not using computed because disable extension requires reloading of the page.
      43              :   // Dynamically update this list won't affect extensions that are already loaded.
      44         2355 :   const disabledExtensionNames = ref<Set<string>>(new Set())
      45         2355 :   const extensionPathState = shallowRef<ExtensionPathState>({
      46              :     paths: [],
      47              :     disabledAtBoot: new Set()
      48              :   })
      49         2355 :   const extensionPaths = computed(() => extensionPathState.value.paths)
      50         2355 :   const isSavingExtensionPaths = ref(false)
      51              : 
      52              :   // Disabled extension names that are currently not in the extension list.
      53              :   // If a node pack is disabled in the backend, we shouldn't remove the configuration
      54              :   // of the frontend extension disable list, in case the node pack is re-enabled.
      55         2355 :   const inactiveDisabledExtensionNames = computed(() => {
      56            2 :     return Array.from(disabledExtensionNames.value).filter(
      57              :       (name) => !(name in extensionByName.value)
      58              :     )
      59              :   })
      60         2355 :   const isAgentCreatedExtension = (extension: ComfyExtension) =>
      61              :     extension.provenance?.createdBy === 'comfy-agent' ||
      62              :     /^agent\.[^.]+\./.test(extension.name)
      63         2355 :   const agentExtensions = computed(() =>
      64              :     extensions.value.filter(isAgentCreatedExtension)
      65              :   )
      66         2355 :   const isExtensionInstalled = (name: string) => name in extensionByName.value
      67              : 
      68         2355 :   const isExtensionEnabled = (name: string) =>
      69              :     !disabledExtensionNames.value.has(name)
      70         2355 :   const enabledExtensions = computed(() => {
      71         2389 :     return extensions.value.filter((ext) => isExtensionEnabled(ext.name))
      72              :   })
      73              : 
      74          367 :   function isExtensionReadOnly(name: string) {
      75          367 :     return (
      76              :       ALWAYS_DISABLED_EXTENSIONS.includes(name) ||
      77              :       ALWAYS_ENABLED_EXTENSIONS.includes(name)
      78              :     )
      79              :   }
      80              : 
      81        85146 :   function registerExtension(extension: ComfyExtension) {
      82        85146 :     if (!extension.name) {
      83            0 :       throw new Error("Extensions must have a 'name' property.")
      84              :     }
      85              : 
      86        85146 :     if (extension.name in extensionByName.value) {
      87            0 :       throw new Error(`Extension named '${extension.name}' already registered.`)
      88              :     }
      89              : 
      90        85146 :     if (disabledExtensionNames.value.has(extension.name)) {
      91            2 :       console.warn(`Extension ${extension.name} is disabled.`)
      92              :     }
      93              : 
      94        85146 :     extensionByName.value[extension.name] = markRaw(extension)
      95              :   }
      96              : 
      97         2355 :   const registration = shallowRef<Registration>({ mode: 'immediate' })
      98         2355 :   const isHoldingCustomNodes = computed(
      99              :     () => registration.value.mode === 'holding'
     100              :   )
     101         2355 :   const areLateCustomNodesPending = computed(
     102              :     () => registration.value.mode !== 'immediate'
     103              :   )
     104              : 
     105              :   /** From now on the extension service holds new extensions instead of registering them. */
     106            0 :   function holdNewCustomNodes() {
     107            0 :     if (registration.value.mode === 'immediate')
     108            0 :       registration.value = { mode: 'holding', held: [] }
     109              :   }
     110              : 
     111            0 :   function holdCustomNode(extension: ComfyExtension) {
     112            0 :     const current = registration.value
     113            0 :     if (current.mode === 'holding')
     114            0 :       registration.value = {
     115              :         mode: 'holding',
     116              :         held: [...current.held, extension]
     117              :       }
     118              :   }
     119              : 
     120            0 :   function releaseHeldCustomNode(extension: ComfyExtension) {
     121            0 :     const current = registration.value
     122            0 :     if (current.mode === 'holding')
     123            0 :       registration.value = {
     124              :         mode: 'holding',
     125              :         held: current.held.filter((held) => held !== extension)
     126              :       }
     127              :   }
     128              : 
     129              :   /** Returns the extensions held so far, in registration order; holding goes on. */
     130            0 :   function takeHeldCustomNodes(): ComfyExtension[] {
     131            0 :     const current = registration.value
     132            0 :     if (current.mode !== 'holding') return []
     133            0 :     registration.value = { mode: 'holding', held: [] }
     134            0 :     return [...current.held]
     135              :   }
     136              : 
     137              :   /** Ends holding and returns the extensions still held; they stay pending until `finishLateCustomNodes`. */
     138            0 :   function stopHoldingCustomNodes(): ComfyExtension[] {
     139            0 :     const current = registration.value
     140            0 :     if (current.mode !== 'holding') return []
     141            0 :     registration.value = { mode: 'starting' }
     142            0 :     return [...current.held]
     143              :   }
     144              : 
     145            0 :   function finishLateCustomNodes() {
     146            0 :     registration.value = { mode: 'immediate' }
     147              :   }
     148              : 
     149         2355 :   const nodeDefHooksOwed = ref(false)
     150              : 
     151              :   /** The next node definitions fetched go through every extension's `addCustomNodeDefs` before they register. */
     152            0 :   function oweNodeDefHooks() {
     153            0 :     nodeDefHooksOwed.value = true
     154              :   }
     155              : 
     156              :   /** Whether the next node definitions fetched are owed to `addCustomNodeDefs`, without settling the debt. */
     157           19 :   function areNodeDefHooksOwed(): boolean {
     158           19 :     return nodeDefHooksOwed.value
     159              :   }
     160              : 
     161              :   /** Whether fetched node definitions are owed to `addCustomNodeDefs`; settles the debt. */
     162           17 :   function takeOwedNodeDefHooks(): boolean {
     163           17 :     const owed = nodeDefHooksOwed.value
     164           17 :     nodeDefHooksOwed.value = false
     165           17 :     return owed
     166              :   }
     167              : 
     168            0 :   function unregisterExtension(name: string) {
     169            0 :     delete extensionByName.value[name]
     170              :   }
     171              : 
     172         2355 :   function loadDisabledExtensionNames(names: string[]) {
     173         2355 :     disabledExtensionNames.value = new Set(names)
     174         2355 :     for (const name of ALWAYS_DISABLED_EXTENSIONS) {
     175         9420 :       disabledExtensionNames.value.add(name)
     176              :     }
     177         2355 :     for (const name of ALWAYS_ENABLED_EXTENSIONS) {
     178            0 :       disabledExtensionNames.value.delete(name)
     179              :     }
     180              :   }
     181              : 
     182         2355 :   function loadExtensionPaths(paths: string[], disabledAtBoot: string[]) {
     183         2355 :     extensionPathState.value = {
     184              :       paths: [...new Set(paths)],
     185              :       disabledAtBoot: new Set(disabledAtBoot)
     186              :     }
     187              :   }
     188              : 
     189           15 :   function isExtensionPathEnabledAtBoot(path: string) {
     190           15 :     return !extensionPathState.value.disabledAtBoot.has(path)
     191              :   }
     192              : 
     193           14 :   function getCurrentDisabledExtensionPaths(): string[] {
     194           14 :     return useSettingStore().get('Comfy.Extension.DisabledPacks')
     195              :   }
     196              : 
     197            2 :   async function setExtensionPathEnabled(
     198              :     path: string,
     199              :     enabled: boolean
     200              :   ): Promise<
     201              :     | 'saved'
     202              :     | 'unchanged'
     203              :     | 'refused'
     204              :     | 'indeterminate'
     205              :     | 'unconfirmed'
     206              :     | undefined
     207              :   > {
     208            2 :     if (isSavingExtensionPaths.value) return
     209              : 
     210            2 :     const previousDisabledPaths = getCurrentDisabledExtensionPaths()
     211            2 :     const nextDisabledPaths = enabled
     212              :       ? previousDisabledPaths.filter((disabledPath) => disabledPath !== path)
     213              :       : [...new Set([...previousDisabledPaths, path])]
     214            2 :     isSavingExtensionPaths.value = true
     215              : 
     216            2 :     try {
     217            2 :       const result = await useSettingStore().setWithOutcome(
     218              :         'Comfy.Extension.DisabledPacks',
     219              :         nextDisabledPaths
     220              :       )
     221            2 :       if (result.status === 'refused' || result.status === 'indeterminate') {
     222            2 :         const reason = result.statusText || 'Request failed'
     223            2 :         reportError(
     224              :           new Error(
     225              :             `Failed to store disabled extension paths: ${result.statusCode} ${reason}`
     226              :           ),
     227              :           {
     228              :             errorType: 'failure_saving_extension_path_setting',
     229              :             surface: 'platform'
     230              :           }
     231              :         )
     232            0 :       } else if (result.status === 'unconfirmed') {
     233            0 :         reportError(result.cause, {
     234              :           errorType: 'failure_saving_extension_path_setting',
     235              :           surface: 'platform'
     236              :         })
     237              :       }
     238            2 :       return result.status
     239              :     } finally {
     240            2 :       isSavingExtensionPaths.value = false
     241              :     }
     242              :   }
     243              : 
     244         2355 :   const coreExtensions = ref(new Set<ComfyExtension>())
     245         2355 :   const coreExtensionsCaptured = ref(false)
     246         2379 :   function markCoreExtensions(extensions: readonly ComfyExtension[]) {
     247        75672 :     for (const extension of extensions) coreExtensions.value.add(extension)
     248              :   }
     249              : 
     250         2355 :   function captureCoreExtensions() {
     251         2355 :     markCoreExtensions(extensions.value)
     252         2355 :     coreExtensionsCaptured.value = true
     253              :   }
     254              : 
     255       157977 :   function isCoreExtension(name: string) {
     256       157977 :     return (
     257              :       isExtensionInstalled(name) &&
     258              :       coreExtensions.value.has(extensionByName.value[name])
     259              :     )
     260              :   }
     261              : 
     262              :   /** Whether a registered extension's commands, keybindings and UI contributions apply. */
     263       229983 :   function contributes(name: string) {
     264       229983 :     if (!coreExtensionsCaptured.value || isCoreExtension(name)) return true
     265        25828 :     return isExtensionEnabled(name)
     266              :   }
     267              : 
     268              :   const hasThirdPartyExtensions = computed(() => {
     269            9 :     return extensions.value.some((ext) => !isCoreExtension(ext.name))
     270              :   })
     271              : 
     272         2355 :   return {
     273              :     extensions,
     274              :     agentExtensions,
     275              :     enabledExtensions,
     276              :     extensionPaths,
     277              :     isSavingExtensionPaths,
     278              :     inactiveDisabledExtensionNames,
     279              :     isExtensionInstalled,
     280              :     isAgentCreatedExtension,
     281              :     isExtensionEnabled,
     282              :     isExtensionReadOnly,
     283              :     registerExtension,
     284              :     unregisterExtension,
     285              :     isHoldingCustomNodes,
     286              :     areLateCustomNodesPending,
     287              :     holdNewCustomNodes,
     288              :     holdCustomNode,
     289              :     releaseHeldCustomNode,
     290              :     takeHeldCustomNodes,
     291              :     stopHoldingCustomNodes,
     292              :     finishLateCustomNodes,
     293              :     oweNodeDefHooks,
     294              :     areNodeDefHooksOwed,
     295              :     takeOwedNodeDefHooks,
     296              :     loadDisabledExtensionNames,
     297              :     loadExtensionPaths,
     298              :     isExtensionPathEnabledAtBoot,
     299              :     getCurrentDisabledExtensionPaths,
     300              :     setExtensionPathEnabled,
     301              :     markCoreExtensions,
     302              :     captureCoreExtensions,
     303              :     contributes,
     304              :     isCoreExtension,
     305              :     hasThirdPartyExtensions
     306              :   }
     307              : })
        

Generated by: LCOV version 2.0-1