* Update package versions in pyproject.toml and poetry.lock files * Update poetry caching action and workflows * Update poetry caching action and workflows * Refactor LLMChainComponent build method in LLMChain.py * Update poetry install command in Makefile * Refactor Makefile to remove redundant install_backend targets * Fix codespell issues in project * Update package versions and dependencies * Fix import order in chat_io.spec.ts, headerComponent/index.tsx, and chatMessage/index.tsx * Update ruff command in Makefile and fix poetry cache reuse in Dockerfile * Refactor ServiceManager class in manager.py to handle default service factories * Fix typo in DOWNLOAD_WEBHOOK_URL variable assignment * Refactor cache_service tests in test_cache_manager.py * Add pytest-profiling * Update Makefile to run unit tests with parallel execution * Refactor ServiceManager class in manager.py to handle default service factories * Refactor node_name condition in Graph class to use "Listen" instead of "GetNotified" * Refactor file paths in tests/conftest.py for better readability and maintainability * Sort vertices in each layer by dependency in Graph class * Refactor variable declaration in SessionService class to use type hinting * Refactor make tests command in python_test.yml workflow * Refactor file paths in tests/conftest.py for better readability and maintainability * Refactor imports in tests/conftest.py to include sqlmodel.Session and related dependencies * Refactor file paths in tests/conftest.py to include available files in error message * Refactor file paths in tests/conftest.py to include available files in error message * Refactor file paths in tests/conftest.py to fix typo in BasicChatwithPromptAndHistory.json
56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
from typing import Coroutine, Optional
|
|
|
|
from langflow.services.base import Service
|
|
from langflow.services.cache.base import CacheService
|
|
from langflow.services.session.utils import compute_dict_hash, session_id_generator
|
|
|
|
|
|
class SessionService(Service):
|
|
name = "session_service"
|
|
|
|
def __init__(self, cache_service):
|
|
self.cache_service: "CacheService" = cache_service
|
|
|
|
async def load_session(self, key, flow_id: str, data_graph: Optional[dict] = None):
|
|
# Check if the data is cached
|
|
if key in self.cache_service:
|
|
result = self.cache_service.get(key)
|
|
if isinstance(result, Coroutine):
|
|
result = await result
|
|
return result
|
|
|
|
if key is None:
|
|
key = self.generate_key(session_id=None, data_graph=data_graph)
|
|
if data_graph is None:
|
|
return (None, None)
|
|
# If not cached, build the graph and cache it
|
|
from langflow.graph.graph.base import Graph
|
|
|
|
graph = Graph.from_payload(data_graph, flow_id=flow_id)
|
|
artifacts: dict = {}
|
|
await self.cache_service.set(key, (graph, artifacts))
|
|
|
|
return graph, artifacts
|
|
|
|
def build_key(self, session_id, data_graph):
|
|
json_hash = compute_dict_hash(data_graph)
|
|
return f"{session_id}{':' if session_id else ''}{json_hash}"
|
|
|
|
def generate_key(self, session_id, data_graph):
|
|
# Hash the JSON and combine it with the session_id to create a unique key
|
|
if session_id is None:
|
|
# generate a 5 char session_id to concatenate with the json_hash
|
|
session_id = session_id_generator()
|
|
return self.build_key(session_id, data_graph=data_graph)
|
|
|
|
async def update_session(self, session_id, value):
|
|
result = self.cache_service.set(session_id, value)
|
|
# if it is a coroutine, await it
|
|
if isinstance(result, Coroutine):
|
|
await result
|
|
|
|
async def clear_session(self, session_id):
|
|
result = self.cache_service.delete(session_id)
|
|
# if it is a coroutine, await it
|
|
if isinstance(result, Coroutine):
|
|
await result
|