Skip to content

fix: robust JSON parsing in Iteration node #4603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/components/nodes/agentflow/Iteration/Iteration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { handleEscapeCharacters } from '../../../src/utils'

class Iteration_Agentflow implements INode {
label: string
Expand Down Expand Up @@ -40,11 +41,22 @@ class Iteration_Agentflow implements INode {

// Helper function to clean JSON strings with redundant backslashes
const cleanJsonString = (str: string): string => {
return str.replace(/\\(["'[\]{}])/g, '$1')
return str.replace(/\\(["'\[\]{}])/g, '$1')
}

const iterationInputArray =
typeof iterationInput === 'string' && iterationInput !== '' ? JSON.parse(cleanJsonString(iterationInput)) : iterationInput
// Try robust parsing using handleEscapeCharacters
let iterationInputArray
if (typeof iterationInput === 'string' && iterationInput !== '') {
let cleaned = cleanJsonString(iterationInput)
try {
iterationInputArray = JSON.parse(handleEscapeCharacters(cleaned, true))
} catch (e) {
// fallback to previous logic if parsing fails
iterationInputArray = JSON.parse(cleaned)
}
} else {
iterationInputArray = iterationInput
}

if (!iterationInputArray || !Array.isArray(iterationInputArray)) {
throw new Error('Invalid input array')
Expand All @@ -66,4 +78,4 @@ class Iteration_Agentflow implements INode {
}
}

module.exports = { nodeClass: Iteration_Agentflow }
export const nodeClass = Iteration_Agentflow;