Skip to content

theanuragg/AI-chat

Repository files navigation

AI Chat - Gemini Powered Chatbot πŸ€–

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.


πŸ“‹ Table of Contents


🎯 Overview

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.

Key Highlights

  • πŸš€ 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

✨ Features

Core Features

  • ⚑ 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

UI/UX Features

  • πŸ’Ž 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)

Technical Features

  • πŸ” 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

πŸ—οΈ High-Level Architecture

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                        β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Component Architecture

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

πŸ”„ Data Flow Diagram

User Message Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   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   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Chat Session Management Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  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β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Tech Stack

Frontend Framework & Core

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

UI Components & Design

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)

State & Data Management

Technology Purpose
Zustand Lightweight state management with persistence
React Hooks Custom hooks for chat logic and API calls

Content Rendering

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

AI Integration

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

Development Tools

Technology Purpose
ESLint Code linting and quality checks
Turbopack Fast development bundler for Next.js
Docker Containerization for deployment

πŸ“ Project Structure

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

πŸš€ Getting Started

Prerequisites

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)

Installation

Option 1: Local Development

  1. Clone the repository
git clone https://github.com/theanuragg/AI-chat.git
cd AI-chat
  1. Install dependencies
npm install
# or
yarn install
# or
pnpm install
  1. Set up environment variables

Create a .env.local file in the root directory:

touch .env.local

Add your Gemini API key:

GEMINI_API_KEY=your_api_key_here
  1. Run the development server
npm run dev
# or
yarn dev
# or
pnpm dev
  1. Open your browser

Navigate to http://localhost:3000

Option 2: Docker Deployment

  1. Clone the repository
git clone https://github.com/theanuragg/AI-chat.git
cd AI-chat
  1. Create environment file
echo "GEMINI_API_KEY=your_api_key_here" > .env.local
  1. 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
  1. Access the application

Navigate to http://localhost:3000

Option 3: Production Build

# Build the application
npm run build

# Start the production server
npm start

πŸ” Environment Variables

Create 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 -

Getting a Gemini API Key

  1. Visit Google AI Studio
  2. Sign in with your Google account
  3. Navigate to "Get API Key" section
  4. Create a new API key or use an existing one
  5. Copy the key and add it to your .env.local file

Note: The free tier of Gemini API includes generous rate limits suitable for development and small-scale production use.


πŸ’» Usage

Basic Chat

  1. Start a conversation: Type your message in the input field and press Enter or click the send button
  2. View response: Watch as the AI response streams in real-time
  3. Copy responses: Click the copy icon on AI messages to copy the text to clipboard

Managing Chat Sessions

  1. New Chat: Click the "New Chat" button in the app bar to start a fresh conversation
  2. View History: Click the "History" button to see all your past conversations
  3. Load Session: Click on any session in the history sidebar to continue that conversation
  4. Delete Session: Use the three-dot menu on any session to delete it
  5. Clear All: Use the "Clear All" option in the more menu to delete all chat history

Theme Switching

Click the sun/moon icon in the app bar to toggle between light and dark modes. Your preference is saved automatically.

Keyboard Shortcuts

  • Enter: Send message
  • Shift + Enter: New line in message
  • Escape: Close any open dialog or sheet

πŸ”Œ API Routes

POST /api/chat

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

πŸ—„οΈ State Management

The application uses Zustand for state management with persistent storage.

Chat Store (stores/chatstore.ts)

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 chat
  • updateLastMessage(updates): Update the last AI message (for streaming)
  • startNewChat(): Clear current chat and start fresh
  • loadChatSession(id): Load a specific chat session
  • deleteChatSession(id): Delete a specific session
  • clearAllChats(): 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)

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • 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

πŸ“„ License

This project is open source and available for educational and personal use.


πŸ™ Acknowledgments

  • 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

πŸ“ž Support

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

About

A modern, responsive chatbot interface powered by Google's Gemini AI with real-time streaming responses.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages