Skip to content

Commit

Permalink
fix/deactivate_in_converse/fallback
Browse files Browse the repository at this point in the history
avoid the caveats documented in OpenVoiceOS/ovos-core#451
  • Loading branch information
JarbasAl committed May 4, 2024
1 parent 936409f commit 5d99a42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
6 changes: 3 additions & 3 deletions ovos_workshop/skills/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ def _handle_fallback_request(self, message: Message):
key=operator.itemgetter(0))
for prio, handler in sorted_handlers:
try:
if handler(message):
# call handler, conditionally activating the skill
status = self._conditional_activate(handler, message=message)
if status:
# indicate completion
status = True
handler_name = get_handler_name(handler)
self.activate() # activate skill for converse
break
except Exception:
LOG.exception('Exception in fallback.')
Expand Down
27 changes: 24 additions & 3 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,27 @@ def _handle_converse_ack(self, message: Message):
"can_handle": self.converse_is_implemented},
context={"skill_id": self.skill_id}))

def _conditional_activate(self, handler, *args, **kwargs):
"""internal helper method, only calls self.activate()
if the handler returns True and does not call self.deactivate()"""
# if handler calls self.deactivate() we do
# NOT want to reactivate the skill
was_deactivated = False

def on_deac(message):
nonlocal was_deactivated
if message.data.get("skill_id") == self.skill_id:
was_deactivated = True

self.bus.on("intent.service.skills.deactivate", on_deac)
# call skill method
result = handler(*args, **kwargs)
# conditional activation
if not was_deactivated and result:
self.activate() # renew activation
self.bus.remove("intent.service.skills.deactivate", on_deac)
return result

def _handle_converse_request(self, message: Message):
"""
If this skill is requested and supports converse, handle the user input
Expand Down Expand Up @@ -1224,9 +1245,9 @@ def _handle_converse_request(self, message: Message):
"utterances": message.data['utterances'],
"lang": message.data['lang']}
kwargs = {k: v for k, v in kwargs.items() if k in params}
result = self.converse(**kwargs)
if result:
self.activate() # renew activation

# call skill converse method, conditionally activating the skill
result = self._conditional_activate(self.converse, **kwargs)

self.bus.emit(message.reply('skill.converse.response',
{"skill_id": self.skill_id,
Expand Down

0 comments on commit 5d99a42

Please sign in to comment.