# erxes/erxes-ai-assistant-gateway architecture

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

- Interactive diagram: https://gitdiagram.com/erxes/erxes-ai-assistant-gateway
- Repository: https://github.com/erxes/erxes-ai-assistant-gateway

## Overview

The product is a shared Discord gateway that maps a guild/channel to a tenant assistant and runtime, then delivers the assistant’s answer back to Discord. Its main flow is Discord interactions or messages → channel binding → runtime request → reply, with separate OAuth installation, Erxes-managed binding, and cron delivery capabilities. The graph reflects documented and indexed connections; unsampled implementation details are not inferred.

Main entry points: [`src/discord/messageGateway.ts`](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/messageGateway.ts) (Message gateway), [`src/app.ts`](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/app.ts) (HTTP gateway), [`src/routes/discordOAuth.ts`](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/discordOAuth.ts) (Discord OAuth), [`src/models/DiscordAssistantBinding.ts`](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/models/DiscordAssistantBinding.ts) (Channel bindings)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingress["Gateway ingress"]
  node_gateway_app["HTTP gateway<br/>[app.ts]"]
  node_interaction_route["Interaction routes"]
  node_cron_route["Cron webhook<br/>[cronWebhook.ts]"]
  node_oauth_route["Discord OAuth<br/>[discordOAuth.ts]"]
end

subgraph group_discord["Discord handling"]
  node_discord_api["Discord API client<br/>[api.ts]"]
  node_message_gateway["Message gateway<br/>[messageGateway.ts]"]
  node_channel_actions["Channel actions<br/>[channelActions.ts]"]
  node_attachment_processing["Attachment processing<br/>[attachments.ts]"]
  node_document_processing["Document extraction<br/>[documents.ts]"]
  node_spreadsheet_processing["Spreadsheet extraction<br/>[spreadsheets.ts]"]
  node_presentation_processing["Presentation extraction<br/>[presentations.ts]"]
  node_followup_route["Follow-up delivery<br/>[discordFollowup.ts]"]
end

subgraph group_admin["Installation and bindings"]
  node_admin_installations["Installation API"]
  node_admin_bindings["Binding API<br/>[adminBindings.ts]"]
  node_binding_lifecycle["Binding lifecycle"]
  node_oauth_flow["OAuth helpers<br/>[oauth.ts]"]
end

subgraph group_runtime["Assistant and state"]
  node_runtime_client["Runtime client<br/>[client.ts]"]
  node_runtime_identity["Runtime signing<br/>[identity.ts]"]
  node_binding_records[("Channel bindings")]
  node_installation_records[("Installations")]
  node_oauth_states[("OAuth states<br/>[OAuthState.ts]")]
  node_mongo[("MongoDB connection<br/>[mongo.ts]")]
end

node_discord_users(("Discord users"))
node_erxes_admin(("Erxes Admin"))
node_runtime_service["Assistant runtime"]

node_discord_users -->|"sends requests"| node_gateway_app
node_gateway_app -->|"routes interactions"| node_interaction_route
node_gateway_app -->|"routes OAuth"| node_oauth_route
node_gateway_app -->|"routes API"| node_admin_installations
node_gateway_app -->|"routes API"| node_admin_bindings
node_gateway_app -->|"routes webhooks"| node_cron_route
node_interaction_route -->|"dispatches messages"| node_message_gateway
node_message_gateway -->|"looks up bindings"| node_binding_records
node_message_gateway -->|"asks runtime"| node_runtime_client
node_runtime_client -->|"signs requests"| node_runtime_identity
node_runtime_client -->|"sends requests"| node_runtime_service
node_runtime_service -->|"returns answer"| node_runtime_client
node_message_gateway -->|"sends replies"| node_discord_api
node_message_gateway -->|"checks attachments"| node_attachment_processing
node_message_gateway -->|"extracts documents"| node_document_processing
node_message_gateway -->|"extracts spreadsheets"| node_spreadsheet_processing
node_message_gateway -->|"extracts presentations"| node_presentation_processing
node_admin_bindings -->|"manages bindings"| node_binding_records
node_admin_bindings -->|"delegates lifecycle"| node_binding_lifecycle
node_admin_installations -->|"lists installations"| node_installation_records
node_erxes_admin -->|"configures channels"| node_admin_bindings
node_erxes_admin -->|"manages installs"| node_admin_installations
node_oauth_route -->|"builds install flow"| node_oauth_flow
node_oauth_route -->|"stores OAuth state"| node_oauth_states
node_oauth_route -->|"saves installation"| node_installation_records
node_oauth_route -->|"exchanges OAuth"| node_discord_api
node_cron_route -->|"selects binding"| node_binding_records
node_cron_route -->|"posts cron result"| node_discord_api
node_followup_route -->|"uses delivery"| node_message_gateway
node_followup_route -->|"loads binding"| node_binding_records
node_mongo -.->|"persists records"| node_binding_records
node_mongo -.->|"persists records"| node_installation_records
node_mongo -.->|"persists records"| node_oauth_states

click node_gateway_app "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/app.ts"
click node_interaction_route "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/discordInteractions.ts"
click node_cron_route "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/cronWebhook.ts"
click node_oauth_route "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/discordOAuth.ts"
click node_admin_installations "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/adminInstallations.ts"
click node_admin_bindings "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/adminBindings.ts"
click node_binding_lifecycle "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/bindingLifecycle.ts"
click node_oauth_flow "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/oauth.ts"
click node_discord_api "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/api.ts"
click node_message_gateway "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/messageGateway.ts"
click node_channel_actions "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/channelActions.ts"
click node_attachment_processing "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/attachments.ts"
click node_document_processing "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/documents.ts"
click node_spreadsheet_processing "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/spreadsheets.ts"
click node_presentation_processing "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/presentations.ts"
click node_followup_route "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/discordFollowup.ts"
click node_runtime_client "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/openclaw/client.ts"
click node_runtime_identity "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/runtime/identity.ts"
click node_binding_records "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/models/DiscordAssistantBinding.ts"
click node_installation_records "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/models/DiscordInstallation.ts"
click node_oauth_states "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/models/OAuthState.ts"
click node_mongo "https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/db/mongo.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_gateway_app,node_interaction_route,node_cron_route,node_oauth_route,node_discord_users toneBlue
class node_discord_api,node_message_gateway,node_channel_actions,node_attachment_processing,node_document_processing,node_spreadsheet_processing,node_presentation_processing,node_followup_route toneAmber
class node_admin_installations,node_admin_bindings,node_binding_lifecycle,node_oauth_flow toneMint
class node_runtime_client,node_runtime_identity,node_binding_records,node_installation_records,node_oauth_states,node_mongo toneRose
class node_erxes_admin,node_runtime_service toneIndigo
```

## Components

### Gateway ingress

- [HTTP gateway](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/app.ts): `src/app.ts`
- [Interaction routes](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/discordInteractions.ts): `src/routes/discordInteractions.ts`
- [Cron webhook](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/cronWebhook.ts): `src/routes/cronWebhook.ts`
- [Discord OAuth](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/discordOAuth.ts): `src/routes/discordOAuth.ts`

### Discord handling

- [Discord API client](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/api.ts): `src/discord/api.ts`
- [Message gateway](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/messageGateway.ts): `src/discord/messageGateway.ts`
- [Channel actions](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/channelActions.ts): `src/discord/channelActions.ts`
- [Attachment processing](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/attachments.ts): `src/discord/attachments.ts`
- [Document extraction](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/documents.ts): `src/discord/documents.ts`
- [Spreadsheet extraction](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/spreadsheets.ts): `src/discord/spreadsheets.ts`
- [Presentation extraction](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/presentations.ts): `src/discord/presentations.ts`
- [Follow-up delivery](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/discordFollowup.ts): `src/routes/discordFollowup.ts`

### Installation and bindings

- [Installation API](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/adminInstallations.ts): `src/routes/adminInstallations.ts`
- [Binding API](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/routes/adminBindings.ts): `src/routes/adminBindings.ts`
- [Binding lifecycle](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/bindingLifecycle.ts): `src/discord/bindingLifecycle.ts`
- [OAuth helpers](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/discord/oauth.ts): `src/discord/oauth.ts`

### Assistant and state

- [Runtime client](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/openclaw/client.ts): `src/openclaw/client.ts`
- [Runtime signing](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/runtime/identity.ts): `src/runtime/identity.ts`
- [Channel bindings](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/models/DiscordAssistantBinding.ts): `src/models/DiscordAssistantBinding.ts`
- [Installations](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/models/DiscordInstallation.ts): `src/models/DiscordInstallation.ts`
- [OAuth states](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/models/OAuthState.ts): `src/models/OAuthState.ts`
- [MongoDB connection](https://github.com/erxes/erxes-ai-assistant-gateway/blob/main/src/db/mongo.ts): `src/db/mongo.ts`

### Other components

- **Discord users**
- **Erxes Admin**
- **Assistant runtime**

## Connections

- Discord users → HTTP gateway: sends requests (evidence: `README.md`)
- HTTP gateway → Interaction routes: routes interactions (evidence: `src/app.ts`)
- HTTP gateway → Discord OAuth: routes OAuth (evidence: `src/app.ts`)
- HTTP gateway → Installation API: routes API (evidence: `src/app.ts`)
- HTTP gateway → Binding API: routes API (evidence: `src/app.ts`)
- HTTP gateway → Cron webhook: routes webhooks (evidence: `src/app.ts`)
- Interaction routes → Message gateway: dispatches messages (evidence: `src/routes/discordInteractions.ts`)
- Message gateway → Channel bindings: looks up bindings (evidence: `src/discord/messageGateway.ts`)
- Message gateway → Runtime client: asks runtime (evidence: `src/discord/messageGateway.ts`)
- Runtime client → Runtime signing: signs requests (evidence: `src/openclaw/client.ts`)
- Runtime client → Assistant runtime: sends requests (evidence: `README.md`)
- Assistant runtime → Runtime client: returns answer (evidence: `README.md`)
- Message gateway → Discord API client: sends replies (evidence: `README.md`)
- Message gateway → Attachment processing: checks attachments (evidence: `src/discord/messageGateway.ts`)
- Message gateway → Document extraction: extracts documents (evidence: `src/discord/messageGateway.ts`)
- Message gateway → Spreadsheet extraction: extracts spreadsheets (evidence: `src/discord/messageGateway.ts`)
- Message gateway → Presentation extraction: extracts presentations (evidence: `src/discord/messageGateway.ts`)
- Binding API → Channel bindings: manages bindings (evidence: `src/routes/adminBindings.ts`)
- Binding API → Binding lifecycle: delegates lifecycle (evidence: `src/routes/adminBindings.ts`)
- Installation API → Installations: lists installations (evidence: `src/routes/adminInstallations.ts`)
- Erxes Admin → Binding API: configures channels (evidence: `README.md`)
- Erxes Admin → Installation API: manages installs (evidence: `README.md`)
- Discord OAuth → OAuth helpers: builds install flow (evidence: `src/routes/discordOAuth.ts`)
- Discord OAuth → OAuth states: stores OAuth state (evidence: `src/routes/discordOAuth.ts`)
- Discord OAuth → Installations: saves installation (evidence: `src/routes/discordOAuth.ts`)
- Discord OAuth → Discord API client: exchanges OAuth (evidence: `src/routes/discordOAuth.ts`)
- Cron webhook → Channel bindings: selects binding (evidence: `src/routes/cronWebhook.ts`)
- Cron webhook → Discord API client: posts cron result (evidence: `src/routes/cronWebhook.ts`)
- Follow-up delivery → Message gateway: uses delivery (evidence: `src/routes/discordFollowup.ts`)
- Follow-up delivery → Channel bindings: loads binding (evidence: `src/routes/discordFollowup.ts`)
- MongoDB connection → Channel bindings: persists records
- MongoDB connection → Installations: persists records
- MongoDB connection → OAuth states: persists records
