Files
gemini-cli/packages/server/src/tools/tool-registry.ts
T

54 lines
1.3 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
2025-04-21 13:29:36 -07:00
import { FunctionDeclaration } from '@google/genai';
2025-04-17 17:25:01 -04:00
import { Tool } from './tools.js';
2025-04-15 21:41:08 -07:00
export class ToolRegistry {
2025-04-17 18:06:21 -04:00
private tools: Map<string, Tool> = new Map();
2025-04-15 21:41:08 -07:00
2025-04-17 18:06:21 -04:00
/**
* 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.`,
);
2025-04-15 21:41:08 -07:00
}
2025-04-17 18:06:21 -04:00
this.tools.set(tool.name, tool);
}
2025-04-15 21:41:08 -07:00
2025-04-17 18:06:21 -04:00
/**
* Retrieves the list of tool schemas (FunctionDeclaration array).
* Extracts the declarations from the ToolListUnion structure.
* @returns An array of FunctionDeclarations.
2025-04-17 18:06:21 -04:00
*/
getFunctionDeclarations(): FunctionDeclaration[] {
2025-04-17 18:06:21 -04:00
const declarations: FunctionDeclaration[] = [];
this.tools.forEach((tool) => {
declarations.push(tool.schema);
});
return declarations;
}
2025-04-15 21:41:08 -07:00
/**
* Returns an array of all registered tool instances.
*/
getAllTools(): Tool[] {
return Array.from(this.tools.values());
}
2025-04-17 18:06:21 -04:00
/**
* Get the definition of a specific tool.
*/
getTool(name: string): Tool | undefined {
return this.tools.get(name);
}
2025-04-15 21:41:08 -07:00
}