Line data Source code
1 : import { uniqBy } from 'es-toolkit'
2 :
3 : import type {
4 : ComfyWorkflow,
5 : PendingWarnings
6 : } from '@/platform/workflow/management/stores/comfyWorkflow'
7 : import type { MissingNodeType } from '@/platform/workflowErrors/nodeReplacement/types'
8 :
9 2356 : const emptyToUndefined = <T>(arr: T[] | undefined): T[] | undefined =>
10 : arr?.length ? arr : undefined
11 :
12 103 : function getMissingNodeKey(node: MissingNodeType): string {
13 103 : if (typeof node === 'string') return `string:${node}`
14 103 : if (node.nodeId != null) return `nodeId:${String(node.nodeId)}`
15 0 : return `type:${node.type}`
16 : }
17 :
18 92 : export function dedupeMissingNodeTypes(
19 : types: readonly MissingNodeType[]
20 : ): MissingNodeType[] {
21 92 : return uniqBy(types, getMissingNodeKey)
22 : }
23 :
24 26 : export function removePendingMissingNodeTypesByType(
25 : currentTypes: readonly MissingNodeType[] | undefined,
26 : typesToRemove: readonly string[]
27 : ): MissingNodeType[] {
28 26 : const removeSet = new Set(typesToRemove)
29 26 : return dedupeMissingNodeTypes(
30 : (currentTypes ?? []).filter((node) => {
31 38 : const nodeType = typeof node === 'string' ? node : node.type
32 38 : return !removeSet.has(nodeType)
33 : })
34 : )
35 : }
36 :
37 4 : export function removePendingMissingNodeTypesByNodeId(
38 : currentTypes: readonly MissingNodeType[] | undefined,
39 : nodeId: string
40 : ): MissingNodeType[] {
41 4 : return dedupeMissingNodeTypes(
42 : (currentTypes ?? []).filter(
43 : (node) =>
44 : typeof node === 'string' ||
45 : node.nodeId == null ||
46 : String(node.nodeId) !== nodeId
47 : )
48 : )
49 : }
50 :
51 0 : export function removePendingMissingNodeTypesByExecutionIdPrefix(
52 : currentTypes: readonly MissingNodeType[] | undefined,
53 : prefix: string
54 : ): MissingNodeType[] {
55 0 : return dedupeMissingNodeTypes(
56 : (currentTypes ?? []).filter((node) => {
57 0 : if (typeof node === 'string' || node.nodeId == null) return true
58 0 : return !String(node.nodeId).startsWith(prefix)
59 : })
60 : )
61 : }
62 :
63 15758 : export function normalizePendingWarnings(
64 : warnings: PendingWarnings | null | undefined
65 : ): PendingWarnings | null {
66 15758 : if (!warnings) return null
67 :
68 15758 : const normalized: PendingWarnings = {
69 : missingNodeTypes: emptyToUndefined(warnings.missingNodeTypes),
70 : missingModelCandidates: emptyToUndefined(warnings.missingModelCandidates),
71 : missingMediaCandidates: emptyToUndefined(warnings.missingMediaCandidates)
72 : }
73 :
74 15758 : return Object.values(normalized).some(Boolean) ? normalized : null
75 : }
76 :
77 12874 : export function updatePendingWarnings(
78 : workflow: Pick<ComfyWorkflow, 'pendingWarnings'> | null | undefined,
79 : updates: Partial<PendingWarnings>
80 : ) {
81 12874 : if (!workflow) return
82 :
83 12874 : workflow.pendingWarnings = normalizePendingWarnings({
84 : ...workflow.pendingWarnings,
85 : ...updates
86 : })
87 : }
|