Skip to content

The agent that grows with you

refactor(telegram): move text_link expansion to telegram_entities sibling

teknium1d7ceee19a802
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index a12865ec6ed..d81cd696f92 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -137,6 +137,7 @@ from gateway.platforms.base import (
SUPPORTED_DOCUMENT_TYPES, SUPPORTED_IMAGE_DOCUMENT_TYPES, _TEXT_INJECT_EXTENSIONS, utf16_len,
)
from gateway.platforms.event import MessageEvent, MessageType, ProcessingOutcome
+from plugins.platforms.telegram.telegram_entities import expand_link_entities
from plugins.platforms.telegram.telegram_ids import normalize_telegram_chat_id
from plugins.platforms.telegram.telegram_network import (
SEED_FALLBACK_IPS, TelegramFallbackTransport, discover_fallback_ips, parse_fallback_ip_env, tcp_keepalive_socket_options)
@@ -5574,64 +5575,6 @@ class TelegramAdapter(BasePlatformAdapter):
"""Guest-mode bypass: explicit bot mention (caller already verified group chat)."""
return self._telegram_guest_mode() and self._message_mentions_bot(message)
- def _expand_link_entities(self, message: Message) -> str:
- """Inline Telegram ``text_link`` URLs into visible message text.
-
- Telegram stores hidden-link entity offsets as UTF-16 code units, while
- Python string indexes are Unicode code points. Convert offsets before
- inserting so links still expand correctly when text before the anchor
- contains emoji or other non-BMP characters.
- """
- text = getattr(message, "text", None)
- if text:
- entities = getattr(message, "entities", None) or []
- else:
- text = getattr(message, "caption", None) or ""
- entities = getattr(message, "caption_entities", None) or []
- if not text or not entities:
- return text
-
- def utf16_index(offset: int) -> Optional[int]:
- units = 0
- for index, char in enumerate(text):
- if units == offset:
- return index
- units += 2 if ord(char) > 0xFFFF else 1
- if units > offset:
- return None
- return len(text) if units == offset else None
-
- utf16_length = sum(2 if ord(char) > 0xFFFF else 1 for char in text)
-
- links: list[tuple[int, int, str]] = []
- for entity in entities:
- entity_type = str(getattr(entity, "type", "")).split(".")[-1].lower()
- raw_url = getattr(entity, "url", None)
- url = raw_url.strip() if isinstance(raw_url, str) else ""
- if entity_type != "text_link" or not url:
- continue
- try:
- offset = int(getattr(entity, "offset", -1))
- length = int(getattr(entity, "length", 0))
- except (TypeError, ValueError):
- continue
- if offset < 0 or length <= 0 or offset + length > utf16_length:
- continue
- start, end = utf16_index(offset), utf16_index(offset + length)
- if start is None or end is None or end <= start:
- continue
- links.append((start, end, url))
-
- expanded = text
- for _start, end, url in sorted(links, reverse=True):
- inline = f" ({url})"
- # The guard makes repeated processing of an already-expanded event
- # harmless without changing the original entity offsets.
- if expanded[end:].startswith(inline):
- continue
- expanded = f"{expanded[:end]}{inline}{expanded[end:]}"
- return expanded
-
def _clean_bot_trigger_text(self, text: Optional[str]) -> Optional[str]:
bot_username = self._current_bot_username()
if not text or not bot_username:
@@ -6282,14 +6225,14 @@ class TelegramAdapter(BasePlatformAdapter):
if self._should_observe_unmentioned_group_message(msg):
_event = self._build_message_event(msg, self._media_message_type(msg), update_id=update.update_id)
if msg.caption:
- _event.text = self._clean_bot_trigger_text(self._expand_link_entities(msg))
+ _event.text = self._clean_bot_trigger_text(expand_link_entities(msg))
await self._cache_observed_media(msg, _event)
self._observe_unmentioned_group_message(msg, _event.message_type, update_id=update.update_id, event=_event)
return
event = self._build_message_event(msg, self._media_message_type(msg), update_id=update.update_id)
if msg.caption:
from plugins.platforms.telegram.telegram_context import group_trigger_text
- event.text = group_trigger_text(self, msg, self._expand_link_entities(msg))
+ event.text = group_trigger_text(self, msg, expand_link_entities(msg))
# Stickers: _handle_sticker overwrites event.text with its vision description, so observe attribution must run after it.
if msg.sticker:
await self._handle_sticker(msg, event)
@@ -6590,7 +6533,7 @@ class TelegramAdapter(BasePlatformAdapter):
_chat_id_str = str(chat.id)
channel_prompt = resolve_channel_prompt(self.config.extra, thread_id_str or _chat_id_str, _chat_id_str if thread_id_str else None)
return MessageEvent(
- text=self._expand_link_entities(message), message_type=msg_type, source=source, raw_message=message,
+ text=expand_link_entities(message), message_type=msg_type, source=source, raw_message=message,
message_id=str(message.message_id), platform_update_id=update_id,
reply_to_message_id=reply_to_id, reply_to_text=reply_to_text, auto_skill=topic_skill,
channel_prompt=group_identity_prompt(self, message, channel_prompt),
diff --git a/plugins/platforms/telegram/telegram_entities.py b/plugins/platforms/telegram/telegram_entities.py
new file mode 100644
index 00000000000..761bef95792
--- /dev/null
+++ b/plugins/platforms/telegram/telegram_entities.py
@@ -0,0 +1,68 @@
+"""Telegram ``text_link`` expansion, kept out of the adapter facade.
+
+A rich-text hyperlink arrives as visible anchor text plus a ``text_link`` entity carrying the
+URL; the model only ever sees the anchor unless the URL is inlined (#31071).
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+
+def _utf16_length(text: str) -> int:
+ return len(text.encode("utf-16-le")) // 2
+
+
+def _code_point_index(text: str, utf16_offset: int) -> int | None:
+ """Python index for a UTF-16 code-unit offset; None when it splits a surrogate pair."""
+ try:
+ return len(text.encode("utf-16-le")[: utf16_offset * 2].decode("utf-16-le"))
+ except UnicodeDecodeError:
+ return None
+
+
+def expand_link_entities(message: Any) -> str:
+ """Message text (or caption) with every hidden ``text_link`` URL inlined after its anchor.
+
+ Entity offsets are UTF-16 code units (emoji before the anchor count twice), so they are mapped
+ to code-point indices before slicing. Malformed entities are skipped; an anchor that already
+ reads as its own URL, or text already carrying the inline form, is left untouched.
+ """
+ text = getattr(message, "text", None)
+ if text:
+ entities = getattr(message, "entities", None) or []
+ else:
+ text = getattr(message, "caption", None) or ""
+ entities = getattr(message, "caption_entities", None) or []
+ if not text or not entities:
+ return text
+
+ utf16_length = _utf16_length(text)
+ links: list[tuple[int, int, str]] = []
+ for entity in entities:
+ entity_type = str(getattr(entity, "type", "")).split(".")[-1].lower()
+ raw_url = getattr(entity, "url", None)
+ url = raw_url.strip() if isinstance(raw_url, str) else ""
+ if entity_type != "text_link" or not url:
+ continue
+ try:
+ offset = int(getattr(entity, "offset", -1))
+ length = int(getattr(entity, "length", 0))
+ except (TypeError, ValueError):
+ continue
+ if offset < 0 or length <= 0 or offset + length > utf16_length:
+ continue
+ start, end = _code_point_index(text, offset), _code_point_index(text, offset + length)
+ if start is None or end is None or end <= start:
+ continue
+ if text[start:end].strip() == url:
+ continue
+ links.append((start, end, url))
+
+ expanded = text
+ for _start, end, url in sorted(links, reverse=True):
+ inline = f" ({url})"
+ if expanded[end:].startswith(inline):
+ continue
+ expanded = f"{expanded[:end]}{inline}{expanded[end:]}"
+ return expanded
diff --git a/tests/gateway/test_telegram_text_link_expansion.py b/tests/gateway/test_telegram_text_link_expansion.py
index d20d2905a38..24802f1cec1 100644
--- a/tests/gateway/test_telegram_text_link_expansion.py
+++ b/tests/gateway/test_telegram_text_link_expansion.py
@@ -2,13 +2,13 @@
Telegram delivers a URL attached to a word (e.g. "тут" -> github.com) as a
``text_link`` entity. The visible text carries no URL, so without expansion the
-model only ever sees the bare word and cannot fetch the link. ``_expand_link_entities``
+model only ever sees the bare word and cannot fetch the link. ``expand_link_entities``
inlines the real URL right after its anchor for both ``text`` and ``caption``.
"""
import pytest
-from plugins.platforms.telegram.adapter import TelegramAdapter
+from plugins.platforms.telegram.telegram_entities import expand_link_entities
class _Entity:
@@ -27,80 +27,46 @@ class _Message:
self.caption_entities = caption_entities
-def adapter():
- # _expand_link_entities only uses getattr on the message, so an unbound
- # instance is enough.
- return TelegramAdapter.__new__(TelegramAdapter)
-
-
-def test_hidden_link_in_word_is_inlined(adapter):
+def test_hidden_link_in_word_is_inlined():
msg = _Message(
text="Ссылка: тут\n#tag",
entities=[_Entity("text_link", 8, 3, "https://github.com/Cysharp/R3")],
)
- out = adapter._expand_link_entities(msg)
+ out = expand_link_entities(msg)
assert "https://github.com/Cysharp/R3" in out
assert out.startswith("Ссылка: тут (https://github.com/Cysharp/R3)")
-def test_plain_text_without_entities_is_unchanged(adapter):
- msg = _Message(text="просто текст без ссылок")
- assert adapter._expand_link_entities(msg) == "просто текст без ссылок"
-
-def test_caption_link_on_media_is_inlined(adapter):
+def test_caption_link_on_media_is_inlined():
msg = _Message(
caption="Смотри тут проект",
caption_entities=[_Entity("text_link", 7, 3, "https://example.com/x")],
)
- assert adapter._expand_link_entities(msg) == "Смотри тут (https://example.com/x) проект"
+ assert expand_link_entities(msg) == "Смотри тут (https://example.com/x) проект"
-def test_utf16_offset_is_respected_after_emoji(adapter):
+def test_utf16_offset_is_respected_after_emoji():
# Telegram entity offsets are measured in UTF-16 code units. The emoji is
# two units, so the visible anchor starts at offset 3, not Python index 2.
msg = _Message(
text="🔥 тут",
entities=[_Entity("text_link", 3, 3, "https://example.com/emoji")],
)
- assert adapter._expand_link_entities(msg) == "🔥 тут (https://example.com/emoji)"
+ assert expand_link_entities(msg) == "🔥 тут (https://example.com/emoji)"
-def test_expansion_is_idempotent(adapter):
+def test_expansion_is_idempotent():
msg = _Message(
text="Ссылка: тут\n#tag",
entities=[_Entity("text_link", 8, 3, "https://github.com/Cysharp/R3")],
)
- out = adapter._expand_link_entities(msg)
+ out = expand_link_entities(msg)
repeat = _Message(text=out, entities=[_Entity("text_link", 8, 3, "https://github.com/Cysharp/R3")])
- assert adapter._expand_link_entities(repeat) == out
-
-
-def test_multiple_distinct_links(adapter):
- msg = _Message(
- text="a b",
- entities=[
- _Entity("text_link", 0, 1, "https://one.com"),
- _Entity("text_link", 2, 1, "https://two.com"),
- ],
- )
- out = adapter._expand_link_entities(msg)
- assert out == "a (https://one.com) b (https://two.com)"
-
+ assert expand_link_entities(repeat) == out
-def test_non_text_link_entities_are_ignored(adapter):
- msg = _Message(text="жирный текст", entities=[_Entity("bold", 0, 6)])
- assert adapter._expand_link_entities(msg) == "жирный текст"
-def test_anchor_repeats_later_in_text(adapter):
- msg = _Message(
- text="тут и ещё тут",
- entities=[_Entity("text_link", 0, 3, "https://x.com")],
- )
- out = adapter._expand_link_entities(msg)
- assert out.startswith("тут (https://x.com) и ещё тут")
@pytest.mark.parametrize(
@@ -111,23 +77,29 @@ def test_anchor_repeats_later_in_text(adapter):
_Entity("text_link", "invalid", 1, "https://example.com/bad-offset"),
],
)
-def test_malformed_link_entities_are_ignored(adapter, entity):
+def test_malformed_link_entities_are_ignored(entity):
msg = _Message(text="abc", entities=[entity])
- assert adapter._expand_link_entities(msg) == "abc"
+ assert expand_link_entities(msg) == "abc"
-def test_text_does_not_use_caption_entities(adapter):
+def test_text_does_not_use_caption_entities():
msg = _Message(
text="plain text",
caption="linked caption",
caption_entities=[_Entity("text_link", 0, 6, "https://example.com/caption")],
)
- assert adapter._expand_link_entities(msg) == "plain text"
+ assert expand_link_entities(msg) == "plain text"
-def test_offset_inside_utf16_surrogate_pair_is_ignored(adapter):
+def test_offset_inside_utf16_surrogate_pair_is_ignored():
msg = _Message(
text="🔥 link",
entities=[_Entity("text_link", 1, 1, "https://example.com/mid-surrogate")],
)
- assert adapter._expand_link_entities(msg) == "🔥 link"
+ assert expand_link_entities(msg) == "🔥 link"
+
+
+def test_anchor_that_is_already_the_url_is_not_duplicated():
+ url = "https://example.com/self"
+ msg = _Message(text=f"see {url} now", entities=[_Entity("text_link", 4, len(url), url)])
+ assert expand_link_entities(msg) == f"see {url} now"