UNPKG

n8n-nodes-databricks-api

Version:
88 lines 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.N8nTool = exports.prepareFallbackToolDescription = void 0; const tools_1 = require("@langchain/core/tools"); const n8n_workflow_1 = require("n8n-workflow"); const zod_1 = require("zod"); const getSimplifiedType = (schema) => { if (schema instanceof zod_1.ZodObject) { return 'object'; } else if (schema instanceof zod_1.ZodNumber) { return 'number'; } else if (schema instanceof zod_1.ZodBoolean) { return 'boolean'; } else if (schema instanceof zod_1.ZodNullable || schema instanceof zod_1.ZodOptional) { return getSimplifiedType(schema.unwrap()); } return 'string'; }; const getParametersDescription = (parameters) => parameters .map(([name, schema]) => { var _a; return `${name}: (description: ${(_a = schema.description) !== null && _a !== void 0 ? _a : ''}, type: ${getSimplifiedType(schema)}, required: ${!schema.isOptional()})`; }) .join(',\n '); const prepareFallbackToolDescription = (toolDescription, schema) => { let description = `${toolDescription}`; const toolParameters = Object.entries(schema.shape); if (toolParameters.length) { description += ` Tool expects valid stringified JSON object with ${toolParameters.length} properties. Property names with description, type and required status: ${getParametersDescription(toolParameters)} ALL parameters marked as required must be provided`; } return description; }; exports.prepareFallbackToolDescription = prepareFallbackToolDescription; class N8nTool extends tools_1.DynamicStructuredTool { constructor(context, fields) { super(fields); this.context = context; } asDynamicTool() { const { name, func, schema, context, description } = this; if (!(schema instanceof zod_1.ZodObject)) { throw new Error('Schema must be a ZodObject'); } const zodSchema = schema; const wrappedFunc = async function (query) { let parsedQuery; let dataToValidate; try { dataToValidate = (0, n8n_workflow_1.jsonParse)(query, { acceptJSObject: true }); } catch (error) { if (Object.keys(zodSchema.shape).length === 1) { const parameterName = Object.keys(zodSchema.shape)[0]; dataToValidate = { [parameterName]: query }; } else { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Input is not a valid JSON: ${error.message}`); } } try { parsedQuery = zodSchema.parse(dataToValidate); } catch (error) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Input validation failed: ${error.message}`); } try { const result = await func(parsedQuery); return result; } catch (e) { const { index } = context.addInputData(n8n_workflow_1.NodeConnectionTypes.AiTool, [[{ json: { query } }]]); void context.addOutputData(n8n_workflow_1.NodeConnectionTypes.AiTool, index, e); return e.toString(); } }; return new tools_1.DynamicTool({ name, description: (0, exports.prepareFallbackToolDescription)(description, schema), func: wrappedFunc, }); } } exports.N8nTool = N8nTool; //# sourceMappingURL=N8nTool.js.map