From 63a95b32514fc535ab509373d2d008d07edeed32 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Sat, 2 Mar 2024 21:06:44 +0000 Subject: [PATCH] refactor/reduce code complexity (#427) * refactor/reduce code complexity extract an helper method from the utterance handler to improve readibility and reduce code complexity handle the case where a pipeline plugin returns a bad intent match object * refactor/reduce code complexity extract an helper method from the utterance handler to improve readibility and reduce code complexity handle the case where a pipeline plugin returns a bad intent match object * docstrs --- ovos_core/intent_services/__init__.py | 60 ++++++++++++++++----------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/ovos_core/intent_services/__init__.py b/ovos_core/intent_services/__init__.py index 919917f4898f..5fb88452b89e 100644 --- a/ovos_core/intent_services/__init__.py +++ b/ovos_core/intent_services/__init__.py @@ -281,7 +281,32 @@ def _validate_session(self, message, lang): sess.touch() return sess - def handle_utterance(self, message): + def _emit_match_message(self, match: IntentMatch, message: Message): + """Update the message data with the matched utterance information and + activate the corresponding skill if available. + + Args: + match (IntentMatch): The matched utterance object. + message (Message): The messagebus data. + """ + message.data["utterance"] = match.utterance + + if match.skill_id: + self.converse.activate_skill(match.skill_id, message=message) + message.context["skill_id"] = match.skill_id + # If the service didn't report back the skill_id it + # takes on the responsibility of making the skill "active" + + # Launch skill if not handled by the match function + if match.intent_type: + # keep all original message.data and update with intent + # match, mycroft-core only keeps "utterances" + data = dict(message.data) + data.update(match.intent_data) + reply = message.reply(match.intent_type, data) + self.bus.emit(reply) + + def handle_utterance(self, message: Message): """Main entrypoint for handling user utterances Monitor the messagebus for 'recognizer_loop:utterance', typically @@ -342,31 +367,18 @@ def handle_utterance(self, message): for match_func in self.get_pipeline(session=sess): match = match_func(utterances, lang, message) if match: - break + try: + self._emit_match_message(match, message) + break + except: + LOG.exception(f"{match_func} returned an invalid match") + LOG.debug(f"no match from {match_func}") + else: + # Nothing was able to handle the intent + # Ask politely for forgiveness for failing in this vital task + self.send_complete_intent_failure(message) LOG.debug(f"intent matching took: {stopwatch.time}") - if match: - message.data["utterance"] = match.utterance - - if match.skill_id: - self.converse.activate_skill(match.skill_id, message=message) - message.context["skill_id"] = match.skill_id - # If the service didn't report back the skill_id it - # takes on the responsibility of making the skill "active" - - # Launch skill if not handled by the match function - if match.intent_type: - # keep all original message.data and update with intent - # match, mycroft-core only keeps "utterances" - data = dict(message.data) - data.update(match.intent_data) - reply = message.reply(match.intent_type, data) - self.bus.emit(reply) - - else: - # Nothing was able to handle the intent - # Ask politely for forgiveness for failing in this vital task - self.send_complete_intent_failure(message) # sync any changes made to the default session, eg by ConverseService if sess.session_id == "default":