Your Mechanical Switch Assistant Backend
███████╗██╗ ██╗██╗████████╗ ██████╗██╗ ██╗ █████╗ ██╗
██╔════╝██║ ██║██║╚══██╔══╝██╔════╝██║ ██║ ██╔══██╗██║
███████╗██║ █╗ ██║██║ ██║ ██║ ███████║ ███████║██║
╚════██║██║███╗██║██║ ██║ ██║ ██╔══██║ ██╔══██║██║
███████║╚███╔███╔╝██║ ██║ ╚██████╗██║ ██║ ██║ ██║██║
╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝
Backend service for the SwitchAI mechanical switch assistant, providing AI-powered chat, switch comparisons, and database management through a RESTful API.
- Runtime: Node.js with TypeScript
- Framework: Express.js
- Database: PostgreSQL with Drizzle ORM
- AI: Google Gemini / Anthropic Claude
- RAG: LangChain with hybrid search
- Infrastructure: Supabase
- Package Manager: pnpm
- Node.js (LTS version)
- pnpm (
npm install -g pnpm) - Supabase CLI (optional for local development)
# Clone and install
git clone <repository_url>
cd backend-switchai
pnpm install
# Setup environment
cp .env.example .env.local
# Edit .env.local with your configuration
# Start database (if using local Supabase)
pnpm supabase:start
# Run in development
pnpm devRequired environment variables in .env.local:
# Database
DATABASE_URL=postgresql://...
DATABASE_ANON_KEY=your-supabase-anon-key
# AI Provider (choose one)
GEMINI_API_KEY=your-gemini-key
CLAUDE_API_KEY=your-claude-key
IS_CLAUDE=false # Set to "true" to use Claude instead of Gemini
# Security
JWT_SECRET=your-secure-jwt-secret-min-32-chars
# LangChain (for RAG and tracing)
LANGCHAIN_API_KEY=your-langsmith-key
LANGCHAIN_PROJECT=SwitchAI-RAG-Evaluation
LANGCHAIN_TRACING_V2=true| Command | Description |
|---|---|
pnpm dev |
Start development server with hot reload |
pnpm start |
Start production server |
pnpm build |
Build for production |
pnpm db:migrate |
Run database migrations |
pnpm db:studio |
Open Drizzle Studio |
pnpm format |
Format code with Prettier |
src/
├── controllers/ # HTTP request handlers
├── services/ # Business logic
│ ├── chat.ts # Main chat service
│ ├── langchain/ # RAG implementation
│ └── llm.factory.ts # AI provider abstraction
├── db/ # Database schema and config
├── middleware/ # Security and validation
├── routes/ # API route definitions
└── utils/ # Helper utilities
The system uses LangChain Expression Language (LCEL) for retrieval-augmented generation:
Key Components:
langchain/wrappers.ts- Custom retriever and embeddings classeslangchain/chain.ts- LCEL pipeline with query re-phrasing and document reorderinglangchain/evaluation.ts- LangSmith integration for testing and evaluation
Flow:
- User query → Input sanitization
- Query embedding → Hybrid search (vector + keyword)
- Context retrieval → Document ranking
- LLM generation → Response validation
- LangSmith tracing → Performance monitoring
Public Endpoints:
GET /health- Health checkPOST /auth/login- User authenticationPOST /auth/register- User registration
Authenticated Endpoints:
POST /api/chat- AI chat with switch comparison detectionGET /api/conversations- List user conversationsGET/POST/PUT/DELETE /api/messages- Message managementGET/PUT/DELETE /api/users- User management
- Input Sanitization: 25+ pattern detection for prompt injection, XSS, SQL injection
- Rate Limiting: IP-based and user-based limits with burst protection
- PII Protection: Automatic detection and scrubbing of sensitive data
- CSP Headers: Content Security Policy implementation
- Secrets Management: Centralized configuration with validation
-- Core tables
users (id, email, name, role, created_at)
conversations (id, user_id, title, created_at)
messages (id, conversation_id, role, content, metadata)
switches (id, name, manufacturer, specifications, embeddings)pnpm test # All tests
pnpm test:security # Security tests
pnpm test:langchain # RAG pipeline testspnpm eval:setup # Create test datasets
pnpm eval:quick # Quick evaluation
pnpm eval:comprehensive # Full evaluation suitepnpm db:generate # Generate migrations
pnpm db:migrate # Apply migrations
pnpm db:studio # Database browser
pnpm db:reset # Reset database- Set production environment variables
- Build the application:
pnpm build - Run migrations:
pnpm db:migrate - Start the server:
pnpm start
- Fork the repository
- Create a feature branch
- Make changes with tests
- Run security checks:
pnpm audit:security - Submit a pull request
See LICENSE file for details.