|
| 1 | +# Architecture Overview |
| 2 | + |
| 3 | +## 1. Purpose and scope |
| 4 | +This document explains how LangRoute is structured and how data flows through the system. It complements the high‑level [README](../README.md) and setup notes in [docs/getting-started.md](./getting-started.md). |
| 5 | + |
| 6 | +## 2. High-level system overview |
| 7 | +LangRoute exposes an OpenAI‑compatible chat API and a Next.js dashboard for administrators. Requests pass through thin API routes into service modules that coordinate model adapters and data access. |
| 8 | + |
| 9 | +```mermaid |
| 10 | +flowchart LR |
| 11 | + Client[Web UI / SDK] |
| 12 | + API[/Next.js API route/] |
| 13 | + Service[Service layer] |
| 14 | + Provider[LLM provider] |
| 15 | + DB[(PostgreSQL)] |
| 16 | +
|
| 17 | + Client --> API --> Service --> Provider |
| 18 | + Service --> DB |
| 19 | +``` |
| 20 | + |
| 21 | +## 3. Components |
| 22 | +### Web UI |
| 23 | +- Next.js App Router under `src/app/(client)` with route groups for auth and dashboard pages. Layouts compose shared navigation and headers. |
| 24 | +- Global providers supply React Query, NextAuth sessions and theming `[src/app/(client)/providers/Providers.tsx]`【F:src/app/(client)/providers/Providers.tsx†L1-L55】. |
| 25 | + |
| 26 | +### API layer |
| 27 | +- API routes live in `src/app/(server)/api`. For example, the chat completion endpoint validates input and delegates to the chat service【F:src/app/(server)/api/chat/route.ts†L1-L32】. |
| 28 | +- API handlers stay thin and rely on Zod schemas for validation (`lib/validation/*`) and `withApiKey` middleware to enforce key checks【F:src/lib/middleware/apiKey.ts†L1-L41】. |
| 29 | + |
| 30 | +### Service layer |
| 31 | +- Business logic resides in `src/app/(server)/services`. `ChatService` validates models and outlines provider routing【F:src/app/(server)/services/chat/service.ts†L1-L87】. `ApiKeyService` encapsulates key creation and management【F:src/app/(server)/services/apiKey/service.ts†L1-L256】. |
| 32 | +- Services throw `ServiceError`, which routes convert into consistent JSON envelopes【F:src/app/(server)/services/system/errorService.ts†L1-L80】. |
| 33 | + |
| 34 | +### Data layer |
| 35 | +- Prisma client wrapper at `src/db/prisma.ts` ensures a singleton connection【F:src/db/prisma.ts†L1-L13】. |
| 36 | +- Database schema defines users, API keys and team tables【F:prisma/schema.prisma†L1-L125】. |
| 37 | + |
| 38 | +### Caching / realtime |
| 39 | +- Environment supports an optional Redis connection (`REDIS_URL`) but no runtime Redis code exists yet【F:env/.env.example†L18-L23】. |
| 40 | + |
| 41 | +### Middleware |
| 42 | +- Root middleware enforces session authentication and skips public routes【F:src/middleware.ts†L1-L47】. |
| 43 | +- Public route patterns are declared in `lib/middleware/publicRoutes.ts`【F:src/lib/middleware/publicRoutes.ts†L1-L61】. |
| 44 | + |
| 45 | +### Configuration |
| 46 | +- Model registry and limits live in `lib/config/llmConfig.ts`【F:src/lib/config/llmConfig.ts†L1-L40】. |
| 47 | +- Environment files are merged by `scripts/prepare-env.mjs` before development runs【F:scripts/prepare-env.mjs†L1-L30】. |
| 48 | +- Example settings reside in `env/.env.example`【F:env/.env.example†L2-L23】. |
| 49 | + |
| 50 | +## 4. Core request flows |
| 51 | +### Chat completions |
| 52 | +1. Client posts to `/api/chat`. |
| 53 | +2. Route validates payload with `ChatCompletionSchema` and applies API key middleware【F:src/app/(server)/api/chat/route.ts†L18-L27】. |
| 54 | +3. `ChatService.processCompletion` checks model config and would route to a provider (placeholder)【F:src/app/(server)/services/chat/service.ts†L45-L58】. |
| 55 | +4. Response returns to client; future versions may stream tokens. |
| 56 | + |
| 57 | +### Admin: create API key |
| 58 | +1. Dashboard page triggers `POST /api/apikeys`. |
| 59 | +2. Handler authenticates user and validates request【F:src/app/(server)/api/apikeys/route.ts†L35-L47】. |
| 60 | +3. `ApiKeyService.createApiKey` generates and stores the key via Prisma【F:src/app/(server)/services/apiKey/service.ts†L130-L150】. |
| 61 | +4. JSON response returns key preview for display in the UI. |
| 62 | + |
| 63 | +### Logging and error handling |
| 64 | +- Utility logger functions prefix messages consistently【F:src/lib/utils/logger.ts†L1-L47】. |
| 65 | +- API routes call `handleApiError` to log and standardize error responses【F:src/app/(server)/services/system/errorService.ts†L60-L80】. |
| 66 | + |
| 67 | +## 5. Layering and conventions |
| 68 | +- Routes remain thin and delegate to services; UI logic stays client-side. |
| 69 | +- Shared domain types live under `lib/models`【F:src/lib/models/User.ts†L1-L93】. |
| 70 | +- Zod schemas in `lib/validation` define request shapes used by both client and server【F:src/lib/validation/chat.schemas.ts†L1-L46】. |
| 71 | + |
| 72 | +## 6. Extensibility |
| 73 | +- **New provider adapter:** implement logic in `services/adapters` and have `ChatService` route to it (folder exists, implementation pending). Add provider metadata to `lib/config/llmConfig.ts`. |
| 74 | +- **New API route:** create a file under `src/app/(server)/api/{name}/route.ts`, add Zod schema in `lib/validation`, and call a service module. |
| 75 | +- **New UI feature:** add a route under `src/app/(client)/(core)` and co-locate components; export shared pieces via `components/index.ts`【F:src/app/(client)/components/index.ts†L1-L15】. |
| 76 | + |
| 77 | +## 7. Performance and reliability |
| 78 | +- Prisma logs can be enabled via `DEBUG_LOGS` environment variable for diagnostics【F:src/db/prisma.ts†L5-L9】. |
| 79 | +- Docker Compose file provisions a local PostgreSQL instance with health checks【F:docker/docker-compose.db.yml†L1-L21】. |
| 80 | +- Rate limiting, caching and background workers are planned but not yet implemented (see TODOs in services and env files). |
| 81 | + |
| 82 | +## 8. Security considerations |
| 83 | +- Authentication uses NextAuth with credential and Google providers, storing hashed passwords and roles【F:src/lib/auth.ts†L1-L80】. |
| 84 | +- API key middleware parses bearer tokens and rejects revoked or expired keys【F:src/lib/middleware/apiKey.ts†L10-L18】【F:src/app/(server)/services/apiKey/service.ts†L244-L255】. |
| 85 | +- Middleware blocks unauthenticated access except for public routes【F:src/middleware.ts†L18-L33】. |
| 86 | +- For additional guidance see [SECURITY.md](../SECURITY.md). |
| 87 | + |
| 88 | +## 9. Future / Planned |
| 89 | +- Redis-backed rate limits and live log streaming. |
| 90 | +- WebSocket gateway (`ws/`) and background workers (`workers/`). |
| 91 | +- Usage tracking service (`api/usage`) and analytics dashboards. |
| 92 | + |
| 93 | +## 10. Cross-links |
| 94 | +- [README](../README.md) |
| 95 | +- [Getting started](./getting-started.md) |
| 96 | +- [CONTRIBUTING](../CONTRIBUTING.md) |
| 97 | +- [CODE OF CONDUCT](../CODE_OF_CONDUCT.md) |
| 98 | +- [SECURITY](../SECURITY.md) |
| 99 | +- [Environment example](../env/.env.example) |
0 commit comments