Commit 01e5e6e
authored
feat(telegram): rich.py renderer for Bot API 10.1 (chat@4.31 4662309 — TG2/4) (#164)
* feat(telegram): rich.py renderer for Bot API 10.1 (chat@4.31 4662309 — TG2/4)
Port packages/adapter-telegram/src/rich.ts character-for-character: the
rich-message -> Markdown / plain-text renderer and media extractor for the
Bot API 10.1 rich-message wire types introduced in TG1.
- TELEGRAM_RICH_MESSAGE_LIMIT = 32768
- truncate_rich_markdown: code-point-aware (list(str) == Array.from), reuses
StreamingMarkdownRenderer.push/finish so the truncated prefix stays valid
markdown; never splits a non-BMP code point.
- escape_text: MARKDOWN_PUNCTUATION /[!-/:-@[-`{-~]/ backslash-escape pass.
- inline/code fence run-sizing: max(1, longest_run+1) / max(3, longest_run+1).
- recursive text()/plain()/block()/plain_block()/media() walkers over every
TelegramRichText / TelegramRichBlock variant, via match statements that
mirror the upstream switch and let pyrefly narrow the recursive union.
Tests port the rich.test.ts it() blocks plus adversarial coverage: a full
punctuation escape sweep (in-class escaped, out-of-class untouched), all-
backtick fence growth, and a surrogate-pair truncation-boundary check. Each
fails on a plausible mutation. Test fixtures annotated to concrete TG1 variant
TypedDicts; pyrefly 0 on src and the test file.
* fix(telegram): JS-truthy empty-list rich-text gates + mutation coverage
Render optional rich-text fields (credit/caption/cell text) when the value
is an empty list []. Upstream gates these with `value.x ? ... : ""`; JS
arrays are always truthy, so credit: [] renders a blank credit, while the
prior Python-truthiness gate wrongly skipped it. Add a _present() helper
that renders for everything except None/absent and the empty string "".
Strip with the exact ECMAScript WhiteSpace+LineTerminator set (JS_WHITESPACE)
instead of Python's broader Unicode-whitespace strip(), matching JS
trimEnd()/trim() character-for-character.
Add regression tests, each verified to fail on its mutation:
- empty-list credit renders (truthiness trap)
- no-backtick pre block emits a 3-backtick fence (max(3,...)/else 3)
- truncation reserves room for the ellipsis (end = LIMIT - 3)
- exact-limit-length input passes through unchanged (<= boundary)1 parent 8939d97 commit 01e5e6e
2 files changed
Lines changed: 894 additions & 0 deletions
0 commit comments