RAG Email Search is an AI-powered application that enables semantic search through email conversations using advanced vector embeddings and retrieval-augmented generation (RAG) technology.
This repository is built using Turborepo for efficient monorepo management, allowing seamless development across multiple applications and shared packages.
web- A Next.js app for the frontend (Next.js Docs)api- An Express.js app for the backend (Express.js Docs)
@repo/ui- A React component library shared across the monorepo@repo/supabase- Supabase setup@repo/eslint-config- Shared ESLint configuration for consistent linting across packages@repo/typescript-config- Shared TypeScript configurations for consistency
- ๐ AI-Powered Email Search: Semantic search through email conversations using vector embeddings
- ๐ง RAG (Retrieval-Augmented Generation): Intelligent question-answering based on email content
- โก Real-time Streaming: Live search results with Server-Sent Events
- ๐ฏ Vector Search: Pinecone-powered semantic similarity matching
- ๐ค OpenAI Integration: Advanced AI responses using GPT models
- ๐ง Email Thread Analysis: Search through question-answer pairs in email threads (With Mock data)
- ๐ก๏ธ Enterprise Security: SQL injection prevention, input validation, and CORS protection
- ๐งช Comprehensive Testing: 52+ tests covering models, services, middleware, and repositories
- ๐ Type Safety: Full TypeScript implementation across frontend and backend
- ๐ Production Ready: Automated testing, pre-push hooks, and quality gates
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ API Layer โ
โ (Controllers + Routes) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ก๏ธ Middleware Layer โ
โ (Validation + Security) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ง Business Logic โ
โ (Services) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐๏ธ Data Access โ
โ (Repositories) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐๏ธ Database Layer โ
โ (PostgreSQL) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
apps/api/src/
โโโ __tests__/ # ๐งช Testing Infrastructure
โ โโโ controllers/ # Controller tests
โ โโโ middleware/ # Middleware tests
โ โโโ models/ # Model tests
โ โโโ repositories/ # Repository tests
โ โโโ services/ # Service tests
โ โโโ simple.test.ts # Basic functionality tests
โ
โโโ controllers/ # ๐ฎ Request Handlers
โ โโโ searchController.ts # Search API endpoints
โ
โโโ data/ # ๐ Static Data
โ โโโ email.json # Sample email data
โ
โโโ db/ # ๐๏ธ Database Layer
โ โโโ pgClient.ts # PostgreSQL connection
โ
โโโ errors/ # โ ๏ธ Error Handling
โ โโโ HttpException.ts # Custom error classes
โ
โโโ middleware/ # ๐ก๏ธ Security & Validation
โ โโโ validation.ts # Input validation & sanitization
โ โโโ rateLimiting.ts # Rate limiting middleware
โ
โโโ models/ # ๐ Data Models
โ โโโ metadata.ts # QAMetadata interface
โ โโโ openai.ts # OpenAI model definitions
โ โโโ search.ts # SearchMatch & EmailSource interfaces
โ
โโโ repositories/ # ๐๏ธ Data Access Layer
โ โโโ messages.ts # Message database operations
โ โโโ threads.ts # Thread database operations
โ
โโโ routes/ # ๐ฃ๏ธ API Routing
โ โโโ index.ts # Main router configuration
โ โโโ searchRoutes.ts # Search-specific routes
โ
โโโ scripts/ # ๐ง Utility Scripts
โ โโโ seed.ts # Database seeding script
โ
โโโ services/ # ๐ง Business Logic
โ โโโ mainSearchSevice.ts # Main search orchestration
โ โโโ promptServices.ts # AI prompt generation
โ โโโ ragServices.ts # RAG (Retrieval-Augmented Generation)
โ โโโ responseServices.ts # Response formatting
โ โโโ searchServices.ts # Vector search operations
โ
โโโ utils/ # ๐ง Utility Functions
โ โโโ openai.ts # OpenAI API integration
โ โโโ vector.ts # Vector database (Pinecone) integration
โ
โโโ index.ts # ๐ Application Entry Point
โโโ mainServer.ts # ๐ Express Server Configuration
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ App Layer โ
โ (Next.js Pages) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐งฉ Components โ
โ (Features + UI) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ฃ Hooks Layer โ
โ (State Management) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ง Utilities โ
โ (Helpers + Types) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
apps/web/
โโโ app/ # ๐ Next.js App Router
โ โโโ api/ # API Routes
โ โโโ chat/ # Chat API endpoint
โ
โโโ components/ # ๐งฉ React Components
โ โโโ __tests__/ # Component Tests
โ โ โโโ features/ # Feature component tests
โ โ โโโ ui/ # UI component tests
โ โโโ features/ # Feature Components
โ โโโ ui/ # Reusable UI Components
โ
โโโ hooks/ # ๐ฃ Custom React Hooks
โ
โโโ lib/ # ๐ง Utility Functions
โ
โโโ public/ # ๐ Static Assets
โ
โโโ types/ # ๐ TypeScript Types
โ
โโโ Configuration Files # โ๏ธ Project Setup
โโโ package.json
โโโ next.config.ts
โโโ tailwind.config.js
โโโ tsconfig.json
โโโ jest.config.js
Testing Categories:
- ๐ฌ Unit Tests - Isolated component testing
- ๐ Integration Tests - Component interaction testing
- ๐งช Service Tests - Business logic validation
Before Testing Implementation:
- โ No validation of data structures
- โ Manual testing required for every change
- โ Security vulnerabilities undetected
- โ No regression detection
- โ Type mismatches
Now with Comprehensive Testing:
- โ 52+ tests automatically validate your code
- โ Model tests ensure data structure integrity
- โ Security validated through middleware tests
- โ Database operations tested with proper mocking
- โ Pre-push hooks prevent broken code
- โ Input validation comprehensive
- โ Regression prevention through automated tests
- โ Husky automated testing with Git hooks
components/
โโโ __tests__/ # All test files are grouped here
โ โโโ features/ # Tests for feature-level components
โ โโโ ui/ # Tests for UI-level components
โ
โโโ features/ # Business or feature-related components
โ โโโ chat-form.tsx
โ
โโโ ui/ # Reusable UI components
โโโ accordion.tsx
โโโ button.tsx
โโโ chat-form.tsx
โโโ textarea.tsx
โโโ tooltip.tsx
Key Test Focus:
chat-form.test.tsx- Verifies the most important user flow: typing a message, submitting it, and seeing it rendered- Streaming Response Testing - Ensures streaming assistant responses are handled correctly
- User Experience Validation - Prevents silent breaks during future changes or refactors
yarn installThis section is for users who have just cloned the repository and need to set up the local database. This project uses Supabase for database management, with migrations handled through the Supabase CLI. Follow these steps to get your database up and running.
You can open up http://127.0.0.1:54323 to open supabase studio.
# for latest .env take a look at env.excample on each repo
DATABASE_URL=
SUPABASE_URL=http://localhost:54321
SUPABASE_KEY=
NODE_ENV=development
OPENAI_API_KEY=
Here are some useful scripts for development:
yarn dev- Start both the Next.js frontend and Express.js backendyarn build- Build all apps and packages
If you find a bug or have a feature request, please open an issue or submit a pull request.
This project is licensed under the MIT License.