A demonstration of hierarchical Durable Objects in Cloudflare Workers, enabling infinite nesting of manager/agent relationships through URL paths.
- Single DO Architecture: Unified Durable Object class handling both manager and agent roles
- Dynamic Hierarchy: Automatic DO creation and management based on URL paths
- Real-time Updates: WebSocket-based communication for instant state changes
- Production Ready:
- Input validation
- Error handling
- Type safety
- Clean shutdown procedures
- Copy
wrangler.example.toml
towrangler.toml
:
cp wrangler.example.toml wrangler.toml
- Install dependencies:
npm install
# or if using bun
bun install
- Start the development server:
npm run dev
# or
bun run dev
- Deploy to Cloudflare Workers:
npm run deploy
# or
bun run deploy
Root (/)
├── agent1
│ ├── subagent1
│ └── subagent2
│ └── subagent2
└── agent2
└── subagent3
Each path segment represents a unique Durable Object instance, creating an infinitely nestable hierarchy.
Each DO maintains:
- Local counter
- List of child agents
- WebSocket connections
- WebSocket Protocol: Real-time bidirectional communication
- Message Types:
increment
: Update local countercreateAgent
: Spawn new child agentdeleteAgent
: Remove child agent and its subtreestate
: Current DO state updateserror
: Error notifications
- Input validation for agent names (alphanumeric, dash, underscore, 1-32 chars)
- Secure WebSocket handling with proper connection lifecycle
- Hierarchical deletion safety with cascading cleanup
-
Root Manager (
/
):- View and manage top-level agents
- Monitor system state
-
Agent Management:
- Create: Enter agent name and click "Create"
- Navigate: Click agent names to traverse hierarchy
- Delete: Remove agents and their complete subtree
-
Agent Communication:
- Direct Message: Use the message input field next to each agent to send them a private message
- Broadcast: Use the broadcast input at the bottom to send a message to all child agents
- Messages appear as alerts and are ephemeral (not stored)
-
Nested Structures:
/team1/project1/task1
/region1/server1/process1
/department1/group1/user1
- Runtime: Cloudflare Workers
- State Management: Durable Objects
- Framework: Hono
- Language: TypeScript
- Real-time: WebSocket API
- Handles cascading deletions safely
- Provides real-time error feedback
- Maintains WebSocket connection state
- Implements proper error handling and validation
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.