ai-elements-chatbot
This skill provides production-ready AI chat UI components built on shadcn/ui for conversational AI interfaces. Use when building ChatGPT-style chat interfaces with streaming responses, tool/function call displays, reasoning visualization, or source citations. Provides 30+ components including Message, Conversation, Response, CodeBlock, Reasoning, Tool, Actions, Sources optimized for Vercel AI SDK v5. Prevents common setup errors with Next.js App Router, Tailwind v4, shadcn/ui integration, AI SDK v5 migration, component composition patterns, voice input browser compatibility, responsive design issues, and streaming optimization. Keywords: ai-elements, vercel-ai-sdk, shadcn, chatbot, conversational-ai, streaming-ui, chat-interface, ai-chat, message-components, conversation-ui, tool-calling, reasoning-display, source-citations, markdown-streaming, function-calling, ai-responses, prompt-input, code-highlighting, web-preview, branch-navigation, thinking-display, perplexity-style, claude-artifacts
NPX Install
npx skill4agent add jackspace/claudeskillz ai-elements-chatbotTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →AI Elements Chatbot Components
Quick Start (15 Minutes)
1. Verify Prerequisites
# Check Next.js version (needs 15+)
npx next --version
# Check AI SDK version (needs 5+)
npm list ai
# Check shadcn/ui is initialized
ls components/ui # Should exist with button.tsx etc- AI Elements is built ON TOP of shadcn/ui (won't work without it)
- Requires Next.js App Router (Pages Router not supported)
- AI SDK v5 has breaking changes from v4
tailwind-v4-shadcnpnpm add ai@latest2. Install AI Elements CLI
# Initialize AI Elements in your project
pnpm dlx ai-elements@latest init
# Add your first components
pnpm dlx ai-elements@latest add message conversation response prompt-input- Components are copied into (NOT installed as npm package)
components/ui/ai/ - Full source code ownership (modify as needed)
- Registry URL must be correct in
components.json
3. Create Basic Chat Interface
// app/chat/page.tsx
'use client';
import { useChat } from 'ai/react';
import { Conversation } from '@/components/ui/ai/conversation';
import { Message } from '@/components/ui/ai/message';
import { MessageContent } from '@/components/ui/ai/message-content';
import { Response } from '@/components/ui/ai/response';
import { PromptInput } from '@/components/ui/ai/prompt-input';
export default function ChatPage() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
api: '/api/chat'
});
return (
<div className="flex h-screen flex-col">
<Conversation className="flex-1">
{messages.map((msg) => (
<Message key={msg.id} role={msg.role}>
<MessageContent>
<Response markdown={msg.content} />
</MessageContent>
</Message>
))}
</Conversation>
<PromptInput
value={input}
onChange={handleInputChange}
onSubmit={handleSubmit}
disabled={isLoading}
/>
</div>
);
}- ✅ Streaming markdown rendering
- ✅ Auto-scrolling conversation
- ✅ Auto-resizing input
- ✅ Role-based message styling
The 5-Step Setup Process
Step 1: Install AI Elements CLI
pnpm dlx ai-elements@latest init- Creates directory
components/ui/ai/ - Updates with AI Elements registry
components.json - Adds necessary dependencies to package.json
- Run from project root (where package.json is)
- Requires shadcn/ui already initialized
- Will fail if missing (run
components.jsonfirst)pnpm dlx shadcn@latest init
Step 2: Add Core Chat Components
# Essential components for basic chat
pnpm dlx ai-elements@latest add message message-content conversation response
# Optional: Input component
pnpm dlx ai-elements@latest add prompt-input actions suggestion- : Container for single message (user/AI)
message - : Wrapper for message parts
message-content - : Auto-scrolling chat container
conversation - : Markdown renderer (streaming-optimized)
response - : Auto-resizing textarea with toolbar
prompt-input - : Copy/regenerate/edit buttons
actions - : Quick prompt pills
suggestion
Step 3: Add Advanced Components (Optional)
# For tool calling
pnpm dlx ai-elements@latest add tool
# For reasoning display (Claude/o1 style)
pnpm dlx ai-elements@latest add reasoning
# For source citations (Perplexity style)
pnpm dlx ai-elements@latest add sources inline-citation
# For code highlighting
pnpm dlx ai-elements@latest add code-block
# For conversation branching
pnpm dlx ai-elements@latest add branch
# For task lists
pnpm dlx ai-elements@latest add task
# For AI-generated images
pnpm dlx ai-elements@latest add image
# For web previews (Claude artifacts style)
pnpm dlx ai-elements@latest add web-preview
# For loading states
pnpm dlx ai-elements@latest add loader- : Building AI assistants with function calling
tool - : Showing AI's thinking process (like Claude or o1)
reasoning - : Adding citations and references (like Perplexity)
sources - : Chat includes code snippets
code-block - : Multi-turn conversations with variations
branch - : AI generates task lists with file references
task - : AI generates images (DALL-E, Stable Diffusion)
image - : AI generates HTML/websites (Claude artifacts)
web-preview - : Show loading states during streaming
loader
Step 4: Create API Route
/app/api/chat/route.tsimport { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai('gpt-4o'),
messages,
});
return result.toDataStreamResponse();
}- Must use AI SDK v5 (not v4
streamText())OpenAIStream() - Returns for streaming
toDataStreamResponse() - Client auto-receives updates via hook
useChat()
Step 5: Verify Installation
# Check components installed
ls components/ui/ai/
# Expected output:
# message.tsx, message-content.tsx, conversation.tsx, response.tsx, prompt-input.tsx, ...
# Start dev server
pnpm dev
# Test chat interface at http://localhost:3000/chat- All components in
components/ui/ai/ - has AI Elements registry
components.json - No TypeScript errors
- Chat interface renders
- Streaming works (type message → get response)
- Auto-scroll works during streaming
Critical Rules
Always Do
'use client'Never Do
'use client'Known Issues Prevention
Issue #1: PromptInputSpeechButton Not Working (Firefox/Safari)
SpeechRecognition is not defined// Conditionally show voice button
const isSpeechSupported = typeof window !== 'undefined' &&
('webkitSpeechRecognition' in window || 'SpeechRecognition' in window);
<PromptInput
enableSpeech={isSpeechSupported}
// Fallback: Implement server-side STT (Whisper, Google Speech)
/>Issue #2: PromptInput Not Responsive on Mobile
<PromptInput
className="min-h-[100px] sm:min-h-[60px]" // Add responsive classes
// ... other props
/>Issue #3: Multiple Thinking Elements in Long Responses
// Merge reasoning chunks client-side
const processedMessages = messages.map(msg => {
if (msg.annotations?.reasoning) {
// Combine all reasoning into single string
const merged = Array.isArray(msg.annotations.reasoning)
? msg.annotations.reasoning.join('\n\n')
: msg.annotations.reasoning;
return { ...msg, annotations: { ...msg.annotations, reasoning: merged } };
}
return msg;
});Issue #4: Component Not Found After Installation
Cannot find module '@/components/ui/ai/message'# Verify components.json has correct registry
cat components.json | grep -A5 "ai-elements"
# Re-initialize if missing
pnpm dlx ai-elements@latest init
# Check components directory exists
ls components/ui/ai/Issue #5: AI SDK v5 Breaking Changes
// ✅ v5 (correct)
const { messages } = useChat(); // Direct messages array
msg.toolInvocations // Tool calls here
// ❌ v4 (incorrect)
const { data } = useChat(); // Wrapped in data object
msg.tool_calls // Different property nameIssue #6: Maximum Update Depth Exceeded
// Memoize callbacks to prevent re-renders
import { useCallback } from 'react';
const { messages } = useChat({
api: '/api/chat',
onFinish: useCallback((message) => {
console.log('Finished', message);
}, []) // Stable dependency array
});Issue #7: Copy Button Returns HTML Instead of Markdown
// Pass raw markdown content, not rendered HTML
<Actions>
<Actions.Copy content={msg.content} format="markdown" />
</Actions>Issue #8: Tailwind v4 CSS Variable Issues
# Use tailwind-v4-shadcn skill to fix, or manually verify:
# Check src/index.css has:
@import "tailwindcss";
:root {
--background: hsl(0 0% 100%);
--foreground: hsl(0 0% 3.9%);
/* ... other variables */
}
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
}Configuration Files Reference
components.json (AI Elements Registry)
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "new-york",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "zinc",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui",
"lib": "@/lib",
"hooks": "@/hooks"
},
"registries": {
"shadcn": "https://ui.shadcn.com/registry",
"ai-elements": "https://www.shadcn.io/ai/registry"
}
}- - AI Elements are client components (not RSC compatible)
"rsc": false - - TypeScript required for type safety
"tsx": true - - Tailwind v4 doesn't use config file
"tailwind.config": "" - - Both shadcn and AI Elements registries needed
registries
Common Patterns
Pattern 1: Basic Chat with Actions
'use client';
import { useChat } from 'ai/react';
import { Conversation, Message, MessageContent, Response, Actions } from '@/components/ui/ai';
import { PromptInput } from '@/components/ui/ai/prompt-input';
export default function ChatWithActions() {
const { messages, input, handleInputChange, handleSubmit, reload, stop } = useChat();
return (
<div className="flex h-screen flex-col">
<Conversation className="flex-1">
{messages.map((msg) => (
<Message key={msg.id} role={msg.role}>
<MessageContent>
<Response markdown={msg.content} />
{msg.role === 'assistant' && (
<Actions>
<Actions.Copy content={msg.content} />
<Actions.Regenerate onClick={() => reload()} />
</Actions>
)}
</MessageContent>
</Message>
))}
</Conversation>
<PromptInput
value={input}
onChange={handleInputChange}
onSubmit={handleSubmit}
/>
</div>
);
}Pattern 2: Chat with Tool Calling
'use client';
import { useChat } from 'ai/react';
import { Tool } from '@/components/ui/ai/tool';
import { z } from 'zod';
export default function ChatWithTools() {
const { messages } = useChat({
api: '/api/chat',
async onToolCall({ toolCall }) {
if (toolCall.toolName === 'get_weather') {
// Execute tool
return { temperature: 72, conditions: 'Sunny' };
}
}
});
return (
<Conversation>
{messages.map((msg) => (
<Message key={msg.id} role={msg.role}>
<MessageContent>
{msg.content && <Response markdown={msg.content} />}
{/* Render tool invocations */}
{msg.toolInvocations?.map((tool) => (
<Tool
key={tool.toolCallId}
name={tool.toolName}
args={tool.args}
result={tool.result}
status={tool.state} // "pending" | "success" | "error"
/>
))}
</MessageContent>
</Message>
))}
</Conversation>
);
}Pattern 3: Chat with Reasoning Display
'use client';
import { useChat } from 'ai/react';
import { Reasoning } from '@/components/ui/ai/reasoning';
export default function ChatWithReasoning() {
const { messages, isLoading } = useChat({
api: '/api/chat',
streamProtocol: 'text'
});
return (
<Conversation>
{messages.map((msg, idx) => {
const reasoning = msg.annotations?.reasoning;
const isStreaming = isLoading && idx === messages.length - 1;
return (
<Message key={msg.id} role={msg.role}>
<MessageContent>
{reasoning && (
<Reasoning
content={reasoning}
streaming={isStreaming}
collapsed={!isStreaming} // Collapse after done
/>
)}
<Response markdown={msg.content} />
</MessageContent>
</Message>
);
})}
</Conversation>
);
}Pattern 4: Chat with Source Citations
'use client';
import { useChat } from 'ai/react';
import { Sources, InlineCitation } from '@/components/ui/ai';
export default function ChatWithSources() {
const { messages } = useChat({
api: '/api/chat'
});
return (
<Conversation>
{messages.map((msg) => {
const sources = msg.annotations?.sources || [];
return (
<Message key={msg.id} role={msg.role}>
<MessageContent>
<Response markdown={msg.content} />
{/* Show sources at bottom */}
{sources.length > 0 && (
<Sources
sources={sources}
citations={msg.annotations?.citations || []}
/>
)}
</MessageContent>
</Message>
);
})}
</Conversation>
);
}Pattern 5: Chat with Code Blocks
'use client';
import { useChat } from 'ai/react';
import { CodeBlock } from '@/components/ui/ai/code-block';
import { Response } from '@/components/ui/ai/response';
export default function ChatWithCode() {
const { messages } = useChat();
return (
<Conversation>
{messages.map((msg) => (
<Message key={msg.id} role={msg.role}>
<MessageContent>
{/* Response component auto-renders code blocks */}
<Response
markdown={msg.content}
components={{
// Optionally override code rendering
code: ({ language, code }) => (
<CodeBlock
language={language}
code={code}
showLineNumbers={true}
/>
)
}}
/>
</MessageContent>
</Message>
))}
</Conversation>
);
}Using Bundled Resources
Scripts (scripts/)
#!/bin/bash
# Automated AI Elements setup
# Check prerequisites
echo "Checking prerequisites..."
# Check Next.js
if ! command -v next &> /dev/null; then
echo "❌ Next.js not found. Install: pnpm add next"
exit 1
fi
# Check shadcn/ui
if [ ! -f "components.json" ]; then
echo "❌ shadcn/ui not initialized. Run: pnpm dlx shadcn@latest init"
exit 1
fi
# Check AI SDK
if ! grep -q '"ai"' package.json; then
echo "Installing AI SDK v5..."
pnpm add ai@latest
fi
# Initialize AI Elements
echo "Initializing AI Elements..."
pnpm dlx ai-elements@latest init
# Add core components
echo "Adding core chat components..."
pnpm dlx ai-elements@latest add message message-content conversation response prompt-input actions
echo "✅ AI Elements setup complete!"
echo "Next: Create /app/api/chat/route.ts for API endpoint"chmod +x scripts/setup-ai-elements.sh
./scripts/setup-ai-elements.shReferences (references/)
- - Complete list of all 30+ components with descriptions
references/component-catalog.md - - AI SDK v5 migration guide with all breaking changes
references/migration-v4-to-v5.md - - 10+ production-tested patterns
references/common-patterns.md
- Load when user asks "what components are available?"
component-catalog.md - Load when encountering v4 code or errors
migration-v4-to-v5.md - Load when building specific features (tool calling, reasoning, etc.)
common-patterns.md
Assets (assets/)
- - Complete working chat page template
assets/chat-interface-starter.tsx - - API route with all AI SDK features (tools, reasoning, streaming)
assets/api-route-template.ts - - Complete components.json with AI Elements registry
assets/components.json
- Copy when creating new chat page
chat-interface-starter.tsx - Copy when setting up API endpoint
api-route-template.ts - Copy when initializing new project
components.json
Advanced Topics
Performance Optimization with Virtualization
import { useVirtualizer } from '@tanstack/react-virtual';
import { useRef } from 'react';
function VirtualizedChat({ messages }) {
const parentRef = useRef<HTMLDivElement>(null);
const virtualizer = useVirtualizer({
count: messages.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 150, // Average message height
overscan: 5 // Render 5 extra items for smooth scrolling
});
return (
<div ref={parentRef} className="h-screen overflow-y-auto">
<div style={{ height: `${virtualizer.getTotalSize()}px`, position: 'relative' }}>
{virtualizer.getVirtualItems().map((virtualRow) => (
<div
key={virtualRow.index}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start}px)`
}}
>
<Message {...messages[virtualRow.index]} />
</div>
))}
</div>
</div>
);
}Custom Styling with CSS Variables
/* src/index.css - Customize AI message colors */
:root {
/* AI message background */
--ai-message: hsl(var(--muted));
--ai-message-foreground: hsl(var(--muted-foreground));
/* User message background */
--user-message: hsl(var(--primary));
--user-message-foreground: hsl(var(--primary-foreground));
/* Tool call colors */
--tool-success: hsl(142 76% 36%);
--tool-error: hsl(var(--destructive));
--tool-pending: hsl(47 91% 58%);
}
.dark {
--ai-message: hsl(var(--muted));
--ai-message-foreground: hsl(var(--muted-foreground));
/* ... dark mode variants */
}Accessibility Best Practices
// Add ARIA labels to interactive elements
<PromptInput
aria-label="Chat message input"
aria-describedby="chat-instructions"
/>
<Actions>
<Actions.Copy
aria-label="Copy message to clipboard"
aria-live="polite" // Announce copy success
/>
<Actions.Regenerate
aria-label="Regenerate AI response"
/>
</Actions>
// Screen reader announcements for streaming
<Response
markdown={content}
aria-live="polite" // Announce updates during streaming
aria-atomic="false" // Only announce changes, not entire content
/>Server-Side Tool Execution
// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai';
import { streamText, tool } from 'ai';
import { z } from 'zod';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai('gpt-4o'),
messages,
tools: {
get_weather: tool({
description: 'Get current weather for a location',
parameters: z.object({
location: z.string().describe('City name'),
unit: z.enum(['celsius', 'fahrenheit']).default('celsius')
}),
execute: async ({ location, unit }) => {
// Execute server-side (secure API keys)
const response = await fetch(`https://api.weather.com/...`);
const data = await response.json();
return { temperature: data.temp, conditions: data.conditions };
}
}),
search_database: tool({
description: 'Search internal database',
parameters: z.object({
query: z.string()
}),
execute: async ({ query }) => {
// Direct database access (server-side only)
const results = await db.search(query);
return results;
}
})
}
});
return result.toDataStreamResponse();
}- Secure API keys (never exposed to client)
- Direct database access
- Rate limiting and validation
- Audit logging
Dependencies
- - Vercel AI SDK for streaming and hooks
ai@^5.0.0 - - Next.js with App Router
next@^15.0.0 - - React 19 with concurrent rendering
react@^19.0.0 - - Tailwind CSS v4 Vite plugin
@tailwindcss/vite@^4.0.0
tailwindcss@^4.0.0- (various Radix UI primitives)
@radix-ui/react-* class-variance-authorityclsxtailwind-merge
- - Virtualization for long conversations
@tanstack/react-virtual@^3.0.0 - - Code syntax highlighting (alternative to Prism)
shiki@^1.0.0 - - LaTeX math rendering
katex@^0.16.0 - - Markdown rendering (if customizing Response component)
react-markdown@^9.0.0
Official Documentation
- AI Elements: https://www.shadcn.io/ai
- Vercel AI SDK: https://sdk.vercel.ai/docs
- shadcn/ui: https://ui.shadcn.com
- Next.js: https://nextjs.org/docs
- Context7 Library ID: (if available)
/vercel/ai-elements - GitHub Repository: https://github.com/vercel/ai-elements
Package Versions (Verified 2025-11-07)
{
"dependencies": {
"ai": "^5.0.0",
"next": "^15.0.0",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"@tailwindcss/vite": "^4.1.14"
},
"devDependencies": {
"ai-elements": "1.6.0",
"typescript": "^5.6.0",
"@types/react": "^19.0.0",
"@types/node": "^20.0.0"
}
}- AI Elements 1.6.0 released 2025-11-07 (same day as this skill)
- AI SDK v5 is required (v4 incompatible)
- React 19 required for concurrent rendering features
- Tailwind v4 required (v3 incompatible)
Production Example
- Without skill: ~25,000 tokens (researching components, integration patterns, debugging)
- With skill: ~8,000 tokens (direct implementation from templates)
- Savings: 68%
- Voice input browser compatibility
- Responsive design issues
- Reasoning block duplication
- AI SDK v5 migration errors
- Component discovery issues
- Re-render loop errors
- Copy functionality bugs
- Tailwind v4 CSS issues
Troubleshooting
Problem: Voice input button not working
// Check browser support
const supported = 'webkitSpeechRecognition' in window;
console.log('Speech supported:', supported); // false in Firefox/Safari
// Use only in supported browsers or implement server-side STT
<PromptInput enableSpeech={supported} />Problem: Components not found after installation
# Verify installation
ls components/ui/ai/ # Should show components
# Check registry in components.json
cat components.json | grep "ai-elements"
# Re-initialize if needed
pnpm dlx ai-elements@latest init
pnpm dlx ai-elements@latest add message conversation responseProblem: Streaming not working
// API route MUST return toDataStreamResponse()
return result.toDataStreamResponse(); // ✅ Correct
// NOT:
return result.toTextStreamResponse(); // ❌ Wrong format for AI ElementsProblem: Styling broken or missing colors
# Use tailwind-v4-shadcn skill to fix, or verify:
# 1. Check src/index.css has @import "tailwindcss" at top
# 2. Verify CSS variables defined in :root and .dark
# 3. Check @theme inline section exists
# 4. Ensure vite.config.ts has @tailwindcss/vite pluginComplete Setup Checklist
- Next.js 15+ installed with App Router
- AI SDK v5+ installed ()
pnpm add ai@latest - shadcn/ui initialized (exists)
components.json - Tailwind v4 configured with Vite plugin
- AI Elements initialized ()
pnpm dlx ai-elements@latest init - Core components added (message, conversation, response, prompt-input)
- API route created ()
/app/api/chat/route.ts - Chat page created with directive
'use client' - Dev server runs without TypeScript errors
- Chat interface renders correctly
- Streaming works (messages appear in real-time)
- Auto-scroll works during streaming
- Actions (copy/regenerate) work correctly
- Check for troubleshooting
references/common-issues.md - Verify all prerequisites (Next.js 15+, AI SDK v5+, shadcn/ui)
- Check official docs: https://www.shadcn.io/ai
- Ensure Tailwind v4 is configured correctly (use skill)
tailwind-v4-shadcn
Related Skills
- tailwind-v4-shadcn (prerequisite) - Sets up Tailwind v4 + shadcn/ui foundation
- ai-sdk-ui (companion) - AI SDK hooks (,
useChat,useCompletion)useAssistant - ai-sdk-core (optional) - Backend AI SDK integration for API routes
- nextjs (framework) - Next.js App Router setup
- clerk-auth (optional) - Add user authentication to chat
- cloudflare-d1 (optional) - Store chat history in database
1. nextjs skill → Next.js 15 setup
2. tailwind-v4-shadcn skill → UI foundation
3. ai-sdk-ui skill → AI hooks and state management
4. ai-elements-chatbot skill (this) → UI components
5. clerk-auth skill (optional) → User authentication