An AI-powered Task Assistant Agent built on Cloudflare's platform. This application demonstrates a complete AI agent system with chat interface, state management, memory, and LLM integration.
- π€ AI-Powered Chat: Interactive chat interface using Llama 3.3 (70B) on Workers AI
- π Task Management: Create, update, and manage tasks with status tracking
- πΎ Persistent State: State management using Durable Objects with SQLite
- π Real-time Communication: WebSocket support for real-time chat
- π§ Conversation Memory: Maintains conversation history for context-aware responses
- β° Scheduled Tasks: Automatic cleanup of old tasks
This application uses the following Cloudflare services:
- Workers AI - Runs Llama 3.3 70B model for natural language processing
- Durable Objects - Provides stateful, persistent storage and WebSocket connections
- Agents SDK - Framework for building AI agents with built-in state management
- Workers - Serverless compute for handling HTTP and WebSocket requests
- Model:
@cf/meta/llama-3.3-70b-instruct-fp8-fast - Provider: Cloudflare Workers AI
- Usage: Powers the chat interface and task assistance
- Storage: Durable Objects with SQLite backend
- Data: Tasks, conversation history, user preferences
- Persistence: Automatic state synchronization across requests
- Protocol: WebSocket for real-time bidirectional communication
- Interface: HTTP endpoint for WebSocket upgrades
- Features: Real-time message streaming and state updates
- Agent Class: Extends Cloudflare Agents SDK
- Scheduled Tasks: Daily cleanup of old completed tasks
- Callable Methods: Exposed API for task management and chat
.
βββ src/
β βββ index.ts # Worker entry point and routing
β βββ agent.ts # TaskAssistantAgent class
β βββ types.ts # TypeScript type definitions
βββ public/
β βββ index.html # Test HTML page for WebSocket testing
βββ wrangler.toml # Cloudflare Workers configuration
βββ package.json # Dependencies and scripts
βββ tsconfig.json # TypeScript configuration
βββ README.md # This file
βββ PROMPTS.md # AI prompts used in development
- Node.js 18+ and npm
- Cloudflare account
- Wrangler CLI installed globally (or use npx)
-
Clone or download this repository
-
Install dependencies:
npm install- Authenticate with Cloudflare:
npx wrangler loginStart the development server:
npm run devOr using wrangler directly:
npx wrangler devThe server will start on http://localhost:8787 (or the port shown in the terminal).
Deploy to Cloudflare:
npm run deployOr:
npx wrangler deployAfter deployment, you'll receive a URL like https://cf-ai-task-assistant.your-subdomain.workers.dev
Connect to the agent via WebSocket:
const ws = new WebSocket('wss://your-worker-url.workers.dev/?agentId=my-agent');
ws.onopen = () => {
console.log('Connected to agent');
// Send a chat message
ws.send(JSON.stringify({
method: 'chat',
params: ['Hello! Can you help me manage my tasks?']
}));
};
ws.onmessage = (event) => {
const response = JSON.parse(event.data);
console.log('Agent response:', response);
};curl https://your-worker-url.workers.dev/healthcurl -X POST https://your-worker-url.workers.dev/api/chat?agentId=my-agent \
-H "Content-Type: application/json" \
-d '{"method": "chat", "params": ["Hello!"]}'The agent exposes the following callable methods:
chat(message: string)- Chat with the AI assistantaddTask(title: string, description?: string)- Add a new tasklistTasks(status?: string)- List tasks (optionally filtered by status)updateTaskStatus(taskId: string, status: string)- Update task statusdeleteTask(taskId: string)- Delete a taskgetConversationHistory()- Get conversation historyclearHistory()- Clear conversation history
- Connect to the agent via WebSocket
- Chat: "I need to finish my project report"
- Agent responds: "I can help you manage that! Would you like me to add 'Finish project report' as a task?"
- Add task: Call
addTask("Finish project report", "Complete the quarterly project report") - Check tasks: Call
listTasks()to see all tasks - Update status: Call
updateTaskStatus(taskId, "in_progress")when you start - Complete: Call
updateTaskStatus(taskId, "completed")when done
# Connect via WebSocket and send a chat message
# The agent will use Llama 3.3 to respond# Add a task, disconnect, reconnect with same agentId
# The task should still be there (persisted in Durable Object)# Use a WebSocket client to connect and send messages
# Verify real-time bidirectional communication# Wait for scheduled cleanup (or manually trigger)
# Verify old completed tasks are removedEdit wrangler.toml to configure:
- Worker name
- Durable Object bindings
- AI bindings
- Environment variables
- The agent maintains conversation history for context
- Tasks are stored in the agent's state (Durable Object)
- WebSocket connections are managed by the Agents SDK
- State changes automatically sync to connected clients
- Authentication errors: Run
npx wrangler login - Build errors: Ensure TypeScript and dependencies are installed
- WebSocket connection fails: Check that the URL uses
wss://(notws://) - AI model not found: Ensure Workers AI is enabled in your Cloudflare account
MIT
Built for Cloudflare AI assignment submission.