Line data Source code
1 : import {
2 : createSharedComposable,
3 : tryOnScopeDispose,
4 : useThrottleFn
5 : } from '@vueuse/core'
6 : import { isEqual } from 'es-toolkit'
7 : import { storeToRefs } from 'pinia'
8 : import { computed, shallowRef, watch } from 'vue'
9 :
10 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
11 : import type {
12 : ExecutableLGraphNode,
13 : ExecutionId,
14 : LGraph
15 : } from '@/lib/litegraph/src/litegraph'
16 : import { ExecutableNodeDTO } from '@/lib/litegraph/src/litegraph'
17 : import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums'
18 : import { reportError } from '@comfyorg/telemetry/reportError'
19 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
20 : import type { DeploymentCompatibility } from '@/platform/workspace/api/workspaceApi'
21 : import { workspaceApi } from '@/platform/workspace/api/workspaceApi'
22 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
23 : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
24 : import { api } from '@/scripts/api'
25 : import { app } from '@/scripts/app'
26 :
27 : /** Whether a deployment, or Comfy Cloud, can run the open workflow. */
28 : export type DeploymentCompatibilityMark =
29 : | { kind: 'runs' }
30 : | { kind: 'missing'; nodeTypes: string[] }
31 : | { kind: 'unknown' }
32 :
33 0 : function isSkippedByExecution(mode: LGraphEventMode | undefined): boolean {
34 0 : return mode === LGraphEventMode.NEVER || mode === LGraphEventMode.BYPASS
35 : }
36 :
37 : /** The distinct, sorted node types graphToPrompt would send for a graph. */
38 0 : export function workflowNodeTypes(graph: LGraph): string[] {
39 0 : const types = new Set<string>()
40 0 : const executableNodes = new Map<ExecutionId, ExecutableLGraphNode>()
41 :
42 0 : for (const node of graph.nodes) {
43 0 : if (isSkippedByExecution(node.mode)) continue
44 0 : const dto = new ExecutableNodeDTO(node, [], executableNodes)
45 0 : for (const inner of dto.getInnerNodes()) {
46 0 : if (inner.isVirtualNode || isSkippedByExecution(inner.mode)) continue
47 0 : const source = inner instanceof ExecutableNodeDTO ? inner.node : null
48 0 : types.add(
49 : source?.last_serialization?.type ?? inner.comfyClass ?? inner.type
50 : )
51 : }
52 : }
53 :
54 0 : return [...types].sort()
55 : }
56 :
57 0 : function missingMark(missing: string[]): DeploymentCompatibilityMark {
58 0 : return missing.length === 0
59 : ? { kind: 'runs' }
60 : : { kind: 'missing', nodeTypes: missing }
61 : }
62 :
63 : /**
64 : * Which of the workspace's deployments, and Comfy Cloud, can run the open
65 : * workflow. Off, it loads nothing, asks nothing and marks nothing.
66 : */
67 2355 : export const useDeploymentCompatibility = createSharedComposable(() => {
68 2009 : const { flags } = useFeatureFlags()
69 2009 : const pickStore = useDeploymentPickStore()
70 2009 : const { deployments, isVisible } = storeToRefs(pickStore)
71 2009 : const workspaceStore = useTeamWorkspaceStore()
72 2009 : const workflowStore = useWorkflowStore()
73 :
74 2009 : const nodeTypes = shallowRef<string[]>([])
75 :
76 2009 : const canAsk = computed(
77 : () =>
78 : flags.deploymentCompatibilityMarksEnabled &&
79 : !!workspaceStore.workspaceId &&
80 : isVisible.value &&
81 : deployments.value.length > 0
82 : )
83 :
84 6250 : function readNodeTypes() {
85 6250 : const next =
86 : canAsk.value && app.isGraphReady ? workflowNodeTypes(app.rootGraph) : []
87 : // graphChanged also fires on moves and resizes; keep the same array then
88 : // so the request key is not rebuilt.
89 6250 : if (isEqual(next, nodeTypes.value)) return
90 0 : nodeTypes.value = next
91 : }
92 2009 : const throttledRead = useThrottleFn(readNodeTypes, 200, true, true)
93 2009 : const onGraphChanged = () => {
94 1124 : void throttledRead()
95 : }
96 2009 : api.addEventListener('graphChanged', onGraphChanged)
97 2009 : tryOnScopeDispose(() => {
98 72 : api.removeEventListener('graphChanged', onGraphChanged)
99 : })
100 2009 : watch([() => workflowStore.activeWorkflow, canAsk], readNodeTypes, {
101 : immediate: true
102 : })
103 :
104 2009 : watch(
105 : [
106 : () => workspaceStore.workspaceId,
107 : () => flags.deploymentCompatibilityMarksEnabled
108 : ],
109 : ([workspaceId, enabled]) => {
110 2018 : if (enabled && workspaceId && pickStore.state.phase === 'idle') {
111 0 : void pickStore.loadOnce()
112 : }
113 : },
114 : { immediate: true }
115 : )
116 :
117 2009 : const request = computed(() => {
118 2018 : const workspaceId = workspaceStore.workspaceId
119 2018 : if (!workspaceId || !canAsk.value || nodeTypes.value.length === 0) {
120 2018 : return null
121 : }
122 0 : return {
123 : workspaceId,
124 : nodeTypes: nodeTypes.value,
125 : key: JSON.stringify([
126 : workspaceId,
127 : deployments.value.map((d) => [d.deployment_id, d.release_id, d.status]),
128 : nodeTypes.value
129 : ])
130 : }
131 : })
132 2009 : const requestKey = computed(() => request.value?.key ?? null)
133 :
134 2009 : const answer = shallowRef<{
135 : key: string
136 : compatibility: DeploymentCompatibility
137 : } | null>(null)
138 2009 : let checkFailureReported = false
139 2009 : let latestCheck = 0
140 :
141 2009 : watch(
142 : requestKey,
143 : async (key) => {
144 2009 : const check = ++latestCheck
145 2009 : answer.value = null
146 2009 : const current = request.value
147 2009 : if (key === null || current === null) return
148 2009 : try {
149 0 : const compatibility = await workspaceApi.checkDeploymentCompatibility(
150 : current.workspaceId,
151 : current.nodeTypes
152 : )
153 0 : if (check === latestCheck) answer.value = { key, compatibility }
154 : } catch (error) {
155 0 : if (checkFailureReported) return
156 0 : checkFailureReported = true
157 0 : reportError(error, {
158 : surface: 'workspace',
159 : errorType: 'deployment_compatibility_check_failure',
160 : level: 'warning'
161 : })
162 : }
163 : },
164 : { immediate: true }
165 : )
166 :
167 2009 : const compatibility = computed(() =>
168 : answer.value !== null && answer.value.key === requestKey.value
169 : ? answer.value.compatibility
170 : : null
171 : )
172 :
173 0 : function markFor(
174 : deploymentId: string | null
175 : ): DeploymentCompatibilityMark | null {
176 0 : const current = compatibility.value
177 0 : if (current === null) return null
178 0 : if (deploymentId === null) {
179 0 : return missingMark(current.cloud.missing_node_types)
180 : }
181 0 : const entry = current.deployments.find(
182 : (d) => d.deployment_id === deploymentId
183 : )
184 0 : if (!entry) return null
185 0 : if (entry.unknown) return { kind: 'unknown' }
186 0 : return missingMark(entry.missing_node_types)
187 : }
188 :
189 : const deploymentsThatRunIt = computed<(string | null)[] | null>(() => {
190 0 : if (compatibility.value === null) return null
191 0 : return [null, ...deployments.value.map((d) => d.deployment_id)].filter(
192 : (id) => markFor(id)?.kind === 'runs'
193 : )
194 : })
195 :
196 2009 : return { compatibility, markFor, deploymentsThatRunIt }
197 : })
|