Draft
Conversation
This commit introduces the @ai-sdk/react-native package, enabling optimized markdown streaming for React Native applications. It includes client-side hooks and server-side utilities for efficient markdown parsing and rendering. Co-authored-by: jcourson8 <jcourson8@gmail.com>
|
Cursor Agent can help with this pull request. Just |
Co-authored-by: jcourson8 <jcourson8@gmail.com>
Owner
Author
|
@cursoragent how to build this to run on my machine i will package json it relative to the fike |
Using in Your React Native ProjectOnce built, you can link it to your React Native app: Option 1: Local file reference// In your React Native app's package.json
{
"dependencies": {
"@ai-sdk/react-native": "file:/path/to/ai/packages/react-native"
}
}Option 2: Using pnpm workspace link (for development)cd /path/to/your-react-native-app
pnpm link /path/to/ai/packages/react-nativeQuick Usage ExampleServer (Next.js API): import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { createMarkdownStreamResponse } from '@ai-sdk/react-native/server';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({ model: openai('gpt-4o'), messages });
return createMarkdownStreamResponse(result.textStream);
}Client (React Native): import { useChat, MarkdownText } from '@ai-sdk/react-native';
import { View, ScrollView } from 'react-native';
function Chat() {
const { messages, sendMessage } = useChat({ api: '/api/chat' });
return (
<ScrollView>
{messages.map(msg => (
<View key={msg.id}>
{msg.parts.map((part, i) =>
part.type === 'text' ? <MarkdownText key={i} text={part.text} /> : null
)}
</View>
))}
</ScrollView>
);
}Commands Summary
Learn more about Cursor Agents |
Co-authored-by: jcourson8 <jcourson8@gmail.com>
Co-authored-by: jcourson8 <jcourson8@gmail.com>
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.
Background
This change introduces first-class support for efficient markdown rendering in React Native applications using the AI SDK. The existing approach of parsing raw markdown strings on the client-side in React Native is performance-intensive, leading to janky UI and high resource usage.
This PR implements the solution pioneered by the v0 team: offloading markdown parsing to the server, streaming JSON tree patches to the client, and rendering native components directly from the JSON tree.
Summary
A new package,
@ai-sdk/react-native, has been created to provide:append-textoptimization for streaming text.useMarkdownChathook to consume the streamed JSON tree patches and manage chat state.MarkdownRenderercomponent that renders the JSON markdown tree directly into React Native components, allowing for full customization of markdown elements.This approach significantly reduces client-side work, improving performance and user experience in React Native AI chat applications.
Manual Verification
The core logic for markdown parsing, diffing, and patching is covered by unit tests.
To manually verify end-to-end functionality, a React Native example application would need to be set up, consuming the
createMarkdownStreamResponseon the server anduseMarkdownChat/MarkdownRendereron the client.Checklist
pnpm changesetin the project root)Future Work
MarkdownRendererfor large trees, potentially with virtualization.