Skip to content

Commit 610ea0b

Browse files
committed
Yaml fixes
1 parent 3c1914c commit 610ea0b

File tree

2 files changed

+85
-42
lines changed

2 files changed

+85
-42
lines changed

apps/sim/lib/workflows/yaml-generator.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,18 @@ interface YamlBlock {
1010
type: string
1111
name: string
1212
inputs?: Record<string, any>
13-
preceding?: string[]
14-
following?: string[]
13+
connections?: {
14+
incoming?: Array<{
15+
source: string
16+
sourceHandle?: string
17+
targetHandle?: string
18+
}>
19+
outgoing?: Array<{
20+
target: string
21+
sourceHandle?: string
22+
targetHandle?: string
23+
}>
24+
}
1525
parentId?: string // Add parentId for nested blocks
1626
}
1727

@@ -153,23 +163,37 @@ function extractBlockInputs(
153163
}
154164

155165
/**
156-
* Find preceding blocks for a given block ID
166+
* Find incoming connections for a given block ID
157167
*/
158-
function findPrecedingBlocks(blockId: string, edges: any[]): string[] {
168+
function findIncomingConnections(blockId: string, edges: any[]): Array<{
169+
source: string
170+
sourceHandle?: string
171+
targetHandle?: string
172+
}> {
159173
return edges
160174
.filter((edge) => edge.target === blockId)
161-
.map((edge) => edge.source)
162-
.filter((source, index, arr) => arr.indexOf(source) === index) // Remove duplicates
175+
.map((edge) => ({
176+
source: edge.source,
177+
sourceHandle: edge.sourceHandle,
178+
targetHandle: edge.targetHandle,
179+
}))
163180
}
164181

165182
/**
166-
* Find following blocks for a given block ID
183+
* Find outgoing connections for a given block ID
167184
*/
168-
function findFollowingBlocks(blockId: string, edges: any[]): string[] {
185+
function findOutgoingConnections(blockId: string, edges: any[]): Array<{
186+
target: string
187+
sourceHandle?: string
188+
targetHandle?: string
189+
}> {
169190
return edges
170191
.filter((edge) => edge.source === blockId)
171-
.map((edge) => edge.target)
172-
.filter((target, index, arr) => arr.indexOf(target) === index) // Remove duplicates
192+
.map((edge) => ({
193+
target: edge.target,
194+
sourceHandle: edge.sourceHandle,
195+
targetHandle: edge.targetHandle,
196+
}))
173197
}
174198

175199
/**
@@ -189,8 +213,8 @@ export function generateWorkflowYaml(
189213
// Process each block
190214
Object.entries(workflowState.blocks).forEach(([blockId, blockState]) => {
191215
const inputs = extractBlockInputs(blockState, blockId, subBlockValues)
192-
const preceding = findPrecedingBlocks(blockId, workflowState.edges)
193-
const following = findFollowingBlocks(blockId, workflowState.edges)
216+
const incoming = findIncomingConnections(blockId, workflowState.edges)
217+
const outgoing = findOutgoingConnections(blockId, workflowState.edges)
194218

195219
const yamlBlock: YamlBlock = {
196220
type: blockState.type,
@@ -203,12 +227,16 @@ export function generateWorkflowYaml(
203227
}
204228

205229
// Only include connections if they exist
206-
if (preceding.length > 0) {
207-
yamlBlock.preceding = preceding
208-
}
209-
210-
if (following.length > 0) {
211-
yamlBlock.following = following
230+
if (incoming.length > 0 || outgoing.length > 0) {
231+
yamlBlock.connections = {}
232+
233+
if (incoming.length > 0) {
234+
yamlBlock.connections.incoming = incoming
235+
}
236+
237+
if (outgoing.length > 0) {
238+
yamlBlock.connections.outgoing = outgoing
239+
}
212240
}
213241

214242
// Include parent-child relationship for nested blocks

apps/sim/stores/workflows/yaml/importer.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ interface YamlBlock {
99
type: string
1010
name: string
1111
inputs?: Record<string, any>
12-
preceding?: string[]
13-
following?: string[]
12+
connections?: {
13+
incoming?: Array<{
14+
source: string
15+
sourceHandle?: string
16+
targetHandle?: string
17+
}>
18+
outgoing?: Array<{
19+
target: string
20+
sourceHandle?: string
21+
targetHandle?: string
22+
}>
23+
}
1424
parentId?: string // Add parentId for nested blocks
1525
}
1626

@@ -120,23 +130,28 @@ function validateBlockReferences(yamlWorkflow: YamlWorkflow): string[] {
120130
const blockIds = new Set(Object.keys(yamlWorkflow.blocks))
121131

122132
Object.entries(yamlWorkflow.blocks).forEach(([blockId, block]) => {
123-
// Check preceding references
124-
if (block.preceding) {
125-
block.preceding.forEach((precedingId) => {
126-
if (!blockIds.has(precedingId)) {
127-
errors.push(`Block '${blockId}' references non-existent preceding block '${precedingId}'`)
133+
// Check incoming connection references
134+
if (block.connections?.incoming) {
135+
block.connections.incoming.forEach((connection) => {
136+
if (!blockIds.has(connection.source)) {
137+
errors.push(`Block '${blockId}' references non-existent source block '${connection.source}'`)
128138
}
129139
})
130140
}
131141

132-
// Check following references
133-
if (block.following) {
134-
block.following.forEach((followingId) => {
135-
if (!blockIds.has(followingId)) {
136-
errors.push(`Block '${blockId}' references non-existent following block '${followingId}'`)
142+
// Check outgoing connection references
143+
if (block.connections?.outgoing) {
144+
block.connections.outgoing.forEach((connection) => {
145+
if (!blockIds.has(connection.target)) {
146+
errors.push(`Block '${blockId}' references non-existent target block '${connection.target}'`)
137147
}
138148
})
139149
}
150+
151+
// Check parent references
152+
if (block.parentId && !blockIds.has(block.parentId)) {
153+
errors.push(`Block '${blockId}' references non-existent parent block '${block.parentId}'`)
154+
}
140155
})
141156

142157
return errors
@@ -190,10 +205,10 @@ function calculateBlockPositions(
190205
const positions: Record<string, { x: number; y: number }> = {}
191206
const blockIds = Object.keys(yamlWorkflow.blocks)
192207

193-
// Find starter blocks (no preceding connections)
208+
// Find starter blocks (no incoming connections)
194209
const starterBlocks = blockIds.filter((id) => {
195210
const block = yamlWorkflow.blocks[id]
196-
return !block.preceding || block.preceding.length === 0
211+
return !block.connections?.incoming || block.connections.incoming.length === 0
197212
})
198213

199214
// If no starter blocks found, use first block as starter
@@ -220,10 +235,10 @@ function calculateBlockPositions(
220235

221236
// Add following blocks to queue
222237
const block = yamlWorkflow.blocks[blockId]
223-
if (block.following) {
224-
block.following.forEach((followingId) => {
225-
if (!visited.has(followingId)) {
226-
queue.push(followingId)
238+
if (block.connections?.outgoing) {
239+
block.connections.outgoing.forEach((connection) => {
240+
if (!visited.has(connection.target)) {
241+
queue.push(connection.target)
227242
}
228243
})
229244
}
@@ -375,16 +390,16 @@ export function convertYamlToWorkflow(yamlWorkflow: YamlWorkflow): ImportResult
375390

376391
// Convert edges from connections
377392
Object.entries(yamlWorkflow.blocks).forEach(([blockId, yamlBlock]) => {
378-
if (yamlBlock.following) {
379-
yamlBlock.following.forEach((targetId) => {
380-
const edgeId = `${blockId}-${targetId}-${Date.now()}`
393+
if (yamlBlock.connections?.outgoing) {
394+
yamlBlock.connections.outgoing.forEach((connection) => {
395+
const edgeId = `${blockId}-${connection.target}-${Date.now()}`
381396

382397
const edge: ImportedEdge = {
383398
id: edgeId,
384399
source: blockId,
385-
target: targetId,
386-
sourceHandle: 'source',
387-
targetHandle: 'target',
400+
target: connection.target,
401+
sourceHandle: connection.sourceHandle || 'source',
402+
targetHandle: connection.targetHandle || 'target',
388403
type: 'workflowEdge',
389404
}
390405

0 commit comments

Comments
 (0)