The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway/platforms): signal_format.py — single pass strips inline markers and records removals; document the algorithm
Teknium3b880c041277
diff --git a/gateway/platforms/signal_format.py b/gateway/platforms/signal_format.pyindex bf152b454c1..c44d8ab28bc 100644--- a/gateway/platforms/signal_format.py+++ b/gateway/platforms/signal_format.py@@ -50,9 +50,8 @@ def markdown_to_signal(text: str) -> tuple[str, list[str]]:styles: list[tuple[int, int, str]] = []while match := _CODE_BLOCK_RE.search(text):inner = match.group(1).rstrip("\n")- start = match.start()+ styles.append((match.start(), len(inner), "MONOSPACE"))text = text[: match.start()] + inner + text[match.end() :]- styles.append((start, len(inner), "MONOSPACE"))new_text = ""last_end = 0for match in _HEADING_RE.finditer(text):@@ -65,6 +64,7 @@ def markdown_to_signal(text: str) -> tuple[str, list[str]]:new_text += heading_textlast_end = eoltext = new_text + text[last_end:]+ # Inline markers: first pattern to claim a span wins; later overlapping matches are dropped.all_matches: list[tuple[int, int, int, int, str]] = []occupied: list[tuple[int, int]] = []for pattern, style in _INLINE_PATTERNS:@@ -74,36 +74,37 @@ def markdown_to_signal(text: str) -> tuple[str, list[str]]:all_matches.append((ms, me, match.start(1), match.end(1), style))occupied.append((ms, me))all_matches.sort()+ # Strip the markers, recording (pos, len) removals so earlier block/heading+ # ranges can be shifted, and capturing inline ranges in the stripped text.+ result = ""+ last_end = 0removals: list[tuple[int, int]] = []- for ms, me, g1s, g1e, _ in all_matches:+ inline_styles: list[tuple[int, int, str]] = []+ for ms, me, g1s, g1e, style in all_matches:if g1s > ms:removals.append((ms, g1s - ms))if me > g1e:removals.append((g1e, me - g1e))+ result += text[last_end:ms]+ inner = text[g1s:g1e]+ inline_styles.append((len(result), len(inner), style))+ result += inner+ last_end = meremovals.sort()+def _adjust(pos: int) -> int:shift = 0for remove_pos, remove_len in removals:- if remove_pos < pos:- shift += min(remove_len, pos - remove_pos)- else:+ if remove_pos >= pos:break+ shift += min(remove_len, pos - remove_pos)return pos - shift- adjusted_prior: list[tuple[int, int, str]] = []- for start, length, style in styles:- new_start = _adjust(start)- new_end = _adjust(start + length)- if new_end > new_start:- adjusted_prior.append((new_start, new_end - new_start, style))- result = ""- last_end = 0- inline_styles: list[tuple[int, int, str]] = []- for ms, me, g1s, g1e, style in all_matches:- result += text[last_end:ms]- inner = text[g1s:g1e]- inline_styles.append((len(result), len(inner), style))- result += inner- last_end = me++ adjusted_prior = [+ (_adjust(start), _adjust(start + length) - _adjust(start), style)+ for start, length, style in styles+ if _adjust(start + length) > _adjust(start)+ ]text = result + text[last_end:]style_strings: list[str] = []for cp_start, cp_len, style_type in sorted(adjusted_prior + inline_styles):