
# Source Library
**A digital library of historical primary sources with AI-aided OCR, translation, and scholarly curation.**
[](https://www.gnu.org/licenses/agpl-3.0)
[](https://nextjs.org)
[](https://www.mongodb.com/cloud/atlas)
[](https://supabase.com)
[](https://ai.google.dev)
[๐ Visit Library](https://sourcelibrary.org) โข [๐ Docs](./docs) โข [๐บ๏ธ System Map](./.claude/docs/system-map.md) โข [๐ค Contribute](#contributing)
---
## ๐ฌ Experience Source Library
| ๐จ Frontend |
Next.js 16, React 19, TailwindCSS, Lucide icons |
| โ๏ธ Backend |
Next.js API routes, AWS Lambda for async processing |
| ๐พ Database |
MongoDB Atlas (primary), Supabase (embeddings) |
| ๐ค AI/ML |
Google Gemini 3.1 (OCR, translation, summarization) |
| ๐๏ธ Storage |
Vercel Blob (images), AWS S3 (archive), Cloudflare R2 (archive) |
| ๐ Auth |
NextAuth v5 with MongoDB adapter |
| ๐ DevOps |
Vercel (hosting), GitHub (VCS), Playwright (E2E tests) |
| โ๏ธ Testing |
Vitest (unit/integration), Playwright (E2E) |
| ๐ Search |
PostgreSQL FTS + semantic search via Supabase |
**๐ฆ Key Dependencies:**
- `sharp` โ Image resizing and cropping
- `@google/generative-ai` โ Gemini API integration
- `xml2js` โ USTC metadata parsing
- `@modelcontextprotocol/sdk` โ MCP server for agent integration
- `stripe` โ Donation and subscription handling
---
## ๐ Core Features
### ๐ Reading & Navigation
- **โก Page pagination** โ Instant navigation between 100+ pages
- **๐ Full-text search** โ Query across OCR'd text and translations
- **๐ Quote generation** โ Copy and cite passages with DOI links
### ๐ Processing Pipeline
- **๐งฉ Smart split detection** โ Automatic gutter detection for two-page spreads (Gemini AI or ML-based)
- **โ๏ธ High-accuracy OCR** โ Gemini Vision API with language-specific models (Latin, German, Greek, Arabic, etc.)
- **๐ฃ๏ธ Context-aware translation** โ Maintains continuity across pages for scholarly accuracy
- **๐ผ๏ธ Gallery extraction** โ AI-powered detection and cataloging of illustrations
### ๐จ Curation & Discovery
- **๐ Themed collections** โ Editorial collections (e.g., "Alchemy & Transmutation," "Kabbalah & Mysticism")
- **๐๏ธ Gallery browsing** โ Curated images from all books (museum-quality metadata)
- **๐ Related editions** โ Link across translations, reprints, and derivative works
- **๐ Authority linking** โ Connect to USTC, VIAF, and other scholarly databases
### ๐ค Scholarly Export
- **๐ฑ EPUB generation** โ Multi-format ebook export
- **๐ PDF with annotations** โ Preserve layout, add scholarly notes
- **๐ DOI minting** โ Version books via Zenodo integration for long-term citation
### ๐ข Tenant Subdomains
- **๐๏ธ Isolated reading rooms** โ Partners host curated subsets on custom domains (e.g., `bph.sourcelibrary.org`)
- **๐จ Branding & navigation** โ Full UI customization per tenant
- **๐ Access control** โ Public or members-only collections
---
## ๐๏ธ Architecture Overview
### ๐ Data Model
**๐ Books** contain structured metadata:
- **๐๏ธ Bibliographic** โ Title, author, language, publication date, USTC ID
- **๐ผ๏ธ Images** โ Links to source (Internet Archive, Gallica, etc.), archival status
- **โ๏ธ Processing** โ OCR status, translation language, extraction metadata
- **๐จ Curation** โ Collections, tier (featured/standard), visibility flags
**๐ Pages** store individual page data:
- **๐ธ Original image** โ Source photo or PDF page
- **โ๏ธ Split coordinates** โ Crop boundaries (0-1000 scale) for two-page spreads
- **โ๏ธ OCR output** โ Raw Gemini extraction + language metadata
- **๐ฃ๏ธ Translation** โ English translation with scholarly notes
- **๐ผ๏ธ Illustrations** โ Detected images with quality scores and descriptions
**๐ผ๏ธ Gallery images** are extracted illustrations:
- **๐ท๏ธ Metadata** โ Subject, figures, symbols, style, techniques, period
- **โญ Quality score** โ 0โ1.0 rating (filters below 0.5)
- **๐ Provenance** โ Source book and page, linked back
### ๐จ Image Tier System
All page images are resized on-demand via ``/api/image``:
| Tier | Dimensions | Quality | ๐ฑ Use Case |
|------|-----------|---------|----------|
| **Thumbnail** | 400px wide | 70% JPEG | Grids, navigation, social sharing |
| **Display** | 1200px wide | 80% JPEG | Main reading view, comfortable for annotation |
| **Full** | 2400px wide | 90% JPEG | Magnifier, fullscreen detail, printing |
Split pages are cropped non-destructively via coordinates; original images are always preserved.
### ๐ Processing Pipeline
```
๐ฅ Import โ โ๏ธ Split Detection โ โ๏ธ OCR โ ๐ฃ๏ธ Translation โ ๐จ Enrichment โ ๐ Publishing
```
1. **๐ฅ Import** โ Upload images, import from IA/Gallica via IIIF, or paste URLs
2. **โ๏ธ Split Detection** โ Detect two-page spreads; mark crop boundaries
3. **โ๏ธ OCR** โ Gemini Vision extracts text per page language
4. **๐ฃ๏ธ Translation** โ Gemini translates to English with prior-page context for continuity
5. **๐จ Enrichment** โ Extract illustrations, generate summaries, assign collections
6. **๐ Publishing** โ Set `visible: true`, mint DOI, push to search index
Batch endpoints process up to 5 pages/request using Gemini Batch API (50% cheaper).
### ๐ API Routes (Key)
**Base URL:** `https://sourcelibrary.org` (production) or `http://localhost:3000` (local dev with `.env.local` configured).
> **Common 404 mistake:** paths like `/api/bph/books` or `/api/bph/books/[id]` **do not exist**. BPH catalogue APIs live under `/api/embed/bph/...`. There is also **no** top-level `/api/[tenant]/books` route โ tenant book listings use `/api/books/library` or the embed routes below.
#### Public read APIs (no auth required today)
| Endpoint | Method | Purpose |
|----------|--------|---------|
| `/api/search?q=