Skip to Content
Serversservers/agents — @leadmetrics/server-agents

servers/agents — @leadmetrics/server-agents

A dedicated Node.js background service that runs all AI agent BullMQ workers. It consumes jobs from agent queues, dispatches them to the appropriate adapter (Claude / OpenAI / Gemini), streams results back, and updates the database.

Source: apps/servers/agents/ Package: @leadmetrics/server-agents

Agent worker logic lives in: packages/agents/


Why a Separate Service

ConcernReason
IsolationAgent failures never affect the API or notification service
Independent scalingScale by replica count — run 3 agent servers for 3× throughput
Resource separationAgent processes are memory- and CPU-intensive; separate from API
Adapter env varsAll LLM credentials live in the agents server only

Scaling note: Concurrency is set per-worker in packages/agents/src/workers/. To scale, run multiple replicas of this server — each replica picks up jobs from the shared BullMQ queues. No code changes needed.


Workers

Each worker corresponds to one agent role and listens on agent__{role}:

WorkerQueueAdapterDescription
Setupagent__setupclaude_localRuns client-researcher → competitor → context-file-writer chain
Strategy Writeragent__strategy_writerclaude_localGenerates marketing strategy document
Deliverable Planneragent__deliverable_plannerclaude_localPlans goals + monthly deliverable volumes
Activity Planneragent__activity_plannerclaude_localSchedules activity pipeline from deliverable plan
Blog Writeragent__blog_writerclaude_localDrafts full blog posts from SEO briefs
Social Post Designeragent__social_post_designercodex_localDesigns social post images via Azure OpenAI
Client Researcheragent__client_researchercodex_localResearches client domain → notes
RAG Ingestionagent__rag_ingestionIngests documents into Qdrant vector store

Queues are shared across all tenantstenantId is in the job payload, not the queue name.


Package Split

PackageContents
packages/agentsAll worker code, agent-events.ts, skills.ts, lib/dependency-resolver.ts
packages/queueConnection, enqueueNotification, enqueueAgentJob, queue names, types only

apps/servers/agents imports workers from @leadmetrics/agents/src/workers/*.


Required Config (apps/servers/agents/.env)

VariableRequiredDescription
DATABASE_URLyesPrisma
REDIS_URLyesBullMQ
DASHBOARD_URLyesUsed in email links from agent events
DO_SPACES_KEYyesDigitalOcean Spaces — all 6 vars required
DO_SPACES_SECRETyes
DO_SPACES_REGIONyes
DO_SPACES_ENDPOINTyese.g. https://sgp1.digitaloceanspaces.com — commonly missing
DO_SPACES_BUCKETyes
DO_SPACES_CDN_URLyes
AZURE_IMAGE_API_KEYyesAzure OpenAI for GPT Image (social post designer)
AZURE_IMAGE_ENDPOINTyes
PIXABAY_API_KEYyesImage search fallback
UNSPLASH_ACCESS_KEYyesImage search fallback

DO_SPACES_ENDPOINT is the most commonly missing variable. Without it, the social post designer worker fails silently and resets posts to dm_review.


Startup Flow (src/index.ts)

  1. Validate config via Zod (src/config.ts).
  2. Create shared IORedis connection.
  3. Call start*Workers() for each agent role.
  4. Graceful shutdown on SIGTERM/SIGINT: call stop*Workers() then redis.quit().

© 2026 Leadmetrics — Internal use only