page.mdx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. export const metadata = { title: "AI SDK Integration" }
  2. # AI SDK Integration
  3. Use json-render with the [Vercel AI SDK](https://sdk.vercel.ai) for seamless streaming. json-render supports two modes: **Generate** (standalone UI) and **Chat** (UI embedded in conversation). See [Generation Modes](/docs/generation-modes) for a detailed comparison.
  4. ## Installation
  5. ```bash
  6. npm install ai @ai-sdk/react
  7. ```
  8. ## Generate Mode
  9. In generate mode, the AI outputs only JSONL patches. The entire response is a UI spec with no prose. This is the default mode and is ideal for playgrounds, builders, and dashboard generators.
  10. ### API Route
  11. ```typescript
  12. // app/api/generate/route.ts
  13. import { streamText } from "ai";
  14. import { catalog } from "@/lib/catalog";
  15. export async function POST(req: Request) {
  16. const { prompt, currentTree } = await req.json();
  17. const systemPrompt = catalog.prompt();
  18. // Optionally include current UI state for context
  19. const contextPrompt = currentTree
  20. ? `\n\nCurrent UI state:\n${JSON.stringify(currentTree, null, 2)}`
  21. : "";
  22. const result = streamText({
  23. model: yourModel,
  24. system: systemPrompt + contextPrompt,
  25. prompt,
  26. });
  27. return result.toTextStreamResponse();
  28. }
  29. ```
  30. ### Client
  31. Use `useUIStream` on the client to compile the JSONL stream into a spec:
  32. ```tsx
  33. "use client";
  34. import { useUIStream, Renderer } from "@json-render/react";
  35. function GenerativeUI() {
  36. const { spec, isStreaming, error, send } = useUIStream({
  37. api: "/api/generate",
  38. });
  39. return (
  40. <div>
  41. <button
  42. onClick={() => send("Create a dashboard with metrics")}
  43. disabled={isStreaming}
  44. >
  45. {isStreaming ? "Generating..." : "Generate"}
  46. </button>
  47. {error && <p className="text-red-500">{error.message}</p>}
  48. <Renderer spec={spec} registry={registry} loading={isStreaming} />
  49. </div>
  50. );
  51. }
  52. ```
  53. ## Chat Mode
  54. In chat mode, the AI responds conversationally and includes JSONL patches inline. Text-only replies are allowed when no UI is needed. This is ideal for chatbots, copilots, and educational assistants.
  55. ### API Route
  56. Use `pipeJsonRender` to separate text from JSONL patches in the stream. Patches are emitted as data parts that the client can pick up.
  57. ```typescript
  58. // app/api/chat/route.ts
  59. import { streamText } from "ai";
  60. import { pipeJsonRender } from "@json-render/core";
  61. import {
  62. createUIMessageStream,
  63. createUIMessageStreamResponse,
  64. } from "ai";
  65. import { catalog } from "@/lib/catalog";
  66. export async function POST(req: Request) {
  67. const { messages } = await req.json();
  68. const result = streamText({
  69. model: yourModel,
  70. system: catalog.prompt({ mode: "chat" }),
  71. messages,
  72. });
  73. const stream = createUIMessageStream({
  74. execute: async ({ writer }) => {
  75. writer.merge(pipeJsonRender(result.toUIMessageStream()));
  76. },
  77. });
  78. return createUIMessageStreamResponse({ stream });
  79. }
  80. ```
  81. ### Client
  82. Use `useChat` from the AI SDK and `useJsonRenderMessage` from json-render to extract the spec from each message:
  83. ```tsx
  84. "use client";
  85. import { useChat } from "@ai-sdk/react";
  86. import { useJsonRenderMessage, Renderer } from "@json-render/react";
  87. function Chat() {
  88. const { messages, input, handleInputChange, handleSubmit } = useChat({
  89. api: "/api/chat",
  90. });
  91. return (
  92. <div>
  93. <div>
  94. {messages.map((msg) => (
  95. <ChatMessage key={msg.id} message={msg} />
  96. ))}
  97. </div>
  98. <form onSubmit={handleSubmit}>
  99. <input
  100. value={input}
  101. onChange={handleInputChange}
  102. placeholder="Ask something..."
  103. />
  104. <button type="submit">Send</button>
  105. </form>
  106. </div>
  107. );
  108. }
  109. function ChatMessage({ message }: { message: { parts: Array<{ type: string; text?: string; data?: unknown }> } }) {
  110. const { spec, text, hasSpec } = useJsonRenderMessage(message.parts);
  111. return (
  112. <div>
  113. {text && <p>{text}</p>}
  114. {hasSpec && spec && (
  115. <Renderer spec={spec} registry={registry} />
  116. )}
  117. </div>
  118. );
  119. }
  120. ```
  121. ## Prompt Engineering
  122. The `catalog.prompt()` method creates an optimized system prompt that:
  123. - Lists all available components and their props
  124. - Describes available actions
  125. - Specifies the expected output format (JSONL-only or text + JSONL depending on mode)
  126. - Includes examples for better generation
  127. ### Custom Rules
  128. Pass custom rules to tailor AI behavior:
  129. ```typescript
  130. const systemPrompt = catalog.prompt({
  131. customRules: [
  132. "Always use Card components for grouping related content",
  133. "Prefer horizontal layouts (Row) for metrics",
  134. "Use consistent spacing with padding=\"md\"",
  135. ],
  136. });
  137. ```
  138. ### Chat Mode Prompt
  139. ```typescript
  140. const chatPrompt = catalog.prompt({ mode: "chat" });
  141. ```
  142. In chat mode, the prompt instructs the AI to respond conversationally first, then include JSONL patches on their own lines when UI is needed. Text-only replies are allowed.
  143. ## Which Mode?
  144. <div className="my-6 overflow-x-auto">
  145. <table className="mdx-table w-full text-sm border-collapse">
  146. <thead>
  147. <tr>
  148. <th></th>
  149. <th>Generate</th>
  150. <th>Chat</th>
  151. </tr>
  152. </thead>
  153. <tbody>
  154. <tr>
  155. <td>Output</td>
  156. <td>JSONL only</td>
  157. <td>Text + JSONL</td>
  158. </tr>
  159. <tr>
  160. <td>Text-only replies</td>
  161. <td>No</td>
  162. <td>Yes</td>
  163. </tr>
  164. <tr>
  165. <td>System prompt</td>
  166. <td><code>catalog.prompt()</code></td>
  167. <td><code>catalog.prompt({ mode: "chat" })</code></td>
  168. </tr>
  169. <tr>
  170. <td>Stream utility</td>
  171. <td><code>useUIStream</code></td>
  172. <td><code>pipeJsonRender</code> + <code>useJsonRenderMessage</code></td>
  173. </tr>
  174. <tr>
  175. <td>Use case</td>
  176. <td>Playgrounds, builders</td>
  177. <td>Chatbots, copilots</td>
  178. </tr>
  179. </tbody>
  180. </table>
  181. </div>
  182. Learn more in the [Generation Modes](/docs/generation-modes) guide.
  183. ## Next
  184. - Learn about [progressive streaming](/docs/streaming)
  185. - See the [chat example](https://github.com/vercel-labs/json-render/tree/main/examples/chat) for a complete implementation