Skip to content

Latest commit

 

History

History
369 lines (299 loc) · 11.6 KB

File metadata and controls

369 lines (299 loc) · 11.6 KB

Frontend-Backend Integration Summary

Overview

This document summarizes the integration work completed to connect the frontend task creation wizard with a mock backend, preparing for future integration with the Python agent framework.

Date

November 2, 2025

Architecture Decision

We implemented a mock backend using JSON file storage to develop and test the frontend independently before integrating with the Python backend. This approach allows us to:

  • Build and test the complete frontend flow
  • Validate data structures and transformations
  • Establish clear API contracts
  • Enable parallel development of frontend and backend

Changes Made

1. Data Structure Updates

front-end/types/task.ts

  • Updated Subtask interface:
    • Changed status type from "open" | "in_progress" | "completed" to "todo" | "in_progress" | "completed"
    • Added completed: boolean field
  • Updated Task interface:
    • Made progress optional (since it's calculated)
    • Made assignedMembers optional
    • Added allocations: Map<string, User> field
    • Added estimatedHours: number field

2. Mock Backend Setup

Created front-end/app/dashboard/tasks.json

  • Empty JSON array to act as the task database
  • Stores all created tasks persistently during development

Created front-end/app/api/tasks/route.ts

  • GET endpoint: Returns all tasks from tasks.json
  • POST endpoint: Creates new tasks with the following logic:
    • Accepts TaskCreationState from the frontend wizard
    • Transforms wizard data into Task format
    • Calculates estimatedHours by summing subtask hours
    • Sets initial progress to 0%
    • Extracts assignedMembers from finalAllocations
    • Maps subtasks with proper status ("todo")
    • Writes to tasks.json
    • Returns created task

Created front-end/app/api/tasks/[id]/route.ts

  • PATCH endpoint: Updates existing tasks
  • DELETE endpoint: Removes tasks by ID

3. Service Layer Refactoring

front-end/services/task-service.ts

Before: Used in-memory data with Node.js file operations (causing errors in client components)

After: Converted to API client

  • Removed all fs/promises and path imports
  • Changed all methods to use fetch() API
  • Made all methods async (return Promises)
  • Proper error handling with try-catch blocks
  • Methods now supported:
    • getTasks(): GET all tasks
    • getOpenTasks(): Filter open tasks client-side
    • getClosedTasks(): Filter closed tasks client-side
    • createTask(taskData): POST new task
    • updateTask(id, updates): PATCH task
    • deleteTask(id): DELETE task
    • updateSubtask(): Update subtask within a task

4. Component Updates

front-end/components/tasks/task-creation-modal.tsx

  • Imported taskService and toast for notifications
  • Added isCreating state for loading indicator
  • Made handleNext async to handle API calls
  • On final step ("Create Task"):
    • Calls taskService.createTask(state)
    • Shows loading state ("Creating...")
    • Displays success/error toast notifications
    • Closes modal on success
  • Added error handling with user-friendly messages

front-end/app/dashboard/page.tsx

  • Changed from synchronous to asynchronous data fetching
  • Added useEffect hook to load tasks on component mount
  • Added isLoading state for better UX
  • Initialized state with empty arrays instead of direct service calls
  • Made refreshTasks() async
  • Simplified handleTaskCreationComplete() to just refresh tasks (creation now handled in modal)
  • Added loading indicator in UI

front-end/components/tasks/task-card.tsx

  • Added null check for assignedMembers before accessing .length
  • Used nullish coalescing operator (??) for progress field (defaults to 0)
  • Prevents runtime errors when optional fields are undefined

front-end/components/tasks/subtask-card.tsx

  • Changed statusConfig key from "open" to "todo" to match data structure
  • Updated label from "Open" to "To Do"
  • Added fallback in case of unexpected status values: statusConfig[subtask.status] || statusConfig.todo

Data Flow

Task Creation Flow

1. User fills 5-step wizard:
   ├─ Step 1: Define Task (title, description, priority, tags)
   ├─ Step 2: Generate Subtasks (AI-generated with breakdown)
   ├─ Step 3: Analyze Metrics (impact, urgency, complexity, etc.)
   ├─ Step 4: Match Team Members (AI-suggested assignments)
   └─ Step 5: Review & Deploy (GitHub integration, notifications)

2. User clicks "Create Task"
   └─ task-creation-modal.tsx → handleNext()

3. Frontend Service Layer
   └─ taskService.createTask(state) → POST /api/tasks

4. Next.js API Route (Server-Side)
   └─ /app/api/tasks/route.ts
      ├─ Receives TaskCreationState
      ├─ Transforms to Task format
      ├─ Calculates estimatedHours
      ├─ Sets progress = 0%
      ├─ Extracts assignedMembers
      └─ Writes to tasks.json

5. Response Flow
   └─ Success: Returns created task
      ├─ Toast notification shown
      ├─ Modal closes
      └─ Dashboard refreshes

6. Dashboard Display
   └─ useEffect fetches updated tasks
      └─ TaskCard components render with new data

Data Transformation

Frontend Wizard State (TaskCreationState)

{
  step: 1-5,
  task: { title, description, priority, status, tags },
  generatedSubtasks: Subtask[],
  analyzedMetrics: TaskMetric[],
  matchResults: Map<subtaskId, MatchCandidate[]>,
  finalAllocations: Map<subtaskId, User>
}

API Transformation (in /api/tasks/route.ts)

{
  id: `task_${timestamp}`,
  title: taskData.task.title,
  description: taskData.task.description,
  priority: taskData.task.priority,
  status: "open",
  tags: taskData.task.tags,
  createdAt: ISO timestamp,
  updatedAt: ISO timestamp,
  progress: 0, // calculated
  estimatedHours: sum(subtask.estimatedHours), // calculated
  subtasks: taskData.generatedSubtasks.map(sub => ({
    ...sub,
    completed: false,
    status: "todo"
  })),
  metrics: taskData.analyzedMetrics,
  allocations: taskData.finalAllocations,
  assignedMembers: [...unique users from allocations] // extracted
}

Stored in tasks.json

[
  {
    "id": "task_1730563200000",
    "title": "Build User Authentication System",
    "description": "...",
    "priority": "high",
    "status": "open",
    "tags": ["backend", "security"],
    "createdAt": "2025-11-02T10:00:00.000Z",
    "updatedAt": "2025-11-02T10:00:00.000Z",
    "progress": 0,
    "estimatedHours": 24,
    "subtasks": [...],
    "metrics": [...],
    "allocations": {...},
    "assignedMembers": [...]
  }
]

Key Technical Decisions

1. Client-Server Separation

  • Problem: Node.js modules (fs, path) cannot be imported in client components
  • Solution: Moved all file operations to API routes (server-side only)
  • Result: Clean separation between client and server code

2. Async State Management

  • Problem: Service methods changed from sync to async
  • Solution: Updated all components to use async/await and useEffect
  • Result: Proper data loading with loading states

3. Null Safety

  • Problem: Optional fields causing runtime errors
  • Solution: Added null checks and fallback values
  • Result: Robust component rendering

4. Status Consistency

  • Problem: Mismatch between Subtask type ("todo") and component config ("open")
  • Solution: Aligned all status values to match TypeScript types
  • Result: No more undefined config lookups

Testing Checklist

  • Task creation wizard flow (all 5 steps)
  • Data persistence to tasks.json
  • Dashboard displays created tasks
  • Loading states work correctly
  • Error handling with toast notifications
  • Subtasks display with correct status
  • Progress calculation
  • Team member assignments
  • Metrics visualization
  • Task cards render without errors

Future Integration with Python Backend

API Endpoints to Implement in Python

The Python backend should implement the following endpoints to replace the mock:

POST /api/tasks

Request Body:

{
  "task": {...},
  "generatedSubtasks": [...],
  "analyzedMetrics": [...],
  "finalAllocations": {...}
}

Backend Processing:

  1. Convert to UnifiedTask format using UnifiedTask.from_frontend_creation_request()
  2. Distribute to agent system via manager.distribute_task(task)
  3. Store in database/shared knowledge
  4. Return task with agent assignments

Response:

{
  "task_id": "...",
  "title": "...",
  "status": "assigned",
  "assigned_agent": "agent_2",
  "created_at": "...",
  "updated_at": "..."
}

GET /api/tasks

  • Return all tasks in frontend format using batch_convert_to_frontend(tasks)

PATCH /api/tasks/{id}

  • Update task status, progress, assignments
  • Sync with agent system

DELETE /api/tasks/{id}

  • Remove task from system

Integration Steps

  1. Update API Base URL

    • In task-service.ts, change apiBaseUrl from /api/tasks to Python backend URL
    • Example: http://localhost:8000/api/tasks
  2. Backend Implementation

    • Use UnifiedTask format from digital_twin_backend/communication/task_format.py
    • Implement endpoints in digital_twin_backend/integration/frontend_api.py
    • Reference TASK_FORMAT_EXAMPLES.md for conversion logic
  3. CORS Configuration

    • Enable CORS on Python backend for Next.js origin
    • Allow credentials if needed for authentication
  4. Authentication

    • Add auth tokens to fetch requests in task-service.ts
    • Implement JWT or session-based auth
  5. Error Handling

    • Standardize error response format between frontend and backend
    • Map Python exceptions to HTTP status codes
  6. Data Sync

    • Implement WebSocket or polling for real-time task updates
    • Show agent progress and status changes live

Files Modified

Created

  • front-end/app/dashboard/tasks.json
  • front-end/app/api/tasks/route.ts
  • front-end/app/api/tasks/[id]/route.ts

Modified

  • front-end/types/task.ts
  • front-end/services/task-service.ts
  • front-end/components/tasks/task-creation-modal.tsx
  • front-end/app/dashboard/page.tsx
  • front-end/components/tasks/task-card.tsx
  • front-end/components/tasks/subtask-card.tsx

Benefits of This Approach

  1. Independent Development: Frontend and backend teams can work in parallel
  2. Rapid Prototyping: Test UI/UX without waiting for backend implementation
  3. Clear Contracts: API structure defined before backend implementation
  4. Easy Testing: Mock data allows for consistent testing scenarios
  5. Minimal Migration: Switching to real backend requires only changing the API base URL
  6. Type Safety: Full TypeScript support throughout the stack
  7. Error Handling: Comprehensive error handling already in place

Next Steps

  1. ✅ Complete frontend task creation flow (DONE)
  2. ✅ Set up mock backend with JSON storage (DONE)
  3. ✅ Implement error handling and loading states (DONE)
  4. 🔄 Implement Python backend endpoints using UnifiedTask format
  5. 🔄 Add real-time updates for agent task processing
  6. 🔄 Implement authentication and authorization
  7. 🔄 Add task filtering and search functionality
  8. 🔄 Implement task editing and deletion UI
  9. 🔄 Add GitHub integration for issue creation
  10. 🔄 Implement notification system

References

  • Backend Task Format: digital_twin_backend/TASK_FORMAT_EXAMPLES.md
  • UnifiedTask Class: digital_twin_backend/communication/task_format.py
  • Test Examples: digital_twin_backend/test_unified_format.py
  • Architecture: ARCHITECTURE.md
  • Implementation Plan: IMPLEMENTATION_PLAN.md