Metanow Docs
Contributing

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 base

Tech stack

LayerTechnology
FrontendNext.js 16, React 19, Tailwind CSS, shadcn/ui, Zustand, i18next
BackendNestJS 11, Drizzle ORM, BullMQ, Better Auth
AIAnthropic SDK (Claude), OpenAI SDK (embeddings), Gemini SDK
DatabasePostgreSQL (with pgvector for similarity search)
QueueRedis + BullMQ
StorageHetzner S3-compatible object storage
PaymentsStripe
BuildTurbo, 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 S3

API client code generation

The frontend never calls the backend with raw fetch. Instead:

  1. pnpm generate-spec runs apps/server/scripts/generate-spec.ts which boots the NestJS module tree (no HTTP, no DB) and writes openapi.json to the repo root.
  2. pnpm openapi-ts reads openapi.json and generates packages/api-client/src/sdk.gen.ts and types.gen.ts.
  3. 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

FileRole
apps/server/src/app.module.tsRoot NestJS module — imports every feature module
apps/server/src/ai/model-router.service.tsAI orchestration — RAG context, prompt construction, trace logging
apps/server/src/briefs/briefs.service.tsBrief CRUD and job enqueueing
apps/server/src/decisions/decisions.service.tsDecision recording, grading, and RAG retrieval
apps/server/src/queues/brief-prompts.processor.tsBullMQ worker for AI generation jobs
apps/server/src/auth/org.guard.tsExtracts org from session; enforces tenant isolation
packages/db/src/schema.tsAll Drizzle table definitions
packages/env/src/server.tsAll server-side environment variables (Zod-validated)
openapi.jsonGenerated OpenAPI spec (repo root) — source of truth for the API client and these docs

On this page