Use getReader for stream shape checks#34
Merged
robert-j-y merged 2 commits intoOpenRouterTeam:mainfrom May 1, 2026
Merged
Conversation
Collaborator
|
Recommended path: move forward with this PR rather than #33. This version checks for Before merge, I’d ask for two additions:
I locally validated the current PR with |
Author
|
pushed unit tests and updated the PR description to address comments |
This was referenced May 1, 2026
4 tasks
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.
Summary
Refactors
isEventStreamfunction to check forgetReaderinstead oftoReadableStream.It also updates the non-streaming response guard to use the same stream-readability check.
Why check for
getReader()instead oftoReadableStream()This function gets used to check what gets passed into
ReusableReadableStream. It later calls:typescript-agent/packages/agent/src/lib/reusable-stream.ts
Line 146 in e3a868c
That means the actual runtime contract this typecheck is: “can the value passed downstream be invoked with
getReader()?”toReadableStream()seems to be a hallucination? I haven't found anything in the OpenRouter SDKs that either calls or providestoReadableStream().So a value that only has
toReadableStream()but does not itself havegetReader()would pass the old guard and then fail later whenReusableReadableStreamtries to read it.Testing
The unit tests cover the two response shapes that matter for this change:
ReadableStreamwhose constructor name is notEventStream, matching bundled/minified runtimes such as OpenNext on Cloudflare Workers.toReadableStream()and does not exposegetReader(), verifying it is rejected before reachingReusableReadableStream.Validation commands:
pnpm --filter @openrouter/agent lint pnpm --filter @openrouter/agent typecheck pnpm --filter @openrouter/agent testI also verified this against a production Cloudflare Worker:
@openrouter/agentfrom this PR branch.next@16.2.4,@opennextjs/cloudflare@1.19.5, andwrangler@4.87.0.ReadableStream/getReader()guard and no constructor-name ortoReadableStream()branch.opennextjs-cloudflare deploy.OPENROUTER_API_KEYWorker secret.Expected response:
{"text":"ok","object":"response"}Observed result from the deployed Worker:
HTTP/2 200{"text":"ok","object":"response"}