From 14f412611299fc350f13b6f96be913d59533cfb3 Mon Sep 17 00:00:00 2001 From: "A.J. Shulman" Date: Thu, 17 Oct 2024 11:14:51 -0400 Subject: Removed awaits inside loops and made Parameters readonly for better type safety --- src/client/views/nodes/chatbot/tools/BaseTool.ts | 11 ++++++----- src/client/views/nodes/chatbot/tools/SearchTool.ts | 22 +++++++++++++--------- src/client/views/nodes/chatbot/tools/ToolTypes.ts | 16 ++++++++-------- .../nodes/chatbot/tools/WebsiteInfoScraperTool.ts | 17 ++++++++++------- 4 files changed, 37 insertions(+), 29 deletions(-) (limited to 'src/client/views/nodes/chatbot/tools') diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts index e01296ac4..58cd514d9 100644 --- a/src/client/views/nodes/chatbot/tools/BaseTool.ts +++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts @@ -1,4 +1,5 @@ -import { Tool, Parameter, ParametersType, Observation } from '../types/types'; +import { Observation } from '../types/types'; +import { Parameter, Tool, ParametersType } from './ToolTypes'; /** * @file BaseTool.ts @@ -10,10 +11,10 @@ import { Tool, Parameter, ParametersType, Observation } from '../types/types'; /** * The `BaseTool` class is an abstract class that implements the `Tool` interface. - * It is generic over a type parameter `P`, which extends `readonly Parameter[]`. - * This means `P` is an array of `Parameter` objects that cannot be modified (immutable). + * It is generic over a type parameter `P`, which extends `ReadonlyArray`. + * This means `P` is a readonly array of `Parameter` objects that cannot be modified (immutable). */ -export abstract class BaseTool

implements Tool

{ +export abstract class BaseTool

> implements Tool

{ // The name of the tool (e.g., "calculate", "searchTool") name: string; // A description of the tool's functionality @@ -29,7 +30,7 @@ export abstract class BaseTool

implements Tool

`). * @param citationRules - Rules or guidelines for citations. * @param briefSummary - A short summary of the tool. */ diff --git a/src/client/views/nodes/chatbot/tools/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts index c5cf951e7..fd5144dd6 100644 --- a/src/client/views/nodes/chatbot/tools/SearchTool.ts +++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts @@ -34,9 +34,9 @@ export class SearchTool extends BaseTool { async execute(args: ParametersType): Promise { const queries = args.query; - const allResults: Observation[] = []; - for (const query of queries) { + // Create an array of promises, each one handling a search for a query + const searchPromises = queries.map(async query => { try { const { results } = await Networking.PostToServer('/getWebSearchResults', { query, @@ -49,16 +49,20 @@ export class SearchTool extends BaseTool { text: `${result.url}${result.snippet}`, }; }); - allResults.push(...data); + return data; } catch (error) { console.log(error); - allResults.push({ - type: 'text', - text: `An error occurred while performing the web search for query: ${query}`, - }); + return [ + { + type: 'text', + text: `An error occurred while performing the web search for query: ${query}`, + }, + ]; } - } + }); + + const allResultsArrays = await Promise.all(searchPromises); - return allResults; + return allResultsArrays.flat(); } } diff --git a/src/client/views/nodes/chatbot/tools/ToolTypes.ts b/src/client/views/nodes/chatbot/tools/ToolTypes.ts index 74a92bcf2..d47a38952 100644 --- a/src/client/views/nodes/chatbot/tools/ToolTypes.ts +++ b/src/client/views/nodes/chatbot/tools/ToolTypes.ts @@ -2,10 +2,10 @@ import { Observation } from '../types/types'; /** * The `Tool` interface represents a generic tool in the system. - * It is generic over a type parameter `P`, which extends `readonly Parameter[]`. + * It is generic over a type parameter `P`, which extends `ReadonlyArray`. * @template P - An array of `Parameter` objects defining the tool's parameters. */ -export interface Tool

{ +export interface Tool

> { // The name of the tool (e.g., "calculate", "searchTool") name: string; // A description of the tool's functionality @@ -34,15 +34,15 @@ export interface Tool

{ */ export type Parameter = { // The type of the parameter; constrained to the types 'string', 'number', 'boolean', 'string[]', 'number[]' - type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; + readonly type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; // The name of the parameter - name: string; + readonly name: string; // A description of the parameter - description: string; + readonly description: string; // Indicates whether the parameter is required - required: boolean; + readonly required: boolean; // (Optional) The maximum number of inputs (useful for array types) - max_inputs?: number; + readonly max_inputs?: number; }; /** @@ -71,6 +71,6 @@ export type ParamType

= P['type'] extends keyof TypeMap ? T * This is used to define the types of the arguments passed to the `execute` method of a tool. * @template P - An array of `Parameter` objects. */ -export type ParametersType

= { +export type ParametersType

> = { [K in P[number] as K['name']]: ParamType; }; diff --git a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts index e91ebdad1..f2e3863a6 100644 --- a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts +++ b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts @@ -72,25 +72,28 @@ export class WebsiteInfoScraperTool extends BaseTool): Promise { const urls = args.urls; - const results: Observation[] = []; - for (const url of urls) { + // Create an array of promises, each one handling a website scrape for a URL + const scrapingPromises = urls.map(async url => { try { const { website_plain_text } = await Networking.PostToServer('/scrapeWebsite', { url }); const id = uuidv4(); this._addLinkedUrlDoc(url, id); - results.push({ + return { type: 'text', text: `\n${website_plain_text}\n`, - }); + } as Observation; } catch (error) { console.log(error); - results.push({ + return { type: 'text', text: `An error occurred while scraping the website: ${url}`, - }); + } as Observation; } - } + }); + + // Wait for all scraping promises to resolve + const results = await Promise.all(scrapingPromises); return results; } -- cgit v1.2.3-70-g09d2