LCOV - code coverage report
Current view: top level - src/platform/workflowErrors/missingModel - missingModelPipeline.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 92.5 % 67 62
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { st } from '@/i18n'
       2              : import type { LGraph } from '@/lib/litegraph/src/litegraph'
       3              : import { assetService } from '@/platform/assets/services/assetService'
       4              : import { isCloud } from '@/platform/distribution/types'
       5              : import {
       6              :   enrichWithEmbeddedMetadata,
       7              :   hasPendingVerification,
       8              :   scanAllModelCandidates,
       9              :   verifyAssetSupportedCandidates
      10              : } from '@/platform/workflowErrors/missingModel/missingModelScan'
      11              : import type { MissingModelWorkflowData } from '@/platform/workflowErrors/missingModel/missingModelScan'
      12              : import { cloudReleaseModelOptions } from '@/platform/workflowErrors/missingModel/releaseModelOptions'
      13              : import type {
      14              :   MissingModelCandidate,
      15              :   MissingModelPipelineResult
      16              : } from '@/platform/workflowErrors/missingModel/types'
      17              : import { reportError } from '@comfyorg/telemetry/reportError'
      18              : import { useToast } from '@/components/ui/toast/toastStore'
      19              : import { updatePendingWarnings } from '@/platform/workflow/core/utils/pendingWarnings'
      20              : import type { ComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow'
      21              : import type { ModelFile } from '@comfyorg/graph-model/workflowSchema'
      22              : import { api } from '@/scripts/api'
      23              : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
      24              : import { useModelToNodeStore } from '@/stores/modelToNodeStore'
      25              : import { useWorkspaceStore } from '@/stores/workspaceStore'
      26              : import type { MissingNodeType } from '@/platform/workflowErrors/nodeReplacement/types'
      27              : import {
      28              :   getNodeByExecutionId,
      29              :   isCandidateScopeActive,
      30              :   isMissingCandidateActive
      31              : } from '@/utils/graphTraversalUtil'
      32              : 
      33              : interface MissingModelPipelineStore {
      34              :   missingModelCandidates: MissingModelCandidate[] | null
      35              :   createVerificationAbortController: () => AbortController
      36              :   setFolderPaths: (paths: Record<string, string[]>) => void
      37              :   setFileSize: (url: string, size: number) => void
      38              :   setGatedRepoUrl: (url: string, repoUrl: string) => void
      39              : }
      40              : 
      41              : interface RunMissingModelPipelineOptions {
      42              :   graph: LGraph
      43              :   graphData: MissingModelWorkflowData
      44              :   missingModelStore: MissingModelPipelineStore
      45              :   missingNodeTypes?: MissingNodeType[]
      46              :   onVerified?: (candidates: MissingModelCandidate[]) => void
      47              :   silent?: boolean
      48              : }
      49              : 
      50              : interface RefreshMissingModelPipelineOptions {
      51              :   graph: LGraph
      52              :   reloadNodeDefs?: () => Promise<void>
      53              :   missingModelStore: MissingModelPipelineStore
      54              :   silent?: boolean
      55              : }
      56              : 
      57              : type MissingModelCandidateWithDownloadMetadata = MissingModelCandidate & {
      58              :   url: string
      59              :   directory: string
      60              : }
      61              : 
      62         3730 : function cacheModelCandidates(
      63              :   wf: Pick<ComfyWorkflow, 'pendingWarnings'> | null | undefined,
      64              :   confirmed: MissingModelCandidate[]
      65              : ) {
      66         3730 :   if (!wf) return
      67         3730 :   updatePendingWarnings(wf, {
      68              :     missingModelCandidates: confirmed
      69              :   })
      70              : }
      71              : 
      72         1013 : function clearMissingModels(
      73              :   wf: Pick<ComfyWorkflow, 'pendingWarnings'> | null | undefined,
      74              :   silent: boolean
      75              : ) {
      76         1013 :   useExecutionErrorStore().surfaceMissingModels([], { silent })
      77         1013 :   cacheModelCandidates(wf, [])
      78              : }
      79              : 
      80         5695 : function hasDownloadMetadata(
      81              :   candidate: MissingModelCandidate
      82              : ): candidate is MissingModelCandidateWithDownloadMetadata {
      83         5695 :   return !!candidate.url && !!candidate.directory
      84              : }
      85              : 
      86         2714 : function toModelFile(candidate: MissingModelCandidateWithDownloadMetadata) {
      87         2714 :   return {
      88              :     name: candidate.name,
      89              :     url: candidate.url,
      90              :     directory: candidate.directory,
      91              :     hash: candidate.hash,
      92              :     hash_type: candidate.hashType
      93              :   }
      94              : }
      95              : 
      96           11 : function getCurrentMissingModelMetadata(
      97              :   missingModelStore: MissingModelPipelineStore
      98              : ): ModelFile[] {
      99           11 :   return (
     100              :     missingModelStore.missingModelCandidates
     101              :       ?.filter(hasDownloadMetadata)
     102              :       .map(toModelFile) ?? []
     103              :   )
     104              : }
     105              : 
     106         3796 : export async function runMissingModelPipeline({
     107              :   graph,
     108              :   graphData,
     109              :   missingModelStore,
     110              :   missingNodeTypes,
     111              :   silent = false,
     112              :   onVerified
     113              : }: RunMissingModelPipelineOptions): Promise<MissingModelPipelineResult> {
     114         3796 :   const controller = missingModelStore.createVerificationAbortController()
     115              : 
     116         3796 :   const getDirectory = (nodeType: string) =>
     117              :     useModelToNodeStore().getCategoryForNodeType(nodeType)
     118         3796 :   const isAssetBrowserWidget = assetService.shouldUseWidgetAssetPicker
     119              : 
     120         3796 :   const candidates = scanAllModelCandidates(
     121              :     graph,
     122              :     isAssetBrowserWidget,
     123              :     getDirectory,
     124              :     cloudReleaseModelOptions()
     125              :   )
     126              : 
     127              :   const enrichedAll = enrichWithEmbeddedMetadata(candidates, graphData)
     128              : 
     129              :   // Drop candidates whose active scope is muted/bypassed. Normal candidates
     130              :   // use nodeId; promoted host candidates use sourceExecutionId so host-keyed
     131              :   // errors still respect inactive interior subgraph containers.
     132              :   // Asymmetric on purpose: a candidate dropped here is not resurrected if
     133              :   // the user un-bypasses the container mid-verification. The realtime
     134              :   // mode-change path (handleNodeModeChange → scanAndAddNodeErrors) is
     135              :   // responsible for surfacing errors after an un-bypass.
     136         3796 :   const enrichedCandidates = enrichedAll.filter((c) =>
     137              :     isCandidateScopeActive(graph, c)
     138              :   )
     139              : 
     140         3796 :   const confirmedCandidates = enrichedCandidates.filter(
     141              :     (c) => c.isMissing === true
     142              :   )
     143         3796 :   const hasDeferredCandidates = enrichedCandidates.some(hasPendingVerification)
     144              :   const downloadableCandidates = confirmedCandidates.filter(hasDownloadMetadata)
     145              : 
     146         3796 :   const missingModels: ModelFile[] = downloadableCandidates.map(toModelFile)
     147              : 
     148         3796 :   const activeWf = useWorkspaceStore().workflow.activeWorkflow
     149         3796 :   updatePendingWarnings(activeWf, {
     150              :     ...(missingNodeTypes ? { missingNodeTypes } : {}),
     151              :     missingModelCandidates: confirmedCandidates
     152              :   })
     153              : 
     154         3796 :   if (!enrichedCandidates.length) {
     155         1013 :     clearMissingModels(activeWf, silent)
     156         1013 :     onVerified?.([])
     157         1013 :     return { missingModels, confirmedCandidates }
     158              :   }
     159              : 
     160              :   // Re-check at surface time: the user may have bypassed a container or
     161              :   // changed the selection while verification or folder paths were in flight.
     162         2783 :   const isStillSelected = (candidate: MissingModelCandidate) => {
     163         5618 :     if (candidate.nodeId == null) return true
     164              :     const node = getNodeByExecutionId(graph, String(candidate.nodeId))
     165              :     const widget = node?.widgets?.find((w) => w.name === candidate.widgetName)
     166         5618 :     return widget?.value === candidate.name
     167              :   }
     168         2783 :   const surfaceActiveCandidates = () => {
     169         2717 :     const confirmed = enrichedCandidates.filter(
     170              :       (c) => isMissingCandidateActive(graph, c) && isStillSelected(c)
     171              :     )
     172         2717 :     useExecutionErrorStore().surfaceMissingModels(confirmed, { silent })
     173         2717 :     cacheModelCandidates(activeWf, confirmed)
     174         2717 :     onVerified?.(
     175              :       enrichedCandidates.filter(
     176              :         (c) => isCandidateScopeActive(graph, c) && isStillSelected(c)
     177              :       )
     178              :     )
     179              :   }
     180         2783 :   const reportVerificationFailure = (err: unknown) => {
     181            0 :     if (controller.signal.aborted) return
     182            0 :     console.warn(
     183              :       '[Missing Model Pipeline] Missing model verification failed:',
     184              :       err
     185              :     )
     186            0 :     reportError(err, {
     187              :       surface: 'assets',
     188              :       errorType: 'missing_model_verification_failed'
     189              :     })
     190            0 :     useToast().warning(
     191              :       st(
     192              :         'toastMessages.missingModelVerificationFailed',
     193              :         'Failed to verify missing models. Some models may not be shown in the Issues tab.'
     194              :       ),
     195              :       { duration: 5000 }
     196              :     )
     197              :   }
     198              : 
     199              :   if (isCloud) {
     200              :     void verifyAssetSupportedCandidates(enrichedCandidates, controller.signal)
     201              :       .then(() => {
     202              :         if (controller.signal.aborted) return
     203              :         surfaceActiveCandidates()
     204              :       })
     205              :       .catch(reportVerificationFailure)
     206              :     return { missingModels, confirmedCandidates }
     207              :   }
     208              : 
     209         3796 :   if (!confirmedCandidates.length && !hasDeferredCandidates) {
     210           35 :     surfaceActiveCandidates()
     211           35 :     return { missingModels, confirmedCandidates }
     212              :   }
     213              : 
     214              :   const verification = hasDeferredCandidates
     215              :     ? verifyAssetSupportedCandidates(enrichedCandidates, controller.signal)
     216              :     : Promise.resolve()
     217         3796 :   void verification
     218              :     .then(async () => {
     219         2748 :       if (controller.signal.aborted) return
     220              :       const hasActiveMissing = enrichedCandidates.some((c) =>
     221              :         isMissingCandidateActive(graph, c)
     222              :       )
     223         2748 :       if (!hasActiveMissing) {
     224            1 :         surfaceActiveCandidates()
     225            1 :         return
     226              :       }
     227         2747 :       const verifiedDownloadableCandidates = enrichedCandidates
     228              :         .filter((c) => c.isMissing === true)
     229              :         .filter(hasDownloadMetadata)
     230         2747 :       const missingModelMetadata =
     231              :         import('@/platform/workflowErrors/missingModel/missingModelMetadata')
     232         2747 :       void Promise.allSettled(
     233              :         verifiedDownloadableCandidates.map(async (c) => {
     234         2705 :           const { fetchAndStoreModelMetadata } = await missingModelMetadata
     235         2679 :           await fetchAndStoreModelMetadata(
     236              :             c.url,
     237              :             missingModelStore,
     238              :             controller.signal
     239              :           )
     240              :         })
     241              :       )
     242         2747 :       await api
     243              :         .getFolderPaths()
     244              :         .then((paths) => {
     245         2719 :           if (controller.signal.aborted) return
     246         2681 :           missingModelStore.setFolderPaths(paths)
     247              :         })
     248              :         .catch((err) => {
     249            0 :           console.warn(
     250              :             '[Missing Model Pipeline] Failed to fetch folder paths:',
     251              :             err
     252              :           )
     253              :         })
     254              :         .finally(() => {
     255         2719 :           if (controller.signal.aborted) return
     256         2681 :           surfaceActiveCandidates()
     257              :         })
     258              :     })
     259              :     .catch(reportVerificationFailure)
     260              : 
     261         3796 :   return { missingModels, confirmedCandidates }
     262              : }
     263              : 
     264           11 : export async function refreshMissingModelPipeline({
     265              :   graph,
     266              :   reloadNodeDefs,
     267              :   missingModelStore,
     268              :   silent = true
     269              : }: RefreshMissingModelPipelineOptions): Promise<MissingModelPipelineResult> {
     270           11 :   await reloadNodeDefs?.()
     271           11 :   const graphData: MissingModelWorkflowData = graph.serialize()
     272           11 :   const activeWorkflowState =
     273              :     useWorkspaceStore().workflow.activeWorkflow?.activeState
     274           11 :   const currentModelMetadata = getCurrentMissingModelMetadata(missingModelStore)
     275           11 :   const models = activeWorkflowState?.models?.length
     276              :     ? activeWorkflowState.models
     277              :     : currentModelMetadata
     278              : 
     279           11 :   return runMissingModelPipeline({
     280              :     graph,
     281              :     graphData: models.length ? { ...graphData, models } : graphData,
     282              :     missingModelStore,
     283              :     silent
     284              :   })
     285              : }
        

Generated by: LCOV version 2.0-1