Line data Source code
1 : import { isEmpty, isNil } from 'es-toolkit/compat'
2 :
3 : import type {
4 : RegistryAccelerator,
5 : RegistryOS
6 : } from '@/platform/customNodes/manager/types/compatibility.types'
7 : import type { ConflictDetail } from '@/platform/customNodes/manager/types/conflictDetectionTypes'
8 :
9 : /**
10 : * Maps system OS string to Registry OS format
11 : * @param systemOS Raw OS string from system stats ('darwin', 'win32', 'linux', etc)
12 : * @returns Registry OS or undefined if unknown
13 : */
14 0 : function getRegistryOS(systemOS?: string): RegistryOS | undefined {
15 0 : if (!systemOS) return undefined
16 :
17 0 : const lower = systemOS.toLowerCase()
18 : // Check darwin first to avoid matching 'win' in 'darwin'
19 0 : if (lower.includes('darwin') || lower.includes('mac')) return 'macOS'
20 0 : if (lower.includes('win')) return 'Windows'
21 0 : if (lower.includes('linux')) return 'Linux'
22 :
23 : return undefined
24 : }
25 :
26 : /**
27 : * Maps device type to Registry accelerator format
28 : * @param deviceType Raw device type from system stats ('cuda', 'mps', 'rocm', 'cpu', etc)
29 : * @returns Registry accelerator
30 : */
31 0 : function getRegistryAccelerator(deviceType?: string): RegistryAccelerator {
32 0 : if (!deviceType) return 'CPU'
33 :
34 0 : const lower = deviceType.toLowerCase()
35 0 : if (lower === 'cuda') return 'CUDA'
36 0 : if (lower === 'mps') return 'Metal'
37 0 : if (lower === 'rocm') return 'ROCm'
38 :
39 0 : return 'CPU'
40 : }
41 :
42 : /**
43 : * Checks OS compatibility
44 : * @param supported Supported OS list from Registry (null/undefined = all OS supported)
45 : * @param current Current system OS
46 : * @returns ConflictDetail if incompatible, null if compatible
47 : */
48 223 : export function checkOSCompatibility(
49 : supported?: RegistryOS[] | null,
50 : current?: string
51 : ): ConflictDetail | null {
52 : // null/undefined/empty = all OS supported
53 223 : if (isNil(supported) || isEmpty(supported)) return null
54 :
55 0 : const currentOS = getRegistryOS(current)
56 0 : if (!currentOS) {
57 0 : return {
58 : type: 'os',
59 : current_value: 'Unknown',
60 : required_value: supported.join(', ')
61 : }
62 : }
63 :
64 0 : if (!supported.includes(currentOS)) {
65 0 : return {
66 : type: 'os',
67 : current_value: currentOS,
68 : required_value: supported.join(', ')
69 : }
70 : }
71 :
72 0 : return null
73 : }
74 :
75 : /**
76 : * Checks accelerator compatibility
77 : * @param supported Supported accelerators from Registry (null/undefined = all accelerators supported)
78 : * @param current Current device type
79 : * @returns ConflictDetail if incompatible, null if compatible
80 : */
81 223 : export function checkAcceleratorCompatibility(
82 : supported?: RegistryAccelerator[] | null,
83 : current?: string
84 : ): ConflictDetail | null {
85 : // null/undefined/empty = all accelerator supported
86 223 : if (isNil(supported) || isEmpty(supported)) return null
87 :
88 0 : const currentAcc = getRegistryAccelerator(current)
89 :
90 0 : if (!supported.includes(currentAcc)) {
91 0 : return {
92 : type: 'accelerator',
93 : current_value: currentAcc,
94 : required_value: supported.join(', ')
95 : }
96 : }
97 :
98 0 : return null
99 : }
100 :
101 : /**
102 : * Normalizes OS values from Registry API
103 : * Handles edge cases like "OS Independent"
104 : * @returns undefined if all OS supported, otherwise filtered valid OS list
105 : */
106 193 : export function normalizeOSList(
107 : osValues?: string[] | null
108 : ): RegistryOS[] | undefined {
109 193 : if (isNil(osValues) || isEmpty(osValues)) return undefined
110 :
111 : // "OS Independent" means all OS supported
112 0 : if (osValues.some((os) => os.toLowerCase() === 'os independent')) {
113 0 : return undefined
114 : }
115 :
116 : // Filter to valid Registry OS values only
117 0 : const validOS: RegistryOS[] = []
118 0 : osValues.forEach((os) => {
119 0 : if (os === 'Windows' || os === 'macOS' || os === 'Linux') {
120 0 : if (!validOS.includes(os)) validOS.push(os)
121 : }
122 : })
123 :
124 193 : return validOS.length > 0 ? validOS : undefined
125 : }
|