summaryrefslogtreecommitdiff
path: root/packages/cli/src/tools/tool-registry.ts
blob: f5d661e6bd036803209a1c8e00872fcb36c48f8a (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
import { ToolListUnion, FunctionDeclaration } from '@google/genai';
import { Tool } from './Tool.js';
import { ToolResult } from './ToolResult.js';

class ToolRegistry {
    private tools: Map<string, Tool> = new Map();

    /**
     * Registers a tool definition.
     * @param tool - The tool object containing schema and execution logic.
     */
    registerTool(tool: Tool): void {
        if (this.tools.has(tool.name)) {
            // Decide on behavior: throw error, log warning, or allow overwrite
            console.warn(`Tool with name "${tool.name}" is already registered. Overwriting.`);
        }
        this.tools.set(tool.name, tool);
    }

    /**
     * Retrieves the list of tool schemas in the format required by Gemini.
     * @returns A ToolListUnion containing the function declarations.
     */
    getToolSchemas(): ToolListUnion {
        const declarations: FunctionDeclaration[] = [];
        this.tools.forEach(tool => {
            declarations.push(tool.schema);
        });

        // Return Gemini's expected format. Handle the case of no tools.
        if (declarations.length === 0) {
            // Depending on the SDK version, you might need `undefined`, `[]`, or `[{ functionDeclarations: [] }]`
            // Check the documentation for your @google/genai version.
            // Let's assume an empty array works or signifies no tools.
            return [];
            // Or if it requires the structure:
            // return [{ functionDeclarations: [] }];
        }
        return [{ functionDeclarations: declarations }];
    }

    /**
     * Optional: Get a list of registered tool names.
     */
    listAvailableTools(): string[] {
        return Array.from(this.tools.keys());
    }

    /**
     * Get the definition of a specific tool.
     */
    getTool(name: string): Tool | undefined {
        return this.tools.get(name);
    }
}

// Export a singleton instance of the registry
export const toolRegistry = new ToolRegistry();