From e3b8b6d32c90115245f57f4e7b0c5301afb92e8f Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sun, 7 Jun 2026 19:16:36 -0700 Subject: [PATCH] feat(hooks): expose thread_id and chat_type in agent:start/end context (#41672) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds thread_id and chat_type to the agent:start/end plugin hook context (via getattr with safe defaults; both are real `source` attrs already used in gateway/run.py). agent:end inherits them via **hook_ctx. Purely additive — no prompt/history mutation. Documents the full ctx dict in hooks.py. Co-authored-by: SNooZyy2 --- gateway/hooks.py | 17 +++++++++++++++++ gateway/run.py | 2 ++ 2 files changed, 19 insertions(+) diff --git a/gateway/hooks.py b/gateway/hooks.py index 5ab45119202..1ea7faa32a1 100644 --- a/gateway/hooks.py +++ b/gateway/hooks.py @@ -17,6 +17,23 @@ Events: - command:* -- Any slash command executed (wildcard match) Errors in hooks are caught and logged but never block the main pipeline. + +Context dict passed to ``agent:start`` / ``agent:end`` handlers: + platform -- source platform name (e.g. "telegram", "matrix", "slack") + user_id -- platform user id of the sender + chat_id -- platform chat id (group/DM identifier) + thread_id -- Telegram forum-topic id / thread root id (string; empty + when not in a thread / topic) + chat_type -- "dm" | "group" | "forum" (empty if unknown) + session_id -- Hermes session id + message -- inbound message text (truncated to 500 chars) + +``agent:end`` adds: + response -- agent response text (truncated to 500 chars) + +Handlers posting a follow-up into the same Telegram forum-topic should +include ``message_thread_id=int(thread_id)`` when ``chat_type == "forum"`` +and ``thread_id`` is non-empty. """ import asyncio diff --git a/gateway/run.py b/gateway/run.py index f643eadf4a7..3d0eb848d61 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -9552,6 +9552,8 @@ class GatewayRunner: "platform": source.platform.value if source.platform else "", "user_id": source.user_id, "chat_id": source.chat_id or "", + "thread_id": str(getattr(source, "thread_id", None)) if getattr(source, "thread_id", None) else "", + "chat_type": getattr(source, "chat_type", "") or "", "session_id": session_entry.session_id, "message": message_text[:500], }