✨ feat(session): add SessionManagerFactory, SessionManager, and related methods

- Added `SessionManagerFactory` class in `src/backend/langflow/services/session/factory.py` to create instances of `SessionManager`.
- Added `SessionManager` class in `src/backend/langflow/services/session/manager.py` to manage sessions.
- Added `load_session`, `generate_key`, `update_session`, and `clear_session` methods to `SessionManager` to handle session-related logic.

These changes were made to introduce session management functionality to the application.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-17 17:58:10 -03:00
commit cf2940be05
3 changed files with 52 additions and 0 deletions

View file

@ -0,0 +1,14 @@
from typing import TYPE_CHECKING
from langflow.services.session.manager import SessionManager
from langflow.services.factory import ServiceFactory
if TYPE_CHECKING:
from langflow.services.cache.manager import BaseCacheManager
class SessionManagerFactory(ServiceFactory):
def __init__(self):
super().__init__(SessionManager)
def create(self, cache_manager: "BaseCacheManager"):
return SessionManager(cache_manager)

View file

@ -0,0 +1,38 @@
from langflow.interface.run import build_sorted_vertices
from langflow.services.base import Service
from langflow.services.cache.utils import compute_dict_hash
class SessionManager(Service):
name = "session_manager"
def __init__(self, cache_manager):
self.cache_manager = cache_manager
def load_session(self, session_id, data_graph):
key = self.generate_key(session_id, data_graph)
# Check if the data is cached
if key in self.cache_manager:
return self.cache_manager.get(key)
# If not cached, build the graph and cache it
graph, artifacts = build_sorted_vertices(data_graph)
self.cache_manager.set(key, (graph, artifacts))
return graph, artifacts
def generate_key(self, session_id, data_graph):
# Hash the JSON and combine it with the session_id to create a unique key
json_hash = compute_dict_hash(data_graph)
return f"{session_id}{':' if session_id else ''}{json_hash}"
def update_session(self, session_id, data_graph, value):
key = self.generate_key(session_id, data_graph)
self.cache_manager.set(key, value)
def clear_session(self, session_id, data_graph):
key = self.generate_key(session_id, data_graph)
self.cache_manager.delete(key)
# Additional methods to handle session-related logic