* 调用工具
(request: ToolCallRequest)
| 147 | * 调用工具 |
| 148 | */ |
| 149 | public async callTool(request: ToolCallRequest): Promise<ToolCallResponse> { |
| 150 | const requestId = randomUUID(); |
| 151 | const startTime = Date.now(); |
| 152 | |
| 153 | try { |
| 154 | // 检查并发限制 |
| 155 | if (this.runningExecutions.size >= this.config.maxConcurrency) { |
| 156 | throw new ToolExecutionError('达到最大并发执行限制', request.toolName); |
| 157 | } |
| 158 | |
| 159 | // 获取工具 |
| 160 | const tool = this.tools.get(request.toolName); |
| 161 | if (!tool) { |
| 162 | throw new ToolExecutionError(`工具 "${request.toolName}" 不存在`, request.toolName); |
| 163 | } |
| 164 | |
| 165 | // 检查工具状态 |
| 166 | if (!this.isToolEnabled(request.toolName)) { |
| 167 | throw new ToolExecutionError(`工具 "${request.toolName}" 已禁用`, request.toolName); |
| 168 | } |
| 169 | |
| 170 | // 生成执行上下文 |
| 171 | const context: ToolExecutionContext = { |
| 172 | executionId: requestId, |
| 173 | timestamp: startTime, |
| 174 | ...request.context, |
| 175 | }; |
| 176 | |
| 177 | // 验证和处理参数 |
| 178 | let processedParams = ToolValidator.applyDefaults(request.parameters, tool.parameters); |
| 179 | |
| 180 | processedParams = ToolValidator.sanitizeParameters(processedParams, tool.parameters); |
| 181 | |
| 182 | ToolValidator.validateParameters(processedParams, tool.parameters, tool.required); |
| 183 | |
| 184 | this.log(`开始执行工具 "${request.toolName}"`, { |
| 185 | requestId, |
| 186 | parameters: processedParams, |
| 187 | }); |
| 188 | |
| 189 | this.emit('toolCallStarted', { |
| 190 | requestId, |
| 191 | toolName: request.toolName, |
| 192 | parameters: processedParams, |
| 193 | context, |
| 194 | }); |
| 195 | |
| 196 | // 执行工具 |
| 197 | const executionPromise = this.executeToolWithTimeout(tool, processedParams); |
| 198 | |
| 199 | this.runningExecutions.set(requestId, executionPromise); |
| 200 | |
| 201 | const result = await executionPromise; |
| 202 | |
| 203 | // 记录执行时间 |
| 204 | result.duration = Date.now() - startTime; |
| 205 | |
| 206 | this.log(`工具 "${request.toolName}" 执行完成`, { |
nothing calls this directly
no test coverage detected