# amitdas4321/engagepilot architecture

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

- Interactive diagram: https://gitdiagram.com/amitdas4321/engagepilot
- Repository: https://github.com/amitdas4321/engagepilot

## Overview

EngagePilot is a self-hosted Instagram comment-to-DM automation platform. Workspace users configure keyword campaigns; Meta comment webhooks are verified and queued, then a separate worker matches campaigns and sends private and optional public replies through Meta. The product also includes inbox automations, conversation management, tracked links, reporting, team permissions, and worker diagnostics. The workflow and several internal calls are documented or excerpted; unsampled dashboard and API relationships are not inferred.

Main entry points: [`lib/db/client.ts`](https://github.com/amitdas4321/engagepilot/blob/main/lib/db/client.ts) (PostgreSQL), [`lib/queue/dm-worker.ts`](https://github.com/amitdas4321/engagepilot/blob/main/lib/queue/dm-worker.ts) (DM processing), [`app/api/automations/route.ts`](https://github.com/amitdas4321/engagepilot/blob/main/app/api/automations/route.ts) (Campaign API), [`app/api/webhook/route.ts`](https://github.com/amitdas4321/engagepilot/blob/main/app/api/webhook/route.ts) (Meta webhook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_campaigns["Campaigns &amp; links"]
  node_campaign_ui["Campaign screens<br/>[page.tsx]"]
  node_campaign_api["Campaign API<br/>[route.ts]"]
  node_tracking["Tracked redirects<br/>[route.ts]"]
end

subgraph group_runtime["Webhook &amp; delivery"]
  node_webhook["Meta webhook<br/>[route.ts]"]
  node_queue["DM job queue<br/>[client.ts]"]
  node_worker_entry["Worker process<br/>[dm-worker.ts]"]
  node_dm_worker["DM processing<br/>[dm-worker.ts]"]
  node_keyword_match["Keyword matching<br/>[keyword-matcher.ts]"]
  node_meta_client["Meta API client<br/>[client.ts]"]
end

subgraph group_inbox["Inbox &amp; AI"]
  node_inbox_ui["Inbox screens<br/>[page.tsx]"]
  node_inbox_api["Inbox automations API<br/>[route.ts]"]
  node_ai_client["AI reply client<br/>[client.ts]"]
end

subgraph group_workspace["Workspace &amp; insights"]
  node_workspace_access["Workspace access"]
  node_members_api["Team management<br/>[route.ts]"]
  node_reports["Shared reports<br/>[data.ts]"]
end

subgraph group_platform["Shared services"]
  node_database[("PostgreSQL<br/>[client.ts]")]
  node_redis[("Redis health state<br/>[worker-health.ts]")]
end

node_user(("Workspace user"))
node_instagram_user(("Instagram commenter"))
node_meta{{"Meta Graph API"}}

node_user -->|"configures"| node_campaign_ui
node_campaign_ui -.->|"manages campaigns"| node_campaign_api
node_campaign_api -->|"persists campaigns"| node_database
node_campaign_api -->|"creates tracked slugs"| node_tracking
node_instagram_user -->|"comments on post"| node_webhook
node_webhook -->|"enqueues jobs"| node_queue
node_webhook -->|"records events"| node_database
node_worker_entry -->|"starts processor"| node_dm_worker
node_dm_worker -->|"consumes jobs"| node_queue
node_dm_worker -->|"reads campaign data"| node_database
node_dm_worker -->|"matches keywords"| node_keyword_match
node_dm_worker -->|"sends replies"| node_meta_client
node_meta_client -->|"calls Graph API"| node_meta
node_worker_entry -->|"records health"| node_redis
node_user -.->|"manages conversations"| node_inbox_ui
node_inbox_api -->|"generates test reply"| node_ai_client
node_inbox_api -->|"stores automation config"| node_database
node_inbox_ui -.->|"loads conversations"| node_meta_client
node_members_api -->|"checks permissions"| node_workspace_access
node_members_api -->|"stores invitations"| node_database
node_tracking -->|"records clicks"| node_database

click node_campaign_ui "https://github.com/amitdas4321/engagepilot/blob/main/app/(dashboard)/campaigns/new/page.tsx"
click node_campaign_api "https://github.com/amitdas4321/engagepilot/blob/main/app/api/automations/route.ts"
click node_tracking "https://github.com/amitdas4321/engagepilot/blob/main/app/r/%5Bslug%5D/route.ts"
click node_webhook "https://github.com/amitdas4321/engagepilot/blob/main/app/api/webhook/route.ts"
click node_queue "https://github.com/amitdas4321/engagepilot/blob/main/lib/queue/client.ts"
click node_worker_entry "https://github.com/amitdas4321/engagepilot/blob/main/worker/dm-worker.ts"
click node_dm_worker "https://github.com/amitdas4321/engagepilot/blob/main/lib/queue/dm-worker.ts"
click node_keyword_match "https://github.com/amitdas4321/engagepilot/blob/main/lib/utils/keyword-matcher.ts"
click node_meta_client "https://github.com/amitdas4321/engagepilot/blob/main/lib/meta/client.ts"
click node_inbox_ui "https://github.com/amitdas4321/engagepilot/blob/main/app/(dashboard)/inbox/page.tsx"
click node_inbox_api "https://github.com/amitdas4321/engagepilot/blob/main/app/api/inbox-automations/route.ts"
click node_ai_client "https://github.com/amitdas4321/engagepilot/blob/main/lib/ai/client.ts"
click node_workspace_access "https://github.com/amitdas4321/engagepilot/blob/main/lib/workspace-access.ts"
click node_members_api "https://github.com/amitdas4321/engagepilot/blob/main/app/api/workspace/members/route.ts"
click node_reports "https://github.com/amitdas4321/engagepilot/blob/main/lib/reports/data.ts"
click node_database "https://github.com/amitdas4321/engagepilot/blob/main/lib/db/client.ts"
click node_redis "https://github.com/amitdas4321/engagepilot/blob/main/lib/ops/worker-health.ts"

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_campaign_ui,node_campaign_api,node_tracking,node_user toneBlue
class node_webhook,node_queue,node_worker_entry,node_dm_worker,node_keyword_match,node_meta_client toneAmber
class node_inbox_ui,node_inbox_api,node_ai_client,node_meta toneMint
class node_workspace_access,node_members_api,node_reports toneRose
class node_database,node_redis,node_instagram_user toneIndigo
```

## Components

### Campaigns & links

- [Campaign screens](https://github.com/amitdas4321/engagepilot/blob/main/app/(dashboard)/campaigns/new/page.tsx): `app/(dashboard)/campaigns/new/page.tsx`
- [Campaign API](https://github.com/amitdas4321/engagepilot/blob/main/app/api/automations/route.ts): `app/api/automations/route.ts`
- [Tracked redirects](https://github.com/amitdas4321/engagepilot/blob/main/app/r/%5Bslug%5D/route.ts): `app/r/[slug]/route.ts`

### Webhook & delivery

- [Meta webhook](https://github.com/amitdas4321/engagepilot/blob/main/app/api/webhook/route.ts): `app/api/webhook/route.ts`
- [DM job queue](https://github.com/amitdas4321/engagepilot/blob/main/lib/queue/client.ts): `lib/queue/client.ts`
- [Worker process](https://github.com/amitdas4321/engagepilot/blob/main/worker/dm-worker.ts): `worker/dm-worker.ts`
- [DM processing](https://github.com/amitdas4321/engagepilot/blob/main/lib/queue/dm-worker.ts): `lib/queue/dm-worker.ts`
- [Keyword matching](https://github.com/amitdas4321/engagepilot/blob/main/lib/utils/keyword-matcher.ts): `lib/utils/keyword-matcher.ts`
- [Meta API client](https://github.com/amitdas4321/engagepilot/blob/main/lib/meta/client.ts): `lib/meta/client.ts`

### Inbox & AI

- [Inbox screens](https://github.com/amitdas4321/engagepilot/blob/main/app/(dashboard)/inbox/page.tsx): `app/(dashboard)/inbox/page.tsx`
- [Inbox automations API](https://github.com/amitdas4321/engagepilot/blob/main/app/api/inbox-automations/route.ts): `app/api/inbox-automations/route.ts`
- [AI reply client](https://github.com/amitdas4321/engagepilot/blob/main/lib/ai/client.ts): `lib/ai/client.ts`

### Workspace & insights

- [Workspace access](https://github.com/amitdas4321/engagepilot/blob/main/lib/workspace-access.ts): `lib/workspace-access.ts`
- [Team management](https://github.com/amitdas4321/engagepilot/blob/main/app/api/workspace/members/route.ts): `app/api/workspace/members/route.ts`
- [Shared reports](https://github.com/amitdas4321/engagepilot/blob/main/lib/reports/data.ts): `lib/reports/data.ts`

### Shared services

- [PostgreSQL](https://github.com/amitdas4321/engagepilot/blob/main/lib/db/client.ts): `lib/db/client.ts`
- [Redis health state](https://github.com/amitdas4321/engagepilot/blob/main/lib/ops/worker-health.ts): `lib/ops/worker-health.ts`

### Other components

- **Workspace user**
- **Instagram commenter**
- **Meta Graph API**

## Connections

- Workspace user → Campaign screens: configures (evidence: `README.md`)
- Campaign screens → Campaign API: manages campaigns
- Campaign API → PostgreSQL: persists campaigns (evidence: `app/api/automations/route.ts`)
- Campaign API → Tracked redirects: creates tracked slugs (evidence: `app/api/automations/route.ts`)
- Instagram commenter → Meta webhook: comments on post (evidence: `README.md`)
- Meta webhook → DM job queue: enqueues jobs (evidence: `app/api/webhook/route.ts`)
- Meta webhook → PostgreSQL: records events (evidence: `app/api/webhook/route.ts`)
- Worker process → DM processing: starts processor (evidence: `worker/dm-worker.ts`)
- DM processing → DM job queue: consumes jobs (evidence: `lib/queue/dm-worker.ts`)
- DM processing → PostgreSQL: reads campaign data (evidence: `lib/queue/dm-worker.ts`)
- DM processing → Keyword matching: matches keywords (evidence: `lib/queue/dm-worker.ts`)
- DM processing → Meta API client: sends replies (evidence: `lib/queue/dm-worker.ts`)
- Meta API client → Meta Graph API: calls Graph API (evidence: `README.md`)
- Worker process → Redis health state: records health (evidence: `worker/dm-worker.ts`)
- Workspace user → Inbox screens: manages conversations
- Inbox automations API → AI reply client: generates test reply (evidence: `app/api/inbox-automations/route.ts`)
- Inbox automations API → PostgreSQL: stores automation config (evidence: `app/api/inbox-automations/route.ts`)
- Inbox screens → Meta API client: loads conversations
- Team management → Workspace access: checks permissions (evidence: `app/api/workspace/members/route.ts`)
- Team management → PostgreSQL: stores invitations (evidence: `app/api/workspace/members/route.ts`)
- Tracked redirects → PostgreSQL: records clicks (evidence: `app/r/[slug]/route.ts`)
