AgentSkillsCN

architecture

当你需要了解 CCR Worker 如何路由消息、管理会话,或处理 WebSocket 连接时使用。

SKILL.md
--- frontmatter
name: architecture
description: Use when you need to understand how the CCR Worker routes messages, manages sessions, or handles WebSocket connections

CCR Worker Architecture

Overview

The CCR Worker is a Cloudflare Worker that routes Telegram messages to the correct Claude Code Remote instance across multiple machines. It uses a Durable Object for persistent state.

System Flow

code
┌──────────────┐      ┌─────────────────────────────────────┐
│   Telegram   │      │         Cloudflare Worker           │
│              │      │                                     │
│  User sends  │─────▶│  POST /webhook/telegram/{secret}    │
│   message    │      │              │                      │
└──────────────┘      │              ▼                      │
                      │    ┌─────────────────────┐          │
                      │    │   RouterDO (DO)     │          │
                      │    │                     │          │
                      │    │ • SQLite session DB │          │
                      │    │ • message→session   │          │
                      │    │ • session→machine   │          │
                      │    │ • command queue     │          │
                      │    └──────────┬──────────┘          │
                      │               │                     │
                      │               ▼                     │
                      │    WebSocket to Machine Agent       │
                      └───────────────┬─────────────────────┘
                                      │
                      ┌───────────────▼───────────────┐
                      │   Machine (devbox/macbook)    │
                      │                               │
                      │   MachineAgent ◄── WebSocket  │
                      │        │                      │
                      │        ▼                      │
                      │   Inject into Claude session  │
                      └───────────────────────────────┘

Components

Worker Entry Point (src/index.js)

Routes HTTP requests:

  • /webhook/telegram/{secret} → Telegram webhook handler
  • /ws?machineId=X → WebSocket upgrade for machine agents
  • /sessions/* → Session management API
  • /notifications/send → Outbound notification API
  • /health → Health check

Durable Object (src/router-do.js)

Single instance that manages all state:

SQLite Tables:

  • sessions - Maps session_id → machine_id
  • messages - Maps Telegram message_id → session_id (for reply routing)
  • command_queue - Pending commands with retry logic
  • seen_updates - Deduplication for Telegram webhooks

WebSocket Handling:

  • Machine agents connect via /ws?machineId=X
  • Uses WebSocket Hibernation API for cost efficiency
  • Auto-reconnects handled by MachineAgent client

Command Flow:

  1. Telegram webhook arrives with reply to notification
  2. Look up message_idsession_id in messages table
  3. Look up session_idmachine_id in sessions table
  4. Queue command in command_queue
  5. Send via WebSocket to connected machine
  6. Wait for ack, retry if needed

API Endpoints

EndpointMethodAuthDescription
/healthGETNoneReturns "ok"
/sessionsGETAPI KeyList all sessions
/sessions/registerPOSTAPI KeyRegister session→machine mapping
/sessions/unregisterPOSTAPI KeyRemove session
/notifications/sendPOSTAPI KeySend notification, store message mapping
/webhook/telegram/{secret}POSTPath secretTelegram webhook receiver
/wsWebSocketAPI Key (subprotocol)Machine agent connection

Authentication

  • API Key: Passed in X-API-Key header or WebSocket subprotocol
  • Webhook Secret: Telegram's X-Telegram-Bot-Api-Secret-Token header
  • Path Secret: URL path component for webhook endpoint

Message Delivery Guarantees

At-least-once delivery:

  • Commands persisted to command_queue before send attempt
  • Retry sweep runs hourly for unacked commands
  • Exponential backoff on failures
  • Dead letter after 24h

Duplicate handling:

  • seen_updates table prevents replay of Telegram webhooks
  • MachineAgent inbox prevents duplicate command execution

Environment Variables

VariableSourceDescription
TELEGRAM_BOT_TOKENSecretBot token for sending messages
TELEGRAM_WEBHOOK_SECRETSecretValidates Telegram webhooks
CCR_API_KEYSecretAuthenticates machine agents
ALLOWED_CHAT_IDSVarComma-separated allowed Telegram chats

Deployment

See deployment skill.

Related