# kevinrivm/vocero-crm architecture

> Architecture diagram of the GitHub repository kevinrivm/vocero-crm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kevinrivm/vocero-crm
- Repository: https://github.com/kevinrivm/vocero-crm

## Overview

#### Purpose and Runtime
Vocero CRM is a self-hosted, single-business WhatsApp sales CRM. It combines a real-time inbox, contacts/pipeline, configurable AI agent, and an internal AI evaluation lab. Runtime is Next.js 15 App Router/React 19 on Node.js, PostgreSQL via Drizzle, and Docker/Caddy for deployment and HTTPS. Core entry points: `src/app/page.tsx`, `src/app/layout.tsx`, `Dockerfile`, `docker-compose.yml`.

#### Application Surfaces and Authentication
The UI separates public authentication from the protected CRM shell. Auth routes establish organization/team access; the application shell hosts Inbox, Contacts, Pipeline, Agent, Lab, and Settings. Better Auth is exposed through the catch-all route and server/session adapters. Paths: `src/app/(auth)/login/page.tsx`, `src/app/(app)/layout.tsx`, `src/app/api/auth/[...all]/route.ts`, `src/lib/auth/session.ts`.

#### Persistence, Tenant Boundaries, and Secrets
PostgreSQL is the system of record for users, organization-scoped CRM data, messages, lead state, configurations, and laboratory runs. Drizzle schema/migrations define evolution; tenant utilities enforce organization scoping. WhatsApp credentials are encrypted before persistence. Paths: `src/lib/db/schema.ts`, `src/lib/db/tenant.ts`, `drizzle/0007_confused_zaran.sql`, `src/lib/crypto/index.ts`.

#### WhatsApp Integration and Inbox Flow
Meta WhatsApp Cloud API is the external messaging boundary. A tokenized webhook accepts Meta verification/events, validates and ingests inbound messages idempotently, resolves identity/contact/conversation state, persists activity, emits UI events, and may trigger AI. Outbound human, AI, and template sends converge on inbox sending rules, including the 24-hour window and delivery-status handling. Paths: `src/app/api/webhooks/wa/[webhookToken]/route.ts`, `src/server/inbox/webhook.ts`, `src/server/inbox/ingest.ts`, `src/server/inbox/send.ts`, `src/lib/meta/client.ts`.

#### Real-Time CRM and Sales Pipeline
SSE provides the browser update channel rather than WebSockets; clients consume events to update inbox and operational views. Contacts created from inbound identity flow into a configurable kanban pipeline. Lead updates retain stage history and support amount, priority, loss, notes, and activity context. Paths: `src/app/api/events/route.ts`, `src/server/events/bus.ts`, `src/app/api/pipeline/board/route.ts`, `src/server/leads/stage-history.ts`, `src/components/inbox/inbox-client.tsx`.

#### AI Agent and External-Bot Boundary
The built-in agent uses an OpenAI-compatible/OpenRouter adapter, business profile and knowledge base, prompts, actions, handoff rules, and pipeline intent handling. It only responds when conversation state permits it. Alternatively, an external agent uses authenticated `/api/bot/*` endpoints; that service receives CRM context and sends through Vocero, so Meta credentials remain internal. Paths: `src/lib/ai/index.ts`, `src/server/ai/trigger.ts`, `src/server/ai/actions.ts`, `src/app/api/bot/messages/route.ts`, `src/server/bot/auth.ts`.

#### Laboratory Sandbox
The Lab runs fixed simulated customer personas against the real configured agent without Meta delivery. A separate LLM judge scores transcripts, produces evidence/suggestions, and can apply approved knowledge-base improvements. This sandbox is a hard boundary: simulated paths must not use production send transport. Paths: `src/server/lab/runner.ts`, `src/server/lab/personas.ts`, `src/server/lab/judge.ts`, `src/app/api/lab/runs/route.ts`.

#### Operations and External Dependencies
Deployment starts migrations with the containerized application and routes HTTPS through Caddy. Required infrastructure is PostgreSQL, Docker, and a public HTTPS domain for Meta webhooks. Optional services are Meta Graph API and an OpenAI-compatible LLM provider. Health/version endpoints support deployment verification; development-only mock APIs isolate integration/e2e work. Paths: `Caddyfile`, `docker-compose.yml`, `scripts/migrate.mjs`, `src/app/api/health/route.ts`, `src/app/api/dev/wa-mock/graph/[...path]/route.ts`.

Main entry points: [`src/server/inbox/ingest.ts`](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/inbox/ingest.ts) (Inbound ingestion), [`src/server/lab/runner.ts`](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/lab/runner.ts) (Lab simulator), [`src/server/ai/trigger.ts`](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/ai/trigger.ts) (AI agent trigger), [`src/app/api/webhooks/wa/[webhookToken]/route.ts`](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/webhooks/wa/%5BwebhookToken%5D/route.ts) (WhatsApp webhook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Next.js CRM"]
  node_public_auth["Auth routes<br/>Next.js routes<br/>[page.tsx]"]
  node_auth_api["Better Auth API<br/>auth endpoint<br/>[route.ts]"]
  node_crm_shell["Protected CRM shell<br/>App Router layout<br/>[layout.tsx]"]
  node_inbox_client["Inbox client<br/>React client<br/>[inbox-client.tsx]"]
  node_pipeline_api["Pipeline board API<br/>route handler<br/>[route.ts]"]
end

subgraph group_data["Data &amp; tenancy"]
  node_database[("PostgreSQL<br/>system of record")]
  node_schema["Drizzle schema &amp; migrations<br/>data evolution<br/>[schema.ts]"]
  node_tenant_scope["Tenant scoping<br/>data access boundary<br/>[tenant.ts]"]
  node_credential_crypto["Credential encryption<br/>secret protection<br/>[index.ts]"]
end

subgraph group_messaging["Messaging &amp; realtime"]
  node_webhook_route["WhatsApp webhook<br/>tokenized endpoint<br/>[route.ts]"]
  node_inbound_ingest["Inbound ingestion<br/>message processor<br/>[ingest.ts]"]
  node_inbox_send["Inbox sending rules<br/>message transport policy<br/>[send.ts]"]
  node_meta_api{{"Meta WhatsApp Cloud API<br/>external messaging API<br/>[client.ts]"}}
  node_sse["SSE event channel<br/>realtime updates<br/>[route.ts]"]
  node_lead_history["Lead stage history<br/>pipeline state<br/>[stage-history.ts]"]
end

subgraph group_ai["AI &amp; Lab"]
  node_agent_trigger["AI agent trigger<br/>conversation orchestrator<br/>[trigger.ts]"]
  node_ai_adapter["LLM agent adapter<br/>OpenAI-compatible client<br/>[index.ts]"]
  node_external_bot["External bot API<br/>authenticated integration boundary<br/>[route.ts]"]
  node_lab_runner["Lab simulator<br/>sandbox runner<br/>[runner.ts]"]
  node_lab_judge["LLM judge<br/>evaluation scorer<br/>[judge.ts]"]
  node_llm_provider{{"LLM provider<br/>external AI service"}}
end

subgraph group_ops["Deployment &amp; externals"]
  node_deployment["Docker + Caddy deployment<br/>container runtime<br/>[docker-compose.yml]"]
end

node_public_auth -->|"sign-in"| node_auth_api
node_auth_api -->|"session access"| node_tenant_scope
node_crm_shell -->|"hosts"| node_inbox_client
node_crm_shell -->|"loads board"| node_pipeline_api
node_tenant_scope -->|"scoped queries"| node_database
node_schema -->|"defines and migrates"| node_database
node_credential_crypto -->|"protected credentials"| node_database
node_meta_api -->|"verification and events"| node_webhook_route
node_webhook_route -->|"validated callbacks"| node_inbound_ingest
node_inbound_ingest -->|"persists CRM activity"| node_database
node_inbound_ingest -->|"emits updates"| node_sse
node_inbound_ingest -->|"eligible conversations"| node_agent_trigger
node_inbox_send -->|"outbound messages"| node_meta_api
node_agent_trigger -->|"agent request"| node_ai_adapter
node_ai_adapter -->|"completion requests"| node_llm_provider
node_agent_trigger -->|"approved AI replies"| node_inbox_send
node_external_bot -->|"sends via Vocero"| node_inbox_send
node_sse -->|"live events"| node_inbox_client
node_pipeline_api -->|"stage updates"| node_lead_history
node_lead_history -->|"stores history"| node_database
node_lab_runner -.->|"simulated agent runs"| node_ai_adapter
node_lab_runner -->|"transcripts"| node_lab_judge
node_lab_judge -->|"evaluation requests"| node_llm_provider
node_lab_runner -->|"stores runs"| node_database
node_deployment -->|"runs migrations and connects"| node_database
node_deployment -->|"public HTTPS routing"| node_webhook_route

click node_public_auth "https://github.com/kevinrivm/vocero-crm/blob/main/src/app/(auth)/login/page.tsx"
click node_auth_api "https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/auth/%5B...all%5D/route.ts"
click node_crm_shell "https://github.com/kevinrivm/vocero-crm/blob/main/src/app/(app)/layout.tsx"
click node_inbox_client "https://github.com/kevinrivm/vocero-crm/blob/main/src/components/inbox/inbox-client.tsx"
click node_pipeline_api "https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/pipeline/board/route.ts"
click node_schema "https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/db/schema.ts"
click node_tenant_scope "https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/db/tenant.ts"
click node_credential_crypto "https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/crypto/index.ts"
click node_webhook_route "https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/webhooks/wa/%5BwebhookToken%5D/route.ts"
click node_inbound_ingest "https://github.com/kevinrivm/vocero-crm/blob/main/src/server/inbox/ingest.ts"
click node_inbox_send "https://github.com/kevinrivm/vocero-crm/blob/main/src/server/inbox/send.ts"
click node_meta_api "https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/meta/client.ts"
click node_sse "https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/events/route.ts"
click node_lead_history "https://github.com/kevinrivm/vocero-crm/blob/main/src/server/leads/stage-history.ts"
click node_agent_trigger "https://github.com/kevinrivm/vocero-crm/blob/main/src/server/ai/trigger.ts"
click node_ai_adapter "https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/ai/index.ts"
click node_external_bot "https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/bot/messages/route.ts"
click node_lab_runner "https://github.com/kevinrivm/vocero-crm/blob/main/src/server/lab/runner.ts"
click node_lab_judge "https://github.com/kevinrivm/vocero-crm/blob/main/src/server/lab/judge.ts"
click node_deployment "https://github.com/kevinrivm/vocero-crm/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_auth,node_auth_api,node_crm_shell,node_inbox_client,node_pipeline_api toneBlue
class node_database,node_schema,node_tenant_scope,node_credential_crypto toneAmber
class node_webhook_route,node_inbound_ingest,node_inbox_send,node_meta_api,node_sse,node_lead_history toneMint
class node_agent_trigger,node_ai_adapter,node_external_bot,node_lab_runner,node_lab_judge,node_llm_provider toneRose
class node_deployment toneIndigo
```

## Components

### Next.js CRM

- [Auth routes](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/(auth)/login/page.tsx): Next.js routes · Establishes organization and team access. · `src/app/(auth)/login/page.tsx`
- [Better Auth API](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/auth/%5B...all%5D/route.ts): auth endpoint · `src/app/api/auth/[...all]/route.ts`
- [Protected CRM shell](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/(app)/layout.tsx): App Router layout · Hosts Inbox, Contacts, Pipeline, Agent, Lab, and Settings. · `src/app/(app)/layout.tsx`
- [Inbox client](https://github.com/kevinrivm/vocero-crm/blob/main/src/components/inbox/inbox-client.tsx): React client · Consumes live inbox updates. · `src/components/inbox/inbox-client.tsx`
- [Pipeline board API](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/pipeline/board/route.ts): route handler · `src/app/api/pipeline/board/route.ts`

### Data & tenancy

- **PostgreSQL**: system of record · Stores organization-scoped CRM, messaging, configuration, and lab data.
- [Drizzle schema & migrations](https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/db/schema.ts): data evolution · `src/lib/db/schema.ts`
- [Tenant scoping](https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/db/tenant.ts): data access boundary · Enforces organization isolation. · `src/lib/db/tenant.ts`
- [Credential encryption](https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/crypto/index.ts): secret protection · Encrypts WhatsApp credentials before storage. · `src/lib/crypto/index.ts`

### Messaging & realtime

- [WhatsApp webhook](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/webhooks/wa/%5BwebhookToken%5D/route.ts): tokenized endpoint · Receives Meta verification and event callbacks. · `src/app/api/webhooks/wa/[webhookToken]/route.ts`
- [Inbound ingestion](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/inbox/ingest.ts): message processor · Idempotently resolves identity, contacts, conversations, and activity. · `src/server/inbox/ingest.ts`
- [Inbox sending rules](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/inbox/send.ts): message transport policy · Applies 24-hour window, templates, and delivery status handling. · `src/server/inbox/send.ts`
- [Meta WhatsApp Cloud API](https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/meta/client.ts): external messaging API · `src/lib/meta/client.ts`
- [SSE event channel](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/events/route.ts): realtime updates · Publishes CRM and inbox updates to browsers. · `src/app/api/events/route.ts`
- [Lead stage history](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/leads/stage-history.ts): pipeline state · Retains stage changes and lead context. · `src/server/leads/stage-history.ts`

### AI & Lab

- [AI agent trigger](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/ai/trigger.ts): conversation orchestrator · Responds only when conversation state permits. · `src/server/ai/trigger.ts`
- [LLM agent adapter](https://github.com/kevinrivm/vocero-crm/blob/main/src/lib/ai/index.ts): OpenAI-compatible client · Uses profile, knowledge, prompts, actions, and handoff rules. · `src/lib/ai/index.ts`
- [External bot API](https://github.com/kevinrivm/vocero-crm/blob/main/src/app/api/bot/messages/route.ts): authenticated integration boundary · External agents receive CRM context but send through Vocero. · `src/app/api/bot/messages/route.ts`
- [Lab simulator](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/lab/runner.ts): sandbox runner · Runs fixed customer personas against the configured agent without Meta delivery. · `src/server/lab/runner.ts`
- [LLM judge](https://github.com/kevinrivm/vocero-crm/blob/main/src/server/lab/judge.ts): evaluation scorer · Scores transcripts and produces improvement suggestions. · `src/server/lab/judge.ts`
- **LLM provider**: external AI service · OpenAI-compatible or OpenRouter endpoint.

### Deployment & externals

- [Docker + Caddy deployment](https://github.com/kevinrivm/vocero-crm/blob/main/docker-compose.yml): container runtime · Runs migrations, application, HTTPS, and webhook hosting. · `docker-compose.yml`

## Connections

- Auth routes → Better Auth API: sign-in
- Better Auth API → Tenant scoping: session access
- Protected CRM shell → Inbox client: hosts
- Protected CRM shell → Pipeline board API: loads board
- Tenant scoping → PostgreSQL: scoped queries
- Drizzle schema & migrations → PostgreSQL: defines and migrates
- Credential encryption → PostgreSQL: protected credentials
- Meta WhatsApp Cloud API → WhatsApp webhook: verification and events
- WhatsApp webhook → Inbound ingestion: validated callbacks
- Inbound ingestion → PostgreSQL: persists CRM activity
- Inbound ingestion → SSE event channel: emits updates
- Inbound ingestion → AI agent trigger: eligible conversations
- Inbox sending rules → Meta WhatsApp Cloud API: outbound messages
- AI agent trigger → LLM agent adapter: agent request
- LLM agent adapter → LLM provider: completion requests
- AI agent trigger → Inbox sending rules: approved AI replies
- External bot API → Inbox sending rules: sends via Vocero
- SSE event channel → Inbox client: live events
- Pipeline board API → Lead stage history: stage updates
- Lead stage history → PostgreSQL: stores history
- Lab simulator → LLM agent adapter: simulated agent runs · No production send transport.
- Lab simulator → LLM judge: transcripts
- LLM judge → LLM provider: evaluation requests
- Lab simulator → PostgreSQL: stores runs
- Docker + Caddy deployment → PostgreSQL: runs migrations and connects
- Docker + Caddy deployment → WhatsApp webhook: public HTTPS routing
