kaeizen commited on
Commit
a50f8ce
·
1 Parent(s): 2560eab

handle scenarios where client did not end prev chat, but started a new one

Browse files
Files changed (2) hide show
  1. agent_manager/__init__.py +9 -6
  2. api/views.py +2 -1
agent_manager/__init__.py CHANGED
@@ -154,12 +154,15 @@ def set_session_agent(session_key):
154
  )
155
  SESSION_AGENTS[session_key] = agent
156
 
157
- def get_or_create_agent(chat_session):
158
- """Get or create an agent keyed by the provided chat_session token."""
159
  # Normalize to string to avoid type-mismatch keys
160
- session_key = str(chat_session) if chat_session else None
161
 
162
- if not session_key:
 
 
 
163
  session_key = str(uuid.uuid4())
164
 
165
  if session_key not in SESSION_AGENTS:
@@ -173,9 +176,9 @@ def get_agent(session_id: str):
173
  """Return an existing agent for a session, or None if expired/closed."""
174
  return SESSION_AGENTS.get(session_id)
175
 
176
- def end_session(chat_session):
177
  """Delete an agent session to free memory."""
178
- session_key = str(chat_session) if chat_session is not None else None
179
  if session_key and session_key in SESSION_AGENTS:
180
  del SESSION_AGENTS[session_key]
181
  cache.delete(f"chat_session_{session_key}")
 
154
  )
155
  SESSION_AGENTS[session_key] = agent
156
 
157
+ def get_or_create_agent(cookie_session, chat_session):
158
+ """Get or create an agent keyed by the provided cookie_session token."""
159
  # Normalize to string to avoid type-mismatch keys
160
+ session_key = str(cookie_session) if cookie_session else None
161
 
162
+ if not session_key or int(chat_session) == 0:
163
+ if session_key in SESSION_AGENTS:
164
+ del SESSION_AGENTS[session_key]
165
+ cache.delete(f"chat_session_{session_key}")
166
  session_key = str(uuid.uuid4())
167
 
168
  if session_key not in SESSION_AGENTS:
 
176
  """Return an existing agent for a session, or None if expired/closed."""
177
  return SESSION_AGENTS.get(session_id)
178
 
179
+ def end_session(cookie_session):
180
  """Delete an agent session to free memory."""
181
+ session_key = str(cookie_session) if cookie_session is not None else None
182
  if session_key and session_key in SESSION_AGENTS:
183
  del SESSION_AGENTS[session_key]
184
  cache.delete(f"chat_session_{session_key}")
api/views.py CHANGED
@@ -19,6 +19,7 @@ def chat(request):
19
  """Start or continue an existing chat session."""
20
  # Prefer secure HttpOnly cookie for session tracking
21
  cookie_session = request.COOKIES.get("gm_session")
 
22
  message = request.data.get("message")
23
 
24
  if not message:
@@ -28,7 +29,7 @@ def chat(request):
28
  }, status=status.HTTP_400_BAD_REQUEST)
29
 
30
  # Use cookie if present; otherwise create a new session
31
- agent, session_key = get_or_create_agent(cookie_session)
32
 
33
  mode = request.data.get("mode")
34
  tone = request.data.get("tone")
 
19
  """Start or continue an existing chat session."""
20
  # Prefer secure HttpOnly cookie for session tracking
21
  cookie_session = request.COOKIES.get("gm_session")
22
+ chat_session = request.data.get("chat_session")
23
  message = request.data.get("message")
24
 
25
  if not message:
 
29
  }, status=status.HTTP_400_BAD_REQUEST)
30
 
31
  # Use cookie if present; otherwise create a new session
32
+ agent, session_key = get_or_create_agent(cookie_session, chat_session)
33
 
34
  mode = request.data.get("mode")
35
  tone = request.data.get("tone")