Architecture
System design overview of duyetbot-agent's loop-based agent architecture with Cloudflare Workers and Durable Objects.
Table of Contents: System Overview | Loop-Based Architecture | Message Flow | Core Modules | Tool System | Package Architecture | Transport Layer | Error Handling | Deployment
System Overview
duyetbot-agent is a personal AI agent system built on Cloudflare Workers + Durable Objects. It uses a loop-based agent pattern with tool iterations for intelligent task execution across Telegram and GitHub platforms.
Architecture Diagram
Key Design Principles
Loop-Based Agent Architecture
The system uses a single agent pattern with an LLM reasoning loop. This replaced the previous multi-agent routing system (~8000 LOC deleted), providing simpler architecture, real-time updates, and unified conversation context.
CloudflareChatAgent
Single Durable Object implementation created via createCloudflareChatAgent():
Benefits Over Multi-Agent System
| Aspect | Old (Multi-Agent) | New (Loop-Based) |
|---|---|---|
| Architecture | 8 agents + routing | 1 agent + tools |
| Code Size | ~8000+ LOC | ~2000 LOC |
| Real-time Updates | Lost in routing | Every tool iteration |
| Debugging | Cross-agent traces | Single execution thread |
| Context | Fragmented per agent | Unified conversation |
| Maintenance | Complex routing logic | Simple tool interface |
| Test Count | 1420+ tests | 969 tests (simpler) |
Message Flow: Webhook to Response
Complete Execution Timeline
Chat Loop Pattern
The agent uses a continuous loop with tool iterations:
Core Modules
The agent is composed of modular components in @duyetbot/cloudflare-agent:
Chat Module (chat/)
| File | Purpose | Key Functions |
|---|---|---|
chat-loop.ts | Main LLM reasoning loop | runChatLoop(), tool iteration control |
tool-executor.ts | Unified tool execution | executeTools(), built-in + MCP support |
context-builder.ts | Build conversation context | buildContext(), history management |
response-handler.ts | Process LLM responses | handleResponse(), extract tool calls |
Tracking Module (tracking/)
| File | Purpose | Key Functions |
|---|---|---|
token-tracker.ts | Token usage tracking | trackTokens(), cost calculation |
execution-logger.ts | Log execution steps | logStep(), debug footer generation |
Persistence Module (persistence/)
| File | Purpose | Key Functions |
|---|---|---|
message-persistence.ts | Message store interface | MessageStore implementations |
d1-persistence.ts | D1 database storage | Async persistence to D1 |
memory-persistence.ts | In-memory storage | Fast in-DO storage |
Workflow Module (workflow/)
| File | Purpose | Key Functions |
|---|---|---|
step-tracker.ts | Track execution steps | Record tool calls and results |
debug-footer.ts | Debug information | Generate execution summary |
Tool System
The agent has access to built-in and MCP-based tools, all executed through a unified interface.
Built-in Tools
From @duyetbot/tools package:
| Tool | Purpose | Example Usage |
|---|---|---|
bash | Execute shell commands | Run scripts, file operations |
git | Git operations | Clone repos, create commits |
github | GitHub API operations | Create issues, merge PRs |
research | Web search and synthesis | Find information, summarize |
plan | Task planning | Break down complex tasks |
MCP Tools
Dynamically discovered from connected MCP servers:
| Server | Tools | Purpose |
|---|---|---|
duyet-mcp | Blog queries, personal info | Answer questions about blog posts |
github-mcp | Advanced GitHub ops | Repository management |
| Custom servers | User-defined | Extend functionality |
Tool Execution Flow
Package Architecture
Monorepo Structure
Dependency Graph
Package Responsibilities
Transport Layer Pattern
The Transport Layer enables clean separation between platform-specific and agent logic.
Transport Interface
Telegram Transport
GitHub Transport
Benefits
| Aspect | Without Transport | With Transport |
|---|---|---|
| App boilerplate | ~300 lines | ~50 lines |
| Duplicate logic | Across apps | None |
| New platform | Copy entire app | Just add transport |
| Testing | Hard (mixed concerns) | Easy (mock transport) |
| Hooks | Per-app | Configurable |
Error Handling & Recovery
Tool Execution Errors
LLM Errors
DO Crash/Timeout
Recovery Mechanisms
Durable Object State Management
State Schema
State Persistence
Deployment Architecture
Application Deployment
Wrangler Configuration
Environment Variables
Metrics & Monitoring
Key Metrics to Track
Logging Pattern
Key Architectural Insights
Quick Reference
- Package Dependency Graph: See Package Architecture
- Message Flow Timing: See Message Flow (T+0ms to T+5002ms)
- Tool System: See Tool System
- Deployment: See Deployment Architecture
- Implementation Status: See
PLAN.md
External References
- Model Context Protocol: https://modelcontextprotocol.io/
- Cloudflare Workers: https://developers.cloudflare.com/workers/
- Cloudflare Durable Objects: https://developers.cloudflare.com/durable-objects/
- OpenRouter API: https://openrouter.ai/docs
- Telegram Bot API: https://core.telegram.org/bots/api
- GitHub Webhooks: https://docs.github.com/en/developers/webhooks-and-events/webhooks