Line data Source code
1 : import type { LGraph } from '@/lib/litegraph/src/LGraph'
2 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
3 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
4 : import {
5 : getExecutionIdByNode,
6 : getRootParentNode
7 : } from '@/utils/graphTraversalUtil'
8 : import { resolveNode } from '@/utils/litegraphUtil'
9 :
10 : import { heuristicRoles } from './heuristicRoles'
11 : import { TOUR_ROLE_PINS } from './tourRolePins'
12 : import type {
13 : RolePin,
14 : SupportedTemplateId,
15 : TourMediaKind
16 : } from './tourRolePins'
17 :
18 : export interface ResolvedRoles {
19 : source: NodeId | null
20 : promptHost: NodeId | null
21 : sink: NodeId | null
22 : mediaKind: TourMediaKind
23 : }
24 :
25 : /** The root-graph node a role is spotlit through: itself, or the subgraph node holding it. */
26 0 : function rootHost(
27 : node: LGraphNode | null | undefined,
28 : root: LGraph
29 : ): NodeId | null {
30 0 : if (!node) return null
31 0 : if (node.graph === root) return node.id
32 0 : const executionId = getExecutionIdByNode(root, node)
33 0 : return executionId ? (getRootParentNode(root, executionId)?.id ?? null) : null
34 : }
35 :
36 : /** The pinned type is the guard: an id alone matches any graph that has it. */
37 0 : function resolvePin(pin: RolePin | undefined, root: LGraph): NodeId | null {
38 0 : if (!pin) return null
39 0 : const node = resolveNode(pin.id, root)
40 0 : return node?.type === pin.type ? rootHost(node, root) : null
41 : }
42 :
43 : /** Read off the graph itself, then host-mapped the same way pins are. */
44 0 : function resolveFromGraph(graph: LGraph): ResolvedRoles | null {
45 0 : const roles = heuristicRoles(graph)
46 0 : if (!roles) return null
47 :
48 0 : return {
49 : source: rootHost(roles.source, graph),
50 : promptHost: rootHost(roles.prompt, graph),
51 : sink: rootHost(roles.sink, graph),
52 : mediaKind: roles.mediaKind
53 : }
54 : }
55 :
56 : /**
57 : * Validates a template's pins against the live graph, falling back to reading
58 : * the roles off the graph for anything unpinned — a shared workflow, or a
59 : * template URL nobody curated. A pin the graph no longer contains degrades to
60 : * null so its step is omitted rather than spotlighting a node that is gone.
61 : */
62 0 : export function resolveTourRoles(
63 : graph: LGraph,
64 : templateId?: string
65 : ): ResolvedRoles | null {
66 0 : if (templateId === undefined || !Object.hasOwn(TOUR_ROLE_PINS, templateId))
67 0 : return resolveFromGraph(graph)
68 0 : const pins = TOUR_ROLE_PINS[templateId as SupportedTemplateId]
69 :
70 0 : return {
71 : source: resolvePin(pins.source, graph),
72 : promptHost: resolvePin(pins.prompt, graph),
73 : sink: resolvePin(pins.sink, graph),
74 : mediaKind: pins.mediaKind
75 : }
76 : }
|