Bugfix/pass execute custom function to worker (#4432)

pass execute custom function to worker
This commit is contained in:
Henry Heng
2025-05-16 01:13:25 +08:00
committed by GitHub
parent a6e64230b4
commit 0a4570ecda
3 changed files with 95 additions and 43 deletions
@@ -0,0 +1,58 @@
import { handleEscapeCharacters, ICommonObject } from 'flowise-components'
import { databaseEntities } from '.'
import { InternalFlowiseError } from '../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'
import { getErrorMessage } from '../errors/utils'
import { DataSource } from 'typeorm'
import { IComponentNodes } from '../Interface'
export const executeCustomNodeFunction = async ({
appDataSource,
componentNodes,
data
}: {
appDataSource: DataSource
componentNodes: IComponentNodes
data: any
}) => {
try {
const body = data
const functionInputVariables = Object.fromEntries(
[...(body?.javascriptFunction ?? '').matchAll(/\$([a-zA-Z0-9_]+)/g)].map((g) => [g[1], undefined])
)
if (functionInputVariables && Object.keys(functionInputVariables).length) {
for (const key in functionInputVariables) {
if (key.includes('vars')) {
delete functionInputVariables[key]
}
}
}
const nodeData = { inputs: { functionInputVariables, ...body } }
if (Object.prototype.hasOwnProperty.call(componentNodes, 'customFunction')) {
try {
const nodeInstanceFilePath = componentNodes['customFunction'].filePath as string
const nodeModule = await import(nodeInstanceFilePath)
const newNodeInstance = new nodeModule.nodeClass()
const options: ICommonObject = {
appDataSource,
databaseEntities
}
const returnData = await newNodeInstance.init(nodeData, '', options)
const dbResponse = typeof returnData === 'string' ? handleEscapeCharacters(returnData, true) : returnData
return dbResponse
} catch (error) {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error running custom function: ${error}`)
}
} else {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Node customFunction not found`)
}
} catch (error) {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: nodesService.executeCustomFunction - ${getErrorMessage(error)}`
)
}
}