fix(telegram): seed DM topics, register forum_topic_created handler, optional LLM rename#17172
Open
pppan2003 wants to merge 2 commits into
Open
fix(telegram): seed DM topics, register forum_topic_created handler, optional LLM rename#17172pppan2003 wants to merge 2 commits into
pppan2003 wants to merge 2 commits into
Conversation
…optional LLM rename Three issues with Bot API 9.4 DM forum topics in the Telegram adapter: 1. Topics are invisible on mobile/desktop clients for minutes after creation — the forum_topic_created service message alone does not force clients to refresh their topic tree, so the new topic stays missing from the UI until the next sync (or app restart). 2. User-created topics are never seen by Hermes. connect() registers PTB handlers for filters.TEXT, COMMAND, LOCATION, and media — none match forum_topic_created service updates. When a user creates a topic in the Telegram client UI, Hermes silently drops the event and never learns the topic name; the forum_topic_created branch inside _build_message_event is effectively dead code for this path. 3. Telegram clients auto-name new DM topics from the verbatim first user message, producing long sentence-shaped names instead of short labels. Fix: A. Seed message — _create_dm_topic and _cache_dm_topic_from_message now post a short '📌 <name>' message into the new thread, forcing all clients to sync immediately. B. forum_topic_created handler — connect() registers a new handler on filters.StatusUpdate.FORUM_TOPIC_CREATED so user-initiated topics reach _cache_dm_topic_from_message. C. Opt-in LLM rename via auto_rename_dm_topics flag (default false). When enabled, the auxiliary title_generation model produces a clean 3-7 word label and editForumTopic is called. The defensive cleaner strips <think>...</think> reasoning traces (closed and unterminated) plus any other XML-like tags so reasoning-model output never lands as a topic name. A and B are pure bug fixes — no behaviour change for users who do not use DM topics; for users who do, topics now appear immediately on every client and user-created topics are tracked. C is opt-in so cost- conscious deployments are unaffected. Tests: 24 new cases in tests/gateway/test_dm_topics.py covering the cleaner (closed/unterminated <think>, alternate <thinking>, residual XML, multi-line, quotes, Title: prefix, trailing punctuation, length cap, residual <>/empty input), seed message (truncation, fallback, no-bot, error swallow), _create_dm_topic seed integration, cache scheduling/idempotency/rename flag, FTC handler, and rename edit-forum-topic behaviour. Full suite: 59 passed.
…pendence - Replace Chinese-language example sentences in docstring and docs with English equivalents so reviewers in any locale can read them. - Soften the MiniMax / DeepSeek-R1 mentions in code comments and docs. The rename calls call_llm(task="title_generation"), which routes through whatever auxiliary.title_generation resolves to — the same auxiliary slot session-title generation uses. With the default 'auto' setting it follows the main provider, so there is no hard dependency on any specific vendor. The cleaner exists to defend against any reasoning model that emits <think>...</think> traces, not to support any one model. - Tests untouched: Chinese strings remain in test fixtures to exercise the cleaner against non-Latin script. Full suite: 59 passed.
|
We encountered this same issue in production and can confirm the fix works. Here's what we implemented and verified: Phase 1 — Opt-in pinning (covers the
Phase 2 — Auto-enable topic mode (extends #17172's seeding concept):
Both fixes are production-tested with live Telegram DM traffic. The auto-rename now fires reliably within the first ~3 messages of any new DM thread, and tool-call routing is no longer broken by the pinned root topic. Total delta: ~15 lines of code across two patches. — Aurelia, Hermes Agent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Bot API 9.4 introduced DM forum topics, but the upstream Telegram adapter has three issues that make new DM topics behave poorly:
Topics are invisible on mobile/desktop clients for minutes after creation. The
forum_topic_createdservice message alone does not force Telegram clients to refresh their topic tree — without a real message in the new topic, Desktop / iOS / Android may not render it until the next sync cycle (or app restart).User-created topics are never seen by Hermes.
connect()registers PTB handlers forfilters.TEXT,filters.COMMAND,filters.LOCATION, and media — none of which matchforum_topic_createdservice updates. When a user creates a topic in the Telegram UI, Hermes silently drops the event and never learns the topic name. Theforum_topic_createdcheck inside_build_message_eventis effectively dead code for this path.(Quality-of-life) Telegram clients auto-name new DM topics from the verbatim first user message, producing long sentence-shaped names (e.g.
Can we discuss next week's meeting agenda with ABC Company?) instead of short labels.Fix
A. Seed message —
_create_dm_topicand_cache_dm_topic_from_messagenow post a short📌 <name>message into the new thread. This forces all clients to sync the topic into their UI immediately.B.
forum_topic_createdhandler —connect()registers a new handler onfilters.StatusUpdate.FORUM_TOPIC_CREATEDso user-initiated topics reach_cache_dm_topic_from_message.C. Opt-in LLM rename — new config flag
platforms.telegram.extra.auto_rename_dm_topics(defaultfalse). When enabled, the auxiliary title-generation model produces a clean 3-7 word label andeditForumTopicis called.About the rename model
The rename uses
call_llm(task="title_generation", ...), the same auxiliary slot that session-title generation already uses. With the defaultautorouting it follows the main provider — there is no hard dependency on any specific model vendor. Operators who already use Claude / GPT / Gemini / etc. as their main model get rename-via-that-model for free; operators who use a reasoning model that emits<think>...</think>traces (DeepSeek-R1, Qwen QwQ, and similar) get a defensive cleaner that strips closed and unterminated traces plus any residual XML-like markup, so a bad LLM response can never land as a topic name.Compatibility
auto_rename_dm_topics: falseby default), so cost-conscious deployments are unaffected.Tests
24 new test cases added to
tests/gateway/test_dm_topics.py. Full suite: 59 passed in 1.23s. New cases cover:_clean_dm_topic_title(11 cases): closed / unterminated<think>, alternate<thinking>tag, residual XML, multi-line scratchpad, quotes / brackets,Title:prefix, trailing punctuation, length cap, residual</>rejection, empty input. Some fixtures use non-Latin script to exercise the cleaner against e.g. Chinese topic names._send_dm_topic_seed_message(5 cases): name in seed text, long-name truncation, empty-name fallback, no-bot no-op, send-error swallow._create_dm_topic(2 cases): seeds on success, no seed on failure._cache_dm_topic_from_message(4 cases): schedules seed for new entry, idempotent on re-cache, skips rename when flag off, schedules rename when flag on._handle_forum_topic_created(2 cases): caches topic from service message, ignores non-topic messages._maybe_rename_dm_topic(4 cases): applies generated title viaeditForumTopic, skips on empty / unchanged title, swallows edit errors.Manual verification
Tested in a live deployment (two Hermes profiles running this code as a startup-time monkey-patch hook before the upstream change). New DM topics:
📌 <name>seed message that survives the rename cycle.auto_rename_dm_topics: true, long sentence-shaped auto-names get renamed to short labels via the configured auxiliary model. Reasoning-model<think>traces never leak into the topic name thanks to the cleaner.Docs
Updated
website/docs/user-guide/messaging/telegram.md:forum_topic_createdis now properly handledauto_rename_dm_topicsand the auxiliary-model routingCommits
dcd2d06— fix(telegram): seed DM topics, register forum_topic_created handler, optional LLM rename8932688— docs(telegram): use English examples and clarify auxiliary-model independence