diff --git a/src/gateway/helpers.ts b/src/gateway/helpers.ts index 1ead63a..5d6d199 100644 --- a/src/gateway/helpers.ts +++ b/src/gateway/helpers.ts @@ -51,21 +51,40 @@ function getChatId(thread: any, message: any): string { /** * Resolve the agent-facing thread ID from a chat message. - * Private/DM → "main", group → "group:" + * Private/DM → "main", group → "group:", forum topic → "group::topic:" */ export function resolveAgentThreadId(thread: any, message: any): string { const chatType = String(message?.chat?.type ?? thread?.chat?.type ?? thread?.type ?? "").toLowerCase(); + + // Extract topic ID if present (forum threads) + const topicId = message?.message_thread_id ?? thread?.messageThreadId ?? extractTopicFromThreadId(thread?.id); + if (["private", "dm", "direct", "im"].includes(chatType)) return "main"; - if (["group", "supergroup", "channel"].includes(chatType)) return `group:${getChatId(thread, message)}`; + if (["group", "supergroup", "channel"].includes(chatType)) { + const base = `group:${getChatId(thread, message)}`; + return topicId ? `${base}:topic:${topicId}` : base; + } const telegramChatId = telegramChatIdFromThreadId(thread?.id); if (telegramChatId !== null) { - return telegramChatId < 0 ? `group:${telegramChatId}` : "main"; + const base = telegramChatId < 0 ? `group:${telegramChatId}` : "main"; + return (topicId && telegramChatId < 0) ? `${base}:topic:${topicId}` : base; } return String(thread?.id ?? "main"); } +/** Extract message_thread_id from thread ID string like "telegram:chatId:topicId" */ +function extractTopicFromThreadId(threadId: string | undefined): number | undefined { + if (!threadId) return undefined; + const parts = threadId.split(":"); + if (parts.length === 3 && parts[0] === "telegram") { + const parsed = parseInt(parts[2], 10); + return Number.isFinite(parsed) ? parsed : undefined; + } + return undefined; +} + // ── System Resources ───────────────────────────────── export interface SystemResources { diff --git a/test/gateway-helpers.test.ts b/test/gateway-helpers.test.ts index 699bb1c..97fc652 100644 --- a/test/gateway-helpers.test.ts +++ b/test/gateway-helpers.test.ts @@ -61,6 +61,24 @@ describe("gateway/helpers", () => { expect(resolveAgentThreadId(thread, message)).toBe("group:-456"); }); + it("routes forum topic to separate session", () => { + const thread = { id: "telegram:-100123:42" }; + const message = { chat: { type: "supergroup", id: -100123 } }; + expect(resolveAgentThreadId(thread, message)).toBe("group:-100123:topic:42"); + }); + + it("routes forum topic via message_thread_id", () => { + const thread = { id: "telegram:-100123" }; + const message = { chat: { type: "supergroup", id: -100123 }, message_thread_id: 99 }; + expect(resolveAgentThreadId(thread, message)).toBe("group:-100123:topic:99"); + }); + + it("group without topic stays flat", () => { + const thread = { id: "telegram:-100123" }; + const message = { chat: { type: "supergroup", id: -100123 } }; + expect(resolveAgentThreadId(thread, message)).toBe("group:-100123"); + }); + it("routes telegram positive IDs to main", () => { const thread = { id: "telegram:789" }; const message = {};