mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat: Add AI tool building capabilities (#7336)
Github issue / Community forum post (link here to close automatically): https://community.n8n.io/t/langchain-memory-chat/23733 --------- Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Val <68596159+valya@users.noreply.github.com> Co-authored-by: Alex Grozav <alex@grozav.com> Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in> Co-authored-by: Deborah <deborah@starfallprojects.co.uk> Co-authored-by: Jesper Bylund <mail@jesperbylund.com> Co-authored-by: Jon <jonathan.bennetts@gmail.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: Giulio Andreini <andreini@netseven.it> Co-authored-by: Mason Geloso <Mason.geloso@gmail.com> Co-authored-by: Mason Geloso <hone@Masons-Mac-mini.local> Co-authored-by: Mutasem Aldmour <mutasem@n8n.io>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { NodeVM, makeResolverFromLegacyOptions } from '@n8n/vm2';
|
||||
import { NodeVM, makeResolverFromLegacyOptions, type Resolver } from '@n8n/vm2';
|
||||
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
|
||||
import { ValidationError } from './ValidationError';
|
||||
@@ -27,6 +27,7 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
private jsCode: string,
|
||||
itemIndex: number | undefined,
|
||||
helpers: IExecuteFunctions['helpers'],
|
||||
options?: { resolver?: Resolver },
|
||||
) {
|
||||
super(
|
||||
{
|
||||
@@ -41,17 +42,29 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
this.vm = new NodeVM({
|
||||
console: 'redirect',
|
||||
sandbox: context,
|
||||
require: vmResolver,
|
||||
require: options?.resolver ?? vmResolver,
|
||||
wasm: false,
|
||||
});
|
||||
|
||||
this.vm.on('console.log', (...args: unknown[]) => this.emit('output', ...args));
|
||||
}
|
||||
|
||||
async runCodeAllItems(): Promise<INodeExecutionData[]> {
|
||||
async runCode(): Promise<unknown> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
try {
|
||||
const executionResult = await this.vm.run(script, __dirname);
|
||||
return executionResult;
|
||||
} catch (error) {
|
||||
throw new ExecutionError(error);
|
||||
}
|
||||
}
|
||||
|
||||
async runCodeAllItems(options?: {
|
||||
multiOutput?: boolean;
|
||||
}): Promise<INodeExecutionData[] | INodeExecutionData[][]> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
|
||||
let executionResult: INodeExecutionData | INodeExecutionData[];
|
||||
let executionResult: INodeExecutionData | INodeExecutionData[] | INodeExecutionData[][];
|
||||
|
||||
try {
|
||||
executionResult = await this.vm.run(script, __dirname);
|
||||
@@ -67,7 +80,25 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
|
||||
if (executionResult === null) return [];
|
||||
|
||||
return this.validateRunCodeAllItems(executionResult);
|
||||
if (options?.multiOutput === true) {
|
||||
// Check if executionResult is an array of arrays
|
||||
if (!Array.isArray(executionResult) || executionResult.some((item) => !Array.isArray(item))) {
|
||||
throw new ValidationError({
|
||||
message: "The code doesn't return an array of arrays",
|
||||
description:
|
||||
'Please return an array of arrays. One array for the different outputs and one for the different items that get returned.',
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
return executionResult.map((data) => {
|
||||
return this.validateRunCodeAllItems(data);
|
||||
});
|
||||
}
|
||||
|
||||
return this.validateRunCodeAllItems(
|
||||
executionResult as INodeExecutionData | INodeExecutionData[],
|
||||
);
|
||||
}
|
||||
|
||||
async runCodeEachItem(): Promise<INodeExecutionData | undefined> {
|
||||
|
||||
Reference in New Issue
Block a user