Skip to content

sean3687/RagEmailSearch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

69 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

What's Inside?

CleanShot 2025-09-24 at 20 49 58@2x

About Project

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.

๐Ÿ—๏ธ Monorepo Structure

This repository is built using Turborepo for efficient monorepo management, allowing seamless development across multiple applications and shared packages.

๐Ÿ“ฆ Applications

๐Ÿ“š Shared Packages

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

๐Ÿš€ Key Features

  • ๐Ÿ” 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

๐Ÿ—๏ธ Architecture

Backend Architecture Pattern

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           ๐ŸŒ API Layer              โ”‚
โ”‚     (Controllers + Routes)          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         ๐Ÿ›ก๏ธ Middleware Layer        โ”‚
โ”‚      (Validation + Security)       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         ๐Ÿง  Business Logic           โ”‚
โ”‚           (Services)               โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         ๐Ÿ—ƒ๏ธ Data Access              โ”‚
โ”‚        (Repositories)              โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         ๐Ÿ—„๏ธ Database Layer          โ”‚
โ”‚        (PostgreSQL)                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Backend Structure

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

Frontend Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           ๐ŸŒ App Layer              โ”‚
โ”‚        (Next.js Pages)             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         ๐Ÿงฉ Components               โ”‚
โ”‚      (Features + UI)               โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         ๐ŸŽฃ Hooks Layer              โ”‚
โ”‚      (State Management)             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         ๐Ÿ”ง Utilities                โ”‚
โ”‚      (Helpers + Types)             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Frontend Directory Structure

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 Strategy

Backend Testing (52+ Tests)

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

Frontend Testing Structure

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

๐Ÿ“ฆ Installation

yarn install

๐Ÿ“‚ Setup Database

This 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=

๐Ÿ“„ Scripts

Here are some useful scripts for development:

  • yarn dev - Start both the Next.js frontend and Express.js backend
  • yarn build - Build all apps and packages

๐Ÿ“‚ Contributing

If you find a bug or have a feature request, please open an issue or submit a pull request.


๐Ÿค License

This project is licensed under the MIT License.

RagSearchTool

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •