---
name: netra-artifacts
description: Add Netra generative UI to a project, so AI answers render as live interfaces, charts, quizzes and interactive 3D in a sandbox. Use when asked to add generative UI, visual answers, 3D explanations or netra-artifacts to an app.
---

# Add Netra generative UI to this project

You are integrating **netra-artifacts** into this project: AI answers become live interfaces (animated UI, charts, quizzes, formulas and interactive 3D) inside the chat, rendered safely in a sandbox and themed to match the product. Work in small, verified steps. Do not rewrite what already works.

## 1. Read the project first
Find the framework (Next.js, Vite, Remix, a Worker...), how model calls are made today, and the design tokens (background, foreground, primary, accent, border, surface, radius, font). Note them; you will pass them as the theme.

## 2. Install
`npm install netra-artifacts` (peer deps: react and react-dom 18+; no other runtime dependencies).

## 3. The server route (one file)
```ts
// app/api/chat/route.ts (Next.js; adapt the path for other frameworks)
import { createArtifactRoute } from "netra-artifacts/server";
import { streamText, generateText } from "ai"; // or any SDK that can stream text

export const POST = createArtifactRoute({
  options: {
    generateTextStream: ({ system, messages, temperature, abortSignal }) =>
      streamText({ model, system, messages, temperature, abortSignal }).textStream,
    generateText: async ({ system, prompt, temperature, abortSignal }) =>
      (await generateText({ model, system, prompt, temperature, abortSignal })).text,
    allowScripts: true,   // charts, narration, quizzes and 3D need it
    allowForms: true,
    followUps: true,
    theme: { colorScheme: "dark", background: "...", foreground: "...", primary: "...", accent: "...", border: "...", surface: "...", radius: "12px", fontFamily: "..." },
  },
});
```
`generateText` is optional but recommended: it powers the intent second opinion, the review harness and the 3D look-and-fix loop. Keep API keys on the server. Rate-limit the route like any model endpoint.

## 4. The review route (self-checking visuals)
```ts
// app/api/review/route.ts
import { createReviewResponse } from "netra-artifacts/server";
export async function POST(req: Request) {
  return createReviewResponse(await req.json(), { generateText, sanitize: { allowForms: true, allowScripts: true, allowInlineStyles: true, allowStyleTags: true, allowSvg: true, allowExternalFonts: true, allowVideoEmbeds: false, allowModuleImports: false } });
}
```

## 5. The chat surface
Quickest: `import { ArtifactChat } from "netra-artifacts/client"` and render `<ArtifactChat endpoint="/api/chat" visualReview={{ endpoint: "/api/review" }} />`.
Custom layout: `useArtifactStream({ endpoint, visualReview: { endpoint: "/api/review" }, initialMessages, initialArtifacts })`, then render each message's `parts` in order (text as Markdown, `ui` parts with `HtmlArtifactCard` wrapped in `ArtifactBoundary`, `presentation: "seamless"` and your `theme`). Show `message.stages` with `AgentTrailView` (who worked on the answer). Add `MotionControl` (animation speed and reduced motion) somewhere in the chrome. Handle `onAction({ action: "ask", payload: { text } })` by sending `text` as the next message.
Persist `messages` and `artifacts` (IndexedDB is a good fit) and pass them back as `initialMessages` / `initialArtifacts` so conversations reopen exactly as they were; 3D scenes then stay editable by id.

## 6. Rules
- Model output renders only through `HtmlArtifactCard` (sandboxed iframe, scripts only). Never `dangerouslySetInnerHTML` it and never add `allow-same-origin`.
- Customise through options, not forks: `instructions`, `plugins`, `templates`, `preferences` (animation, narration, 3D, research), `planner`, `review`, `review3d`.
- Use the project's real tokens for the theme so visuals look native.

## 7. Verify (do not skip)
Typecheck and build. Then send through the real route and look at each result in the browser:
`hi` (plain text) · `explain how quicksort works` (animated step-through) · `build a 3D bicycle and show how the wheels turn` (builds live, numbered parts, click a label) · `now add a person next to it` (edits the same scene in place) · `show how a rocket goes from the launch pad to space` (keyframe animation with sky and camera follow) · `quiz me on photosynthesis` (live quiz) · `explain this with no animations` (still visuals).
Report what you changed, the files, and anything you could not verify.
