aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/BaseTool.ts
blob: 8800e223883fca757fb77bc5b9acb72de775a4b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Observation } from '../types/types';
import { Parameter, ParametersType, ToolInfo } from '../types/tool_types';

/**
 * @file BaseTool.ts
 * @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
 * and retrieving action rules for use within the assistant's workflow.
 */

/**
 * 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>> {
    // 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;

    /**
     * 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.
     */
    constructor(toolInfo: ToolInfo<P>) {
        this.name = toolInfo.name;
        this.description = toolInfo.description;
        this.parameterRules = toolInfo.parameterRules;
        this.citationRules = toolInfo.citationRules;
    }

    /**
     * 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[]>;

    /**
     * This is a hacky way for a tool to ignore required parameter errors.
     * Used by crateDocTool to allow processing of simple arrays of Documents
     * where the array doesn't conform to a normal Doc structure.
     * @param inputParam
     * @returns
     */
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    inputValidator(inputParam: ParametersType<readonly Parameter[]>) {
        return false;
    }

    /**
     * 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,
            citationRules: this.citationRules,
            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
            ),
        };
    }
}