Repository navigation
feat(plugin): let http.request hooks answer a request - #67635
nv-nedelman-1 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
The change works as described. I set a 429 as event.response from an http.request hook and streamed through the real LLM client. Nothing was sent over the network, and the stream failed with a RateLimit AI.Error, the same as a real provider 429. That is what lets the retry hook fall back to another provider, as #65198 asks. The hook runs inside the HTTP layer, before status codes are turned into errors, so a local answer gets the same error handling and x-should-retry checks as a real response. session-model-request-hooks.test.ts passes, and tsc --noEmit is clean for packages/core and packages/plugin.
Public API: it adds an optional response?: Response to SessionHttpRequest (http.request hook event) in both @opencode/plugin (Promise) and @opencode/plugin/effect. This is additive, so existing plugins are unaffected.
One thing worth adding: the plugin docs (services/site/src/docs/content/build/plugins/server.mdx, the "Native HTTP" section and the hook type listing) don't mention the new field yet. They should say that setting it skips the request and that http.response hooks still run. They should also note it has no effect for providers that stream over a WebSocket, since http.request never sees that traffic.
Fixes #65198
|
Dont forget auto thumbs up your initial comment or this will get closed in 1 month because devs here ignores 99% things... you need 2 votes |
Issue for this PR
Addresses #65198
Type of change
What does this PR do?
Plugins can block a tool call (
execute.beforecan fail withTool.Error), but there's no analogous way to block orlocally answer a model request. An
http.requesthook can only throw, ending the turn with an unknown error andskipping the retry handling.
To that end, this PR adds an optional
responseto thehttp.requestevent. If a hook sets it, the request is not sent and the provided response is used directly.Implementation details:
http.responsehooks still run/the response then gets normal handling, so OpenCode and other plugins treat it like a provider's responseresponse: undefinedso plugins can detect support via"response" in eventHow did you verify your code works?
packages/core/test/session-model-request-hooks.test.tspasses locally. checks that the hook's response comes back, the request is never sent, andhttp.responsehooks see the answer.Screenshots / recordings
N/A
Checklist
If you do not follow this template your PR will be automatically rejected.
Added by the opencode bot (AI-generated).
Fixes #65198
Public API: adds optional
responseto thehttp.requestsession hook event in the Promise and Effect plugin APIs (backwards compatible)