# coreinch/duecoach architecture

> Architecture diagram of the GitHub repository coreinch/duecoach, 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/coreinch/duecoach
- Repository: https://github.com/coreinch/duecoach

## Overview

Duecoach is a Telegram-based ADHD coaching bot. A message enters through Telegram, is gated and routed by the channel-independent core, then receives a command, safety response, or model-generated coaching reply; the bot also runs reminders and proactive check-ins. SQLite holds consent, conversations, coaching state, and scheduled items. The README documents the playbook, bilingual support, privacy controls, and safety behavior. Edges follow the excerpts and SOURCE INDEX; links involving unsampled implementation details are omitted.

Main entry points: [`duecoach/llm.py`](https://github.com/coreinch/duecoach/blob/main/duecoach/llm.py) (Model and tools loop), [`duecoach/core.py`](https://github.com/coreinch/duecoach/blob/main/duecoach/core.py) (Message router), [`duecoach/chat.py`](https://github.com/coreinch/duecoach/blob/main/duecoach/chat.py) (Reply handling), [`duecoach/db.py`](https://github.com/coreinch/duecoach/blob/main/duecoach/db.py) (SQLite state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_conversation["Conversation handling"]
  node_telegram["Telegram channel<br/>[telegram.py]"]
  node_core["Message router<br/>[core.py]"]
  node_commands["Slash commands<br/>[commands.py]"]
  node_safety["Crisis detection<br/>[crisis.py]"]
end

subgraph group_coaching["Coaching engine"]
  node_chat["Reply handling<br/>[chat.py]"]
  node_llm["Model and tools loop<br/>[llm.py]"]
  node_prompts["Coaching prompts<br/>[prompts.py]"]
  node_playbook["Strategy playbook<br/>[playbook.py]"]
  node_tools["Coaching tools<br/>[tools.py]"]
  node_strings["Bilingual text<br/>[strings.py]"]
end

subgraph group_state["User state"]
  node_config["Runtime configuration<br/>[config.py]"]
  node_db[("SQLite state<br/>[db.py]")]
end

subgraph group_operations["Proactive operations"]
  node_bot["Reminders and check-ins<br/>[bot.py]"]
  node_evalcases["Conversation eval cases<br/>[cases.py]"]
end

node_user(("Telegram user"))
node_provider["Language model provider"]

node_user -->|"sends message"| node_telegram
node_telegram -->|"routes text"| node_core
node_core -->|"checks access"| node_config
node_core -->|"loads user"| node_db
node_core -->|"checks crisis"| node_safety
node_core -->|"dispatches command"| node_commands
node_core -->|"routes coaching"| node_chat
node_commands -->|"invokes coaching"| node_chat
node_commands -->|"reads and updates"| node_db
node_commands -->|"uses instructions"| node_prompts
node_chat -->|"requests reply"| node_llm
node_chat -->|"stores crisis exchange"| node_db
node_chat -->|"formats fixed text"| node_strings
node_llm -.->|"calls model"| node_provider
node_llm -->|"loads and saves context"| node_db
node_llm -->|"uses prompts"| node_prompts
node_llm -->|"uses strategies"| node_playbook
node_llm -->|"runs tool calls"| node_tools
node_llm -->|"uses language text"| node_strings
node_bot -->|"reads schedules"| node_db
node_bot -->|"generates check-ins"| node_llm
node_bot -->|"delivers messages"| node_telegram

click node_telegram "https://github.com/coreinch/duecoach/blob/main/duecoach/channels/telegram.py"
click node_core "https://github.com/coreinch/duecoach/blob/main/duecoach/core.py"
click node_commands "https://github.com/coreinch/duecoach/blob/main/duecoach/commands.py"
click node_safety "https://github.com/coreinch/duecoach/blob/main/duecoach/crisis.py"
click node_chat "https://github.com/coreinch/duecoach/blob/main/duecoach/chat.py"
click node_llm "https://github.com/coreinch/duecoach/blob/main/duecoach/llm.py"
click node_prompts "https://github.com/coreinch/duecoach/blob/main/duecoach/prompts.py"
click node_playbook "https://github.com/coreinch/duecoach/blob/main/duecoach/playbook.py"
click node_tools "https://github.com/coreinch/duecoach/blob/main/duecoach/tools.py"
click node_strings "https://github.com/coreinch/duecoach/blob/main/duecoach/strings.py"
click node_config "https://github.com/coreinch/duecoach/blob/main/duecoach/config.py"
click node_db "https://github.com/coreinch/duecoach/blob/main/duecoach/db.py"
click node_bot "https://github.com/coreinch/duecoach/blob/main/duecoach/bot.py"
click node_evalcases "https://github.com/coreinch/duecoach/blob/main/evals/cases.py"

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_telegram,node_core,node_commands,node_safety,node_user toneBlue
class node_chat,node_llm,node_prompts,node_playbook,node_tools,node_strings toneAmber
class node_config,node_db toneMint
class node_bot,node_evalcases toneRose
class node_provider toneIndigo
```

## Components

### Conversation handling

- [Telegram channel](https://github.com/coreinch/duecoach/blob/main/duecoach/channels/telegram.py): `duecoach/channels/telegram.py`
- [Message router](https://github.com/coreinch/duecoach/blob/main/duecoach/core.py): `duecoach/core.py`
- [Slash commands](https://github.com/coreinch/duecoach/blob/main/duecoach/commands.py): `duecoach/commands.py`
- [Crisis detection](https://github.com/coreinch/duecoach/blob/main/duecoach/crisis.py): `duecoach/crisis.py`

### Coaching engine

- [Reply handling](https://github.com/coreinch/duecoach/blob/main/duecoach/chat.py): `duecoach/chat.py`
- [Model and tools loop](https://github.com/coreinch/duecoach/blob/main/duecoach/llm.py): `duecoach/llm.py`
- [Coaching prompts](https://github.com/coreinch/duecoach/blob/main/duecoach/prompts.py): `duecoach/prompts.py`
- [Strategy playbook](https://github.com/coreinch/duecoach/blob/main/duecoach/playbook.py): `duecoach/playbook.py`
- [Coaching tools](https://github.com/coreinch/duecoach/blob/main/duecoach/tools.py): `duecoach/tools.py`
- [Bilingual text](https://github.com/coreinch/duecoach/blob/main/duecoach/strings.py): `duecoach/strings.py`

### User state

- [Runtime configuration](https://github.com/coreinch/duecoach/blob/main/duecoach/config.py): `duecoach/config.py`
- [SQLite state](https://github.com/coreinch/duecoach/blob/main/duecoach/db.py): `duecoach/db.py`

### Proactive operations

- [Reminders and check-ins](https://github.com/coreinch/duecoach/blob/main/duecoach/bot.py): `duecoach/bot.py`
- [Conversation eval cases](https://github.com/coreinch/duecoach/blob/main/evals/cases.py): `evals/cases.py`

### Other components

- **Telegram user**
- **Language model provider**

## Connections

- Telegram user → Telegram channel: sends message (evidence: `duecoach/channels/telegram.py`)
- Telegram channel → Message router: routes text (evidence: `duecoach/channels/telegram.py`)
- Message router → Runtime configuration: checks access (evidence: `duecoach/core.py`)
- Message router → SQLite state: loads user (evidence: `duecoach/core.py`)
- Message router → Crisis detection: checks crisis (evidence: `duecoach/core.py`)
- Message router → Slash commands: dispatches command (evidence: `duecoach/core.py`)
- Message router → Reply handling: routes coaching (evidence: `duecoach/core.py`)
- Slash commands → Reply handling: invokes coaching (evidence: `duecoach/commands.py`)
- Slash commands → SQLite state: reads and updates (evidence: `duecoach/commands.py`)
- Slash commands → Coaching prompts: uses instructions (evidence: `duecoach/commands.py`)
- Reply handling → Model and tools loop: requests reply (evidence: `duecoach/chat.py`)
- Reply handling → SQLite state: stores crisis exchange (evidence: `duecoach/chat.py`)
- Reply handling → Bilingual text: formats fixed text (evidence: `duecoach/chat.py`)
- Model and tools loop → Language model provider: calls model
- Model and tools loop → SQLite state: loads and saves context (evidence: `duecoach/llm.py`)
- Model and tools loop → Coaching prompts: uses prompts (evidence: `duecoach/llm.py`)
- Model and tools loop → Strategy playbook: uses strategies (evidence: `duecoach/llm.py`)
- Model and tools loop → Coaching tools: runs tool calls (evidence: `duecoach/llm.py`)
- Model and tools loop → Bilingual text: uses language text (evidence: `duecoach/llm.py`)
- Reminders and check-ins → SQLite state: reads schedules (evidence: `duecoach/bot.py`)
- Reminders and check-ins → Model and tools loop: generates check-ins (evidence: `duecoach/bot.py`)
- Reminders and check-ins → Telegram channel: delivers messages (evidence: `duecoach/bot.py`)
