Skip to content

Commit

Permalink
feat: add support for calling tools in CustomFunction (#3143)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish authored Sep 5, 2024
1 parent 0e30db1 commit 1e8839e
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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 { NodeVM } from 'vm2'
import { DataSource } from 'typeorm'
Expand All @@ -19,7 +21,7 @@ class CustomFunction_Utilities implements INode {
constructor() {
this.label = 'Custom JS Function'
this.name = 'customFunction'
this.version = 2.0
this.version = 3.0
this.type = 'CustomFunction'
this.icon = 'customfunction.svg'
this.category = 'Utilities'
Expand All @@ -43,6 +45,14 @@ class CustomFunction_Utilities implements INode {
optional: true,
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',
name: 'javascriptFunction',
Expand Down Expand Up @@ -71,6 +81,7 @@ class CustomFunction_Utilities implements INode {
const functionInputVariablesRaw = nodeData.inputs?.functionInputVariables
const appDataSource = options.appDataSource as DataSource
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 flow = {
Expand Down Expand Up @@ -109,6 +120,7 @@ class CustomFunction_Utilities implements INode {
let sandbox: any = { $input: input }
sandbox['$vars'] = prepareSandboxVars(variables)
sandbox['$flow'] = flow
sandbox['$tools'] = tools

if (Object.keys(inputVars).length) {
for (const item in inputVars) {
Expand Down

0 comments on commit 1e8839e

Please sign in to comment.