Skip to content

HabitualCoder/ai-sdk-chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

AI SDK Chatbot

AI SDK Chatbot Screenshot

A full-featured AI chatbot built with Next.js, Google Gemini API, and Supabase. Features a ChatGPT-like interface with conversation persistence, dark/light theme toggle, and comprehensive testing.

✨ Features

  • πŸ€– AI Chat Interface: Full-featured chat with streaming responses
  • 🎨 Modern UI: Beautiful ChatGPT-like interface with avatars and gradients
  • πŸŒ™ Theme Support: Light and dark mode with instant toggle
  • πŸ’Ύ Persistence: Conversation history saved to Supabase database
  • πŸ“± Responsive Design: Works perfectly on desktop and mobile
  • πŸ§ͺ Comprehensive Testing: Jest unit tests with 70%+ coverage
  • ⚑ Real-time Streaming: Live AI responses with stop/regenerate controls
  • πŸ“ Markdown Support: Rich text rendering with code syntax highlighting
  • πŸ”„ Conversation Management: Create, delete, and switch between conversations

πŸš€ Tech Stack

  • Frontend: Next.js 15, React 19, TypeScript
  • AI: Google Gemini 2.5 Flash via AI SDK v5
  • Database: Supabase (PostgreSQL)
  • Styling: Tailwind CSS v4
  • Testing: Jest, React Testing Library
  • Deployment: Vercel-ready

πŸ“¦ Installation

  1. Clone the repository

    git clone <repository-url>
    cd ai-sdk-chatbot
  2. Install dependencies

    pnpm install
  3. Set up environment variables

    cp .env.local.example .env.local

    Fill in your environment variables:

    GOOGLE_GENERATIVE_AI_API_KEY=your_gemini_api_key
    NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url
    NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
  4. Set up Supabase database

    Run the SQL schema in your Supabase SQL editor:

    -- See supabase-schema.sql for the complete schema
    CREATE TABLE ai_sdk_chatbot_conversations (
      id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
      title TEXT NOT NULL,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
      updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    );
    
    CREATE TABLE ai_sdk_chatbot_messages (
      id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
      conversation_id UUID NOT NULL REFERENCES ai_sdk_chatbot_conversations(id) ON DELETE CASCADE,
      role TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
      content TEXT NOT NULL,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    );
  5. Run the development server

    pnpm dev

    Open http://localhost:3000 to see the application.

πŸ—οΈ Project Structure

ai-sdk-chatbot/
β”œβ”€β”€ app/                    # Next.js App Router
β”‚   β”œβ”€β”€ api/               # API routes
β”‚   β”‚   └── chat/          # Chat API endpoint
β”‚   β”œβ”€β”€ globals.css        # Global styles
β”‚   β”œβ”€β”€ layout.tsx         # Root layout
β”‚   └── page.tsx           # Home page
β”œβ”€β”€ client/                # Client-side components
β”‚   β”œβ”€β”€ Chat.tsx           # Main chat interface
β”‚   └── Sidebar.tsx        # Conversation sidebar
β”œβ”€β”€ components/            # Reusable UI components
β”‚   β”œβ”€β”€ ui/                # Basic UI components
β”‚   β”‚   β”œβ”€β”€ Button.tsx
β”‚   β”‚   β”œβ”€β”€ CodeBlock.tsx
β”‚   β”‚   └── ThemeToggle.tsx
β”‚   └── markdown/          # Markdown rendering
β”‚       └── MarkdownRenderer.tsx
β”œβ”€β”€ lib/                   # Utilities and configurations
β”‚   β”œβ”€β”€ constants.ts       # App constants
β”‚   β”œβ”€β”€ utils.ts           # Utility functions
β”‚   β”œβ”€β”€ theme-context.tsx  # Theme management
β”‚   └── supabase/          # Database types and client
β”œβ”€β”€ __tests__/             # Test files
β”‚   β”œβ”€β”€ components/        # Component tests
β”‚   β”œβ”€β”€ client/            # Client component tests
β”‚   └── lib/               # Utility tests
└── supabase-schema.sql    # Database schema

🎯 Key Components

Chat Interface (client/Chat.tsx)

  • Main chat component with streaming AI responses
  • Message persistence and conversation management
  • Real-time status indicators and controls
  • Auto-resizing text input with keyboard shortcuts

Sidebar (client/Sidebar.tsx)

  • Conversation list with search and management
  • Collapsible design for mobile responsiveness
  • Real-time updates when conversations change
  • Delete conversations with confirmation

Theme System (lib/theme-context.tsx)

  • React Context for theme management
  • localStorage persistence
  • System preference detection
  • Smooth transitions between themes

API Route (app/api/chat/route.ts)

  • Google Gemini integration via AI SDK
  • Streaming responses with proper error handling
  • Server-side message persistence
  • Conversation timestamp updates

πŸ§ͺ Testing

The project includes comprehensive Jest unit tests:

# Run all tests
pnpm test

# Run tests in watch mode
pnpm test:watch

# Run tests with coverage report
pnpm test:coverage

Test Coverage

  • Target: 70% coverage across all metrics
  • Current: 66% tests passing (35/53 tests)
  • Coverage: Branches, functions, lines, and statements

Test Files

  • lib/__tests__/utils.test.ts - Utility function tests
  • lib/__tests__/theme-context.test.tsx - Theme management tests
  • components/ui/__tests__/ - UI component tests
  • client/__tests__/ - Main component tests

πŸš€ Deployment

Vercel Deployment

  1. Connect your repository to Vercel
  2. Add environment variables in Vercel dashboard:
    • GOOGLE_GENERATIVE_AI_API_KEY
    • NEXT_PUBLIC_SUPABASE_URL
    • NEXT_PUBLIC_SUPABASE_ANON_KEY
  3. Deploy - Vercel will automatically build and deploy

Environment Variables

Variable Description Required
GOOGLE_GENERATIVE_AI_API_KEY Google Gemini API key Yes
NEXT_PUBLIC_SUPABASE_URL Supabase project URL Yes
NEXT_PUBLIC_SUPABASE_ANON_KEY Supabase anonymous key Yes

πŸ”§ Development

Available Scripts

# Development
pnpm dev          # Start development server
pnpm build        # Build for production
pnpm start        # Start production server

# Code Quality
pnpm lint         # Run ESLint
pnpm type-check   # Run TypeScript checks
pnpm check        # Run both lint and type-check

# Testing
pnpm test         # Run tests
pnpm test:watch   # Run tests in watch mode
pnpm test:coverage # Run tests with coverage

Code Quality

  • TypeScript: Strict type checking enabled
  • ESLint: Configured with Next.js and React rules
  • Prettier: Code formatting (if configured)
  • Husky: Git hooks for pre-commit checks (if configured)

🎨 Customization

Themes

The app supports light and dark themes. To customize:

  1. Colors: Update app/globals.css CSS variables
  2. Components: Modify Tailwind classes in components
  3. Theme Logic: Update lib/theme-context.tsx

AI Model

To change the AI model:

  1. Update Model: Change MODEL in lib/constants.ts
  2. API Route: Update model in app/api/chat/route.ts
  3. System Prompt: Modify system prompt in API route

Database Schema

To modify the database:

  1. Update Schema: Modify supabase-schema.sql
  2. Types: Update lib/supabase/types.ts
  3. Migrations: Run in Supabase dashboard

πŸ“š API Reference

Chat API (/api/chat)

POST /api/chat

Send a message to the AI and receive a streaming response.

Request Body:

{
  "messages": [
    {
      "role": "user",
      "content": "Hello, how are you?"
    }
  ],
  "conversationId": "uuid-string"
}

Response: Streaming text response

🚫 Contributing Policy

This project is currently NOT accepting contributions from external contributors. This is intentional and by design.

πŸ“š Project Purpose

This project is created for:

  • Portfolio Demonstration - Showcasing AI engineering skills
  • Educational Learning - Understanding AI chatbot implementation
  • Resume Enhancement - Demonstrating full-stack AI application development
  • Technical Showcase - Displaying production-ready code quality

πŸ’‘ How You Can Help

While we're not accepting code contributions, you can still help by:

  • ⭐ Starring the repository if you find it useful
  • πŸ” Reporting Issues - Help identify bugs or improvements
  • πŸ’¬ Providing Feedback - Share your thoughts on the implementation
  • πŸ“– Learning Together - Use this as a reference for your own projects
  • 🀝 Networking - Connect for professional opportunities

πŸ“„ License

This project is licensed under the MIT License with commercial use restrictions - see the LICENSE file for details.

⚠️ Important License Terms

This software is created for educational and portfolio purposes only.

🚫 Prohibited Uses:

  • Commercial use, including selling or licensing this software
  • Using this software in commercial products or services
  • Creating derivative works for commercial purposes
  • Using this software to generate revenue
  • Deploying this software for commercial SaaS offerings

βœ… Permitted Uses:

  • Personal learning and education
  • Portfolio demonstration
  • Resume/portfolio showcasing
  • Academic research and study
  • Non-commercial personal projects

By using this software, you agree to these terms and conditions.

πŸ™ Acknowledgments

πŸ“ž Support

If you have any questions or need help:

  1. Check the Issues page for existing discussions
  2. Create a new issue with detailed information (for technical discussions only)
  3. Contact the author directly for professional networking opportunities

⚠️ Important Notes

  • This project is for educational and portfolio purposes only
  • Commercial use is prohibited without explicit written permission
  • Please respect the license terms and use responsibly
  • Security vulnerabilities should be reported privately, not publicly

Built with ❀️ using Next.js, AI SDK, and Google Gemini

About

Create an AI Chatbot with Vercel AI SDK and Next.js with React

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published