add json5 for parsing
This commit is contained in:
parent
bb7c1f62cb
commit
8fd7b9350f
|
|
@ -105,6 +105,7 @@
|
||||||
"ioredis": "^5.3.2",
|
"ioredis": "^5.3.2",
|
||||||
"ipaddr.js": "^2.2.0",
|
"ipaddr.js": "^2.2.0",
|
||||||
"jsdom": "^22.1.0",
|
"jsdom": "^22.1.0",
|
||||||
|
"json5": "2.2.3",
|
||||||
"jsonpointer": "^5.0.1",
|
"jsonpointer": "^5.0.1",
|
||||||
"jsonrepair": "^3.11.1",
|
"jsonrepair": "^3.11.1",
|
||||||
"langchain": "^0.3.5",
|
"langchain": "^0.3.5",
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import { TextSplitter } from 'langchain/text_splitter'
|
||||||
import { DocumentLoader } from 'langchain/document_loaders/base'
|
import { DocumentLoader } from 'langchain/document_loaders/base'
|
||||||
import { NodeVM } from '@flowiseai/nodevm'
|
import { NodeVM } from '@flowiseai/nodevm'
|
||||||
import { Sandbox } from '@e2b/code-interpreter'
|
import { Sandbox } from '@e2b/code-interpreter'
|
||||||
|
import JSON5 from 'json5'
|
||||||
|
|
||||||
export const numberOrExpressionRegex = '^(\\d+\\.?\\d*|{{.*}})$' //return true if string consists only numbers OR expression {{}}
|
export const numberOrExpressionRegex = '^(\\d+\\.?\\d*|{{.*}})$' //return true if string consists only numbers OR expression {{}}
|
||||||
export const notEmptyRegex = '(.|\\s)*\\S(.|\\s)*' //return true if string is not empty or blank
|
export const notEmptyRegex = '(.|\\s)*\\S(.|\\s)*' //return true if string is not empty or blank
|
||||||
|
|
@ -1382,6 +1383,39 @@ const convertRequireToImport = (requireLine: string): string | null => {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse output if it's a stringified JSON or array
|
||||||
|
* @param {any} output - The output to parse
|
||||||
|
* @returns {any} - The parsed output or original output if not parseable
|
||||||
|
*/
|
||||||
|
const parseOutput = (output: any): any => {
|
||||||
|
// If output is not a string, return as-is
|
||||||
|
if (typeof output !== 'string') {
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim whitespace
|
||||||
|
const trimmedOutput = output.trim()
|
||||||
|
|
||||||
|
// Check if it's an empty string
|
||||||
|
if (!trimmedOutput) {
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it looks like JSON (starts with { or [)
|
||||||
|
if ((trimmedOutput.startsWith('{') && trimmedOutput.endsWith('}')) || (trimmedOutput.startsWith('[') && trimmedOutput.endsWith(']'))) {
|
||||||
|
try {
|
||||||
|
const parsedOutput = JSON5.parse(trimmedOutput)
|
||||||
|
return parsedOutput
|
||||||
|
} catch (e) {
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the original string if it doesn't look like JSON
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute JavaScript code using either Sandbox or NodeVM
|
* Execute JavaScript code using either Sandbox or NodeVM
|
||||||
* @param {string} code - The JavaScript code to execute
|
* @param {string} code - The JavaScript code to execute
|
||||||
|
|
@ -1497,7 +1531,7 @@ export const executeJavaScriptCode = async (
|
||||||
// Clean up sandbox
|
// Clean up sandbox
|
||||||
sbx.kill()
|
sbx.kill()
|
||||||
|
|
||||||
return output
|
return parseOutput(output)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(`Sandbox Execution Error: ${e}`)
|
throw new Error(`Sandbox Execution Error: ${e}`)
|
||||||
}
|
}
|
||||||
|
|
@ -1539,7 +1573,7 @@ export const executeJavaScriptCode = async (
|
||||||
streamOutput(streamOutputString)
|
streamOutput(streamOutputString)
|
||||||
}
|
}
|
||||||
|
|
||||||
return finalOutput
|
return parseOutput(finalOutput)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(`NodeVM Execution Error: ${e}`)
|
throw new Error(`NodeVM Execution Error: ${e}`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -375,6 +375,9 @@ importers:
|
||||||
jsdom:
|
jsdom:
|
||||||
specifier: ^22.1.0
|
specifier: ^22.1.0
|
||||||
version: 22.1.0(bufferutil@4.0.8)(canvas@2.11.2(encoding@0.1.13))(utf-8-validate@6.0.4)
|
version: 22.1.0(bufferutil@4.0.8)(canvas@2.11.2(encoding@0.1.13))(utf-8-validate@6.0.4)
|
||||||
|
json5:
|
||||||
|
specifier: 2.2.3
|
||||||
|
version: 2.2.3
|
||||||
jsonpointer:
|
jsonpointer:
|
||||||
specifier: ^5.0.1
|
specifier: ^5.0.1
|
||||||
version: 5.0.1
|
version: 5.0.1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue