Архитектура потоков
INBOUND DATA FLOW
📨
Telegram API
:443
webhook/polling
→🤖
Bot
FastAPI
:8000
asyncpg
→🗄️
PostgreSQL
Truth
:5432
↓
🧠
Ollama
LLM
:11434
↔
⚡
n8n
Workflows
:5678
OUTBOUND DATA FLOW
🗄️
PostgreSQL
Result
🤖
Bot
Format
📨
Telegram
Response
Типы потоков
1. Message Flow (Входящие)
User Message
Telegram API
webhook/polling
message_handler
Python Bridge
↓
Normalize
text, voice, photo
Extract
chat_id, user_id
Queue
chat_queue
Store
messages table
↓
Command
CommandHandler
Question
ChatHandler
Task
OperatorHandler
Unknown
DefaultHandler
2. Process Flow (Исполнение)
Process Trigger
intent matched
Create Run
status: queued
Agent Selection
query policy
↓
n8n Path (complex)
Step 1
internal API
Step 2
internal API
Step N
process_run_steps
Direct LLM (simple)
LLM Call
single-step
Log
llm_calls table
↓
Update Run
succeeded/failed
3. Response Flow (Исходящие)
LLM Response
raw
Response Processor
↓
Parse
text, json, md
Format
buttons, links
Split
> 4096 chars
↓
Telegram API
sendMessage(s)
Store
messages table
Log Event
automation_events
4. State Flow (Состояния)
User State Machine:
NEW
→REGISTERED
→ACTIVE
→SUSPENDED
→BANNED
Process State Machine:
QUEUED
→RUNNING
→WAITING_USER
↔SUCCEEDED
DEGRADED
|FAILED
|CANCELLED
Точки хранения данных
| Таблица | Данные | Retention | Индекс |
|---|---|---|---|
| messages | Все сообщения (user + bot) | ∞ | chat_id, created_at |
| process_runs | Запуски процессов | 90 days | process_id, status, user_id |
| process_run_steps | Шаги процесса | 90 days | run_id, step_number |
| llm_calls | Все вызовы LLM | 30 days | run_id, model, created_at |
| automation_events | События автоматизации | 90 days | event_type, created_at |
Точки отказа и Fallback
Точки отказа
- ●Telegram API — rate limits, downtime
- ●Ollama — model loading, OOM, timeout
- ●n8n — workflow errors, webhook failures
- ●External APIs — Tavily, SerpAPI, etc.
Fallback стратегии
- ●LLM — Ollama → GLM → OpenAI
- ●n8n — retry 3x → degraded response
- ●Web Search — Tavily → SerpAPI → cached
- ●Database — connection pool, retry