|
| 1 | +import { |
| 2 | + ToolDefinition, |
| 3 | + ToolProvider, |
| 4 | + ToolProviderConstructor, |
| 5 | + ToolReference, |
| 6 | + ToolResult, |
| 7 | +} from "./tool-types"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tool call record for tracking function calls made during execution |
| 11 | + */ |
| 12 | +export interface ToolCall { |
| 13 | + name: string; |
| 14 | + arguments: any; |
| 15 | + result: any; |
| 16 | + timestamp: number; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Tool call tracker for a specific execution context |
| 21 | + */ |
| 22 | +export class ToolCallTracker { |
| 23 | + private toolCalls: ToolCall[] = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * Wrap tool definitions to track calls when executed |
| 27 | + */ |
| 28 | + public wrapToolDefinitions( |
| 29 | + toolDefinitions: ToolDefinition[] |
| 30 | + ): ToolDefinition[] { |
| 31 | + return toolDefinitions.map((toolDef) => ({ |
| 32 | + ...toolDef, |
| 33 | + function: this.wrapToolFunction(toolDef), |
| 34 | + })); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get all tool calls made during this execution |
| 39 | + */ |
| 40 | + public getToolCalls(): ToolCall[] { |
| 41 | + return [...this.toolCalls]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Clear all tracked tool calls |
| 46 | + */ |
| 47 | + public clearToolCalls(): void { |
| 48 | + this.toolCalls = []; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Wrap a tool function to track when it's called |
| 53 | + */ |
| 54 | + private wrapToolFunction( |
| 55 | + toolDef: ToolDefinition |
| 56 | + ): (args: any) => Promise<string> { |
| 57 | + return async (args: any): Promise<string> => { |
| 58 | + const startTime = Date.now(); |
| 59 | + |
| 60 | + try { |
| 61 | + const result = await toolDef.function(args); |
| 62 | + |
| 63 | + // Record the tool call |
| 64 | + this.toolCalls.push({ |
| 65 | + name: toolDef.name, |
| 66 | + arguments: args, |
| 67 | + result: result, |
| 68 | + timestamp: startTime, |
| 69 | + }); |
| 70 | + |
| 71 | + return result; |
| 72 | + } catch (error) { |
| 73 | + // Record failed tool calls too |
| 74 | + this.toolCalls.push({ |
| 75 | + name: toolDef.name, |
| 76 | + arguments: args, |
| 77 | + result: { |
| 78 | + error: error instanceof Error ? error.message : "Unknown error", |
| 79 | + }, |
| 80 | + timestamp: startTime, |
| 81 | + }); |
| 82 | + |
| 83 | + throw error; |
| 84 | + } |
| 85 | + }; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Base tool registry that manages different types of tool providers |
| 91 | + * and provides a unified interface for tool resolution and execution |
| 92 | + */ |
| 93 | +export abstract class BaseToolRegistry { |
| 94 | + protected providers: Map<string, ToolProvider> = new Map(); |
| 95 | + |
| 96 | + /** |
| 97 | + * Register a tool provider for a specific tool type |
| 98 | + */ |
| 99 | + public registerProvider(type: string, provider: ToolProvider): void { |
| 100 | + this.providers.set(type, provider); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Register a tool provider constructor that will be instantiated |
| 105 | + */ |
| 106 | + public registerProviderConstructor( |
| 107 | + type: string, |
| 108 | + ProviderConstructor: ToolProviderConstructor, |
| 109 | + ...args: any[] |
| 110 | + ): void { |
| 111 | + const provider = new ProviderConstructor(...args); |
| 112 | + this.registerProvider(type, provider); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get tool definition for a tool reference |
| 117 | + */ |
| 118 | + public async getToolDefinition( |
| 119 | + toolRef: ToolReference |
| 120 | + ): Promise<ToolDefinition> { |
| 121 | + const provider = this.providers.get(toolRef.type); |
| 122 | + if (!provider) { |
| 123 | + throw new Error(`No provider registered for tool type: ${toolRef.type}`); |
| 124 | + } |
| 125 | + return provider.getToolDefinition(toolRef.identifier); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Execute a tool with given parameters |
| 130 | + */ |
| 131 | + public async executeTool( |
| 132 | + toolRef: ToolReference, |
| 133 | + parameters: any |
| 134 | + ): Promise<ToolResult> { |
| 135 | + const provider = this.providers.get(toolRef.type); |
| 136 | + if (!provider) { |
| 137 | + throw new Error(`No provider registered for tool type: ${toolRef.type}`); |
| 138 | + } |
| 139 | + return provider.executeTool(toolRef.identifier, parameters); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get all tool definitions for multiple tool references |
| 144 | + */ |
| 145 | + public async getToolDefinitions( |
| 146 | + toolRefs: ToolReference[] |
| 147 | + ): Promise<ToolDefinition[]> { |
| 148 | + return Promise.all( |
| 149 | + toolRefs.map((toolRef) => this.getToolDefinition(toolRef)) |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Execute multiple tools with their respective parameters |
| 155 | + */ |
| 156 | + public async executeTools( |
| 157 | + executions: Array<{ toolRef: ToolReference; parameters: any }> |
| 158 | + ): Promise<ToolResult[]> { |
| 159 | + return Promise.all( |
| 160 | + executions.map(({ toolRef, parameters }) => |
| 161 | + this.executeTool(toolRef, parameters) |
| 162 | + ) |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Check if a tool type is supported |
| 168 | + */ |
| 169 | + public hasProvider(type: string): boolean { |
| 170 | + return this.providers.has(type); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Get all registered tool types |
| 175 | + */ |
| 176 | + public getRegisteredTypes(): string[] { |
| 177 | + return Array.from(this.providers.keys()); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Get all available tools from all providers (if they support listing) |
| 182 | + */ |
| 183 | + public async getAllAvailableTools(): Promise< |
| 184 | + Array<{ type: string; tools: ToolDefinition[] }> |
| 185 | + > { |
| 186 | + const results: Array<{ type: string; tools: ToolDefinition[] }> = []; |
| 187 | + |
| 188 | + for (const [type, provider] of this.providers.entries()) { |
| 189 | + if (provider.listTools) { |
| 190 | + try { |
| 191 | + const tools = await provider.listTools(); |
| 192 | + results.push({ type, tools }); |
| 193 | + } catch (error) { |
| 194 | + console.warn(`Failed to list tools for provider ${type}:`, error); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return results; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Abstract method for subclasses to initialize their providers |
| 204 | + */ |
| 205 | + protected abstract initializeProviders(): void; |
| 206 | +} |
0 commit comments