Line data Source code
1 : import { reportError } from '@comfyorg/telemetry/reportError'
2 : import type { Component, ComputedRef } from 'vue'
3 : import { computed, defineAsyncComponent, watch } from 'vue'
4 :
5 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
6 : import { useAgentConsentStore } from '@/workbench/extensions/agent/stores/agent/agentConsentStore'
7 : import { useAgentPanelRenderStore } from '@/workbench/extensions/agent/stores/agent/agentPanelRenderStore'
8 : import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore'
9 :
10 : import { useBillingIdentity } from './useBillingIdentity'
11 :
12 : interface AgentDockMount {
13 : docked: ComputedRef<boolean>
14 : DockedAgentPanel: Component
15 : }
16 :
17 2444 : export function useAgentDockMount(): AgentDockMount {
18 2444 : const agentPanelStore = useAgentPanelStore()
19 2444 : useAgentPanelRenderStore()
20 2444 : const { subscription, fetchStatus } = useBillingContext()
21 :
22 : // This composable is owned by the persistent graph/linear hosts, unlike the
23 : // dock component, which is unmounted whenever the panel closes. Keep the
24 : // exhaustion episode synchronized here so funds recovery while closed can
25 : // re-arm the next impression.
26 2444 : watch(
27 : () => subscription.value?.agentHasFunds,
28 : (hasFunds) => {
29 2466 : if (hasFunds === true) agentPanelStore.reportedExhaustionIdentity = null
30 : },
31 : { immediate: true }
32 : )
33 :
34 2444 : const consentStore = useAgentConsentStore()
35 2444 : watch(
36 : () => consentStore.confirmedAcceptances,
37 : (count) => {
38 2444 : if (!count || count <= agentPanelStore.billingRefreshedAcceptances) return
39 0 : agentPanelStore.billingRefreshedAcceptances = count
40 0 : void fetchStatus({ fresh: true }).catch((error: unknown) => {
41 0 : reportError(error, {
42 : surface: 'agent',
43 : errorType: 'error_refreshing_agent_billing_status'
44 : })
45 : })
46 : },
47 : { immediate: true, flush: 'sync' }
48 : )
49 :
50 2444 : const billingIdentity = useBillingIdentity()
51 :
52 2444 : watch(
53 : [billingIdentity, () => subscription.value?.agentScopedHasFunds],
54 : ([identity, scopedHasFunds]) => {
55 2465 : if (scopedHasFunds === undefined) return
56 0 : agentPanelStore.observeAgentScopedFunds(identity, scopedHasFunds)
57 : },
58 : { immediate: true }
59 : )
60 :
61 2444 : return {
62 : docked: computed(() => agentPanelStore.isVisible),
63 : DockedAgentPanel: defineAsyncComponent(
64 : () =>
65 : import('@/workbench/extensions/agent/components/agent/DockedAgentPanel.vue')
66 : )
67 : }
68 : }
|