Skip to content

Commit

Permalink
feat/ovos.utterance.handled
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed May 28, 2024
1 parent fbc6ef9 commit 2a56e2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
16 changes: 10 additions & 6 deletions ovos_workshop/skills/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down
17 changes: 11 additions & 6 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 2a56e2b

Please sign in to comment.