The Cloudflare Worker provides AI agent capabilities and sandbox execution for Duet. It acts as a load balancer that distributes requests to Durable Objects (one per room).Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jaypopat/cf_ai_duet/llms.txt
Use this file to discover all available pages before exploring further.
Architecture
The Worker uses several Cloudflare services:- Cloudflare Workers - Request routing and API endpoints
- Durable Objects - Per-room LLM context and memory
- Cloudflare AI (Llama) - AI inference for the agent
- Cloudflare Sandboxes - Isolated command execution environment
Sandbox features require a paid Cloudflare plan. AI inference and Durable Objects work on the free tier.
Prerequisites
- Cloudflare account
- Wrangler CLI installed
- Bun or Node.js for local development
Setup
Configure wrangler.toml
The Update the
wrangler.toml file is already configured with:name field to your desired Worker name.Local Development
To run the Worker locally for development:When running locally, AI inference still happens on Cloudflare’s edge, not locally.
Worker Configuration
Durable Objects
The Worker uses two Durable Objects:-
DuetAgent - Manages per-room chat history and AI context
- Stores the last 20 messages for context
- Handles AI inference requests
- Maintains conversation state
-
Sandbox - Provides isolated command execution
- One sandbox instance per room
- Allows AI agent to run commands
- Supports direct command execution by users
Migrations
Durable Object migrations are configured inwrangler.toml:
Container Configuration
The Sandbox Durable Object uses a custom container:API Endpoints
The Worker exposes the following endpoints:Health Check
ok if the Worker is running.
Room Messages
Sandbox Execution
Room Cleanup
Usage with Duet Server
After deploying, use your Worker URL when starting the Duet Go server:Monitoring
Observability is configured inwrangler.toml:
enabled = true.
Troubleshooting
Common Issues
- 404 errors: Ensure room ID is included in the request path
- Sandbox not available: Check if you have a paid Cloudflare plan
- AI inference failing: Verify AI binding is configured in wrangler.toml
- Durable Objects not persisting: Check migrations are applied correctly