Skip to content

Commit

Permalink
use existing Session class
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Nov 11, 2022
1 parent 99deebb commit ccfb6ee
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 79 deletions.
75 changes: 72 additions & 3 deletions mycroft/session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
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:
"""
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

Expand All @@ -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. """
Expand Down
79 changes: 3 additions & 76 deletions mycroft/skills/intent_services/converse_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit ccfb6ee

Please sign in to comment.