Skip to content

Commit 39d8e9c

Browse files
committed
fix(loops): string loop
1 parent 554279a commit 39d8e9c

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

sim/executor/loops.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,18 @@ export class LoopManager {
247247
return [];
248248
}
249249

250-
// Simple expression evaluation using Function constructor
250+
// First check if it's valid JSON (array or object)
251+
if (trimmedExpression.startsWith('[') || trimmedExpression.startsWith('{')) {
252+
try {
253+
// Try to parse as JSON first
254+
return JSON.parse(trimmedExpression);
255+
} catch (jsonError) {
256+
console.error(`Error parsing JSON for loop ${loopId}:`, jsonError);
257+
// If JSON parsing fails, continue with expression evaluation
258+
}
259+
}
260+
261+
// If not valid JSON or JSON parsing failed, try to evaluate as an expression
251262
const result = new Function('context', `return ${loop.forEachItems}`)(context);
252263

253264
// If the result is an array or object, return it

sim/executor/resolver.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,18 @@ export class InputResolver {
626626
if (typeof loop.forEachItems === 'string') {
627627
try {
628628
// Check if it's valid JSON
629-
if (loop.forEachItems.trim().startsWith('[') || loop.forEachItems.trim().startsWith('{')) {
630-
return JSON.parse(loop.forEachItems);
629+
const trimmedExpression = loop.forEachItems.trim();
630+
if (trimmedExpression.startsWith('[') || trimmedExpression.startsWith('{')) {
631+
try {
632+
// Try to parse as JSON first
633+
return JSON.parse(trimmedExpression);
634+
} catch (jsonError) {
635+
console.error(`Error parsing JSON for loop:`, jsonError);
636+
// If JSON parsing fails, continue with expression evaluation
637+
}
631638
}
632639

633-
// Otherwise, try to evaluate it as an expression
634-
const trimmedExpression = loop.forEachItems.trim();
640+
// If not valid JSON or JSON parsing failed, try to evaluate as an expression
635641
if (trimmedExpression && !trimmedExpression.startsWith('//')) {
636642
const result = new Function('context', `return ${loop.forEachItems}`)(context);
637643
if (Array.isArray(result) || (typeof result === 'object' && result !== null)) {

sim/stores/workflows/workflow/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,13 +572,14 @@ export const useWorkflowStore = create<WorkflowStoreWithHistory>()(
572572
) {
573573
try {
574574
// First try to parse to validate it's valid JSON
575-
JSON.parse(items);
575+
const parsed = JSON.parse(items);
576576

577577
// If parsing succeeds, store the original string to preserve formatting
578578
// This way we keep the user's exact formatting (spacing, line breaks, etc.)
579579
parsedItems = items;
580580
} catch (e) {
581-
// If parsing fails, keep it as a string
581+
// If parsing fails, keep it as a string expression
582+
console.error('Invalid JSON format for forEach items:', e);
582583
parsedItems = items;
583584
}
584585
}

0 commit comments

Comments
 (0)