How kevinrivm/vocero-crm works
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: ingest.ts Inbound ingestion · runner.ts Lab simulator · trigger.ts AI agent trigger · route.ts WhatsApp webhook
Components 22
Next.js CRM
- Auth routes · Establishes organization and team access.
src/app/(auth)/login/page.tsx - Better Auth API
src/app/api/auth/[...all]/route.ts - Protected CRM shell · Hosts Inbox, Contacts, Pipeline, Agent, Lab, and Settings.
src/app/(app)/layout.tsx - Inbox client · Consumes live inbox updates.
src/components/inbox/inbox-client.tsx - Pipeline board API
src/app/api/pipeline/board/route.ts
Data & tenancy
- PostgreSQL · Stores organization-scoped CRM, messaging, configuration, and lab data.
- Drizzle schema & migrations
src/lib/db/schema.ts - Tenant scoping · Enforces organization isolation.
src/lib/db/tenant.ts - Credential encryption · Encrypts WhatsApp credentials before storage.
src/lib/crypto/index.ts
Messaging & realtime
- WhatsApp webhook · Receives Meta verification and event callbacks.
src/app/api/webhooks/wa/[webhookToken]/route.ts - Inbound ingestion · Idempotently resolves identity, contacts, conversations, and activity.
src/server/inbox/ingest.ts - Inbox sending rules · Applies 24-hour window, templates, and delivery status handling.
src/server/inbox/send.ts - Meta WhatsApp Cloud API
src/lib/meta/client.ts - SSE event channel · Publishes CRM and inbox updates to browsers.
src/app/api/events/route.ts - Lead stage history · Retains stage changes and lead context.
src/server/leads/stage-history.ts
AI & Lab
- AI agent trigger · Responds only when conversation state permits.
src/server/ai/trigger.ts - LLM agent adapter · Uses profile, knowledge, prompts, actions, and handoff rules.
src/lib/ai/index.ts - External bot API · External agents receive CRM context but send through Vocero.
src/app/api/bot/messages/route.ts - Lab simulator · Runs fixed customer personas against the configured agent without Meta delivery.
src/server/lab/runner.ts - LLM judge · Scores transcripts and produces improvement suggestions.
src/server/lab/judge.ts - LLM provider · OpenAI-compatible or OpenRouter endpoint.
Deployment & externals
- Docker + Caddy deployment · Runs migrations, application, HTTPS, and webhook hosting.
docker-compose.yml
Connections 26
- 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