Draft
Conversation
This commit introduces an example demonstrating how to stream MDX tree data and patches to the client for real-time rendering. Co-authored-by: jcourson8 <jcourson8@gmail.com>
|
Cursor Agent can help with this pull request. Just |
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 addresses a user request to enable streaming MDX/JSON tree structures (and patches) to a client, particularly for React Native, to avoid client-side markdown parsing while retaining the
useChatdeveloper experience. The goal was to demonstrate how to achieve this with minimal to no changes to the AI SDK itself.Summary
This PR introduces a new example (
examples/next-openai/app/use-chat-mdx-tree-patches) that showcases how to stream structured data parts alongside regular assistant text using the existinguseChathook andDefaultChatTransport.The example includes:
app/api/use-chat-mdx-tree-patches/route.ts) that:data-mdxTreesnapshot.data-mdxPatchevents astransient: truechunks.data-mdxTreesnapshot.app/use-chat-mdx-tree-patches/page.tsx) that:useChatwithDefaultChatTransport.onDatato applydata-mdxPatchevents to a local JSON tree cache.This demonstrates that the AI SDK's
data-*UI message chunk mechanism can be leveraged for advanced streaming scenarios like MDX tree patches without requiring core SDK modifications.Manual Verification
next-openaiexample:pnpm --filter next-openai devhttp://localhost:3000/use-chat-mdx-tree-patchesdata-mdxPatchchunks.data-mdxTreepart in the message history, showing the final JSON tree snapshot.Checklist
pnpm changesetin the project root)Future Work
data-*chunks with{ patch, protocol }semantics inprocessUIMessageStreamto automatically apply patches to existing data parts, reducing boilerplate inonDatahandlers.fetchadapter or transport example within the SDK.