# aliceljy/telegram-ai-bridge architecture

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

- Interactive diagram: https://gitdiagram.com/aliceljy/telegram-ai-bridge
- Repository: https://github.com/aliceljy/telegram-ai-bridge

## Overview

telegram-ai-bridge routes Telegram conversations to heterogeneous agent backends, with sessions, streamed results, shared group memory, and optional bot-to-bot A2A-TG collaboration. The graph preserves the documented War Room, Discuss, media, and phone-based session capabilities. The main bridge and several runtime modules were not sampled; links involving them rely on README statements, while internal edges are limited to shown source references and calls.

Main entry points: [`bridge.js`](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/bridge.js) (Message orchestration), [`a2a/bus.js`](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/bus.js) (A2A-TG bus), [`shared-context/index.js`](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/index.js) (Shared context API), [`agent/server.js`](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/agent/server.js) (Agent server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_conversation["Telegram workflow"]
  node_bridge["Message orchestration<br/>[bridge.js]"]
  node_commands["Bot commands<br/>[index.js]"]
  node_sessions["Session lifecycle<br/>[sessions.js]"]
  node_discuss["Discuss mode<br/>[discuss-mode.js]"]
end

subgraph group_agents["Agent execution"]
  node_adapters["Agent adapters"]
  node_executor["Local executor<br/>[local-agent.js]"]
  node_agent_server["Agent server<br/>[server.js]"]
  node_agent_providers["Agent capabilities"]
end

subgraph group_collaboration["Shared collaboration"]
  node_context_pipeline["Group context"]
  node_shared_context["Shared context API<br/>[index.js]"]
  node_sqlite[("SQLite store<br/>[sqlite.js]")]
  node_redis[("Redis store<br/>[redis.js]")]
  node_json_store[("JSON store<br/>[json.js]")]
  node_a2a_bus["A2A-TG bus<br/>[bus.js]"]
  node_envelope["A2A envelope<br/>[envelope.js]"]
  node_loop_guard["Loop guard<br/>[loop-guard.js]"]
  node_peer_health["Peer health<br/>[peer-health.js]"]
end

subgraph group_runtime["Runtime services"]
  node_output_relay["Media and output<br/>[output-relay.js]"]
end

node_person(("Telegram user"))
node_telegram(("Telegram"))
node_telegram_external(("Telegram service"))
node_agent_services["Agent CLIs and SDKs"]
node_peer_bots(("Peer bots"))

node_person -->|"messages"| node_telegram
node_telegram -.->|"routes updates"| node_bridge
node_bridge -.->|"dispatches commands"| node_commands
node_bridge -.->|"manages sessions"| node_sessions
node_bridge -.->|"applies room mode"| node_discuss
node_bridge -.->|"runs agent turns"| node_adapters
node_bridge -.->|"builds group context"| node_context_pipeline
node_context_pipeline -.->|"uses shared messages"| node_shared_context
node_context_pipeline -->|"uses token utilities"| node_shared_context
node_shared_context -->|"selects backend"| node_sqlite
node_shared_context -->|"selects backend"| node_redis
node_shared_context -->|"selects backend"| node_json_store
node_bridge -.->|"coordinates bots"| node_a2a_bus
node_a2a_bus -->|"creates and validates"| node_envelope
node_a2a_bus -->|"checks loops"| node_loop_guard
node_a2a_bus -->|"checks peer availability"| node_peer_health
node_a2a_bus -->|"broadcasts messages"| node_peer_bots
node_executor -->|"spawns process"| node_agent_server
node_agent_server -->|"creates backend"| node_adapters
node_agent_server -->|"dispatches capabilities"| node_agent_providers
node_adapters -.->|"invokes SDKs and CLIs"| node_agent_services
node_bridge -.->|"relays media"| node_output_relay
node_bridge -.->|"sends replies and files"| node_telegram_external

click node_bridge "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/bridge.js"
click node_commands "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/commands/index.js"
click node_sessions "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/sessions.js"
click node_discuss "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/discuss-mode.js"
click node_context_pipeline "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/group-context-pipeline.js"
click node_shared_context "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/index.js"
click node_sqlite "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/sqlite.js"
click node_redis "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/redis.js"
click node_json_store "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/json.js"
click node_a2a_bus "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/bus.js"
click node_envelope "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/envelope.js"
click node_loop_guard "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/loop-guard.js"
click node_peer_health "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/peer-health.js"
click node_adapters "https://github.com/AliceLJY/telegram-ai-bridge/tree/main/adapters"
click node_executor "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/executor/local-agent.js"
click node_agent_server "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/agent/server.js"
click node_agent_providers "https://github.com/AliceLJY/telegram-ai-bridge/tree/main/agent/providers"
click node_output_relay "https://github.com/AliceLJY/telegram-ai-bridge/blob/main/output-relay.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_bridge,node_commands,node_sessions,node_discuss,node_person toneBlue
class node_adapters,node_executor,node_agent_server,node_agent_providers toneAmber
class node_context_pipeline,node_shared_context,node_sqlite,node_redis,node_json_store,node_a2a_bus,node_envelope,node_loop_guard,node_peer_health toneMint
class node_output_relay toneRose
class node_telegram,node_telegram_external,node_agent_services,node_peer_bots toneIndigo
```

## Components

### Telegram workflow

- [Message orchestration](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/bridge.js): `bridge.js`
- [Bot commands](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/commands/index.js): `commands/index.js`
- [Session lifecycle](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/sessions.js): `sessions.js`
- [Discuss mode](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/discuss-mode.js): `discuss-mode.js`

### Agent execution

- [Agent adapters](https://github.com/AliceLJY/telegram-ai-bridge/tree/main/adapters): `adapters`
- [Local executor](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/executor/local-agent.js): `executor/local-agent.js`
- [Agent server](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/agent/server.js): `agent/server.js`
- [Agent capabilities](https://github.com/AliceLJY/telegram-ai-bridge/tree/main/agent/providers): `agent/providers`

### Shared collaboration

- [Group context](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/group-context-pipeline.js): `group-context-pipeline.js`
- [Shared context API](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/index.js): `shared-context/index.js`
- [SQLite store](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/sqlite.js): `shared-context/sqlite.js`
- [Redis store](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/redis.js): `shared-context/redis.js`
- [JSON store](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/shared-context/json.js): `shared-context/json.js`
- [A2A-TG bus](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/bus.js): `a2a/bus.js`
- [A2A envelope](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/envelope.js): `a2a/envelope.js`
- [Loop guard](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/loop-guard.js): `a2a/loop-guard.js`
- [Peer health](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/a2a/peer-health.js): `a2a/peer-health.js`

### Runtime services

- [Media and output](https://github.com/AliceLJY/telegram-ai-bridge/blob/main/output-relay.js): `output-relay.js`

### Other components

- **Telegram user**
- **Telegram**
- **Telegram service**
- **Agent CLIs and SDKs**
- **Peer bots**

## Connections

- Telegram user → Telegram: messages (evidence: `README.md`)
- Telegram → Message orchestration: routes updates (evidence: `README.md`)
- Message orchestration → Bot commands: dispatches commands (evidence: `README.md`)
- Message orchestration → Session lifecycle: manages sessions (evidence: `README.md`)
- Message orchestration → Discuss mode: applies room mode (evidence: `README.md`)
- Message orchestration → Agent adapters: runs agent turns (evidence: `README.md`)
- Message orchestration → Group context: builds group context (evidence: `README.md`)
- Group context → Shared context API: uses shared messages (evidence: `README.md`)
- Group context → Shared context API: uses token utilities (evidence: `group-context-pipeline.js`)
- Shared context API → SQLite store: selects backend (evidence: `shared-context/index.js`)
- Shared context API → Redis store: selects backend (evidence: `shared-context/index.js`)
- Shared context API → JSON store: selects backend (evidence: `shared-context/index.js`)
- Message orchestration → A2A-TG bus: coordinates bots (evidence: `README.md`)
- A2A-TG bus → A2A envelope: creates and validates (evidence: `a2a/bus.js`)
- A2A-TG bus → Loop guard: checks loops (evidence: `a2a/bus.js`)
- A2A-TG bus → Peer health: checks peer availability (evidence: `a2a/bus.js`)
- A2A-TG bus → Peer bots: broadcasts messages (evidence: `a2a/bus.js`)
- Local executor → Agent server: spawns process (evidence: `executor/local-agent.js`)
- Agent server → Agent adapters: creates backend (evidence: `agent/server.js`)
- Agent server → Agent capabilities: dispatches capabilities (evidence: `agent/server.js`)
- Agent adapters → Agent CLIs and SDKs: invokes SDKs and CLIs (evidence: `README.md`)
- Message orchestration → Media and output: relays media (evidence: `README.md`)
- Message orchestration → Telegram service: sends replies and files (evidence: `README.md`)
