Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/deactivate_in_converse/fallback #199

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading