Line data Source code
1 : import { computed } from 'vue'
2 : import { useI18n } from 'vue-i18n'
3 :
4 : import type { components } from '@/types/comfyRegistryTypes'
5 : import type { components as ManagerComponents } from '@comfyorg/api-client/generatedManagerTypes'
6 : import { useConflictDetection } from '@/platform/customNodes/manager/composables/useConflictDetection'
7 : import { useNodeConflictDialog } from '@/platform/customNodes/manager/composables/dialogs/useNodeConflictDialog'
8 : import { useComfyManagerStore } from '@/platform/customNodes/manager/stores/comfyManagerStore'
9 : import type { ConflictDetail } from '@/platform/customNodes/manager/types/conflictDetectionTypes'
10 :
11 : type NodePack = components['schemas']['Node']
12 :
13 268 : export function usePackInstall(
14 : getNodePacks: () => NodePack[],
15 : getHasConflict?: () => boolean | undefined,
16 : getConflictInfo?: () => ConflictDetail[] | undefined
17 : ) {
18 268 : const managerStore = useComfyManagerStore()
19 268 : const { show: showNodeConflictDialog } = useNodeConflictDialog()
20 268 : const { checkNodeCompatibility } = useConflictDetection()
21 268 : const { t } = useI18n()
22 :
23 : // Check if any of the packs are currently being installed
24 268 : const isInstalling = computed(() => {
25 190 : const nodePacks = getNodePacks()
26 190 : if (!nodePacks.length) return false
27 190 : return nodePacks.some((pack) => managerStore.isPackInstalling(pack.id))
28 : })
29 :
30 268 : const createPayload = (installItem: NodePack) => {
31 0 : if (!installItem.id) {
32 0 : console.error(t('manager.packInstall.nodeIdRequired'))
33 0 : return
34 : }
35 :
36 : const isUnclaimedPack = installItem.publisher?.name === 'Unclaimed'
37 0 : const versionToInstall = isUnclaimedPack
38 : ? ('nightly' as ManagerComponents['schemas']['SelectedVersion'])
39 : : (installItem.latest_version?.version ?? 'latest')
40 :
41 0 : return {
42 : id: installItem.id,
43 : repository: installItem.repository ?? '',
44 : channel: 'dev' as ManagerComponents['schemas']['ManagerChannel'],
45 : mode: 'cache' as ManagerComponents['schemas']['ManagerDatabaseSource'],
46 : selected_version: versionToInstall,
47 : version: versionToInstall
48 : }
49 : }
50 :
51 268 : const installPack = (item: NodePack) => {
52 0 : const payload = createPayload(item)
53 0 : if (!payload) return Promise.resolve()
54 0 : return managerStore.installPack.call(payload)
55 : }
56 :
57 268 : const performInstallation = async (packs: NodePack[]) => {
58 0 : try {
59 : const results = await Promise.allSettled(packs.map(installPack))
60 0 : const failures = results.filter(
61 : (r): r is PromiseRejectedResult => r.status === 'rejected'
62 : )
63 0 : if (failures.length) {
64 0 : console.error(
65 : '[usePackInstall] Some installations failed:',
66 : failures.map((f) => f.reason)
67 : )
68 : }
69 : } finally {
70 0 : managerStore.installPack.clear()
71 : }
72 : }
73 :
74 268 : const installAllPacks = async () => {
75 0 : const nodePacks = getNodePacks()
76 0 : if (!nodePacks.length) return
77 :
78 0 : const hasConflict = getHasConflict?.()
79 0 : const conflictInfo = getConflictInfo?.()
80 :
81 0 : if (hasConflict) {
82 0 : if (!conflictInfo) return
83 :
84 0 : const conflictedPackages = nodePacks
85 : .map((pack) => {
86 0 : const compatibilityCheck = checkNodeCompatibility(pack)
87 0 : return {
88 : package_id: pack.id || '',
89 : package_name: pack.name || '',
90 : has_conflict: compatibilityCheck.hasConflict,
91 : conflicts: compatibilityCheck.conflicts,
92 : is_compatible: !compatibilityCheck.hasConflict
93 : }
94 : })
95 : .filter((result) => result.has_conflict)
96 :
97 0 : showNodeConflictDialog({
98 : conflictedPackages,
99 : buttonText: t('manager.conflicts.installAnyway'),
100 : onButtonClick: async () => {
101 0 : const uninstalledPacks = nodePacks.filter(
102 : (pack) => !managerStore.isPackInstalled(pack.id)
103 : )
104 0 : if (!uninstalledPacks.length) return
105 0 : await performInstallation(uninstalledPacks)
106 : }
107 : })
108 0 : return
109 : }
110 :
111 0 : const uninstalledPacks = nodePacks.filter(
112 : (pack) => !managerStore.isPackInstalled(pack.id)
113 : )
114 0 : if (!uninstalledPacks.length) return
115 0 : await performInstallation(uninstalledPacks)
116 : }
117 :
118 268 : return { isInstalling, installAllPacks, performInstallation }
119 : }
|