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:legacy cps session #22

Merged
merged 1 commit into from
Oct 31, 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
12 changes: 6 additions & 6 deletions ocp_pipeline/legacy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from typing import Tuple, Optional

from ovos_bus_client.message import Message
from ovos_bus_client.message import Message, dig_for_message
from ovos_bus_client.util import wait_for_reply
from ovos_utils.ocp import MediaType, PlaybackType, MediaEntry

Expand Down Expand Up @@ -67,19 +67,19 @@ def handle_cps_response(self, message):
# Collect all replies until the timeout
self.query_replies[message.data["phrase"]].append(message.data)

def send_query(self, phrase):
def send_query(self, phrase, message: Optional[Message] = None):
self.query_replies[phrase] = []
self.query_extensions[phrase] = []
self.bus.emit(Message('play:query',
{"phrase": phrase}))
message = message or dig_for_message() or Message("")
self.bus.emit(message.forward('play:query',{"phrase": phrase}))

def get_results(self, phrase):
if self.query_replies.get(phrase):
return [self.cps2media(r) for r in self.query_replies[phrase]]
return []

def search(self, phrase, timeout=5):
self.send_query(phrase)
def search(self, phrase, timeout=5, message: Optional[Message] = None):
self.send_query(phrase, message)
self.waiting = True
start_ts = time.time()
while self.waiting and time.time() - start_ts <= timeout:
Expand Down
29 changes: 14 additions & 15 deletions ocp_pipeline/opm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from ovos_bus_client.session import SessionManager
from ovos_config import Configuration
from ovos_plugin_manager.ocp import available_extractors
from ovos_plugin_manager.templates.pipeline import IntentHandlerMatch, ConfidenceMatcherPipeline, PipelineStageMatcher, \
PipelineMatch
from ovos_plugin_manager.templates.pipeline import IntentHandlerMatch, ConfidenceMatcherPipeline
from ovos_utils.lang import standardize_lang_tag, get_language_dir
from ovos_utils.log import LOG, deprecated, log_deprecation
from ovos_utils.messagebus import FakeBus
Expand Down Expand Up @@ -1062,7 +1061,7 @@ def match_fallback(self, utterances: List[str], lang: str, message: Message = No
return self.match_low(utterances, lang, message)

@deprecated("match_legacy is deprecated! use MycroftCPSLegacyPipeline class directly instead", "2.0.0")
def match_legacy(self, utterances: List[str], lang: str, message: Message = None) -> Optional[PipelineMatch]:
def match_legacy(self, utterances: List[str], lang: str, message: Message = None) -> Optional[IntentHandlerMatch]:
""" match legacy mycroft common play skills (must import from deprecated mycroft module)
not recommended, legacy support only

Expand All @@ -1071,17 +1070,19 @@ def match_legacy(self, utterances: List[str], lang: str, message: Message = None
return MycroftCPSLegacyPipeline(self.bus, self.config).match(utterances, lang, message)


class MycroftCPSLegacyPipeline(PipelineStageMatcher):
class MycroftCPSLegacyPipeline(ConfidenceMatcherPipeline, OVOSAbstractApplication):
def __init__(self, bus: Optional[Union[MessageBusClient, FakeBus]] = None,
config: Optional[Dict] = None):
super().__init__(bus, config)
OVOSAbstractApplication.__init__(self, bus=bus or FakeBus(),
skill_id=OCP_ID, resources_dir=f"{dirname(__file__)}")
ConfidenceMatcherPipeline.__init__(self, bus, config)
self.mycroft_cps = LegacyCommonPlay(self.bus)
OCPPipelineMatcher.load_intent_files()
self.bus.on("ocp:legacy_cps", self.handle_legacy_cps)
self.add_event("ocp:legacy_cps", self.handle_legacy_cps, is_intent=True)

############
# Legacy Mycroft CommonPlay skills
def match(self, utterances: List[str], lang: str, message: Message = None) -> Optional[PipelineMatch]:
def match(self, utterances: List[str], lang: str, message: Message = None) -> Optional[IntentHandlerMatch]:
""" match legacy mycroft common play skills (must import from deprecated mycroft module)
not recommended, legacy support only

Expand All @@ -1104,13 +1105,11 @@ def match(self, utterances: List[str], lang: str, message: Message = None) -> Op
if match["name"] == "play":
LOG.info(f"Legacy Mycroft CommonPlay match: {match}")
utterance = match["entities"].pop("query")
self.bus.emit(Message("ocp:legacy_cps",
{"query": utterance, "conf": 0.7}))
return PipelineMatch(handled=True,
match_data={"query": utterance,
"conf": 0.7},
skill_id=OCP_ID,
utterance=utterance)
return IntentHandlerMatch(match_type="ocp:legacy_cps",
match_data={"query": utterance,
"conf": 0.7},
skill_id=OCP_ID,
utterance=utterance)

def match_medium(self, utterances: List[str], lang: str, message: Message = None) -> Optional[IntentHandlerMatch]:
return None
Expand All @@ -1121,7 +1120,7 @@ def match_low(self, utterances: List[str], lang: str, message: Message = None) -
def handle_legacy_cps(self, message: Message):
"""intent handler for legacy CPS matches"""
utt = message.data["query"]
res = self.mycroft_cps.search(utt)
res = self.mycroft_cps.search(utt, message=message)
if res:
best = OCPPipelineMatcher.select_best([r[0] for r in res], message)
if best:
Expand Down
Loading