UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

194 lines 8.61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolWorkflowV1 = void 0; const tools_1 = require("@langchain/core/tools"); const get_1 = __importDefault(require("lodash/get")); const isObject_1 = __importDefault(require("lodash/isObject")); const manual = __importStar(require("n8n-nodes-base/dist/nodes/Set/v2/manual.mode")); const n8n_workflow_1 = require("n8n-workflow"); const versionDescription_1 = require("./versionDescription"); const schemaParsing_1 = require("../../../../utils/schemaParsing"); class ToolWorkflowV1 { constructor(baseDescription) { this.description = { ...baseDescription, ...versionDescription_1.versionDescription, }; } async supplyData(itemIndex) { const workflowProxy = this.getWorkflowDataProxy(0); const name = this.getNodeParameter('name', itemIndex); const description = this.getNodeParameter('description', itemIndex); let subExecutionId; let subWorkflowId; const useSchema = this.getNodeParameter('specifyInputSchema', itemIndex); let tool = undefined; const runFunction = async (query, runManager) => { const source = this.getNodeParameter('source', itemIndex); const workflowInfo = {}; if (source === 'database') { const nodeVersion = this.getNode().typeVersion; if (nodeVersion <= 1.1) { workflowInfo.id = this.getNodeParameter('workflowId', itemIndex); } else { const { value } = this.getNodeParameter('workflowId', itemIndex, {}); workflowInfo.id = value; } subWorkflowId = workflowInfo.id; } else if (source === 'parameter') { const workflowJson = this.getNodeParameter('workflowJson', itemIndex); try { workflowInfo.code = JSON.parse(workflowJson); subWorkflowId = workflowProxy.$workflow.id; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The provided workflow is not valid JSON: "${error.message}"`, { itemIndex, }); } } const rawData = { query }; const workflowFieldsJson = this.getNodeParameter('fields.values', itemIndex, [], { rawExpressions: true, }); for (const entry of workflowFieldsJson) { if (entry.type === 'objectValue' && entry.objectValue.startsWith('=')) { rawData[entry.name] = entry.objectValue.replace(/^=+/, ''); } } const options = { include: 'all', }; const newItem = await manual.execute.call(this, { json: { query } }, itemIndex, options, rawData, this.getNode()); const items = [newItem]; let receivedData; try { receivedData = await this.executeWorkflow(workflowInfo, items, runManager?.getChild(), { parentExecution: { executionId: workflowProxy.$execution.id, workflowId: workflowProxy.$workflow.id, }, }); subExecutionId = receivedData.executionId; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error); } const response = (0, get_1.default)(receivedData, 'data[0][0].json'); if (response === undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'There was an error: "The workflow did not return a response"'); } return response; }; const toolHandler = async (query, runManager) => { const { index } = this.addInputData(n8n_workflow_1.NodeConnectionTypes.AiTool, [[{ json: { query } }]]); let response = ''; let executionError; try { response = await runFunction(query, runManager); } catch (error) { executionError = error; response = `There was an error: "${error.message}"`; } if (typeof response === 'number') { response = response.toString(); } if ((0, isObject_1.default)(response)) { response = JSON.stringify(response, null, 2); } if (typeof response !== 'string') { executionError = new n8n_workflow_1.NodeOperationError(this.getNode(), 'Wrong output type returned', { description: `The response property should be a string, but it is an ${typeof response}`, }); response = `There was an error: "${executionError.message}"`; } let metadata; if (subExecutionId && subWorkflowId) { metadata = { subExecution: { executionId: subExecutionId, workflowId: subWorkflowId, }, }; } if (executionError) { void this.addOutputData(n8n_workflow_1.NodeConnectionTypes.AiTool, index, executionError, metadata); } else { const json = (0, n8n_workflow_1.jsonParse)(response, { fallbackValue: { response } }); void this.addOutputData(n8n_workflow_1.NodeConnectionTypes.AiTool, index, [[{ json }]], metadata); } return response; }; const functionBase = { name, description, func: toolHandler, }; if (useSchema) { try { const jsonExample = this.getNodeParameter('jsonSchemaExample', itemIndex, ''); const inputSchema = this.getNodeParameter('inputSchema', itemIndex, ''); const schemaType = this.getNodeParameter('schemaType', itemIndex); const jsonSchema = schemaType === 'fromJson' ? (0, schemaParsing_1.generateSchemaFromExample)(jsonExample) : (0, n8n_workflow_1.jsonParse)(inputSchema); const zodSchema = (0, schemaParsing_1.convertJsonSchemaToZod)(jsonSchema); tool = new tools_1.DynamicStructuredTool({ schema: zodSchema, ...functionBase, }); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Error during parsing of JSON Schema. \n ' + error); } } else { tool = new tools_1.DynamicTool(functionBase); } return { response: tool, }; } } exports.ToolWorkflowV1 = ToolWorkflowV1; //# sourceMappingURL=ToolWorkflowV1.node.js.map