Skip to content

Commit

Permalink
refactor/reduce code complexity (#427)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JarbasAl authored Mar 2, 2024
1 parent 23cc4c9 commit 63a95b3
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions ovos_core/intent_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 63a95b3

Please sign in to comment.