aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/BaseTool.ts
diff options
context:
space:
mode:
authoreleanor-park <eleanor_park@brown.edu>2024-10-30 19:39:46 -0400
committereleanor-park <eleanor_park@brown.edu>2024-10-30 19:39:46 -0400
commitc11c760db62f78a07b624b98b209e6ee86036c8e (patch)
treec9587b50042a5115373e91ba8ecf9b76913cd321 /src/client/views/nodes/chatbot/tools/BaseTool.ts
parentb5944e87f9d4f3149161de4de0d76db486461c76 (diff)
parent4c768162e0436115a05b9c8b0e4d837d626d45ba (diff)
Merge branch 'master' into eleanor-gptdraw
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/BaseTool.ts')
-rw-r--r--src/client/views/nodes/chatbot/tools/BaseTool.ts82
1 files changed, 64 insertions, 18 deletions
diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts
index a77f567a5..58cd514d9 100644
--- a/src/client/views/nodes/chatbot/tools/BaseTool.ts
+++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts
@@ -1,32 +1,78 @@
+import { Observation } from '../types/types';
+import { Parameter, Tool, ParametersType } from './ToolTypes';
+
/**
* @file BaseTool.ts
- * @description This file defines the abstract BaseTool class, which serves as a blueprint
+ * @description This file defines the abstract `BaseTool` class, which serves as a blueprint
* for tool implementations in the AI assistant system. Each tool has a name, description,
- * parameters, and citation rules. The BaseTool class provides a structure for executing actions
+ * parameters, and citation rules. The `BaseTool` class provides a structure for executing actions
* and retrieving action rules for use within the assistant's workflow.
*/
-import { Tool } 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 `ReadonlyArray<Parameter>`.
+ * This means `P` is a readonly array of `Parameter` objects that cannot be modified (immutable).
+ */
+export abstract class BaseTool<P extends ReadonlyArray<Parameter>> implements Tool<P> {
+ // The name of the tool (e.g., "calculate", "searchTool")
+ name: string;
+ // A description of the tool's functionality
+ description: string;
+ // An array of parameter definitions for the tool
+ parameterRules: P;
+ // Guidelines for how to handle citations when using the tool
+ citationRules: string;
+ // A brief summary of the tool's purpose
+ briefSummary: string;
-export abstract class BaseTool<T extends Record<string, unknown> = Record<string, unknown>> implements Tool<T> {
- constructor(
- public name: string,
- public description: string,
- public parameters: Record<string, unknown>,
- public citationRules: string,
- public briefSummary: string
- ) {}
+ /**
+ * Constructs a new `BaseTool` instance.
+ * @param name - The name of the tool.
+ * @param description - A detailed description of what the tool does.
+ * @param parameterRules - A readonly array of parameter definitions (`ReadonlyArray<Parameter>`).
+ * @param citationRules - Rules or guidelines for citations.
+ * @param briefSummary - A short summary of the tool.
+ */
+ constructor(name: string, description: string, parameterRules: P, citationRules: string, briefSummary: string) {
+ this.name = name;
+ this.description = description;
+ this.parameterRules = parameterRules;
+ this.citationRules = citationRules;
+ this.briefSummary = briefSummary;
+ }
- abstract execute(args: T): Promise<unknown>;
+ /**
+ * The `execute` method is abstract and must be implemented by subclasses.
+ * It defines the action the tool performs when executed.
+ * @param args - The arguments for the tool's execution, whose types are inferred from `ParametersType<P>`.
+ * @returns A promise that resolves to an array of `Observation` objects.
+ */
+ abstract execute(args: ParametersType<P>): Promise<Observation[]>;
+ /**
+ * Generates an action rule object that describes the tool's usage.
+ * This is useful for dynamically generating documentation or for tools that need to expose their parameters at runtime.
+ * @returns An object containing the tool's name, description, and parameter definitions.
+ */
getActionRule(): Record<string, unknown> {
return {
- [this.name]: {
- name: this.name,
- citationRules: this.citationRules,
- description: this.description,
- parameters: this.parameters,
- },
+ tool: this.name,
+ description: this.description,
+ parameters: this.parameterRules.reduce(
+ (acc, param) => {
+ // Build an object for each parameter without the 'name' property, since it's used as the key
+ acc[param.name] = {
+ type: param.type,
+ description: param.description,
+ required: param.required,
+ // Conditionally include 'max_inputs' only if it is defined
+ ...(param.max_inputs !== undefined && { max_inputs: param.max_inputs }),
+ } as Omit<P[number], 'name'>; // Type assertion to exclude the 'name' property
+ return acc;
+ },
+ {} as Record<string, Omit<P[number], 'name'>> // Initialize the accumulator as an empty object
+ ),
};
}
}