Architecture
replybots compiles to a single WebAssembly Worker (src/lib.rs) paired with static assets (public/) served from Cloudflare’s edge.
D1-Only State
All persistent state is stored in Cloudflare D1 (migrations/0001_create_schema.sql):
- No KV, no Durable Objects, no Queues — burst debouncing, conversation sessions, rate limits, and billing ledgers all use atomic D1 queries.
- Metadata-only
messagestable — storeschannel,direction,sender,recipient,action_taken,conversation_id, andcreated_at, never message bodies.
Inbound Reply Pipeline
- Webhook verification & dispatch (
src/handlers/webhook.rs) — verifiesX-Hub-Signature-256withMETA_APP_SECRETand parses inbound WhatsApp events. - Burst debounce (
pending_inbound) — buffers rapid consecutive customer messages for the configuredwait_seconds(0–30s) so multiple short texts receive a single coherent reply. - Prompt-injection screening (
src/ai.rs) — runs@cf/meta/prompt-guard-2-86mon inbound text. - Reply generation (
src/ai.rs) — in AI mode, calls@cf/meta/llama-4-scout-17b-16e-instructwith the single built-in core persona and recent conversation context (conversation_sessions). - Safety & risk gate (
src/safety.rs,src/risk.rs) — vets output via@cf/meta/llama-guard-3-8bandrisk::signalchecks (unauthorized prices, commitments, or[[HANDOFF]]markers). - Handoff & escalation (
src/escalations.rs) — when handed off, sends a holding reply, puts the thread intohandoff_cooldown_mins, and optionally sends an alert email via Cloudflaresend_email.
Extending Channels
Outbound delivery and channel metadata are abstracted behind the Channel trait (src/channel/), allowing additional channels (such as Discord or Telegram) to plug into the same pipeline and D1 tables.