# shivighn/twilight_agent architecture

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

- Interactive diagram: https://gitdiagram.com/shivighn/twilight_agent
- Repository: https://github.com/shivighn/twilight_agent

## Overview

Twilight Agent combines a WhatsApp gateway and Python tool-running agent for petty-cash automation, plus a scheduled fleet-issue assignment and escalation feature. The main workflow filters WhatsApp messages, forwards text or downloaded media to the agent, selects or runs a tool, persists results, and returns chat feedback. The fleet-issue branch fetches eligible issues, assigns and tracks them, then sends grouped notifications and escalations. Edges reflect shown calls or documented HTTP behavior; unsampled implementation details are omitted.

Main entry points: [`src/fleetIssues/index.js`](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/index.js) (Fleet run coordinator), [`src/whatsapp/messageHandler.js`](https://github.com/shivighn/twilight_agent/blob/main/src/whatsapp/messageHandler.js) (Message handler), [`src/fleetIssues/db.js`](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/db.js) (Fleet data access), [`src/fleetIssues/issueAssigner.js`](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/issueAssigner.js) (Issue assignment)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["WhatsApp Gateway"]
  node_wa_client["WhatsApp client<br/>[client.js]"]
  node_message_handler["Message handler<br/>[messageHandler.js]"]
  node_media["Media extraction<br/>[extractor.js]"]
  node_storage[("Gateway storage<br/>[storage.js]")]
  node_gateway_config["Gateway configuration<br/>[index.js]"]
  node_gateway_entry["Gateway entry<br/>[index.js]"]
end

subgraph group_agent["Agent Processing"]
  node_agent_api["Agent API<br/>[main.py]"]
  node_agent_runner["Agent runner<br/>[agent.py]"]
  node_tool_registry["Tool registry<br/>[tool_registry.py]"]
  node_base_tool["Tool interface<br/>[base_tool.py]"]
  node_petty_cash["Petty-cash extraction"]
  node_opening_balance["Opening balance"]
  node_llm_dispatch["LLM dispatcher<br/>[llm_client.py]"]
  node_nvidia["NVIDIA client<br/>[nvidia_client.py]"]
  node_openrouter["OpenRouter client"]
  node_agent_config["Agent configuration<br/>[config.py]"]
end

subgraph group_fleet["Fleet Issues"]
  node_scheduler["Fleet scheduler<br/>[scheduler.js]"]
  node_fleet_run["Fleet run coordinator<br/>[index.js]"]
  node_issue_fetch["Issue fetching<br/>[issueFetcher.js]"]
  node_issue_assign["Issue assignment<br/>[issueAssigner.js]"]
  node_fm_resolver["Manager resolution<br/>[fmResolver.js]"]
  node_escalation["Escalation checks"]
  node_tracking[("Notification tracking")]
  node_message_builder["Notification builder<br/>[messageBuilder.js]"]
  node_fleet_sender["WhatsApp sender<br/>[whatsappSender.js]"]
  node_run_logger["Run logging<br/>[runLogger.js]"]
end

subgraph group_data["Persistence and Providers"]
  node_agent_db["Agent database access<br/>[db.py]"]
  node_fleet_db["Fleet data access<br/>[db.js]"]
end

node_whatsapp(("WhatsApp groups"))
node_supabase[("Supabase")]

node_whatsapp -->|"sends messages"| node_wa_client
node_gateway_entry -->|"starts connection"| node_wa_client
node_gateway_entry -->|"registers handler"| node_message_handler
node_wa_client -->|"dispatches messages"| node_message_handler
node_message_handler -->|"extracts media"| node_media
node_message_handler -->|"queues media"| node_storage
node_message_handler -->|"POSTs messages"| node_agent_api
node_agent_api -->|"runs processing"| node_agent_runner
node_agent_api -->|"registers tools"| node_tool_registry
node_agent_runner -->|"selects tools"| node_tool_registry
node_tool_registry -->|"uses interface"| node_base_tool
node_agent_runner -->|"requests selection"| node_llm_dispatch
node_llm_dispatch -->|"tries provider"| node_nvidia
node_llm_dispatch -->|"falls back"| node_openrouter
node_petty_cash -->|"extracts with vision"| node_llm_dispatch
node_petty_cash -->|"writes entry"| node_agent_db
node_opening_balance -->|"upserts balance"| node_agent_db
node_agent_db -->|"uses PostgREST"| node_supabase
node_nvidia -->|"reads settings"| node_agent_config
node_scheduler -->|"schedules runs"| node_fleet_run
node_gateway_entry -->|"starts scheduler"| node_scheduler
node_fleet_run -->|"fetches issues"| node_issue_fetch
node_fleet_run -->|"assigns new issues"| node_issue_assign
node_fleet_run -->|"reads tracking"| node_tracking
node_fleet_run -->|"checks escalation"| node_escalation
node_fleet_run -->|"builds notifications"| node_message_builder
node_fleet_run -->|"sends notifications"| node_fleet_sender
node_fleet_run -->|"records run"| node_run_logger
node_issue_fetch -->|"queries issues"| node_fleet_db
node_issue_assign -->|"updates assignment"| node_fleet_db
node_issue_assign -->|"resolves manager"| node_fm_resolver
node_issue_assign -->|"checks notifications"| node_tracking
node_escalation -->|"resets status"| node_tracking
node_tracking -->|"persists state"| node_fleet_db
node_fleet_sender -->|"sends via client"| node_wa_client
node_run_logger -->|"stores run data"| node_fleet_db
node_fleet_db -->|"uses PostgREST"| node_supabase

click node_wa_client "https://github.com/shivighn/twilight_agent/blob/main/src/whatsapp/client.js"
click node_message_handler "https://github.com/shivighn/twilight_agent/blob/main/src/whatsapp/messageHandler.js"
click node_media "https://github.com/shivighn/twilight_agent/blob/main/src/media/extractor.js"
click node_storage "https://github.com/shivighn/twilight_agent/blob/main/src/utils/storage.js"
click node_agent_api "https://github.com/shivighn/twilight_agent/blob/main/agent/main.py"
click node_agent_runner "https://github.com/shivighn/twilight_agent/blob/main/agent/agent/agent.py"
click node_tool_registry "https://github.com/shivighn/twilight_agent/blob/main/agent/agent/tool_registry.py"
click node_base_tool "https://github.com/shivighn/twilight_agent/blob/main/agent/tools/base_tool.py"
click node_petty_cash "https://github.com/shivighn/twilight_agent/blob/main/agent/tools/petty_cash_extractor_tool.py"
click node_opening_balance "https://github.com/shivighn/twilight_agent/blob/main/agent/tools/set_opening_balance_tool.py"
click node_llm_dispatch "https://github.com/shivighn/twilight_agent/blob/main/agent/llm/llm_client.py"
click node_nvidia "https://github.com/shivighn/twilight_agent/blob/main/agent/llm/nvidia_client.py"
click node_openrouter "https://github.com/shivighn/twilight_agent/blob/main/agent/llm/openrouter_client.py"
click node_agent_config "https://github.com/shivighn/twilight_agent/blob/main/agent/config.py"
click node_agent_db "https://github.com/shivighn/twilight_agent/blob/main/agent/db.py"
click node_scheduler "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/scheduler.js"
click node_fleet_run "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/index.js"
click node_issue_fetch "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/issueFetcher.js"
click node_issue_assign "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/issueAssigner.js"
click node_fm_resolver "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/fmResolver.js"
click node_escalation "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/escalationManager.js"
click node_tracking "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/notificationTracker.js"
click node_message_builder "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/messageBuilder.js"
click node_fleet_sender "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/whatsappSender.js"
click node_fleet_db "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/db.js"
click node_run_logger "https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/runLogger.js"
click node_gateway_config "https://github.com/shivighn/twilight_agent/blob/main/src/config/index.js"
click node_gateway_entry "https://github.com/shivighn/twilight_agent/blob/main/src/index.js"

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_wa_client,node_message_handler,node_media,node_storage,node_gateway_config,node_gateway_entry toneBlue
class node_agent_api,node_agent_runner,node_tool_registry,node_base_tool,node_petty_cash,node_opening_balance,node_llm_dispatch,node_nvidia,node_openrouter,node_agent_config,node_supabase toneAmber
class node_scheduler,node_fleet_run,node_issue_fetch,node_issue_assign,node_fm_resolver,node_escalation,node_tracking,node_message_builder,node_fleet_sender,node_run_logger toneMint
class node_agent_db,node_fleet_db toneRose
class node_whatsapp toneIndigo
```

## Components

### WhatsApp Gateway

- [WhatsApp client](https://github.com/shivighn/twilight_agent/blob/main/src/whatsapp/client.js): `src/whatsapp/client.js`
- [Message handler](https://github.com/shivighn/twilight_agent/blob/main/src/whatsapp/messageHandler.js): `src/whatsapp/messageHandler.js`
- [Media extraction](https://github.com/shivighn/twilight_agent/blob/main/src/media/extractor.js): `src/media/extractor.js`
- [Gateway storage](https://github.com/shivighn/twilight_agent/blob/main/src/utils/storage.js): `src/utils/storage.js`
- [Gateway configuration](https://github.com/shivighn/twilight_agent/blob/main/src/config/index.js): `src/config/index.js`
- [Gateway entry](https://github.com/shivighn/twilight_agent/blob/main/src/index.js): `src/index.js`

### Agent Processing

- [Agent API](https://github.com/shivighn/twilight_agent/blob/main/agent/main.py): `agent/main.py`
- [Agent runner](https://github.com/shivighn/twilight_agent/blob/main/agent/agent/agent.py): `agent/agent/agent.py`
- [Tool registry](https://github.com/shivighn/twilight_agent/blob/main/agent/agent/tool_registry.py): `agent/agent/tool_registry.py`
- [Tool interface](https://github.com/shivighn/twilight_agent/blob/main/agent/tools/base_tool.py): `agent/tools/base_tool.py`
- [Petty-cash extraction](https://github.com/shivighn/twilight_agent/blob/main/agent/tools/petty_cash_extractor_tool.py): `agent/tools/petty_cash_extractor_tool.py`
- [Opening balance](https://github.com/shivighn/twilight_agent/blob/main/agent/tools/set_opening_balance_tool.py): `agent/tools/set_opening_balance_tool.py`
- [LLM dispatcher](https://github.com/shivighn/twilight_agent/blob/main/agent/llm/llm_client.py): `agent/llm/llm_client.py`
- [NVIDIA client](https://github.com/shivighn/twilight_agent/blob/main/agent/llm/nvidia_client.py): `agent/llm/nvidia_client.py`
- [OpenRouter client](https://github.com/shivighn/twilight_agent/blob/main/agent/llm/openrouter_client.py): `agent/llm/openrouter_client.py`
- [Agent configuration](https://github.com/shivighn/twilight_agent/blob/main/agent/config.py): `agent/config.py`

### Fleet Issues

- [Fleet scheduler](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/scheduler.js): `src/fleetIssues/scheduler.js`
- [Fleet run coordinator](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/index.js): `src/fleetIssues/index.js`
- [Issue fetching](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/issueFetcher.js): `src/fleetIssues/issueFetcher.js`
- [Issue assignment](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/issueAssigner.js): `src/fleetIssues/issueAssigner.js`
- [Manager resolution](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/fmResolver.js): `src/fleetIssues/fmResolver.js`
- [Escalation checks](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/escalationManager.js): `src/fleetIssues/escalationManager.js`
- [Notification tracking](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/notificationTracker.js): `src/fleetIssues/notificationTracker.js`
- [Notification builder](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/messageBuilder.js): `src/fleetIssues/messageBuilder.js`
- [WhatsApp sender](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/whatsappSender.js): `src/fleetIssues/whatsappSender.js`
- [Run logging](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/runLogger.js): `src/fleetIssues/runLogger.js`

### Persistence and Providers

- [Agent database access](https://github.com/shivighn/twilight_agent/blob/main/agent/db.py): `agent/db.py`
- [Fleet data access](https://github.com/shivighn/twilight_agent/blob/main/src/fleetIssues/db.js): `src/fleetIssues/db.js`

### Other components

- **WhatsApp groups**
- **Supabase**

## Connections

- WhatsApp groups → WhatsApp client: sends messages (evidence: `src/whatsapp/client.js`)
- Gateway entry → WhatsApp client: starts connection (evidence: `src/index.js`)
- Gateway entry → Message handler: registers handler (evidence: `src/index.js`)
- WhatsApp client → Message handler: dispatches messages (evidence: `src/whatsapp/client.js`)
- Message handler → Media extraction: extracts media (evidence: `src/whatsapp/messageHandler.js`)
- Message handler → Gateway storage: queues media (evidence: `src/whatsapp/messageHandler.js`)
- Message handler → Agent API: POSTs messages (evidence: `src/whatsapp/messageHandler.js`)
- Agent API → Agent runner: runs processing (evidence: `agent/main.py`)
- Agent API → Tool registry: registers tools (evidence: `agent/main.py`)
- Agent runner → Tool registry: selects tools (evidence: `agent/agent/agent.py`)
- Tool registry → Tool interface: uses interface (evidence: `agent/agent/tool_registry.py`)
- Agent runner → LLM dispatcher: requests selection (evidence: `agent/agent/agent.py`)
- LLM dispatcher → NVIDIA client: tries provider (evidence: `agent/llm/llm_client.py`)
- LLM dispatcher → OpenRouter client: falls back (evidence: `agent/llm/llm_client.py`)
- Petty-cash extraction → LLM dispatcher: extracts with vision (evidence: `agent/tools/petty_cash_extractor_tool.py`)
- Petty-cash extraction → Agent database access: writes entry (evidence: `agent/tools/petty_cash_extractor_tool.py`)
- Opening balance → Agent database access: upserts balance (evidence: `agent/tools/set_opening_balance_tool.py`)
- Agent database access → Supabase: uses PostgREST (evidence: `agent/db.py`)
- NVIDIA client → Agent configuration: reads settings (evidence: `agent/llm/nvidia_client.py`)
- Fleet scheduler → Fleet run coordinator: schedules runs (evidence: `src/fleetIssues/scheduler.js`)
- Gateway entry → Fleet scheduler: starts scheduler (evidence: `src/index.js`)
- Fleet run coordinator → Issue fetching: fetches issues (evidence: `src/fleetIssues/index.js`)
- Fleet run coordinator → Issue assignment: assigns new issues (evidence: `src/fleetIssues/index.js`)
- Fleet run coordinator → Notification tracking: reads tracking (evidence: `src/fleetIssues/index.js`)
- Fleet run coordinator → Escalation checks: checks escalation (evidence: `src/fleetIssues/index.js`)
- Fleet run coordinator → Notification builder: builds notifications (evidence: `src/fleetIssues/index.js`)
- Fleet run coordinator → WhatsApp sender: sends notifications (evidence: `src/fleetIssues/index.js`)
- Fleet run coordinator → Run logging: records run (evidence: `src/fleetIssues/index.js`)
- Issue fetching → Fleet data access: queries issues (evidence: `src/fleetIssues/issueFetcher.js`)
- Issue assignment → Fleet data access: updates assignment (evidence: `src/fleetIssues/issueAssigner.js`)
- Issue assignment → Manager resolution: resolves manager (evidence: `src/fleetIssues/issueAssigner.js`)
- Issue assignment → Notification tracking: checks notifications (evidence: `src/fleetIssues/issueAssigner.js`)
- Escalation checks → Notification tracking: resets status (evidence: `src/fleetIssues/escalationManager.js`)
- Notification tracking → Fleet data access: persists state (evidence: `src/fleetIssues/notificationTracker.js`)
- WhatsApp sender → WhatsApp client: sends via client (evidence: `src/fleetIssues/whatsappSender.js`)
- Run logging → Fleet data access: stores run data (evidence: `src/fleetIssues/runLogger.js`)
- Fleet data access → Supabase: uses PostgREST (evidence: `src/fleetIssues/db.js`)
