@@ -107,6 +107,14 @@ def __init__(
107107 text_aggregator : Optional [BaseTextAggregator ] = None ,
108108 # Types of text aggregations that should not be spoken.
109109 skip_aggregator_types : Optional [List [str ]] = [],
110+ # A list of callables to transform text before just before sending it to TTS.
111+ # Each callable takes the aggregated text and its type, and returns the transformed text.
112+ # To register, provide a list of tuples of (aggregation_type | '*', transform_function).
113+ text_transforms : Optional [
114+ List [
115+ Tuple [AggregationType | str , Callable [[str , str | AggregationType ], Awaitable [str ]]]
116+ ]
117+ ] = None ,
110118 # Text filter executed after text has been aggregated.
111119 text_filters : Optional [Sequence [BaseTextFilter ]] = None ,
112120 text_filter : Optional [BaseTextFilter ] = None ,
@@ -131,6 +139,11 @@ def __init__(
131139 Use an LLMTextProcessor before the TTSService for custom text aggregation.
132140
133141 skip_aggregator_types: List of aggregation types that should not be spoken.
142+ text_transforms: A list of callables to transform text before just before sending it
143+ to TTS. Each callable takes the aggregated text and its type, and returns the
144+ transformed text. To register, provide a list of tuples of
145+ (aggregation_type | '*', transform_function).
146+
134147 text_filters: Sequence of text filters to apply after aggregation.
135148 text_filter: Single text filter (deprecated, use text_filters).
136149
@@ -164,7 +177,9 @@ def __init__(
164177 )
165178
166179 self ._skip_aggregator_types : List [str ] = skip_aggregator_types or []
167- self ._text_transforms : List [Tuple [str , Callable [[str , str ], Awaitable [str ]]]] = []
180+ self ._text_transforms : List [
181+ Tuple [AggregationType | str , Callable [[str , AggregationType | str ], Awaitable [str ]]]
182+ ] = text_transforms or []
168183 # TODO: Deprecate _text_filters when added to LLMTextProcessor
169184 self ._text_filters : Sequence [BaseTextFilter ] = text_filters or []
170185 self ._transport_destination : Optional [str ] = transport_destination
@@ -323,7 +338,9 @@ async def cancel(self, frame: CancelFrame):
323338 self ._stop_frame_task = None
324339
325340 def add_text_transformer (
326- self , transform_function : Callable [[str , str ], Awaitable [str ]], aggregation_type : str = "*"
341+ self ,
342+ transform_function : Callable [[str , AggregationType | str ], Awaitable [str ]],
343+ aggregation_type : AggregationType | str = "*" ,
327344 ):
328345 """Transform text for a specific aggregation type.
329346
@@ -337,7 +354,9 @@ def add_text_transformer(
337354 self ._text_transforms .append ((aggregation_type , transform_function ))
338355
339356 def remove_text_transformer (
340- self , transform_function : Callable [[str , str ], Awaitable [str ]], aggregation_type : str = "*"
357+ self ,
358+ transform_function : Callable [[str , AggregationType | str ], Awaitable [str ]],
359+ aggregation_type : AggregationType | str = "*" ,
341360 ):
342361 """Remove a text transformer for a specific aggregation type.
343362
0 commit comments