|
1 | 1 | import abc
|
2 |
| -from typing import List, Tuple, Optional |
| 2 | +from typing import List, Tuple, Optional, Union |
3 | 3 |
|
4 | 4 | from ovos_bus_client.util import get_mycroft_bus
|
5 | 5 | from ovos_config.config import Configuration
|
6 | 6 | from ovos_config.locale import get_default_lang
|
| 7 | +from ovos_plugin_manager.templates.pipeline import IntentHandlerMatch, PipelineMatch |
7 | 8 | from ovos_utils.log import LOG
|
8 | 9 |
|
9 | 10 | from ovos_plugin_manager.utils import ReadWriteStream
|
@@ -75,10 +76,72 @@ def transform(self, utterances: List[str],
|
75 | 76 | return utterances, {}
|
76 | 77 |
|
77 | 78 | def default_shutdown(self):
|
78 |
| - """ perform any shutdown actions """ |
| 79 | + """ |
| 80 | + Performs any necessary shutdown or cleanup actions. |
| 81 | + |
| 82 | + Intended to be overridden by subclasses to implement custom shutdown logic. |
| 83 | + """ |
79 | 84 | pass
|
80 | 85 |
|
81 | 86 |
|
| 87 | +class IntentTransformer: |
| 88 | + """ runs before selected intent is triggered, can be used to inject message.data""" |
| 89 | + |
| 90 | + def __init__(self, name, priority=50, config=None): |
| 91 | + """ |
| 92 | + Initializes the IntentTransformer with a name, priority, and optional configuration. |
| 93 | + |
| 94 | + If no configuration is provided, attempts to load it from the global configuration under "intent_transformers" using the given name. |
| 95 | + """ |
| 96 | + self.name = name |
| 97 | + self.bus = None |
| 98 | + self.priority = priority |
| 99 | + if not config: |
| 100 | + config_core = dict(Configuration()) |
| 101 | + config = config_core.get("intent_transformers", {}).get(self.name) |
| 102 | + self.config = config or {} |
| 103 | + |
| 104 | + def bind(self, bus=None): |
| 105 | + """ |
| 106 | + Attach a message bus instance to the transformer. |
| 107 | + |
| 108 | + If no bus is provided, the default Mycroft message bus is used. |
| 109 | + """ |
| 110 | + self.bus = bus or get_mycroft_bus() |
| 111 | + |
| 112 | + def initialize(self): |
| 113 | + """ |
| 114 | + Performs any necessary initialization actions for the transformer. |
| 115 | + |
| 116 | + Intended to be overridden by subclasses to implement custom setup logic. |
| 117 | + """ |
| 118 | + pass |
| 119 | + |
| 120 | + @abc.abstractmethod |
| 121 | + def transform(self, intent: Union[IntentHandlerMatch, PipelineMatch]) -> Union[IntentHandlerMatch, PipelineMatch]: |
| 122 | + """ |
| 123 | + Transforms the intent match object before the intent handler is triggered. |
| 124 | + |
| 125 | + This method can be used to modify or inject data into the intent, such as performing named entity recognition (NER) or altering match data. By default, it returns the intent unchanged. |
| 126 | + |
| 127 | + Args: |
| 128 | + intent: The intent match object to be transformed. |
| 129 | + |
| 130 | + Returns: |
| 131 | + The transformed intent match object. |
| 132 | + """ |
| 133 | + return intent |
| 134 | + |
| 135 | + def default_shutdown(self): |
| 136 | + """ |
| 137 | + Performs any necessary shutdown actions for the transformer. |
| 138 | + |
| 139 | + Intended to be overridden by subclasses to implement cleanup procedures. |
| 140 | + """ |
| 141 | + pass |
| 142 | + |
| 143 | + |
| 144 | + |
82 | 145 | class AudioTransformer:
|
83 | 146 | """process audio data and optionally transform it before STT stage"""
|
84 | 147 |
|
|
0 commit comments