$ show cloudflare/moltworker
This repository implements a Cloudflare Worker that runs the OpenClaw personal AI assistant inside a Cloudflare Sandbox container. The worker acts as a reverse proxy, forwarding HTTP and WebSocket traffic to the OpenClaw gateway running on port 18789 inside the container. It provides an admin UI (React SPA) for device pairing management, REST API endpoints for device approval and storage sync, a Chrome DevTools Protocol (CDP) shim for browser automation via Cloudflare Browser Rendering, and debug routes for troubleshooting. The architecture is a single Hono application with middleware for logging, environment validation, and Cloudflare Access authentication. The catch-all route proxies requests to the container, with special handling for WebSocket connections (message interception for error transformation) and crash recovery (automatic gateway restart with R2 backup restore). The codebase is well-structured with TypeScript strict mode, comprehensive unit tests, and a full end-to-end test suite that deploys to real Cloudflare infrastructure. Notable features include a robust process lifecycle manager for the gateway, a cron-based container wake mechanism, and a CDP shim that translates CDP commands to Puppeteer calls.
The LLM identified 4 files worth highlighting for reuse:
A self-contained utility function `redactSensitiveParams` that redacts sensitive query parameters (containing 'secret', 'token', 'key', 'password', 'auth', 'credential') from URLs for safe logging. It uses case-insensitive matching on both key and value. This is a simple, reusable pattern for any application that needs to log URLs without leaking secrets.
A self-contained module that determines whether a container should be woken up based on OpenClaw's cron job schedule. It parses a JSON cron store from R2, computes next run times for 'at', 'every', and 'cron' schedule types (using the croner library), and returns the earliest upcoming run within a configurable lead time. This pattern is reusable for any system that needs to wake a container before scheduled jobs fire, and demonstrates clean handling of multiple schedule formats.
A reusable Node.js CDP (Chrome DevTools Protocol) client library that connects to a Cloudflare Browser Rendering WebSocket endpoint. It handles WebSocket connection, target creation, and provides a clean async API for common browser automation tasks (navigate, screenshot, evaluate, click, type, etc.). The library is self-contained (only depends on 'ws'), well-structured with proper timeout handling and error propagation, and can be extracted and used in any Node.js project needing CDP-based browser automation.
A module for managing Sandbox SDK backup/restore snapshots with R2 persistence. It provides `restoreIfNeeded` (idempotent restore with cross-isolate signaling), `createSnapshot` (delete-then-write pattern with 7-day TTL), and `signalRestoreNeeded` (writes a marker to R2 so all worker isolates re-restore). The code is well-documented with important caveats about FUSE overlay interference and mount checking. This pattern is reusable for any Cloudflare Sandbox project that needs persistent storage across container restarts.