Architecture Overview
Uxie is built with a modern, scalable architecture leveraging Next.js, tRPC, Prisma, and cutting-edge AI technologies. This guide explains the core architectural decisions and how different parts of the system work together.Project Structure
The codebase follows a well-organized structure that separates concerns effectively:Hybrid Next.js Routing
Uxie uses both Next.js routing paradigms strategically:App Router
Used for modern AI API routes that leverage:
- Streaming responses with Vercel AI SDK
- Route handlers with
maxDurationsupport - Server-side streaming for chat and completions
src/app/api/ (src/app/api/chat/route.ts:1)Pages Router
Used for:
- tRPC API endpoints (
/api/trpc) - NextAuth.js authentication (
/api/auth) - Main application pages and UI routes
src/pages/ (src/pages/_app.tsx:1)tRPC Integration Pattern
Uxie uses tRPC for end-to-end type-safe APIs:Router Setup
The main router aggregates all feature routers:Context & Middleware
tRPC context provides access to session and database:Authentication Middleware
Authentication Middleware
Protected procedures use middleware to enforce authentication:This ensures type-safe access to user data in protected routes (src/server/api/trpc.ts:110).
Client Configuration
The tRPC client is configured with:- SuperJSON for serialization of Dates, Maps, Sets
- React Query for caching and optimistic updates
- HTTP batch link for request batching
Database Layer with Prisma
Prisma provides type-safe database access with PostgreSQL:Core Models
User & Authentication
Usermodel stores user profilesAccountmodel handles OAuth providers- NextAuth.js Prisma adapter manages sessions
Document Management
Documentstores PDF metadata and URLs- Tracks vectorization status (
isVectorised) - Links to highlights, messages, and flashcards
- Supports collaboration with
Collaboratormodel
Annotations & Highlights
Highlightstores PDF annotationsCordinatestores bounding boxes- Supports both text and image highlights
Connection Management
AI/RAG Architecture
Uxie implements Retrieval-Augmented Generation (RAG) for intelligent PDF interaction:Vectorization Pipeline
Text Chunking
Documents are split into overlapping chunks:This ensures semantic coherence across chunk boundaries (src/lib/vectorise.ts:86).
Semantic Search & Chat
The chat system uses tool-calling for retrieval:RAG Chat Flow
RAG Chat Flow
- User Question → Sent to chat API route
- Tool Invocation → LLM calls
getInformationtool - Vector Search → Query is embedded and matched against Pinecone:
- Context Injection → Top 4 results are passed back to LLM
- Response Generation → LLM generates answer based on context
- Streaming → Response streams to client using Vercel AI SDK
AI Model Configuration
Real-time Collaboration
Liveblocks Integration
Uxie uses Liveblocks for real-time collaboration features:
- Shared Cursors - See other users’ cursor positions
- Live Editing - Collaborative note editing with BlockNote
- Yjs Integration - CRDT-based conflict-free editing
File Storage
- PDFs - Stored on UploadThing CDN
- Embeddings - Stored in Pinecone (768 dimensions, cosine similarity)
- Metadata - Stored in Supabase PostgreSQL
Authentication Flow
NextAuth.js handles authentication with Google OAuth:- User clicks “Sign in with Google”
- OAuth flow redirects to Google
- Google callback returns user profile
- NextAuth creates/updates user in database via Prisma adapter
- Session cookie issued with JWT
Performance Optimizations
Webpack Customizations
Special handling for client-side libraries:
scribe.js-ocr- OCR processingkokoro-js- Local TTS models- Custom Terser config to avoid breaking ONNX runtime
React Query Caching
- Optimistic updates for highlights
- Background refetching for documents
- Automatic cache invalidation
Key Architectural Decisions
| Decision | Rationale |
|---|---|
| Hybrid Next.js routing | Leverage App Router for streaming AI while keeping tRPC on Pages Router |
| tRPC over REST | End-to-end type safety eliminates API contract bugs |
| Pinecone over alternatives | Managed vector database with excellent performance |
| HuggingFace embeddings | Free, high-quality embeddings without vendor lock-in |
| Prisma over raw SQL | Type-safe queries with excellent TypeScript support |
| Monorepo structure | Full-stack in one repository simplifies development |
Next Steps
Tech Stack
Explore the full technology stack and dependencies
Contributing
Learn how to set up your development environment
