Skip to content

feat: stream converter utils + AI SDK examples#775

Merged
dinwwwh merged 5 commits intomainfrom
feat/shared/stream-convert-utils
Jul 16, 2025
Merged

feat: stream converter utils + AI SDK examples#775
dinwwwh merged 5 commits intomainfrom
feat/shared/stream-convert-utils

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Jul 15, 2025

Closes: https://github.com/unnoq/orpc/issues/768

Summary by CodeRabbit

  • New Features

    • Added documentation for integrating the AI SDK, including setup instructions and usage examples for both server and client sides.
    • Introduced new utilities for converting between streams and async iterators, now available in the shared package.
    • Expanded sidebar navigation in the documentation to include an "AI SDK" section.
  • Bug Fixes

    • None.
  • Tests

    • Added comprehensive tests to verify correct conversion between streams and async iterators, including error handling and resource cleanup.
  • Chores

    • Updated development dependencies to include the latest AI SDK packages.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jul 15, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 15, 2025 1:26pm

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 15, 2025

Walkthrough

The changes introduce utility functions for converting between ReadableStream and AsyncIteratorClass, along with comprehensive tests for these conversions. Documentation and sidebar navigation are updated to include an "AI SDK Integration" guide. Related dependencies are added, and relevant exports are propagated across shared, client, and server packages.

Changes

Files/Paths Change Summary
apps/content/.vitepress/config.ts Added "AI SDK" link under "Integrations" in the documentation sidebar.
apps/content/docs/integrations/ai-sdk.md New documentation: "AI SDK Integration" guide with server/client examples and usage notes.
apps/content/package.json Added devDependencies: @ai-sdk/google, @ai-sdk/react, and ai (all beta versions).
packages/shared/src/stream.ts Added utility functions: streamToAsyncIteratorClass and asyncIteratorToStream for stream/iterator conversion.
packages/shared/src/stream.test.ts New test suite for stream/iterator conversion utilities, covering normal, empty, error, and cleanup cases.
packages/shared/src/index.ts Exported all from ./stream to make new utilities available.
packages/client/src/index.ts, packages/server/src/index.ts Re-exported new utilities as eventIteratorToStream and streamToEventIterator alongside existing exports.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant AI_SDK

    Client->>Server: Initiate chat (oRPC)
    Server->>AI_SDK: Send chat messages
    AI_SDK-->>Server: Stream responses
    Server->>Server: Convert stream to EventIterator (streamToEventIterator)
    Server-->>Client: Send EventIterator via oRPC
    Client->>Client: Convert EventIterator to stream (eventIteratorToStream)
    Client->>Client: Use AI SDK React hooks for UI
Loading

Suggested labels

size:L

Poem

In the warren where streams and iterators meet,
A rabbit hops coding with nimble feet.
Docs now guide the AI SDK way,
With tests to ensure streams never stray.
New exports abound, dependencies bloom—
Hopping forward, there’s plenty of room!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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 significantly enhances oRPC's capabilities by introducing robust utility functions for converting between ReadableStream and AsyncIterator types. This foundational improvement is immediately leveraged to provide a new, detailed integration guide and examples for using oRPC with the AI SDK, facilitating the development of AI-powered streaming applications.

Highlights

  • Stream Conversion Utilities: Introduced new utility functions (streamToAsyncIteratorClass and asyncIteratorToStream) in the @orpc/shared package. These functions enable seamless conversion between ReadableStream and AsyncIteratorClass types, making stream handling more flexible within the oRPC framework.
  • AI SDK Integration Documentation: Added a new documentation page (/docs/integrations/ai-sdk) that provides comprehensive examples and guidance on integrating oRPC with the AI SDK. This includes how to use the new stream conversion utilities for both server-side (converting AI SDK streams to oRPC Event Iterators) and client-side (converting oRPC Event Iterators back to streams for @ai-sdk/react) implementations.
  • Dependency Management: Updated package.json and pnpm-lock.yaml to include necessary AI SDK related packages (@ai-sdk/google, @ai-sdk/react, ai) to support the new integration examples and documentation.
  • Stream Utility Testing: Included dedicated unit tests for the new stream conversion utilities (packages/shared/src/stream.test.ts) to ensure their correctness, robustness, and proper handling of various scenarios including empty streams, errors, and resource cleanup.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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.

@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 15, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jul 15, 2025

More templates

@orpc/arktype

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

@orpc/client

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

@orpc/contract

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

@orpc/experimental-durable-event-iterator

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

@orpc/hey-api

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

@orpc/json-schema

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

@orpc/nest

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

@orpc/openapi

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

@orpc/openapi-client

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

@orpc/react

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

@orpc/react-query

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

@orpc/server

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

@orpc/shared

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

@orpc/solid-query

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

@orpc/standard-server

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

@orpc/standard-server-aws-lambda

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

@orpc/standard-server-fetch

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

@orpc/standard-server-node

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

@orpc/standard-server-peer

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

@orpc/svelte-query

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

@orpc/tanstack-query

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

@orpc/trpc

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

@orpc/valibot

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

@orpc/vue-colada

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

@orpc/vue-query

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

@orpc/zod

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

commit: 9c4000a

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This PR introduces stream conversion utilities (streamToEventIterator and eventIteratorToStream) to bridge between Web Streams and oRPC's Event Iterators, along with documentation and examples for integrating with the Vercel AI SDK. The new utilities are well-tested. The review focuses on improving the correctness and completeness of the documentation examples and suggests a stylistic improvement in the test code.

Comment thread apps/content/docs/integrations/ai-sdk.md
Comment thread apps/content/docs/integrations/ai-sdk.md
Comment thread packages/shared/src/stream.test.ts
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
packages/shared/src/stream.ts (1)

10-10: Remove unnecessary type cast.

The type cast to Promise<IteratorResult<T>> is unnecessary since ReadableStreamReadResult<T> is already compatible with IteratorResult<T> structurally.

-      return reader.read() as Promise<IteratorResult<T>>
+      return reader.read()
apps/content/docs/integrations/ai-sdk.md (1)

52-54: Consider providing more helpful guidance for reconnection.

The current implementation just throws an error. While the info callout below explains this, the code could be more informative.

       reconnectToStream(options) {
-        throw new Error('Unsupported')
+        throw new Error('Reconnection not supported by default. See documentation for custom implementation.')
       },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e1b0a55 and 9c4000a.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (8)
  • apps/content/.vitepress/config.ts (1 hunks)
  • apps/content/docs/integrations/ai-sdk.md (1 hunks)
  • apps/content/package.json (2 hunks)
  • packages/client/src/index.ts (1 hunks)
  • packages/server/src/index.ts (1 hunks)
  • packages/shared/src/index.ts (1 hunks)
  • packages/shared/src/stream.test.ts (1 hunks)
  • packages/shared/src/stream.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/shared/src/stream.test.ts (4)
packages/client/src/index.ts (2)
  • streamToAsyncIteratorClass (16-16)
  • asyncIteratorToStream (10-10)
packages/server/src/index.ts (2)
  • streamToAsyncIteratorClass (50-50)
  • asyncIteratorToStream (44-44)
packages/shared/src/stream.ts (2)
  • streamToAsyncIteratorClass (3-16)
  • asyncIteratorToStream (18-36)
packages/shared/src/iterator.ts (1)
  • AsyncIteratorClass (23-96)
⏰ 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: lint
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (10)
apps/content/.vitepress/config.ts (1)

145-145: LGTM - Sidebar navigation addition looks correct.

The new AI SDK integration item follows the established pattern and will provide proper navigation to the new documentation.

apps/content/package.json (1)

11-12: Confirm beta package versions align with stability requirements

The added AI SDK packages in apps/content/package.json are all beta releases, whereas the latest stable versions are:

  • @ai-sdk/google: used 2.0.0-beta.9 → stable 1.2.22
  • @ai-sdk/react: used 2.0.0-beta.18 → stable 0.0.69
  • ai: used 5.0.0-beta.18 → stable 3.4.23

Since these are only in devDependencies for documentation, the stability risk is reduced. Please verify that:

  • You intentionally need these beta features for your docs.
  • You’re comfortable with potential breaking changes in beta releases.
  • You’ve pinned versions to avoid unexpected updates.

apps/content/package.json (Lines 11–12)

packages/shared/src/index.ts (1)

13-13: Stream module export verified

  • Confirmed packages/shared/src/stream.ts exists
  • Exports streamToAsyncIteratorClass and asyncIteratorToStream as expected

The new export * from './stream' line correctly exposes these utilities in the shared package’s public API.

packages/server/src/index.ts (1)

43-51: Aliases Verified – No Action Needed

Verification confirms that both asyncIteratorToStream and streamToAsyncIteratorClass are indeed exported in packages/shared/src/stream.ts. The alias mappings in packages/server/src/index.ts are valid and require no further changes.

packages/client/src/index.ts (1)

9-17: LGTM - Consistent export structure with server package.

The export statement maintains consistency with the server package and provides the same stream/iterator conversion utilities to client-side code, which supports the AI SDK integration use cases.

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

3-16: LGTM! Well-structured stream-to-iterator conversion.

The implementation correctly handles the conversion from ReadableStream to AsyncIteratorClass with proper cleanup through the reader.cancel() method.


18-36: LGTM! Robust iterator-to-stream conversion with proper cleanup.

The implementation correctly handles the conversion from AsyncIterator to ReadableStream, including proper cleanup through the optional iterator.return() method.

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

4-84: Excellent test coverage for streamToAsyncIteratorClass.

The tests comprehensively cover normal operation, empty streams, error propagation, and cleanup behavior. The cleanup test particularly demonstrates proper resource management.


86-167: Comprehensive test coverage for asyncIteratorToStream.

The tests thoroughly cover conversion functionality, error handling, and cleanup behavior. The error test correctly verifies that iterator errors are properly propagated through the stream interface.


169-189: Excellent integration test for chained conversions.

This test validates that the conversion utilities work correctly when chained together, ensuring data integrity throughout the conversion process.

Comment thread apps/content/docs/integrations/ai-sdk.md
Comment thread apps/content/docs/integrations/ai-sdk.md
@dinwwwh dinwwwh merged commit e788265 into main Jul 16, 2025
11 checks passed
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.

Is there any way to integrate ai-sdk with orpc?

1 participant