Line data Source code
1 : import type {
2 : NodeExecutionOutput,
3 : ResultItem
4 : } from '@comfyorg/api-client/executionTypes'
5 : import { zResultItem } from '@comfyorg/api-client/executionTypes'
6 : import type { AugmentedResultItem } from '@/utils/resultItem'
7 :
8 2356 : const METADATA_KEYS = new Set(['animated'])
9 2356 : const resultItemSchema = zResultItem.passthrough()
10 :
11 6820 : function isUnknownArray(value: unknown): value is unknown[] {
12 6820 : return Array.isArray(value)
13 : }
14 :
15 7040 : function isResultItem(item: unknown, mediaType: string): item is ResultItem {
16 7040 : const parsed = resultItemSchema.safeParse(item)
17 7040 : if (!parsed.success) return false
18 7040 : if (parsed.data.filename !== undefined) return true
19 7040 : return mediaType === 'text' && typeof parsed.data.content === 'string'
20 : }
21 :
22 6823 : export function parseNodeOutput(
23 : nodeId: string | number,
24 : nodeOutput: Record<string, unknown> | null | undefined
25 : ): AugmentedResultItem[] {
26 6823 : if (!nodeOutput) return []
27 :
28 6823 : return Object.entries(nodeOutput).flatMap(([mediaType, items]) => {
29 8273 : if (METADATA_KEYS.has(mediaType) || !isUnknownArray(items)) return []
30 6820 : return items
31 : .map((item) =>
32 : mediaType === 'text' && typeof item === 'string'
33 : ? { content: item }
34 : : item
35 : )
36 : .filter((item) => isResultItem(item, mediaType))
37 : .map((item) => ({
38 : subfolder: '',
39 : ...item,
40 : filename: item.filename ?? '',
41 : mediaType,
42 : nodeId
43 : }))
44 : })
45 : }
46 :
47 5583 : export function parseTaskOutput(
48 : taskOutput: Record<string, NodeExecutionOutput | null | undefined>
49 : ): AugmentedResultItem[] {
50 5583 : return Object.entries(taskOutput).flatMap(([nodeId, nodeOutput]) =>
51 : parseNodeOutput(nodeId, nodeOutput)
52 : )
53 : }
|