[V0.1 WORKING] Added chat, profile, & calendar screen implementations.
This commit is contained in:
@@ -1,53 +1,74 @@
|
||||
# modules/nlp/api.py
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.database import get_db
|
||||
from core.exceptions import bad_request_exception
|
||||
|
||||
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
|
||||
|
||||
from modules.calendar.schemas import CalendarEventCreate, CalendarEventUpdate
|
||||
|
||||
router = APIRouter(prefix="/nlp", tags=["nlp"])
|
||||
|
||||
@router.post("/process-command")
|
||||
def process_command(user_input: str, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
def process_command(request_data: ProcessCommandRequest, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
"""
|
||||
Process the user command and return the appropriate action.
|
||||
Process the user command, execute the action, and return a user-friendly response.
|
||||
"""
|
||||
command = process_request(user_input)
|
||||
|
||||
if "error" in command:
|
||||
raise bad_request_exception(command["error"])
|
||||
|
||||
match command["intent"]:
|
||||
case "ask_ai":
|
||||
result = ask_ai(**command["params"])
|
||||
return {"action": "ai_response", "details": result}
|
||||
|
||||
case "get_calendar_events":
|
||||
result = get_calendar_events(db, current_user.id, **command["params"])
|
||||
return {"action": "calendar_events_retrieved", "details": result}
|
||||
|
||||
case "add_calendar_event":
|
||||
event = CalendarEventCreate(**command["params"])
|
||||
result = create_calendar_event(db, current_user.id, event)
|
||||
return {"action": "calendar_event_created", "details": result}
|
||||
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"]
|
||||
|
||||
case "update_calendar_event":
|
||||
event = CalendarEventCreate(**command["params"])
|
||||
result = update_calendar_event(db, current_user.id, 0, event_data=event) ## PLACEHOLDER
|
||||
return {"action": "calendar_event_updated", "details": result}
|
||||
|
||||
case "delete_calendar_event":
|
||||
result = update_calendar_event(db, current_user.id, 0) ## PLACEHOLDER
|
||||
return {"action": "calendar_event_deleted", "details": result}
|
||||
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 "unknown":
|
||||
return {"action": "unknown_command", "details": command["params"]}
|
||||
case _:
|
||||
raise bad_request_exception(400, detail="Unrecognized command")
|
||||
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.")
|
||||
Reference in New Issue
Block a user