@@ -24,7 +24,7 @@ import { useUndoRedoStore } from '@/stores/undo-redo'
2424import { useWorkflowDiffStore } from '@/stores/workflow-diff/store'
2525import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
2626import { useSubBlockStore } from '@/stores/workflows/subblock/store'
27- import { filterNewEdges , mergeSubblockState } from '@/stores/workflows/utils'
27+ import { filterNewEdges , filterValidEdges , mergeSubblockState } from '@/stores/workflows/utils'
2828import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2929import type { BlockState , Loop , Parallel , Position } from '@/stores/workflows/workflow/types'
3030
@@ -226,9 +226,12 @@ export function useCollaborativeWorkflow() {
226226 case EDGES_OPERATIONS . BATCH_ADD_EDGES : {
227227 const { edges } = payload
228228 if ( Array . isArray ( edges ) && edges . length > 0 ) {
229- const newEdges = filterNewEdges ( edges , useWorkflowStore . getState ( ) . edges )
229+ const blocks = useWorkflowStore . getState ( ) . blocks
230+ const currentEdges = useWorkflowStore . getState ( ) . edges
231+ const validEdges = filterValidEdges ( edges , blocks )
232+ const newEdges = filterNewEdges ( validEdges , currentEdges )
230233 if ( newEdges . length > 0 ) {
231- useWorkflowStore . getState ( ) . batchAddEdges ( newEdges )
234+ useWorkflowStore . getState ( ) . batchAddEdges ( newEdges , { skipValidation : true } )
232235 }
233236 }
234237 break
@@ -1004,7 +1007,11 @@ export function useCollaborativeWorkflow() {
10041007
10051008 if ( edges . length === 0 ) return false
10061009
1007- const newEdges = filterNewEdges ( edges , useWorkflowStore . getState ( ) . edges )
1010+ // Filter out invalid edges (e.g., edges targeting trigger blocks) and duplicates
1011+ const blocks = useWorkflowStore . getState ( ) . blocks
1012+ const currentEdges = useWorkflowStore . getState ( ) . edges
1013+ const validEdges = filterValidEdges ( edges , blocks )
1014+ const newEdges = filterNewEdges ( validEdges , currentEdges )
10081015 if ( newEdges . length === 0 ) return false
10091016
10101017 const operationId = crypto . randomUUID ( )
@@ -1020,7 +1027,7 @@ export function useCollaborativeWorkflow() {
10201027 userId : session ?. user ?. id || 'unknown' ,
10211028 } )
10221029
1023- useWorkflowStore . getState ( ) . batchAddEdges ( newEdges )
1030+ useWorkflowStore . getState ( ) . batchAddEdges ( newEdges , { skipValidation : true } )
10241031
10251032 if ( ! options ?. skipUndoRedo ) {
10261033 newEdges . forEach ( ( edge ) => undoRedo . recordAddEdge ( edge . id ) )
@@ -1484,9 +1491,23 @@ export function useCollaborativeWorkflow() {
14841491
14851492 if ( blocks . length === 0 ) return false
14861493
1494+ // Filter out invalid edges (e.g., edges targeting trigger blocks)
1495+ // Combine existing blocks with new blocks for validation
1496+ const existingBlocks = useWorkflowStore . getState ( ) . blocks
1497+ const newBlocksMap = blocks . reduce (
1498+ ( acc , block ) => {
1499+ acc [ block . id ] = block
1500+ return acc
1501+ } ,
1502+ { } as Record < string , BlockState >
1503+ )
1504+ const allBlocks = { ...existingBlocks , ...newBlocksMap }
1505+ const validEdges = filterValidEdges ( edges , allBlocks )
1506+
14871507 logger . info ( 'Batch adding blocks collaboratively' , {
14881508 blockCount : blocks . length ,
1489- edgeCount : edges . length ,
1509+ edgeCount : validEdges . length ,
1510+ filteredEdges : edges . length - validEdges . length ,
14901511 } )
14911512
14921513 const operationId = crypto . randomUUID ( )
@@ -1496,16 +1517,18 @@ export function useCollaborativeWorkflow() {
14961517 operation : {
14971518 operation : BLOCKS_OPERATIONS . BATCH_ADD_BLOCKS ,
14981519 target : OPERATION_TARGETS . BLOCKS ,
1499- payload : { blocks, edges, loops, parallels, subBlockValues } ,
1520+ payload : { blocks, edges : validEdges , loops, parallels, subBlockValues } ,
15001521 } ,
15011522 workflowId : activeWorkflowId || '' ,
15021523 userId : session ?. user ?. id || 'unknown' ,
15031524 } )
15041525
1505- useWorkflowStore . getState ( ) . batchAddBlocks ( blocks , edges , subBlockValues )
1526+ useWorkflowStore . getState ( ) . batchAddBlocks ( blocks , validEdges , subBlockValues , {
1527+ skipEdgeValidation : true ,
1528+ } )
15061529
15071530 if ( ! options ?. skipUndoRedo ) {
1508- undoRedo . recordBatchAddBlocks ( blocks , edges , subBlockValues )
1531+ undoRedo . recordBatchAddBlocks ( blocks , validEdges , subBlockValues )
15091532 }
15101533
15111534 return true
0 commit comments