feat(plugin): add tool.execute.error hook#10028
Open
kynnyhsap wants to merge 3 commits intoanomalyco:devfrom
Open
feat(plugin): add tool.execute.error hook#10028kynnyhsap wants to merge 3 commits intoanomalyco:devfrom
kynnyhsap wants to merge 3 commits intoanomalyco:devfrom
Conversation
Contributor
|
The following comment was made by an LLM, it may be inaccurate: The search results only show PR #10028 (the current PR itself) and other unrelated PRs about bash tool schema, memory leaks, Prometheus metrics, and shell resolution. No duplicate PRs found |
Contributor
Author
Manual TestingCreate a test plugin at import { type Plugin, tool } from "@opencode-ai/plugin"
export const TestErrorHookPlugin: Plugin = async (ctx) => {
return {
// Tool that always fails
tool: {
failing_tool: tool({
description: "A tool that always throws an error for testing",
args: {
message: tool.schema.string().describe("Error message to throw"),
},
async execute(args) {
throw new Error(`Intentional error: ${args.message}`)
},
}),
},
// Error hook to catch and handle errors
"tool.execute.error": async (input, output) => {
// Test 1: Modify the error message
if (input.tool === "failing_tool" && input.args.message === "modify-me") {
output.error = new Error("Modified error: The original error was intercepted and changed!")
}
// Test 2: Recover from error by returning a result
if (input.tool === "failing_tool" && input.args.message === "recover-me") {
output.result = {
title: "Recovered from error",
output: "The error was caught and handled gracefully by the plugin!",
metadata: { recovered: true },
}
}
},
}
}Test Prompts
|
8618735 to
4f38092
Compare
Add a new plugin hook that is triggered when a tool execution throws an error. This allows plugins to: - Log/track tool errors - Modify the error message before it's thrown - Recover from errors by providing a fallback result The hook receives the tool name, session ID, call ID, args, and a mutable output object containing the error. Plugins can set output.result to return a successful result instead of throwing the error.
4f38092 to
27fbad7
Compare
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a new plugin hook that fires when tool execution throws an error, allowing plugins to log, modify, or recover from errors.
Fixes #10027
What Changed
Added
tool.execute.errorhook to the plugin system. The hook receives:input: tool name, session ID, call ID, and argsoutput: mutable object witherrorand optionalresultPlugins can either modify the error or provide a fallback result.
Example Usage
How I Verified