Duyetbot Agent
Advanced

Build Custom Agent (LEGACY)

DEPRECATED - This guide describes building agents for the legacy multi-agent system that was removed in December 2024.

DEPRECATION NOTICE: This document describes building custom agents for the legacy multi-agent routing system which was removed in December 2024. The system now uses a single CloudflareChatAgent with built-in tools. To extend functionality, create new tools instead of new agents. See custom-tools.md for current approach.

Legacy Documentation

TL;DR (Historical): Extend BaseAgent. Add class/bind in shared-agents. bun run deploy:shared-agents. Test via Telegram!

📋 Prerequisites

🛠️ Step 1: Create Agent

In packages/cloudflare-agent/src/agents/:

// weather-agent.ts
import { BaseAgent } from '../base-agent.js';
 
export class WeatherAgent extends BaseAgent {
  name = 'WeatherAgent';
 
  async execute(query: string) {
    // Fetch weather via tool
    const weather = await this.tools.weather(query);
    return `Weather: ${weather}`;
  }
}

Export in index.ts:

export { WeatherAgent } from './weather-agent.js';

🔗 Step 2: Bind DO

In apps/shared-agents/wrangler.toml:

[[durable_objects.bindings]]
name = "WeatherAgent"
class_name = "WeatherAgent"

🚀 Step 3: Deploy & Test

bun run deploy:shared-agents

Router auto-routes "weather" queries.

Test Telegram: "What's Hanoi weather?"

Expect: "Weather: 28°C sunny"

🔄 Agent Flow

+--------------------+
| User: "Weather?"   |
+--------+----------+
         |
         v
+--------------------+
| RouterAgent        |
| Classify           |
+--------+----------+
         |
         v
+--------------------+
| WeatherAgent DO    |
+--------+----------+
         |
         v
+--------------------+
| Tools: weather API |
+--------+----------+
         |
         v
+--------------------+
| Response: "28°C"   |
+--------+----------+
         |
         v
+--------------------+
| Edit thinking ->   |
| Final              |
+--------------------+

🎯 Quiz

Q: Bind new DO where? A: shared-agents/wrangler.toml

Pro Tip: Add pattern /weather/i in router for fast route.

Custom Durable Objects

TL;DR: class MyAgent extends Agent<MyEnv, MyState>. handle(ctx). agentRegistry.register({name:'my-agent',...}). Import agents/index.ts. ✅ Routed.

Extend Base

From base-agent.ts:

import { Agent } from 'agents';
import { agentRegistry } from './registry.js';
 
export class MyAgent extends Agent<MyEnv, MyState> {
  async handle(ctx: AgentContext): Promise<AgentResult> {
    // Custom logic
    return { success: true, content: 'Hello!' };
  }
}

Register

In agents/my-agent.ts:

agentRegistry.register({
  name: 'my-agent',
  description: 'Handles custom tasks',
  examples: ['custom query'],
  priority: 50
});

Import agents/my-agent.ts in agents/index.ts.

Router Auto-Uses

Registry builds classification prompt dynamically registry.ts.

Quiz: Registration where? A: agentRegistry.register() ✅

Pro Tip ✅: Priority 100 for HITL.

🚀 Next

Telegram Setup -> Build now: Add WeatherAgent! {{t('agent.live')}}

On this page