Line data Source code
1 : import type { MaybeRefOrGetter } from 'vue'
2 : import { computed, toValue } from 'vue'
3 :
4 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
5 : import { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode'
6 : import type { UUID } from '@/utils/uuid'
7 : import type { NodeExecutionOutput } from '@comfyorg/api-client/executionTypes'
8 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
9 : import {
10 : getPreviewExposureHostLocator,
11 : usePreviewExposureStore
12 : } from '@/stores/previewExposureStore'
13 : import type { AugmentedResultItem } from '@/utils/resultItem'
14 : import { isAudioResult, isVideoResult } from '@/utils/resultItem'
15 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
16 : import {
17 : appendNodeExecutionId,
18 : createNodeLocatorId
19 : } from '@comfyorg/graph-model/nodeIdentification'
20 : import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'
21 :
22 : type PromotedOutput = {
23 : type: 'image' | 'video' | 'audio'
24 : items: AugmentedResultItem[]
25 : }
26 : type PromotedLivePreview = { type: 'preview'; url: string }
27 : type PromotedContent = PromotedOutput | PromotedLivePreview
28 :
29 : type PromotedPreview = PromotedContent & {
30 : sourceNodeId: NodeId
31 : sourceWidgetName: string
32 : }
33 :
34 9 : function getPreviewMediaType(
35 : node: LGraphNode,
36 : items: AugmentedResultItem[]
37 : ): PromotedOutput['type'] {
38 9 : const media = node.previewMediaType
39 9 : if (media === 'image' || media === 'video' || media === 'audio') return media
40 5 : if (items.some(isVideoResult)) return 'video'
41 5 : if (items.some(isAudioResult)) return 'audio'
42 5 : return 'image'
43 : }
44 :
45 8015 : export function usePromotedPreviews(
46 : lgraphNode: MaybeRefOrGetter<LGraphNode | null | undefined>
47 : ) {
48 8015 : const previewExposureStore = usePreviewExposureStore()
49 8015 : const nodeOutputStore = useNodeOutputStore()
50 :
51 346 : function toPromotedContent(
52 : node: LGraphNode,
53 : previews: string[] | undefined,
54 : outputs: NodeExecutionOutput | undefined
55 : ): PromotedContent | undefined {
56 346 : const url = previews?.at(-1)
57 346 : if (url) return { type: 'preview', url }
58 341 : if (!outputs) return undefined
59 9 : const items = nodeOutputStore.getNodeOutputItems(node, outputs)
60 9 : if (items.length) return { type: getPreviewMediaType(node, items), items }
61 : return undefined
62 : }
63 :
64 180 : function readPromotedContent(
65 : leafHost: SubgraphNode,
66 : sourceNodeId: NodeId,
67 : executionId: NodeExecutionId,
68 : interiorNode: LGraphNode
69 : ): PromotedContent | undefined {
70 180 : const locatorId = createNodeLocatorId(leafHost.subgraph.id, sourceNodeId)
71 180 : return (
72 : toPromotedContent(
73 : interiorNode,
74 : nodeOutputStore.getNodePreviewImagesByExecutionId(executionId),
75 : nodeOutputStore.getNodeOutputByExecutionId(executionId)
76 : ) ??
77 : toPromotedContent(
78 : interiorNode,
79 : nodeOutputStore.nodePreviewImages[locatorId],
80 : nodeOutputStore.nodeOutputs[locatorId]
81 : )
82 : )
83 : }
84 :
85 : const promotedPreviews = computed((): PromotedPreview[] => {
86 8040 : const node = toValue(lgraphNode)
87 8040 : if (!(node instanceof SubgraphNode)) return []
88 3382 : if (node.isDetached) return []
89 :
90 297 : const rootGraphId = node.rootGraph.id
91 297 : const hostLocator = getPreviewExposureHostLocator(node)
92 1876 : if (!hostLocator) return []
93 297 : const exposures = previewExposureStore.getExposures(
94 : rootGraphId,
95 : hostLocator
96 : )
97 1267 : if (!exposures.length) return []
98 :
99 179 : const hostNodesByLocator = new Map<string, SubgraphNode>([
100 : [hostLocator, node]
101 : ])
102 179 : const hostExecutionsByLocator = new Map<string, string>([
103 : [hostLocator, String(node.id)]
104 : ])
105 :
106 185 : function resolveNestedHost(
107 : rootGraphId: UUID,
108 : currentHostLocator: string,
109 : sourceNodeId: NodeId
110 : ) {
111 : const currentHost = hostNodesByLocator.get(currentHostLocator)
112 185 : const sourceNode = currentHost?.subgraph.getNodeById(sourceNodeId)
113 185 : if (!(sourceNode instanceof SubgraphNode)) return undefined
114 :
115 0 : const nestedHostLocator = getPreviewExposureHostLocator(sourceNode)
116 0 : if (!nestedHostLocator) return undefined
117 0 : const currentExecutionId = hostExecutionsByLocator.get(currentHostLocator)
118 0 : if (!currentExecutionId) return undefined
119 0 : hostNodesByLocator.set(nestedHostLocator, sourceNode)
120 0 : hostExecutionsByLocator.set(
121 : nestedHostLocator,
122 : `${currentExecutionId}:${sourceNode.id}`
123 : )
124 0 : return { rootGraphId, hostNodeLocator: nestedHostLocator }
125 : }
126 :
127 179 : return exposures.flatMap((exposure): PromotedPreview[] => {
128 185 : const resolved = previewExposureStore.resolveChain(
129 : rootGraphId,
130 : hostLocator,
131 : exposure.name,
132 : resolveNestedHost
133 : )
134 185 : const leaf = resolved?.leaf ?? {
135 : sourceNodeId: exposure.sourceNodeId,
136 : sourcePreviewName: exposure.sourcePreviewName
137 : }
138 185 : const leafHostLocator =
139 : resolved?.steps.at(-1)?.hostNodeLocator ?? hostLocator
140 185 : const leafHost = hostNodesByLocator.get(leafHostLocator) ?? node
141 185 : const leafHostExecutionId =
142 : hostExecutionsByLocator.get(leafHostLocator) ?? String(node.id)
143 185 : const interiorNode = leafHost.subgraph.getNodeById(leaf.sourceNodeId)
144 185 : if (!interiorNode) return []
145 :
146 181 : const leafExecutionId = appendNodeExecutionId(
147 : leafHostExecutionId,
148 : leaf.sourceNodeId
149 : )
150 185 : if (!leafExecutionId) return []
151 :
152 180 : const content = readPromotedContent(
153 : leafHost,
154 : leaf.sourceNodeId,
155 : leafExecutionId,
156 : interiorNode
157 : )
158 180 : if (!content) return []
159 :
160 14 : return [
161 : {
162 : ...content,
163 : sourceNodeId: leaf.sourceNodeId,
164 : sourceWidgetName: leaf.sourcePreviewName
165 : }
166 : ]
167 : })
168 : })
169 :
170 8015 : return { promotedPreviews }
171 : }
|