LCOV - code coverage report
Current view: top level - src/workbench/extensions/agent - AgentPanelRoot.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 26.7 % 896 239
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : <script setup lang="ts">
       2              : import type { AgentErrorContext } from '@/platform/workflowErrors/agentErrorContext'
       3              : import './agentPanel.css'
       4              : 
       5              : import type { AgentAnswerRequest } from '@comfyorg/ingest-types'
       6              : import { useClipboard, useEventListener } from '@vueuse/core'
       7              : import { storeToRefs } from 'pinia'
       8              : import {
       9              :   computed,
      10              :   defineAsyncComponent,
      11              :   nextTick,
      12              :   onBeforeUnmount,
      13              :   onMounted,
      14              :   provide,
      15              :   ref,
      16              :   watch
      17              : } from 'vue'
      18              : import { useI18n } from 'vue-i18n'
      19              : 
      20              : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
      21              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
      22              : import { useTelemetry } from '@comfyorg/telemetry'
      23              : import { reportError } from '@comfyorg/telemetry/reportError'
      24              : import type {
      25              :   AgentCoachReleaseReason,
      26              :   AgentErrorMetadata,
      27              :   AgentMessageSentMetadata,
      28              :   AgentPaywallSurface,
      29              :   AgentRunApprovalDecision,
      30              :   AgentStopMethod
      31              : } from '@comfyorg/telemetry/types'
      32              : import { useSettingStore } from '@/platform/settings/settingStore'
      33              : import { useDialogStore } from '@/stores/dialogStore'
      34              : import { hasUnsyncedHumanOps } from '@/workbench/extensions/agent/crdt/unsyncedHumanOps'
      35              : import { formatWorkflowSyncErrorDetail } from '@/workbench/extensions/agent/crdt/workflowSyncErrorDetail'
      36              : import {
      37              :   readWorkflowDraftLineage,
      38              :   useWorkflowService,
      39              :   writeWorkflowDraftLineage
      40              : } from '@/platform/workflow/core/services/workflowService'
      41              : import type { ComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow'
      42              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
      43              : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
      44              : import { useAppMode } from '@/composables/useAppMode'
      45              : import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema'
      46              : import { findServerPreviewUrl } from '@/platform/assets/utils/assetPreviewUtil'
      47              : import { fetchDroppedAsset, getDroppedAsset } from '@/utils/eventUtils'
      48              : import { isHdrImageFilename } from '@/utils/hdrFormatUtil'
      49              : import type { DroppedAsset } from '@/utils/eventUtils'
      50              : import { useAssetsStore } from '@/stores/assetsStore'
      51              : import {
      52              :   AGENT_ATTACH_ACCEPT,
      53              :   agentAttachRefVerdict,
      54              :   agentAttachVerdict,
      55              :   isValidAgentAttachment
      56              : } from './utils/attachableFiles'
      57              : import type { AgentAttachVerdict } from './utils/attachableFiles'
      58              : import { getNodeByLocatorId } from '@/utils/graphTraversalUtil'
      59              : // oxlint-disable-next-line comfy/no-restricted-paths
      60              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
      61              : import type { MinimapDecorationLayer } from '@/platform/canvas/minimapDecorationRegistry'
      62              : import { registerMinimapDecorationLayer } from '@/platform/canvas/minimapDecorationRegistry'
      63              : // The composition root injects the renderer-owned layout port; follower core
      64              : // stays independent of renderer and LiteGraph runtime values.
      65              : // oxlint-disable-next-line comfy/no-restricted-paths
      66              : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
      67              : 
      68              : import { api } from '@/scripts/api'
      69              : import { app } from '@/scripts/app'
      70              : import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema'
      71              : import { blankGraph } from '@/scripts/defaultGraph'
      72              : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
      73              : import { useExecutionStore } from '@/stores/executionStore'
      74              : import { useWorkflowTabActivityStore } from '@/stores/workflowTabActivityStore'
      75              : import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore'
      76              : import { isLGraphNode } from '@/utils/litegraphUtil'
      77              : import { useToast } from '@/components/ui/toast/toastStore'
      78              : import {
      79              :   toOwningGraphId,
      80              :   toRootGraphId
      81              : } from '@comfyorg/graph-model/graphScopeId'
      82              : import type { RootGraphId } from '@comfyorg/graph-model/graphScopeId'
      83              : import { isCloud } from '@/platform/distribution/types'
      84              : import { parseNodeId } from '@comfyorg/graph-model/nodeId'
      85              : import { parseNodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
      86              : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
      87              : import { useAccountPreconditionDialog } from '@/platform/billing/subscription/composables/useAccountPreconditionDialog'
      88              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
      89              : import { usePlanCreditLine } from '@/platform/billing/composables/useTopUpPlanCreditCopy'
      90              : import { useOnboardingTourStore } from '@/platform/onboarding/onboardingTourStore'
      91              : import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI'
      92              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
      93              : import {
      94              :   adoptSharedOnboardingFlag,
      95              :   hasSeenCoach,
      96              :   resetCoach,
      97              :   scopedOnboardingKey,
      98              :   trackCoachDeferral
      99              : } from './composables/agent/useOnboarding'
     100              : 
     101              : import { useFreeUsePlacement } from './experiments/freeUsePlacement'
     102              : import AgentPanel from './components/agent/AgentPanel.vue'
     103              : import AgentCreditTransitionNotice from './components/agent/AgentCreditTransitionNotice.vue'
     104              : import AgentHostFenceNotice from './components/agent/AgentHostFenceNotice.vue'
     105              : import RunNoticeBanner from './components/agent/RunNoticeBanner.vue'
     106              : import { agentBoundWorkflowIdKey } from './components/agent/agentBoundWorkflowId'
     107              : import AgentGraphActivityBar from './components/AgentGraphActivityBar.vue'
     108              : import OnboardingCoach from './components/agent/OnboardingCoach.vue'
     109              : import {
     110              :   MAX_ATTACHMENT_BYTES,
     111              :   MAX_TURN_ATTACHMENTS,
     112              :   useAttachment
     113              : } from './composables/agent/useAttachment'
     114              : import type { ActiveTab } from './types/activeTab'
     115              : import type { SelectedNode } from './composables/agent/useCanvasSelection'
     116              : import {
     117              :   selectedNodeKey,
     118              :   useCanvasSelection
     119              : } from './composables/agent/useCanvasSelection'
     120              : import type {
     121              :   AgentActiveTabData,
     122              :   AgentWsEvent,
     123              :   TurnId
     124              : } from './schemas/agentApiSchema'
     125              : import { toTurnId } from './schemas/agentApiSchema'
     126              : import type {
     127              :   ConversationEntry,
     128              :   TurnTab
     129              : } from './stores/agent/agentConversationStore'
     130              : import { useAgentConversationStore } from './stores/agent/agentConversationStore'
     131              : import type {
     132              :   TurnOrigin,
     133              :   WorkflowTurnContext
     134              : } from './composables/agent/useAgentSession'
     135              : import { useAgentTabSignals } from './composables/agent/useAgentTabSignals'
     136              : import type { CoachStep } from './composables/agent/useOnboarding'
     137              : import { useBillingIdentity } from './composables/useBillingIdentity'
     138              : import { useAgentConsent } from './composables/agent/useAgentConsent'
     139              : import { useAgentWorkflowResolver } from './composables/agent/useAgentWorkflowResolver'
     140              : import { useAgentWorkflowSelection } from './composables/agent/useAgentWorkflowSelection'
     141              : import {
     142              :   isRetryableRequestFailure,
     143              :   trackAgentError,
     144              :   useAgentSession
     145              : } from './composables/agent/useAgentSession'
     146              : import { useAgentDraftSubmission } from './composables/agent/useAgentDraftSubmission'
     147              : import { useAgentSendGates } from './composables/agent/useAgentSendGates'
     148              : import type { CoachHoldEnd } from './composables/agent/useAgentSendGates'
     149              : import { useAgentWorkflowTabBindingStore } from './stores/agent/agentWorkflowTabBindingStore'
     150              : import {
     151              :   agentRequestFailureFingerprint,
     152              :   describeAgentRequestFailure
     153              : } from './services/agent/agentRequestFailure'
     154              : import {
     155              :   createAgentRestClient,
     156              :   isLoopbackOnlyRefusal
     157              : } from './services/agent/agentRestClient'
     158              : import { useAgentChatHistoryRefresh } from './composables/agent/useAgentChatHistoryRefresh'
     159              : import { useAgentWorkflowChatFollow } from './composables/agent/useAgentWorkflowChatFollow'
     160              : import { ensureSignedIn, hasAgentCredential } from './services/agent/agentAuth'
     161              : import type { DraftSnapshot } from './services/agent/agentRestClient'
     162              : import type { AgentPaywallAction } from './services/agent/agentPaywallPresentation'
     163              : import {
     164              :   DEFAULT_AGENT_PAYWALL_PRESENTATION,
     165              :   resolveAgentPaywallPresentation,
     166              :   toAgentPaywallCta,
     167              :   toAgentPaywallReason
     168              : } from './services/agent/agentPaywallPresentation'
     169              : import { isCreditsStopPart } from './services/agent/agentMessageParts'
     170              : import type { CanvasConfirmation } from './services/agent/agentEventTransport'
     171              : import { toolCanClaimCanvasContent } from './services/agent/agentToolClassification'
     172              : import { areAgentWorkflowIdsEquivalent } from './services/agent/agentWorkflowId'
     173              : import { resolveAgentIdentity } from './services/agent/agentIdentity'
     174              : import { desktopHostUser } from '@/platform/auth/desktopHost/desktopHostSession'
     175              : import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore'
     176              : import { useAuthStore } from '@/stores/authStore'
     177              : import { toSocketIdentity, useAgentPageSession } from './agentPageSession'
     178              : import { useAgentChatHistoryStore } from './stores/agent/agentChatHistoryStore'
     179              : import { useAgentNodeDefinitionRefreshStore } from './stores/agent/agentNodeDefinitionRefreshStore'
     180              : import { agentMessageText } from './utils/agentMessageText'
     181              : import {
     182              :   deriveSessionTitle,
     183              :   withCurrentSessionTitle
     184              : } from './utils/sessionTitle'
     185              : import { useAgentComposerStore } from './stores/agent/agentComposerStore'
     186              : import { useAgentConsentStore } from './stores/agent/agentConsentStore'
     187              : import { isCreditTransitionNoticeShown } from './stores/agent/creditTransitionNoticeState'
     188              : import { useAgentPanelStore } from './stores/agent/agentPanelStore'
     189              : import { useAgentPanelRenderStore } from './stores/agent/agentPanelRenderStore'
     190              : import { useAgentGraphActivityStore } from './stores/agent/agentGraphActivityStore'
     191              : import {
     192              :   isCrdtDebugEnabled,
     193              :   resolveDebugPanelEnabled,
     194              :   startCrdtDebugGate
     195              : } from './crdt/crdtDebugGate'
     196              : import { attachAgentWriteOverwriteWatch } from './crdt/agentWriteOverwrites'
     197              : import { attachDocOpMinter } from './crdt/docOpMinter'
     198              : import { attachRestoreOpMinter } from './crdt/restoreOpMinter'
     199              : import { useAgentCrdtFollower } from './crdt/useAgentCrdtFollower'
     200              : 
     201            1 : const CrdtDevPanel = defineAsyncComponent(
     202              :   () => import('./crdt/CrdtDevPanel.vue')
     203              : )
     204              : 
     205            1 : const { t } = useI18n()
     206            1 : const toast = useToast()
     207            1 : let errorAttachRun = 0
     208            1 : onBeforeUnmount(() => {
     209            0 :   errorAttachRun++
     210              : })
     211            1 : const { open: openAccountPrecondition } = useAccountPreconditionDialog()
     212            1 : const { workspaceRole } = useWorkspaceUI()
     213            1 : const {
     214              :   subscription,
     215              :   tier: subscriptionTier,
     216              :   type: billingType,
     217              :   billingStatus,
     218              :   canAccessSubscriptionFeatures,
     219              :   fetchStatus: refreshBillingStatus
     220              : } = useBillingContext()
     221            1 : const conversationStore = useAgentConversationStore()
     222            1 : const history = useAgentChatHistoryStore()
     223            1 : const { accepted: consentAccepted, identity: consentIdentity } = storeToRefs(
     224              :   useAgentConsentStore()
     225              : )
     226            1 : watch(
     227              :   subscription,
     228              :   (currentSubscription) => {
     229            1 :     if (currentSubscription?.agentHasFunds) conversationStore.resolvePaywalls()
     230              :   },
     231              :   { immediate: true }
     232              : )
     233            1 : const {
     234              :   canTopUp,
     235              :   canSubscribeSelfServe,
     236              :   hasResolvedCapabilities,
     237              :   isReady: capabilityReadSettled,
     238              :   snapshotAuthoritative
     239              : } = useBillingCapabilities()
     240            1 : const planCreditLine = usePlanCreditLine()
     241            1 : const paywallPresentation = computed(() => {
     242              :   if (isCloud && !hasResolvedCapabilities.value && !canTopUp.value) {
     243              :     return DEFAULT_AGENT_PAYWALL_PRESENTATION
     244              :   }
     245            1 :   return resolveAgentPaywallPresentation({
     246              :     distribution: isCloud ? 'cloud' : 'local',
     247              :     role: workspaceRole.value,
     248              :     tier: subscriptionTier.value,
     249              :     canTopUp: canTopUp.value,
     250              :     canSubscribeSelfServe: canSubscribeSelfServe.value,
     251              :     planCreditLine: planCreditLine.value
     252              :   })
     253              : })
     254            1 : const sidebarTabStore = useSidebarTabStore()
     255            1 : const { isBuilderMode } = useAppMode()
     256              : 
     257            1 : const { resolvedUserInfo, userDisplayName } = useCurrentUser()
     258            1 : const userName = computed(
     259              :   () => userDisplayName.value?.trim().split(/\s+/)[0] || undefined
     260              : )
     261              : 
     262            1 : onBeforeUnmount(startCrdtDebugGate())
     263              : 
     264            1 : const rest = createAgentRestClient()
     265              : 
     266            1 : const authStore = useAuthStore()
     267            1 : const apiKeyAuthStore = useApiKeyAuthStore()
     268            1 : const teamWorkspaceStore = useTeamWorkspaceStore()
     269            1 : const { events, docTransport, onIdentityChange } = useAgentPageSession(() =>
     270              :   toSocketIdentity(
     271              :     authStore.userId,
     272              :     apiKeyAuthStore.currentUser?.id,
     273              :     teamWorkspaceStore.activeWorkspaceId,
     274              :     desktopHostUser.value?.workspaceId
     275              :   )
     276              : )
     277              : 
     278            1 : const agentIdentity = resolveAgentIdentity({
     279              :   getIdentity: () => rest.getIdentity(),
     280              :   onSocketOpen: (listener) => docTransport.onSocketOpen(listener),
     281              :   isRetryable: (error) => isRetryableRequestFailure(error, false),
     282              :   onFailure: (error) =>
     283              :     reportError(error, {
     284              :       surface: 'agent',
     285              :       errorType: 'failure_resolving_agent_identity',
     286              :       level: 'warning'
     287              :     })
     288              : })
     289            1 : onBeforeUnmount(() => agentIdentity.stop())
     290              : 
     291            1 : onBeforeUnmount(onIdentityChange(() => agentIdentity.reset()))
     292              : 
     293            0 : function onPaywallAction(
     294              :   action: AgentPaywallAction,
     295              :   surface: AgentPaywallSurface
     296              : ): void {
     297            0 :   useTelemetry()?.trackAgentPaywallCtaClicked({
     298              :     cta: toAgentPaywallCta(action),
     299              :     surface
     300              :   })
     301            0 :   if (action === 'addCredits') {
     302            0 :     if (canTopUp.value) {
     303            0 :       useTelemetry()?.trackAddApiCreditButtonClicked({
     304              :         source: 'agent_paywall'
     305              :       })
     306              :     }
     307            0 :     openAccountPrecondition('credits', { source: 'agent_paywall' })
     308            0 :     return
     309              :   }
     310            0 :   useTelemetry()?.trackSubscription('subscribe_clicked', {
     311              :     current_tier: subscriptionTier.value?.toLowerCase(),
     312              :     reason: 'agent_paywall'
     313              :   })
     314            0 :   openAccountPrecondition('subscription', { source: 'agent_paywall' })
     315              : }
     316              : 
     317            1 : const { messages: conversationMessages, entries: conversationEntries } =
     318              :   storeToRefs(conversationStore)
     319            1 : watch(
     320              :   () =>
     321              :     // Gated on the read having *settled*, which includes settling as
     322              :     // unavailable. `snapshotAuthoritative` is narrower — it excludes that
     323              :     // outage state — and gating on it stranded those sessions: the paywall
     324              :     // still renders, an owner still gets the fallback Add credits CTA, and its
     325              :     // click is still reported, so the funnel saw CTAs with no impression.
     326              :     capabilityReadSettled.value
     327              :       ? conversationMessages.value
     328              :           .filter((message) => message.parts.some(isCreditsStopPart))
     329              :           .map((message) => ({
     330              :             id: message.id,
     331              :             surface: message.parts.some((part) => part.type === 'paywall')
     332              :               ? ('refused_send' as const)
     333              :               : ('credits_exhausted' as const)
     334              :           }))
     335              :       : [],
     336              :   (creditsStops) => {
     337            1 :     const telemetry = useTelemetry()
     338            1 :     if (!telemetry) return
     339            0 :     for (const { id, surface } of creditsStops) {
     340            0 :       if (!conversationStore.claimPaywallImpression(id)) continue
     341            0 :       telemetry.trackAgentPaywallShown({
     342              :         // An unavailable read leaves `canTopUp` guessing true for an owner, so
     343              :         // the presentation would name a confident reason drawn from a fallback
     344              :         // rather than from capabilities. Report the impression as `unknown`.
     345              :         reason: snapshotAuthoritative.value
     346              :           ? toAgentPaywallReason(paywallPresentation.value)
     347              :           : 'unknown',
     348              :         surface
     349              :       })
     350              :     }
     351              :   },
     352              :   { immediate: true }
     353              : )
     354              : 
     355              : /**
     356              :  * The standing credit-exhaustion surface.
     357              :  *
     358              :  * The inline card above is reachable from exactly one moment: a turn POST that
     359              :  * came back 402/`no_funds` (`recordSendError` -> `recordPaywall`). Every other
     360              :  * way a user meets the ceiling — including a refusal inside a server-side LLM
     361              :  * hop that the browser cannot observe — needs a durable upgrade path.
     362              :  *
     363              :  * So this reads the funds signal the client already holds rather than waiting
     364              :  * for a refusal to carry it. It is deliberately the symmetric half of the
     365              :  * `hasFunds` watch above, which already clears paywalls when funds return.
     366              :  *
     367              :  * Consolidated cloud billing only: legacy balance loading currently collapses
     368              :  * unknown into false, so it cannot safely drive this surface.
     369              :  *
     370              :  * Non-blocking by construction — it renders beside the composer and disables
     371              :  * nothing.
     372              :  */
     373            1 : const agentHasFunds = computed(() => subscription.value?.agentHasFunds)
     374            1 : const agentScopedHasFunds = computed(
     375              :   () => subscription.value?.agentScopedHasFunds
     376              : )
     377            1 : const creditsExhausted = computed(() => {
     378            1 :   if (!consentAccepted.value) return false
     379            0 :   if (billingType.value !== 'workspace') return false
     380              :   // Same gate as the impression report above: an unsettled read cannot say
     381              :   // which presentation is right, and a card naming the wrong remediation is
     382              :   // worse than no card.
     383            0 :   if (!capabilityReadSettled.value) return false
     384            0 :   if (agentHasFunds.value !== false) return false
     385            1 :   if (
     386              :     billingStatus.value === 'paused' ||
     387              :     billingStatus.value === 'payment_failed'
     388              :   )
     389            0 :     return false
     390              :   // A standing card must offer a next step. Refusal-anchored inline cards can
     391              :   // still explain member, sales-managed, or unavailable states without a CTA.
     392            0 :   return ['subscribed', 'subscriptionRequired', 'local'].includes(
     393              :     paywallPresentation.value.kind
     394              :   )
     395              : })
     396              : 
     397            1 : const unresolvedPaywallPending = computed(() =>
     398              :   conversationEntries.value.some(
     399              :     (entry) =>
     400              :       entry.role === 'assistant' &&
     401              :       entry.parts.some((part) => part.type === 'paywall')
     402              :   )
     403              : )
     404              : 
     405              : /**
     406              :  * Suppressed while the latest transcript entry is an unresolved inline card:
     407              :  * the two render the same copy. An older card may be scrolled away and must not
     408              :  * hide the standing recovery path after a later server-side refusal.
     409              :  */
     410            1 : const latestEntryHasPaywall = computed(() => {
     411            0 :   const latestEntry = conversationEntries.value.at(-1)
     412            0 :   return (
     413              :     latestEntry?.role === 'assistant' &&
     414              :     latestEntry.parts.some(isCreditsStopPart)
     415              :   )
     416              : })
     417            1 : const threadHasUserMessage = computed(() =>
     418              :   conversationEntries.value.some((entry) => entry.role === 'user')
     419              : )
     420            0 : async function sendWithFundsRecheck(
     421              :   send: (onAccepted: () => void) => Promise<boolean>
     422              : ): Promise<boolean> {
     423            0 :   if (billingType.value !== 'workspace' || agentHasFunds.value !== false)
     424            0 :     return send(() => undefined)
     425            0 :   let recheckId: number | undefined
     426            0 :   let stopAdoptionWatch: (() => void) | undefined
     427            0 :   const settle = () => {
     428            0 :     if (recheckId !== undefined) agentPanelStore.finishFundsRecheck(recheckId)
     429              :   }
     430            0 :   let admitted = false
     431            0 :   let sent: boolean
     432            0 :   try {
     433            0 :     sent = await send(() => {
     434            0 :       admitted = true
     435            0 :       recheckId = agentPanelStore.beginFundsRecheck(billingIdentity.value, () =>
     436              :         stopAdoptionWatch?.()
     437              :       )
     438            0 :       stopAdoptionWatch = watch([subscription, billingIdentity], settle, {
     439              :         flush: 'sync'
     440              :       })
     441              :     })
     442              :   } finally {
     443            0 :     if (!admitted) settle()
     444              :   }
     445            0 :   if (!admitted) return sent
     446            0 :   void refreshBillingStatus().catch((error: unknown) => {
     447            0 :     reportError(error, {
     448              :       surface: 'agent',
     449              :       errorType: 'error_refreshing_agent_billing_status'
     450              :     })
     451              :   })
     452            0 :   return sent
     453              : }
     454              : 
     455            1 : const showStandingPaywall = computed(() => {
     456            1 :   if (!creditsExhausted.value || !threadHasUserMessage.value) return false
     457            0 :   if (agentPanelStore.hasPendingFundsRecheck(billingIdentity.value))
     458            0 :     return false
     459            0 :   return !latestEntryHasPaywall.value
     460              : })
     461              : 
     462            1 : const agentPanelStore = useAgentPanelStore()
     463            1 : const billingIdentity = useBillingIdentity()
     464              : 
     465            1 : watch(billingIdentity, () => {
     466            0 :   agentPanelStore.cancelFundsRechecks()
     467            0 :   agentPanelStore.reportedExhaustionIdentity = null
     468              : })
     469              : 
     470            1 : const showCreditTransitionNotice = computed(
     471              :   () =>
     472              :     isCreditTransitionNoticeShown(
     473              :       agentPanelStore.creditTransitionNotice,
     474              :       billingIdentity.value
     475              :     ) &&
     476              :     consentAccepted.value &&
     477              :     billingType.value === 'workspace' &&
     478              :     capabilityReadSettled.value &&
     479              :     canAccessSubscriptionFeatures.value &&
     480              :     agentScopedHasFunds.value === false &&
     481              :     agentHasFunds.value === true
     482              : )
     483              : 
     484            0 : function onDismissCreditTransitionNotice(): void {
     485            0 :   useTelemetry()?.trackAgentCreditTransitionNotice({ action: 'dismissed' })
     486            0 :   agentPanelStore.dismissCreditTransitionNotice(billingIdentity.value)
     487              : }
     488              : 
     489            0 : function onCreditTransitionNoticeShown(): void {
     490            0 :   const telemetry = useTelemetry()
     491            0 :   if (!telemetry) return
     492            0 :   if (
     493              :     !agentPanelStore.claimCreditTransitionNoticeImpression(
     494              :       billingIdentity.value
     495              :     )
     496              :   )
     497            0 :     return
     498            0 :   telemetry.trackAgentCreditTransitionNotice({ action: 'shown' })
     499              : }
     500              : 
     501            0 : function onStandingPaywallShown(): void {
     502            0 :   if (
     503              :     !showStandingPaywall.value ||
     504              :     agentPanelStore.reportedExhaustionIdentity === billingIdentity.value
     505              :   )
     506            0 :     return
     507            0 :   const telemetry = useTelemetry()
     508            0 :   if (!telemetry) return
     509            0 :   agentPanelStore.reportedExhaustionIdentity = billingIdentity.value
     510            0 :   telemetry.trackAgentPaywallShown({
     511              :     reason: snapshotAuthoritative.value
     512              :       ? toAgentPaywallReason(paywallPresentation.value)
     513              :       : 'unknown',
     514              :     surface: 'credits_exhausted'
     515              :   })
     516              : }
     517              : 
     518            1 : const workflowStore = useWorkflowStore()
     519            1 : const workflowService = useWorkflowService()
     520            1 : const bindingStore = useAgentWorkflowTabBindingStore()
     521            1 : const composerStore = useAgentComposerStore()
     522            1 : const { selectedWorkflow: selectedTarget } = storeToRefs(agentPanelStore)
     523            1 : const { dismissedSelectionSignature, enabled: agentEnabled } =
     524              :   storeToRefs(agentPanelStore)
     525            1 : const workflowResolver = useAgentWorkflowResolver({
     526              :   workflows: workflowStore,
     527              :   bindings: bindingStore,
     528              :   listCloudWorkflows: () => rest.listCloudWorkflows(),
     529              :   getCloudWorkflow: (workflowId) => rest.getCloudWorkflow(workflowId)
     530              : })
     531            1 : const {
     532              :   refreshCloudWorkflowIds,
     533              :   forgetCloudWorkflowId,
     534              :   cloudIdFor,
     535              :   boundOrOpenWorkflowFor,
     536              :   storedWorkflowFor,
     537              :   openWorkflowFor,
     538              :   availableWorkflowReferences,
     539              :   openTabsSnapshot
     540              : } = workflowResolver
     541            1 : const {
     542              :   isSelecting: workflowSelecting,
     543              :   selectingTarget,
     544              :   savingReference,
     545              :   onVisibleWorkflowChanged,
     546              :   selectTarget: onSelectWorkflowTarget,
     547              :   selectReference: onSelectWorkflowReference,
     548              :   restoreTarget: onWorkflowRestored,
     549              :   adoptTarget: adoptWorkflowTarget,
     550              :   requestReferences: onRequestWorkflowReferences,
     551              :   cancelSelection: cancelWorkflowSelection
     552              : } = useAgentWorkflowSelection({
     553              :   resolver: workflowResolver,
     554              :   canSelectTarget: () => !isSending.value && status.value === 'idle',
     555              :   warnWorkflowUnavailable,
     556              :   warnRestoreFailed,
     557              :   onTargetBound: (workflowId, previousWorkflowId, source) =>
     558              :     reportWorkflowBound(workflowId, previousWorkflowId, source)
     559              : })
     560            1 : const tabActivity = useWorkflowTabActivityStore()
     561            1 : const CREATING_TAB_MIN_DURATION_MS = 500
     562              : // Opens at the template's view so the follower's first nodes land on screen.
     563            1 : const agentTabGraph: ComfyWorkflowJSON = {
     564              :   ...blankGraph,
     565              :   extra: { ds: { offset: [0, 0], scale: 1 } }
     566              : }
     567              : 
     568            1 : const canvasStore = useCanvasStore()
     569            1 : const graphActivity = useAgentGraphActivityStore()
     570            1 : const settingStore = useSettingStore()
     571            1 : const dialogStore = useDialogStore()
     572            1 : watch(
     573              :   () => canvasStore.canvas?.graph,
     574              :   (graph, _previous, onCleanup) => {
     575            1 :     if (!graph?.events) return
     576            1 :     const events = graph.events as EventTarget
     577            1 :     const onNodeRemoved: EventListener = (event) => {
     578            0 :       if (!(event instanceof CustomEvent)) return
     579            0 :       const nodeId = parseNodeId(String(event.detail.node?.id))
     580            0 :       if (nodeId) graphActivity.removeNodes([nodeId])
     581              :     }
     582            1 :     events.addEventListener('node:removed', onNodeRemoved)
     583            1 :     onCleanup(() => events.removeEventListener('node:removed', onNodeRemoved))
     584              :   },
     585              :   { immediate: true }
     586              : )
     587              : // Claimed on mount rather than in setup so a setup failure cannot leave the id
     588              : // registered with no unmount to release it.
     589            1 : let agentMinimapLayer: MinimapDecorationLayer | undefined
     590            2 : function syncAgentMinimapLayer(activity: typeof graphActivity.state): void {
     591            2 :   if (activity.phase === 'idle') {
     592            2 :     agentMinimapLayer?.replace([])
     593            2 :     return
     594              :   }
     595            0 :   const rootGraphId = toRootGraphId(activity.rootGraphId)
     596            2 :   agentMinimapLayer?.replace(
     597              :     activity.nodeIds.map((nodeId) => ({
     598              :       target: {
     599              :         rootGraphId,
     600              :         owningGraphId: toOwningGraphId(activity.rootGraphId),
     601              :         nodeId
     602              :       },
     603              :       enter: 'pop'
     604              :     }))
     605              :   )
     606            2 :   if (
     607              :     activity.phase === 'running' &&
     608              :     !settingStore.get('Comfy.Minimap.Visible')
     609              :   )
     610            0 :     void settingStore.set('Comfy.Minimap.Visible', true)
     611              : }
     612            1 : watch(() => graphActivity.state, syncAgentMinimapLayer, { immediate: true })
     613            1 : onMounted(() => {
     614            1 :   agentMinimapLayer = registerMinimapDecorationLayer('agent.graph-activity')
     615            1 :   syncAgentMinimapLayer(graphActivity.state)
     616              : })
     617              : // Teardown must be total. Nothing is re-thrown: an error escaping an unmount
     618              : // hook reaches Vue's logError, which re-throws outside production builds (this
     619              : // app registers no app.config.errorHandler) and aborts both the steps after it
     620              : // and the rest of unmountComponent -- stranding this panel's later releases on
     621              : // singletons that outlive it, such as the PM-1575 canvas-sync gate below.
     622              : //
     623              : // Steps are keyed rather than positional for two reasons: the key names the
     624              : // failure in telemetry, which is now the only signal that a release was
     625              : // skipped, and insertion order is the run order (no key is integer-like, so
     626              : // Object.entries order is specified). They return `undefined` rather than
     627              : // `void` so an async step -- whose rejection would escape the catch below
     628              : // while still looking contained -- fails to compile instead.
     629            0 : function runPanelTeardown(
     630              :   steps: Readonly<Record<string, () => undefined>>
     631              : ): void {
     632            0 :   for (const [step, release] of Object.entries(steps)) {
     633            0 :     try {
     634            0 :       release()
     635              :     } catch (error) {
     636            0 :       try {
     637            0 :         reportError(error, {
     638              :           surface: 'agent',
     639              :           errorType: 'failure_tearing_down_agent_panel',
     640              :           tags: { step }
     641              :         })
     642              :       } catch {
     643              :         // A reporter that throws must not abort the teardown it reports on.
     644              :       }
     645              :     }
     646              :   }
     647              : }
     648            1 : onBeforeUnmount(() =>
     649              :   runPanelTeardown({
     650              :     disposeMinimapLayer: () => {
     651            0 :       agentMinimapLayer?.dispose()
     652              :     }
     653              :   })
     654              : )
     655            1 : const { withConsent } = useAgentConsent()
     656            1 : const workspaceStore = teamWorkspaceStore
     657            1 : const onboardingKey = computed(() =>
     658              :   scopedOnboardingKey(
     659              :     resolvedUserInfo.value?.id,
     660              :     workspaceStore.activeWorkspaceId
     661              :   )
     662              : )
     663            1 : watch(
     664              :   onboardingKey,
     665              :   (key) => {
     666              :     // Only carry the pre-consent, device-wide flag into a scope that had
     667              :     // already accepted consent when it loaded. A newly consenting scope has
     668              :     // not seen this scoped tour yet and should receive it once.
     669            1 :     if (key && consentAccepted.value) adoptSharedOnboardingFlag(key)
     670              :   },
     671              :   { immediate: true }
     672              : )
     673            1 : const { activeTour } = storeToRefs(useOnboardingTourStore())
     674            1 : const coachDeferredBy = computed(() =>
     675              :   canvasStore.linearMode
     676              :     ? 'app_mode'
     677              :     : activeTour.value !== null
     678              :       ? 'tour_active'
     679              :       : dialogStore.dialogStack.length > 0
     680              :         ? 'dialog_open'
     681              :         : null
     682              : )
     683            1 : const sendGates = useAgentSendGates(
     684              :   () => threadId.value,
     685              :   (gate) => {
     686            0 :     agentPanelStore.heldSendGate = gate
     687              :   }
     688              : )
     689            1 : const coachCompletionWaiters = new Set<(proceed: boolean) => void>()
     690            1 : let coachReleaseRevoked = false
     691            1 : function releaseCoachCompletionWaiters(
     692              :   holdEnd: CoachHoldEnd,
     693              :   proceed = true
     694              : ): void {
     695            1 :   sendGates.end({ gate: 'coach', ...holdEnd })
     696            1 :   for (const resolve of coachCompletionWaiters) resolve(proceed)
     697            1 :   coachCompletionWaiters.clear()
     698              : }
     699            1 : const coachReleaseReasons = {
     700              :   finish: 'finished',
     701              :   skip: 'skipped',
     702              :   elsewhere: 'elsewhere'
     703              : } as const satisfies Record<string, AgentCoachReleaseReason>
     704              : 
     705            0 : function onCoachFinished(action: keyof typeof coachReleaseReasons): void {
     706            0 :   releaseCoachCompletionWaiters({
     707              :     outcome: 'released',
     708              :     reason: coachReleaseReasons[action]
     709              :   })
     710              : }
     711            1 : watch(
     712              :   [consentAccepted, onboardingKey, coachDeferredBy, consentIdentity],
     713              :   ([accepted, key, reason, identity], previous) => {
     714            1 :     if (accepted && key && !hasSeenCoach(key)) trackCoachDeferral(key, reason)
     715            1 :     if (!accepted)
     716            1 :       releaseCoachCompletionWaiters({
     717              :         outcome: 'dropped',
     718              :         reason: 'consent_cleared'
     719              :       })
     720            0 :     else if (identity !== previous[3])
     721            0 :       releaseCoachCompletionWaiters({
     722              :         outcome: 'dropped',
     723              :         reason: 'identity_changed'
     724              :       })
     725            0 :     else if (reason !== null)
     726            0 :       releaseCoachCompletionWaiters({
     727              :         outcome: 'released',
     728              :         reason: `deferred_${reason}`
     729              :       })
     730              :   },
     731              :   { immediate: true }
     732              : )
     733            1 : const coachRef = ref<InstanceType<typeof OnboardingCoach>>()
     734            1 : const sendHeldByCoach = ref(false)
     735              : 
     736            0 : async function waitForCoachCompletion(
     737              :   clientMessageId: string
     738              : ): Promise<boolean> {
     739            0 :   const key = onboardingKey.value
     740            0 :   if (!key || hasSeenCoach(key)) return true
     741            0 :   await nextTick()
     742            0 :   if (
     743              :     key !== onboardingKey.value ||
     744              :     !consentAccepted.value ||
     745              :     coachDeferredBy.value !== null ||
     746              :     !coachRef.value
     747              :   )
     748            0 :     return true
     749            0 :   sendHeldByCoach.value = true
     750            0 :   sendGates.hold('coach', clientMessageId)
     751            0 :   try {
     752              :     const proceed = await new Promise<boolean>((resolve) =>
     753              :       coachCompletionWaiters.add(resolve)
     754              :     )
     755            0 :     return proceed && !coachReleaseRevoked
     756              :   } finally {
     757            0 :     sendHeldByCoach.value = false
     758              :   }
     759              : }
     760              : 
     761            0 : function restartCoach(): void {
     762              :   // Take-the-tour stays clickable while the coach is deferred by App Mode, and
     763              :   // there is no instance to hand the transition to. Clearing the persisted flag
     764              :   // makes the replay wait for the mount instead of being dropped.
     765            0 :   if (coachRef.value) coachRef.value.restart()
     766            0 :   else if (onboardingKey.value) resetCoach(onboardingKey.value)
     767              : }
     768              : 
     769            0 : function toSelectedNode(node: LGraphNode): SelectedNode {
     770            0 :   return {
     771              :     id: String(node.id),
     772              :     locatorId: workflowStore.nodeToNodeLocatorId(node),
     773              :     title: node.title || node.type
     774              :   }
     775              : }
     776              : 
     777            1 : const canReferenceNodes = computed(
     778              :   () =>
     779              :     selectedTarget.value !== null &&
     780              :     workflowStore.activeWorkflow?.path === selectedTarget.value.path
     781              : )
     782            1 : const nodeReferenceDisabledReason = computed(() => {
     783            2 :   if (selectedTarget.value === null) return t('agent.selectWorkflowForNodes')
     784            2 :   if (!canReferenceNodes.value)
     785            0 :     return t('agent.switchWorkflowForNodes', {
     786              :       workflowName: selectedTarget.value.filename
     787              :     })
     788              :   return undefined
     789              : })
     790              : 
     791            1 : const selectedNodes = computed<SelectedNode[]>(() =>
     792              :   canvasStore.selectedItems.filter(isLGraphNode).map(toSelectedNode)
     793              : )
     794            1 : composerStore.setNodeScope(selectedTarget.value?.instanceId ?? null)
     795            1 : const {
     796              :   staged: selectionTags,
     797              :   consume: consumeSelection,
     798              :   remove: removeSelectionTag,
     799              :   add: addSelectionTag,
     800              :   replace: replaceSelectionTags
     801              : } = useCanvasSelection({
     802              :   staged: computed({
     803              :     get: () => composerStore.nodes,
     804              :     set: composerStore.setNodes
     805              :   }),
     806              :   onNodesAdded: agentPanelStore.retainWorkflowTarget,
     807              :   retainWhenNotLive: true,
     808              :   selection: selectedNodes,
     809              :   enabled: () => agentEnabled.value && selectedTarget.value !== null,
     810              :   isLive: () => agentPanelStore.isOpen,
     811              :   isTracking: () => canReferenceNodes.value && canvasStore.isPickingNodes,
     812              :   scope: () => selectedTarget.value?.instanceId ?? null,
     813              :   dismissedSignature: dismissedSelectionSignature,
     814              :   retainStagedNode: (node) => {
     815            0 :     const locator = parseNodeLocatorId(selectedNodeKey(node))
     816            0 :     if (!locator) return false
     817            0 :     const viewedSubgraphUuid =
     818              :       canvasStore.currentGraph?.isRootGraph === false
     819              :         ? canvasStore.currentGraph.id
     820              :         : null
     821            0 :     return locator.subgraphUuid !== viewedSubgraphUuid
     822              :   }
     823              : })
     824              : 
     825            1 : watch(
     826              :   selectionTags,
     827              :   (tags, previousTags) => {
     828            0 :     const stagedKeys = new Set(tags.map(selectedNodeKey))
     829            0 :     const nextKeys = new Set(
     830              :       [...composerStore.selectionModeNodeKeys].filter((key) =>
     831              :         stagedKeys.has(key)
     832              :       )
     833              :     )
     834            0 :     if (canvasStore.isPickingNodes) {
     835            0 :       const previousKeys = new Set(previousTags.map(selectedNodeKey))
     836            0 :       const selectedKeys = new Set(selectedNodes.value.map(selectedNodeKey))
     837            0 :       for (const tag of tags) {
     838            0 :         const key = selectedNodeKey(tag)
     839            0 :         if (!previousKeys.has(key) && selectedKeys.has(key)) nextKeys.add(key)
     840              :       }
     841              :     }
     842            0 :     composerStore.setSelectionModeNodeKeys(nextKeys)
     843              :   },
     844              :   { deep: true, flush: 'sync' }
     845              : )
     846            1 : const hasNodeSelectionModeAddition = computed(() =>
     847              :   selectionTags.value.some((tag) =>
     848              :     composerStore.selectionModeNodeKeys.has(selectedNodeKey(tag))
     849              :   )
     850              : )
     851              : 
     852            1 : let nodeReferenceWorkflow = selectionTags.value.length
     853              :   ? selectedTarget.value
     854              :   : null
     855              : 
     856            0 : function viewedGraphNodes() {
     857            0 :   return (
     858              :     (canvasStore.currentGraph ?? app.canvas?.graph ?? app.graph)?.nodes ?? []
     859              :   )
     860              : }
     861              : 
     862            0 : function mentionableNodes(): SelectedNode[] {
     863            0 :   return canReferenceNodes.value ? viewedGraphNodes().map(toSelectedNode) : []
     864              : }
     865              : 
     866            1 : watch(
     867              :   selectionTags,
     868              :   (tags) => {
     869            0 :     nodeReferenceWorkflow = tags.length ? selectedTarget.value : null
     870              :   },
     871              :   { deep: true, flush: 'sync' }
     872              : )
     873              : 
     874            1 : const workflowDetached = computed(() => selectedTarget.value === null)
     875              : 
     876              : // Resolves the tab a turn is attributed to. `null` (the send had no origin
     877              : // tab) resolves to nothing rather than falling back to the selected target, so
     878              : // re-attaching during prepare() cannot pull a later tab into this turn.
     879            0 : function originWorkflow(origin?: TurnOrigin): ComfyWorkflow | undefined {
     880            0 :   if (origin === null) return undefined
     881            0 :   return (
     882              :     (origin === undefined
     883              :       ? selectedTarget.value
     884              :       : workflowStore.openWorkflows.find(
     885              :           (workflow) => workflow.path === origin.tabPath
     886              :         )) ?? undefined
     887              :   )
     888              : }
     889              : 
     890            0 : function targetWorkflowTurnContext(
     891              :   origin?: TurnOrigin
     892              : ): WorkflowTurnContext | undefined {
     893            0 :   if (workflowDetached.value) return undefined
     894            0 :   const target = originWorkflow(origin)
     895            0 :   if (!target) return undefined
     896            0 :   const id = cloudIdFor(target)
     897            0 :   if (id === undefined && !target.isTemporary && origin !== undefined)
     898            0 :     return undefined
     899            0 :   return id === undefined
     900              :     ? { tabPath: target.path }
     901              :     : { id, tabPath: target.path }
     902              : }
     903              : 
     904            0 : function serializedCanvas(target: ComfyWorkflow) {
     905            0 :   if (target.path === workflowStore.activeWorkflow?.path)
     906            0 :     target.changeTracker?.prepareForSave()
     907            0 :   return target.activeState
     908              : }
     909              : 
     910            0 : function targetWorkflowDraft(origin?: TurnOrigin): DraftSnapshot | undefined {
     911            0 :   if (workflowDetached.value) return undefined
     912            0 :   const target = originWorkflow(origin)
     913            0 :   if (!target) return undefined
     914            0 :   const documentId = cloudIdFor(target)
     915            0 :   if (canvasMissedDocumentFrames(documentId)) {
     916            0 :     if (target.activeState) reportWithheldDraft(documentId)
     917            0 :     return undefined
     918              :   }
     919            0 :   const content = serializedCanvas(target)
     920            0 :   if (!content) return undefined
     921            0 :   return { content }
     922              : }
     923              : 
     924            0 : function reportWithheldDraft(workflowId: string | undefined): void {
     925            0 :   const errorType = 'agent_canvas_draft_withheld_stale'
     926            0 :   reportError(new Error('agent canvas draft withheld: canvas may be stale'), {
     927              :     surface: 'agent',
     928              :     errorType,
     929              :     level: 'warning',
     930              :     fingerprint: [errorType],
     931              :     tags: {
     932              :       failure_kind: 'bad_state',
     933              :       feature_area: 'agent',
     934              :       operation: 'send',
     935              :       outcome: 'withheld'
     936              :     },
     937              :     context: { workflowId }
     938              :   })
     939              : }
     940              : 
     941            0 : function canvasMissedDocumentFrames(documentId: string | undefined): boolean {
     942            0 :   return (
     943              :     documentId !== undefined &&
     944              :     boundWorkflowId.value === documentId &&
     945              :     crdtStatus.value.canvasMayBeStale
     946              :   )
     947              : }
     948              : 
     949            0 : function canvasForWorkflow(workflowId: string): Record<string, unknown> | null {
     950            0 :   const target = boundOrOpenWorkflowFor(workflowId)
     951            0 :   return target ? serializedCanvas(target) : null
     952              : }
     953              : 
     954            1 : const selectedTargetTab = computed<ActiveTab | null>(() => {
     955            2 :   const target = selectedTarget.value
     956            2 :   return target
     957              :     ? {
     958              :         path: target.path,
     959              :         name: target.filename,
     960              :         isPersisted: target.isPersisted,
     961              :         modified: target.isModified
     962              :       }
     963              :     : null
     964              : })
     965              : 
     966            1 : const editableWorkflowId = computed(() => {
     967            2 :   const target = selectedTarget.value
     968            2 :   return target ? cloudIdFor(target) : undefined
     969              : })
     970              : 
     971            1 : const workflowTabs = computed<ActiveTab[]>(() =>
     972              :   workflowStore.openWorkflows.map((tab) => ({
     973              :     path: tab.path,
     974              :     name: tab.filename,
     975              :     isPersisted: tab.isPersisted,
     976              :     modified: tab.isModified
     977              :   }))
     978              : )
     979              : 
     980            0 : function onWorkflowAdopted(
     981              :   workflowId: string,
     982              :   sent: WorkflowTurnContext | undefined,
     983              :   previousWorkflowId: string | null
     984              : ): void {
     985            0 :   if (sent === undefined) return
     986              :   const adoptable =
     987              :     sent.id === undefined
     988              :       ? bindingStore.tabPathFor(workflowId) === undefined &&
     989              :         storedWorkflowFor(workflowId) === null
     990              :       : sent.id === workflowId
     991            0 :   if (adoptable) {
     992            0 :     bindingStore.bind(workflowId, sent.tabPath)
     993            0 :     reportWorkflowBound(
     994              :       workflowId,
     995              :       previousWorkflowId,
     996              :       sent.id === undefined ? 'minted' : 'active_tab'
     997              :     )
     998              :   }
     999              : }
    1000              : 
    1001            0 : async function onWorkflowRestoredAndBound(
    1002              :   workflowId: string | undefined,
    1003              :   isCurrent: () => boolean
    1004              : ): Promise<boolean> {
    1005            0 :   const { ready, committedWorkflowId } = await onWorkflowRestored(
    1006              :     workflowId,
    1007              :     isCurrent
    1008              :   )
    1009            0 :   if (committedWorkflowId === null || !isCurrent()) return ready
    1010            0 :   bindRestoredWorkflow(committedWorkflowId)
    1011            0 :   return ready
    1012              : }
    1013              : 
    1014            0 : function bindRestoredWorkflow(workflowId: string): void {
    1015            0 :   if (hasUnsyncedHumanOps(workflowId)) return
    1016            0 :   if (boundWorkflowId.value !== workflowId) bindWorkflow(workflowId)
    1017              : }
    1018              : 
    1019            0 : function adoptFollowedWorkflow(
    1020              :   workflow: ComfyWorkflow,
    1021              :   workflowId: string
    1022              : ): void {
    1023            0 :   adoptWorkflowTarget(workflow, workflowId)
    1024            0 :   bindRestoredWorkflow(workflowId)
    1025              : }
    1026              : 
    1027            0 : function warnWorkflowUnavailable(): void {
    1028            0 :   toast.warning(t('agent.targetNavigationUnavailable'), { duration: 5000 })
    1029              : }
    1030              : 
    1031            0 : function warnRestoreFailed(): void {
    1032            0 :   const { view } = agentPanelStore
    1033              :   // A loading history row reports its own failure.
    1034            0 :   if (view.screen === 'history' && view.selection.status === 'loading') return
    1035            0 :   toast.warning(t('agent.targetWorkflowOpenFailed'), { duration: 5000 })
    1036              : }
    1037              : 
    1038            0 : function trackWorkflowOpenFailure(
    1039              :   uiTreatment: AgentErrorMetadata['ui_treatment']
    1040              : ): void {
    1041            0 :   trackAgentError('workflow_open_failed', 'post_acceptance', uiTreatment, {
    1042              :     retryable: false
    1043              :   })
    1044              : }
    1045              : 
    1046              : /**
    1047              :  * The sent message, held from the moment the user sends until the cloud ids
    1048              :  * are refreshed, alongside the turn's origin. Scoped to one `sendMessage`
    1049              :  * call, so a send never hands these fields to the next one.
    1050              :  */
    1051            1 : let pendingSend: {
    1052              :   metadata: AgentMessageSentMetadata
    1053              :   origin: TurnOrigin
    1054              : } | null = null
    1055              : 
    1056              : /**
    1057              :  * A freshly opened tab has no cloud id until `refreshCloudWorkflowIds()` lands,
    1058              :  * and the turn is posted with the id resolved by that refresh (QAF-19).
    1059              :  * Reporting before it would file the first send of a session — the one the
    1060              :  * activation funnel is measuring — against no workflow at all.
    1061              :  *
    1062              :  * Resolved through the turn's own origin rather than the live selection,
    1063              :  * because `performSend` posts the origin tab's id: switching target or
    1064              :  * starting a new chat while the refresh is in flight must not retarget the
    1065              :  * report at a workflow the turn was never sent against.
    1066              :  */
    1067            0 : function reportPendingSend(): void {
    1068            0 :   const sent = pendingSend
    1069            0 :   if (!sent) return
    1070            0 :   pendingSend = null
    1071            0 :   useTelemetry()?.trackAgentMessageSent({
    1072              :     ...sent.metadata,
    1073              :     workflow_id: targetWorkflowTurnContext(sent.origin)?.id ?? null
    1074              :   })
    1075              : }
    1076              : 
    1077            1 : function registerAgentRunJobOwner(event: AgentWsEvent): void {
    1078            1 :   if (event.type !== 'agent_tool_call') return
    1079            1 :   const { job_id: jobId, job_workflow_id: workflowId } = event.data
    1080            1 :   if (!jobId || !workflowId) return
    1081            0 :   const workflow = boundOrOpenWorkflowFor(workflowId)
    1082            0 :   if (workflow) useExecutionStore().registerJobOwner(jobId, workflow)
    1083              : }
    1084              : 
    1085            1 : const agentNodeDefinitionRefresh = useAgentNodeDefinitionRefreshStore()
    1086              : 
    1087            1 : function observeAgentEvent(event: AgentWsEvent): void {
    1088            1 :   agentNodeDefinitionRefresh.observeAgentEvent(event)
    1089            1 :   registerAgentRunJobOwner(event)
    1090              : }
    1091              : 
    1092            1 : const {
    1093              :   sendMessage,
    1094              :   stopTurn,
    1095              :   isSending: sessionIsSending,
    1096              :   isTranscriptReady,
    1097              :   newChat,
    1098              :   start,
    1099              :   stop,
    1100              :   entries,
    1101              :   editableTurnId,
    1102              :   isStreaming,
    1103              :   status,
    1104              :   notices,
    1105              :   threadId,
    1106              :   loadThread,
    1107              :   boundWorkflowId,
    1108              :   bindWorkflow,
    1109              :   reportWorkflowBound,
    1110              :   answerAsk,
    1111              :   answeringAskIds
    1112              : } = useAgentSession({
    1113              :   rest,
    1114              :   events,
    1115              :   onThreadStarted: (source) =>
    1116              :     useTelemetry()?.trackAgentThreadStarted({ source }),
    1117              :   onAgentEvent: observeAgentEvent,
    1118              :   onThreadActivated: (id) => history.setActive(id),
    1119              :   onAskResolved: forgetApproval,
    1120              :   refreshCredentialDuringTurns: !isCloud,
    1121              :   workflow: {
    1122              :     initialize: agentPanelStore.initializeTargetTracking,
    1123              :     current: targetWorkflowTurnContext,
    1124              :     adopted: onWorkflowAdopted,
    1125              :     restored: restoreChatWorkflow,
    1126              :     prepare: async () => {
    1127            0 :       try {
    1128            0 :         await refreshCloudWorkflowIds()
    1129              :       } finally {
    1130              :         // In `finally` so a refresh that fails still reports the send, with
    1131              :         // whichever id the client already had, rather than losing the event.
    1132            0 :         reportPendingSend()
    1133              :       }
    1134              :     },
    1135              :     disowned: forgetCloudWorkflowId,
    1136              :     tabs: openTabsSnapshot,
    1137              :     activeTab: enqueueActiveTab,
    1138              :     draft: targetWorkflowDraft
    1139              :   }
    1140              : })
    1141              : 
    1142            1 : provide(
    1143              :   agentBoundWorkflowIdKey,
    1144              :   computed(() => boundWorkflowId.value ?? undefined)
    1145              : )
    1146              : 
    1147            1 : const isSending = computed(
    1148              :   () => sessionIsSending.value || composerStore.submission?.phase === 'pending'
    1149              : )
    1150              : 
    1151            1 : const isBoundWorkflowActive = computed(() => {
    1152            2 :   const bound = boundWorkflowId.value
    1153            2 :   const active = workflowStore.activeWorkflow
    1154            2 :   return (
    1155              :     bound !== null &&
    1156              :     active !== null &&
    1157              :     boundOrOpenWorkflowFor(bound)?.path === active.path
    1158              :   )
    1159              : })
    1160              : 
    1161              : // The CRDT follower is the inbound content channel: subscribes to the
    1162              : // session's bound workflow while its tab is active. Suspending the background
    1163              : // subscription makes reopening pull state-vector catch-up only after the
    1164              : // workflow's serialized activeState has hydrated the transient stores.
    1165            1 : const agentUserId = computed(() => {
    1166            0 :   const identity = agentIdentity.state.value
    1167            0 :   return identity.status === 'resolved' ? identity.userId : null
    1168              : })
    1169              : 
    1170            1 : const isFollowerActive = computed(
    1171              :   () => isBoundWorkflowActive.value && agentUserId.value !== null
    1172              : )
    1173              : 
    1174            1 : const {
    1175              :   status: crdtStatus,
    1176              :   debugSnapshot: crdtDebugSnapshot,
    1177              :   enqueueHumanOperations,
    1178              :   noteUnboundLocalIntent,
    1179              :   retrySubscription,
    1180              :   docInputNames,
    1181              :   docWidgetValue,
    1182              :   docPromotedWidgets,
    1183              :   mayWritePromotedHost,
    1184              :   docHasSubgraphDefinition,
    1185              :   notifyWidgetWriteRefused,
    1186              :   subscriptionSent
    1187              : } = useAgentCrdtFollower(boundWorkflowId, {
    1188              :   transport: docTransport,
    1189              :   userId: () => agentUserId.value,
    1190              :   isTargetActive: isFollowerActive,
    1191              :   // `app.isGraphReady` is a plain getter; reading `canvasStore.canvas` (set
    1192              :   // right after `app.setup()`) makes the follower's graph watch fire once the
    1193              :   // root graph exists.
    1194              :   getGraph: () =>
    1195              :     canvasStore.canvas && app.isGraphReady ? app.rootGraph : null,
    1196              :   events: {
    1197              :     onAppliedUpdate({ workflowId, opIds, isLive }) {
    1198            0 :       conversationStore.notifyCanvasCaughtUp({ workflowId, opIds, isLive })
    1199              :     },
    1200              :     onMaterialized({ workflowId, nodeIds }) {
    1201            0 :       if (app.isGraphReady) {
    1202            0 :         graphActivity.recordMaterialized(
    1203              :           { workflowId, rootGraphId: toRootGraphId(app.rootGraph.id) },
    1204              :           nodeIds,
    1205              :           conversationTurnId.value
    1206              :         )
    1207            0 :         if (status.value === 'idle') graphActivity.finishTurn()
    1208              :       }
    1209              :     },
    1210              :     onReset: graphActivity.resetWorkflow,
    1211              :     onLineageConfirmed({ workflowId, lineageSeq }) {
    1212            0 :       const path = bindingStore.tabPathFor(workflowId)
    1213            0 :       if (path) writeWorkflowDraftLineage(path, { workflowId, lineageSeq })
    1214              :     },
    1215              :     onSyncError: (message, code) =>
    1216              :       toast.error(t('agent.workflowSyncFailedTitle'), {
    1217              :         description: formatWorkflowSyncErrorDetail(t, message, code)
    1218              :       })
    1219              :   },
    1220              :   applierDeps: {
    1221              :     getCanvas: () => app.canvas,
    1222              :     withRemoteActor: (actor, fn) => layoutStore.withActor(actor, fn),
    1223              :     viewportBounds() {
    1224            0 :       const canvas = canvasStore.canvas
    1225            0 :       if (!canvas || canvas.graph?.isRootGraph === false) return null
    1226            0 :       const [x, y, width, height] = canvas.ds.visible_area
    1227            0 :       return { x, y, width, height }
    1228              :     }
    1229              :   },
    1230              :   canvasFor: canvasForWorkflow,
    1231              :   knownLineageFor(workflowId) {
    1232            0 :     const path = bindingStore.tabPathFor(workflowId)
    1233            0 :     const lineage = path ? readWorkflowDraftLineage(path) : null
    1234            0 :     return lineage?.workflowId === workflowId ? lineage.lineageSeq : null
    1235              :   }
    1236              : })
    1237              : // The bound document's serialized root graph id, independent of what is
    1238              : // currently on the canvas: `beforeLoadNewGraph` persists the outgoing
    1239              : // workflow's `activeState` before the shared renderer graph is rewritten, so
    1240              : // this stays the bound workflow's own root id through a tab switch instead of
    1241              : // tracking whichever graph the switch is loading.
    1242            0 : function boundRootGraphId(): RootGraphId | null {
    1243            0 :   const bound = boundWorkflowId.value
    1244            0 :   if (bound === null) return null
    1245            0 :   const id = boundOrOpenWorkflowFor(bound)?.activeState?.id
    1246            0 :   return id === undefined ? null : toRootGraphId(id)
    1247              : }
    1248            0 : function followerCanDeliver(): boolean {
    1249            0 :   return isFollowerActive.value && subscriptionSent()
    1250              : }
    1251            0 : function noteUnboundVisibleIntent(): void {
    1252            0 :   const visible = workflowStore.activeWorkflow
    1253            0 :   const workflowId = visible
    1254              :     ? bindingStore.workflowIdFor(visible.path)
    1255              :     : undefined
    1256            0 :   if (workflowId !== undefined) noteUnboundLocalIntent(workflowId)
    1257              : }
    1258            1 : const docOpMinter = attachDocOpMinter({
    1259              :   isEnabled: () => agentPanelStore.enabled,
    1260              :   isDocBound: followerCanDeliver,
    1261              :   onUnboundLocalIntent: noteUnboundLocalIntent,
    1262              :   onUnboundVisibleIntent: noteUnboundVisibleIntent,
    1263              :   enqueue: enqueueHumanOperations,
    1264              :   getGraph: () => (app.isGraphReady ? app.rootGraph : null),
    1265              :   boundRootGraphId,
    1266              :   docInputNames,
    1267              :   docWidgetValue,
    1268              :   docPromotedWidgets,
    1269              :   mayWritePromotedHost,
    1270              :   docHasSubgraphDefinition,
    1271              :   onWidgetWriteRefused: notifyWidgetWriteRefused
    1272              : })
    1273            1 : const isChangeTrackerRestoring = () =>
    1274              :   workflowStore.activeWorkflow?.changeTracker?._restoringState === true
    1275            1 : const restoreOpMinter = attachRestoreOpMinter({
    1276              :   isEnabled: () => agentPanelStore.enabled,
    1277              :   isDocBound: followerCanDeliver,
    1278              :   enqueue: enqueueHumanOperations,
    1279              :   getGraph: () => (app.isGraphReady ? app.rootGraph : null),
    1280              :   docInputNames: (nodeId) => {
    1281            0 :     const parsed = parseNodeId(nodeId)
    1282            0 :     return parsed === null ? null : docInputNames(parsed)
    1283              :   },
    1284              :   isRestoringState: isChangeTrackerRestoring
    1285              : })
    1286            1 : const agentWriteOverwriteWatch = attachAgentWriteOverwriteWatch({
    1287              :   isEnabled: () => agentPanelStore.enabled,
    1288              :   isRestoringState: isChangeTrackerRestoring
    1289              : })
    1290            1 : const isCrdtDevPanelEnabled = resolveDebugPanelEnabled(
    1291              :   agentPanelStore.enabled,
    1292              :   isCrdtDebugEnabled()
    1293              : )
    1294            1 : const { activeTurnId: conversationTurnId } = storeToRefs(conversationStore)
    1295              : 
    1296            0 : function canvasConfirmationState(workflowId?: string): CanvasConfirmation {
    1297            0 :   const bound = boundWorkflowId.value ?? undefined
    1298            0 :   if (bound === undefined) return 'none'
    1299            0 :   if (
    1300              :     workflowId !== undefined &&
    1301              :     !areAgentWorkflowIdsEquivalent(bound, workflowId)
    1302              :   )
    1303            0 :     return 'unavailable'
    1304            0 :   if (!isBoundWorkflowActive.value) return 'unavailable'
    1305            0 :   const status = crdtStatus.value
    1306            0 :   if (!status.enabled) return 'none'
    1307            0 :   if (status.connected) return 'awaiting'
    1308            0 :   return status.terminal === null ? 'none' : 'unavailable'
    1309              : }
    1310            1 : conversationStore.setCanvasSyncGate(
    1311              :   canvasConfirmationState,
    1312              :   () => crdtStatus.value.outcomes.appliedLive
    1313              : )
    1314              : 
    1315            0 : function tabAt(path: string | undefined, status: 'guessed' | 'known'): TurnTab {
    1316            0 :   const workflow =
    1317              :     path === undefined ? undefined : workflowStore.getWorkflowByPath(path)
    1318            0 :   return workflow
    1319              :     ? { status, workflowInstanceId: workflow.instanceId }
    1320              :     : { status: 'unknown' }
    1321              : }
    1322              : 
    1323            0 : function turnTab(): TurnTab {
    1324            0 :   if (workflowDetached.value)
    1325            0 :     return {
    1326              :       status: agentPanelStore.canRestoreWorkflow ? 'unknown' : 'none'
    1327              :     }
    1328            0 :   const bound = boundWorkflowId.value
    1329            0 :   const context = targetWorkflowTurnContext()
    1330            0 :   if (bound === null)
    1331            0 :     return tabAt(
    1332              :       context?.id === undefined ? undefined : context.tabPath,
    1333              :       'guessed'
    1334              :     )
    1335            0 :   return tabAt(
    1336              :     bindingStore.tabPathFor(bound) ??
    1337              :       (context?.id === bound ? context.tabPath : undefined),
    1338              :     'known'
    1339              :   )
    1340              : }
    1341              : 
    1342            0 : function adoptRestoredWorkflowTab(workflowId: string): void {
    1343            0 :   const restored = selectedTarget.value
    1344            0 :   const threadId = conversationStore.threadId
    1345            0 :   if (
    1346              :     restored !== null &&
    1347              :     threadId !== null &&
    1348              :     bindingStore.tabPathFor(workflowId) === restored.path
    1349              :   )
    1350            0 :     conversationStore.adoptThreadTab(threadId, restored.instanceId)
    1351              : }
    1352              : 
    1353            0 : async function restoreThreadWorkflow(
    1354              :   workflowId: string | undefined,
    1355              :   isCurrent: () => boolean
    1356              : ): Promise<boolean> {
    1357            0 :   const restoring = agentPanelStore.canRestoreWorkflow
    1358            0 :   const ready = await onWorkflowRestoredAndBound(workflowId, isCurrent)
    1359            0 :   if (ready && restoring && workflowId !== undefined)
    1360            0 :     adoptRestoredWorkflowTab(workflowId)
    1361            0 :   return ready
    1362              : }
    1363              : 
    1364            1 : conversationStore.setTurnTabResolver(turnTab)
    1365            1 : const { clearTurnSignals } = useAgentTabSignals()
    1366              : 
    1367            1 : const boundCanvasNodeCount = ref(0)
    1368            1 : function readBoundCanvasNodeCount(): void {
    1369            1 :   boundCanvasNodeCount.value = isBoundWorkflowActive.value
    1370              :     ? (workflowStore.activeWorkflow?.activeState?.nodes?.length ?? 0)
    1371              :     : 0
    1372              : }
    1373            1 : watch([boundWorkflowId, isBoundWorkflowActive], readBoundCanvasNodeCount, {
    1374              :   immediate: true
    1375              : })
    1376            1 : useEventListener(
    1377              :   api,
    1378              :   'graphChanged',
    1379              :   (event: CustomEvent<ComfyWorkflowJSON>) => {
    1380            0 :     if (isBoundWorkflowActive.value)
    1381            0 :       boundCanvasNodeCount.value = event.detail.nodes?.length ?? 0
    1382              :   }
    1383              : )
    1384              : 
    1385            1 : const completedTurnClaimedCanvasContent = ref(false)
    1386            0 : function completedTurnCanClaimCanvasContent(): boolean {
    1387            0 :   const lastCanvasTool = conversationMessages.value
    1388              :     .at(-1)
    1389              :     ?.parts.findLast(
    1390              :       (part) =>
    1391              :         part.type === 'tool' &&
    1392              :         (part.name === 'clear_canvas'
    1393              :           ? part.ok === true
    1394              :           : part.ok !== false && toolCanClaimCanvasContent(part.name))
    1395              :     )
    1396            0 :   return (
    1397              :     lastCanvasTool?.type === 'tool' && lastCanvasTool.name !== 'clear_canvas'
    1398              :   )
    1399              : }
    1400            1 : const canvasEmptyAfterTurn = computed(
    1401              :   () =>
    1402              :     completedTurnClaimedCanvasContent.value &&
    1403              :     isBoundWorkflowActive.value &&
    1404              :     boundCanvasNodeCount.value === 0
    1405              : )
    1406              : 
    1407            0 : function refreshAgentBillingStatus(): void {
    1408            0 :   void refreshBillingStatus().catch((error: unknown) => {
    1409            0 :     reportError(error, {
    1410              :       surface: 'agent',
    1411              :       errorType: 'error_refreshing_agent_billing_status'
    1412              :     })
    1413              :   })
    1414              : }
    1415              : 
    1416              : // Adoption (onWorkflowAdopted) and tab activation (onAgentActiveTab) are the
    1417              : // primary spinner setters; the non-idle branch only re-arms it after the
    1418              : // stash/resume flip of a panel remount, where those setters never run.
    1419            1 : let wasTurnActive = false
    1420            1 : watch(
    1421              :   [status, conversationTurnId],
    1422              :   ([value, turnId]) => {
    1423            1 :     const active = value !== 'idle'
    1424            1 :     const completedTurn = wasTurnActive && !active
    1425            1 :     if (completedTurn)
    1426            0 :       completedTurnClaimedCanvasContent.value =
    1427              :         completedTurnCanClaimCanvasContent()
    1428            1 :     else if (active) completedTurnClaimedCanvasContent.value = false
    1429            1 :     wasTurnActive = active
    1430            1 :     if (active) {
    1431            0 :       graphActivity.startTurn(turnId)
    1432            0 :       return
    1433              :     }
    1434            1 :     if (completedTurn) {
    1435            0 :       graphActivity.finishTurn()
    1436            0 :       if (billingType.value === 'workspace') refreshAgentBillingStatus()
    1437              :     }
    1438              :   },
    1439              :   { immediate: true, flush: 'sync' }
    1440              : )
    1441            1 : watch([threadId, boundWorkflowId], () => {
    1442            0 :   completedTurnClaimedCanvasContent.value = false
    1443              : })
    1444              : 
    1445            1 : const executionErrorStore = useExecutionErrorStore()
    1446              : 
    1447            0 : function surfaceAgentError(type: 'agent_api_failed', details: string): void {
    1448            0 :   executionErrorStore.recordPromptError({
    1449              :     type,
    1450              :     message: t(`errorCatalog.promptErrors.${type}.desc`),
    1451              :     details
    1452              :   })
    1453            0 :   executionErrorStore.showErrorOverlay()
    1454              : }
    1455              : 
    1456            1 : let noticesSeen = 0
    1457            1 : watch(
    1458              :   () => notices.value.length,
    1459              :   (length) => {
    1460            0 :     for (const notice of notices.value.slice(noticesSeen))
    1461            0 :       surfaceAgentError('agent_api_failed', notice.text)
    1462            0 :     noticesSeen = length
    1463              :   }
    1464              : )
    1465              : 
    1466            1 : let activeTabGeneration = 0
    1467            1 : let activeTabChain: Promise<void> = Promise.resolve()
    1468              : 
    1469            0 : function enqueueActiveTab(data: AgentActiveTabData): Promise<boolean> {
    1470            0 :   const generation = ++activeTabGeneration
    1471            0 :   const turnId =
    1472              :     data.message_id === undefined
    1473              :       ? conversationTurnId.value
    1474              :       : toTurnId(data.message_id)
    1475            0 :   const result = activeTabChain.then(() =>
    1476              :     onAgentActiveTab(data, generation, turnId)
    1477              :   )
    1478            0 :   activeTabChain = result.then(() => undefined)
    1479            0 :   return result
    1480              : }
    1481              : 
    1482            0 : async function onOpenApprovalWorkflow(
    1483              :   askId: string,
    1484              :   workflowId: string,
    1485              :   workflowName?: string
    1486              : ): Promise<void> {
    1487            0 :   const decidedAt = Date.now()
    1488            0 :   if (await enqueueActiveTab({ workflow_id: workflowId, name: workflowName }))
    1489            0 :     trackApprovalResolved(askId, 'open_workflow', decidedAt)
    1490              : }
    1491              : 
    1492            0 : async function onNavigateToReferenceWorkflow(
    1493              :   workflowId: string
    1494              : ): Promise<void> {
    1495            0 :   try {
    1496            0 :     let target = openWorkflowFor(workflowId)
    1497            0 :     if (target === null) {
    1498            0 :       await Promise.all([
    1499              :         refreshCloudWorkflowIds(),
    1500              :         workflowStore.syncWorkflows()
    1501              :       ])
    1502            0 :       target = storedWorkflowFor(workflowId)
    1503              :     }
    1504            0 :     if (target === null || !(await workflowService.openWorkflow(target))) {
    1505            0 :       warnWorkflowUnavailable()
    1506            0 :       return
    1507              :     }
    1508            0 :     bindingStore.bind(workflowId, target.path)
    1509              :   } catch {
    1510            0 :     warnWorkflowUnavailable()
    1511              :   }
    1512              : }
    1513              : 
    1514            0 : async function onShowTarget(
    1515              :   isNavigationCurrent: () => boolean = () => true,
    1516              :   warnOpenFailed: () => void = warnWorkflowUnavailable
    1517              : ): Promise<boolean> {
    1518            0 :   const target = selectedTarget.value
    1519            0 :   if (target === null) return false
    1520            0 :   const isCurrent = () =>
    1521              :     isNavigationCurrent() && selectedTarget.value === target
    1522            0 :   try {
    1523            0 :     const opened = await workflowService.openWorkflow(target, { isCurrent })
    1524            0 :     if (!isCurrent()) return false
    1525            0 :     if (!opened) warnOpenFailed()
    1526            0 :     return opened
    1527              :   } catch {
    1528            0 :     if (isCurrent()) warnOpenFailed()
    1529            0 :     return false
    1530              :   }
    1531              : }
    1532              : 
    1533            0 : function agentTabFilename(name: string | undefined): string | undefined {
    1534            0 :   const cleaned = [
    1535              :     ...(name ?? '')
    1536              :       .replace(/[/\\\p{Cc}]/gu, '-')
    1537              :       .replace(/\.json$/i, '')
    1538              :       .trim()
    1539              :       .replace(/^\.+/, '')
    1540              :   ]
    1541              :     .slice(0, 80)
    1542              :     .join('')
    1543              :     .replace(/^[\s.]+/u, '')
    1544              :     .trim()
    1545            0 :   return cleaned.length === 0 ? undefined : `${cleaned}.json`
    1546              : }
    1547              : 
    1548              : interface ActiveTabActivation {
    1549              :   stale: () => boolean
    1550              :   showTurnOn: (tab: ComfyWorkflow) => void
    1551              : }
    1552              : 
    1553            0 : async function onAgentActiveTab(
    1554              :   data: AgentActiveTabData,
    1555              :   generation: number,
    1556              :   turnId: TurnId | null
    1557              : ): Promise<boolean> {
    1558            0 :   const previousWorkflowId = boundWorkflowId.value
    1559            0 :   const stale = () => generation !== activeTabGeneration
    1560            0 :   if (stale()) return false
    1561            0 :   const activation: ActiveTabActivation = {
    1562              :     stale,
    1563              :     showTurnOn: (tab) => {
    1564            0 :       if (turnId !== null)
    1565            0 :         conversationStore.moveTurnToTab(turnId, tab.instanceId)
    1566              :     }
    1567              :   }
    1568            0 :   try {
    1569            0 :     const bound = boundOrOpenWorkflowFor(data.workflow_id)
    1570            0 :     return bound
    1571              :       ? await activateExistingAgentTab(
    1572              :           data,
    1573              :           bound,
    1574              :           previousWorkflowId,
    1575              :           activation
    1576              :         )
    1577              :       : await createAndActivateAgentTab(data, previousWorkflowId, activation)
    1578              :   } catch (error) {
    1579            0 :     if (stale()) return false
    1580            0 :     bindWorkflow(data.workflow_id)
    1581            0 :     reportError(error, {
    1582              :       surface: 'agent',
    1583              :       errorType: 'agent_workflow_open_failed'
    1584              :     })
    1585            0 :     surfaceAgentError(
    1586              :       'agent_api_failed',
    1587              :       error instanceof Error ? error.message : String(error)
    1588              :     )
    1589            0 :     trackWorkflowOpenFailure('error_overlay')
    1590            0 :     return false
    1591              :   } finally {
    1592            0 :     tabActivity.setCreating(false)
    1593              :   }
    1594              : }
    1595              : 
    1596            0 : async function activateExistingAgentTab(
    1597              :   data: AgentActiveTabData,
    1598              :   bound: ComfyWorkflow,
    1599              :   previousWorkflowId: string | null,
    1600              :   { stale, showTurnOn }: ActiveTabActivation
    1601              : ): Promise<boolean> {
    1602            0 :   const opened = await workflowService.openWorkflow(bound)
    1603            0 :   if (stale()) return false
    1604            0 :   if (!opened) {
    1605            0 :     warnWorkflowUnavailable()
    1606            0 :     trackWorkflowOpenFailure('toast')
    1607            0 :     return false
    1608              :   }
    1609            0 :   bindingStore.bind(data.workflow_id, bound.path)
    1610            0 :   showTurnOn(bound)
    1611            0 :   bindWorkflow(data.workflow_id)
    1612            0 :   reportWorkflowBound(data.workflow_id, previousWorkflowId, 'active_tab')
    1613            0 :   useTelemetry()?.trackAgentWorkflowApplied({
    1614              :     workflow_id: data.workflow_id,
    1615              :     target: 'active_tab_switch'
    1616              :   })
    1617            0 :   return true
    1618              : }
    1619              : 
    1620            0 : async function createAndActivateAgentTab(
    1621              :   data: AgentActiveTabData,
    1622              :   previousWorkflowId: string | null,
    1623              :   { stale, showTurnOn }: ActiveTabActivation
    1624              : ): Promise<boolean> {
    1625            0 :   const creatingStartedAt = Date.now()
    1626            0 :   tabActivity.setCreating(true)
    1627            0 :   const remainingCreatingTime =
    1628              :     CREATING_TAB_MIN_DURATION_MS - (Date.now() - creatingStartedAt)
    1629            0 :   if (remainingCreatingTime > 0)
    1630            0 :     await new Promise((resolve) => setTimeout(resolve, remainingCreatingTime))
    1631            0 :   if (stale()) return false
    1632            0 :   const tab = workflowStore.createNewTemporary(
    1633              :     agentTabFilename(data.name),
    1634              :     agentTabGraph
    1635              :   )
    1636            0 :   tabActivity.setCreating(false)
    1637            0 :   let opened: boolean
    1638            0 :   try {
    1639            0 :     opened = await workflowService.openWorkflow(tab)
    1640              :   } catch (error) {
    1641            0 :     await workflowService.closeWorkflow(tab, { warnIfUnsaved: false })
    1642            0 :     throw error
    1643              :   }
    1644            0 :   if (stale() || !opened) {
    1645            0 :     await workflowService.closeWorkflow(tab, { warnIfUnsaved: false })
    1646            0 :     if (!stale()) {
    1647            0 :       warnWorkflowUnavailable()
    1648            0 :       trackWorkflowOpenFailure('toast')
    1649              :     }
    1650            0 :     return false
    1651              :   }
    1652            0 :   showTurnOn(tab)
    1653            0 :   bindingStore.bind(data.workflow_id, tab.path)
    1654            0 :   bindWorkflow(data.workflow_id)
    1655            0 :   reportWorkflowBound(data.workflow_id, previousWorkflowId, 'active_tab')
    1656            0 :   useTelemetry()?.trackAgentWorkflowApplied({
    1657              :     workflow_id: data.workflow_id,
    1658              :     target: 'active_tab_open'
    1659              :   })
    1660            0 :   return true
    1661              : }
    1662              : 
    1663            0 : function onApprovalShown(
    1664              :   askId: string,
    1665              :   turnId: string,
    1666              :   workflowId: string | null
    1667              : ): void {
    1668            0 :   if (!conversationStore.recordApprovalShown(askId, Date.now())) return
    1669            0 :   useTelemetry()?.trackAgentRunApprovalShown({
    1670              :     turn_id: turnId,
    1671              :     workflow_id: workflowId
    1672              :   })
    1673              : }
    1674              : 
    1675            0 : function trackApprovalResolved(
    1676              :   askId: string,
    1677              :   decision: AgentRunApprovalDecision,
    1678              :   decidedAt = Date.now(),
    1679              :   shownAt = conversationStore.approvalShownAt(askId)
    1680              : ): void {
    1681            0 :   if (shownAt === undefined) return
    1682            0 :   if (decision !== 'open_workflow')
    1683            0 :     conversationStore.forgetApprovalTiming(askId)
    1684            0 :   useTelemetry()?.trackAgentRunApprovalResolved({
    1685              :     decision,
    1686              :     time_to_decide_ms: Math.max(0, decidedAt - shownAt)
    1687              :   })
    1688              : }
    1689              : 
    1690            0 : function forgetApproval(askId: string): void {
    1691            0 :   conversationStore.forgetApproval(askId)
    1692              : }
    1693              : 
    1694            0 : async function onAnswerAsk(
    1695              :   askId: string,
    1696              :   answer: AgentAnswerRequest
    1697              : ): Promise<void> {
    1698            0 :   const shownAt = conversationStore.approvalShownAt(askId)
    1699            0 :   const decidedAt = Date.now()
    1700            0 :   const decision = runApprovalDecision(answer)
    1701            0 :   if ((await answerAsk(askId, answer)) && decision)
    1702            0 :     trackApprovalResolved(askId, decision, decidedAt, shownAt)
    1703              : }
    1704              : 
    1705            0 : function runApprovalDecision(
    1706              :   answer: AgentAnswerRequest
    1707              : ): 'run' | 'cancel' | undefined {
    1708            0 :   if (answer.selected.length !== 1 || answer.other_text) return undefined
    1709            0 :   const [choice] = answer.selected
    1710            0 :   return choice === 'run' || choice === 'cancel' ? choice : undefined
    1711              : }
    1712              : 
    1713            1 : void refreshCloudWorkflowIds()
    1714            1 : onBeforeUnmount(() => {
    1715            0 :   runPanelTeardown({
    1716              :     releaseCoachCompletionWaiters: () => {
    1717            0 :       const deferredBy = coachDeferredBy.value
    1718            0 :       if (deferredBy !== null && agentPanelStore.isVisible)
    1719            0 :         releaseCoachCompletionWaiters({
    1720              :           outcome: 'released',
    1721              :           reason: `deferred_${deferredBy}`
    1722              :         })
    1723              :       else {
    1724            0 :         coachReleaseRevoked = true
    1725            0 :         releaseCoachCompletionWaiters(
    1726              :           { outcome: 'dropped', reason: 'panel_closed' },
    1727              :           false
    1728              :         )
    1729              :       }
    1730              :     },
    1731              :     invalidateConsentHeldSubmission: () => {
    1732            0 :       if (
    1733              :         !sendHeldByCoach.value &&
    1734              :         (coachDeferredBy.value === null || !agentPanelStore.isVisible) &&
    1735              :         composerStore.submission?.id === consentHeldSubmissionId
    1736              :       ) {
    1737            0 :         sendGates.end({
    1738              :           gate: 'consent',
    1739              :           outcome: 'dropped',
    1740              :           reason: 'panel_closed'
    1741              :         })
    1742            0 :         composerStore.invalidateSubmission()
    1743              :       }
    1744              :     },
    1745              :     detachDocOpMinter: () => {
    1746            0 :       docOpMinter.detach()
    1747              :     },
    1748              :     detachRestoreOpMinter: () => {
    1749            0 :       restoreOpMinter.detach()
    1750              :     },
    1751              :     detachAgentWriteOverwriteWatch: () => {
    1752            0 :       agentWriteOverwriteWatch.detach()
    1753              :     },
    1754              :     exitNodeSelectionMode: canvasStore.stopNodePicking,
    1755              :     stopSession: () => {
    1756            0 :       stop()
    1757              :     },
    1758              :     invalidateActiveTabGeneration: () => {
    1759            0 :       ++activeTabGeneration
    1760              :     },
    1761              :     clearAgentTurnTabs: () => {
    1762            0 :       clearTurnSignals()
    1763              :     },
    1764              :     resetTurnTabResolver: () => {
    1765            0 :       conversationStore.setTurnTabResolver(() => ({ status: 'unknown' }))
    1766              :     },
    1767              :     clearCreatingTab: () => {
    1768            0 :       tabActivity.setCreating(false)
    1769              :     },
    1770              :     resetCanvasSyncGate: () => {
    1771            0 :       conversationStore.setCanvasSyncGate(() => 'none')
    1772              :     }
    1773              :   })
    1774              : })
    1775              : 
    1776            1 : const { copy } = useClipboard({ legacy: true })
    1777              : 
    1778              : // DES-1221. Read while the panel is on screen, so the experiment's denominator
    1779              : // is the panel openers the hypothesis is about.
    1780            1 : const { variant: freeUseVariant, reportNotice: onFreeUseNotice } =
    1781              :   useFreeUsePlacement(isCloud)
    1782              : 
    1783            1 : const freeUseGrantActive = computed(
    1784              :   () =>
    1785              :     subscription.value?.agentScopedHasFundsAuthoritative === true &&
    1786              :     subscription.value.agentScopedHasFunds === true
    1787              : )
    1788              : 
    1789            1 : const freeUsePlacement = computed(() =>
    1790              :   !freeUseGrantActive.value || unresolvedPaywallPending.value
    1791              :     ? 'control'
    1792              :     : freeUseVariant.value
    1793              : )
    1794              : 
    1795            0 : function onFeedback(turnId: string, vote: 'up' | 'down' | null): void {
    1796            0 :   const message = entries.value.find(
    1797              :     (entry) => entry.role === 'assistant' && entry.id === turnId
    1798              :   )
    1799            0 :   const workflowId =
    1800              :     message?.role === 'assistant'
    1801              :       ? (message.parts
    1802              :           .flatMap((part) => (part.type === 'tabLink' ? [part.workflowId] : []))
    1803              :           .at(-1) ?? null)
    1804              :       : null
    1805              : 
    1806            0 :   useTelemetry()?.trackAgentMessageFeedback({
    1807              :     message_id: turnId,
    1808              :     turn_id: turnId,
    1809              :     vote,
    1810              :     workflow_id: workflowId
    1811              :   })
    1812              : }
    1813              : 
    1814            1 : const warnedIncompleteReasons = new Set<string>()
    1815              : 
    1816            0 : function warnIncompleteHistory(reason: string, announce: boolean): void {
    1817            0 :   if (warnedIncompleteReasons.has(reason)) return
    1818            0 :   reportError(new Error('Agent thread list ended before its last page'), {
    1819              :     surface: 'agent',
    1820              :     errorType: 'agent_thread_list_load_failed',
    1821              :     tags: { reason }
    1822              :   })
    1823            0 :   if (announce) {
    1824            0 :     warnedIncompleteReasons.add(reason)
    1825            0 :     toast.warning(t('agent.historyIncomplete'), { duration: 5000 })
    1826              :   }
    1827            0 :   trackAgentError(
    1828              :     'thread_list_load_failed',
    1829              :     'pre_acceptance',
    1830              :     announce ? 'toast' : 'none',
    1831              :     { retryable: false }
    1832              :   )
    1833              : }
    1834              : 
    1835            1 : const hostFenceMessage = ref<string>()
    1836            1 : const hostFenced = computed(() => hostFenceMessage.value !== undefined)
    1837            1 : let startedRefreshes = 0
    1838            1 : let fencedAtRefresh = 0
    1839              : 
    1840            1 : const { refreshHistory: runHistoryRefresh } = useAgentChatHistoryRefresh({
    1841              :   listThreads: rest.listThreads,
    1842              :   history,
    1843              :   untitledChat: () => t('agent.untitledChat'),
    1844              :   onFailure: (failure, uiTreatment) => {
    1845            0 :     if (failure.status === 'incomplete') {
    1846            0 :       warnIncompleteHistory(failure.reason, uiTreatment === 'error_overlay')
    1847            0 :       return
    1848              :     }
    1849            0 :     const error = failure.cause
    1850            0 :     const diagnostics = describeAgentRequestFailure(error)
    1851            0 :     reportError(error, {
    1852              :       surface: 'agent',
    1853              :       errorType: 'agent_thread_list_load_failed',
    1854              :       tags: diagnostics,
    1855              :       fingerprint: agentRequestFailureFingerprint(
    1856              :         'agent_thread_list_load_failed',
    1857              :         diagnostics
    1858              :       )
    1859              :     })
    1860            0 :     const loopbackOnly = isLoopbackOnlyRefusal(error)
    1861            0 :     if (loopbackOnly) {
    1862            0 :       hostFenceMessage.value = error.message
    1863            0 :       fencedAtRefresh = startedRefreshes
    1864            0 :     } else if (uiTreatment === 'error_overlay')
    1865            0 :       surfaceAgentError(
    1866              :         'agent_api_failed',
    1867              :         error instanceof Error ? error.message : String(error)
    1868              :       )
    1869            0 :     trackAgentError(
    1870              :       'thread_list_load_failed',
    1871              :       'pre_acceptance',
    1872              :       loopbackOnly ? 'inline_notice' : uiTreatment,
    1873              :       {
    1874              :         retryable: isRetryableRequestFailure(error, false),
    1875              :         diagnostics
    1876              :       }
    1877              :     )
    1878              :   }
    1879              : })
    1880              : 
    1881            1 : async function refreshHistory(
    1882              :   ...args: Parameters<typeof runHistoryRefresh>
    1883              : ): ReturnType<typeof runHistoryRefresh> {
    1884            1 :   const refresh = ++startedRefreshes
    1885            1 :   const outcome = await runHistoryRefresh(...args)
    1886            1 :   if (
    1887              :     (outcome === 'complete' || outcome === 'incomplete') &&
    1888              :     refresh >= fencedAtRefresh
    1889              :   )
    1890            1 :     hostFenceMessage.value = undefined
    1891            1 :   return outcome
    1892              : }
    1893              : 
    1894            1 : const provisionalTitle = ref<string>()
    1895              : 
    1896            1 : void refreshHistory()
    1897              : 
    1898            1 : const currentChatReady = computed(
    1899              :   () =>
    1900              :     isTranscriptReady.value &&
    1901              :     threadId.value === history.activeId &&
    1902              :     selectedTarget.value !== null
    1903              : )
    1904              : 
    1905            1 : const historyGroups = computed(() => {
    1906            2 :   const id = threadId.value
    1907            2 :   if (id === null || !isTranscriptReady.value || id !== history.activeId)
    1908            2 :     return history.grouped
    1909            2 :   return withCurrentSessionTitle(
    1910              :     history.grouped,
    1911              :     history.titleFor(id) || deriveSessionTitle(entries.value)
    1912              :   )
    1913              : })
    1914              : 
    1915            1 : const workflowChatFollow = useAgentWorkflowChatFollow({
    1916              :   activeWorkflow: () => workflowStore.activeWorkflow,
    1917              :   selectedTarget: () => selectedTarget.value,
    1918              :   currentThreadId: () => threadId.value,
    1919              :   sessions: () => history.sessions,
    1920              :   followsVisibleWorkflow: () => agentPanelStore.followsVisibleWorkflow,
    1921              :   hasDraft: () => composerStore.hasDraft,
    1922              :   userEngaged,
    1923              :   cloudIdFor,
    1924              :   refreshCloudWorkflowIds,
    1925              :   refreshHistory,
    1926              :   loadThread,
    1927              :   clearDraft: () => composerStore.setText(''),
    1928              :   trackThreadStarted: () =>
    1929              :     useTelemetry()?.trackAgentThreadStarted({ source: 'history_select' }),
    1930              :   restoreThreadWorkflow,
    1931              :   adoptWorkflowTarget: adoptFollowedWorkflow,
    1932              :   adoptRestoredWorkflowTab
    1933              : })
    1934              : 
    1935            1 : const {
    1936              :   failure: followFailure,
    1937              :   openingWorkflowName,
    1938              :   followTab,
    1939              :   retireFollow,
    1940              :   retryFollow: onRetryFollow
    1941              : } = workflowChatFollow
    1942              : 
    1943            1 : watch(
    1944              :   threadId,
    1945              :   (id, previousId) => {
    1946            1 :     if (
    1947              :       id === null ||
    1948              :       previousId !== null ||
    1949              :       provisionalTitle.value === undefined
    1950              :     )
    1951            1 :       return
    1952            0 :     if (!workflowChatFollow.isOpeningThread(id))
    1953            0 :       history.rename(id, provisionalTitle.value)
    1954            0 :     provisionalTitle.value = undefined
    1955              :   },
    1956              :   { immediate: true, flush: 'sync' }
    1957              : )
    1958              : 
    1959            0 : function onOpenHistory(): void {
    1960            0 :   retireFollow()
    1961            0 :   void refreshHistory()
    1962              : }
    1963              : 
    1964            0 : async function onSelectHistory(
    1965              :   id: string,
    1966              :   isCurrent: () => boolean
    1967              : ): Promise<boolean> {
    1968            0 :   retireFollow()
    1969            0 :   provisionalTitle.value = undefined
    1970            0 :   if (currentChatReady.value && id === threadId.value)
    1971            0 :     return onShowTarget(isCurrent, warnRestoreFailed)
    1972            0 :   composerStore.invalidateSubmission()
    1973            0 :   errorAttachRun++
    1974            0 :   composerStore.clearErrorContext()
    1975            0 :   cancelWorkflowSelection()
    1976            0 :   agentPanelStore.beginWorkflowRestoration()
    1977            0 :   canvasStore.stopNodePicking()
    1978            0 :   const opened = await loadThread(id, isCurrent)
    1979            0 :   if (opened)
    1980            0 :     useTelemetry()?.trackAgentThreadStarted({ source: 'history_select' })
    1981            0 :   void refreshHistory()
    1982            0 :   return opened
    1983              : }
    1984              : 
    1985            1 : function userEngaged(): boolean {
    1986            1 :   return composerStore.hasDraft || isSending.value || workflowSelecting.value
    1987              : }
    1988              : 
    1989            0 : function restoreChatWorkflow(
    1990              :   workflowId: string | undefined,
    1991              :   isCurrent: () => boolean
    1992              : ): Promise<boolean> | boolean {
    1993            0 :   return workflowChatFollow.restoreChatWorkflow(workflowId, isCurrent)
    1994              : }
    1995              : 
    1996            1 : onBeforeUnmount(retireFollow)
    1997              : 
    1998            1 : watch(
    1999              :   () => workflowStore.activeWorkflow,
    2000              :   workflowChatFollow.activeWorkflowChanged
    2001              : )
    2002              : 
    2003            1 : workflowStore.onUserActivation((workflow) => void followTab(workflow))
    2004              : 
    2005            1 : watch(userEngaged, workflowChatFollow.userEngagementChanged)
    2006              : 
    2007            1 : const mountedOn = workflowStore.activeWorkflow
    2008            1 : if (
    2009              :   mountedOn !== null &&
    2010              :   selectedTarget.value !== null &&
    2011              :   selectedTarget.value.path !== mountedOn.path
    2012              : )
    2013            0 :   void followTab(mountedOn)
    2014              : 
    2015            0 : function warnWorkflowChanged(): void {
    2016            0 :   toast.warning(t('agent.errorContext.workflowChanged'), { duration: 5000 })
    2017              : }
    2018              : 
    2019            0 : async function openChatForErroredWorkflow(
    2020              :   workflow: ComfyWorkflow,
    2021              :   run: number
    2022              : ): Promise<boolean> {
    2023            0 :   await followTab(workflow)
    2024            0 :   if (run !== errorAttachRun) return false
    2025            0 :   if (workflowStore.activeWorkflow?.path !== workflow.path) {
    2026            0 :     warnWorkflowChanged()
    2027            0 :     return false
    2028              :   }
    2029            0 :   if (selectedTarget.value?.path === workflow.path) return true
    2030            0 :   if (composerStore.hasDraft) {
    2031            0 :     toast.warning(t('agent.errorContext.draftBlocked'), { duration: 5000 })
    2032            0 :     return false
    2033              :   }
    2034            0 :   onNewChat('new_chat_button')
    2035            0 :   return true
    2036              : }
    2037              : 
    2038            0 : async function attachErrorContext(context: AgentErrorContext): Promise<void> {
    2039            0 :   const run = ++errorAttachRun
    2040            0 :   const workflow = workflowStore.activeWorkflow
    2041            0 :   if (context.workflowPath && workflow?.path !== context.workflowPath) {
    2042            0 :     warnWorkflowChanged()
    2043            0 :     return
    2044              :   }
    2045            0 :   if (workflow && !(await openChatForErroredWorkflow(workflow, run))) return
    2046            0 :   agentPanelStore.retainWorkflowTarget()
    2047            0 :   composerStore.setErrorContext(context)
    2048              : }
    2049              : 
    2050            1 : watch(
    2051              :   () => agentPanelStore.pendingErrorContext,
    2052              :   (context) => {
    2053            1 :     if (!context) return
    2054            0 :     agentPanelStore.pendingErrorContext = null
    2055            0 :     void attachErrorContext(context)
    2056              :   },
    2057              :   { immediate: true }
    2058              : )
    2059              : 
    2060            0 : function buildTranscriptMarkdown(entries: ConversationEntry[]): string {
    2061            0 :   return entries
    2062              :     .map((entry) => {
    2063            0 :       if (entry.role === 'user') return `**You:** ${agentMessageText(entry)}`
    2064              :       const text = entry.parts
    2065              :         .filter((part) => part.type === 'text')
    2066              :         .map((part) => part.text)
    2067              :         .join('')
    2068            0 :       return `**Agent:** ${text}`
    2069              :     })
    2070              :     .join('\n\n')
    2071              : }
    2072              : 
    2073            0 : function onCopyMarkdown(id: string): void {
    2074            0 :   if (id === history.activeId && id === threadId.value)
    2075            0 :     void copy(buildTranscriptMarkdown(entries.value))
    2076            0 :   else toast.info(t('agent.copyUnavailable'))
    2077              : }
    2078              : 
    2079            1 : const coachSteps = computed<CoachStep[]>(() => [
    2080              :   {
    2081              :     card: 'intro',
    2082              :     target: '#agent-panel-root',
    2083              :     placement: 'left-center',
    2084              :     title: t('agent.coachTitle'),
    2085              :     body: t('agent.coachBody')
    2086              :   },
    2087              :   {
    2088              :     card: 'workflow',
    2089              :     target: '#agent-composer',
    2090              :     placement: 'left-end',
    2091              :     title: t('agent.coachWorkflowTitle'),
    2092              :     body: t('agent.coachWorkflowBody')
    2093              :   },
    2094              :   {
    2095              :     card: 'history',
    2096              :     target: '#agent-chat-history',
    2097              :     placement: 'left-start',
    2098              :     tooltip: t('agent.showChatHistory'),
    2099              :     title: t('agent.coachHistoryTitle'),
    2100              :     body: t('agent.coachHistoryBody')
    2101              :   }
    2102              : ])
    2103              : 
    2104            1 : let consentHeldSubmissionId: number | undefined
    2105            0 : async function holdSendForConsent(
    2106              :   clientMessageId: string,
    2107              :   onAccept: () => void
    2108              : ): Promise<void> {
    2109            0 :   let accepted = false
    2110            0 :   let exit: 'declined' | 'dismissed' | undefined
    2111            0 :   sendGates.hold('consent', clientMessageId)
    2112            0 :   try {
    2113            0 :     await withConsent(
    2114              :       'first_message',
    2115              :       () => {
    2116            0 :         accepted = true
    2117            0 :         onAccept()
    2118              :       },
    2119              :       {
    2120              :         onExit: (consentExit) => {
    2121            0 :           exit = consentExit
    2122              :         }
    2123              :       }
    2124              :     )
    2125              :   } finally {
    2126            0 :     sendGates.end(
    2127              :       accepted
    2128              :         ? { gate: 'consent', outcome: 'released', reason: 'accepted' }
    2129              :         : {
    2130              :             gate: 'consent',
    2131              :             outcome: 'dropped',
    2132              :             reason: exit ?? 'not_completed'
    2133              :           },
    2134              :       clientMessageId
    2135              :     )
    2136              :   }
    2137              : }
    2138              : 
    2139            0 : async function consentAllowsSubmission(
    2140              :   submissionId: number | undefined,
    2141              :   clientMessageId: string
    2142              : ): Promise<boolean> {
    2143            0 :   let hasConsent = false
    2144            0 :   let coachReleased = false
    2145            0 :   let acceptedIdentity: string | null = null
    2146            0 :   consentHeldSubmissionId = submissionId
    2147            0 :   try {
    2148            0 :     await holdSendForConsent(clientMessageId, () => {
    2149            0 :       hasConsent = true
    2150            0 :       acceptedIdentity = consentIdentity.value
    2151              :     })
    2152            0 :     if (hasConsent)
    2153            0 :       coachReleased = await waitForCoachCompletion(clientMessageId)
    2154              :   } finally {
    2155            0 :     if (consentHeldSubmissionId === submissionId)
    2156            0 :       consentHeldSubmissionId = undefined
    2157              :   }
    2158            0 :   return (
    2159              :     composerStore.submission?.id === submissionId &&
    2160              :     hasConsent &&
    2161              :     coachReleased &&
    2162              :     consentAccepted.value &&
    2163              :     consentIdentity.value === acceptedIdentity
    2164              :   )
    2165              : }
    2166              : 
    2167            1 : const { submit: onSend } = useAgentDraftSubmission({
    2168              :   canSubmit: () => !workflowSelecting.value && !isSending.value,
    2169              :   onSubmit: agentPanelStore.retainWorkflowTarget,
    2170              :   onRejected: sendGates.rejectDraft,
    2171              :   target: () => selectedTarget.value,
    2172              :   editableWorkflowId: () => editableWorkflowId.value,
    2173              :   selection: {
    2174              :     staged: selectionTags,
    2175              :     workflow: () => nodeReferenceWorkflow,
    2176              :     consume: consumeSelection,
    2177              :     replace: replaceSelectionTags,
    2178              :     exit: canvasStore.stopNodePicking
    2179              :   },
    2180              :   // fallow-ignore-next-line complexity -- Existing PR logic; this lane changes only the composing panel test.
    2181              :   send: async (text, attachments, nodes, references, meta) => {
    2182            0 :     const submissionId = composerStore.submission?.id
    2183            0 :     if (
    2184              :       !consentAccepted.value &&
    2185              :       !(await consentAllowsSubmission(submissionId, meta.clientMessageId))
    2186              :     )
    2187            0 :       return false
    2188            0 :     if (!hasAgentCredential()) {
    2189            0 :       const identityBeforeSignIn = consentIdentity.value
    2190            0 :       if (!(await ensureSignedIn())) return false
    2191            0 :       if (
    2192              :         composerStore.submission?.id !== submissionId ||
    2193              :         !consentAccepted.value ||
    2194              :         consentIdentity.value !== identityBeforeSignIn
    2195              :       )
    2196            0 :         return false
    2197              :     }
    2198              : 
    2199            0 :     const originContext = targetWorkflowTurnContext()
    2200            0 :     pendingSend = {
    2201              :       metadata: {
    2202              :         attachment_count: attachments.length,
    2203              :         node_tag_count: nodes.length,
    2204              :         thread_id: threadId.value,
    2205              :         workflow_id: originContext?.id ?? null,
    2206              :         client_message_id: meta.clientMessageId,
    2207              :         input_method: meta.inputMethod,
    2208              :         starter_prompt_id: meta.starterPrompt?.id ?? null,
    2209              :         starter_prompt_click_id: meta.starterPrompt?.clickId ?? null
    2210              :       },
    2211              :       origin:
    2212              :         originContext === undefined ? null : { tabPath: originContext.tabPath }
    2213              :     }
    2214            0 :     const selectionWorkflow = selectedTarget.value
    2215            0 :     try {
    2216            0 :       return await sendWithFundsRecheck((onAccepted) =>
    2217              :         sendMessage(
    2218              :           text,
    2219              :           attachments,
    2220              :           nodes,
    2221              :           references,
    2222              :           () => (selectionWorkflow ? cloudIdFor(selectionWorkflow) : undefined),
    2223              :           meta.clientMessageId,
    2224              :           onAccepted
    2225              :         )
    2226              :       )
    2227              :     } finally {
    2228            0 :       reportPendingSend()
    2229              :     }
    2230              :   }
    2231              : })
    2232              : 
    2233              : // The session owns the acknowledgement boundary: a stop that lands before the
    2234              : // POST acks is remembered there and committed at ack (see stopPendingAck).
    2235            0 : function onStop(method: AgentStopMethod): void {
    2236            0 :   void stopTurn(method)
    2237              : }
    2238              : 
    2239            0 : function onRenameChat(title: string): void {
    2240            0 :   if (threadId.value === null) provisionalTitle.value = title
    2241            0 :   else history.rename(threadId.value, title)
    2242              : }
    2243              : 
    2244            0 : function onRenameHistory(id: string, title: string): void {
    2245            0 :   history.rename(id, title)
    2246              : }
    2247              : 
    2248            0 : function onDeleteHistory(id: string): void {
    2249            0 :   const isCurrent =
    2250              :     id === history.activeId ||
    2251              :     (history.activeId === null && id === threadId.value)
    2252            0 :   history.remove(id)
    2253              :   // Deleting the open chat also ends it; a dead thread must not stay editable.
    2254            0 :   if (isCurrent) onNewChat('history_delete')
    2255              : }
    2256              : 
    2257            0 : function onNewChat(
    2258              :   source: 'new_chat_button' | 'history_back' | 'history_delete'
    2259              : ): void {
    2260            0 :   retireFollow()
    2261            0 :   if (source === 'history_back') {
    2262            0 :     newChat()
    2263            0 :     agentPanelStore.startFollowingVisibleWorkflow()
    2264            0 :     return
    2265              :   }
    2266            0 :   provisionalTitle.value = undefined
    2267            0 :   composerStore.invalidateSubmission()
    2268            0 :   cancelWorkflowSelection()
    2269            0 :   canvasStore.stopNodePicking()
    2270            0 :   attachment.cancelAllUploads()
    2271            0 :   replaceSelectionTags([])
    2272            0 :   dismissedSelectionSignature.value = null
    2273            0 :   errorAttachRun++
    2274            0 :   composerStore.clearDraft()
    2275            0 :   newChat(source)
    2276            0 :   agentPanelStore.startFollowingVisibleWorkflow()
    2277              : }
    2278              : 
    2279            1 : const fileInput = ref<HTMLInputElement>()
    2280            1 : const assetDragActive = ref(false)
    2281            1 : let assetDragDepth = 0
    2282              : 
    2283            1 : watch(
    2284              :   selectedTarget,
    2285              :   (target) => {
    2286            2 :     canvasStore.stopNodePicking()
    2287            2 :     composerStore.setNodeScope(target?.instanceId ?? null)
    2288            2 :     nodeReferenceWorkflow = null
    2289              :   },
    2290              :   { flush: 'sync' }
    2291              : )
    2292              : 
    2293            1 : watch(
    2294              :   () => workflowStore.activeWorkflow,
    2295              :   () => {
    2296            1 :     canvasStore.stopNodePicking()
    2297            1 :     onVisibleWorkflowChanged()
    2298              :   },
    2299              :   { flush: 'sync' }
    2300              : )
    2301              : 
    2302              : // Target startup is an explicit session event, not a read of a thread ID that
    2303              : // happens to have been assigned by start(). Register scope cleanup first.
    2304            1 : start({
    2305              :   restore:
    2306              :     agentPanelStore.view.screen === 'chat' ||
    2307              :     (agentPanelStore.view.selection.status === 'idle' &&
    2308              :       threadId.value === agentPanelStore.view.previousThreadId)
    2309              : })
    2310            1 : watch(threadId, composerStore.trackDraftThread, {
    2311              :   immediate: true,
    2312              :   flush: 'sync'
    2313              : })
    2314              : 
    2315            1 : watch(
    2316              :   () => agentPanelStore.pendingCloudHandoff,
    2317              :   (prompt) => {
    2318            1 :     if (!prompt) return
    2319            0 :     composerStore.setText(
    2320              :       [composerStore.draft, prompt].filter(Boolean).join('\n\n')
    2321              :     )
    2322            0 :     agentPanelStore.pendingCloudHandoff = null
    2323              :   },
    2324              :   { immediate: true }
    2325              : )
    2326              : 
    2327            1 : watch(() => canvasStore.currentGraph, canvasStore.stopNodePicking, {
    2328              :   flush: 'sync'
    2329              : })
    2330              : 
    2331            0 : function onSelectNodes(): void {
    2332            0 :   if (!canReferenceNodes.value || canvasStore.isPickingNodes) return
    2333            0 :   const canvas = app.canvas
    2334            0 :   if (!canvas) return
    2335              : 
    2336            0 :   const merged = new Map<string, LGraphNode>(
    2337              :     [...canvas.selectedItems]
    2338              :       .filter(isLGraphNode)
    2339              :       .map((node) => [workflowStore.nodeToNodeLocatorId(node), node] as const)
    2340              :   )
    2341            0 :   for (const tag of selectionTags.value) {
    2342            0 :     const key = selectedNodeKey(tag)
    2343            0 :     const node = getNodeByLocatorId(app.rootGraph, key)
    2344            0 :     if (node) merged.set(key, node)
    2345              :   }
    2346            0 :   if (merged.size) {
    2347            0 :     canvas.selectItems([...merged.values()])
    2348              :   }
    2349            0 :   canvasStore.startNodePicking()
    2350            0 :   void nextTick(() => {
    2351            0 :     if (canvasStore.isPickingNodes) canvas.canvas.focus()
    2352              :   })
    2353              : }
    2354              : 
    2355            1 : const assetsStore = useAssetsStore()
    2356            1 : let inputAssetRefresh: Promise<unknown> = Promise.resolve()
    2357            1 : const { flags } = useFeatureFlags()
    2358              : 
    2359            1 : const attachment = useAttachment({
    2360              :   validate: isValidAgentAttachment,
    2361              :   upload: async (file, signal) => {
    2362              :     const uploaded = await rest.uploadImage(file, file.name, signal)
    2363            0 :     const filename = uploaded.name ?? file.name
    2364            0 :     return {
    2365              :       ref: filename,
    2366              :       url: isHdrImageFilename(file.name)
    2367              :         ? undefined
    2368              :         : api.apiURL(
    2369              :             `/view?filename=${encodeURIComponent(filename)}&type=input`
    2370              :           )
    2371              :     }
    2372              :   },
    2373              :   resolveStoredPreview: async (file, ref) =>
    2374              :     isHdrImageFilename(file.name)
    2375              :       ? ((await findServerPreviewUrl(ref)) ?? undefined)
    2376              :       : undefined,
    2377              :   // The library caches input assets; without this refresh a just-uploaded file
    2378              :   // is neither listed in the Assets tab nor mentionable this session. One run
    2379              :   // per settled batch, chained, because the query queue coalesces an
    2380              :   // overlapping refresh into the in-flight one instead of scheduling a
    2381              :   // trailing pass.
    2382              :   onUploaded: () => {
    2383            0 :     inputAssetRefresh = inputAssetRefresh
    2384              :       .then(() => assetsStore.inputAssets.loadNew())
    2385              :       .catch(() => undefined)
    2386              :   },
    2387              :   maxBytes: () => flags.maxUploadSize ?? MAX_ATTACHMENT_BYTES,
    2388              :   onError: warnAttachment,
    2389              :   onDuplicate: notifyDuplicateAttachments,
    2390              :   staged: () => composerStore.attachments,
    2391              :   stage: composerStore.addAttachment,
    2392              :   update: composerStore.updateAttachment,
    2393              :   remove: composerStore.removeAttachment
    2394              : })
    2395              : 
    2396            1 : watch(
    2397              :   [
    2398              :     () => resolvedUserInfo.value?.id,
    2399              :     () => workspaceStore.activeWorkspaceId,
    2400              :     () => assetsStore.deletingAssetIds.size
    2401              :   ],
    2402              :   attachment.forgetUploads
    2403              : )
    2404              : 
    2405            0 : function notifyDuplicateAttachments(names: string[]): void {
    2406            0 :   toast.info(
    2407              :     t(
    2408              :       'agent.attachmentsAlreadyAdded',
    2409              :       { name: names[0], count: names.length },
    2410              :       names.length
    2411              :     ),
    2412              :     { duration: 3500 }
    2413              :   )
    2414              : }
    2415              : 
    2416            1 : onBeforeUnmount(() =>
    2417              :   runPanelTeardown({
    2418              :     cancelAllUploads: () => {
    2419            0 :       attachment.cancelAllUploads()
    2420              :     }
    2421              :   })
    2422              : )
    2423              : 
    2424            0 : function onAttach(): void {
    2425            0 :   canvasStore.stopNodePicking()
    2426            0 :   useTelemetry()?.trackAgentAttachButtonClicked({ method: 'menu' })
    2427            0 :   fileInput.value?.click()
    2428              : }
    2429              : 
    2430            0 : async function onAttachFiles(files: File[]): Promise<void> {
    2431            0 :   if (await attachment.addFiles(files))
    2432            0 :     useTelemetry()?.trackAgentAttachButtonClicked({ method: 'paste' })
    2433              : }
    2434              : 
    2435            0 : function onOpenAssets(): void {
    2436            0 :   canvasStore.stopNodePicking()
    2437            0 :   sidebarTabStore.activeSidebarTabId = 'assets'
    2438              : }
    2439              : 
    2440            0 : function onMentionPick(node: SelectedNode): void {
    2441            0 :   if (!canReferenceNodes.value) return
    2442            0 :   const stagedBefore = selectionTags.value.length
    2443            0 :   addSelectionTag(node)
    2444            0 :   if (selectionTags.value.length > stagedBefore)
    2445            0 :     useTelemetry()?.trackAgentNodeTagged({ source: 'mention_picker' })
    2446              : }
    2447              : 
    2448            0 : function onRemoveSelectionTag(id: string): void {
    2449            0 :   const node = canvasStore.selectedItems
    2450              :     .filter(isLGraphNode)
    2451              :     .find((item) => selectedNodeKey(toSelectedNode(item)) === id)
    2452            0 :   removeSelectionTag(id)
    2453            0 :   if (node) {
    2454            0 :     canvasStore.canvas?.deselect(node)
    2455            0 :     canvasStore.canvas?.setDirty(true)
    2456              :   }
    2457              : }
    2458              : 
    2459            0 : function onClosePanel(): void {
    2460            0 :   canvasStore.stopNodePicking()
    2461            0 :   useTelemetry()?.trackAgentCloseButtonClicked()
    2462            0 :   agentPanelStore.close('close_button')
    2463              : }
    2464              : 
    2465            0 : async function onFilesPicked(event: Event): Promise<void> {
    2466            0 :   const input = event.target as HTMLInputElement
    2467            0 :   const files = input.files
    2468            0 :   if (files && files.length > 0) await attachment.addFiles(Array.from(files))
    2469            0 :   input.value = ''
    2470              : }
    2471              : 
    2472            0 : function isAssetDrag(event: DragEvent): boolean {
    2473              :   // Bare text/uri-list matches any dragged hyperlink; only claim real asset
    2474              :   // cards, which always carry the asset-info payload.
    2475            0 :   return (event.dataTransfer?.types ?? []).includes(MIME_ASSET_INFO)
    2476              : }
    2477              : 
    2478            0 : function isAttachableDrag(event: DragEvent): boolean {
    2479            0 :   return (
    2480              :     (event.dataTransfer?.types ?? []).includes('Files') || isAssetDrag(event)
    2481              :   )
    2482              : }
    2483              : 
    2484            0 : function clearAssetDrag(): void {
    2485            0 :   assetDragDepth = 0
    2486            0 :   assetDragActive.value = false
    2487              : }
    2488              : 
    2489            0 : function onPanelDragEnter(event: DragEvent): void {
    2490            0 :   if (hostFenced.value) return
    2491            0 :   if (!isAttachableDrag(event)) return
    2492            0 :   assetDragDepth += 1
    2493            0 :   assetDragActive.value = true
    2494              : }
    2495              : 
    2496            0 : function onPanelDragLeave(): void {
    2497            0 :   if (assetDragDepth === 0) return
    2498            0 :   assetDragDepth -= 1
    2499            0 :   if (assetDragDepth === 0) assetDragActive.value = false
    2500              : }
    2501              : 
    2502            0 : function warnAttachment(message: string): void {
    2503            0 :   toast.warning(message, { duration: 5000 })
    2504              : }
    2505              : 
    2506            0 : function droppedAssetVerdict(asset: DroppedAsset): AgentAttachVerdict {
    2507            0 :   const fromRef = asset.ref ? agentAttachRefVerdict(asset.ref) : 'unknown'
    2508            0 :   return fromRef === 'unknown' ? agentAttachVerdict(asset.name) : fromRef
    2509              : }
    2510              : 
    2511            0 : function stageAcceptedAssetCard(
    2512              :   asset: DroppedAsset,
    2513              :   ref: string,
    2514              :   sourceKey: string
    2515              : ): boolean {
    2516            0 :   if (composerStore.attachments.some((item) => item.ref === ref)) {
    2517            0 :     notifyDuplicateAttachments([asset.name])
    2518            0 :     return false
    2519              :   }
    2520            0 :   if (composerStore.attachments.length >= MAX_TURN_ATTACHMENTS) {
    2521            0 :     warnAttachment(
    2522              :       t('agent.attachmentCountExceeded', { count: MAX_TURN_ATTACHMENTS })
    2523              :     )
    2524            0 :     return false
    2525              :   }
    2526            0 :   const added = composerStore.addAttachment({
    2527              :     id: `asset:${crypto.randomUUID()}`,
    2528              :     name: asset.name,
    2529              :     ref,
    2530              :     sourceKey,
    2531              :     previewUrl: asset.previewUrl,
    2532              :     mediaUrl: asset.mediaUrl,
    2533              :     mediaKind: asset.kind
    2534              :   })
    2535            0 :   if (!added) notifyDuplicateAttachments([asset.name])
    2536            0 :   return added
    2537              : }
    2538              : 
    2539            0 : async function attachDroppedAsset(event: DragEvent): Promise<boolean> {
    2540            0 :   const asset = event.dataTransfer && getDroppedAsset(event.dataTransfer)
    2541            0 :   if (!asset) {
    2542            0 :     warnAttachment(t('agent.assetNotAttachable'))
    2543            0 :     return false
    2544              :   }
    2545              : 
    2546            0 :   const sourceKey = asset.ref ? `asset:${asset.ref}` : `uri:${asset.uri}`
    2547            0 :   if (droppedAssetVerdict(asset) === 'rejected') {
    2548            0 :     attachment.addUnsupportedAsset(asset.name, sourceKey)
    2549            0 :     return false
    2550              :   }
    2551            0 :   if (asset.ref && asset.kind !== 'other') {
    2552            0 :     return stageAcceptedAssetCard(asset, asset.ref, sourceKey)
    2553              :   }
    2554              : 
    2555            0 :   const result = await attachment.addDeferredFile(
    2556              :     asset.name,
    2557              :     (signal) => fetchDroppedAsset(asset, { signal, nameFromSource: true }),
    2558              :     sourceKey
    2559              :   )
    2560            0 :   if (result === 'fetch_failed')
    2561            0 :     warnAttachment(t('agent.assetFetchFailed', { name: asset.name }))
    2562            0 :   return result === 'uploaded'
    2563              : }
    2564              : 
    2565            0 : function onPanelDragOver(event: DragEvent): void {
    2566            0 :   if (isAttachableDrag(event)) event.preventDefault()
    2567              : }
    2568              : 
    2569            0 : async function onPanelDrop(event: DragEvent): Promise<void> {
    2570            0 :   clearAssetDrag()
    2571            0 :   if (hostFenced.value) {
    2572            0 :     event.preventDefault()
    2573            0 :     event.stopPropagation()
    2574            0 :     return
    2575              :   }
    2576            0 :   if ((event.dataTransfer?.files.length ?? 0) === 0 && isAssetDrag(event)) {
    2577            0 :     event.preventDefault()
    2578            0 :     void attachDroppedAsset(event).then((attached) => {
    2579            0 :       if (attached)
    2580            0 :         useTelemetry()?.trackAgentAttachButtonClicked({ method: 'drag_drop' })
    2581              :     })
    2582            0 :     return
    2583              :   }
    2584            0 :   const files = Array.from(event.dataTransfer?.files ?? [])
    2585            0 :   if (files.length === 0) return
    2586            0 :   event.preventDefault()
    2587            0 :   if (await attachment.addFiles(files))
    2588            0 :     useTelemetry()?.trackAgentAttachButtonClicked({ method: 'drag_drop' })
    2589              : }
    2590              : 
    2591            1 : const agentPanelRenderStore = useAgentPanelRenderStore()
    2592            1 : onMounted(() => agentPanelRenderStore.reach('drawn'))
    2593              : </script>
    2594              : 
    2595              : <template>
    2596              :   <AgentGraphActivityBar :canvas="canvasStore.canvas" />
    2597              :   <div
    2598              :     id="agent-panel-root"
    2599              :     class="relative size-full"
    2600              :     @dragenter="onPanelDragEnter"
    2601              :     @dragleave="onPanelDragLeave"
    2602              :     @dragover="onPanelDragOver"
    2603              :     @drop="onPanelDrop"
    2604              :   >
    2605              :     <input
    2606              :       ref="fileInput"
    2607              :       type="file"
    2608              :       :accept="AGENT_ATTACH_ACCEPT"
    2609              :       multiple
    2610              :       class="hidden"
    2611              :       data-testid="agent-file-input"
    2612              :       @change="onFilesPicked"
    2613              :     />
    2614              :     <AgentPanel
    2615              :       :entries
    2616              :       :editable-turn-id="editableTurnId"
    2617              :       :answering-ask-ids="answeringAskIds"
    2618              :       :user-name="userName"
    2619              :       :streaming="isStreaming"
    2620              :       :submitting="isSending || status === 'thinking'"
    2621              :       :can-attach="true"
    2622              :       :can-open-assets="!isBuilderMode"
    2623              :       :is-maximized="agentPanelStore.isMaximized"
    2624              :       :history-groups="historyGroups"
    2625              :       :select-history="onSelectHistory"
    2626              :       :current-chat-ready="currentChatReady"
    2627              :       :opening-workflow-name="openingWorkflowName"
    2628              :       :session-id="threadId"
    2629              :       :custom-title="
    2630              :         history.titleFor(threadId) ??
    2631              :         historyGroups.current.find((session) => session.id === threadId)
    2632              :           ?.title ??
    2633              :         provisionalTitle
    2634              :       "
    2635              :       :selection-tags="selectionTags"
    2636              :       :has-node-selection-mode-addition="hasNodeSelectionModeAddition"
    2637              :       :node-reference-disabled-reason="nodeReferenceDisabledReason"
    2638              :       :select-workflow-reference="onSelectWorkflowReference"
    2639              :       :saving-reference="savingReference"
    2640              :       :available-workflows="availableWorkflowReferences"
    2641              :       :editable-workflow-id="editableWorkflowId"
    2642              :       :active-tab="selectedTargetTab"
    2643              :       :workflow-tabs="workflowTabs"
    2644              :       :visible-tab-path="workflowStore.activeWorkflow?.path ?? null"
    2645              :       :follows-visible-workflow="agentPanelStore.followsVisibleWorkflow"
    2646              :       :selecting-tab-path="selectingTarget?.path ?? null"
    2647              :       :select-tab="onSelectWorkflowTarget"
    2648              :       :workflow-detached="workflowDetached"
    2649              :       :target-unavailable="agentPanelStore.targetUnavailable"
    2650              :       :canvas-empty-after-turn="canvasEmptyAfterTurn"
    2651              :       :get-mention-nodes="mentionableNodes"
    2652              :       :paywall-presentation="paywallPresentation"
    2653              :       :credits-exhausted="showStandingPaywall"
    2654              :       :blocked="hostFenced"
    2655              :       :sync-retry-available="
    2656              :         crdtStatus.terminal === 'retry_exhausted' ||
    2657              :         crdtStatus.terminal === 'timeout'
    2658              :       "
    2659              :       :free-use-placement="freeUsePlacement"
    2660              :       @free-use-notice="onFreeUseNotice"
    2661              :       @send="onSend"
    2662              :       @stop="onStop"
    2663              :       @attach="onAttach"
    2664              :       @attach-files="onAttachFiles"
    2665              :       @remove-attachment="attachment.cancelUpload"
    2666              :       @open-assets="onOpenAssets"
    2667              :       @select-nodes="onSelectNodes"
    2668              :       @remove-tag="onRemoveSelectionTag"
    2669              :       @mention-pick="onMentionPick"
    2670              :       @request-workflow-references="onRequestWorkflowReferences"
    2671              :       @remove-workflow-reference="composerStore.removeWorkflowReference"
    2672              :       @feedback="onFeedback"
    2673              :       @answer-ask="onAnswerAsk"
    2674              :       @approval-shown="onApprovalShown"
    2675              :       @open-workflow="onOpenApprovalWorkflow"
    2676              :       @open-reference-workflow="onNavigateToReferenceWorkflow"
    2677              :       @show-target="onShowTarget"
    2678              :       @paywall-action="onPaywallAction"
    2679              :       @standing-paywall-shown="onStandingPaywallShown"
    2680              :       @retry-sync="retrySubscription"
    2681              :       @new-chat="onNewChat"
    2682              :       @start-tour="restartCoach"
    2683              :       @toggle-size="agentPanelStore.toggleMaximize()"
    2684              :       @close="onClosePanel"
    2685              :       @open-history="onOpenHistory"
    2686              :       @delete-history="onDeleteHistory"
    2687              :       @rename-history="onRenameHistory"
    2688              :       @rename-chat="onRenameChat"
    2689              :       @copy-history="onCopyMarkdown"
    2690              :     >
    2691              :       <template #panelNotice>
    2692              :         <AgentHostFenceNotice
    2693              :           v-if="hostFenceMessage"
    2694              :           :message="hostFenceMessage"
    2695              :           class="mx-4 mt-3"
    2696              :         />
    2697              :       </template>
    2698              :       <template v-if="isCrdtDevPanelEnabled" #instrument>
    2699              :         <CrdtDevPanel :status="crdtStatus" :snapshot="crdtDebugSnapshot" />
    2700              :       </template>
    2701              :       <template #composerNotice>
    2702              :         <RunNoticeBanner
    2703              :           v-if="followFailure"
    2704              :           context="followFailed"
    2705              :           :workflow-name="followFailure.filename"
    2706              :           @retry="onRetryFollow"
    2707              :         />
    2708              :         <AgentCreditTransitionNotice
    2709              :           v-if="showCreditTransitionNotice"
    2710              :           data-testid="agent-credit-transition-notice"
    2711              :           @shown="onCreditTransitionNoticeShown"
    2712              :           @dismiss="onDismissCreditTransitionNotice"
    2713              :         />
    2714              :       </template>
    2715              :     </AgentPanel>
    2716              :     <div
    2717              :       v-if="assetDragActive"
    2718              :       role="status"
    2719              :       class="pointer-events-none absolute inset-2 z-20 flex flex-col items-center justify-center gap-3 rounded-xl border-2 border-dashed bg-secondary/90 p-4 font-inter text-sm/5 text-foreground"
    2720              :     >
    2721              :       <i
    2722              :         aria-hidden="true"
    2723              :         class="icon-[lucide--upload] size-8 text-muted-foreground"
    2724              :       />
    2725              :       <span>{{ t('agent.dragAndDropAssets') }}</span>
    2726              :     </div>
    2727              :     <OnboardingCoach
    2728              :       v-if="consentAccepted && onboardingKey && coachDeferredBy === null"
    2729              :       :key="onboardingKey"
    2730              :       ref="coachRef"
    2731              :       :steps="coachSteps"
    2732              :       :storage-key="onboardingKey"
    2733              :       :hold-notice="sendHeldByCoach ? t('agent.coachHoldNotice') : undefined"
    2734              :       @finished="onCoachFinished"
    2735              :       @stalled="
    2736              :         releaseCoachCompletionWaiters({
    2737              :           outcome: 'released',
    2738              :           reason: 'stalled'
    2739              :         })
    2740              :       "
    2741              :     />
    2742              :   </div>
    2743              : </template>
        

Generated by: LCOV version 2.0-1