Line data Source code
1 : import type { OutputAssetMetadata } from '@/platform/assets/schemas/assetMetadataSchema'
2 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
3 : import { isCloud } from '@/platform/distribution/types'
4 : import type { JobOutputAsset } from '@comfyorg/api-client/jobTypes'
5 : import { getOutputKey } from '@/platform/assets/utils/outputKeyUtil'
6 : import {
7 : getJobAssets,
8 : getJobDetail,
9 : getPreviewableOutputsFromJobDetail
10 : } from '@/services/jobOutputCache'
11 : import type { ResultItemType } from '@comfyorg/api-client/resultItemTypeSchema'
12 : import type { AugmentedResultItem } from '@/utils/resultItem'
13 : import { resultItemPreviewUrl, resultItemUrl } from '@/utils/resultItemUrl'
14 :
15 2356 : const storedOutputIdentityKey = Symbol('storedOutputIdentity')
16 :
17 : type StoredOutputIdentity = {
18 : type: ResultItemType
19 : hash?: string
20 : }
21 :
22 : type WithStoredOutputIdentity = {
23 : [storedOutputIdentityKey]?: StoredOutputIdentity
24 : }
25 :
26 4116 : function withStoredOutputIdentity(
27 : item: AssetItem,
28 : { type, hash }: AugmentedResultItem
29 : ): AssetItem {
30 4116 : if (!type) return item
31 4116 : const identity: StoredOutputIdentity = { type, hash }
32 4116 : return Object.assign(item, { [storedOutputIdentityKey]: identity })
33 : }
34 :
35 : /** Root and stored hash of the grouped output an expanded stack item was built from. */
36 1910 : export function getStoredOutputIdentity(
37 : asset: AssetItem & WithStoredOutputIdentity
38 : ): StoredOutputIdentity | undefined {
39 1910 : return asset[storedOutputIdentityKey]
40 : }
41 :
42 : type OutputAssetMapOptions = {
43 : jobId: string
44 : outputs: readonly AugmentedResultItem[]
45 : createdAt?: string
46 : executionTimeInSeconds?: number
47 : workflow?: OutputAssetMetadata['workflow']
48 : excludeOutputKey?: string
49 : }
50 :
51 : type ResolveOutputAssetItemsOptions = {
52 : createdAt?: string
53 : excludeOutputKey?: string
54 : fallbackItems?: AssetItem[]
55 : }
56 :
57 3727 : function shouldLoadFullOutputs(
58 : outputCount: OutputAssetMetadata['outputCount'],
59 : outputsLength: number
60 : ): boolean {
61 3727 : return (
62 : typeof outputCount === 'number' &&
63 : outputCount > 1 &&
64 : outputsLength < outputCount
65 : )
66 : }
67 :
68 : /**
69 : * Maps a job's outputs to AssetItems with ids derived from the composite
70 : * `[nodeId, subfolder, filename]` key. Records sharing a composite key are
71 : * dropped after the first to keep `:key` unique in VirtualGrid — colliding
72 : * ids cause Vue to reuse one DOM node and visibly duplicate the asset on
73 : * scroll.
74 : *
75 : * The dedupe key ignores `type`/`mediaType`/`format`/`frame_rate` because
76 : * those fields don't appear in `AssetItem.id`, so widening the key would
77 : * just let the collision propagate. The kept copy is the first one seen;
78 : * callers that reverse the input (e.g. `resolveOutputAssetItems`) retain
79 : * the last record in the API's original order.
80 : */
81 3661 : function mapOutputsToAssetItems({
82 : jobId,
83 : outputs,
84 : createdAt,
85 : executionTimeInSeconds,
86 : workflow,
87 : excludeOutputKey
88 : }: OutputAssetMapOptions): AssetItem[] {
89 3661 : const createdAtValue = createdAt ?? new Date().toISOString()
90 3661 : const seenOutputKeys = new Set<string>()
91 :
92 3661 : return outputs.reduce<AssetItem[]>((items, output) => {
93 5057 : const outputKey = getOutputKey(output)
94 5057 : if (!output.filename || !outputKey || outputKey === excludeOutputKey) {
95 941 : return items
96 : }
97 4912 : if (seenOutputKeys.has(outputKey)) {
98 0 : return items
99 : }
100 4116 : seenOutputKeys.add(outputKey)
101 :
102 5057 : const item: AssetItem = {
103 : id: output.assetId || `${jobId}-${outputKey}`,
104 : name: output.filename,
105 : display_name: output.display_name,
106 : size: 0,
107 : created_at: createdAtValue,
108 : updated_at: createdAtValue,
109 : tags: ['output'],
110 : thumbnail_url: resultItemPreviewUrl(output),
111 : preview_url: resultItemUrl(output),
112 : user_metadata: {
113 : jobId,
114 : nodeId: output.nodeId,
115 : subfolder: output.subfolder,
116 : executionTimeInSeconds,
117 : workflow
118 : }
119 : }
120 5057 : items.push(withStoredOutputIdentity(item, output))
121 :
122 5057 : return items
123 : }, [])
124 : }
125 :
126 : /**
127 : * Overlays a resolved job asset onto a synthesized output item, linking it to
128 : * the asset system: the placeholder `<jobId>-<outputKey>` id becomes the real
129 : * asset id, and size/mime/preview are filled from the endpoint.
130 : *
131 : * `user_metadata` is left untouched. Its `nodeId` and `subfolder` come from
132 : * the same output entry and are read back as a pair — `useOutputStacks` rebuilds
133 : * `getOutputKey({ nodeId, subfolder, filename })` to exclude the output already
134 : * on screen — so overwriting only `nodeId` with the endpoint's (hash-correlated,
135 : * possibly disagreeing) `node_id` would describe an output that does not exist.
136 : * The endpoint's `node_id` is used to disambiguate matches instead, not to
137 : * relabel an item whose own `nodeId` is already required to be present.
138 : *
139 : * The endpoint's `hash` is deliberately not copied: downstream
140 : * `getAssetUrlFilename` treats `hash` as the storage filename, which the
141 : * endpoint's (`blake3:`-prefixed) value is not. The synthesized
142 : * `thumbnail_url` wins over the asset's `preview_url` because it carries the
143 : * resolution-capped `res=` variant; the asset value is the unbounded original.
144 : */
145 : function overlayJobAsset(item: AssetItem, asset: JobOutputAsset): AssetItem {
146 : return {
147 : ...item,
148 : id: asset.id,
149 : size: asset.size ?? item.size,
150 : mime_type: asset.mime_type ?? item.mime_type,
151 : preview_url: asset.preview_url ?? item.preview_url,
152 : thumbnail_url: item.thumbnail_url ?? asset.preview_url ?? undefined
153 : }
154 : }
155 :
156 : /**
157 : * Composite `(filename, node id)` key. The node id is stringified because the
158 : * output side carries a `SerializedNodeId` (`string | number`) while the
159 : * endpoint always sends a string, and `9` must pair with `'9'`.
160 : */
161 : function nodeScopedName(name: string, nodeId: unknown): string | null {
162 : return typeof nodeId === 'string' || typeof nodeId === 'number'
163 : ? JSON.stringify([name, String(nodeId)])
164 : : null
165 : }
166 :
167 : /**
168 : * Indexes items and assets under a shared key and returns a lookup that pairs
169 : * them only when the key identifies exactly one of each. A null key (the field
170 : * it needs is absent) is never indexed, so it can never pair.
171 : */
172 : function createUniquePairing(
173 : items: readonly AssetItem[],
174 : jobAssets: readonly JobOutputAsset[],
175 : keyOfItem: (item: AssetItem) => string | null,
176 : keyOfAsset: (asset: JobOutputAsset) => string | null
177 : ): (key: string | null) => JobOutputAsset | null {
178 : const assetsByKey = new Map<string, JobOutputAsset[]>()
179 : for (const asset of jobAssets) {
180 : const key = keyOfAsset(asset)
181 : if (key === null) continue
182 : const existing = assetsByKey.get(key)
183 : if (existing) {
184 : existing.push(asset)
185 : } else {
186 : assetsByKey.set(key, [asset])
187 : }
188 : }
189 :
190 : const itemCounts = new Map<string, number>()
191 : for (const item of items) {
192 : const key = keyOfItem(item)
193 : if (key === null) continue
194 : itemCounts.set(key, (itemCounts.get(key) ?? 0) + 1)
195 : }
196 :
197 : return (key) => {
198 : if (key === null) return null
199 : const candidates = assetsByKey.get(key)
200 : return candidates?.length === 1 && itemCounts.get(key) === 1
201 : ? candidates[0]
202 : : null
203 : }
204 : }
205 :
206 : /**
207 : * Resolves persisted job outputs to real asset entities via
208 : * GET /api/jobs/{job_id}/assets, matching by filename (the stable identifier
209 : * across the history and asset id spaces). Cloud-only; degrades to the
210 : * unresolved items when the endpoint returns nothing (e.g. not yet deployed).
211 : *
212 : * A filename duplicated on either side — among the outputs or among the
213 : * returned assets — falls back to `(filename, node_id)`, which separates the
214 : * common collision of one filename written by two different nodes. An output
215 : * still ambiguous under that pair, or whose asset carries no `node_id`, is
216 : * left unresolved: `zJobOutputAsset` carries no subfolder and the endpoint's
217 : * ordering is unspecified, so nothing else pairs duplicates reliably, and a
218 : * wrong pairing would render one file's preview under another's identity.
219 : */
220 : async function enrichWithJobAssets(
221 : jobId: string,
222 : items: AssetItem[]
223 : ): Promise<AssetItem[]> {
224 : if (!items.length) return items
225 :
226 : let jobAssets: JobOutputAsset[]
227 : try {
228 : jobAssets = await getJobAssets(jobId)
229 : } catch (error) {
230 : console.error(`Failed to enrich job ${jobId} with assets:`, error)
231 : return items
232 : }
233 : if (!jobAssets.length) return items
234 :
235 : const matchByName = createUniquePairing(
236 : items,
237 : jobAssets,
238 : (item) => item.name,
239 : (asset) => asset.name
240 : )
241 : const matchByNodeScopedName = createUniquePairing(
242 : items,
243 : jobAssets,
244 : (item) => nodeScopedName(item.name, item.user_metadata?.nodeId),
245 : (asset) => nodeScopedName(asset.name, asset.node_id)
246 : )
247 :
248 : return items.map((item) => {
249 : const match =
250 : matchByName(item.name) ??
251 : matchByNodeScopedName(
252 : nodeScopedName(item.name, item.user_metadata?.nodeId)
253 : )
254 : return match ? overlayJobAsset(item, match) : item
255 : })
256 : }
257 :
258 3727 : export async function resolveOutputAssetItems(
259 : metadata: OutputAssetMetadata,
260 : {
261 : createdAt,
262 : excludeOutputKey,
263 : fallbackItems
264 : }: ResolveOutputAssetItemsOptions = {}
265 : ): Promise<AssetItem[]> {
266 3727 : let outputsToDisplay = metadata.allOutputs ?? []
267 3727 : if (shouldLoadFullOutputs(metadata.outputCount, outputsToDisplay.length)) {
268 1423 : const jobDetail = await getJobDetail(metadata.jobId)
269 1423 : if (!jobDetail && fallbackItems) return fallbackItems
270 1357 : const previewableOutputs = getPreviewableOutputsFromJobDetail(jobDetail)
271 1423 : if (previewableOutputs.length) {
272 1320 : outputsToDisplay = previewableOutputs
273 : }
274 : }
275 :
276 : const items = mapOutputsToAssetItems({
277 : jobId: metadata.jobId,
278 : outputs: [...outputsToDisplay].reverse(),
279 : createdAt,
280 : executionTimeInSeconds: metadata.executionTimeInSeconds,
281 : workflow: metadata.workflow,
282 : excludeOutputKey
283 : })
284 :
285 3661 : return isCloud ? enrichWithJobAssets(metadata.jobId, items) : items
286 : }
|