( params: ExecuteToolCallParams<T>, )
| 287 | AgentRuntimeScopedDeps |
| 288 | |
| 289 | export async function executeToolCall<T extends ToolName>( |
| 290 | params: ExecuteToolCallParams<T>, |
| 291 | ): Promise<void> { |
| 292 | const { |
| 293 | toolName, |
| 294 | input, |
| 295 | excludeToolFromMessageHistory = false, |
| 296 | fromHandleSteps = false, |
| 297 | |
| 298 | agentState, |
| 299 | agentTemplate, |
| 300 | logger, |
| 301 | previousToolCallFinished, |
| 302 | toolCalls, |
| 303 | toolCallsToAddToMessageHistory, |
| 304 | toolResults, |
| 305 | toolResultsToAddToMessageHistory, |
| 306 | userInputId, |
| 307 | |
| 308 | onCostCalculated, |
| 309 | onResponseChunk, |
| 310 | requestToolCall, |
| 311 | } = params |
| 312 | const toolCallId = params.toolCallId ?? generateCompactId() |
| 313 | |
| 314 | const toolCall: CodebuffToolCall<T> | ToolCallError = parseRawToolCall<T>({ |
| 315 | rawToolCall: { |
| 316 | toolName, |
| 317 | toolCallId, |
| 318 | input, |
| 319 | }, |
| 320 | }) |
| 321 | |
| 322 | // Filter out restricted tools - emit error instead of tool call/result |
| 323 | // This prevents the CLI from showing tool calls that the agent doesn't have permission to use |
| 324 | if ( |
| 325 | toolCall.toolName && |
| 326 | !agentTemplate.toolNames.includes(toolCall.toolName) && |
| 327 | !fromHandleSteps |
| 328 | ) { |
| 329 | // Emit an error event instead of tool call/result pair |
| 330 | // The stream parser will convert this to a user message for proper API compliance |
| 331 | onResponseChunk({ |
| 332 | type: 'error', |
| 333 | message: `Tool \`${toolName}\` is not currently available. Make sure to only use tools provided at the start of the conversation AND that you most recently have permission to use.`, |
| 334 | }) |
| 335 | return previousToolCallFinished |
| 336 | } |
| 337 | |
| 338 | if ('error' in toolCall) { |
| 339 | const formattedInput = formatValueForError(input) |
| 340 | onResponseChunk({ |
| 341 | type: 'error', |
| 342 | message: `${toolCall.error}\n\nOriginal tool call input:\n${formattedInput}`, |
| 343 | }) |
| 344 | logger.debug( |
| 345 | { toolCall, error: toolCall.error }, |
| 346 | `${toolName} error: ${toolCall.error}`, |
no test coverage detected