From edc6f9162d09313a1d9ee3e2d5c2dac7a6567543 Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 20 May 2023 13:18:53 +0100 Subject: [PATCH] add headers and maxlength to requests tools --- .../nodes/tools/RequestsGet/RequestsGet.ts | 34 +++++++++++++++++-- .../nodes/tools/RequestsPost/RequestsPost.ts | 34 +++++++++++++++++-- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/packages/components/nodes/tools/RequestsGet/RequestsGet.ts b/packages/components/nodes/tools/RequestsGet/RequestsGet.ts index 4df64720..2085187b 100644 --- a/packages/components/nodes/tools/RequestsGet/RequestsGet.ts +++ b/packages/components/nodes/tools/RequestsGet/RequestsGet.ts @@ -1,4 +1,4 @@ -import { INode } from '../../../src/Interface' +import { INode, INodeData, INodeParams } from '../../../src/Interface' import { getBaseClasses } from '../../../src/utils' import { RequestsGetTool } from 'langchain/tools' @@ -10,6 +10,7 @@ class RequestsGet_Tools implements INode { icon: string category: string baseClasses: string[] + inputs: INodeParams[] constructor() { this.label = 'Requests Get' @@ -19,10 +20,37 @@ class RequestsGet_Tools implements INode { this.category = 'Tools' this.description = 'Execute HTTP GET requests' this.baseClasses = [this.type, ...getBaseClasses(RequestsGetTool)] + this.inputs = [ + { + label: 'Max Output Length', + name: 'maxOutputLength', + type: 'number', + optional: true + }, + { + label: 'Headers', + name: 'headers', + type: 'json', + additionalParams: true, + optional: true + } + ] } - async init(): Promise { - return new RequestsGetTool() + async init(nodeData: INodeData): Promise { + const headers = nodeData.inputs?.headers as string + const maxOutputLength = nodeData.inputs?.maxOutputLength as string + + const obj: any = {} + if (maxOutputLength) { + obj.maxOutputLength = parseInt(maxOutputLength, 10) + } + + if (headers) { + const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(headers) + return Object.keys(obj).length ? new RequestsGetTool(parsedHeaders, obj) : new RequestsGetTool(parsedHeaders) + } + return Object.keys(obj).length ? new RequestsGetTool(undefined, obj) : new RequestsGetTool() } } diff --git a/packages/components/nodes/tools/RequestsPost/RequestsPost.ts b/packages/components/nodes/tools/RequestsPost/RequestsPost.ts index 6efaf208..f0535c97 100644 --- a/packages/components/nodes/tools/RequestsPost/RequestsPost.ts +++ b/packages/components/nodes/tools/RequestsPost/RequestsPost.ts @@ -1,4 +1,4 @@ -import { INode } from '../../../src/Interface' +import { INode, INodeData, INodeParams } from '../../../src/Interface' import { getBaseClasses } from '../../../src/utils' import { RequestsPostTool } from 'langchain/tools' @@ -10,6 +10,7 @@ class RequestsPost_Tools implements INode { icon: string category: string baseClasses: string[] + inputs: INodeParams[] constructor() { this.label = 'Requests Post' @@ -19,10 +20,37 @@ class RequestsPost_Tools implements INode { this.category = 'Tools' this.description = 'Execute HTTP POST requests' this.baseClasses = [this.type, ...getBaseClasses(RequestsPostTool)] + this.inputs = [ + { + label: 'Max Output Length', + name: 'maxOutputLength', + type: 'number', + optional: true + }, + { + label: 'Headers', + name: 'headers', + type: 'json', + additionalParams: true, + optional: true + } + ] } - async init(): Promise { - return new RequestsPostTool() + async init(nodeData: INodeData): Promise { + const headers = nodeData.inputs?.headers as string + const maxOutputLength = nodeData.inputs?.maxOutputLength as string + + const obj: any = {} + if (maxOutputLength) { + obj.maxOutputLength = parseInt(maxOutputLength, 10) + } + + if (headers) { + const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(headers) + return Object.keys(obj).length ? new RequestsPostTool(parsedHeaders, obj) : new RequestsPostTool(parsedHeaders) + } + return Object.keys(obj).length ? new RequestsPostTool(undefined, obj) : new RequestsPostTool() } }