Line data Source code
1 : import { z } from 'zod'
2 :
3 : import { matchesWidgetName } from '@/utils/widgetBinding'
4 :
5 : import { isModelFileName } from '@/platform/workflowErrors/missingModel/missingModelScan'
6 : import { getCnrIdFromProperties } from '@/platform/workflowErrors/nodeReplacement/cnrIdUtil'
7 : import {
8 : collectReachableSubgraphDefinitions,
9 : parseFlattenableSubgraphDefinitions
10 : } from '@/platform/workflow/core/utils/workflowFlattening'
11 : import type {
12 : FlattenableSubgraphDefinition,
13 : FlattenableWorkflowNode
14 : } from '@/platform/workflow/core/utils/workflowFlattening'
15 :
16 : export interface NodePack {
17 : readonly id: string
18 : readonly versions: readonly string[]
19 : }
20 :
21 : export interface BuildInputs {
22 : readonly workflowName: string
23 : readonly workflowFileName: string
24 : readonly nodeClasses: readonly string[]
25 : readonly nodePacks: readonly NodePack[]
26 : readonly models: readonly string[]
27 : }
28 :
29 1 : function widgetStrings(
30 : values: readonly unknown[] | Record<string, unknown> | undefined
31 : ): string[] {
32 1 : if (!values) return []
33 : const list = Array.isArray(values) ? values : Object.values(values)
34 1 : return list.filter((value): value is string => typeof value === 'string')
35 : }
36 :
37 7 : function embeddedModelNames(
38 : properties: Record<string, unknown> | undefined
39 : ): string[] {
40 7 : const models = properties?.models
41 7 : if (!Array.isArray(models)) return []
42 1 : return models.flatMap((model: unknown) =>
43 : typeof model === 'object' &&
44 : model !== null &&
45 : 'name' in model &&
46 : typeof model.name === 'string'
47 : ? [model.name]
48 : : []
49 : )
50 : }
51 :
52 1 : const CORE_PACK_ID = 'comfy-core'
53 :
54 7 : function recordedPack(
55 : properties: Record<string, unknown> | undefined
56 : ): { id: string; version?: string } | undefined {
57 7 : const id = getCnrIdFromProperties(properties)
58 7 : if (!id || id === CORE_PACK_ID) return undefined
59 7 : const version = properties?.ver
60 7 : return typeof version === 'string' && version ? { id, version } : { id }
61 : }
62 :
63 1 : const zNode = z.object({
64 : id: z.union([z.string(), z.number()]),
65 : type: z.string(),
66 : widgets_values: z
67 : .union([z.array(z.unknown()), z.record(z.string(), z.unknown())])
68 : .optional()
69 : .catch(undefined),
70 : properties: z.record(z.string(), z.unknown()).optional().catch(undefined)
71 : })
72 :
73 1 : function parseNodes(nodes: readonly unknown[]): FlattenableWorkflowNode[] {
74 1 : return nodes.flatMap((node) => {
75 7 : const parsed = zNode.safeParse(node)
76 7 : return parsed.success ? [parsed.data] : []
77 : })
78 : }
79 :
80 1 : const zWorkflowShape = z.object({
81 : nodes: z.array(z.unknown()).catch([]),
82 : definitions: z
83 : .object({ subgraphs: z.array(z.unknown()).catch([]) })
84 : .catch({ subgraphs: [] })
85 : })
86 :
87 1 : function workflowParts(graph: unknown): {
88 : roots: FlattenableWorkflowNode[]
89 : subgraphs: FlattenableSubgraphDefinition[]
90 : } {
91 1 : const parsed = zWorkflowShape.safeParse(graph)
92 1 : if (!parsed.success) return { roots: [], subgraphs: [] }
93 1 : return {
94 : roots: parseNodes(parsed.data.nodes),
95 : subgraphs: parseFlattenableSubgraphDefinitions(
96 : parsed.data.definitions.subgraphs,
97 : parseNodes
98 : )
99 : }
100 : }
101 :
102 : export type ModelInputLookup = (
103 : nodeType: string
104 : ) => readonly (string | RegExp)[]
105 :
106 1 : function loaderModelValues(
107 : values: FlattenableWorkflowNode['widgets_values'],
108 : inputs: readonly (string | RegExp)[]
109 : ): string[] {
110 1 : if (!values) return []
111 1 : if (Array.isArray(values))
112 1 : return widgetStrings(values).filter(isModelFileName)
113 0 : return Object.entries(values).flatMap(([name, value]) =>
114 : inputs.some((selector) => matchesWidgetName(name, selector)) &&
115 : typeof value === 'string' &&
116 : value
117 : ? [value]
118 : : []
119 : )
120 : }
121 :
122 7 : function modelNames(
123 : node: FlattenableWorkflowNode,
124 : modelInputsFor: ModelInputLookup
125 : ): string[] {
126 7 : const embedded = embeddedModelNames(node.properties)
127 7 : const inputs = modelInputsFor(node.type)
128 7 : if (!inputs.length) return embedded
129 1 : return [...embedded, ...loaderModelValues(node.widgets_values, inputs)]
130 : }
131 :
132 1 : export function deriveBuildInputs(
133 : graph: unknown,
134 : workflow: { name: string; fileName: string },
135 : modelInputsFor: ModelInputLookup = () => []
136 : ): BuildInputs {
137 1 : const { roots, subgraphs } = workflowParts(graph)
138 1 : const definitions = collectReachableSubgraphDefinitions(roots, subgraphs)
139 1 : const subgraphIds = new Set(definitions.map((definition) => definition.id))
140 1 : const nodes = [
141 : ...roots,
142 : ...definitions.flatMap((definition) => parseNodes(definition.nodes))
143 : ]
144 1 : const nodeClasses = new Set<string>()
145 1 : const packVersions = new Map<string, Set<string>>()
146 1 : const models = new Set<string>()
147 :
148 1 : for (const node of nodes) {
149 7 : if (!subgraphIds.has(node.type)) nodeClasses.add(node.type)
150 :
151 7 : const pack = recordedPack(node.properties)
152 7 : if (pack) {
153 0 : const versions = packVersions.get(pack.id) ?? new Set<string>()
154 0 : if (pack.version) versions.add(pack.version)
155 0 : packVersions.set(pack.id, versions)
156 : }
157 :
158 7 : for (const name of modelNames(node, modelInputsFor)) models.add(name)
159 : }
160 :
161 1 : const collator = new Intl.Collator('en')
162 2 : function sorted(values: Set<string>): string[] {
163 2 : return [...values].sort((a, b) => collator.compare(a, b))
164 : }
165 :
166 1 : return {
167 : workflowName: workflow.name,
168 : workflowFileName: workflow.fileName,
169 : nodeClasses: sorted(nodeClasses),
170 : nodePacks: [...packVersions]
171 : .map(([id, versions]) => ({ id, versions: sorted(versions) }))
172 : .sort((a, b) => collator.compare(a.id, b.id)),
173 : models: sorted(models)
174 : }
175 : }
|