← Back to PRs

#16441: feat: add topicId matching to bindings for Telegram forum topics

by alextnetto open 2026-02-14 19:10 View on GitHub →
channel: telegram stale size: S
## Summary Add an optional `topicId` field to the binding match schema, enabling per-topic agent routing in Telegram forum groups. ## Use Case In multi-agent setups, different Telegram forum topics in the same group should route to different agents. Currently, bindings can match on `channel`, `peer`, `guildId`, `teamId`, and `roles` — but not on Telegram forum topic IDs. ## Changes - **`src/config/types.agents.ts`**: Add optional `topicId: string` to `AgentBinding.match` - **`src/routing/resolve-route.ts`**: - Add `topicId` to `ResolveAgentRouteInput`, `NormalizedBindingMatch`, and `BindingScope` - New `binding.peer+topic` tier ranked above `binding.peer` for higher specificity - `matchesBindingScope` checks `topicId` when present on a binding - **`src/telegram/bot-message-context.ts`**, **`bot-handlers.ts`**, **`bot-native-commands.ts`**: Pass `topicId` (from `resolvedThreadId`) to `resolveAgentRoute` - **`src/routing/resolve-route.test.ts`**: 3 new tests covering topic match, topic mismatch, and no-topic fallback ## Example Config ```json { "agentId": "cfo", "match": { "channel": "telegram", "peer": { "kind": "group", "id": "-1003888463743" }, "topicId": "315" } } ``` ## Backward Compatibility When `topicId` is absent from a binding, behavior is unchanged — matches all topics in the group as before. <!-- greptile_comment --> <h3>Greptile Summary</h3> This PR adds optional `topicId`-based routing for Telegram forum topics, enabling per-topic agent binding in multi-agent setups. The routing logic correctly introduces a new `binding.peer+topic` tier above `binding.peer`, and `matchesBindingScope` properly prevents topic-constrained bindings from matching at lower tiers when the topic doesn't match. The Telegram handler files consistently pass `resolvedThreadId` as `topicId`. Tests cover the key scenarios. - **Critical:** `topicId` is missing from the Zod validation schema in `src/config/zod-schema.agents.ts`. The `match` object uses `.strict()`, so any config with `topicId` in a binding will be **rejected at config load time**, making the feature unusable until the schema is updated. - **Minor:** Debug log lines in `resolve-route.ts` do not include `topicId`, which will make it harder to troubleshoot topic-based routing issues. <h3>Confidence Score: 2/5</h3> - The feature will fail at config validation due to a missing Zod schema update — merging as-is will prevent anyone from using topicId in bindings. - The routing logic itself is well-implemented and the tests are solid, but the missing `topicId` in the Zod `BindingsSchema` (.strict() mode) means the feature is broken at the config layer. This is a blocking issue that must be fixed before merge. - `src/config/zod-schema.agents.ts` — needs `topicId: z.string().optional()` added to the binding match schema. <sub>Last reviewed commit: 210c2f1</sub> <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs