mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
Bugfix/Gsuite tool params (#5189)
* fix gsuite tool params * custom assistant only check for mandatory fields for visible params * azure chat openai fix for gpt5 * return raw from executeJavaScriptCode * add json5 for parsing * azure chatopenai use maxCompletionTokens
This commit is contained in:
@@ -30,6 +30,16 @@ export const SOURCE_DOCUMENTS_PREFIX = '\n\n----FLOWISE_SOURCE_DOCUMENTS----\n\n
|
||||
export const ARTIFACTS_PREFIX = '\n\n----FLOWISE_ARTIFACTS----\n\n'
|
||||
export const TOOL_ARGS_PREFIX = '\n\n----FLOWISE_TOOL_ARGS----\n\n'
|
||||
|
||||
/**
|
||||
* Utility function to format tool error messages with parameters for debugging
|
||||
* @param errorMessage - The base error message
|
||||
* @param params - The parameters that were passed to the tool
|
||||
* @returns Formatted error message with tool arguments appended
|
||||
*/
|
||||
export const formatToolError = (errorMessage: string, params: any): string => {
|
||||
return errorMessage + TOOL_ARGS_PREFIX + JSON.stringify(params)
|
||||
}
|
||||
|
||||
export type AgentFinish = {
|
||||
returnValues: Record<string, any>
|
||||
log: string
|
||||
|
||||
@@ -18,6 +18,7 @@ import { TextSplitter } from 'langchain/text_splitter'
|
||||
import { DocumentLoader } from 'langchain/document_loaders/base'
|
||||
import { NodeVM } from '@flowiseai/nodevm'
|
||||
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 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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param {string} code - The JavaScript code to execute
|
||||
@@ -1497,7 +1531,7 @@ export const executeJavaScriptCode = async (
|
||||
// Clean up sandbox
|
||||
sbx.kill()
|
||||
|
||||
return output
|
||||
return parseOutput(output)
|
||||
} catch (e) {
|
||||
throw new Error(`Sandbox Execution Error: ${e}`)
|
||||
}
|
||||
@@ -1529,16 +1563,17 @@ export const executeJavaScriptCode = async (
|
||||
const response = await vm.run(`module.exports = async function() {${code}}()`, __dirname)
|
||||
|
||||
let finalOutput = response
|
||||
if (typeof response === 'object') {
|
||||
finalOutput = JSON.stringify(response, null, 2)
|
||||
}
|
||||
|
||||
// Stream output if streaming function provided
|
||||
if (streamOutput && finalOutput) {
|
||||
streamOutput(finalOutput)
|
||||
let streamOutputString = finalOutput
|
||||
if (typeof response === 'object') {
|
||||
streamOutputString = JSON.stringify(finalOutput, null, 2)
|
||||
}
|
||||
streamOutput(streamOutputString)
|
||||
}
|
||||
|
||||
return finalOutput
|
||||
return parseOutput(finalOutput)
|
||||
} catch (e) {
|
||||
throw new Error(`NodeVM Execution Error: ${e}`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user