Skip to content

feat(client, server): add eventIteratorToUnproxiedDataStream util - #1110

Merged
dinwwwh merged 4 commits into
mainfrom
feat/shared/eventIteratorToUnproxiedDataStream-util
Oct 20, 2025
Merged

feat(client, server): add eventIteratorToUnproxiedDataStream util#1110
dinwwwh merged 4 commits into
mainfrom
feat/shared/eventIteratorToUnproxiedDataStream-util

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Oct 19, 2025

Copy link
Copy Markdown
Member

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

    • Added a data-stream conversion that returns unproxied values for streamed results, exposed across client and server packages for better compatibility with AI SDKs and integrations.
  • Documentation

    • Updated AI SDK integration docs to recommend using the new unproxied data-stream handling for reliable structured-data processing.
  • Tests

    • Added tests covering normal, empty, error, and cancellation scenarios for the new data-stream behavior.

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.
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 19, 2025
@vercel

vercel Bot commented Oct 19, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
orpc Ready Ready Preview Comment Oct 20, 2025 2:27am

💡 Enable Vercel Agent with $100 free credit for automated AI reviews

@coderabbitai

coderabbitai Bot commented Oct 19, 2025

Copy link
Copy Markdown

Note

Other AI code review bot(s) detected

CodeRabbit 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.

Walkthrough

Adds a new utility that converts async event iterators into ReadableStreams that emit unproxied (shallow-cloned) values, re-exports it from client/server barrels as eventIteratorToUnproxiedDataStream, and updates AI SDK docs to recommend the unproxied variant.

Changes

Cohort / File(s) Summary
Core implementation & tests
packages/shared/src/stream.ts, packages/shared/src/stream.test.ts
Adds asyncIteratorToUnproxiedDataStream<T>(iter) which wraps an AsyncIterator into a ReadableStream<T> and shallow-clones objects/arrays to remove proxies before enqueueing. Tests added/updated for normal flow, empty iterators, iterator errors, and cancellation cleanup.
Public API exports
packages/client/src/index.ts, packages/server/src/index.ts
Re-exports eventIteratorToUnproxiedDataStream as an alias for asyncIteratorToUnproxiedDataStream from @orpc/shared in both client and server public barrels.
Documentation
apps/content/docs/integrations/ai-sdk.md
Replaces eventIteratorToStream(...) usage with eventIteratorToUnproxiedDataStream(...) and adds an informational note recommending the unproxied variant due to AI SDK performing structuredClone on messages.

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)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through proxies, nudged each boxed thing,
I spread and I cloned so the chat could take wing,
Streams now flow simple, no DataClone fright,
Small unwrapped bundles — everything's light.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "feat(client, server): add eventIteratorToUnproxiedDataStream util" accurately describes the primary change in this pull request. It clearly communicates that a new utility function is being added to both the client and server packages, which is precisely what the changeset implements—a new eventIteratorToUnproxiedDataStream function exported from client and server packages, with the underlying implementation and tests added to the shared stream utilities. The title is specific, concise, and directly related to the main objective.
Linked Issues Check ✅ Passed The code changes directly address the objective stated in linked issue #977: to provide unproxied data from event iterators for compatibility with downstream libraries like the AI SDK that call structuredClone and cannot handle proxy objects. The implementation adds asyncIteratorToUnproxiedDataStream (aliased as eventIteratorToUnproxiedDataStream) which converts async iterators to ReadableStreams while unproxying values via object cloning, array mapping, or pass-through for primitive values. The utility is properly exported from both client and server packages, and the ai-sdk.md documentation is updated to recommend this function over the existing eventIteratorToStream when integrating with AI SDK, directly addressing the compatibility requirement.
Out of Scope Changes Check ✅ Passed All changes in this pull request are directly scoped to implementing and exposing the new unproxied event iterator utility. The implementation in packages/shared/src/stream.ts provides the core functionality, re-exports in client and server packages make it available to consumers, tests in stream.test.ts validate the implementation, and the ai-sdk.md documentation change guides users to the new utility specifically for AI SDK integration scenarios. There are no ancillary changes, unrelated bug fixes, or modifications to unrelated code paths.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/shared/eventIteratorToUnproxiedDataStream-util

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c5393db and 1fb8091.

📒 Files selected for processing (2)
  • apps/content/docs/integrations/ai-sdk.md (2 hunks)
  • packages/shared/src/stream.ts (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/shared/src/stream.ts (3)
packages/client/src/index.ts (1)
  • asyncIteratorToUnproxiedDataStream (13-13)
packages/server/src/index.ts (1)
  • asyncIteratorToUnproxiedDataStream (46-46)
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). (2)
  • GitHub Check: publish-commit
  • GitHub Check: lint
🔇 Additional comments (5)
apps/content/docs/integrations/ai-sdk.md (2)

62-62: LGTM! Clear migration to unproxied stream utility.

The import and usage correctly reflect the new utility function that ensures compatibility with AI SDK's structuredClone requirements.

Also applies to: 68-68


119-123: Helpful guidance for users.

The info block clearly explains the rationale for preferring the unproxied variant and directly addresses the compatibility issue with AI SDK's structuredClone.

packages/shared/src/stream.ts (3)

2-2: Appropriate utility import.

The isObject helper is correctly imported to distinguish plain objects from arrays and other types during the unproxying process.


4-6: Good documentation practice.

Adding JSDoc comments to existing functions improves the public API documentation.

Also applies to: 22-24


49-73: Solid implementation; shallow cloning is appropriate for this use case.

The function correctly unproxies objects and arrays using shallow cloning, which addresses the AI SDK compatibility issue. Arrays are now properly handled (addressing the past review comment), and the cancellation logic correctly forwards to the iterator.

Shallow cloning is sufficient here because oRPC proxies are applied at the top level for metadata tracking. Nested proxied objects are unlikely in this context.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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, eventIteratorToUnproxiedDataStream, which facilitates the conversion of asynchronous iterators into readable streams while automatically unproxying any emitted object data. This is particularly important for integrations with third-party libraries, such as the AI SDK, that may not correctly process proxied objects. The changes include the implementation of this utility, its export from core packages, an update to the AI SDK integration documentation to guide users on its usage, and the addition of robust unit tests to validate its functionality.

Highlights

  • New Utility for Unproxied Data Streams: Introduced eventIteratorToUnproxiedDataStream to convert async iterators to readable streams, specifically designed to unproxy emitted data.
  • AI SDK Compatibility: This new utility resolves compatibility issues with tools like AI SDK that do not support proxied data, ensuring raw data is emitted.
  • Documentation Update: The AI SDK integration documentation has been updated to recommend using the new eventIteratorToUnproxiedDataStream with a clear explanation of its purpose.
  • Exported Utility: The asyncIteratorToUnproxiedDataStream utility is now exported from both @orpc/client and @orpc/server packages.
  • Comprehensive Testing: New test cases have been added for asyncIteratorToUnproxiedDataStream to ensure its correct behavior, including handling proxied objects, empty iterators, errors, and stream cancellation.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist 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.

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.

Comment thread packages/shared/src/stream.ts
Comment thread packages/shared/src/stream.test.ts
@codecov

codecov Bot commented Oct 19, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pkg-pr-new

pkg-pr-new Bot commented Oct 19, 2025

Copy link
Copy Markdown
More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@1110

@orpc/client

npm i https://pkg.pr.new/@orpc/client@1110

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@1110

@orpc/experimental-durable-iterator

npm i https://pkg.pr.new/@orpc/experimental-durable-iterator@1110

@orpc/hey-api

npm i https://pkg.pr.new/@orpc/hey-api@1110

@orpc/interop

npm i https://pkg.pr.new/@orpc/interop@1110

@orpc/json-schema

npm i https://pkg.pr.new/@orpc/json-schema@1110

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@1110

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@1110

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@1110

@orpc/otel

npm i https://pkg.pr.new/@orpc/otel@1110

@orpc/experimental-publisher

npm i https://pkg.pr.new/@orpc/experimental-publisher@1110

@orpc/react

npm i https://pkg.pr.new/@orpc/react@1110

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@1110

@orpc/experimental-react-swr

npm i https://pkg.pr.new/@orpc/experimental-react-swr@1110

@orpc/server

npm i https://pkg.pr.new/@orpc/server@1110

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@1110

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@1110

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@1110

@orpc/standard-server-aws-lambda

npm i https://pkg.pr.new/@orpc/standard-server-aws-lambda@1110

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@1110

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@1110

@orpc/standard-server-peer

npm i https://pkg.pr.new/@orpc/standard-server-peer@1110

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@1110

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@1110

@orpc/trpc

npm i https://pkg.pr.new/@orpc/trpc@1110

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@1110

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@1110

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@1110

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@1110

commit: 1fb8091

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 11d989e and c5393db.

📒 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 isObject import 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 (returns false for 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 structuredClone failures while maintaining reasonable performance.

@dinwwwh
dinwwwh merged commit e91d2fd into main Oct 20, 2025
11 checks passed
@dinwwwh
dinwwwh deleted the feat/shared/eventIteratorToUnproxiedDataStream-util branch June 22, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client event iterator returns proxy events breaking applications that exepect structuredClone to work

1 participant