Overview
The Invoice OCR project follows Next.js 15 App Router conventions with a clear separation between UI components, business logic, and API routes.Directory Structure
Core Directories
app/
Next.js App Router directory containing pages, layouts, and API routes.
API Routes:
app/api/ocr/route.ts- Raw text extraction from images and PDFsapp/api/ocr-structured/route.ts- Structured JSON extraction for MyBillBook schemaapp/api/ocr-structured-v4/route.ts- Normalized India GST output with reconciliationapp/api/models/route.ts- List available OpenRouter models
app/page.tsx- Main upload interface for OCR processingapp/review/page.tsx- LangFuse JSON review tool for debugging OCR responsesapp/compare/page.tsx- Side-by-side comparison interfaceapp/layout.tsx- Root layout with metadata and font configuration
components/
React components organized by feature and UI primitives.
Feature Components:
ocr-uploader.tsx- Handles file uploads (images/PDFs) with drag-and-dropinvoice-viewer.tsx- Displays compact schema invoice datainvoice-viewer-v4.tsx- Displays v4 normalized schema with reconciliation detailsconfetti.tsx- Celebration animation for successful operations
components/ui/):
Shadcn/ui components including buttons, labels, separators, and other primitives built on Radix UI.
lib/
Business logic and utility functions.
Core Logic:
invoice.ts- Compact schema reconciliation logicinvoice_v4.ts- v4 normalized schema with India GST validationutils.ts- Utility functions (e.g.,cn()for class merging)
lib/__tests__/):
invoice_v4.test.ts- Comprehensive tests for reconciliation enginestandards.test.ts- Standards compliance validation tests
public/
Static assets served directly by Next.js.
Configuration Files
TypeScript Configuration
tsconfig.json
- Strict mode enabled
- Path alias
@/*maps to repo root - App Router and React 19 compiler options
Next.js Configuration
next.config.ts
- App Router configuration
- Image optimization settings
- Environment variable handling
Testing Configuration
vitest.config.ts
- Globals enabled for test utilities
- Node environment for API testing
- Path alias support (
@/) - Coverage configuration (v8 provider)
- Includes
lib/**/*.tsfor coverage tracking
Styling Configuration
postcss.config.mjs
- Tailwind CSS v4 with
@tailwindcss/postcss
app/globals.css
- Tailwind directives
- CSS custom properties for theming
- shadcn/ui base styles
Linting Configuration
eslint.config.mjs
- Extends
next/core-web-vitals - TypeScript-aware linting rules
Key Files
Entry Points
app/layout.tsx- Root layout, metadata, and Geist fontapp/page.tsx- Main application interfaceapp/api/*/route.ts- API endpoint handlers
Business Logic
lib/invoice_v4.ts- Core reconciliation engine with:- Line item validation (quantity × rate)
- Discount calculations
- GST/tax computation (CGST, SGST, IGST)
- Additional charges (shipping, handling)
- Total reconciliation (tolerance ≤ ₹0.05)
Component Patterns
- Functional React components (TypeScript)
- Kebab-case filenames (
invoice-viewer-v4.tsx) - PascalCase component names
@/imports for all internal modules
Import Path Alias
All imports use the@/ alias to reference the repository root:
Environment Configuration
Environment variables are configured in.env.local (gitignored):
Required:
OPENROUTER_API_KEY- Your OpenRouter API key
OPENROUTER_MODEL- Model to use (default:openai/gpt-4o-mini, fallback:google/gemini-2.0-flash)OPENROUTER_PDF_ENGINE- PDF parser engine (pdf-text|mistral-ocr|native)OPENROUTER_SITE_URL- Your site URL for OpenRouter headersOPENROUTER_APP_NAME- App name for OpenRouter headers
Never commit secrets or
.env.local files. All sensitive configuration should remain local only.File Naming Conventions
- Components:
kebab-case.tsx(e.g.,invoice-viewer-v4.tsx) - Types: PascalCase for exported names
- Routes:
route.tsfor API endpoints - Pages:
page.tsxfor App Router pages - Tests:
*.test.tsinlib/__tests__/directory
Next Steps
Tech Stack
Learn about the technologies and frameworks used
Local Development
Set up your development environment
