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

Function mergeSubblockState

apps/sim/stores/workflows/utils.ts:206–283  ·  view source on GitHub ↗
(
  blocks: Record<string, BlockState>,
  workflowId?: string,
  blockId?: string
)

Source from the content-addressed store, hash-verified

204 * @returns Merged block states with updated values
205 */
206export function mergeSubblockState(
207 blocks: Record<string, BlockState>,
208 workflowId?: string,
209 blockId?: string
210): Record<string, BlockState> {
211 const subBlockStore = useSubBlockStore.getState()
212
213 const workflowSubblockValues = workflowId ? subBlockStore.workflowValues[workflowId] || {} : {}
214
215 if (workflowId) {
216 return mergeSubblockStateWithValues(blocks, workflowSubblockValues, blockId)
217 }
218
219 const blocksToProcess = blockId ? { [blockId]: blocks[blockId] } : blocks
220
221 return Object.entries(blocksToProcess).reduce(
222 (acc, [id, block]) => {
223 if (!block) {
224 return acc
225 }
226
227 const blockSubBlocks = block.subBlocks || {}
228
229 const blockValues = workflowSubblockValues[id] || {}
230
231 const mergedSubBlocks = Object.entries(blockSubBlocks).reduce(
232 (subAcc, [subBlockId, subBlock]) => {
233 if (!subBlock) {
234 return subAcc
235 }
236
237 let storedValue = null
238
239 if (workflowId) {
240 if (blockValues[subBlockId] !== undefined) {
241 storedValue = blockValues[subBlockId]
242 }
243 } else {
244 storedValue = subBlockStore.getValue(id, subBlockId)
245 }
246
247 subAcc[subBlockId] = {
248 ...subBlock,
249 value: (storedValue !== undefined && storedValue !== null
250 ? storedValue
251 : subBlock.value) as SubBlockState['value'],
252 }
253
254 return subAcc
255 },
256 {} as Record<string, SubBlockState>
257 )
258
259 // Add any values that exist in the store but aren't in the block structure
260 // This handles cases where block config has been updated but values still exist
261 // IMPORTANT: This includes runtime subblock IDs like webhookId, triggerPath, etc.
262 Object.entries(blockValues).forEach(([subBlockId, value]) => {
263 if (!mergedSubBlocks[subBlockId] && value !== null && value !== undefined) {

Callers 14

getWorkflowWithValuesFunction · 0.90
store.tsFile · 0.90
store.tsFile · 0.90
captureBaselineSnapshotFunction · 0.90
createDiffMethod · 0.90
handleRedeployFunction · 0.90
useDeploymentFunction · 0.90
useEditorSubblockLayoutFunction · 0.90
executeWorkflowFunction · 0.90

Calls 1

Tested by

no test coverage detected