Line data Source code
1 : import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums'
2 : import type { DeclaredModelFile } from '@comfyorg/graph-model/workflowSchema'
3 : import { zDeclaredModelFile } from '@comfyorg/graph-model/workflowSchema'
4 : import { getParentExecutionIds } from '@comfyorg/graph-model/nodeIdentification'
5 : import type { FlattenableWorkflowNode } from './workflowFlattening'
6 :
7 : type NodeModelMetadata = {
8 : type: string
9 : widgets_values?: readonly unknown[] | Record<string, unknown>
10 : /**
11 : * `models` is whatever the workflow carried. The legacy schema passes it
12 : * through unparsed, so nothing upstream has checked that it is an array, or
13 : * that its entries are model records.
14 : */
15 : properties?: { models?: unknown }
16 : }
17 :
18 5935 : export function getModelFileKey(
19 : model: Pick<DeclaredModelFile, 'name' | 'directory'>
20 : ): string {
21 5935 : return JSON.stringify([model.name, model.directory])
22 : }
23 :
24 21554 : function declaredModels(models: unknown): DeclaredModelFile[] {
25 21554 : if (!Array.isArray(models)) return []
26 2686 : return models.flatMap((entry) => {
27 2686 : const model = zDeclaredModelFile.safeParse(entry)
28 2686 : return model.success ? [model.data] : []
29 : })
30 : }
31 :
32 : /**
33 : * The declared models a node actually has selected in its widgets.
34 : *
35 : * Returns `undefined` when there is nothing to match against - the node
36 : * declares no models, or carries no widget values to select them with - and
37 : * an empty array when it declares models but none of them are selected.
38 : * Callers that only care whether a node contributes requirements can treat
39 : * both as "nothing"; callers that distinguish "no metadata" from "metadata
40 : * that matched nothing" have to check for `undefined`.
41 : */
42 21554 : export function getSelectedModelsMetadata(
43 : node: NodeModelMetadata
44 : ): DeclaredModelFile[] | undefined {
45 21554 : const models = declaredModels(node.properties?.models)
46 21554 : if (!models.length || !node.widgets_values) return
47 :
48 21554 : const widgetValues = Array.isArray(node.widgets_values)
49 : ? node.widgets_values
50 : : Object.values(node.widgets_values)
51 21554 : if (!widgetValues.length) return
52 :
53 2686 : const selectedNames = new Set(
54 : widgetValues.filter(
55 : (value): value is string =>
56 : typeof value === 'string' && value.trim().length > 0
57 : )
58 : )
59 :
60 2686 : return models.filter((model) => selectedNames.has(model.name))
61 : }
62 :
63 44141 : export function isInactiveWorkflowNodeMode(mode: number | undefined): boolean {
64 44141 : return mode === LGraphEventMode.NEVER || mode === LGraphEventMode.BYPASS
65 : }
66 :
67 21563 : export function isNodeAndAncestorsActive(
68 : node: FlattenableWorkflowNode,
69 : nodesById: ReadonlyMap<string, FlattenableWorkflowNode>
70 : ): boolean {
71 21563 : if (isInactiveWorkflowNodeMode(node.mode)) return false
72 :
73 21556 : return getParentExecutionIds(String(node.id)).every(
74 : (ancestorId) => !isInactiveWorkflowNodeMode(nodesById.get(ancestorId)?.mode)
75 : )
76 : }
|