|
|
|
@ -105,17 +105,25 @@ class SonioxPool: |
|
|
|
|
|
|
|
soniox_pool = SonioxPool() |
|
|
|
|
|
|
|
def sanitize_and_flatten_text(text: str) -> str: |
|
|
|
def sanitize_text(text: str, allow_multiline: bool = True) -> str: |
|
|
|
if not text: |
|
|
|
return "" |
|
|
|
flattened = re.sub(r"[\r\n\t]+", " ", text) |
|
|
|
flattened = re.sub(r"\s+", " ", flattened).strip() |
|
|
|
if allow_multiline: |
|
|
|
normalized = text.replace("\r\n", "\n").replace("\r", "\n") |
|
|
|
lines = [re.sub(r"[ \t]+", " ", line) for line in normalized.split("\n")] |
|
|
|
cleaned = "\n".join(lines).strip("\r\n") |
|
|
|
else: |
|
|
|
cleaned = re.sub(r"[\r\n\t]+", " ", text) |
|
|
|
cleaned = re.sub(r"\s+", " ", cleaned).strip() |
|
|
|
|
|
|
|
# Suppress lone punctuation artifacts (e.g. «, », ., ,, !, ?, etc.) |
|
|
|
if re.fullmatch(r"[\s«»\.\,\،\؛\؟\!\?\:\;\-\–—\"\'\(\)\[\]\{\}]+", flattened): |
|
|
|
if re.fullmatch(r"[\s«»\.\,\،\؛\؟\!\?\:\;\-\–—\"\'\(\)\[\]\{\}]+", cleaned): |
|
|
|
return "" |
|
|
|
|
|
|
|
return flattened |
|
|
|
return cleaned |
|
|
|
|
|
|
|
def sanitize_and_flatten_text(text: str) -> str: |
|
|
|
return sanitize_text(text, allow_multiline=False) |
|
|
|
|
|
|
|
async def broadcast_state(payload_dict: dict, exclude_ws=None): |
|
|
|
"""Broadcasts state snapshot to all connected clients (Mac and Phone) except sender.""" |
|
|
|
@ -249,8 +257,9 @@ async def handle_phone_stream_ws(request): |
|
|
|
|
|
|
|
# ALL text / sync / insert operations must be broadcast to Mac! |
|
|
|
if msg_type in ("sync_state", "insert_speech", "speech_insert", "phone_input_edit", "update_input", "paste"): |
|
|
|
clean_text = sanitize_and_flatten_text(data.get("text", "")) |
|
|
|
if not clean_text and msg_type in ("insert_speech", "speech_insert"): |
|
|
|
is_speech = msg_type in ("insert_speech", "speech_insert") |
|
|
|
clean_text = sanitize_text(data.get("text", ""), allow_multiline=(not is_speech)) |
|
|
|
if not clean_text and is_speech: |
|
|
|
continue # Do not broadcast empty speech or lone quotes |
|
|
|
data["text"] = clean_text |
|
|
|
data["source"] = "android" |
|
|
|
|