LCOV - code coverage report
Current view: top level - src/extensions/core - agentPanel.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 62.9 % 205 129
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { whenever } from '@vueuse/core'
       2              : import { storeToRefs } from 'pinia'
       3              : import { computed, ref, watch } from 'vue'
       4              : 
       5              : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
       6              : import { useOnboardingTourStore } from '@/platform/onboarding/onboardingTourStore'
       7              : import { useTelemetry } from '@comfyorg/telemetry'
       8              : import { reportError } from '@comfyorg/telemetry/reportError'
       9              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
      10              : import type {
      11              :   AgentConsentNotOfferedReason,
      12              :   AgentConsentOfferExit,
      13              :   AgentConsentOfferStage
      14              : } from '@comfyorg/telemetry/types'
      15              : import { useFirstRunEntry } from '@/renderer/extensions/firstRunTour/gettingStarted/firstRunEntry'
      16              : import {
      17              :   CONSENT_DIALOG_KEY,
      18              :   useAgentConsent
      19              : } from '@/workbench/extensions/agent/composables/agent/useAgentConsent'
      20              : import { registerWorkflowTabActivityTracker } from '@/workbench/extensions/agent/services/agent/workflowTabActivityTracker'
      21              : import { useAgentConsentStore } from '@/workbench/extensions/agent/stores/agent/agentConsentStore'
      22              : import { useAgentConsentIntroductionStore } from '@/workbench/extensions/agent/stores/agent/agentConsentIntroductionStore'
      23              : import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore'
      24              : import {
      25              :   notifyRestoreMintersAfterGraphConfigure,
      26              :   notifyRestoreMintersBeforeGraphLoad,
      27              :   notifyRestoreMintersGraphLoadError
      28              : } from '@/workbench/extensions/agent/crdt/restoreOpMinter'
      29              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
      30              : import { useExtensionService } from '@/platform/extensions/services/extensionService'
      31              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
      32              : import {
      33              :   clearPendingSignInAccept,
      34              :   hasPendingSignInAccept,
      35              :   takePendingSignInAccept
      36              : } from '@/workbench/extensions/agent/services/agent/pendingSignInAccept'
      37              : import { useAgentComposerStore } from '@/workbench/extensions/agent/stores/agent/agentComposerStore'
      38              : import { selectedNodeKey } from '@/workbench/extensions/agent/composables/agent/useCanvasSelection'
      39              : import { useDialogStore } from '@/stores/dialogStore'
      40              : import { getNodeByLocatorId } from '@/utils/graphTraversalUtil'
      41              : 
      42         2355 : const ACTIVATION_VISITS_STORAGE_KEY = 'Comfy.AgentPanel.activationVisits'
      43         2355 : const AUTOMATIC_ACTIVATION_VISITS = 2
      44              : 
      45            2 : function claimAutomaticActivationVisit(): boolean {
      46            2 :   try {
      47            2 :     const storedVisits = Number(
      48              :       localStorage.getItem(ACTIVATION_VISITS_STORAGE_KEY) ?? 0
      49              :     )
      50            2 :     const priorVisits =
      51              :       Number.isSafeInteger(storedVisits) && storedVisits >= 0 ? storedVisits : 0
      52            2 :     if (priorVisits >= AUTOMATIC_ACTIVATION_VISITS) return false
      53              : 
      54            1 :     localStorage.setItem(ACTIVATION_VISITS_STORAGE_KEY, String(priorVisits + 1))
      55            1 :     return true
      56              :   } catch {
      57            0 :     return false
      58              :   }
      59              : }
      60              : 
      61         2355 : function automaticActivationVisitsComplete(): boolean {
      62         2355 :   try {
      63         2355 :     const storedVisits = Number(
      64              :       localStorage.getItem(ACTIVATION_VISITS_STORAGE_KEY) ?? 0
      65              :     )
      66         2355 :     return Number.isSafeInteger(storedVisits)
      67              :       ? storedVisits >= AUTOMATIC_ACTIVATION_VISITS
      68              :       : false
      69              :   } catch {
      70              :     // Storage failures must not turn into an unbounded automatic opener.
      71            0 :     return true
      72              :   }
      73              : }
      74              : 
      75         2355 : let registered = false
      76              : 
      77         2355 : export function registerAgentPanelExtension(): void {
      78         2355 :   if (registered) return
      79              : 
      80         2355 :   useExtensionService().registerExtension({
      81              :     name: 'Comfy.AgentPanel',
      82              :     beforeLoadGraph() {
      83         3903 :       notifyRestoreMintersBeforeGraphLoad()
      84         3903 :       useCanvasStore().stopNodePicking()
      85              :     },
      86              :     afterLoadGraph(app) {
      87         3897 :       const composer = useAgentComposerStore()
      88         3897 :       if (composer.nodeScope !== useWorkflowStore().activeWorkflow?.instanceId)
      89         3896 :         return
      90            1 :       composer.setNodes(
      91              :         composer.nodes.flatMap((reference) => {
      92            0 :           const node = getNodeByLocatorId(
      93              :             app.rootGraph,
      94              :             selectedNodeKey(reference)
      95              :           )
      96            0 :           return node ? [{ ...reference, title: node.title }] : []
      97              :         })
      98              :       )
      99              :     },
     100              :     onGraphLoadError() {
     101            6 :       notifyRestoreMintersGraphLoadError()
     102              :     },
     103              :     afterConfigureGraph() {
     104         3897 :       notifyRestoreMintersAfterGraphConfigure()
     105              :     },
     106              :     setup() {
     107         2355 :       const agentPanelStore = useAgentPanelStore()
     108         2355 :       const consentStore = useAgentConsentStore()
     109         2355 :       const introductionStore = useAgentConsentIntroductionStore()
     110         2355 :       const { enabled } = storeToRefs(agentPanelStore)
     111         2355 :       const workspaceStore = useTeamWorkspaceStore()
     112         2355 :       const { resolvedUserInfo, isLoggedIn, isAuthInitialized } =
     113              :         useCurrentUser()
     114         2355 :       const { withConsent, resumeAcceptAfterReload } = useAgentConsent()
     115         2355 :       const { firstRunHoldsScreen, whenStartupDecided } = useFirstRunEntry()
     116         2355 :       const onboardingTourStore = useOnboardingTourStore()
     117         2355 :       const dialogStore = useDialogStore()
     118         2355 :       registerWorkflowTabActivityTracker(enabled)
     119              : 
     120         2355 :       watch(
     121              :         () => consentStore.accepted,
     122              :         (value) => {
     123         2355 :           agentPanelStore.consentAccepted = value
     124              :         },
     125              :         { immediate: true, flush: 'sync' }
     126              :       )
     127              : 
     128         2355 :       const screenBusyReason = (): 'tour_active' | 'dialog_open' | null =>
     129              :         onboardingTourStore.activeTour !== null
     130              :           ? 'tour_active'
     131              :           : dialogStore.dialogStack.length > 0
     132              :             ? 'dialog_open'
     133              :             : null
     134         2355 :       const screenHolder = (): AgentConsentNotOfferedReason | null =>
     135              :         firstRunHoldsScreen.value ? 'first_run_screen' : screenBusyReason()
     136         2355 :       const screenIsClear = computed(() => screenHolder() === null)
     137              : 
     138         2355 :       const reportedWithheld = new Set<string>()
     139         2355 :       const withholdOffer = (reason: AgentConsentNotOfferedReason): void => {
     140            0 :         if (!introductionOwed()) return
     141            0 :         const key = `${introductionStore.scope}:${reason}`
     142            0 :         if (reportedWithheld.has(key)) return
     143            0 :         reportedWithheld.add(key)
     144            0 :         useTelemetry()?.trackAgentConsentNotOffered({ reason })
     145              :       }
     146              : 
     147         2355 :       const offerHeld = ref(false)
     148         2355 :       const holdOffer = (reason: AgentConsentNotOfferedReason): void => {
     149            0 :         withholdOffer(reason)
     150            0 :         offerHeld.value = true
     151              :       }
     152              : 
     153         2355 :       const dropHold = (): void => {
     154            2 :         offerHeld.value = false
     155              :       }
     156              : 
     157         2355 :       const reportedExits = new Set<string>()
     158         2355 :       const reportOfferExit = (
     159              :         exit: AgentConsentOfferExit,
     160              :         stage: AgentConsentOfferStage
     161              :       ): void => {
     162            1 :         const retryArmed = offerHeld.value
     163            1 :         const key = `${stage}:${exit}:${retryArmed}`
     164            1 :         if (reportedExits.has(key)) return
     165            1 :         reportedExits.add(key)
     166            1 :         useTelemetry()?.trackAgentConsentOfferExited({
     167              :           exit,
     168              :           stage,
     169              :           retry_armed: retryArmed
     170              :         })
     171              :       }
     172              : 
     173         2355 :       const consentCardSeenIn = new Set<string>()
     174         2355 :       const launchInActivationWindow = !automaticActivationVisitsComplete()
     175         2355 :       whenever(
     176              :         () => dialogStore.isDialogOpen(CONSENT_DIALOG_KEY),
     177              :         () => {
     178            1 :           const { scope } = introductionStore
     179            1 :           if (scope) consentCardSeenIn.add(scope)
     180              :         }
     181              :       )
     182              : 
     183         2355 :       let signedInThisPage = false
     184         2355 :       watch(
     185              :         isLoggedIn,
     186              :         (loggedIn) => {
     187         2356 :           if (loggedIn) signedInThisPage = true
     188              :         },
     189              :         { immediate: true, flush: 'sync' }
     190              :       )
     191              : 
     192         2355 :       const sessionEnded = (): boolean => signedInThisPage && !isLoggedIn.value
     193              : 
     194            2 :       function introductionOwed(): boolean {
     195            2 :         const { scope } = introductionStore
     196            2 :         return scope !== null && !introductionStore.hasResponded(scope)
     197              :       }
     198         2355 :       const offerEligible = (): boolean =>
     199              :         agentPanelStore.enabled &&
     200              :         isAuthInitialized.value &&
     201              :         !sessionEnded() &&
     202              :         !introductionStore.isContinuingSignIn &&
     203              :         introductionOwed() &&
     204              :         !consentStore.isChecking &&
     205              :         !consentStore.accepted
     206         2355 :       const offerIneligibility = (): AgentConsentOfferExit | null =>
     207              :         consentStore.isChecking
     208              :           ? 'consent_unresolved'
     209              :           : consentStore.accepted
     210              :             ? 'consent_already_accepted'
     211              :             : null
     212              : 
     213         2355 :       let autoShowInFlight = false
     214              : 
     215            0 :       function finishAutomaticOffer(offeredIdentity: string | null): void {
     216            0 :         autoShowInFlight = false
     217            0 :         if (consentStore.identity !== offeredIdentity) loadConsentIfEligible()
     218            0 :         else if (offerHeld.value && screenIsClear.value) loadConsentIfEligible()
     219              :       }
     220              : 
     221         2355 :       const offerConsentUnprompted = (): void => {
     222            0 :         if (autoShowInFlight) {
     223            0 :           reportOfferExit('offer_in_flight', 'offer')
     224            0 :           return
     225              :         }
     226            0 :         if (!offerEligible()) {
     227            0 :           const ineligible = offerIneligibility()
     228            0 :           if (ineligible) reportOfferExit(ineligible, 'offer')
     229            0 :           if (consentStore.accepted) dropHold()
     230            0 :           return
     231              :         }
     232            0 :         const { scope } = introductionStore
     233            0 :         if (scope && consentCardSeenIn.has(scope)) {
     234            0 :           reportOfferExit('card_already_seen', 'offer')
     235            0 :           dropHold()
     236            0 :           return
     237              :         }
     238            0 :         const held = screenHolder()
     239            0 :         if (held) {
     240            0 :           holdOffer(held)
     241            0 :           return
     242              :         }
     243              : 
     244            0 :         const offeredIdentity = consentStore.identity
     245            0 :         autoShowInFlight = true
     246            0 :         dropHold()
     247            0 :         agentPanelStore.suppressRestoredOpen()
     248            0 :         let shownOverClosedPanel = false
     249            0 :         void withConsent(
     250              :           'first_load',
     251              :           () => {
     252            0 :             if (!agentPanelStore.enabled || agentPanelStore.isOpen) return
     253            0 :             if (automaticActivationVisitsComplete() && !shownOverClosedPanel)
     254            0 :               return
     255            0 :             agentPanelStore.open('automatic_consent')
     256              :           },
     257              :           {
     258              :             canShow: () => {
     259            0 :               const held = screenHolder()
     260            0 :               if (held) holdOffer(held)
     261            0 :               const showing =
     262              :                 held === null && agentPanelStore.enabled && !sessionEnded()
     263            0 :               if (showing) shownOverClosedPanel = !agentPanelStore.isOpen
     264            0 :               return showing
     265              :             }
     266              :           }
     267              :         ).finally(() => finishAutomaticOffer(offeredIdentity))
     268              :       }
     269              : 
     270            2 :       function prepareStartupPanel(): boolean {
     271            2 :         if (!introductionOwed()) {
     272            1 :           dropHold()
     273            1 :           return false
     274              :         }
     275            1 :         if (!launchInActivationWindow) {
     276            1 :           reportOfferExit('activation_window_complete', 'startup')
     277            1 :           dropHold()
     278            1 :           return false
     279              :         }
     280            0 :         if (introductionStore.canRecordResponse()) return true
     281            0 :         withholdOffer('storage_unavailable')
     282            0 :         dropHold()
     283            0 :         return false
     284              :       }
     285              : 
     286         2355 :       const offerWhenStartupDecided = (): void => {
     287            2 :         whenStartupDecided()
     288              :           .then((decided) => {
     289            2 :             if (!decided) {
     290            0 :               if (offerEligible()) withholdOffer('boot_undecided')
     291              :               else {
     292            0 :                 const ineligible = offerIneligibility()
     293            0 :                 if (ineligible) reportOfferExit(ineligible, 'startup')
     294              :               }
     295            0 :               return
     296              :             }
     297            2 :             if (!prepareStartupPanel()) return
     298            0 :             offerConsentUnprompted()
     299              :           })
     300              :           .catch((error: unknown) => {
     301            0 :             reportOfferExit('startup_probe_failed', 'startup')
     302            0 :             reportError(error, {
     303              :               surface: 'agent',
     304              :               errorType: 'agent_consent_auto_offer_failure'
     305              :             })
     306              :           })
     307              :       }
     308              : 
     309         2355 :       let activationPending = false
     310         2355 :       let activationOffered = false
     311         2355 :       const openWhenStartupDecided = (): void => {
     312         2359 :         if (!agentPanelStore.enabled || activationPending || activationOffered)
     313         2357 :           return
     314            2 :         activationPending = true
     315            2 :         whenStartupDecided()
     316              :           .then((decided) => {
     317            2 :             if (decided && agentPanelStore.enabled) {
     318            2 :               activationOffered = true
     319            2 :               if (claimAutomaticActivationVisit() && !agentPanelStore.isOpen)
     320            1 :                 agentPanelStore.open('activation')
     321              :             }
     322              :           })
     323              :           .catch((error: unknown) => {
     324            0 :             reportError(error, {
     325              :               surface: 'agent',
     326              :               errorType: 'agent_panel_activation_failure'
     327              :             })
     328              :           })
     329              :           .finally(() => {
     330            2 :             activationPending = false
     331              :           })
     332              :       }
     333              : 
     334         2355 :       let resumingAccept = false
     335         2355 :       let pendingAcceptChecked = false
     336         2355 :       let pendingAcceptAccount: string | undefined
     337         2373 :       function resumePendingAccept(): boolean {
     338         2373 :         if (pendingAcceptChecked) return false
     339         2367 :         if (!isLoggedIn.value) {
     340         2339 :           pendingAcceptChecked = true
     341         2339 :           clearPendingSignInAccept()
     342         2339 :           return false
     343              :         }
     344         2373 :         const accountId = resolvedUserInfo.value?.id
     345         2373 :         pendingAcceptAccount ??= accountId
     346         2373 :         if (!accountId || workspaceStore.isSwitching)
     347            0 :           return hasPendingSignInAccept()
     348           16 :         pendingAcceptChecked = true
     349           16 :         const draft = takePendingSignInAccept()
     350         2373 :         if (draft === null || accountId !== pendingAcceptAccount) return false
     351            0 :         resumingAccept = true
     352            0 :         void resumeAcceptAfterReload(draft).finally(() => {
     353            0 :           resumingAccept = false
     354            0 :           loadConsentIfEligible()
     355              :         })
     356            0 :         return true
     357              :       }
     358              : 
     359         2355 :       const loadConsentIfEligible = (): void => {
     360         2373 :         if (!isAuthInitialized.value) return
     361         2373 :         if (resumingAccept || resumePendingAccept()) return
     362         2373 :         if (!agentPanelStore.enabled) return
     363            2 :         if (!isLoggedIn.value) {
     364            2 :           if (!sessionEnded()) offerWhenStartupDecided()
     365            2 :           return
     366              :         }
     367            0 :         if (!resolvedUserInfo.value) return
     368            0 :         const identity = consentStore.identity
     369            0 :         void consentStore
     370              :           .load()
     371              :           .then((isAccepted) => {
     372            0 :             if (identity !== consentStore.identity) return
     373            0 :             if (isAccepted) {
     374            0 :               reportOfferExit('consent_already_accepted', 'load')
     375            0 :               dropHold()
     376            0 :             } else offerWhenStartupDecided()
     377              :           })
     378              :           .catch((error: unknown) => {
     379            0 :             reportOfferExit('consent_read_failed', 'load')
     380            0 :             reportError(error, {
     381              :               surface: 'agent',
     382              :               errorType: 'agent_consent_setting_load_failure'
     383              :             })
     384              :           })
     385              :       }
     386         2355 :       watch(
     387              :         [
     388              :           () => resolvedUserInfo.value?.id,
     389              :           () => consentStore.identity,
     390              :           () => isAuthInitialized.value
     391              :         ],
     392              :         loadConsentIfEligible
     393              :       )
     394              : 
     395         2355 :       whenever(
     396              :         () => offerHeld.value && screenIsClear.value,
     397              :         () => {
     398            0 :           loadConsentIfEligible()
     399              :         }
     400              :       )
     401         2355 :       setupFlagGate(loadConsentIfEligible, openWhenStartupDecided)
     402              :     }
     403              :   })
     404         2355 :   registered = true
     405              : }
     406              : 
     407         2355 : function setupFlagGate(
     408              :   loadConsentIfEligible: () => void,
     409              :   openWhenStartupDecided: () => void
     410              : ): void {
     411         2355 :   const agentPanelStore = useAgentPanelStore()
     412              :   const devForcedOn =
     413              :     import.meta.env.MODE === 'development' &&
     414              :     import.meta.env.VITE_AGENT_STANDALONE === 'true'
     415              : 
     416         2355 :   watch(
     417              :     () =>
     418              :       [
     419              :         devForcedOn || agentPanelStore.comfyAgent?.state === 'ready',
     420              :         agentPanelStore.comfyAgent
     421              :       ] as const,
     422              :     ([enabled]) => {
     423         2359 :       agentPanelStore.enabled = enabled
     424         2359 :       loadConsentIfEligible()
     425         2359 :       openWhenStartupDecided()
     426         2359 :       if (!enabled) useCanvasStore().stopNodePicking()
     427              :     },
     428              :     { immediate: true }
     429              :   )
     430         2355 :   watch(
     431              :     () => devForcedOn || agentPanelStore.flagsSettled,
     432              :     (settled) => {
     433         4708 :       agentPanelStore.gateSettled = settled
     434              :     },
     435              :     { immediate: true }
     436              :   )
     437              : }
        

Generated by: LCOV version 2.0-1