From ccfb6ee20a9999691f8aa2aeab3b4955a99f135c Mon Sep 17 00:00:00 2001 From: jarbasai Date: Fri, 11 Nov 2022 16:19:29 +0000 Subject: [PATCH] use existing Session class --- mycroft/session/__init__.py | 75 +++++++++++++++++- .../intent_services/converse_service.py | 79 +------------------ 2 files changed, 75 insertions(+), 79 deletions(-) diff --git a/mycroft/session/__init__.py b/mycroft/session/__init__.py index 458bbc4a046a..d0560206de78 100644 --- a/mycroft/session/__init__.py +++ b/mycroft/session/__init__.py @@ -16,8 +16,9 @@ from threading import Lock from uuid import uuid4 -from ovos_config.config import Configuration +from mycroft.messagebus.message import dig_for_message from mycroft.util.log import LOG +from ovos_config.config import Configuration class Session: @@ -25,8 +26,13 @@ class Session: An class representing a Mycroft Session Identifier """ - def __init__(self, session_id, expiration_seconds=180): - self.session_id = session_id + def __init__(self, session_id=None, expiration_seconds=360, active_skills=None, history=None, + max_time=5, max_messages=5): + self.session_id = session_id or str(uuid4()) + self.active_skills = active_skills or [] # [skill_id , timestamp] + self.history = history or [] # [Message , timestamp] + self.max_time = max_time # minutes + self.max_messages = max_messages self.touch_time = int(time.time()) self.expiration_seconds = expiration_seconds @@ -49,6 +55,69 @@ def expired(self): def __str__(self): return "{%s,%d}" % (str(self.session_id), self.touch_time) + def activate_skill(self, skill_id): + # remove it from active list + self.deactivate_skill(skill_id) + # add skill with timestamp to start of active list + self.active_skills.insert(0, [skill_id, time.time()]) + + def deactivate_skill(self, skill_id): + active_ids = [s[0] for s in self.active_skills] + if skill_id in active_ids: + idx = active_ids.index(skill_id) + self.active_skills.pop(idx) + + def is_active(self, skill_id): + self._prune_history() + active_ids = [s[0] for s in self.active_skills] + return skill_id in active_ids + + def _prune_history(self): + # filter old messages from history + now = time.time() + self.history = [m for m in self.history + if now - m[1] < 60 * self.max_time] + # keep only self.max_messages + if len(self.history) > self.max_messages: + self.history = self.history[self.max_messages * -1:] + + def clear(self): + self.active_skills = [] # [skill_id , timestamp] + self.history = [] # [Message , timestamp] + + def as_dict(self): + return { + "active_skills": self.active_skills, + "session_id": self.session_id, + "history": self.history + } + + def update_history(self, message=None): + message = message or dig_for_message() + if message: + self.history.append(message) + self._prune_history() + + @staticmethod + def from_dict(data): + uid = data.get("session_id") + active = data.get("active_skills") or [] + history = data.get("history") or [] + max_time = data.get("max_time") or 5 + max_messages = data.get("max_messages") or 5 + return Session(uid, active_skills=active, + history=history, + max_time=max_time, + max_messages=max_messages) + + @staticmethod + def from_message(message=None): + message = message or dig_for_message() + if message: + return Session.from_dict(message.context) + # new session + return Session() + class SessionManager: """ Keeps track of the current active session. """ diff --git a/mycroft/skills/intent_services/converse_service.py b/mycroft/skills/intent_services/converse_service.py index 18a105695415..89afac56c96b 100644 --- a/mycroft/skills/intent_services/converse_service.py +++ b/mycroft/skills/intent_services/converse_service.py @@ -1,5 +1,4 @@ import time -from uuid import uuid4 from ovos_config.config import Configuration from mycroft.messagebus.message import Message, dig_for_message @@ -8,79 +7,7 @@ ) from mycroft.skills.permissions import ConverseMode, ConverseActivationMode from mycroft.util.log import LOG - - -class ConverseSession: - def __init__(self, session_id=None, active_skills=None, history=None, - max_time=5, max_messages=5): - self.session_id = session_id or str(uuid4()) - self.active_skills = active_skills or [] # [skill_id , timestamp] - self.history = history or [] # [Message , timestamp] - self.max_time = max_time # minutes - self.max_messages = max_messages - - def activate_skill(self, skill_id): - # remove it from active list - self.deactivate_skill(skill_id) - # add skill with timestamp to start of active list - self.active_skills.insert(0, [skill_id, time.time()]) - - def deactivate_skill(self, skill_id): - active_ids = [s[0] for s in self.active_skills] - if skill_id in active_ids: - idx = active_ids.index(skill_id) - self.active_skills.pop(idx) - - def is_active(self, skill_id): - self._prune_history() - active_ids = [s[0] for s in self.active_skills] - return skill_id in active_ids - - def _prune_history(self): - # filter old messages from history - now = time.time() - self.history = [m for m in self.history - if now - m[1] < 60 * self.max_time] - # keep only self.max_messages - if len(self.history) > self.max_messages: - self.history = self.history[self.max_messages * -1:] - - def clear(self): - self.active_skills = [] # [skill_id , timestamp] - self.history = [] # [Message , timestamp] - - def as_dict(self): - return { - "active_skills": self.active_skills, - "session_id": self.session_id, - "history": self.history - } - - def update_history(self, message=None): - message = message or dig_for_message() - if message: - self.history.append(message) - self._prune_history() - - @staticmethod - def from_dict(data): - uid = data.get("session_id") - active = data.get("active_skills") or [] - history = data.get("history") or [] - max_time = data.get("max_time") or 5 - max_messages = data.get("max_messages") or 5 - return ConverseSession(uid, active_skills=active, - history=history, - max_time=max_time, - max_messages=max_messages) - - @staticmethod - def from_message(message=None): - message = message or dig_for_message() - if message: - return ConverseSession.from_dict(message.context) - # new session - return ConverseSession() +from mycroft.session import Session class ConverseService: @@ -89,7 +16,7 @@ class ConverseService: def __init__(self, bus): self.bus = bus self._consecutive_activations = {} - self.default_session = ConverseSession("default") + self.default_session = Session("default") self.sessions = {} # uid: ConverseSession def get_session_id(self, message=None): @@ -107,7 +34,7 @@ def get_session(self, message=None): return self.default_session # create new session if needed if uid not in self.sessions: - self.sessions[uid] = ConverseSession.from_message(message) + self.sessions[uid] = Session.from_message(message) return self.sessions[uid] @property