A sophisticated AI-powered chat application built with Next.js, Mastra framework, and Supabase that implements a multi-agent workflow for intelligent document retrieval and response generation.
- Multi-Agent Workflow: Orchestrated AI agents for query classification, document retrieval, and response generation
- RAG (Retrieval Augmented Generation): Semantic search through knowledge base using vector embeddings
- Real-time Chat Interface: Modern, responsive chat UI with streaming responses
- Authentication: Secure user authentication with Supabase Auth
- Vector Search: Advanced document similarity matching with configurable thresholds
- Multiple AI Providers: Support for Google Gemini, OpenAI, and Mistral models
├── app/ # Next.js app directory
│ ├── (auth)/ # Authentication routes (login, callback)
│ ├── api/ # API routes and endpoints
│ ├── globals.css # Global styles
│ ├── layout.tsx # Root layout component
│ └── page.tsx # Home page component
├── components/ # Reusable React components
│ ├── chat/ # Chat-related components (interface, message bar)
│ ├── common/ # Shared UI components (sidebar)
│ └── ProtectedRoute.tsx # Route protection wrapper
├── hooks/ # Custom React hooks
│ └── useAuth.ts # Authentication hook
├── lib/ # Core library code
│ ├── mastra/ # Mastra framework configuration
│ │ ├── agents/ # AI agents (classifier, RAG, responder)
│ │ ├── tools/ # Custom tools (RAG tool for document retrieval)
│ │ ├── workflow/ # Multi-agent workflow orchestration
│ │ └── index.ts # Mastra instance configuration
│ ├── supabase.ts # Supabase client configuration
│ └── utils.ts # Utility functions
├── scripts/ # Database and testing scripts
│ ├── check-database.ts # Database connectivity verification
│ ├── debug-embeddings.ts # Embedding debugging utilities
│ ├── populate-embeddings.ts # Knowledge base seeding script
│ └── test-rag-tool.ts # RAG functionality testing
├── services/ # Business logic services
│ └── conversation.services.ts # Conversation management
├── supabase/ # Database schema and migrations
│ ├── complete-setup.sql # Complete database setup
│ ├── seed.sql # Sample data seeding
│ └── *.sql # Various SQL scripts
├── .env.local # Environment variables
├── package.json # Dependencies and scripts
└── README.md # Project documentation
- Frontend: Next.js 15, React 19, TypeScript, Tailwind CSS
- Backend: Hono API framework, Vercel Edge Runtime
- AI Framework: Mastra Core with multi-agent workflows
- Database: Supabase (PostgreSQL with vector extensions)
- AI Models: Google Gemini, OpenAI, Mistral
- Authentication: Supabase Auth
- Styling: Tailwind CSS with custom components
- Node.js 18+ and npm/pnpm
- Supabase account and project
- Google AI API key (for Gemini models)
- Optional: OpenAI or Mistral API keys
git clone <repository-url>
cd ai-agent
npm installCreate .env.local file:
# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key
# AI Provider Keys
GOOGLE_GENERATIVE_AI_API_KEY=your_google_ai_key
OPENAI_API_KEY=your_openai_key (optional)
MISTRAL_API_KEY=your_mistral_key (optional)
# Database
DATABASE_URL=your_supabase_database_urlRun the database setup scripts:
# Execute the SQL files in your Supabase SQL editor
# 1. supabase/complete-setup.sql
# 2. supabase/seed.sql (optional sample data)npm run seed:embeddings# Start Next.js development server
npm run dev
# Start Mastra development server (in another terminal)
npm run dev:mastraVisit http://localhost:3000 for the application and http://localhost:4111 for Mastra playground.
npm run dev- Start Next.js development servernpm run build- Build production applicationnpm run start- Start production servernpm run dev:mastra- Start Mastra development servernpm run build:mastra- Build Mastra workflowsnpm run seed:embeddings- Populate knowledge base with embeddingsnpm run verify:setup- Verify database and API setupnpm run test:rag- Test RAG functionalitynpm run check:database- Check database connectivity
- Query Classification Agent: Analyzes user queries to determine intent and category
- RAG Agent: Retrieves relevant documents from the knowledge base using semantic search
- Responder Agent: Generates contextual responses based on retrieved information
- Documents are chunked and embedded using Google's text-embedding-004 model
- Vector similarity search with configurable thresholds (0.6-0.8)
- Supports category filtering and result count limits
- Real-time document retrieval with similarity scoring
- Supabase Auth integration with email/password and social providers
- Protected routes with automatic redirection
- Session management with React hooks
- Row Level Security (RLS) policies in Supabase
- API route protection with authentication middleware
- Environment variable validation
- Secure token handling
- Chat Interface: Real-time messaging with streaming responses
- RAG Tool: Semantic document retrieval with vector search
- Multi-Agent System: Orchestrated AI workflow for complex queries
- Knowledge Base: Vector-enabled document storage and retrieval
- Authentication: Secure user management and session handling
The application is optimized for Vercel deployment with Edge Runtime support:
npm run build
# Deploy to Vercel or your preferred platform- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License.