-
-
Notifications
You must be signed in to change notification settings - Fork 16.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1397 from FlowiseAI/feature/Utilities
Feature/Utilities
- Loading branch information
Showing
23 changed files
with
1,783 additions
and
711 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/components/nodes/utilities/CustomFunction/CustomFunction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' | ||
import { NodeVM } from 'vm2' | ||
import { availableDependencies, handleEscapeCharacters } from '../../../src/utils' | ||
|
||
class CustomFunction_Utilities implements INode { | ||
label: string | ||
name: string | ||
version: number | ||
description: string | ||
type: string | ||
icon: string | ||
category: string | ||
baseClasses: string[] | ||
inputs: INodeParams[] | ||
outputs: INodeOutputsValue[] | ||
|
||
constructor() { | ||
this.label = 'Custom JS Function' | ||
this.name = 'customFunction' | ||
this.version = 1.0 | ||
this.type = 'CustomFunction' | ||
this.icon = 'customfunction.svg' | ||
this.category = 'Utilities' | ||
this.description = `Execute custom javascript function` | ||
this.baseClasses = [this.type, 'Utilities'] | ||
this.inputs = [ | ||
{ | ||
label: 'Input Variables', | ||
name: 'functionInputVariables', | ||
description: 'Input variables can be used in the function with prefix $. For example: $var', | ||
type: 'json', | ||
optional: true, | ||
acceptVariable: true, | ||
list: true | ||
}, | ||
{ | ||
label: 'Function Name', | ||
name: 'functionName', | ||
type: 'string', | ||
optional: true, | ||
placeholder: 'My Function' | ||
}, | ||
{ | ||
label: 'Javascript Function', | ||
name: 'javascriptFunction', | ||
type: 'code' | ||
} | ||
] | ||
this.outputs = [ | ||
{ | ||
label: 'Output', | ||
name: 'output', | ||
baseClasses: ['string', 'number', 'boolean', 'json', 'array'] | ||
} | ||
] | ||
} | ||
|
||
async init(nodeData: INodeData, input: string): Promise<any> { | ||
const javascriptFunction = nodeData.inputs?.javascriptFunction as string | ||
const functionInputVariablesRaw = nodeData.inputs?.functionInputVariables | ||
|
||
let inputVars: ICommonObject = {} | ||
if (functionInputVariablesRaw) { | ||
try { | ||
inputVars = | ||
typeof functionInputVariablesRaw === 'object' ? functionInputVariablesRaw : JSON.parse(functionInputVariablesRaw) | ||
} catch (exception) { | ||
throw new Error("Invalid JSON in the PromptTemplate's promptValues: " + exception) | ||
} | ||
} | ||
|
||
let sandbox: any = { $input: input } | ||
|
||
if (Object.keys(inputVars).length) { | ||
for (const item in inputVars) { | ||
sandbox[`$${item}`] = inputVars[item] | ||
} | ||
} | ||
|
||
const defaultAllowBuiltInDep = [ | ||
'assert', | ||
'buffer', | ||
'crypto', | ||
'events', | ||
'http', | ||
'https', | ||
'net', | ||
'path', | ||
'querystring', | ||
'timers', | ||
'tls', | ||
'url', | ||
'zlib' | ||
] | ||
|
||
const builtinDeps = process.env.TOOL_FUNCTION_BUILTIN_DEP | ||
? defaultAllowBuiltInDep.concat(process.env.TOOL_FUNCTION_BUILTIN_DEP.split(',')) | ||
: defaultAllowBuiltInDep | ||
const externalDeps = process.env.TOOL_FUNCTION_EXTERNAL_DEP ? process.env.TOOL_FUNCTION_EXTERNAL_DEP.split(',') : [] | ||
const deps = availableDependencies.concat(externalDeps) | ||
|
||
const nodeVMOptions = { | ||
console: 'inherit', | ||
sandbox, | ||
require: { | ||
external: { modules: deps }, | ||
builtin: builtinDeps | ||
} | ||
} as any | ||
|
||
const vm = new NodeVM(nodeVMOptions) | ||
try { | ||
const response = await vm.run(`module.exports = async function() {${javascriptFunction}}()`, __dirname) | ||
if (typeof response === 'string') { | ||
return handleEscapeCharacters(response, false) | ||
} | ||
return response | ||
} catch (e) { | ||
throw new Error(e) | ||
} | ||
} | ||
} | ||
|
||
module.exports = { nodeClass: CustomFunction_Utilities } |
1 change: 1 addition & 0 deletions
1
packages/components/nodes/utilities/CustomFunction/customfunction.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions
52
packages/components/nodes/utilities/GetVariable/GetVariable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' | ||
|
||
class GetVariable_Utilities implements INode { | ||
label: string | ||
name: string | ||
version: number | ||
description: string | ||
type: string | ||
icon: string | ||
category: string | ||
baseClasses: string[] | ||
inputs: INodeParams[] | ||
outputs: INodeOutputsValue[] | ||
|
||
constructor() { | ||
this.label = 'Get Variable' | ||
this.name = 'getVariable' | ||
this.version = 1.0 | ||
this.type = 'GetVariable' | ||
this.icon = 'getvar.svg' | ||
this.category = 'Utilities' | ||
this.description = `Get variable that was saved using Set Variable node` | ||
this.baseClasses = [this.type, 'Utilities'] | ||
this.inputs = [ | ||
{ | ||
label: 'Variable Name', | ||
name: 'variableName', | ||
type: 'string', | ||
placeholder: 'var1' | ||
} | ||
] | ||
this.outputs = [ | ||
{ | ||
label: 'Output', | ||
name: 'output', | ||
baseClasses: ['string', 'number', 'boolean', 'json', 'array'] | ||
} | ||
] | ||
} | ||
|
||
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { | ||
const variableName = nodeData.inputs?.variableName as string | ||
const dynamicVars = options.dynamicVariables as Record<string, unknown> | ||
|
||
if (Object.prototype.hasOwnProperty.call(dynamicVars, variableName)) { | ||
return dynamicVars[variableName] | ||
} | ||
return undefined | ||
} | ||
} | ||
|
||
module.exports = { nodeClass: GetVariable_Utilities } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions
56
packages/components/nodes/utilities/SetVariable/SetVariable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' | ||
|
||
class SetVariable_Utilities implements INode { | ||
label: string | ||
name: string | ||
version: number | ||
description: string | ||
type: string | ||
icon: string | ||
category: string | ||
baseClasses: string[] | ||
inputs: INodeParams[] | ||
outputs: INodeOutputsValue[] | ||
|
||
constructor() { | ||
this.label = 'Set Variable' | ||
this.name = 'setVariable' | ||
this.version = 1.0 | ||
this.type = 'SetVariable' | ||
this.icon = 'setvar.svg' | ||
this.category = 'Utilities' | ||
this.description = `Set variable which can be retrieved at a later stage. Variable is only available during runtime.` | ||
this.baseClasses = [this.type, 'Utilities'] | ||
this.inputs = [ | ||
{ | ||
label: 'Input', | ||
name: 'input', | ||
type: 'string | number | boolean | json | array', | ||
optional: true, | ||
list: true | ||
}, | ||
{ | ||
label: 'Variable Name', | ||
name: 'variableName', | ||
type: 'string', | ||
placeholder: 'var1' | ||
} | ||
] | ||
this.outputs = [ | ||
{ | ||
label: 'Output', | ||
name: 'output', | ||
baseClasses: ['string', 'number', 'boolean', 'json', 'array'] | ||
} | ||
] | ||
} | ||
|
||
async init(nodeData: INodeData): Promise<any> { | ||
const inputRaw = nodeData.inputs?.input | ||
const variableName = nodeData.inputs?.variableName as string | ||
|
||
return { output: inputRaw, dynamicVariables: { [variableName]: inputRaw } } | ||
} | ||
} | ||
|
||
module.exports = { nodeClass: SetVariable_Utilities } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.