|
| 1 | +# 📋 Task Format Examples - Complete Reference |
| 2 | + |
| 3 | +## 🎯 **The Solution: UnifiedTask** |
| 4 | + |
| 5 | +**ONE format that works everywhere:** |
| 6 | +- ✅ Backend agent processing |
| 7 | +- ✅ Frontend dashboard display |
| 8 | +- ✅ API requests/responses |
| 9 | +- ✅ Agent-to-agent communication |
| 10 | +- ✅ Database storage |
| 11 | + |
| 12 | +## 🔧 **How to Use in Different Scenarios** |
| 13 | + |
| 14 | +### **Scenario 1: Frontend Creates New Task** |
| 15 | + |
| 16 | +**Your Next.js Dashboard:** |
| 17 | +```typescript |
| 18 | +// Frontend sends this |
| 19 | +const newTask = { |
| 20 | + header: "Update API Documentation", |
| 21 | + type: "Technical content", |
| 22 | + target: "7", // Priority |
| 23 | + limit: "3" // Estimated hours |
| 24 | +} |
| 25 | + |
| 26 | +// POST to backend |
| 27 | +fetch('http://localhost:8000/api/tasks/from-frontend', { |
| 28 | + method: 'POST', |
| 29 | + headers: {'Content-Type': 'application/json'}, |
| 30 | + body: JSON.stringify(newTask) |
| 31 | +}) |
| 32 | +``` |
| 33 | + |
| 34 | +**Backend Receives (frontend_api.py):** |
| 35 | +```python |
| 36 | +from digital_twin_backend.communication.task_format import UnifiedTask |
| 37 | + |
| 38 | +@app.post("/api/tasks/from-frontend") |
| 39 | +async def create_task_from_frontend(frontend_data: Dict): |
| 40 | + # Convert frontend format to UnifiedTask |
| 41 | + task = UnifiedTask.from_frontend_format(frontend_data) |
| 42 | + |
| 43 | + # Distribute to agents (same format throughout!) |
| 44 | + result = await manager.distribute_task(task) |
| 45 | + |
| 46 | + # Return in API format |
| 47 | + return task.to_api_response() |
| 48 | +``` |
| 49 | + |
| 50 | +**Agents Process:** |
| 51 | +```python |
| 52 | +# Manager and workers all use the SAME UnifiedTask object |
| 53 | +async def distribute_task(self, task: UnifiedTask): |
| 54 | + # Phase 1: Consultation |
| 55 | + for agent in workers: |
| 56 | + assessment = await agent.assess_task(task) # Same format! |
| 57 | + |
| 58 | + # Phase 2: Negotiation |
| 59 | + final_assignment = await self.negotiate(task) # Same format! |
| 60 | + |
| 61 | + # Assignment |
| 62 | + task.assign_to(final_assignment["agent_id"]) |
| 63 | + |
| 64 | + return task.to_api_response() |
| 65 | +``` |
| 66 | + |
| 67 | +**Backend Sends Back to Frontend:** |
| 68 | +```python |
| 69 | +@app.get("/api/dashboard/data") |
| 70 | +async def get_dashboard_data(): |
| 71 | + # Get all UnifiedTasks |
| 72 | + tasks = list(shared_knowledge.tasks.values()) |
| 73 | + |
| 74 | + # Convert to frontend format |
| 75 | + from digital_twin_backend.communication.task_format import batch_convert_to_frontend |
| 76 | + |
| 77 | + frontend_data = batch_convert_to_frontend(tasks) |
| 78 | + return {"data": frontend_data} |
| 79 | +``` |
| 80 | + |
| 81 | +### **Scenario 2: Agent Receives Task for Assessment** |
| 82 | + |
| 83 | +**Manager sends to Agent:** |
| 84 | +```python |
| 85 | +# Manager has UnifiedTask |
| 86 | +task = UnifiedTask(title="API Docs", task_type="Technical content", priority=7, estimated_hours=3) |
| 87 | + |
| 88 | +# Sends to agent in unified format |
| 89 | +message = { |
| 90 | + "from": "manager", |
| 91 | + "to": "agent_1", |
| 92 | + "message_type": "task_consultation", |
| 93 | + "task": task.to_agent_format() # Full details |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +**Agent Processes:** |
| 98 | +```python |
| 99 | +# Agent receives message |
| 100 | +async def _handle_task_consultation(self, message: Dict): |
| 101 | + # Parse task from message |
| 102 | + task = UnifiedTask.from_dict(message["task"]) |
| 103 | + |
| 104 | + # Assess using UnifiedTask properties |
| 105 | + can_handle = self._check_skills(task.required_skills) |
| 106 | + confidence = self._calculate_confidence(task) |
| 107 | + |
| 108 | + # Create assessment |
| 109 | + assessment = TaskAssessment( |
| 110 | + can_handle=can_handle, |
| 111 | + confidence=confidence, |
| 112 | + estimated_time=task.estimated_hours * (1 + self.stress_level) |
| 113 | + ) |
| 114 | + |
| 115 | + return assessment |
| 116 | +``` |
| 117 | + |
| 118 | +### **Scenario 3: Complete E2E Flow** |
| 119 | + |
| 120 | +```python |
| 121 | +# 1. Frontend creates task |
| 122 | +frontend_task = { |
| 123 | + "header": "Implement User Authentication", |
| 124 | + "type": "Backend Development", |
| 125 | + "target": "9", |
| 126 | + "limit": "8" |
| 127 | +} |
| 128 | + |
| 129 | +# 2. Backend converts |
| 130 | +task = UnifiedTask.from_frontend_format(frontend_task) |
| 131 | +# Now it's a UnifiedTask with: |
| 132 | +# - title: "Implement User Authentication" |
| 133 | +# - task_type: "Backend Development" |
| 134 | +# - priority: 9 |
| 135 | +# - estimated_hours: 8.0 |
| 136 | +# - required_skills: ["backend", "technical", "database"] (auto-inferred!) |
| 137 | +# - status: TaskStatus.PENDING |
| 138 | + |
| 139 | +# 3. Distribute to agents |
| 140 | +result = await manager.distribute_task(task) |
| 141 | + |
| 142 | +# 4. Agent 1 (Eddie) assesses |
| 143 | +eddie_assessment = await eddie.assess_task(task) |
| 144 | +# - Uses task.required_skills to check match |
| 145 | +# - Uses task.estimated_hours for time estimate |
| 146 | +# - Uses task.priority for urgency assessment |
| 147 | + |
| 148 | +# 5. Agent 2 (Jamik) assesses |
| 149 | +jamik_assessment = await jamik.assess_task(task) |
| 150 | + |
| 151 | +# 6. Agents negotiate |
| 152 | +# Both agents reference the SAME UnifiedTask |
| 153 | +jamik_says: "I have 0.95 in backend (task.required_skills), I can take this" |
| 154 | +eddie_says: "Jamik's better suited for this task.task_type" |
| 155 | + |
| 156 | +# 7. Task assigned |
| 157 | +task.assign_to("agent_2") # Jamik |
| 158 | +# - task.status becomes TaskStatus.ASSIGNED |
| 159 | +# - task.assigned_to = "agent_2" |
| 160 | +# - task.updated_at = now |
| 161 | + |
| 162 | +# 8. Store in shared knowledge |
| 163 | +await shared_knowledge.add_task(task.to_dict()) |
| 164 | + |
| 165 | +# 9. Send to frontend |
| 166 | +frontend_update = task.to_frontend_format() |
| 167 | +# { |
| 168 | +# "id": 1, |
| 169 | +# "header": "Implement User Authentication", |
| 170 | +# "type": "Backend Development", |
| 171 | +# "status": "In Process", |
| 172 | +# "target": "9", |
| 173 | +# "limit": "8", |
| 174 | +# "reviewer": "agent_2" // or Jamik's name |
| 175 | +# } |
| 176 | +``` |
| 177 | + |
| 178 | +## 📊 **Format Comparison Table** |
| 179 | + |
| 180 | +| Component | Before (Multiple Formats) | After (UnifiedTask) | |
| 181 | +|-----------|---------------------------|---------------------| |
| 182 | +| **Frontend** | Custom JSON format | `task.to_frontend_format()` | |
| 183 | +| **API Request** | Pydantic TaskRequest | `UnifiedTask.from_api_request()` | |
| 184 | +| **API Response** | Pydantic TaskResponse | `task.to_api_response()` | |
| 185 | +| **Backend** | TaskInfo dataclass | `UnifiedTask` directly | |
| 186 | +| **Agents** | Custom dict | `task.to_agent_format()` | |
| 187 | +| **Storage** | Multiple schemas | `task.to_dict()` | |
| 188 | + |
| 189 | +## 🎯 **Key Benefits** |
| 190 | + |
| 191 | +✅ **One Format Everywhere** - No conversion errors |
| 192 | +✅ **Auto-Inference** - Skills determined from task type |
| 193 | +✅ **Type Safety** - Consistent field types |
| 194 | +✅ **Easy Conversion** - One-line transforms to any format |
| 195 | +✅ **Backward Compatible** - Works with your existing frontend |
| 196 | +✅ **Future Proof** - Easy to extend with new fields |
| 197 | + |
| 198 | +## 📝 **Quick Start Code** |
| 199 | + |
| 200 | +```python |
| 201 | +from digital_twin_backend.communication.task_format import UnifiedTask, TaskStatus |
| 202 | + |
| 203 | +# Create task (any component can do this) |
| 204 | +task = UnifiedTask( |
| 205 | + title="Your Task", |
| 206 | + task_type="Technical content", |
| 207 | + priority=7, |
| 208 | + estimated_hours=3 |
| 209 | +) |
| 210 | + |
| 211 | +# Use in frontend API |
| 212 | +frontend_json = task.to_frontend_format() |
| 213 | + |
| 214 | +# Use in agent processing |
| 215 | +agent_data = task.to_agent_format() |
| 216 | + |
| 217 | +# Store in database |
| 218 | +db_dict = task.to_dict() |
| 219 | + |
| 220 | +# Parse from anywhere |
| 221 | +task = UnifiedTask.from_frontend_format(frontend_data) |
| 222 | +task = UnifiedTask.from_api_request(api_data) |
| 223 | +task = UnifiedTask.from_dict(any_dict) |
| 224 | +``` |
| 225 | + |
| 226 | +## 🚀 **Integration Example** |
| 227 | + |
| 228 | +Here's how the unified format simplifies your entire system: |
| 229 | + |
| 230 | +**Before (Multiple Formats):** |
| 231 | +```python |
| 232 | +# Frontend format |
| 233 | +frontend_task = {"header": "...", "type": "...", "target": "7", "limit": "3"} |
| 234 | + |
| 235 | +# Convert to backend |
| 236 | +backend_task = TaskInfo(title=frontend_task["header"], ...) |
| 237 | + |
| 238 | +# Convert to agent format |
| 239 | +agent_dict = {"title": backend_task.title, "type": backend_task.task_type, ...} |
| 240 | + |
| 241 | +# Convert back to frontend |
| 242 | +frontend_response = {"id": 1, "header": backend_task.title, ...} |
| 243 | + |
| 244 | +# Too many conversions! 😵 |
| 245 | +``` |
| 246 | + |
| 247 | +**After (UnifiedTask):** |
| 248 | +```python |
| 249 | +# ONE format |
| 250 | +task = UnifiedTask.from_frontend_format(frontend_data) |
| 251 | + |
| 252 | +# Use everywhere |
| 253 | +await manager.distribute_task(task) # Agents get same object |
| 254 | +await shared_knowledge.add_task(task.to_dict()) # Storage |
| 255 | +frontend_response = task.to_frontend_format() # Frontend |
| 256 | + |
| 257 | +# Simple! 🎉 |
| 258 | +``` |
| 259 | + |
| 260 | +--- |
| 261 | + |
| 262 | +**Now your entire system uses ONE consistent task format!** |
| 263 | + |
| 264 | +No more confusion about fields, types, or conversions. Everything just works! 🚀 |
0 commit comments