MCPcopy
hub / github.com/simstudioai/sim / regenerateWorkflowIds

Function regenerateWorkflowIds

apps/sim/stores/workflows/utils.ts:332–416  ·  view source on GitHub ↗
(
  workflowState: WorkflowState,
  options: { clearTriggerRuntimeValues?: boolean } = {}
)

Source from the content-addressed store, hash-verified

330}
331
332export function regenerateWorkflowIds(
333 workflowState: WorkflowState,
334 options: { clearTriggerRuntimeValues?: boolean } = {}
335): WorkflowState & { idMap: Map<string, string> } {
336 const { clearTriggerRuntimeValues = true } = options
337 const blockIdMap = new Map<string, string>()
338 const nameMap = new Map<string, string>()
339 const newBlocks: Record<string, BlockState> = {}
340
341 // First pass: generate new IDs and remap condition/router IDs in subBlocks
342 Object.entries(workflowState.blocks).forEach(([oldId, block]) => {
343 const newId = generateId()
344 blockIdMap.set(oldId, newId)
345 const oldNormalizedName = normalizeName(block.name)
346 nameMap.set(oldNormalizedName, oldNormalizedName)
347 const newBlock = { ...block, id: newId, subBlocks: structuredClone(block.subBlocks) }
348 remapConditionIds(newBlock.subBlocks, {}, oldId, newId)
349 newBlocks[newId] = newBlock
350 })
351
352 // Second pass: update parentId references
353 Object.values(newBlocks).forEach((block) => {
354 if (block.data?.parentId) {
355 const newParentId = blockIdMap.get(block.data.parentId)
356 if (newParentId) {
357 block.data = { ...block.data, parentId: newParentId }
358 } else {
359 // Parent not in the workflow, clear the relationship
360 block.data = { ...block.data, parentId: undefined, extent: undefined }
361 }
362 }
363 })
364
365 const newEdges = workflowState.edges.map((edge) => {
366 const newSource = blockIdMap.get(edge.source) || edge.source
367 const newSourceHandle =
368 edge.sourceHandle && blockIdMap.has(edge.source)
369 ? remapConditionEdgeHandle(edge.sourceHandle, edge.source, newSource)
370 : edge.sourceHandle
371
372 return {
373 ...edge,
374 id: generateId(),
375 source: newSource,
376 target: blockIdMap.get(edge.target) || edge.target,
377 sourceHandle: newSourceHandle,
378 }
379 })
380
381 const newLoops: Record<string, Loop> = {}
382 if (workflowState.loops) {
383 Object.entries(workflowState.loops).forEach(([oldLoopId, loop]) => {
384 const newLoopId = blockIdMap.get(oldLoopId) || oldLoopId
385 newLoops[newLoopId] = {
386 ...loop,
387 id: newLoopId,
388 nodes: loop.nodes.map((nodeId) => blockIdMap.get(nodeId) || nodeId),
389 }

Callers 1

parseWorkflowJsonFunction · 0.90

Calls 7

generateIdFunction · 0.90
normalizeNameFunction · 0.90
remapConditionEdgeHandleFunction · 0.90
remapConditionIdsFunction · 0.85
updateBlockReferencesFunction · 0.85
setMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected