( step: ToolCallBatchContext, pendingResult: PendingToolResult, )
| 486 | } |
| 487 | |
| 488 | async function finalizePendingToolResult( |
| 489 | step: ToolCallBatchContext, |
| 490 | pendingResult: PendingToolResult, |
| 491 | ): Promise<PendingToolResult> { |
| 492 | const { hooks, signal, turnId, currentStep, llm } = step; |
| 493 | if (hooks?.finalizeToolResult === undefined) { |
| 494 | return { ...pendingResult, result: normalizeToolResult(pendingResult.result) }; |
| 495 | } |
| 496 | |
| 497 | try { |
| 498 | const finalizedResult = await hooks.finalizeToolResult({ |
| 499 | toolCall: pendingResult.toolCall, |
| 500 | toolCalls: step.toolCalls, |
| 501 | args: pendingResult.args, |
| 502 | result: pendingResult.result, |
| 503 | turnId, |
| 504 | stepNumber: currentStep, |
| 505 | signal, |
| 506 | llm, |
| 507 | }); |
| 508 | const effectiveResult = coerceToolResult( |
| 509 | finalizedResult ?? pendingResult.result, |
| 510 | pendingResult.toolName, |
| 511 | ); |
| 512 | return { |
| 513 | ...pendingResult, |
| 514 | stopTurn: pendingResult.stopTurn === true || toolResultStopsTurn(effectiveResult), |
| 515 | result: normalizeToolResult(effectiveResult), |
| 516 | }; |
| 517 | } catch (error) { |
| 518 | // This is the redaction/truncation boundary. If it fails, do not persist |
| 519 | // the raw tool output; write an error result instead. |
| 520 | const aborted = isAbortError(error) || signal.aborted; |
| 521 | if (!aborted) { |
| 522 | step.log?.warn('finalizeToolResult hook failed', { |
| 523 | toolName: pendingResult.toolName, |
| 524 | toolCallId: pendingResult.toolCall.id, |
| 525 | error, |
| 526 | }); |
| 527 | } |
| 528 | const output = aborted |
| 529 | ? `Tool "${pendingResult.toolName}" aborted during finalizeToolResult hook.` |
| 530 | : `finalizeToolResult hook failed for "${pendingResult.toolName}": ${errorMessage(error)}`; |
| 531 | return { |
| 532 | ...pendingResult, |
| 533 | stopTurn: pendingResult.stopTurn, |
| 534 | result: { output, isError: true }, |
| 535 | }; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | async function executeTool( |
| 540 | step: ToolCallStepContext, |
no test coverage detected