mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2025-07-14 01:41:25 -06:00
feat: add _sync implementation in the inmemory session sevice.
PiperOrigin-RevId: 758832846
This commit is contained in:
parent
dc90c91ed1
commit
bc43a1196a
@ -13,6 +13,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import logging
|
||||||
import time
|
import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
@ -28,12 +29,15 @@ from .base_session_service import ListSessionsResponse
|
|||||||
from .session import Session
|
from .session import Session
|
||||||
from .state import State
|
from .state import State
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class InMemorySessionService(BaseSessionService):
|
class InMemorySessionService(BaseSessionService):
|
||||||
"""An in-memory implementation of the session service."""
|
"""An in-memory implementation of the session service."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# A map from app name to a map from user ID to a map from session ID to session.
|
# A map from app name to a map from user ID to a map from session ID to
|
||||||
|
# session.
|
||||||
self.sessions: dict[str, dict[str, dict[str, Session]]] = {}
|
self.sessions: dict[str, dict[str, dict[str, Session]]] = {}
|
||||||
# A map from app name to a map from user ID to a map from key to the value.
|
# A map from app name to a map from user ID to a map from key to the value.
|
||||||
self.user_state: dict[str, dict[str, dict[str, Any]]] = {}
|
self.user_state: dict[str, dict[str, dict[str, Any]]] = {}
|
||||||
@ -48,6 +52,37 @@ class InMemorySessionService(BaseSessionService):
|
|||||||
user_id: str,
|
user_id: str,
|
||||||
state: Optional[dict[str, Any]] = None,
|
state: Optional[dict[str, Any]] = None,
|
||||||
session_id: Optional[str] = None,
|
session_id: Optional[str] = None,
|
||||||
|
) -> Session:
|
||||||
|
return self._create_session_impl(
|
||||||
|
app_name=app_name,
|
||||||
|
user_id=user_id,
|
||||||
|
state=state,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
def create_session_sync(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
app_name: str,
|
||||||
|
user_id: str,
|
||||||
|
state: Optional[dict[str, Any]] = None,
|
||||||
|
session_id: Optional[str] = None,
|
||||||
|
) -> Session:
|
||||||
|
logger.warning('Deprecated. Please migrate to the async method.')
|
||||||
|
return self._create_session_impl(
|
||||||
|
app_name=app_name,
|
||||||
|
user_id=user_id,
|
||||||
|
state=state,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _create_session_impl(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
app_name: str,
|
||||||
|
user_id: str,
|
||||||
|
state: Optional[dict[str, Any]] = None,
|
||||||
|
session_id: Optional[str] = None,
|
||||||
) -> Session:
|
) -> Session:
|
||||||
session_id = (
|
session_id = (
|
||||||
session_id.strip()
|
session_id.strip()
|
||||||
@ -79,6 +114,37 @@ class InMemorySessionService(BaseSessionService):
|
|||||||
user_id: str,
|
user_id: str,
|
||||||
session_id: str,
|
session_id: str,
|
||||||
config: Optional[GetSessionConfig] = None,
|
config: Optional[GetSessionConfig] = None,
|
||||||
|
) -> Session:
|
||||||
|
return self._get_session_impl(
|
||||||
|
app_name=app_name,
|
||||||
|
user_id=user_id,
|
||||||
|
session_id=session_id,
|
||||||
|
config=config,
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_session_sync(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
app_name: str,
|
||||||
|
user_id: str,
|
||||||
|
session_id: str,
|
||||||
|
config: Optional[GetSessionConfig] = None,
|
||||||
|
) -> Session:
|
||||||
|
logger.warning('Deprecated. Please migrate to the async method.')
|
||||||
|
return self._get_session_impl(
|
||||||
|
app_name=app_name,
|
||||||
|
user_id=user_id,
|
||||||
|
session_id=session_id,
|
||||||
|
config=config,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _get_session_impl(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
app_name: str,
|
||||||
|
user_id: str,
|
||||||
|
session_id: str,
|
||||||
|
config: Optional[GetSessionConfig] = None,
|
||||||
) -> Session:
|
) -> Session:
|
||||||
if app_name not in self.sessions:
|
if app_name not in self.sessions:
|
||||||
return None
|
return None
|
||||||
@ -130,6 +196,17 @@ class InMemorySessionService(BaseSessionService):
|
|||||||
@override
|
@override
|
||||||
def list_sessions(
|
def list_sessions(
|
||||||
self, *, app_name: str, user_id: str
|
self, *, app_name: str, user_id: str
|
||||||
|
) -> ListSessionsResponse:
|
||||||
|
return self._list_sessions_impl(app_name=app_name, user_id=user_id)
|
||||||
|
|
||||||
|
def list_sessions_sync(
|
||||||
|
self, *, app_name: str, user_id: str
|
||||||
|
) -> ListSessionsResponse:
|
||||||
|
logger.warning('Deprecated. Please migrate to the async method.')
|
||||||
|
return self._list_sessions_impl(app_name=app_name, user_id=user_id)
|
||||||
|
|
||||||
|
def _list_sessions_impl(
|
||||||
|
self, *, app_name: str, user_id: str
|
||||||
) -> ListSessionsResponse:
|
) -> ListSessionsResponse:
|
||||||
empty_response = ListSessionsResponse()
|
empty_response = ListSessionsResponse()
|
||||||
if app_name not in self.sessions:
|
if app_name not in self.sessions:
|
||||||
@ -145,9 +222,23 @@ class InMemorySessionService(BaseSessionService):
|
|||||||
sessions_without_events.append(copied_session)
|
sessions_without_events.append(copied_session)
|
||||||
return ListSessionsResponse(sessions=sessions_without_events)
|
return ListSessionsResponse(sessions=sessions_without_events)
|
||||||
|
|
||||||
@override
|
|
||||||
def delete_session(
|
def delete_session(
|
||||||
self, *, app_name: str, user_id: str, session_id: str
|
self, *, app_name: str, user_id: str, session_id: str
|
||||||
|
) -> None:
|
||||||
|
self._delete_session_impl(
|
||||||
|
app_name=app_name, user_id=user_id, session_id=session_id
|
||||||
|
)
|
||||||
|
|
||||||
|
def delete_session_sync(
|
||||||
|
self, *, app_name: str, user_id: str, session_id: str
|
||||||
|
) -> None:
|
||||||
|
logger.warning('Deprecated. Please migrate to the async method.')
|
||||||
|
self._delete_session_impl(
|
||||||
|
app_name=app_name, user_id=user_id, session_id=session_id
|
||||||
|
)
|
||||||
|
|
||||||
|
def _delete_session_impl(
|
||||||
|
self, *, app_name: str, user_id: str, session_id: str
|
||||||
) -> None:
|
) -> None:
|
||||||
if (
|
if (
|
||||||
self.get_session(
|
self.get_session(
|
||||||
@ -161,6 +252,13 @@ class InMemorySessionService(BaseSessionService):
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
def append_event(self, session: Session, event: Event) -> Event:
|
def append_event(self, session: Session, event: Event) -> Event:
|
||||||
|
return self._append_event_impl(session=session, event=event)
|
||||||
|
|
||||||
|
def append_event_sync(self, session: Session, event: Event) -> Event:
|
||||||
|
logger.warning('Deprecated. Please migrate to the async method.')
|
||||||
|
return self._append_event_impl(session=session, event=event)
|
||||||
|
|
||||||
|
def _append_event_impl(self, session: Session, event: Event) -> Event:
|
||||||
# Update the in-memory session.
|
# Update the in-memory session.
|
||||||
super().append_event(session=session, event=event)
|
super().append_event(session=session, event=event)
|
||||||
session.last_update_time = event.timestamp
|
session.last_update_time = event.timestamp
|
||||||
|
Loading…
Reference in New Issue
Block a user