feat: add onToolError hook for customizing tool error payloads befo…#1054
feat: add onToolError hook for customizing tool error payloads befo…#1054
onToolError hook for customizing tool error payloads befo…#1054Conversation
…re serialization
🦋 Changeset detectedLatest commit: 452ab68 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This comment has been minimized.
This comment has been minimized.
📝 WalkthroughWalkthroughThis PR adds an Changes
Sequence Diagram(s)sequenceDiagram
participant Agent as Agent
participant Tool as Tool
participant Hook as onToolError Hook
participant Output as Output Handler
Agent->>Tool: Execute tool(args)
Tool-->>Agent: throws error (originalError)
Agent->>Hook: Invoke with {tool, args, error, originalError, context}
alt Hook returns output override
Hook-->>Agent: { output: {...} }
Agent->>Output: Use override output
else Hook returns undefined
Hook-->>Agent: undefined
Agent->>Output: Use default serialized error
end
Output-->>Agent: Final output payload
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/core/src/agent/agent.ts (1)
5315-5368:⚠️ Potential issue | 🟠 MajorAvoid invoking tool
onEndtwice on error.
tool.hooks?.onEndis executed twice with identical arguments, which can double side effects (logging, cleanup, external calls). Please remove the duplicate invocation.Suggested fix
- await tool.hooks?.onEnd?.({ - tool, - args, - output: undefined, - error: voltAgentError, - options: executionOptions, - }); - - await tool.hooks?.onEnd?.({ - tool, - args, - output: undefined, - error: voltAgentError, - options: executionOptions, - }); + await tool.hooks?.onEnd?.({ + tool, + args, + output: undefined, + error: voltAgentError, + options: executionOptions, + });
🤖 Fix all issues with AI agents
In `@packages/core/src/agent/hooks/index.spec.ts`:
- Around line 341-395: The test currently creates a plain Error for toolError
but OnToolErrorHookArgs requires VoltAgentError; replace the plain Error with a
VoltAgentError instance (or wrap a native Error inside a VoltAgentError) and
pass that as the error in the onToolError call (you can keep a separate native
Error as originalError if you want to assert both). Update the toolError
declaration used in the expect/assert and the agent.hooks.onToolError invocation
(references: OnToolErrorHookArgs, toolError, onToolErrorSpy,
agent.hooks.onToolError) so the test uses the proper VoltAgentError type.
🧹 Nitpick comments (1)
packages/core/src/agent/agent.spec.ts (1)
984-1027: Remove unnecessaryas anycast when assigning toError.stack.
Error.stackis optional and writable in TypeScript's Error interface; the cast is not needed and violates type safety principles.♻️ Proposed fix
- (error as any).stack = "should-not-be-returned"; + error.stack = "should-not-be-returned";
…re serialization
PR Checklist
Please check if your PR fulfills the following requirements:
Bugs / Features
What is the current behavior?
What is the new behavior?
fixes (issue)
Notes for reviewers
Summary by cubic
Add onToolError hook to let agents customize or replace tool error payloads before they are serialized and sent to the model. This makes it easy to normalize, redact, or compact errors without changing tool code.
Written for commit 452ab68. Summary will update on new commits.
Summary by CodeRabbit
New Features
Documentation
Tests