74 lines
3.3 KiB
Python
74 lines
3.3 KiB
Python
# modules/nlp/api.py
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.database import get_db
|
|
|
|
from modules.auth.dependencies import get_current_user
|
|
from modules.auth.models import User
|
|
from modules.nlp.service import process_request, ask_ai
|
|
from modules.nlp.schemas import ProcessCommandRequest
|
|
from modules.calendar.service import create_calendar_event, get_calendar_events, update_calendar_event, delete_calendar_event
|
|
from modules.calendar.schemas import CalendarEventCreate, CalendarEventUpdate
|
|
|
|
router = APIRouter(prefix="/nlp", tags=["nlp"])
|
|
|
|
@router.post("/process-command")
|
|
def process_command(request_data: ProcessCommandRequest, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
|
"""
|
|
Process the user command, execute the action, and return a user-friendly response.
|
|
"""
|
|
user_input = request_data.user_input
|
|
command_data = process_request(user_input)
|
|
intent = command_data["intent"]
|
|
params = command_data["params"]
|
|
response_text = command_data["response_text"]
|
|
|
|
if intent == "error":
|
|
raise HTTPException(status_code=400, detail=response_text)
|
|
|
|
if intent == "clarification_needed":
|
|
return {"response": response_text}
|
|
|
|
if intent == "unknown":
|
|
return {"response": response_text}
|
|
|
|
try:
|
|
match intent:
|
|
case "ask_ai":
|
|
ai_answer = ask_ai(**params)
|
|
return {"response": ai_answer}
|
|
|
|
case "get_calendar_events":
|
|
result = get_calendar_events(db, current_user.id, **params)
|
|
return {"response": response_text, "details": result}
|
|
|
|
case "add_calendar_event":
|
|
event = CalendarEventCreate(**params)
|
|
result = create_calendar_event(db, current_user.id, event)
|
|
return {"response": response_text, "details": result}
|
|
|
|
case "update_calendar_event":
|
|
event_id = params.pop('event_id', None)
|
|
if event_id is None:
|
|
raise HTTPException(status_code=400, detail="Event ID is required for update.")
|
|
event_data = CalendarEventUpdate(**params)
|
|
result = update_calendar_event(db, current_user.id, event_id, event_data=event_data)
|
|
return {"response": response_text, "details": result}
|
|
|
|
case "delete_calendar_event":
|
|
event_id = params.get('event_id')
|
|
if event_id is None:
|
|
raise HTTPException(status_code=400, detail="Event ID is required for delete.")
|
|
result = delete_calendar_event(db, current_user.id, event_id)
|
|
return {"response": response_text, "details": {"deleted": True, "event_id": event_id}}
|
|
|
|
case _:
|
|
print(f"Warning: Unhandled intent '{intent}' reached api.py match statement.")
|
|
raise HTTPException(status_code=500, detail="An unexpected error occurred processing the command.")
|
|
|
|
except HTTPException as http_exc:
|
|
raise http_exc
|
|
except Exception as e:
|
|
print(f"Error executing intent '{intent}': {e}")
|
|
raise HTTPException(status_code=500, detail="Sorry, I encountered an error while trying to perform that action.") |