diff options
| author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-10-17 10:41:49 -0400 |
|---|---|---|
| committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-10-17 10:41:49 -0400 |
| commit | 80d86bd5ae3e1d3dc70e7636f72a872a5fb2f01d (patch) | |
| tree | 0eaea49f596bd16720f05a6535958ab8270673c8 /src/client/views/nodes/chatbot/tools/BaseTool.ts | |
| parent | 596502c232ea6b6b88c3c58486e139074ea056ff (diff) | |
Implemented strict typechecking for tools, specifically tool inputs
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/BaseTool.ts')
| -rw-r--r-- | src/client/views/nodes/chatbot/tools/BaseTool.ts | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts index 4f8f81bcb..e01296ac4 100644 --- a/src/client/views/nodes/chatbot/tools/BaseTool.ts +++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts @@ -1,58 +1,76 @@ -import { Tool, ParameterArray, ParametersType, Observation, ParamConfig } from '../types/types'; +import { Tool, Parameter, ParametersType, Observation } from '../types/types'; /** * @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. */ -export abstract class BaseTool implements Tool<ParamConfig[]> { +/** + * 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). + */ +export abstract class BaseTool<P extends readonly Parameter[]> implements Tool<P> { + // The name of the tool (e.g., "calculate", "searchTool") name: string; + // A description of the tool's functionality description: string; - parameterRules: ParameterArray; // Still using ParameterArray + // 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; - paramConfig: ParamConfig[]; - constructor( - name: string, - description: string, - parameterRules: ParameterArray, // Allow tuple types for parameters - citationRules: string, - 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 - An array of parameter definitions (`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; - this.paramConfig = this.parameterRules.map(param => ({ - name: param.name, - type: param.type, - })); // Convert ParameterArray to ParamConfig[] } - // Abstract execute method with dynamic types based on parameters - abstract execute(args: ParametersType<ParamConfig[]>): Promise<Observation[]>; + /** + * 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[]>; - // Implement the getActionRule method + /** + * 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 { 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, - max_inputs: param.max_inputs, - }; + // 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<ParameterArray[number], 'name'>> + {} as Record<string, Omit<P[number], 'name'>> // Initialize the accumulator as an empty object ), }; } |
