Skip to content

Commit

Permalink
Troubleshooting message routing context
Browse files Browse the repository at this point in the history
Update docstrings/type annotations
  • Loading branch information
NeonDaniel committed Nov 15, 2023
1 parent 0765d2c commit 4154d62
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions ovos_core/intent_services/commonqa_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@ def __init__(self, bus):
self.bus.on('common_query.question', self.handle_question)
# TODO: Register available CommonQuery skills

def voc_match(self, utterance, voc_filename, lang, exact=False):
"""Determine if the given utterance contains the vocabulary provided.
def voc_match(self, utterance: str, voc_filename: str, lang: str,
exact: bool = False) -> bool:
"""
Determine if the given utterance contains the vocabulary provided.
By default the method checks if the utterance contains the given vocab
By default, the method checks if the utterance contains the given vocab
thereby allowing the user to say things like "yes, please" and still
match against "Yes.voc" containing only "yes". An exact match can be
requested.
The method checks the "res/text/{lang}" folder of mycroft-core.
The result is cached to avoid hitting the disk each time the method is called.
Args:
utterance (str): Utterance to be tested
voc_filename (str): Name of vocabulary file (e.g. 'yes' for
Expand Down Expand Up @@ -86,7 +85,7 @@ def voc_match(self, utterance, voc_filename, lang, exact=False):

return match

def is_question_like(self, utterance, lang):
def is_question_like(self, utterance: str, lang: str):
# skip utterances with less than 3 words
if len(utterance.split(" ")) < 3:
return False
Expand All @@ -95,8 +94,9 @@ def is_question_like(self, utterance, lang):
return False
return True

def match(self, utterances, lang, message):
"""Send common query request and select best response
def match(self, utterances: str, lang: str, message: Message):
"""
Send common query request and select best response
Args:
utterances (list): List of tuples,
Expand All @@ -122,9 +122,9 @@ def match(self, utterances, lang, message):
break
return match

def handle_question(self, message):
""" Send the phrase to the CommonQuerySkills and prepare for handling
the replies.
def handle_question(self, message: Message):
"""
Send the phrase to CommonQuerySkills and prepare for handling replies.
"""
utt = message.data.get('utterance')
sid = SessionManager.get(message).session_id
Expand Down Expand Up @@ -156,7 +156,7 @@ def handle_question(self, message):

if timeout:
LOG.warning(f"Timed out getting responses for: {query.query}")
self._query_timeout(msg)
self._query_timeout(msg.response(msg.data))
if not query.completed.wait(10):
raise TimeoutError("Timed out processing responses")
answered = bool(query.answered)
Expand All @@ -165,7 +165,7 @@ def handle_question(self, message):
f"remaining active_queries={len(self.active_queries)}")
return answered

def handle_query_response(self, message):
def handle_query_response(self, message: Message):
search_phrase = message.data['phrase']
skill_id = message.data['skill_id']
searching = message.data.get('searching')
Expand Down Expand Up @@ -204,6 +204,13 @@ def handle_query_response(self, message):
query.responses_gathered.set()

def _query_timeout(self, message: Message):
"""
All accepted responses have been provided, either because all skills
replied or a timeout condition was met. The best response is selected,
spoken, and `question:action` is emitted so the associated skill's
handler can perform any additional actions.
@param message: question:query.response Message with `phrase` data
"""
query = self.active_queries.get(SessionManager.get(message).session_id)
LOG.info(f'Check responses with {len(query.replies)} replies')
search_phrase = message.data.get('phrase', "")
Expand All @@ -228,20 +235,20 @@ def _query_timeout(self, message: Message):
pass

# invoke best match
self.speak(best['answer'], message.forward("", best))
self.speak(best['answer'], message.reply("", best))
LOG.info('Handling with: ' + str(best['skill_id']))
cb = best.get('callback_data') or {}
self.bus.emit(message.forward('question:action',
data={'skill_id': best['skill_id'],
'phrase': search_phrase,
'callback_data': cb}))
self.bus.emit(message.reply('question:action',
data={'skill_id': best['skill_id'],
'phrase': search_phrase,
'callback_data': cb}))
query.answered = True
else:
query.answered = False
query.completed.set()

# TODO: Should `speak` be `_speak`?
def speak(self, utterance, message=None):
def speak(self, utterance: str, message: Message = None):
"""Speak a sentence.
Args:
Expand Down

0 comments on commit 4154d62

Please sign in to comment.