Architecture
Monorepo structure, tech stack, and how the layers of MetaNow Console connect.
MetaNow Console is a pnpm + Turbo monorepo with two runnable apps, a docs site, and four shared packages.
Repository layout
metanow-console/
├── apps/
│ ├── web/ Next.js 16 frontend (port 3001)
│ ├── server/ NestJS 11 API (port 8000)
│ ├── fumadocs/ This documentation (port 4000)
│ └── dashboard/ Secondary dashboard
│
└── packages/
├── db/ Drizzle ORM schema + migrations (PostgreSQL)
├── types/ Shared TypeScript DTOs
├── api-client/ Auto-generated typed REST client (from OpenAPI spec)
├── env/ Zod-validated environment variables (t3-env)
├── ui/ Shared shadcn/ui component library
└── config/ Shared tsconfig baseTech stack
| Layer | Technology |
|---|---|
| Frontend | Next.js 16, React 19, Tailwind CSS, shadcn/ui, Zustand, i18next |
| Backend | NestJS 11, Drizzle ORM, BullMQ, Better Auth |
| AI | Anthropic SDK (Claude), OpenAI SDK (embeddings), Gemini SDK |
| Database | PostgreSQL (with pgvector for similarity search) |
| Queue | Redis + BullMQ |
| Storage | Hetzner S3-compatible object storage |
| Payments | Stripe |
| Build | Turbo, pnpm workspaces, Biome (lint/format), Jest |
How the layers connect
Browser
│
│ HTTP
▼
apps/web (Next.js Server Components + packages/api-client)
│
│ HTTP (fetch to localhost:8000 in dev)
▼
apps/server (NestJS)
│
├── Controller validates request, extracts org from session via OrgGuard
├── Service business logic, org-scoped DB queries
├── Drizzle typed SQL via packages/db schema
└── ModelRouterService → Anthropic / Gemini / Noop
writes trace to ai_decision_trace
│
├── PostgreSQL (primary data store)
├── Redis (BullMQ job queues)
└── Hetzner S3 (file exports)
BullMQ Workers (same NestJS process, separate @Processor classes)
└── BriefPromptsProcessor calls ModelRouterService
└── BriefExportsProcessor generates PDF/PNG, uploads to S3API client code generation
The frontend never calls the backend with raw fetch. Instead:
pnpm generate-specrunsapps/server/scripts/generate-spec.tswhich boots the NestJS module tree (no HTTP, no DB) and writesopenapi.jsonto the repo root.pnpm openapi-tsreadsopenapi.jsonand generatespackages/api-client/src/sdk.gen.tsandtypes.gen.ts.- The web app imports typed functions from
@metanow-console/api-client.
Run both steps after any NestJS controller or DTO change.
Key patterns
1. Multi-tenant org scoping
Every API request is scoped to an organisation. The OrgGuard reads the organisation from the authenticated session and exposes it via the @CurrentOrg() decorator. Every service method receives orgId as its first argument and every database query filters by org_id.
Cross-org access returns 404, never 403 — this prevents an attacker from probing whether a resource exists in another organisation.
2. AI provider abstraction
All AI calls go through ModelRouterService in apps/server/src/ai/. The service selects a provider (AnthropicAiProvider, GeminiAiProvider, or NoopAiProvider for tests) based on the AI_PROVIDER environment variable. Output is always the same shape:
{
chosen: Record<string, unknown>;
alternativesConsidered: Array<Record<string, unknown>>;
reasoning: string;
confidence: "strong" | "medium" | "weak" | "none";
}Callers never import a provider directly — only ModelRouterService.
3. Async job processing
Brief generation and file exports are long-running operations. They are enqueued as BullMQ jobs and return immediately with a { jobId, status: "queued" } response. The client polls a status endpoint until the job completes.
Job IDs are deterministic (e.g. brief-prompts-${briefId}), so re-submitting the same request returns the existing job instead of creating a duplicate.
4. Trace-first AI logging
Before returning any AI result, ModelRouterService writes a row to ai_decision_trace. This captures the full generation context — entity fingerprint, retrieved similar decisions, token count, estimated cost, and confidence. Audit and cost tracking are decoupled from business logic.
5. Soft deletes
No record is ever hard-deleted. Every major table has a deleted_at timestamp column. Queries always filter WHERE deleted_at IS NULL. The history is preserved for audit and can be recovered if needed.
6. Database access via dependency injection
The Drizzle client is injected as @Inject(DB) into every service. This means tests can overrideProvider(DB, transactionClient) to run queries inside a rolled-back transaction without touching production data.
Important files
| File | Role |
|---|---|
apps/server/src/app.module.ts | Root NestJS module — imports every feature module |
apps/server/src/ai/model-router.service.ts | AI orchestration — RAG context, prompt construction, trace logging |
apps/server/src/briefs/briefs.service.ts | Brief CRUD and job enqueueing |
apps/server/src/decisions/decisions.service.ts | Decision recording, grading, and RAG retrieval |
apps/server/src/queues/brief-prompts.processor.ts | BullMQ worker for AI generation jobs |
apps/server/src/auth/org.guard.ts | Extracts org from session; enforces tenant isolation |
packages/db/src/schema.ts | All Drizzle table definitions |
packages/env/src/server.ts | All server-side environment variables (Zod-validated) |
openapi.json | Generated OpenAPI spec (repo root) — source of truth for the API client and these docs |