feat(client, server): add eventIteratorToUnproxiedDataStream util - #1110
Conversation
Some event iterators proxy their emitted data to track event metadata. When integrating with tools that don't support proxied data (such as AI SDK), use eventIteratorToUnproxiedDataStream instead of eventIteratorToStream to ensure raw, unproxied data is emitted.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
💡 Enable Vercel Agent with $100 free credit for automated AI reviews |
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdds a new utility that converts async event iterators into ReadableStreams that emit unproxied (shallow-cloned) values, re-exports it from client/server barrels as Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant App as Client app
participant ORPC as orpc client
participant Unproxy as eventIteratorToUnproxiedDataStream
participant AI as AI SDK (chat)
App->>ORPC: client.chat(...)
ORPC-->>App: AsyncIterator<Event> (proxied)
App->>Unproxy: eventIteratorToUnproxiedDataStream(asyncIterator)
note right of Unproxy #F7F7FF: Shallow-clone objects/arrays to remove proxies\nEnqueue unproxied values into ReadableStream
Unproxy-->>App: ReadableStream<Event (unproxied)>
App->>AI: pipe/read stream into AI SDK
AI-->>App: consumes events safely (structuredClone-compatible)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🧰 Additional context used🧬 Code graph analysis (1)packages/shared/src/stream.ts (3)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
🔇 Additional comments (5)
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 |
Summary of ChangesHello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new utility function, Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new utility eventIteratorToUnproxiedDataStream to handle integrations with tools like AI SDK that don't support proxied data. The changes include the new utility function, corresponding tests, updated exports, and documentation.
The implementation of asyncIteratorToUnproxiedDataStream correctly identifies the need to unproxy data but has a flaw in its current form: it doesn't handle proxied arrays, which could lead to runtime errors. I've suggested a fix to make it more robust by explicitly handling arrays.
Additionally, I've recommended expanding the test suite to cover more data types, such as arrays, to ensure the utility is reliable across different scenarios.
Overall, this is a valuable addition. Addressing the feedback will make the new utility more robust and prevent potential issues for developers using it.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
More templates
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-iterator
@orpc/hey-api
@orpc/interop
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/experimental-publisher
@orpc/react
@orpc/react-query
@orpc/experimental-react-swr
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/shared/src/stream.ts (1)
45-48: Clarify JSDoc to mention arrays and shallow copying.The documentation says "object values" but the implementation also handles arrays (lines 62-63). Consider updating to be more precise:
/** * Converts an `AsyncIterator` into a `ReadableStream`, ensuring that - * all emitted object values are *unproxied* before enqueuing. + * all emitted objects and arrays are shallow-copied to remove proxy + * wrappers before enqueuing. */This clarifies both the scope (objects and arrays) and the approach (shallow copying).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
apps/content/docs/integrations/ai-sdk.md(2 hunks)packages/shared/src/stream.test.ts(2 hunks)packages/shared/src/stream.ts(3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/shared/src/stream.test.ts
- apps/content/docs/integrations/ai-sdk.md
🧰 Additional context used
🧬 Code graph analysis (1)
packages/shared/src/stream.ts (3)
packages/server/src/index.ts (1)
asyncIteratorToUnproxiedDataStream(46-46)packages/client/src/index.ts (1)
asyncIteratorToUnproxiedDataStream(13-13)packages/shared/src/object.ts (1)
isObject(44-52)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: publish-commit
- GitHub Check: test
- GitHub Check: lint
🔇 Additional comments (2)
packages/shared/src/stream.ts (2)
2-2: LGTM: Import is appropriate for the new unproxying logic.The
isObjectimport is correctly used to identify plain objects that need shallow copying to remove proxy wrappers.
49-73: LGTM: Past review concern addressed, implementation is correct.The unproxying logic now properly handles both arrays and objects, addressing the previous review comment. The implementation correctly:
- Uses
isObject(value)to detect plain objects (returnsfalsefor arrays)- Falls through to
Array.isArray(value)for array detection- Shallow-copies both with spread operators to remove top-level proxy wrappers
- Passes through primitives and other types unchanged
The shallow-copy approach is appropriate for this use case, as it removes the top-level proxy wrappers that cause
structuredClonefailures while maintaining reasonable performance.
Some event iterators proxy their emitted data to track event metadata. When integrating with tools that don't support proxied data (such as AI SDK), use eventIteratorToUnproxiedDataStream instead of eventIteratorToStream to ensure raw, unproxied data is emitted.
Fixed: #977
Summary by CodeRabbit
New Features
Documentation
Tests