mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 07:01:04 +03:00
Bugfix/Parse JSON correctly (#5220)
* parse JSON correctly * add codeblock highlight
This commit is contained in:
@@ -8,8 +8,7 @@ import {
|
||||
IServerSideEventStreamer
|
||||
} from '../../../src/Interface'
|
||||
import axios, { AxiosRequestConfig } from 'axios'
|
||||
import { getCredentialData, getCredentialParam, processTemplateVariables } from '../../../src/utils'
|
||||
import JSON5 from 'json5'
|
||||
import { getCredentialData, getCredentialParam, processTemplateVariables, parseJsonBody } from '../../../src/utils'
|
||||
import { DataSource } from 'typeorm'
|
||||
import { BaseMessageLike } from '@langchain/core/messages'
|
||||
import { updateFlowState } from '../utils'
|
||||
@@ -168,7 +167,7 @@ class ExecuteFlow_Agentflow implements INode {
|
||||
let overrideConfig = nodeData.inputs?.executeFlowOverrideConfig
|
||||
if (typeof overrideConfig === 'string' && overrideConfig.startsWith('{') && overrideConfig.endsWith('}')) {
|
||||
try {
|
||||
overrideConfig = JSON5.parse(overrideConfig)
|
||||
overrideConfig = parseJsonBody(overrideConfig)
|
||||
} catch (parseError) {
|
||||
throw new Error(`Invalid JSON in executeFlowOverrideConfig: ${parseError.message}`)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,8 @@ import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Inter
|
||||
import { AxiosRequestConfig, Method, ResponseType } from 'axios'
|
||||
import FormData from 'form-data'
|
||||
import * as querystring from 'querystring'
|
||||
import { getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||
import { getCredentialData, getCredentialParam, parseJsonBody } from '../../../src/utils'
|
||||
import { secureAxiosRequest } from '../../../src/httpSecurity'
|
||||
import JSON5 from 'json5'
|
||||
|
||||
class HTTP_Agentflow implements INode {
|
||||
label: string
|
||||
@@ -20,16 +19,6 @@ class HTTP_Agentflow implements INode {
|
||||
credential: INodeParams
|
||||
inputs: INodeParams[]
|
||||
|
||||
private parseJsonBody(body: string): any {
|
||||
try {
|
||||
return JSON5.parse(body)
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Invalid JSON format in body. Original error: ${error.message}. Please ensure your JSON is properly formatted with quoted strings and valid escape sequences.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.label = 'HTTP'
|
||||
this.name = 'httpAgentflow'
|
||||
@@ -285,7 +274,7 @@ class HTTP_Agentflow implements INode {
|
||||
if (method !== 'GET' && body) {
|
||||
switch (bodyType) {
|
||||
case 'json': {
|
||||
requestConfig.data = typeof body === 'string' ? this.parseJsonBody(body) : body
|
||||
requestConfig.data = typeof body === 'string' ? parseJsonBody(body) : body
|
||||
requestHeaders['Content-Type'] = 'application/json'
|
||||
break
|
||||
}
|
||||
@@ -303,7 +292,7 @@ class HTTP_Agentflow implements INode {
|
||||
break
|
||||
}
|
||||
case 'xWwwFormUrlencoded':
|
||||
requestConfig.data = querystring.stringify(typeof body === 'string' ? this.parseJsonBody(body) : body)
|
||||
requestConfig.data = querystring.stringify(typeof body === 'string' ? parseJsonBody(body) : body)
|
||||
requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'
|
||||
break
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import JSON5 from 'json5'
|
||||
import { parseJsonBody } from '../../../src/utils'
|
||||
|
||||
class Iteration_Agentflow implements INode {
|
||||
label: string
|
||||
@@ -42,10 +42,10 @@ class Iteration_Agentflow implements INode {
|
||||
// Helper function to clean JSON strings with redundant backslashes
|
||||
const safeParseJson = (str: string): string => {
|
||||
try {
|
||||
return JSON5.parse(str)
|
||||
return parseJsonBody(str)
|
||||
} catch {
|
||||
// Try parsing after cleaning
|
||||
return JSON5.parse(str.replace(/\\(["'[\]{}])/g, '$1'))
|
||||
return parseJsonBody(str.replace(/\\(["'[\]{}])/g, '$1'))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Tool } from '@langchain/core/tools'
|
||||
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../../src/Interface'
|
||||
import { MCPToolkit, validateMCPServerConfig } from '../core'
|
||||
import { getVars, prepareSandboxVars } from '../../../../src/utils'
|
||||
import { getVars, prepareSandboxVars, parseJsonBody } from '../../../../src/utils'
|
||||
import { DataSource } from 'typeorm'
|
||||
import hash from 'object-hash'
|
||||
import JSON5 from 'json5'
|
||||
|
||||
const mcpServerConfig = `{
|
||||
"command": "npx",
|
||||
@@ -270,7 +269,7 @@ function substituteVariablesInString(str: string, sandbox: any): string {
|
||||
|
||||
function convertToValidJSONString(inputString: string) {
|
||||
try {
|
||||
const jsObject = JSON5.parse(inputString)
|
||||
const jsObject = parseJsonBody(inputString)
|
||||
return JSON.stringify(jsObject, null, 2)
|
||||
} catch (error) {
|
||||
console.error('Error converting to JSON:', error)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
|
||||
import { getBaseClasses, stripHTMLFromToolInput, parseJsonBody } from '../../../src/utils'
|
||||
import { desc, RequestParameters, RequestsDeleteTool } from './core'
|
||||
import JSON5 from 'json5'
|
||||
|
||||
const codeExample = `{
|
||||
"id": {
|
||||
@@ -131,7 +130,7 @@ class RequestsDelete_Tools implements INode {
|
||||
if (queryParamsSchema) obj.queryParamsSchema = queryParamsSchema
|
||||
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
|
||||
if (headers) {
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : parseJsonBody(stripHTMLFromToolInput(headers))
|
||||
obj.headers = parsedHeaders
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
|
||||
import { secureFetch } from '../../../src/httpSecurity'
|
||||
import JSON5 from 'json5'
|
||||
import { parseJsonBody } from '../../../src/utils'
|
||||
|
||||
export const desc = `Use this when you need to execute a DELETE request to remove data from a website.`
|
||||
|
||||
@@ -23,7 +23,7 @@ const createRequestsDeleteSchema = (queryParamsSchema?: string) => {
|
||||
// If queryParamsSchema is provided, parse it and add dynamic query params
|
||||
if (queryParamsSchema) {
|
||||
try {
|
||||
const parsedSchema = JSON5.parse(queryParamsSchema)
|
||||
const parsedSchema = parseJsonBody(queryParamsSchema)
|
||||
const queryParamsObject: Record<string, z.ZodTypeAny> = {}
|
||||
|
||||
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {
|
||||
@@ -109,7 +109,7 @@ export class RequestsDeleteTool extends DynamicStructuredTool {
|
||||
|
||||
if (this.queryParamsSchema && params.queryParams && Object.keys(params.queryParams).length > 0) {
|
||||
try {
|
||||
const parsedSchema = JSON5.parse(this.queryParamsSchema)
|
||||
const parsedSchema = parseJsonBody(this.queryParamsSchema)
|
||||
const pathParams: Array<{ key: string; value: string }> = []
|
||||
|
||||
Object.entries(params.queryParams).forEach(([key, value]) => {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
|
||||
import { getBaseClasses, stripHTMLFromToolInput, parseJsonBody } from '../../../src/utils'
|
||||
import { desc, RequestParameters, RequestsGetTool } from './core'
|
||||
import JSON5 from 'json5'
|
||||
|
||||
const codeExample = `{
|
||||
"id": {
|
||||
@@ -131,7 +130,7 @@ class RequestsGet_Tools implements INode {
|
||||
if (queryParamsSchema) obj.queryParamsSchema = queryParamsSchema
|
||||
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
|
||||
if (headers) {
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : parseJsonBody(stripHTMLFromToolInput(headers))
|
||||
obj.headers = parsedHeaders
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
|
||||
import { secureFetch } from '../../../src/httpSecurity'
|
||||
import JSON5 from 'json5'
|
||||
import { parseJsonBody } from '../../../src/utils'
|
||||
|
||||
export const desc = `Use this when you need to execute a GET request to get data from a website.`
|
||||
|
||||
@@ -23,7 +23,7 @@ const createRequestsGetSchema = (queryParamsSchema?: string) => {
|
||||
// If queryParamsSchema is provided, parse it and add dynamic query params
|
||||
if (queryParamsSchema) {
|
||||
try {
|
||||
const parsedSchema = JSON5.parse(queryParamsSchema)
|
||||
const parsedSchema = parseJsonBody(queryParamsSchema)
|
||||
const queryParamsObject: Record<string, z.ZodTypeAny> = {}
|
||||
|
||||
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {
|
||||
@@ -109,7 +109,7 @@ export class RequestsGetTool extends DynamicStructuredTool {
|
||||
|
||||
if (this.queryParamsSchema && params.queryParams && Object.keys(params.queryParams).length > 0) {
|
||||
try {
|
||||
const parsedSchema = JSON5.parse(this.queryParamsSchema)
|
||||
const parsedSchema = parseJsonBody(this.queryParamsSchema)
|
||||
const pathParams: Array<{ key: string; value: string }> = []
|
||||
|
||||
Object.entries(params.queryParams).forEach(([key, value]) => {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
|
||||
import { getBaseClasses, stripHTMLFromToolInput, parseJsonBody } from '../../../src/utils'
|
||||
import { RequestParameters, desc, RequestsPostTool } from './core'
|
||||
import JSON5 from 'json5'
|
||||
|
||||
const codeExample = `{
|
||||
"name": {
|
||||
@@ -141,11 +140,11 @@ class RequestsPost_Tools implements INode {
|
||||
if (bodySchema) obj.bodySchema = stripHTMLFromToolInput(bodySchema)
|
||||
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
|
||||
if (headers) {
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : parseJsonBody(stripHTMLFromToolInput(headers))
|
||||
obj.headers = parsedHeaders
|
||||
}
|
||||
if (body) {
|
||||
const parsedBody = typeof body === 'object' ? body : JSON5.parse(body)
|
||||
const parsedBody = typeof body === 'object' ? body : parseJsonBody(body)
|
||||
obj.body = parsedBody
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
|
||||
import { secureFetch } from '../../../src/httpSecurity'
|
||||
import JSON5 from 'json5'
|
||||
import { parseJsonBody } from '../../../src/utils'
|
||||
|
||||
export const desc = `Use this when you want to execute a POST request to create or update a resource.`
|
||||
|
||||
@@ -28,7 +28,7 @@ const createRequestsPostSchema = (bodySchema?: string) => {
|
||||
// If bodySchema is provided, parse it and add dynamic body params
|
||||
if (bodySchema) {
|
||||
try {
|
||||
const parsedSchema = JSON5.parse(bodySchema)
|
||||
const parsedSchema = parseJsonBody(bodySchema)
|
||||
const bodyParamsObject: Record<string, z.ZodTypeAny> = {}
|
||||
|
||||
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
|
||||
import { getBaseClasses, stripHTMLFromToolInput, parseJsonBody } from '../../../src/utils'
|
||||
import { RequestParameters, desc, RequestsPutTool } from './core'
|
||||
import JSON5 from 'json5'
|
||||
|
||||
const codeExample = `{
|
||||
"name": {
|
||||
@@ -141,11 +140,11 @@ class RequestsPut_Tools implements INode {
|
||||
if (bodySchema) obj.bodySchema = stripHTMLFromToolInput(bodySchema)
|
||||
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
|
||||
if (headers) {
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
|
||||
const parsedHeaders = typeof headers === 'object' ? headers : parseJsonBody(stripHTMLFromToolInput(headers))
|
||||
obj.headers = parsedHeaders
|
||||
}
|
||||
if (body) {
|
||||
const parsedBody = typeof body === 'object' ? body : JSON5.parse(body)
|
||||
const parsedBody = typeof body === 'object' ? body : parseJsonBody(body)
|
||||
obj.body = parsedBody
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
|
||||
import { secureFetch } from '../../../src/httpSecurity'
|
||||
import JSON5 from 'json5'
|
||||
import { parseJsonBody } from '../../../src/utils'
|
||||
|
||||
export const desc = `Use this when you want to execute a PUT request to update or replace a resource.`
|
||||
|
||||
@@ -28,7 +28,7 @@ const createRequestsPutSchema = (bodySchema?: string) => {
|
||||
// If bodySchema is provided, parse it and add dynamic body params
|
||||
if (bodySchema) {
|
||||
try {
|
||||
const parsedSchema = JSON5.parse(bodySchema)
|
||||
const parsedSchema = parseJsonBody(bodySchema)
|
||||
const bodyParamsObject: Record<string, z.ZodTypeAny> = {}
|
||||
|
||||
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {
|
||||
|
||||
Reference in New Issue
Block a user