Wiki/Scenarios

Сценарии: Послойная отработка

Как пользовательский запрос проходит через все слои платформы

🎯Ключевая идея

Любой сценарий в clowbot проходит 4 обязательных слоя: PostgreSQL → Python Bridge → n8n/Agents → LLM. Понимание этого потока — ключ к созданию надёжных сценариев.

01Послойная диаграмма

USER MESSAGE

LAYER 0: Telegram

📨
Telegram API
• long-polling
• text/voice/photo/command
• chat context loaded

LAYER 1: Python Bridge (FastAPI)

Normalize
Auth/RBAC
Queue
Classify
Route

LAYER 2: PostgreSQL (Single Source of Truth)

User context
Messages
runtime_policy
agent_selection
process_run

LAYER 3A: n8n (Workflows)

• Web search
• Reminders
• Content
• Multi-step

LAYER 3B: Agent (Direct LLM)

• Quick reply
• Simple chat
• Clarification

LAYER 4: LLM Provider (Ollama / OpenAI / GLM)

🧠
Prompt Assembly
🧠
Model Inference
🧠
Response Parse
🗄️
Log to DB

LAYER 5: Response Assembly

Format
Split >4096
Add artifacts
Update status

LAYER 6: Telegram Response

📨
Send Message
🗄️
Store to DB
Log Event
USER SEES REPLY

Время прохождения слоёв

~50ms
Telegram → Bridge
~100ms
Bridge → DB
~500ms
DB → n8n/Agent
1-30s
LLM Generation

02Примеры сценариев

Сценарий 1: Simple Chat

User: "Привет, как дела?"

User
Telegram
Bridge
DB
Agent
LLM
Response

Сценарий 2: Web Search

User: "Найди информацию о..."

User
Bridge
DB
n8n
Tavily
LLM
Response

Сценарий 3: Reminder

User: "Напомни мне через 5 минут"

User
Operator
DB
reminders
n8n
scheduler
Trigger
Notify

03Сценарии отказа

LLM Timeout

1. LLM не отвечает за 300s

2. Fallback: degraded response

3. Log to llm_calls (status: timeout)

4. User: "Попробуйте позже"

n8n Unavailable

1. n8n webhook timeout

2. Retry 3x with backoff

3. Fallback: direct LLM path

4. Log to automation_events

DB Connection Lost

1. asyncpg pool exhausted

2. Wait for connection

3. If timeout: graceful shutdown

4. Restart with health check

Rate Limited

1. User exceeds daily limit

2. Check user.tier

3. Return friendly message

4. Suggest upgrade if free tier

04Тестирование сценариев

Swarm Live E2E Rule

Каждый активный процесс должен быть верифицирован в живом Telegram сценарии.

Требования к сценарию:
  • • Один бот триггерит другой через /cmd@bot
  • • No fan-out (не все боты отвечают)
  • • Chain progression visible in Telegram
  • • Auditable in DB
  • • Status via /swarm_test status
Group Command Safety:
  • • Unaddressed group commands must NOT trigger all bots
  • • Primary bot may return addressing hint
  • • Silent failure is better than spam

Связанные страницы

Навыки для понимания

Layer architectureError handlingFallback chainsE2E testingObservability