Docs
Services
Session.service

Session Service

services.session_service.check_assistant_session(app: Flask, session)

Validates the given session string using the assistant session serializer. This function checks if the session data exists and whether it is valid according to the serialization scheme.

  • Parameters:
    • app (Flask) – The Flask application instance, used for contextual application data.
    • session (str) – The session data string to be validated.
  • Returns: Returns False if the session data is invalid or does not exist. If the session : is valid, returns the deserialized session data as a dictionary.
  • Return type: bool or dict
  • Raises: Exception – Catches and logs exceptions related to invalid session data, typically from failed deserialization, and returns False.

Usage:

result = check_assistant_session(current_app, session_str)
if result:
  # Proceed with session-specific logic
else:
  # Handle invalid or expired session

services.session_service.check_session_validation()

Middleware function to validate user sessions for requests. It exempts certain endpoints from validation, checks for valid session tokens in request cookies, and loads the session if valid.

Exemptions:

  • This function does not check sessions for endpoints related to user authentication and initialization such as login, registration, logout, and specific OpenAI operations.
  • Returns:
  • Directly returns None if the session is valid or if the endpoint is exempt.
  • Returns a JSON error response with a 401 status if the session is invalid.
  • Return type: None or JSON response
  • Raises: Exception – Handles exceptions from session deserialization and returns an appropriate JSON error.

Usage: Should be used as a decorator or a before_request function in Flask to secure endpoints.