# jamesugbanu/agent-sdr architecture

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

- Interactive diagram: https://gitdiagram.com/jamesugbanu/agent-sdr
- Repository: https://github.com/jamesugbanu/agent-sdr

## Overview

Agent SDR is a guarded AI sales-development system. A user configures an ICP campaign; its API enqueues work that a background worker runs through the agent loop, which selects permitted tools for prospecting, research, qualification, personalization, outreach, replies, CRM, and meetings. Deterministic policy, budgets, approvals, and persisted state constrain actions. Separate paths support human approval, inbound email events, and operational visibility. The README documents the overall flow; sampled source confirms selected queue, worker, orchestration, campaign, and approval links, while some tool and integration internals were not sampled.

Main entry points: [`src/server/agents/orchestrator.ts`](https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/agents/orchestrator.ts) (Agent Orchestrator), [`src/server/tools`](https://github.com/jamesugbanu/agent-sdr/tree/main/src/server/tools) (Lifecycle Tool Implementations), [`src/lib/db.ts`](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/db.ts) (PostgreSQL), [`src/lib/queue.ts`](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/queue.ts) (Job Queue)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["User Experience"]
  node_ui["Campaign and Approval UI"]
  node_campaign_api["Campaign API<br/>[route.ts]"]
  node_approval_api["Approval API<br/>[route.ts]"]
end

subgraph group_runtime["Agent Runtime"]
  node_agent["Agent Orchestrator<br/>[orchestrator.ts]"]
  node_snapshot["Run State Snapshot<br/>[state.ts]"]
  node_decision["Tool Decision<br/>[decisions.ts]"]
  node_registry["Tool Registry<br/>[registry.ts]"]
  node_queue["Job Queue<br/>[queue.ts]"]
  node_worker["Background Worker<br/>[index.ts]"]
end

subgraph group_lifecycle["Lead Lifecycle"]
  node_tool_impl["Lifecycle Tool Implementations"]
  node_inbound["Email Webhooks<br/>[route.ts]"]
  node_event_webhook["Delivery Event Webhook<br/>[route.ts]"]
end

subgraph group_governance["Guardrails and Operations"]
  node_policy["Contact Policy<br/>[policy.ts]"]
  node_state_machine["Lead State Machine<br/>[state-machine.ts]"]
  node_activity["Activity and Analytics APIs<br/>[route.ts]"]
  node_dead_letters["Dead-Letter Admin<br/>[route.ts]"]
end

subgraph group_persistence["Persistence and Integrations"]
  node_llm["LLM Providers<br/>[llm.ts]"]
  node_integrations["External Integrations<br/>[integrations.ts]"]
  node_db[("PostgreSQL<br/>[db.ts]")]
end

node_user(("Sales User"))

node_user -->|"configures and reviews"| node_ui
node_ui -->|"submits campaign"| node_campaign_api
node_campaign_api -->|"enqueues prospecting"| node_queue
node_queue -->|"dispatches jobs"| node_worker
node_worker -->|"runs agent"| node_agent
node_agent -->|"builds snapshot"| node_snapshot
node_agent -->|"requests decision"| node_decision
node_decision -->|"selects tool"| node_llm
node_agent -->|"executes selected tool"| node_registry
node_registry -->|"dispatches tools"| node_tool_impl
node_tool_impl -.->|"checks contact rules"| node_policy
node_tool_impl -->|"transitions lead"| node_state_machine
node_tool_impl -.->|"uses providers"| node_integrations
node_agent -->|"persists run state"| node_db
node_inbound -->|"enqueues reply"| node_queue
node_event_webhook -->|"updates delivery state"| node_state_machine
node_ui -->|"submits decision"| node_approval_api
node_approval_api -->|"enqueues approved outreach"| node_queue
node_approval_api -->|"rechecks contactability"| node_policy
node_tool_impl -->|"stores lead evidence"| node_db
node_integrations -->|"records integration state"| node_db
node_activity -->|"reads activity data"| node_db
node_dead_letters -.->|"manages failed jobs"| node_db

click node_ui "https://github.com/jamesugbanu/agent-sdr/tree/main/src/app"
click node_campaign_api "https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/campaigns/route.ts"
click node_approval_api "https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/approvals/%5Bid%5D/route.ts"
click node_agent "https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/agents/orchestrator.ts"
click node_snapshot "https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/agents/state.ts"
click node_decision "https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/agents/decisions.ts"
click node_llm "https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/llm.ts"
click node_registry "https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/tools/registry.ts"
click node_tool_impl "https://github.com/jamesugbanu/agent-sdr/tree/main/src/server/tools"
click node_queue "https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/queue.ts"
click node_worker "https://github.com/jamesugbanu/agent-sdr/blob/main/workers/index.ts"
click node_policy "https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/policy.ts"
click node_state_machine "https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/state-machine.ts"
click node_inbound "https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/webhooks/email/route.ts"
click node_event_webhook "https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/webhooks/email-events/route.ts"
click node_integrations "https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/integrations.ts"
click node_db "https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/db.ts"
click node_activity "https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/activity/stream/route.ts"
click node_dead_letters "https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/admin/dead-letters/route.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_ui,node_campaign_api,node_approval_api,node_user toneBlue
class node_agent,node_snapshot,node_decision,node_registry,node_queue,node_worker toneAmber
class node_tool_impl,node_inbound,node_event_webhook toneMint
class node_policy,node_state_machine,node_activity,node_dead_letters toneRose
class node_llm,node_integrations,node_db toneIndigo
```

## Components

### User Experience

- [Campaign and Approval UI](https://github.com/jamesugbanu/agent-sdr/tree/main/src/app): `src/app`
- [Campaign API](https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/campaigns/route.ts): `src/app/api/campaigns/route.ts`
- [Approval API](https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/approvals/%5Bid%5D/route.ts): `src/app/api/approvals/[id]/route.ts`

### Agent Runtime

- [Agent Orchestrator](https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/agents/orchestrator.ts): `src/server/agents/orchestrator.ts`
- [Run State Snapshot](https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/agents/state.ts): `src/server/agents/state.ts`
- [Tool Decision](https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/agents/decisions.ts): `src/server/agents/decisions.ts`
- [Tool Registry](https://github.com/jamesugbanu/agent-sdr/blob/main/src/server/tools/registry.ts): `src/server/tools/registry.ts`
- [Job Queue](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/queue.ts): `src/lib/queue.ts`
- [Background Worker](https://github.com/jamesugbanu/agent-sdr/blob/main/workers/index.ts): `workers/index.ts`

### Lead Lifecycle

- [Lifecycle Tool Implementations](https://github.com/jamesugbanu/agent-sdr/tree/main/src/server/tools): `src/server/tools`
- [Email Webhooks](https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/webhooks/email/route.ts): `src/app/api/webhooks/email/route.ts`
- [Delivery Event Webhook](https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/webhooks/email-events/route.ts): `src/app/api/webhooks/email-events/route.ts`

### Guardrails and Operations

- [Contact Policy](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/policy.ts): `src/lib/policy.ts`
- [Lead State Machine](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/state-machine.ts): `src/lib/state-machine.ts`
- [Activity and Analytics APIs](https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/activity/stream/route.ts): `src/app/api/activity/stream/route.ts`
- [Dead-Letter Admin](https://github.com/jamesugbanu/agent-sdr/blob/main/src/app/api/admin/dead-letters/route.ts): `src/app/api/admin/dead-letters/route.ts`

### Persistence and Integrations

- [LLM Providers](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/llm.ts): `src/lib/llm.ts`
- [External Integrations](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/integrations.ts): `src/lib/integrations.ts`
- [PostgreSQL](https://github.com/jamesugbanu/agent-sdr/blob/main/src/lib/db.ts): `src/lib/db.ts`

### Other components

- **Sales User**

## Connections

- Sales User → Campaign and Approval UI: configures and reviews (evidence: `README.md`)
- Campaign and Approval UI → Campaign API: submits campaign (evidence: `README.md`)
- Campaign API → Job Queue: enqueues prospecting (evidence: `src/app/api/campaigns/route.ts`)
- Job Queue → Background Worker: dispatches jobs (evidence: `README.md`)
- Background Worker → Agent Orchestrator: runs agent (evidence: `workers/index.ts`)
- Agent Orchestrator → Run State Snapshot: builds snapshot (evidence: `src/server/agents/orchestrator.ts`)
- Agent Orchestrator → Tool Decision: requests decision (evidence: `src/server/agents/orchestrator.ts`)
- Tool Decision → LLM Providers: selects tool (evidence: `src/server/agents/decisions.ts`)
- Agent Orchestrator → Tool Registry: executes selected tool (evidence: `README.md`)
- Tool Registry → Lifecycle Tool Implementations: dispatches tools (evidence: `src/server/tools/registry.ts`)
- Lifecycle Tool Implementations → Contact Policy: checks contact rules
- Lifecycle Tool Implementations → Lead State Machine: transitions lead (evidence: `src/server/tools/research.ts`)
- Lifecycle Tool Implementations → External Integrations: uses providers (evidence: `README.md`)
- Agent Orchestrator → PostgreSQL: persists run state (evidence: `README.md`)
- Email Webhooks → Job Queue: enqueues reply (evidence: `src/app/api/webhooks/email/route.ts`)
- Delivery Event Webhook → Lead State Machine: updates delivery state (evidence: `src/app/api/webhooks/email-events/route.ts`)
- Campaign and Approval UI → Approval API: submits decision (evidence: `README.md`)
- Approval API → Job Queue: enqueues approved outreach (evidence: `src/app/api/approvals/[id]/route.ts`)
- Approval API → Contact Policy: rechecks contactability (evidence: `src/app/api/approvals/[id]/route.ts`)
- Lifecycle Tool Implementations → PostgreSQL: stores lead evidence (evidence: `src/server/tools/research.ts`)
- External Integrations → PostgreSQL: records integration state (evidence: `src/lib/integrations.ts`)
- Activity and Analytics APIs → PostgreSQL: reads activity data (evidence: `src/app/api/activity/stream/route.ts`)
- Dead-Letter Admin → PostgreSQL: manages failed jobs
