Skip to content

fix(plugin): support stateless generation HTTP hooks - #53990

Closed
peeyush-ciridae wants to merge 1 commit into
anomalyco:devfrom
peeyush-ciridae:generate-http-hooks
Closed

peeyush-ciridae wants to merge 1 commit into
anomalyco:devfrom
peeyush-ciridae:generate-http-hooks

Conversation

@peeyush-ciridae

Copy link
Copy Markdown

Issue for this PR

Reproduced on 2.0.25: Anthropic OAuth Haiku 5.5 works in sessions but fails through ctx.generate.text / /api/experimental/generate because stateless generation has no native HTTP hook surface. The provider returns 429 Error; OpenCode reports 503. Haiku 4.5 accepts the same untransformed request.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds provider-scoped ctx.generate.hook("http.request" | "http.response", ...) in both plugin APIs and applies them to stateless native HTTP calls. Events expose a request ID and model, without inventing a session or agent. The endpoint also waits for plugin activation before selecting a model, fixing cold-start “Model unavailable” failures.

The Anthropic auth plugin must register these hooks separately; this PR does not bake provider-specific authentication into Core.

How did you verify your code works?

  • bun run check: passed (repository-wide lint warnings remain).
  • Core bun test test/generate.test.ts test/session-generate.test.ts: 7 passed; verifies request/body and response rewriting, provider scoping, session-hook isolation, and distinct request IDs.
  • Plugin bun test: 9 passed. Server bun test test/generate.test.ts: 1 passed.
  • Website generated-artifact check and build: passed.
  • Isolated source server with the companion OAuth plugin change: Haiku 5.5 and 4.5 both return OK through /api/experimental/generate, including the first request after startup. Used a private database snapshot; installed setup unchanged.
  • Broader Core plugin run: 414 passed, 5 failed when Azure autodiscovery hit the test network guard: test attempted network request: POST https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2022-10-01. Not addressed here.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • No issue referenced. Please add Closes #<number> linking to the relevant issue.

Please edit this PR description to address the above within 72 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@peeyush-ciridae

Copy link
Copy Markdown
Author

Companion OAuth plugin registration: ex-machina-co/opencode-anthropic-auth#288. Both changes were verified together against real Haiku 5.5 and 4.5 stateless requests.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@opencode-agent

opencode-agent Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

I'm still triaging this pull request; I'll answer here when that's done.

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't run this end to end: the Anthropic OAuth case needs real credentials and your companion plugin change. The closest Core, plugin and server tests pass, and Core, plugin and server type-check. The new Core test shows the request and response rewrites reach the wire, and that hooks are scoped to their provider and kept apart from session hooks.

Concerns:

  • Built-in providers that add auth in session HTTP hooks still don't work through ctx.generate.text. Azure Entra (packages/core/src/plugin/provider/azure.ts), Snowflake Cortex and GitHub Copilot add auth or required headers only in ctx.session.hook("http.request"). With a separate generate hook domain, stateless generation for those providers is still sent without those headers, the same failure this PR fixes for Anthropic. Every auth plugin also has to register the same callback twice. Please either register the built-in providers' HTTP hooks for generate too, or say whether a separate domain is meant to be the design. That choice is the maintainers' call.
  • The HTTP hook wrapper is copied from the session code (see the inline comment).
  • The Plugin.awaitActivation line in packages/server/src/handlers/generate.ts duplicates open PR #52887, which fixes #52881 with the same two lines. Leaving it out of this PR, or coordinating with #52887, would keep this PR focused on the hook feature.

})
const requestID = SessionID.create()
const scope = { requestID, model: resolved.ref }
const hasHttpHooks =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has/trigger/body-rebuild/response-wrapping block is a line-for-line copy of the session HTTP hook wrapper in packages/core/src/session/model-request.ts (around lines 337–364). The only difference is the hook domain and the scope fields. Two copies of this tricky conversion (body re-encoding, content type, the 204/205/304 null-body statuses, error mapping) will drift apart over time. Could you move it into one shared helper that takes the domain and scope, and use it from both places?

@opencode-agent

opencode-agent Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Thanks for linking the companion plugin PR and for testing both together against Haiku 5.5 and 4.5. That covers what I couldn't run here.

The head commit hasn't changed, so the review's points still apply:

  • Built-in providers like Azure Entra, Snowflake Cortex and GitHub Copilot add their auth or required headers only in session HTTP hooks. Through ctx.generate.text they still send requests without them. They would need generate registrations too, unless maintainers prefer one hook surface for both.
  • The HTTP hook wrapper in generate.ts is a copy of the one in session/model-request.ts. One shared helper would stop the two from drifting apart.
  • The Plugin.awaitActivation change overlaps fix(core): await plugin activation before text generation (#52881) #52887.

@thdxr
thdxr changed the base branch from v2 to dev October 10, 2026 19:37
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 72-hour window.

Feel free to open a new pull request that follows our guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant