LCOV - code coverage report
Current view: top level - src/platform/workflow/deploy/composables - useAgentHandoff.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 51.0 % 49 25
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { downloadBlob } from '@/base/common/downloadUtil'
       2              : import { useToast } from '@/components/ui/toast/toastStore'
       3              : import { useCopyToClipboard } from '@/composables/useCopyToClipboard'
       4              : import { t } from '@/i18n'
       5              : import type { Distribution } from '@/platform/distribution/types'
       6              : import { DISTRIBUTION } from '@/platform/distribution/types'
       7              : import { reportError } from '@comfyorg/telemetry/reportError'
       8              : import {
       9              :   buildAgentHandoffDocument,
      10              :   handoffFileName
      11              : } from '@/platform/workflow/deploy/utils/agentHandoff'
      12              : import { openAgentApp } from '@/platform/workflow/deploy/utils/agentApps'
      13              : import type { AgentApp } from '@/platform/workflow/deploy/utils/agentApps'
      14              : import { deriveBuildInputs } from '@/platform/workflow/deploy/utils/buildInputs'
      15              : import type { BuildInputs } from '@/platform/workflow/deploy/utils/buildInputs'
      16              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
      17              : import { useModelToNodeStore } from '@/stores/modelToNodeStore'
      18              : 
      19            1 : const UNTITLED_WORKFLOW_NAME = 'workflow'
      20              : 
      21            1 : export function useAgentHandoff(distribution: Distribution = DISTRIBUTION) {
      22            1 :   const workflowStore = useWorkflowStore()
      23            1 :   const toast = useToast()
      24            1 :   const modelToNodeStore = useModelToNodeStore()
      25            1 :   const { copyToClipboard } = useCopyToClipboard()
      26              : 
      27            1 :   function modelInputsByLoader(): ReadonlyMap<
      28              :     string,
      29              :     readonly (string | RegExp)[]
      30              :   > {
      31            1 :     modelToNodeStore.registerDefaults()
      32            1 :     const inputs = new Map<string, (string | RegExp)[]>()
      33            1 :     for (const providers of Object.values(modelToNodeStore.modelToNodeMap)) {
      34           17 :       for (const { nodeDef, key } of providers ?? []) {
      35           21 :         if (!key) continue
      36           21 :         const keys = inputs.get(nodeDef.name) ?? []
      37           21 :         if (!keys.includes(key)) inputs.set(nodeDef.name, [...keys, key])
      38              :       }
      39              :     }
      40            1 :     return inputs
      41              :   }
      42              : 
      43            1 :   function snapshot() {
      44            1 :     const workflow = workflowStore.activeWorkflow
      45            1 :     workflow?.changeTracker.prepareForSave()
      46            1 :     const name =
      47              :       workflow?.filename.replace(/\.json$/i, '') || UNTITLED_WORKFLOW_NAME
      48            1 :     const graph = workflow?.activeState ?? {}
      49            1 :     const loaders = modelInputsByLoader()
      50            1 :     return {
      51              :       graph,
      52              :       inputs: deriveBuildInputs(
      53              :         graph,
      54              :         { name, fileName: handoffFileName(name) },
      55              :         (nodeType) => loaders.get(nodeType) ?? []
      56              :       )
      57              :     }
      58              :   }
      59              : 
      60            1 :   function captureInputs(): BuildInputs {
      61            1 :     return snapshot().inputs
      62              :   }
      63              : 
      64            0 :   function prepareHandoff() {
      65            0 :     const { graph, inputs } = snapshot()
      66            0 :     return {
      67              :       brief: buildAgentHandoffDocument({ distribution, inputs }),
      68              :       downloadWorkflow() {
      69            0 :         if (distribution !== 'cloud') return
      70            0 :         downloadBlob(
      71              :           inputs.workflowFileName,
      72              :           new Blob([JSON.stringify(graph, null, 2)], {
      73              :             type: 'application/json'
      74              :           })
      75              :         )
      76              :       }
      77              :     }
      78              :   }
      79              : 
      80            0 :   function reportHandoffFailure(error: unknown, errorType: string) {
      81            0 :     reportError(error, { errorType, surface: 'platform' })
      82            0 :     toast.error(t('g.error'), {
      83              :       description: t('deployToComfyApi.briefFailed'),
      84              :       duration: 5000
      85              :     })
      86              :   }
      87              : 
      88            0 :   async function copyBrief(): Promise<boolean> {
      89            0 :     try {
      90            0 :       const { brief, downloadWorkflow } = prepareHandoff()
      91            0 :       if (!(await copyToClipboard(brief, { toastOnSuccess: false }))) {
      92            0 :         reportError(new Error('The clipboard refused the brief'), {
      93              :           errorType: 'error_copying_deploy_agent_brief',
      94              :           surface: 'platform'
      95              :         })
      96            0 :         return false
      97              :       }
      98            0 :       downloadWorkflow()
      99            0 :       return true
     100              :     } catch (error) {
     101            0 :       reportHandoffFailure(error, 'error_copying_deploy_agent_brief')
     102            0 :       return false
     103              :     }
     104              :   }
     105              : 
     106            0 :   function openBriefIn(app: AgentApp): void {
     107            0 :     try {
     108            0 :       const { brief, downloadWorkflow } = prepareHandoff()
     109            0 :       downloadWorkflow()
     110            0 :       openAgentApp(app, brief)
     111              :     } catch (error) {
     112            0 :       reportHandoffFailure(error, 'error_opening_deploy_agent_brief')
     113              :     }
     114              :   }
     115              : 
     116            1 :   return { captureInputs, copyBrief, openBriefIn }
     117              : }
        

Generated by: LCOV version 2.0-1