$npx -y skills add OthmanAdi/openui-forge --skill openui-forge-anthropicOpenUI generative UI with Anthropic Claude SDK backend. Stream conversion to OpenAI NDJSON format.
| 1 | # OpenUI Forge — Anthropic |
| 2 | |
| 3 | Build generative UI apps with OpenUI + Anthropic Claude. Converts Anthropic streaming events to OpenAI-compatible NDJSON. |
| 4 | |
| 5 | ## Activation Triggers |
| 6 | |
| 7 | - "openui anthropic", "openui claude", "openui sonnet" |
| 8 | - "generative ui claude", "claude streaming ui" |
| 9 | |
| 10 | ## Prerequisites |
| 11 | |
| 12 | - Node.js >= 22 (24 LTS recommended), React >= 18.3.1 (19+ recommended) |
| 13 | - `ANTHROPIC_API_KEY` environment variable set |
| 14 | - Next.js project (App Router recommended) |
| 15 | |
| 16 | ## Quick Start |
| 17 | |
| 18 | 1. Install dependencies: |
| 19 | ```bash |
| 20 | npm install @openuidev/react-ui @openuidev/react-headless @openuidev/react-lang lucide-react zod @anthropic-ai/sdk |
| 21 | ``` |
| 22 | 2. Add the CSS import to `app/layout.tsx`: |
| 23 | ```tsx |
| 24 | import "@openuidev/react-ui/components.css"; |
| 25 | ``` |
| 26 | 3. Create the API route and frontend page below |
| 27 | 4. Run `npm run dev` and test |
| 28 | |
| 29 | ## Full Code |
| 30 | |
| 31 | ### Backend: `app/api/chat/route.ts` |
| 32 | |
| 33 | The backend streams from Anthropic and converts each event into OpenAI-compatible SSE chunks that `openAIAdapter()` expects (`data: {json}\n\n` lines, terminated by `data: [DONE]`). |
| 34 | |
| 35 | ```typescript |
| 36 | import { openuiChatLibrary } from "@openuidev/react-ui/genui-lib"; |
| 37 | import Anthropic from "@anthropic-ai/sdk"; |
| 38 | |
| 39 | const client = new Anthropic(); |
| 40 | |
| 41 | export async function POST(req: Request) { |
| 42 | const { messages } = await req.json(); |
| 43 | |
| 44 | const systemPrompt = openuiChatLibrary.prompt({ |
| 45 | preamble: "You are a helpful assistant that generates interactive UIs.", |
| 46 | additionalRules: ["Always use Stack as root when combining multiple components."], |
| 47 | }); |
| 48 | |
| 49 | // ANTHROPIC_MODEL alternatives: claude-opus-4-8, claude-haiku-4-5, claude-fable-5 |
| 50 | const stream = client.messages.stream({ |
| 51 | model: process.env.ANTHROPIC_MODEL ?? "claude-sonnet-4-6", |
| 52 | max_tokens: 4096, |
| 53 | system: systemPrompt, |
| 54 | messages, |
| 55 | }); |
| 56 | |
| 57 | const encoder = new TextEncoder(); |
| 58 | const readableStream = new ReadableStream({ |
| 59 | async start(controller) { |
| 60 | const id = `chatcmpl-${Date.now()}`; |
| 61 | for await (const event of stream) { |
| 62 | if ( |
| 63 | event.type === "content_block_delta" && |
| 64 | event.delta.type === "text_delta" |
| 65 | ) { |
| 66 | const chunk = { |
| 67 | id, |
| 68 | object: "chat.completion.chunk", |
| 69 | choices: [ |
| 70 | { |
| 71 | index: 0, |
| 72 | delta: { content: event.delta.text }, |
| 73 | finish_reason: null, |
| 74 | }, |
| 75 | ], |
| 76 | }; |
| 77 | controller.enqueue( |
| 78 | encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`) |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | const done = { |
| 83 | id, |
| 84 | object: "chat.completion.chunk", |
| 85 | choices: [{ index: 0, delta: {}, finish_reason: "stop" }], |
| 86 | }; |
| 87 | controller.enqueue(encoder.encode(`data: ${JSON.stringify(done)}\n\n`)); |
| 88 | controller.enqueue(encoder.encode("data: [DONE]\n\n")); |
| 89 | controller.close(); |
| 90 | }, |
| 91 | }); |
| 92 | |
| 93 | return new Response(readableStream, { |
| 94 | headers: { "Content-Type": "text/event-stream" }, |
| 95 | }); |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | ### Frontend: `app/chat/page.tsx` |
| 100 | |
| 101 | ```tsx |
| 102 | "use client"; |
| 103 | import { FullScreen } from "@openuidev/react-ui"; |
| 104 | import { openuiChatLibrary } from "@openuidev/react-ui/genui-lib"; |
| 105 | import { |
| 106 | openAIAdapter, |
| 107 | openAIMessageFormat, |
| 108 | } from "@openuidev/react-headless"; |
| 109 | |
| 110 | export default function ChatPage() { |
| 111 | return ( |
| 112 | <FullScreen |
| 113 | componentLibrary={openuiChatLibrary} |
| 114 | streamProtocol={openAIAdapter()} |
| 115 | messageFormat={openAIMessageFormat} |
| 116 | apiUrl="/api/chat" |
| 117 | /> |
| 118 | ); |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | > The backend emits SSE (`data: {json}\n\n`). Pair it with `openAIAdapter()` on the frontend — `openAIReadableStreamAdapter()` is for NDJSON (no `data:` prefix) and will silently produce no output here. |
| 123 | |
| 124 | ## Component Creation |
| 125 | |
| 126 | ```tsx |
| 127 | import { defineComponent } from "@openuidev/react-lang"; |
| 128 | import { z } from "zod"; |
| 129 | |
| 130 | export const StatusCard = defineComponent({ |
| 131 | name: "StatusCard", |
| 132 | description: "Displays a status with label and color indicator", |
| 133 | props: z.object({ |
| 134 | label: z.string().describe("Status label text"), |
| 135 | status: z.enum(["ok", "warning", "error"]).describe("Current status level"), |
| 136 | }), |
| 137 | component: ({ props }) => { |
| 138 | const colors = { ok: "#22c55e", warning: "#eab308", error: "#ef4444" }; |
| 139 | return ( |
| 140 | <div style={{ display: "flex", alignItems: "center", gap: 8 }}> |
| 141 | <span style={{ width: 10, height: 10, borderRadius: "50%", background: colors[props.status] }} /> |
| 142 | <span>{props.label}</span> |
| 143 | </div> |
| 144 | ); |
| 145 | }, |
| 146 | }); |
| 147 | ``` |
| 148 | |
| 149 | ## System Prompt Generation |
| 150 | |
| 151 | ```bash |
| 152 | npx @openuidev/cli generate ./src/lib/library.ts --out src/generated/system-prompt.txt |
| 153 | ``` |
| 154 | |
| 155 | Or at runtime: `openuiChatLibrary.prompt({ preamble: "...", additionalRules: [...] })`. |
| 156 | |
| 157 | ## Validation Checklist |
| 158 | |
| 159 | - [ ] `ANTHROPIC_API_KEY` is set in `.env.local` |
| 160 | - [ ] CSS import present in root layout |
| 161 | - [ ] Backend converts Anthropic `content_block_delta` events to OpenAI-compatible SSE |