Line data Source code
1 : import { useEventListener, whenever } from '@vueuse/core'
2 : import { defineStore } from 'pinia'
3 : import { v4 as uuidv4 } from 'uuid'
4 : import { computed, ref, watch } from 'vue'
5 :
6 : import { t } from '@/i18n'
7 : import { useCachedRequest } from '@/composables/useCachedRequest'
8 : import { useServerLogs } from '@/platform/customNodes/manager/services/useServerLogs'
9 : import { reportError } from '@comfyorg/telemetry/reportError'
10 : import { useToast } from '@/components/ui/toast/toastStore'
11 : import { api } from '@/scripts/api'
12 : import { app } from '@/scripts/app'
13 : import { useComfyRegistryService } from '@/services/comfyRegistryService'
14 :
15 : import { normalizePackKeys } from '@/platform/customNodes/manager/utils/packUtils'
16 : import { useManagerQueue } from '@/platform/customNodes/manager/composables/useManagerQueue'
17 : import { useComfyManagerService } from '@/platform/customNodes/manager/services/comfyManagerService'
18 : import type {
19 : NodePackId,
20 : RegistryPack,
21 : TaskLog
22 : } from '@/platform/customNodes/manager/types/comfyManagerTypes'
23 : import type { components } from '@comfyorg/api-client/generatedManagerTypes'
24 : import { versionStatusFilters } from '@/platform/customNodes/manager/utils/nodePackVersionUtil'
25 :
26 : type InstallPackParams = components['schemas']['InstallPackParams']
27 : type InstalledPacksResponse = components['schemas']['InstalledPacksResponse']
28 : type ManagerPackInfo = components['schemas']['ManagerPackInfo']
29 : type ManagerPackInstalled = components['schemas']['ManagerPackInstalled']
30 : type ManagerTaskHistory = Record<
31 : string,
32 : components['schemas']['TaskHistoryItem']
33 : >
34 : type ManagerTaskQueue = components['schemas']['TaskStateMessage']
35 : type UpdateAllPacksParams = components['schemas']['UpdateAllPacksParams']
36 :
37 2 : function isTaskForRequest(taskId: string, requestId: string) {
38 2 : return taskId === requestId || taskId.startsWith(`${requestId}_`)
39 : }
40 :
41 : /**
42 : * Store for state of installed node packs
43 : */
44 2356 : export const useComfyManagerStore = defineStore('comfyManager', () => {
45 2355 : const managerService = useComfyManagerService()
46 2355 : const toast = useToast()
47 :
48 2355 : const installedPacks = ref<InstalledPacksResponse>({})
49 2355 : const enabledPacksIds = ref<Set<NodePackId>>(new Set())
50 2355 : const disabledPacksIds = ref<Set<NodePackId>>(new Set())
51 2355 : const installedPacksIds = ref<Set<NodePackId>>(new Set())
52 2355 : const installingPacksIds = ref<Set<NodePackId>>(new Set())
53 2355 : const updatingPacksIds = ref<Set<NodePackId>>(new Set())
54 2355 : const isStale = ref(true)
55 2355 : const taskLogs = ref<TaskLog[]>([])
56 2355 : const succeededTasksLogs = ref<TaskLog[]>([])
57 2355 : const failedTasksLogs = ref<TaskLog[]>([])
58 :
59 2355 : const serverTaskHistory = ref<ManagerTaskHistory>({})
60 2355 : const requestFailures = ref<ManagerTaskHistory>({})
61 2355 : const taskHistory = computed(() => ({
62 : ...requestFailures.value,
63 : ...serverTaskHistory.value
64 : }))
65 2355 : const queueError = ref<string | null>(null)
66 2355 : const succeededTasksIds = ref<string[]>([])
67 2355 : const failedTasksIds = ref<string[]>([])
68 2355 : const taskQueue = ref<ManagerTaskQueue>({
69 : history: {},
70 : running_queue: [],
71 : pending_queue: [],
72 : installed_packs: {}
73 : })
74 :
75 : // Track task ID to pack ID mapping for proper state cleanup
76 2355 : const taskIdToPackId = ref(new Map<string, NodePackId>())
77 :
78 2355 : const managerQueue = useManagerQueue(
79 : serverTaskHistory,
80 : taskQueue,
81 : installedPacks
82 : )
83 2355 : const pendingRequests = ref(
84 : new Map<string, 'submitting' | 'queued' | 'confirmed'>()
85 : )
86 2355 : let queueStartRequest = 0
87 2355 : const isProcessingTasks = computed(
88 : () =>
89 : managerQueue.isProcessing.value ||
90 : pendingRequests.value.size > 0 ||
91 : updatingPacksIds.value.size > 0
92 : )
93 :
94 2355 : useEventListener(
95 : app.api,
96 : ['cm-task-started', 'cm-task-completed'],
97 : (
98 : event: CustomEvent<
99 : | components['schemas']['MessageTaskStarted']
100 : | components['schemas']['MessageTaskDone']
101 : >
102 : ) => {
103 1 : const { ui_id: taskId, state } = event.detail
104 1 : const observedIds = [
105 : ...state.running_queue.map((task) => task.ui_id),
106 : ...state.pending_queue.map((task) => task.ui_id),
107 : ...Object.keys(state.history)
108 : ]
109 1 : for (const id of observedIds) {
110 1 : delete requestFailures.value[id]
111 : }
112 1 : for (const [requestId, requestState] of pendingRequests.value) {
113 1 : if (
114 : requestState === 'confirmed' ||
115 : observedIds.some((id) => isTaskForRequest(id, requestId))
116 : ) {
117 1 : pendingRequests.value.delete(requestId)
118 : }
119 : }
120 1 : if (event.type === 'cm-task-completed') {
121 1 : const packId = taskIdToPackId.value.get(taskId)
122 1 : if (packId) installingPacksIds.value.delete(packId)
123 1 : taskIdToPackId.value.delete(taskId)
124 : }
125 1 : if (
126 : managerQueue.currentQueueLength.value > 0 ||
127 : !isProcessingTasks.value
128 : ) {
129 1 : queueError.value = null
130 : }
131 : }
132 : )
133 :
134 2355 : const setStale = () => {
135 0 : isStale.value = true
136 : }
137 :
138 2 : function isTaskFailed(requestId: string) {
139 2 : return failedTasksIds.value.some((id) => isTaskForRequest(id, requestId))
140 : }
141 :
142 0 : function isTaskInProgress(requestId: string) {
143 0 : return (
144 : pendingRequests.value.has(requestId) ||
145 : [...taskQueue.value.running_queue, ...taskQueue.value.pending_queue].some(
146 : (task) => isTaskForRequest(task.ui_id, requestId)
147 : )
148 : )
149 : }
150 :
151 2355 : const partitionTaskLogs = () => {
152 2 : const successTaskLogs: TaskLog[] = []
153 2 : const failTaskLogs: TaskLog[] = []
154 2 : for (const log of taskLogs.value) {
155 2 : if (isTaskFailed(log.taskId)) {
156 1 : failTaskLogs.push(log)
157 : } else {
158 1 : successTaskLogs.push(log)
159 : }
160 : }
161 2 : succeededTasksLogs.value = successTaskLogs
162 2 : failedTasksLogs.value = failTaskLogs
163 : }
164 :
165 2355 : const partitionTasks = () => {
166 1 : const successTasksIds = []
167 1 : const failTasksIds = []
168 1 : for (const task of Object.values(taskHistory.value)) {
169 1 : if (task.status?.status_str === 'success') {
170 0 : successTasksIds.push(task.ui_id)
171 : } else {
172 1 : failTasksIds.push(task.ui_id)
173 : }
174 : }
175 1 : succeededTasksIds.value = successTasksIds
176 1 : failedTasksIds.value = failTasksIds
177 : }
178 :
179 2355 : whenever(
180 : taskHistory,
181 : () => {
182 1 : partitionTasks()
183 1 : partitionTaskLogs()
184 : },
185 : { deep: true }
186 : )
187 :
188 2355 : const getPackId = (pack: ManagerPackInstalled) => pack.cnr_id || pack.aux_id
189 :
190 2355 : const isInstalledPackId = (packName: NodePackId | undefined): boolean =>
191 : !!packName && installedPacksIds.value.has(packName)
192 :
193 2355 : const isEnabledPackId = (packName: NodePackId | undefined): boolean =>
194 : !!packName &&
195 : isInstalledPackId(packName) &&
196 : enabledPacksIds.value.has(packName)
197 :
198 2355 : const isInstallingPackId = (packName: NodePackId | undefined): boolean =>
199 : !!packName &&
200 : (installingPacksIds.value.has(packName) ||
201 : updatingPacksIds.value.has(packName))
202 :
203 2355 : const packsToIdSet = (packs: ManagerPackInstalled[]) =>
204 : packs.reduce((acc, pack) => {
205 32 : const id = pack.cnr_id || pack.aux_id
206 32 : if (id) acc.add(id)
207 32 : return acc
208 : }, new Set<NodePackId>())
209 :
210 : /**
211 : * A pack is disabled if there is a disabled entry and no corresponding
212 : * enabled entry. If `packname@1.0.2` is disabled, but `packname@1.0.3` is
213 : * enabled, then `packname` is considered enabled.
214 : *
215 : * @example
216 : * installedPacks = {
217 : * "packname@1_0_2": { enabled: false, cnr_id: "packname" },
218 : * "packname": { enabled: true, cnr_id: "packname" }
219 : * }
220 : * isDisabled("packname") // false
221 : *
222 : * installedPacks = {
223 : * "packname@1_0_2": { enabled: false, cnr_id: "packname" },
224 : * }
225 : * isDisabled("packname") // true
226 : */
227 2355 : const updateDisabledIds = (packs: ManagerPackInstalled[]) => {
228 : // Use temporary variables to avoid triggering reactivity
229 16 : const enabledIds = new Set<NodePackId>()
230 16 : const disabledIds = new Set<NodePackId>()
231 :
232 16 : for (const pack of packs) {
233 32 : const id = getPackId(pack)
234 32 : if (!id) continue
235 :
236 32 : const { enabled } = pack
237 :
238 32 : if (enabled) enabledIds.add(id)
239 16 : else disabledIds.add(id)
240 :
241 : // If pack in both (has a disabled and enabled version), remove from disabled
242 : const inBothSets = enabledIds.has(id) && disabledIds.has(id)
243 32 : if (inBothSets) disabledIds.delete(id)
244 : }
245 :
246 16 : enabledPacksIds.value = enabledIds
247 16 : disabledPacksIds.value = disabledIds
248 : }
249 :
250 2355 : const updateInstalledIds = (packs: ManagerPackInstalled[]) => {
251 16 : installedPacksIds.value = packsToIdSet(packs)
252 : }
253 :
254 2355 : const onPacksChanged = () => {
255 16 : const packs = Object.values(installedPacks.value)
256 16 : updateDisabledIds(packs)
257 16 : updateInstalledIds(packs)
258 : }
259 :
260 2355 : watch(installedPacks, onPacksChanged, { deep: true })
261 :
262 2355 : const refreshInstalledList = async () => {
263 2370 : const packs = await managerService.listInstalledPacks()
264 2370 : if (packs) {
265 : // Normalize pack keys to ensure consistent access
266 15 : installedPacks.value = normalizePackKeys(packs)
267 : }
268 2370 : isStale.value = false
269 : }
270 :
271 2355 : whenever(isStale, refreshInstalledList, { immediate: true })
272 :
273 1 : async function confirmQueuedRequests() {
274 1 : const queuedRequests = [...pendingRequests.value]
275 : .filter(([, state]) => state !== 'submitting')
276 : .map(([id]) => id)
277 1 : if (!queuedRequests.length) return true
278 :
279 1 : const runningQueue = taskQueue.value.running_queue
280 1 : const requestId = queueStartRequest
281 1 : const status = await managerService.getQueueStatus(
282 : api.clientId ?? api.initialClientId ?? 'unknown'
283 : )
284 1 : if (requestId !== queueStartRequest) return true
285 1 : if (status === null) return false
286 :
287 1 : const hasNewQueueState = taskQueue.value.running_queue !== runningQueue
288 1 : for (const id of queuedRequests) {
289 1 : if (status.total_count > 0 && !hasNewQueueState) {
290 0 : pendingRequests.value.set(id, 'confirmed')
291 : } else {
292 1 : pendingRequests.value.delete(id)
293 : }
294 : }
295 1 : return true
296 : }
297 :
298 1 : async function startQueue() {
299 1 : const requestId = ++queueStartRequest
300 1 : queueError.value = null
301 1 : const result = await managerService.startQueue()
302 1 : if (requestId !== queueStartRequest) return
303 1 : if (result !== null && (await confirmQueuedRequests())) return
304 :
305 1 : if (requestId === queueStartRequest && isProcessingTasks.value) {
306 0 : queueError.value = managerService.error.value ?? t('g.unknownError')
307 0 : toast.error(t('g.error'), { description: queueError.value })
308 : }
309 : }
310 :
311 2355 : const enqueueTaskWithLogs = async (
312 : task: (taskId: string) => Promise<string | null>,
313 : taskName: string
314 : ) => {
315 1 : const taskId = uuidv4()
316 1 : const { logs } = useServerLogs({
317 : ui_id: taskId,
318 : immediate: true
319 : })
320 :
321 1 : pendingRequests.value.set(taskId, 'submitting')
322 1 : taskLogs.value.push({ taskName, taskId, logs: logs.value })
323 1 : partitionTaskLogs()
324 :
325 : const result = await task(taskId)
326 1 : if (result === null) {
327 0 : const packId = taskIdToPackId.value.get(taskId)
328 0 : if (packId) installingPacksIds.value.delete(packId)
329 0 : taskIdToPackId.value.delete(taskId)
330 0 : pendingRequests.value.delete(taskId)
331 0 : const message = managerService.error.value ?? t('g.unknownError')
332 0 : toast.error(t('g.error'), { description: message })
333 :
334 0 : requestFailures.value[taskId] = {
335 : ui_id: taskId,
336 : client_id: api.clientId || 'unknown',
337 : kind: 'error',
338 : result: 'failed',
339 : status: {
340 : status_str: 'error',
341 : completed: false,
342 : messages: [message]
343 : },
344 : timestamp: new Date().toISOString()
345 : }
346 0 : return
347 : }
348 1 : if (pendingRequests.value.has(taskId)) {
349 1 : pendingRequests.value.set(taskId, 'queued')
350 : }
351 1 : await startQueue()
352 : }
353 :
354 2355 : const installPack = useCachedRequest<InstallPackParams, void>(
355 : async (params: InstallPackParams, signal?: AbortSignal) => {
356 1 : if (!params.id || installingPacksIds.value.has(params.id)) return
357 :
358 1 : let actionDescription = t('g.installing')
359 1 : if (installedPacksIds.value.has(params.id)) {
360 0 : const installedPack = installedPacks.value[params.id]
361 :
362 0 : if (installedPack.ver !== params.selected_version) {
363 0 : actionDescription = t('manager.changingVersion', {
364 : from: installedPack.ver,
365 : to: params.selected_version
366 : })
367 : } else {
368 0 : actionDescription = t('g.enabling')
369 : }
370 : }
371 :
372 1 : installingPacksIds.value.add(params.id)
373 1 : const task = (taskId: string) => {
374 1 : taskIdToPackId.value.set(taskId, params.id)
375 1 : return managerService.installPack(params, taskId, signal)
376 : }
377 1 : await enqueueTaskWithLogs(task, `${actionDescription} ${params.id}`)
378 : },
379 : { maxSize: 1 }
380 : )
381 :
382 0 : async function switchPack(
383 : pack: RegistryPack & { id: NodePackId },
384 : policy: keyof typeof versionStatusFilters
385 : ) {
386 0 : const registry = useComfyRegistryService()
387 0 : const packName = pack.name ?? pack.id
388 0 : const versions = await registry.getPackVersions(pack.id, {
389 : statuses: versionStatusFilters[policy]
390 : })
391 0 : if (!isEnabledPackId(pack.id)) return
392 0 : if (versions === null) {
393 0 : toast.error(packName, {
394 : description: registry.error.value ?? t('manager.errorConnecting')
395 : })
396 0 : return
397 : }
398 0 : const version = versions[0]?.version
399 0 : if (!version) {
400 0 : toast.warning(packName, { description: t('manager.noUpdateVersion') })
401 0 : return
402 : }
403 0 : if (version === getInstalledPackVersion(pack.id)) {
404 0 : toast.info(packName, {
405 : description: t('manager.updateVersionInstalled', { version })
406 : })
407 0 : return
408 : }
409 0 : await installPack.call({
410 : id: pack.id,
411 : version,
412 : selected_version: version,
413 : repository: pack.repository ?? '',
414 : channel: 'default',
415 : mode: 'cache'
416 : })
417 : }
418 :
419 0 : async function updatePacks(
420 : packs: RegistryPack[],
421 : policy: keyof typeof versionStatusFilters = 'active'
422 : ) {
423 0 : const packsToUpdate = packs.filter(
424 : (pack): pack is RegistryPack & { id: NodePackId } =>
425 : isEnabledPackId(pack.id) && !isInstallingPackId(pack.id)
426 : )
427 0 : if (!packsToUpdate.length) return
428 0 : for (const pack of packsToUpdate) {
429 0 : updatingPacksIds.value.add(pack.id)
430 : }
431 0 : try {
432 0 : for (const pack of packsToUpdate) {
433 0 : await switchPack(pack, policy)
434 : }
435 : } catch (error) {
436 0 : reportError(error, {
437 : surface: 'platform',
438 : errorType: 'failure_updating_node_packs'
439 : })
440 0 : toast.error(t('manager.update'), {
441 : description: t('manager.updateFailed')
442 : })
443 : } finally {
444 0 : for (const pack of packsToUpdate) {
445 0 : updatingPacksIds.value.delete(pack.id)
446 : }
447 0 : installPack.clear()
448 : }
449 : }
450 :
451 2355 : const uninstallPack = async (
452 : params: ManagerPackInfo,
453 : signal?: AbortSignal
454 : ) => {
455 0 : installPack.clear()
456 0 : installPack.cancel()
457 :
458 0 : installingPacksIds.value.add(params.id)
459 0 : const uninstallParams: components['schemas']['UninstallPackParams'] = {
460 : node_name: params.id,
461 : is_unknown: false
462 : }
463 0 : const task = (taskId: string) => {
464 0 : taskIdToPackId.value.set(taskId, params.id)
465 0 : return managerService.uninstallPack(uninstallParams, taskId, signal)
466 : }
467 0 : await enqueueTaskWithLogs(
468 : task,
469 : t('manager.uninstalling', { id: params.id })
470 : )
471 : }
472 :
473 2355 : const updatePack = useCachedRequest<ManagerPackInfo, void>(
474 : async (params: ManagerPackInfo, signal?: AbortSignal) => {
475 0 : updateAllPacks.cancel()
476 0 : const updateParams: components['schemas']['UpdatePackParams'] = {
477 : node_name: params.id,
478 : node_ver: params.version
479 : }
480 0 : const task = (taskId: string) =>
481 : managerService.updatePack(updateParams, taskId, signal)
482 0 : await enqueueTaskWithLogs(task, t('g.updating', { id: params.id }))
483 : },
484 : { maxSize: 1 }
485 : )
486 :
487 2355 : const updateAllPacks = useCachedRequest<UpdateAllPacksParams, void>(
488 : async (params: UpdateAllPacksParams, signal?: AbortSignal) => {
489 0 : const task = (taskId: string) =>
490 : managerService.updateAllPacks(params, taskId, signal)
491 0 : await enqueueTaskWithLogs(task, t('manager.updatingAllPacks'))
492 : },
493 : { maxSize: 1 }
494 : )
495 :
496 2355 : const disablePack = async (params: ManagerPackInfo, signal?: AbortSignal) => {
497 0 : const disableParams: components['schemas']['DisablePackParams'] = {
498 : node_name: params.id,
499 : is_unknown: false
500 : }
501 0 : const task = (taskId: string) =>
502 : managerService.disablePack(disableParams, taskId, signal)
503 0 : await enqueueTaskWithLogs(task, t('g.disabling', { id: params.id }))
504 : }
505 :
506 2355 : const enablePack = async (params: ManagerPackInfo, signal?: AbortSignal) => {
507 0 : const enableParams: components['schemas']['EnablePackParams'] = {
508 : cnr_id: params.id
509 : }
510 0 : const task = (taskId: string) =>
511 : managerService.enablePack(enableParams, taskId, signal)
512 0 : await enqueueTaskWithLogs(task, t('g.enabling', { id: params.id }))
513 : }
514 :
515 2355 : const getInstalledPackVersion = (packId: NodePackId) => {
516 231 : if (!Object.hasOwn(installedPacks.value, packId)) return
517 : const pack = installedPacks.value[packId]
518 32 : return pack.ver
519 : }
520 :
521 2355 : const clearLogs = () => {
522 0 : taskLogs.value = []
523 : }
524 :
525 2355 : const resetTaskState = () => {
526 0 : queueError.value = null
527 : // Clear all task-related reactive state for fresh start after restart
528 0 : taskLogs.value = []
529 0 : serverTaskHistory.value = {}
530 0 : requestFailures.value = {}
531 0 : succeededTasksIds.value = []
532 0 : failedTasksIds.value = []
533 0 : succeededTasksLogs.value = []
534 0 : failedTasksLogs.value = []
535 0 : installingPacksIds.value.clear()
536 0 : updatingPacksIds.value.clear()
537 0 : taskIdToPackId.value.clear()
538 0 : pendingRequests.value.clear()
539 0 : queueStartRequest++
540 0 : managerQueue.isProcessing.value = false
541 :
542 : // Reset task queue to initial state
543 0 : taskQueue.value = {
544 : history: {},
545 : running_queue: [],
546 : pending_queue: [],
547 : installed_packs: {}
548 : }
549 : }
550 :
551 2355 : return {
552 : // Manager state
553 : isLoading: managerService.isLoading,
554 : error: managerService.error,
555 : taskLogs,
556 : clearLogs,
557 : resetTaskState,
558 : setStale,
559 :
560 : // Installed packs state
561 : installedPacks,
562 : installedPacksIds,
563 : isPackInstalled: isInstalledPackId,
564 : isPackEnabled: isEnabledPackId,
565 : isPackInstalling: isInstallingPackId,
566 : getInstalledPackVersion,
567 : refreshInstalledList,
568 :
569 : // Task queue state and actions
570 : taskHistory,
571 : taskQueue,
572 : queueError,
573 : startQueue,
574 : isProcessingTasks,
575 : succeededTasksIds,
576 : failedTasksIds,
577 : isTaskFailed,
578 : isTaskInProgress,
579 : succeededTasksLogs,
580 : failedTasksLogs,
581 : managerQueue,
582 :
583 : // Pack actions
584 : installPack,
585 : uninstallPack,
586 : updatePack,
587 : updatePacks,
588 : updateAllPacks,
589 : disablePack,
590 : enablePack
591 : }
592 : })
|