Skip to content

feat(telegram): auto-rename forum topics on session title change - #9921

Closed
adam-tpb wants to merge 1 commit into
NousResearch:mainfrom
adam-tpb:feat/telegram-auto-rename-topic
Closed

adam-tpb wants to merge 1 commit into
NousResearch:mainfrom
adam-tpb:feat/telegram-auto-rename-topic

Conversation

@adam-tpb

Copy link
Copy Markdown

What does this PR do?

Automatically renames Telegram forum topics to match the session title when auto_rename_topics: true is set in config. This keeps topic lists scannable — instead of generic names like "General" or "Topic 3", each topic shows what the conversation is actually about.

Fires in two cases:

  1. After the auto-generated title is set (first exchange, background thread)
  2. When the user manually sets a title via /title

Related Issue

No specific issue, but closely related to:

This bridges the gap between "session has a title" and "Telegram topic reflects that title."

Type of Change

  • ✨ New feature (non-breaking change that adds functionality)

Changes Made

  • agent/title_generator.py: Added on_title_set callback to auto_title_session() and maybe_auto_title(). Fires after title is stored. Decoupled from platform code.
  • gateway/platforms/telegram.py: Added rename_forum_topic() method (calls editForumTopic Bot API) and _is_auto_rename_topics_enabled() config check.
  • gateway/run.py: Added _make_title_rename_callback() (thread-safe for auto-title background thread) and _rename_topic_for_source() (async helper for manual /title). Both only activate when platform=Telegram, thread_id exists, and config flag is set.
  • website/docs/user-guide/messaging/telegram.md: Full documentation section for the feature.
  • tests/gateway/test_auto_rename_topics.py: 9 new tests.
  • tests/agent/test_title_generator.py: Updated 1 assertion for new kwarg.

How to Test

  1. Enable in ~/.hermes/config.yaml:
    platforms:
      telegram:
        extra:
          auto_rename_topics: true
  2. In a Telegram forum topic, send a first message to Hermes.
  3. Wait ~5 seconds for title generation — the topic name should update automatically.
  4. Also test /title My Custom Name — topic should rename immediately.

Checklist

Code

  • My commit messages follow Conventional Commits (feat(telegram):)
  • I searched for existing PRs — no duplicate found
  • My PR contains only changes related to this feature
  • I have run pytest and all tests pass (51 passed, 0 failed)
  • I have added tests for my changes

Documentation & Housekeeping

  • I have updated relevant documentation (website/docs/user-guide/messaging/telegram.md)
  • cli-config.yaml.example — N/A (config lives in user config.yaml under platforms.telegram.extra)
  • CONTRIBUTING.md / AGENTS.md — N/A (no architecture changes)
  • Cross-platform impact — N/A (Telegram-only, server-side Bot API call)
  • Tool descriptions/schemas — N/A (no tool changes)

When auto_rename_topics: true is set under platforms.telegram.extra
in config.yaml, Hermes automatically renames Telegram forum topics to
match the session title.

This fires in two cases:
1. After the auto-generated title is set (first exchange)
2. When the user manually sets a title via /title

Only applies to Telegram forum topics (sessions with a thread_id).
Requires the bot to be admin in the supergroup.

Changes:
- agent/title_generator.py: add on_title_set callback
- gateway/platforms/telegram.py: add rename_forum_topic method
- gateway/run.py: wire up callback for auto-title and manual /title
- tests/gateway/test_auto_rename_topics.py: 9 new tests
- tests/agent/test_title_generator.py: update assertion for new kwarg
- website/docs/user-guide/messaging/telegram.md: feature docs
@barronlroth

Copy link
Copy Markdown
Contributor

Would love to see this implemented/merged. As Telegram topics become a core way to organize Hermes conversations — especially now that private-chat topics are first-class in the Bot API — keeping the visible Telegram topic name in sync with the generated/manual session title feels pretty important.

Without this, the actual Hermes session title and the Telegram sidebar drift apart, which makes topic-heavy usage much harder to scan. This PR looks like the right product behavior to me.

@sbosshardt

Copy link
Copy Markdown

I applied the code changes in this PR to my Hermes Agent, but I noticed threads don't always auto rename for me. I asked my agent to help identify and fix the bug, and it identified an issue in the auto-title callback path.

_make_title_rename_callback() currently relies on asyncio.get_running_loop(). In the worker-thread execution path (run_sync), there may be no running loop in that thread, so callback setup can be skipped and topic rename won’t fire.

This is fixable by passing the gateway loop from the caller (_loop_for_step) and only falling back to get_running_loop() when needed. Another small improvement is changing the surrounding auto-title hook to debug-log skips instead of silently discarding exceptions.

diff --git a/gateway/run.py b/gateway/run.py
@@
-    def _make_title_rename_callback(self, source: SessionSource):
+    def _make_title_rename_callback(self, source: SessionSource, loop=None):
@@
-        _loop = asyncio.get_running_loop()
+        _loop = loop
+        if _loop is None:
+            try:
+                _loop = asyncio.get_running_loop()
+            except RuntimeError:
+                # _run_agent executes in a worker thread (run_sync) with no
+                # running event loop; callers should pass the gateway loop.
+                return None
@@
-                    _title_callback = self._make_title_rename_callback(source)
+                    _title_callback = self._make_title_rename_callback(source, loop=_loop_for_step)
@@
-                except Exception:
-                    pass
+                except Exception as e:
+                    logger.debug("Auto-title hook skipped: %s", e)

Targeted local check passed:
venv/bin/python -m pytest tests/gateway/test_auto_rename_topics.py

Cherry-pickable commit from my repo:
11bf19d91e956d16967e8749bdd3803793918d41
(also available as compare: adam-tpb/hermes-agent@feat/telegram-auto-rename-topic...sbosshardt:fix/pr-9921-loop-callback-prbase)

@Wahrheitssuchender

Copy link
Copy Markdown

What I would actually like to see is the other way around. Match the session title to the Forum Topic title. When creating a new topic in the Telegram supergroup a session is also started and the title is based on the first messages. Instead the session title should match the topic title! This makes much more sense than the other way around where my telegram topics would rename to some random session title based on a message.
Also as sessions can close automatically or after a /reset new sessions for the same topic are created. These session titles should reflect also the topic title and to not create duplicate session titles have an increasing number eg Topic Title (1), then Topic Title (2) etc. This will make it way easier to identify sessions that belong to the same topic.
I tried my Hermes agent to create a skill to do this automatically but it keeps failing to read topic titles from Telegram. That doesn't seem to be possible with the API .

@teknium1

Copy link
Copy Markdown
Collaborator

Thanks for the Telegram topic-title synchronization work. Current main now provides this behavior, including the worker-thread scheduling concern raised in the discussion.

Automated hermes-sweeper review evidence:

  • gateway/run.py:19015 installs the generated-title callback for Telegram topic lanes; gateway/run.py:13720 schedules it using the stored gateway loop when invoked from the background title thread.
  • gateway/run.py:13612 performs the rename with title sanitization, session-binding verification, preservation of operator-declared topics, and the disable_topic_auto_rename opt-out.
  • gateway/slash_commands.py:3487 propagates successful manual /title changes through the same rename scheduler, shipped in 38f1a923af6e77cad16a4a270c74f79847311c2b (v2026.7.1).
  • Existing coverage includes generated-title rename and guards in tests/gateway/test_telegram_topic_mode.py:888, plus manual /title propagation in tests/gateway/test_title_command.py:173.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have platform/telegram Telegram bot adapter sweeper:implemented-on-main Sweeper: behavior already present on current main type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants