Generative UI implementation patterns for AI SDK RSC including server-side streaming components, dynamic UI generation, and client-server coordination. Use when implementing generative UI, building AI SDK RSC, creating streaming components, or when user mentions generative UI, React Server Components, dynamic UI, AI-generated interfaces, or server-side streaming.
Limited to specific tools
Additional assets for this skill
This skill is limited to using the following tools:
examples/chart-generator.tsxscripts/validate-rsc-setup.shtemplates/client-wrapper.tsxtemplates/server-action-pattern.tsxPurpose: Provide reusable templates, patterns, and validation scripts for implementing Generative UI with Vercel AI SDK RSC (React Server Components).
Activation Triggers:
Key Resources:
templates/server-action-pattern.tsx - Server action template for AI RSCtemplates/streaming-component.tsx - Streaming component patterntemplates/client-wrapper.tsx - Client component wrapper patterntemplates/route-handler.ts - API route handler for streaming UIscripts/validate-rsc-setup.sh - Validate Next.js RSC configurationscripts/generate-ui-component.sh - Generate UI component from schemaexamples/ - Real-world generative UI implementationsWhen to use: Next.js App Router with React Server Components
Template: templates/server-action-pattern.tsx
Pattern:
'use server'
import { streamUI } from 'ai/rsc'
import { openai } from '@ai-sdk/openai'
export async function generateUI(prompt: string) {
const result = await streamUI({
model: openai('gpt-4')
prompt
text: ({ content }) => <p>{content}</p>
tools: {
// Tool definitions for dynamic UI generation
}
})
return result.value
}
Key features:
When to use: Need real-time UI updates during AI generation
Template: templates/streaming-component.tsx
Pattern:
When to use: Complex interactions between client and server components
Template: templates/client-wrapper.tsx
Pattern:
# Check Next.js version and App Router setup
./scripts/validate-rsc-setup.sh
Checks:
app/ directory existsDecision tree:
# Generate component from pattern
./scripts/generate-ui-component.sh <pattern-type> <component-name>
# Examples:
./scripts/generate-ui-component.sh stream-text MessageCard
./scripts/generate-ui-component.sh dynamic-tool ChartGenerator
./scripts/generate-ui-component.sh workflow MultiStepForm
Use template: templates/server-action-pattern.tsx
Customize:
Use template: templates/client-wrapper.tsx
For:
Pattern:
const tools = {
showChart: tool({
description: 'Display data as chart'
parameters: z.object({
data: z.array(z.number())
type: z.enum(['bar', 'line', 'pie'])
})
generate: async ({ data, type }) => {
return <ChartComponent data={data} type={type} />
}
})
showTable: tool({
description: 'Display data as table'
parameters: z.object({
rows: z.array(z.record(z.string()))
})
generate: async ({ rows }) => {
return <TableComponent rows={rows} />
}
})
}
Key principle: Let AI choose appropriate UI component based on data
const result = await streamUI({
// ... config
onError: (error) => {
return <ErrorBoundary error={error} />
}
fallback: <LoadingSpinner />
})
Best practices:
Use dynamic imports for heavy components:
const HeavyChart = dynamic(() => import('./HeavyChart'))
Control chunk size for optimal UX:
const result = await streamUI({
// ... config
experimental_streamChunking: true
})
Cache static UI components:
export const revalidate = 3600 // 1 hour
Critical: Never expose server actions to client
// ✅ Good: server action
'use server'
export async function generateUI() { /* ... */ }
// ❌ Bad: client-accessible
export async function generateUI() { /* ... */ }
Always sanitize AI-generated content:
import DOMPurify from 'isomorphic-dompurify'
const sanitized = DOMPurify.sanitize(aiContent)
File structure:
app/
actions/
generate-ui.ts # Server actions
components/
ui/
generated/ # Generated UI components
client-wrapper.tsx # Client components
api/
stream-ui/
route.ts # Alternative API route pattern
Required: Proper RSC types
// tsconfig.json
{
"compilerOptions": {
"jsx": "preserve"
"lib": ["dom", "dom.iterable", "esnext"]
"module": "esnext"
"moduleResolution": "bundler"
}
}
import { generateUI } from './actions'
test('generates UI from prompt', async () => {
const ui = await generateUI('Show weather chart')
expect(ui).toMatchSnapshot()
})
Use testing-patterns skill for comprehensive streaming tests
Use case: AI generates form fields dynamically
Template: examples/multi-step-form.tsx
Use case: AI selects appropriate chart type
Template: examples/chart-generator.tsx
Use case: AI creates dashboard widgets
Template: examples/dashboard-generator.tsx
Use case: AI generates content in card layouts
Template: examples/content-cards.tsx
Scripts:
validate-rsc-setup.sh - Verify Next.js and RSC configurationgenerate-ui-component.sh - Scaffold component from patternTemplates:
server-action-pattern.tsx - Complete server action templatestreaming-component.tsx - Streaming component patternclient-wrapper.tsx - Client component wrapperroute-handler.ts - API route alternative patternExamples:
multi-step-form.tsx - Complete multi-step form implementationchart-generator.tsx - Dynamic chart generationdashboard-generator.tsx - Full dashboard examplecontent-cards.tsx - Card-based content layoutSupported Frameworks: Next.js 13.4+ (App Router only) SDK Version: Vercel AI SDK 5+ with RSC support React Version: React 18+ (Server Components)
Best Practice: Always start with validate-rsc-setup.sh to ensure environment compatibility