From 57c1f5d352764438a0bfbadd29541877919266b7 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Tue, 28 May 2024 06:26:51 +0100 Subject: [PATCH] feat/ovos.utterance.handled (#205) companion to https://github.com/OpenVoiceOS/ovos-core/pull/478 --- ovos_workshop/skills/fallback.py | 16 ++++++++++------ ovos_workshop/skills/ovos.py | 17 +++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ovos_workshop/skills/fallback.py b/ovos_workshop/skills/fallback.py index 194e9cda..3cdd1aff 100644 --- a/ovos_workshop/skills/fallback.py +++ b/ovos_workshop/skills/fallback.py @@ -15,14 +15,14 @@ import operator from typing import Optional, List, Callable, Tuple -from ovos_config import Configuration - from ovos_bus_client import MessageBusClient -from ovos_utils.log import LOG -from ovos_utils.events import get_handler_name from ovos_bus_client.message import Message +from ovos_config import Configuration +from ovos_utils.events import get_handler_name +from ovos_utils.log import LOG from ovos_utils.metrics import Stopwatch from ovos_utils.skills import get_non_properties + from ovos_workshop.decorators.compat import backwards_compat from ovos_workshop.permissions import FallbackMode from ovos_workshop.skills.ovos import OVOSSkill @@ -62,6 +62,7 @@ class FallbackSkill(_MetaFB, metaclass=_MutableFallback): A Fallback can either observe or consume an utterance. A consumed utterance will not be seen by any other Fallback handlers. """ + def __new__classic__(cls, *args, **kwargs): if cls is FallbackSkill: # direct instantiation of class, dynamic wizardry for unittests @@ -387,6 +388,9 @@ def _handle_fallback_request(self, message: Message): self.bus.emit(message.forward( f"ovos.skills.fallback.{self.skill_id}.response", data={"result": status, "fallback_handler": handler_name})) + if status: + self.bus.emit(message.forward("ovos.utterance.handled", + {"handler": handler_name})) def _old_register_fallback(self, handler: callable, priority: int): """ core < 0.0.8 """ @@ -423,7 +427,7 @@ def register_fallback(self, handler: callable, priority: int): self.bus.emit(Message("ovos.skills.fallback.register", {"skill_id": self.skill_id, "priority": self.priority})) - + def remove_fallback(self, handler_to_del: Optional[callable] = None) -> bool: """ Remove fallback registration / fallback handler. @@ -441,7 +445,7 @@ def remove_fallback(self, handler_to_del: Optional[callable] = None) -> bool: LOG.warning('No fallback matching {}'.format(handler_to_del)) if len(self._fallback_handlers) == 0: self.bus.emit(Message("ovos.skills.fallback.deregister", - {"skill_id": self.skill_id})) + {"skill_id": self.skill_id})) return found_handler def default_shutdown(self): diff --git a/ovos_workshop/skills/ovos.py b/ovos_workshop/skills/ovos.py index d5faa05b..11f43e04 100644 --- a/ovos_workshop/skills/ovos.py +++ b/ovos_workshop/skills/ovos.py @@ -1444,7 +1444,7 @@ def register_intent_file(self, intent_file: str, handler: callable): self.intent_service.register_padatious_intent(name, filename, lang) if handler: self.add_event(name, handler, 'mycroft.skill.handler', - activation=True) + activation=True, is_intent=True) def register_entity_file(self, entity_file: str): """ @@ -1557,7 +1557,7 @@ def _on_event_start(self, message: Message, handler_info: str, self.deactivate() def _on_event_end(self, message: Message, handler_info: str, - skill_data: dict): + skill_data: dict, is_intent: bool = False): """ Store settings (if changed) and indicate that the skill handler has completed. @@ -1569,6 +1569,8 @@ def _on_event_end(self, message: Message, handler_info: str, msg_type = handler_info + '.complete' message.context["skill_id"] = self.skill_id self.bus.emit(message.forward(msg_type, skill_data)) + if is_intent: + self.bus.emit(message.forward("ovos.utterance.handled", skill_data)) def _on_event_error(self, error: str, message: Message, handler_info: str, skill_data: dict, speak_errors: bool): @@ -1624,7 +1626,7 @@ def _register_adapt_intent(self, if handler: self.add_event(intent_parser.name, handler, 'mycroft.skill.handler', - activation=True) + activation=True, is_intent=True) # skill developer facing utils def speak(self, utterance: str, expect_response: bool = False, @@ -2253,7 +2255,8 @@ def remove_voc(self, utt: str, voc_filename: str, # event related skill developer facing utils def add_event(self, name: str, handler: callable, handler_info: Optional[str] = None, once: bool = False, - speak_errors: bool = True, activation: Optional[bool] = None): + speak_errors: bool = True, activation: Optional[bool] = None, + is_intent: bool = False): """ Create event handler for executing intent or other event. @@ -2274,7 +2277,8 @@ def add_event(self, name: str, handler: callable, def on_error(error, message): if isinstance(error, AbortEvent): self.log.info("Skill execution aborted") - self._on_event_end(message, handler_info, skill_data) + self._on_event_end(message, handler_info, skill_data, + is_intent=is_intent) return self._on_event_error(error, message, handler_info, skill_data, speak_errors) @@ -2284,7 +2288,8 @@ def on_start(message): skill_data, activation) def on_end(message): - self._on_event_end(message, handler_info, skill_data) + self._on_event_end(message, handler_info, skill_data, + is_intent=is_intent) wrapper = create_wrapper(handler, self.skill_id, on_start, on_end, on_error)