A Turborepo monorepo for Cloudflare Agents with intelligent Gateway routing and agent switching capabilities.
This workspace is organized as a monorepo with the following structure:
cf-agents-workspace/
├── apps/
│ ├── backend/ # Cloudflare Worker with agents
│ └── frontend/ # React frontend application
├── packages/
│ └── shared/ # Shared utilities and types
├── turbo.json # Turborepo configuration
└── package.json # Root package.json with workspaces
- Auto Agent: General-purpose assistant with weather, time, and scheduling capabilities
- Campaign Agent: Specialized for marketing campaigns, strategy development, and automation
- Centralized request routing through Gateway
- WebSocket connections directly to agents (bypassing Gateway for reliability)
- Multiple routing strategies: URL patterns, headers, query parameters
- Fallback mechanisms for robust operation
- Seamless switching between agent types in the UI
- Persistent agent preferences
- Connection status monitoring
- Automatic reconnection handling
- Node.js 18+
- npm or yarn
- Cloudflare account (for deployment)
-
Install dependencies
npm install
-
Build shared packages
npm run build
-
Start development servers
# Start both frontend and backend npm run dev # Or start individually npm run backend:dev npm run frontend:dev
-
Configure environment Copy
.dev.vars.exampleto.dev.varsinapps/backend/and set your OpenAI API key:OPENAI_API_KEY=your_openai_api_key_here
-
Deploy backend to Cloudflare
npm run backend:deploy
-
Build frontend
npm run frontend:build
npm run dev- Start all development serversnpm run build- Build all packages and appsnpm run lint- Lint all codenpm run test- Run all testsnpm run format- Format all code
npm run backend:dev- Start backend development servernpm run backend:deploy- Deploy backend to Cloudflare
npm run frontend:dev- Start frontend development servernpm run frontend:build- Build frontend for production
- Purpose: Cloudflare Worker with AI agents and Gateway routing
- Technology: Cloudflare Workers, Durable Objects, AI SDK
- Key Features:
- Chat and Campaign agents
- Gateway intelligent routing
- WebSocket support
- Tool execution with human-in-the-loop
- Purpose: React web application for agent interaction
- Technology: React 19, Vite, TailwindCSS
- Key Features:
- Agent type switching
- Real-time chat interface
- Connection status monitoring
- Responsive design
- Purpose: Common utilities, types, and constants
- Exports:
- Agent types and utilities
- Connection status types
- Shared constants
- Helper functions
Client Request → Main Worker → {
├── System endpoints (/check-open-ai-key) → Direct handling
├── Gateway routes (/gateway/*, /api/gateway/*) → Gateway routing
├── API endpoints (/api/*) → Direct API handling
└── All other requests → Standard agent routing
}
- chat: General-purpose agent for weather, time, scheduling
- campaign-agent: Specialized for marketing and campaigns
# Direct agent connections (WebSocket)
/agents/chat/session-123
/agents/campaign-agent/marketing-team
# Gateway routing
/gateway/agents/campaign/smart-route
# API endpoints
/api/health
/api/status- Add to
packages/shared/src/index.ts - Build the shared package:
npm run build --filter=@cf-agents/shared - Import in apps:
import { utility } from "@cf-agents/shared"
- Add agent class to
apps/backend/src/server.ts - Update wrangler.jsonc durable object bindings
- Add routing logic to Gateway
- Update frontend agent options
- Use
npm run devto start both frontend and backend - Check browser console for frontend issues
- Check Wrangler logs for backend issues
- Use the debug toggle in the UI for detailed message inspection
- Fork the repository
- Create a feature branch
- Make changes following the established patterns
- Test your changes:
npm run test - Lint your code:
npm run lint - Submit a pull request
MIT License - see LICENSE file for details.