A modern, feature-rich AI chatbot application powered by Google's Gemini AI with real-time streaming responses, persistent chat history, and an elegant user interface.
Built with Next.js 15, Shadcn UI, TypeScript, and Tailwind CSS.
- Overview
- Features
- High-Level Architecture
- Data Flow Diagram
- Tech Stack
- Project Structure
- Getting Started
- Environment Variables
- Usage
- API Routes
- State Management
- Contributing
AI Chat is a production-ready conversational AI application that provides users with an intelligent chat interface powered by Google's Gemini AI. The application features real-time streaming responses, persistent chat history using browser storage, dark/light theme support, and a fully responsive design that works seamlessly across all devices.
- π Real-time AI Streaming: Experience natural conversations with live response streaming
- πΎ Persistent Chat History: All conversations are saved locally and can be accessed anytime
- π¨ Modern UI/UX: Clean, intuitive interface built with Shadcn UI components
- π± Fully Responsive: Optimized experience on mobile, tablet, and desktop devices
- π Theme Support: Switch between dark and light modes based on preference
- β‘ High Performance: Built on Next.js 15 with optimized rendering and Turbopack
- β‘ Real-time Streaming Responses: AI responses stream live for a natural conversation experience
- π¬ Multi-Session Management: Create, manage, and switch between multiple chat sessions
- πΎ Persistent Storage: Chat history automatically saved using Zustand with localStorage
- π Session History: View all past conversations with timestamps and message counts
- ποΈ Chat Management: Delete individual sessions or clear all chat history
- π Modern Interface: Clean, intuitive design using Shadcn UI components
- π± Responsive Design: Fully optimized layouts for mobile, tablet, and desktop
- π Dark/Light Mode: Theme toggle with persistent preference
- π Message Actions: Copy AI responses to clipboard with visual feedback
- β³ Loading States: Visual indicators and animated loaders during AI processing
- β¬οΈ Auto-scroll: Automatically scrolls to the latest message in conversations
- π― Smart Input: Auto-expanding textarea with keyboard shortcuts (Enter to send, Shift+Enter for new line)
- π Error Handling: Graceful error messages with user-friendly feedback
- π¨ Syntax Highlighting: Code blocks in AI responses with proper syntax highlighting
- π Markdown Support: Full markdown rendering for formatted AI responses
- π Performance Optimized: Built with Next.js 15 App Router and Turbopack
- π³ Docker Ready: Containerized deployment with Docker and Docker Compose support
The application follows a modern three-tier architecture:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PRESENTATION LAYER β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Next.js 15 App Router (React 19) β β
β β - Server-Side Rendering (SSR) β β
β β - Client Components for Interactivity β β
β β - Shadcn UI Components + Tailwind CSS β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β APPLICATION LAYER β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β State Management (Zustand) β β
β β - Chat Store (messages, sessions, UI state) β β
β β - Persistent Storage (localStorage) β β
β β β β
β β Custom Hooks β β
β β - useChat (message handling, API calls) β β
β β - useTheme (dark/light mode) β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API LAYER β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Next.js API Routes (/api/chat) β β
β β - Stream Handler β β
β β - Response Parser β β
β β - Error Management β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EXTERNAL SERVICES β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Google Gemini AI API β β
β β - Model: gemini-1.5-flash β β
β β - Stream Generate Content β β
β β - Real-time Response Streaming β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
App Structure
βββ Layout (Root)
β βββ Theme Provider (Dark/Light)
β βββ Global Styles
β
βββ Pages
βββ Home (/)
βββ Hero Component
βββ ChatAppBar (Navigation & Controls)
β βββ New Chat Button
β βββ History Sidebar (Sheet)
β βββ Theme Toggle
β βββ More Options (Dropdown)
β
βββ MessageList (Conversation Display)
β βββ User Messages
β βββ AI Messages (with Markdown & Syntax Highlighting)
β
βββ ChatInput (Message Composition)
βββ Textarea (Auto-expanding)
βββ About Button
βββ Send Button
ββββββββββββββββ
β User Types β
β Message β
ββββββββ¬ββββββββ
β
βΌ
ββββββββββββββββββββββββ
β ChatInput Component β
β - Capture input β
β - Validate text β
ββββββββ¬ββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β useChat Hook β
β - Create user msg β
β - Add to store β
β - Clear input β
ββββββββ¬ββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Chat Store (Zustand)β
β - Update messages[] β
β - Create/update β
β session β
β - Save to localStorageβ
ββββββββ¬ββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β API Call β
β POST /api/chat β
β { prompt: text } β
ββββββββ¬ββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββ
β API Route Handler β
β - Validate request β
β - Connect to Gemini API β
β - Setup streaming β
ββββββββ¬ββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββ
β Google Gemini AI β
β - Process prompt β
β - Generate response β
β - Stream back tokens β
ββββββββ¬ββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββ
β Stream Processing β
β - Parse SSE events β
β - Extract text chunks β
β - Send to client β
ββββββββ¬ββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββ
β Client Stream Reader β
β - Accumulate chunks β
β - Update AI message in store β
β - Render progressively β
ββββββββ¬ββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββ
β MessageList Component β
β - Display streaming response β
β - Show typing indicator β
β - Apply markdown formatting β
ββββββββββββββββββββββββββββββββββ
βββββββββββββββββββ
β User Action β
β (New Chat, β
β Load History, β
β Delete) β
ββββββββββ¬βββββββββ
β
βΌ
ββββββββββββββββββββββββ
β ChatAppBar β
β - Trigger action β
ββββββββββ¬ββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Chat Store Actions β
β - startNewChat() β
β - loadChatSession() β
β - deleteChatSession()β
β - clearAllChats() β
ββββββββββ¬ββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Update State β
β - Modify sessions[] β
β - Update messages[] β
β - Change currentID β
ββββββββββ¬ββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Persist to Storage β
β - localStorage sync β
β - Zustand middlewareβ
ββββββββββ¬ββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β UI Update β
β - Re-render componentsβ
β - Update history listβ
β - Show active sessionβ
ββββββββββββββββββββββββ
| Technology | Version | Purpose |
|---|---|---|
| Next.js | 15.3.1 | React framework with App Router, SSR, and API routes |
| React | 19.0.0 | UI library for building interactive components |
| TypeScript | 5.x | Type-safe development with static typing |
| Tailwind CSS | 4.1.7 | Utility-first CSS framework for styling |
| Technology | Purpose |
|---|---|
| Shadcn UI | Pre-built accessible component library |
| Radix UI | Headless UI primitives for dialogs, dropdowns, sheets |
| Lucide React | Beautiful, consistent icon set |
| next-themes | Theme switching (dark/light mode) |
| Technology | Purpose |
|---|---|
| Zustand | Lightweight state management with persistence |
| React Hooks | Custom hooks for chat logic and API calls |
| Technology | Purpose |
|---|---|
| React Markdown | Render markdown content from AI responses |
| React Syntax Highlighter | Syntax highlighting for code blocks |
| DOMPurify | Sanitize HTML to prevent XSS attacks |
| Marked | Fast markdown parser |
| Technology | Purpose |
|---|---|
| Google Gemini AI API | Direct REST API integration with gemini-1.5-flash model |
| Server-Sent Events (SSE) | Real-time response streaming via fetch API |
| Technology | Purpose |
|---|---|
| ESLint | Code linting and quality checks |
| Turbopack | Fast development bundler for Next.js |
| Docker | Containerization for deployment |
AI-chat/
βββ app/ # Next.js App Router directory
β βββ api/ # API routes
β β βββ chat/ # Chat API endpoint
β β βββ route.ts # Gemini AI streaming handler
β βββ font/ # Custom fonts
β βββ layout.tsx # Root layout with theme provider
β βββ page.tsx # Home page component
β βββ globals.css # Global styles
β
βββ Components/ # React components
β βββ landing/ # Main landing/chat components
β β βββ hero.tsx # Main chat container
β β βββ appbar.tsx # Navigation bar with controls
β β βββ chatInput.tsx # Message input component
β β βββ messagelist.tsx # Message display with markdown
β β βββ about.tsx # About/info popup
β β βββ theme-provider.tsx # Theme context provider
β βββ ui/ # Reusable UI components (Shadcn)
β βββ button.tsx
β βββ dialog.tsx
β βββ dropdown-menu.tsx
β βββ sheet.tsx
β βββ scroll-area.tsx
β βββ alert-dialog.tsx
β
βββ stores/ # State management
β βββ chatstore.ts # Zustand store for chat state
β
βββ hooks/ # Custom React hooks
β βββ usechat.ts # Hook for chat functionality
β
βββ lib/ # Utility functions
β βββ utils.ts # Helper utilities
β
βββ public/ # Static assets
β βββ crab.webp # About icon
β βββ debate.jpg # OG image
β
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose setup
βββ next.config.ts # Next.js configuration
βββ tsconfig.json # TypeScript configuration
βββ tailwind.config.ts # Tailwind CSS configuration
βββ components.json # Shadcn UI configuration
βββ package.json # Dependencies and scripts
βββ README.md # This file
Before you begin, ensure you have the following installed:
- Node.js 18.x or higher
- npm 9.x or higher (or yarn / pnpm)
- Docker (optional, for containerized deployment)
- Google Gemini API Key (free from Google AI Studio)
- Clone the repository
git clone https://github.com/theanuragg/AI-chat.git
cd AI-chat- Install dependencies
npm install
# or
yarn install
# or
pnpm install- Set up environment variables
Create a .env.local file in the root directory:
touch .env.localAdd your Gemini API key:
GEMINI_API_KEY=your_api_key_here- Run the development server
npm run dev
# or
yarn dev
# or
pnpm dev- Open your browser
Navigate to http://localhost:3000
- Clone the repository
git clone https://github.com/theanuragg/AI-chat.git
cd AI-chat- Create environment file
echo "GEMINI_API_KEY=your_api_key_here" > .env.local- Build and run with Docker
# Using Docker directly
docker build -t ai-chat .
docker run -p 3000:3000 --env-file .env.local ai-chat
# Or using Docker Compose
docker-compose up -d- Access the application
Navigate to http://localhost:3000
# Build the application
npm run build
# Start the production server
npm startCreate a .env.local file in the root directory with the following variables:
| Variable | Description | Required | Default |
|---|---|---|---|
GEMINI_API_KEY |
Google Gemini API key for AI responses | β Yes | - |
- Visit Google AI Studio
- Sign in with your Google account
- Navigate to "Get API Key" section
- Create a new API key or use an existing one
- Copy the key and add it to your
.env.localfile
Note: The free tier of Gemini API includes generous rate limits suitable for development and small-scale production use.
- Start a conversation: Type your message in the input field and press Enter or click the send button
- View response: Watch as the AI response streams in real-time
- Copy responses: Click the copy icon on AI messages to copy the text to clipboard
- New Chat: Click the "New Chat" button in the app bar to start a fresh conversation
- View History: Click the "History" button to see all your past conversations
- Load Session: Click on any session in the history sidebar to continue that conversation
- Delete Session: Use the three-dot menu on any session to delete it
- Clear All: Use the "Clear All" option in the more menu to delete all chat history
Click the sun/moon icon in the app bar to toggle between light and dark modes. Your preference is saved automatically.
- Enter: Send message
- Shift + Enter: New line in message
- Escape: Close any open dialog or sheet
Sends a message to the Gemini AI and streams the response back.
Request Body:
{
"prompt": "Your message here"
}Response: Server-Sent Events (SSE) stream with the following format:
data: {"text": "chunk of text"}
data: {"text": "another chunk"}
data: [DONE]
Error Responses:
{
"error": "Error message"
}Status Codes:
200: Success (streaming)400: Bad request (missing prompt)500: Server error
The application uses Zustand for state management with persistent storage.
State Structure:
{
messages: Message[], // Current conversation messages
inputText: string, // Current input field value
showHeading: boolean, // Show/hide welcome heading
hasMessages: boolean, // Has any messages in current chat
isLoading: boolean, // API request in progress
showDebatePopup: boolean, // About popup visibility
chatSessions: ChatSession[], // All saved chat sessions
currentSessionId: string | null // Active session ID
}Key Actions:
addMessage(message): Add a new message to the current chatupdateLastMessage(updates): Update the last AI message (for streaming)startNewChat(): Clear current chat and start freshloadChatSession(id): Load a specific chat sessiondeleteChatSession(id): Delete a specific sessionclearAllChats(): Delete all chat history
Persistence:
The store automatically saves to localStorage with the key chat-storage. This includes:
- All chat sessions
- Current session ID
- Messages in the current session
- UI state (showHeading, hasMessages)
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow the existing code style and conventions
- Write meaningful commit messages
- Test your changes thoroughly
- Update documentation as needed
- Ensure all linting and type checks pass
This project is open source and available for educational and personal use.
- Google Gemini AI for providing the powerful AI model
- Vercel for Next.js and hosting platform
- Shadcn for the beautiful UI component library
- Radix UI for accessible component primitives
If you have any questions or need help, please:
- Open an issue on GitHub
- Check existing issues for solutions
- Review the documentation
Built with β€οΈ using Next.js and Gemini AI