Skip to content

Commit

Permalink
feat/converse_session (#160)
Browse files Browse the repository at this point in the history
Co-authored-by: JarbasAi <[email protected]>
  • Loading branch information
NeonJarbas and JarbasAl authored Sep 30, 2023
1 parent d218adf commit f752ca7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
25 changes: 24 additions & 1 deletion ovos_core/intent_services/converse_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import ovos_core.intent_services
from ovos_bus_client.message import Message
from ovos_bus_client.session import SessionManager
from ovos_bus_client.session import SessionManager, UtteranceState
from ovos_utils import flatten_list
from ovos_utils.log import LOG
from ovos_utils.messagebus import get_message_lang
Expand All @@ -23,6 +23,8 @@ def __init__(self, bus):
self.bus.on('intent.service.skills.activate', self.handle_activate_skill_request)
self.bus.on('active_skill_request', self.handle_activate_skill_request) # TODO backwards compat, deprecate
self.bus.on('intent.service.active_skills.get', self.handle_get_active_skills)
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)

@property
def config(self):
Expand Down Expand Up @@ -256,6 +258,17 @@ def converse(self, utterances, skill_id, lang, message):
"""
session = SessionManager.get(message)
session.lang = lang

state = session.utterance_states.get(skill_id, UtteranceState.INTENT)
if state == UtteranceState.RESPONSE:
session.update_history(message)
converse_msg = message.reply("skill.converse.get_response",
{"skill_id": skill_id,
"utterances": utterances,
"lang": lang})
self.bus.emit(converse_msg)
return True

if self._converse_allowed(skill_id):
session.update_history(message)
converse_msg = message.reply("skill.converse.request",
Expand Down Expand Up @@ -293,6 +306,16 @@ def converse_with_skills(self, utterances, lang, message):
return ovos_core.intent_services.IntentMatch('Converse', None, None, skill_id, utterances[0])
return None

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 handle_activate_skill_request(self, message):
# TODO imperfect solution - only a skill can activate itself
# someone can forge this message and emit it raw, but in OpenVoiceOS all
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ovos-plugin-manager<0.1.0, >=0.0.24a5
ovos-config~=0.0,>=0.0.11a9
ovos-lingua-franca>=0.4.7
ovos_backend_client>=0.1.0a6
ovos_workshop<0.1.0, >=0.0.12a50
ovos_workshop<0.1.0, >=0.0.13a5

# provides plugins and classic machine learning framework
ovos-classifiers<0.1.0, >=0.0.0a33
6 changes: 3 additions & 3 deletions test/unittests/skills/test_mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,10 @@ def test_native_langs(self):
s.config_core['secondary_langs'] = ['en', 'en-us', 'en-AU',
'es', 'pt-PT']
self.assertEqual(s.lang, 'en-us')
self.assertEqual(s._secondary_langs, ['en', 'en-au', 'es',
self.assertEqual(s.secondary_langs, ['en', 'en-au', 'es',
'pt-pt'])
self.assertEqual(len(s._native_langs), len(set(s._native_langs)))
self.assertEqual(set(s._native_langs), {'en-us', 'en-au', 'pt-pt'})
self.assertEqual(len(s.native_langs), len(set(s.native_langs)))
self.assertEqual(set(s.native_langs), {'en-us', 'en-au', 'pt-pt'})
s.config_core['lang'] = lang
s.config_core['secondary_langs'] = secondary

Expand Down
15 changes: 7 additions & 8 deletions test/unittests/skills/test_mycroft_skill_get_response.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""Tests for the mycroft skill's get_response variations."""

import time
from os.path import dirname, join
from threading import Thread
import time
from unittest import TestCase, mock
from unittest import TestCase, mock, skip

from lingua_franca import load_language

from mycroft.skills import MycroftSkill
from ovos_bus_client.message import Message

from test.unittests.mocks import base_config, AnyCallable


load_language("en-us")


Expand Down Expand Up @@ -57,6 +55,7 @@ def create_skill(mock_conf, lang='en-us'):


class TestMycroftSkillWaitResponse(TestCase):
@skip("TODO - refactor for new event based get_response")
def test_wait(self):
"""Ensure that _wait_response() returns the response from converse."""
skill = create_skill()
Expand Down Expand Up @@ -162,13 +161,13 @@ def test_converse_detection(self):
skill.speak_dialog = mock.Mock()

def validator(*args, **kwargs):
self.assertTrue(skill._converse_is_implemented)
self.assertTrue(skill.converse_is_implemented)

self.assertFalse(skill._converse_is_implemented)
self.assertFalse(skill.converse_is_implemented)
skill.get_response('what do you want', validator=validator)
skill._wait_response.assert_called_with(AnyCallable(), validator,
AnyCallable(), -1)
self.assertFalse(skill._converse_is_implemented)
self.assertFalse(skill.converse_is_implemented)


class TestMycroftSkillAskYesNo(TestCase):
Expand Down Expand Up @@ -230,7 +229,7 @@ def test_ask_yesno_other(self):
response = skill.ask_yesno('Do you like breakfast')
self.assertEqual(response, 'I am a fish')

@mock.patch('ovos_workshop.skills.base.dig_for_message')
@mock.patch('ovos_workshop.skills.ovos.dig_for_message')
def test_ask_yesno_german(self, dig_mock):
"""Check that when the skill is set to german it responds to "ja"."""
# lang is session based, it comes from originating message in ovos-core
Expand Down

0 comments on commit f752ca7

Please sign in to comment.