feat: stream converter utils + AI SDK examples#775
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThe changes introduce utility functions for converting between Changes
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
Suggested labels
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
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 (
streamToAsyncIteratorClassandasyncIteratorToStream) in the@orpc/sharedpackage. These functions enable seamless conversion betweenReadableStreamandAsyncIteratorClasstypes, 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.jsonandpnpm-lock.yamlto 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
-
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. ↩
Codecov ReportAll 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-event-iterator
@orpc/hey-api
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/react
@orpc/react-query
@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.
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.
There was a problem hiding this comment.
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 sinceReadableStreamReadResult<T>is already compatible withIteratorResult<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
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis 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 requirementsThe added AI SDK packages in
apps/content/package.jsonare 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.tsexists- Exports
streamToAsyncIteratorClassandasyncIteratorToStreamas expectedThe 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 NeededVerification confirms that both
asyncIteratorToStreamandstreamToAsyncIteratorClassare indeed exported inpackages/shared/src/stream.ts. The alias mappings inpackages/server/src/index.tsare 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.
Closes: https://github.com/unnoq/orpc/issues/768
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Chores