Skip to content

Commit

Permalink
lang from session
Browse files Browse the repository at this point in the history
ovos-bus-client

ovos-bus-client

manage session in intent service not skill class

get_response tied to session

dont expire default session for backwards compat

improve default session

use existing SessionManager class

use existing Session class

use existing Session class

message history

simplify

lazy manage history

add ConverseSession

feat/converse_session

get active skill list from message.context
  • Loading branch information
JarbasAl committed Apr 11, 2023
1 parent c252f46 commit b423c9c
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 126 deletions.
2 changes: 1 addition & 1 deletion mycroft/audio/service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from threading import Thread, Lock
from os.path import exists, expanduser
from ovos_bus_client import Message
from ovos_bus_client.message import Message

from mycroft.audio.tts import TTSFactory, TTS
from ovos_config.config import Configuration
Expand Down
2 changes: 1 addition & 1 deletion mycroft/configuration/mycroft.conf
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@
// Overrride: none
"session": {
// Time To Live, in seconds
"ttl": 180
"ttl": -1
},

// Speech to Text parameters
Expand Down
2 changes: 1 addition & 1 deletion mycroft/listener/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from mycroft.listener.mic import MutableMicrophone, ResponsiveRecognizer, ListenerState
from ovos_config.config import Configuration
from mycroft.metrics import Stopwatch, report_timing
from mycroft.session import SessionManager
from ovos_bus_client.session import SessionManager
from mycroft.listener.stt import STTFactory
from mycroft.util import find_input_device
from ovos_utils.log import LOG
Expand Down
4 changes: 2 additions & 2 deletions mycroft/listener/mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from mycroft.deprecated.speech_client import NoiseTracker
from mycroft.listener.data_structures import RollingMean, CyclicAudioBuffer
from mycroft.listener.silence import SilenceDetector, SilenceResultType, SilenceMethod
from mycroft.session import SessionManager
from ovos_bus_client.session import SessionManager
from mycroft.util import (
resolve_resource_file,
play_wav, play_ogg, play_mp3
Expand All @@ -50,7 +50,7 @@
from ovos_config.locations import get_xdg_data_save_path
from mycroft.util.audio_utils import play_audio_file
from ovos_plugin_manager.vad import OVOSVADFactory
from ovos_utils.messagebus import get_message_lang
from ovos_bus_client.util import get_message_lang

WakeWordData = namedtuple('WakeWordData',
['audio', 'found', 'stopped', 'end_audio'])
Expand Down
2 changes: 1 addition & 1 deletion mycroft/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from ovos_backend_client.api import MetricsApi
from ovos_config.config import Configuration
from mycroft.session import SessionManager
from ovos_bus_client.session import SessionManager
from ovos_utils.log import LOG
from mycroft.version import CORE_VERSION_STR
from copy import copy
Expand Down
85 changes: 1 addition & 84 deletions mycroft/session/__init__.py
Original file line number Diff line number Diff line change
@@ -1,84 +1 @@
# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import time
from threading import Lock
from uuid import uuid4

from ovos_config.config import Configuration
from ovos_utils.log import LOG


class Session:
"""
An class representing a Mycroft Session Identifier
"""

def __init__(self, session_id, expiration_seconds=180):
self.session_id = session_id
self.touch_time = int(time.time())
self.expiration_seconds = expiration_seconds

def touch(self):
"""
update the touch_time on the session
:return:
"""
self.touch_time = int(time.time())

def expired(self):
"""
determine if the session has expired
:return:
"""
return int(time.time()) - self.touch_time > self.expiration_seconds

def __str__(self):
return "{%s,%d}" % (str(self.session_id), self.touch_time)


class SessionManager:
""" Keeps track of the current active session. """
__current_session = None
__lock = Lock()

@staticmethod
def get():
"""
get the active session.
:return: An active session
"""
config = Configuration().get('session')

with SessionManager.__lock:
if (not SessionManager.__current_session or
SessionManager.__current_session.expired()):
SessionManager.__current_session = Session(
str(uuid4()), expiration_seconds=config.get('ttl', 180))
LOG.info(
"New Session Start: " +
SessionManager.__current_session.session_id)
return SessionManager.__current_session

@staticmethod
def touch():
"""
Update the last_touch timestamp on the current session
:return: None
"""
SessionManager.get().touch()
from ovos_bus_client.session import Session, SessionManager
2 changes: 1 addition & 1 deletion mycroft/skills/common_query_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,4 @@ def CQS_action(self, phrase, data):
"""
# Derived classes may implement this if they use additional media
# or wish to set context after being called.
pass
return None
24 changes: 20 additions & 4 deletions mycroft/skills/intent_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
from ovos_utils.log import LOG
from mycroft.util.audio_utils import play_error_sound
from mycroft.util.parse import normalize
from ovos_utils.messagebus import get_message_lang
from ovos_bus_client.util import get_message_lang
from ovos_bus_client.session import SessionManager


def _normalize_all_utterances(utterances):
Expand Down Expand Up @@ -102,6 +103,9 @@ def __init__(self, bus):
self.handle_activate_skill_request)
self.bus.on('intent.service.skills.deactivate',
self.handle_deactivate_skill_request)
self.bus.on("skill.converse.get_response.enable", self.handle_get_response_enable)
self.bus.on("skill.converse.get_response.disable", self.handle_get_response_disable)

# TODO backwards compat, deprecate
self.bus.on('active_skill_request', self.handle_activate_skill_request)

Expand Down Expand Up @@ -156,7 +160,7 @@ def handle_activate_skill_request(self, message):
# this doesnt happen accidentally at very least
skill_id = message.data['skill_id']
source_skill = message.context.get("skill_id")
self.converse.activate_skill(skill_id, source_skill)
self.converse.activate_skill(skill_id, source_skill, message)

def handle_deactivate_skill_request(self, message):
# TODO imperfect solution - only a skill can deactivate itself
Expand All @@ -165,7 +169,7 @@ def handle_deactivate_skill_request(self, message):
# this doesnt happen accidentally
skill_id = message.data['skill_id']
source_skill = message.context.get("skill_id") or skill_id
self.converse.deactivate_skill(skill_id, source_skill)
self.converse.deactivate_skill(skill_id, source_skill, message)

def reset_converse(self, message):
"""Let skills know there was a problem with speech recognition"""
Expand Down Expand Up @@ -282,6 +286,8 @@ def handle_utterance(self, message):
"""
try:
lang = get_message_lang(message)
sess = SessionManager.get(message)
sess.lang = lang # update session object
try:
setup_locale(lang)
except Exception as e:
Expand Down Expand Up @@ -314,7 +320,7 @@ def handle_utterance(self, message):
break
if match:
if match.skill_id:
self.converse.activate_skill(match.skill_id)
self.converse.activate_skill(match.skill_id, message=message)
# If the service didn't report back the skill_id it
# takes on the responsibility of making the skill "active"

Expand All @@ -335,6 +341,16 @@ def handle_utterance(self, message):
except Exception as err:
LOG.exception(err)

def handle_get_response_enable(self, message):
skill_id = message.data["skill_id"]
session = SessionManager.get(message)
session.enable_response_mode(skill_id)

def handle_get_response_disable(self, message):
skill_id = message.data["skill_id"]
session = SessionManager.get(message)
session.disable_response_mode(skill_id)

def send_complete_intent_failure(self, message):
"""Send a message that no skill could handle the utterance.
Expand Down
2 changes: 1 addition & 1 deletion mycroft/skills/intent_services/commonqa_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mycroft.skills.skill_data import CoreResources
from ovos_utils.enclosure.api import EnclosureAPI
from ovos_utils.log import LOG
from ovos_utils.messagebus import get_message_lang
from ovos_bus_client.util import get_message_lang

EXTENSION_TIME = 10

Expand Down
Loading

0 comments on commit b423c9c

Please sign in to comment.