UNPKG

@n8n/n8n-nodes-langchain

Version:

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

341 lines 13.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolHttpRequest = void 0; const tools_1 = require("@langchain/core/tools"); const n8n_workflow_1 = require("n8n-workflow"); const N8nTool_1 = require("../../../utils/N8nTool"); const ai_utilities_1 = require("@n8n/ai-utilities"); const descriptions_1 = require("./descriptions"); const utils_1 = require("./utils"); class ToolHttpRequest { constructor() { this.description = { displayName: 'HTTP Request Tool', name: 'toolHttpRequest', icon: { light: 'file:httprequest.svg', dark: 'file:httprequest.dark.svg' }, group: ['output'], version: [1, 1.1], description: 'Makes an HTTP request and returns the response data', subtitle: '={{ $parameter.toolDescription }}', defaults: { name: 'HTTP Request', }, credentials: [], codex: { categories: ['AI'], subcategories: { AI: ['Tools'], Tools: ['Recommended Tools'], }, resources: { primaryDocumentation: [ { url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolhttprequest/', }, ], }, }, hidden: true, inputs: [], outputs: [n8n_workflow_1.NodeConnectionTypes.AiTool], outputNames: ['Tool'], properties: [ (0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiAgent]), { displayName: 'Description', name: 'toolDescription', type: 'string', description: 'Explain to LLM what this tool does, better description would allow LLM to produce expected result', placeholder: 'e.g. Get the current weather in the requested city', default: '', typeOptions: { rows: 3, }, }, { displayName: 'Method', name: 'method', type: 'options', options: [ { name: 'DELETE', value: 'DELETE', }, { name: 'GET', value: 'GET', }, { name: 'PATCH', value: 'PATCH', }, { name: 'POST', value: 'POST', }, { name: 'PUT', value: 'PUT', }, ], default: 'GET', }, { displayName: 'Tip: You can use a {placeholder} for any part of the request to be filled by the model. Provide more context about them in the placeholders section', name: 'placeholderNotice', type: 'notice', default: '', }, { displayName: 'URL', name: 'url', type: 'string', default: '', required: true, placeholder: 'e.g. http://www.example.com/{path}', }, ...descriptions_1.authenticationProperties, { displayName: 'Send Query Parameters', name: 'sendQuery', type: 'boolean', default: false, noDataExpression: true, description: 'Whether the request has query params or not', }, { ...descriptions_1.specifyBySelector, displayName: 'Specify Query Parameters', name: 'specifyQuery', displayOptions: { show: { sendQuery: [true], }, }, }, { ...descriptions_1.parametersCollection, displayName: 'Query Parameters', name: 'parametersQuery', displayOptions: { show: { sendQuery: [true], specifyQuery: ['keypair'], }, }, }, { ...descriptions_1.jsonInput, name: 'jsonQuery', displayOptions: { show: { sendQuery: [true], specifyQuery: ['json'], }, }, }, { displayName: 'Send Headers', name: 'sendHeaders', type: 'boolean', default: false, noDataExpression: true, description: 'Whether the request has headers or not', }, { ...descriptions_1.specifyBySelector, displayName: 'Specify Headers', name: 'specifyHeaders', displayOptions: { show: { sendHeaders: [true], }, }, }, { ...descriptions_1.parametersCollection, displayName: 'Header Parameters', name: 'parametersHeaders', displayOptions: { show: { sendHeaders: [true], specifyHeaders: ['keypair'], }, }, }, { ...descriptions_1.jsonInput, name: 'jsonHeaders', displayOptions: { show: { sendHeaders: [true], specifyHeaders: ['json'], }, }, }, { displayName: 'Send Body', name: 'sendBody', type: 'boolean', default: false, noDataExpression: true, description: 'Whether the request has body or not', }, { ...descriptions_1.specifyBySelector, displayName: 'Specify Body', name: 'specifyBody', displayOptions: { show: { sendBody: [true], }, }, }, { ...descriptions_1.parametersCollection, displayName: 'Body Parameters', name: 'parametersBody', displayOptions: { show: { sendBody: [true], specifyBody: ['keypair'], }, }, }, { ...descriptions_1.jsonInput, name: 'jsonBody', displayOptions: { show: { sendBody: [true], specifyBody: ['json'], }, }, }, descriptions_1.placeholderDefinitionsCollection, ...descriptions_1.optimizeResponseProperties, ], }; } async supplyData(itemIndex) { const name = (0, n8n_workflow_1.nodeNameToToolName)(this.getNode()); try { (0, n8n_workflow_1.tryToParseAlphanumericString)(name); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'The name of this tool is not a valid alphanumeric string', { itemIndex, description: "Only alphanumeric characters and underscores are allowed in the tool's name, and the name cannot start with a number", }); } const toolDescription = this.getNodeParameter('toolDescription', itemIndex); const sendQuery = this.getNodeParameter('sendQuery', itemIndex, false); const sendHeaders = this.getNodeParameter('sendHeaders', itemIndex, false); const sendBody = this.getNodeParameter('sendBody', itemIndex, false); const requestOptions = { method: this.getNodeParameter('method', itemIndex, 'GET'), url: this.getNodeParameter('url', itemIndex), qs: {}, headers: { 'User-Agent': undefined, }, body: {}, returnFullResponse: true, }; const authentication = this.getNodeParameter('authentication', itemIndex, 'none'); if (authentication !== 'none') { const domain = new URL(requestOptions.url).hostname; if (domain.includes('{') && domain.includes('}')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), "Can't use a placeholder for the domain when using authentication", { itemIndex, description: 'This is for security reasons, to prevent the model accidentally sending your credentials to an unauthorized domain', }); } } const httpRequest = await (0, utils_1.configureHttpRequestFunction)(this, authentication, itemIndex); const optimizeResponse = (0, utils_1.configureResponseOptimizer)(this, itemIndex); const rawRequestOptions = { qs: '', headers: '', body: '', }; const placeholdersDefinitions = this.getNodeParameter('placeholderDefinitions.values', itemIndex, []).map((p) => { if (p.name.startsWith('{') && p.name.endsWith('}')) { p.name = p.name.slice(1, -1); } return p; }); const toolParameters = []; toolParameters.push(...(0, utils_1.extractParametersFromText)(placeholdersDefinitions, requestOptions.url, 'path')); if (sendQuery) { (0, utils_1.updateParametersAndOptions)({ ctx: this, itemIndex, toolParameters, placeholdersDefinitions, requestOptions, rawRequestOptions, requestOptionsProperty: 'qs', inputTypePropertyName: 'specifyQuery', jsonPropertyName: 'jsonQuery', parametersPropertyName: 'parametersQuery.values', }); } if (sendHeaders) { (0, utils_1.updateParametersAndOptions)({ ctx: this, itemIndex, toolParameters, placeholdersDefinitions, requestOptions, rawRequestOptions, requestOptionsProperty: 'headers', inputTypePropertyName: 'specifyHeaders', jsonPropertyName: 'jsonHeaders', parametersPropertyName: 'parametersHeaders.values', }); } if (sendBody) { (0, utils_1.updateParametersAndOptions)({ ctx: this, itemIndex, toolParameters, placeholdersDefinitions, requestOptions, rawRequestOptions, requestOptionsProperty: 'body', inputTypePropertyName: 'specifyBody', jsonPropertyName: 'jsonBody', parametersPropertyName: 'parametersBody.values', }); } for (const placeholder of placeholdersDefinitions) { if (!toolParameters.find((parameter) => parameter.name === placeholder.name)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Misconfigured placeholder '${placeholder.name}'`, { itemIndex, description: "This placeholder is defined in the 'Placeholder Definitions' but isn't used anywhere. Either remove the definition, or add the placeholder to a part of the request.", }); } } const func = (0, utils_1.configureToolFunction)(this, itemIndex, toolParameters, requestOptions, rawRequestOptions, httpRequest, optimizeResponse); let tool; if (this.getNode().typeVersion >= 1.1) { const schema = (0, utils_1.makeToolInputSchema)(toolParameters); tool = new N8nTool_1.N8nTool(this, { name, description: toolDescription, func, schema, }); } else { const description = (0, utils_1.prepareToolDescription)(toolDescription, toolParameters); tool = new tools_1.DynamicTool({ name, description, func }); } return { response: tool, }; } } exports.ToolHttpRequest = ToolHttpRequest; //# sourceMappingURL=ToolHttpRequest.node.js.map