|
| 1 | +import type { NodeTracing } from '@/types/workflow' |
| 2 | +import { BlockEnum } from '@/app/components/workflow/types' |
| 3 | + |
| 4 | +/** |
| 5 | + * Format human-input nodes to ensure only the latest status is kept for each node. |
| 6 | + * Human-input nodes can have multiple log entries as their status changes |
| 7 | + * (e.g., running -> paused -> succeeded/failed). |
| 8 | + * This function keeps only the entry with the latest index for each unique node_id. |
| 9 | + */ |
| 10 | +const formatHumanInputNode = (list: NodeTracing[]): NodeTracing[] => { |
| 11 | + // Group human-input nodes by node_id |
| 12 | + const humanInputNodeMap = new Map<string, NodeTracing>() |
| 13 | + |
| 14 | + // Track which node_ids are human-input type |
| 15 | + const humanInputNodeIds = new Set<string>() |
| 16 | + |
| 17 | + // First pass: identify human-input nodes and keep the one with the highest index |
| 18 | + list.forEach((item) => { |
| 19 | + if (item.node_type === BlockEnum.HumanInput) { |
| 20 | + humanInputNodeIds.add(item.node_id) |
| 21 | + |
| 22 | + const existingNode = humanInputNodeMap.get(item.node_id) |
| 23 | + if (!existingNode || item.index > existingNode.index) { |
| 24 | + humanInputNodeMap.set(item.node_id, item) |
| 25 | + } |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + // If no human-input nodes, return the list as is |
| 30 | + if (humanInputNodeIds.size === 0) |
| 31 | + return list |
| 32 | + |
| 33 | + // Second pass: filter the list to remove duplicate human-input nodes |
| 34 | + // and keep only the latest one for each node_id |
| 35 | + const result: NodeTracing[] = [] |
| 36 | + const addedHumanInputNodeIds = new Set<string>() |
| 37 | + |
| 38 | + list.forEach((item) => { |
| 39 | + if (item.node_type === BlockEnum.HumanInput) { |
| 40 | + // Only add the human-input node with the highest index |
| 41 | + if (!addedHumanInputNodeIds.has(item.node_id)) { |
| 42 | + const latestNode = humanInputNodeMap.get(item.node_id) |
| 43 | + if (latestNode) { |
| 44 | + result.push(latestNode) |
| 45 | + addedHumanInputNodeIds.add(item.node_id) |
| 46 | + } |
| 47 | + } |
| 48 | + // Skip duplicate human-input nodes |
| 49 | + } |
| 50 | + else { |
| 51 | + // Keep all non-human-input nodes |
| 52 | + result.push(item) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + return result |
| 57 | +} |
| 58 | + |
| 59 | +export default formatHumanInputNode |
0 commit comments