Skip to content

Commit bbf6970

Browse files
authored
Bugfix/Add proper parsing for tool input args (#4780)
add proper parsing for tool input args
1 parent 9b60cf1 commit bbf6970

File tree

1 file changed

+32
-1
lines changed
  • packages/components/nodes/agentflow/Tool

1 file changed

+32
-1
lines changed

packages/components/nodes/agentflow/Tool/Tool.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,37 @@ class Tool_Agentflow implements INode {
227227

228228
let toolCallArgs: Record<string, any> = {}
229229

230+
const parseInputValue = (value: string): any => {
231+
if (typeof value !== 'string') {
232+
return value
233+
}
234+
235+
// Remove escape characters (backslashes before special characters)
236+
// ex: \["a", "b", "c", "d", "e"\]
237+
let cleanedValue = value
238+
.replace(/\\"/g, '"') // \" -> "
239+
.replace(/\\\\/g, '\\') // \\ -> \
240+
.replace(/\\\[/g, '[') // \[ -> [
241+
.replace(/\\\]/g, ']') // \] -> ]
242+
.replace(/\\\{/g, '{') // \{ -> {
243+
.replace(/\\\}/g, '}') // \} -> }
244+
245+
// Try to parse as JSON if it looks like JSON/array
246+
if (
247+
(cleanedValue.startsWith('[') && cleanedValue.endsWith(']')) ||
248+
(cleanedValue.startsWith('{') && cleanedValue.endsWith('}'))
249+
) {
250+
try {
251+
return JSON.parse(cleanedValue)
252+
} catch (e) {
253+
// If parsing fails, return the cleaned value
254+
return cleanedValue
255+
}
256+
}
257+
258+
return cleanedValue
259+
}
260+
230261
if (newToolNodeInstance.transformNodeInputsToToolArgs) {
231262
const defaultParams = newToolNodeInstance.transformNodeInputsToToolArgs(newNodeData)
232263

@@ -239,7 +270,7 @@ class Tool_Agentflow implements INode {
239270
for (const item of toolInputArgs) {
240271
const variableName = item.inputArgName
241272
const variableValue = item.inputArgValue
242-
toolCallArgs[variableName] = variableValue
273+
toolCallArgs[variableName] = parseInputValue(variableValue)
243274
}
244275

245276
const flowConfig = {

0 commit comments

Comments
 (0)