66 lines
2.8 KiB
Python
66 lines
2.8 KiB
Python
# D:\GNPL-Private\TimeCraft\backend\app\api\endpoints.py
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
from typing import List
|
|
|
|
from app.models.task import TaskCreateRequest, ScheduleResponse
|
|
from app.services.llm_service import get_scheduled_tasks
|
|
|
|
# Initialize the API router
|
|
router = APIRouter()
|
|
|
|
@router.post(
|
|
"/schedule/tasks",
|
|
response_model=ScheduleResponse, # Define the response model for automatic serialization/validation
|
|
status_code=status.HTTP_200_OK, # Set the default successful status code
|
|
summary="Create and Schedule Tasks using LLM",
|
|
description="Receives a list of user-defined tasks, sends them to an LLM for intelligent scheduling, and returns a structured schedule."
|
|
)
|
|
async def create_and_schedule_tasks(tasks: List[TaskCreateRequest]):
|
|
"""
|
|
Handles the POST request to create and schedule tasks.
|
|
|
|
Args:
|
|
tasks (List[TaskCreateRequest]): A list of task objects received from the frontend.
|
|
|
|
Returns:
|
|
ScheduleResponse: A structured response containing the scheduled tasks.
|
|
|
|
Raises:
|
|
HTTPException: If no tasks are provided, or if there's an error during LLM interaction
|
|
or internal processing.
|
|
"""
|
|
# Validate if any tasks were provided in the request body
|
|
if not tasks:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="No tasks provided for scheduling. Please submit at least one task."
|
|
)
|
|
|
|
try:
|
|
# Call the LLM service to get the scheduled tasks
|
|
scheduled_response = await get_scheduled_tasks(tasks)
|
|
return scheduled_response
|
|
except RuntimeError as e:
|
|
# Catch errors specifically from LLM interaction issues (e.g., network, API issues)
|
|
print(f"LLM interaction failed: {e}") # Log the error for debugging
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to schedule tasks due to an issue with the LLM service. Please try again later. Details: {e}"
|
|
)
|
|
except ValueError as e:
|
|
# Catch errors related to invalid data or parsing issues from the LLM's response
|
|
print(f"Data validation or parsing error: {e}") # Log the error
|
|
raise HTTPException(
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, # 422 for semantically erroneous request syntax
|
|
detail=f"The LLM returned an invalid schedule structure or there was an internal data processing error. Details: {e}"
|
|
)
|
|
except Exception as e:
|
|
# Catch any other unexpected errors
|
|
print(f"An unexpected error occurred in endpoint: {e}") # Log the error
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"An unexpected error occurred while processing your request. Please contact support. Details: {e}"
|
|
)
|
|
|