Nada commited on
Commit
4406616
·
1 Parent(s): 92cf70d
Files changed (1) hide show
  1. app.py +8 -0
app.py CHANGED
@@ -113,10 +113,18 @@ async def start_session(user_id: str):
113
  @app.post("/send_message", response_model=MessageResponse)
114
  async def send_message(request: MessageRequest):
115
  try:
 
 
 
 
 
 
 
116
  response = chatbot.process_message(request.user_id, request.message)
117
  session = chatbot.conversations[request.user_id]
118
  return MessageResponse(response=response, session_id=session.session_id)
119
  except Exception as e:
 
120
  raise HTTPException(status_code=500, detail=str(e))
121
 
122
  @app.post("/end_session", response_model=SessionSummary)
 
113
  @app.post("/send_message", response_model=MessageResponse)
114
  async def send_message(request: MessageRequest):
115
  try:
116
+ # Check if user has an active session
117
+ if request.user_id not in chatbot.conversations or not chatbot.conversations[request.user_id].is_active:
118
+ # Start a new session if none exists
119
+ session_id, _ = chatbot.start_session(request.user_id)
120
+ logger.info(f"Started new session {session_id} for user {request.user_id} during message send")
121
+
122
+ # Process the message
123
  response = chatbot.process_message(request.user_id, request.message)
124
  session = chatbot.conversations[request.user_id]
125
  return MessageResponse(response=response, session_id=session.session_id)
126
  except Exception as e:
127
+ logger.error(f"Error processing message for user {request.user_id}: {str(e)}")
128
  raise HTTPException(status_code=500, detail=str(e))
129
 
130
  @app.post("/end_session", response_model=SessionSummary)