Repository navigation
feat(telegram): emit Bot API 10.2 rich message blocks - #107986
Merged
Merged
Conversation
obviyus
force-pushed
the
feat/telegram-rich-blocks
branch
from
July 15, 2026 07:45
696dcac to
19a11e7
Compare
This was referenced Jul 15, 2026
telegram: track re-enable criteria for richMessages default-on (Bot API 10.1 client rollout)
#103458
Open
This was referenced Jul 21, 2026
This was referenced Jul 27, 2026
This was referenced Nov 1, 2026
This was referenced Nov 3, 2026
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.
What Problem This Solves
Telegram rich messages (
channels.telegram.richMessages) were rendered by serializing agent markdown into a Telegram-specific rich-HTML string: a tag allowlist with per-tag attribute regexes, a nesting cap of 16,<br>materialization (Bot API HTML mode drops bare newlines), an HTML tag-rebalancing chunker, and a ~500-line HTML table renderer with ASCII fallbacks. Every one of those layers existed only to survive an HTML round trip that Telegram immediately parses back into blocks, and each was a standing source ofcan't parse entities400s and formatting degradations.Bot API 10.2 added
InputRichMessage.blocks, a typed block/RichTexttree, which lets the plugin emit exactly what the wire wants — and lets the full agent authoring contract (<details>, checkbox lists, tables with spans, math, media, maps) map to real typed blocks instead of sanitized HTML.Why This Change Was Made
Four commits:
Test-only: removes the rich-HTML formatter tests superseded by the blocks emitter (split out for the review bundler; see Evidence).
Blocks emitter: markdown → shared markdown IR → typed
InputRichBlock[](rich-blocks.ts), sent viasendRichMessage {blocks}/editMessageText {rich_message: {blocks}}from both funnels. The rich-HTML emitter, sanitizer, nesting limiter,<br>materialization, and HTML table renderer are deleted. The legacyparse_mode: "HTML"path (default when rich is off) is unchanged, and caller-authored HTML (formatting.parseMode: "HTML") stays on that legacy funnel even for rich accounts — parse_mode HTML preserves literal newlines and tag-aware chunking, the contract HTML callers authored against. Structural nesting is recursive (fence-in-quote =preinsideblockquote); plain-text fallback is projected from the emitted blocks so tables/captions/media URLs survive rich-send failures; 500-block/32k-char/50-media limits are enforced by surrogate-safe, wrapper-preserving splitting;skip_entity_detectionis document-scoped because the render commits to one linkify decision.HTML-island layer (
rich-blocks-html.ts) + system-prompt update: the agent prompt has always instructed models to author rich content as markdown plus HTML islands (<details><summary>,<ul>+ checkbox<input>s,<table>with caption/colspan/rowspan/align,<figure>/<img>/<video>/<audio>(+gif→animation, ogg/opus→voice note),<tg-math>/<tg-math-block>,<tg-map>,<tg-collage>/<tg-slideshow>,<aside>, anchors + fragmentanchor_links,sup/sub/mark/tg-spoiler/tg-emoji). This layer parses those islands and maps every advertised construct to its typed 10.2 block, with a strict no-silent-loss policy: malformed, unclosed, stray-content, duplicate-caption, or mixed-validity islands stay literal text rather than partially converting. The "Telegram rich ON" prompt line is rewritten for 10.2 with complete, parseable syntax examples.Message-tool sanitize fix (pre-existing bug exposed by live E2E): the Telegram outbound adapter ran every message-tool send (
visibleReplies: "automatic"groups, proactive sends) throughsanitizeForPlainText, stripping the agent's rich islands before the emitter could map them — the streamed preview rendered perfectly, then got superseded by a flattened durable final. The adapter sanitize is now effective-account-aware (thesanitizeTextadapter callback gains optionalcfg/accountId, wired indeliver.ts): rich accounts strip only assistant-visible scaffolding; non-rich accounts keep the exact old plain conversion. The island matcher also learned to ignore tag names quoted inside<code>elements (live model output mentions tags in prose, which was miscounting match depth).Error-driven degradation over speculative pre-validation: every structural rejection Telegram can raise (
BLOCKS_TOO_MANY,DEPTH_INVALID,TEXT_TOO_LONG,MEDIA_TOO_MANY,TABLE_COLS_TOO_MANY, allRICH_MESSAGE_*_INVALID,can't parse InputRichBlock) triggers the shared plain-text fallback on both funnels — all strings live-verified.User Impact
Rich-enabled accounts get the full advertised formatting surface rendered natively and reliably: collapsible tool output, native tables (spans/captions/alignment), task-list checkboxes, math, inline media/collages/maps, preserved line breaks, file refs as code instead of spurious previews, and graceful plain-text degradation that no longer drops tables, captions, or media URLs. The default (non-rich) path is unchanged.
Evidence
sendRichMessage {blocks}call and echoed structurally intact: details (open), checkbox list, math block + inline math, sub/sup/marked, divider, captioned photo, map, pullquote;editMessageText {rich_message:{blocks}},anchor_link, andskip_entity_detectionaccepted; bare\npreserved in paragraphRichText(unlike HTML mode);README.mdstayedcodewith no server re-linkification.RICH_MESSAGE_*rejections (now classified); a 501-row table, 501-child blockquote, 501-item list, and 51 maps are all accepted — only top-level blocks count toward 500 and maps don't consume the media budget, so the splitter counts exactly that.audiorejected (AUDIO_INVALID) → ogg/opus map to voice notes; gif accepted asanimation; unknown-but-numeric custom-emoji IDs accepted (degrade server-side) while non-numeric IDs 400 → validated locally.pnpm test extensions/telegram149 files / 2,808 tests, prompt snapshots, and system-prompt tests green at the final SHA;src/agentssuite (628 files / 11,801 tests) green.pnpm tsgo:extensions,pnpm check:test-types, oxlint, oxfmt all clean.visibleReplies: "automatic"): the final visible Telegram message renders nativedetails+table+mathematical_expression+ checkboxlistblocks, confirmed via the Bot API echo. Before the sanitize fix, the same run delivered a flattened final (islands stripped) while the deleted preview had been perfect.gpt-5.6-sol, high): 18 cycles across the feature commits, converging to a final clean verdict ("patch is correct — no actionable defects"). ~35 findings fixed along the way (nested-structure emission, fallback content loss, splitting/limits, island parsing edge cases); a handful rejected with live-API counterexamples (recursive block counting, fragment-link rejection, map media budget) or conscious documented trade-offs (anchor locality across >32k chunks). Every accepted external-behavior claim was settled by probing the production API, not by assumption.?token=…URL (secret-scanner false positive on removed content); those deletions were manually line-audited and only cover removed internals.