mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 23:01:09 +03:00
feat: add support for calling tools in CustomFunction (#3143)
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { flatten } from 'lodash'
|
||||||
|
import { type StructuredTool } from '@langchain/core/tools'
|
||||||
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
|
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
|
||||||
import { NodeVM } from 'vm2'
|
import { NodeVM } from 'vm2'
|
||||||
import { DataSource } from 'typeorm'
|
import { DataSource } from 'typeorm'
|
||||||
@@ -19,7 +21,7 @@ class CustomFunction_Utilities implements INode {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.label = 'Custom JS Function'
|
this.label = 'Custom JS Function'
|
||||||
this.name = 'customFunction'
|
this.name = 'customFunction'
|
||||||
this.version = 2.0
|
this.version = 3.0
|
||||||
this.type = 'CustomFunction'
|
this.type = 'CustomFunction'
|
||||||
this.icon = 'customfunction.svg'
|
this.icon = 'customfunction.svg'
|
||||||
this.category = 'Utilities'
|
this.category = 'Utilities'
|
||||||
@@ -43,6 +45,14 @@ class CustomFunction_Utilities implements INode {
|
|||||||
optional: true,
|
optional: true,
|
||||||
placeholder: 'My Function'
|
placeholder: 'My Function'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: 'Additional Tools',
|
||||||
|
description: 'Tools can be used in the function with $tools.{tool_name}.invoke(args)',
|
||||||
|
name: 'tools',
|
||||||
|
type: 'Tool',
|
||||||
|
list: true,
|
||||||
|
optional: true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: 'Javascript Function',
|
label: 'Javascript Function',
|
||||||
name: 'javascriptFunction',
|
name: 'javascriptFunction',
|
||||||
@@ -71,6 +81,7 @@ class CustomFunction_Utilities implements INode {
|
|||||||
const functionInputVariablesRaw = nodeData.inputs?.functionInputVariables
|
const functionInputVariablesRaw = nodeData.inputs?.functionInputVariables
|
||||||
const appDataSource = options.appDataSource as DataSource
|
const appDataSource = options.appDataSource as DataSource
|
||||||
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||||
|
const tools = Object.fromEntries((flatten(nodeData.inputs?.tools) as StructuredTool[])?.map((tool) => [tool.name, tool]) ?? [])
|
||||||
|
|
||||||
const variables = await getVars(appDataSource, databaseEntities, nodeData)
|
const variables = await getVars(appDataSource, databaseEntities, nodeData)
|
||||||
const flow = {
|
const flow = {
|
||||||
@@ -109,6 +120,7 @@ class CustomFunction_Utilities implements INode {
|
|||||||
let sandbox: any = { $input: input }
|
let sandbox: any = { $input: input }
|
||||||
sandbox['$vars'] = prepareSandboxVars(variables)
|
sandbox['$vars'] = prepareSandboxVars(variables)
|
||||||
sandbox['$flow'] = flow
|
sandbox['$flow'] = flow
|
||||||
|
sandbox['$tools'] = tools
|
||||||
|
|
||||||
if (Object.keys(inputVars).length) {
|
if (Object.keys(inputVars).length) {
|
||||||
for (const item in inputVars) {
|
for (const item in inputVars) {
|
||||||
|
|||||||
Reference in New Issue
Block a user