| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- """Render agent markdown into Slack Block Kit blocks.
- Opt-in (``slack.extra.rich_blocks: true``) alternative to the flat mrkdwn
- ``text`` payload produced by :meth:`SlackAdapter.format_message`. Block Kit
- gives us real structural primitives — section headers, dividers, and true
- *nested* lists via ``rich_text`` — that plain mrkdwn can only approximate.
- Design constraints (why this module is deliberately conservative):
- * **Markdown pipe-tables render as native ``table`` blocks** — real grid
- cells with per-column alignment and inline-formatted ``rich_text`` content.
- A table that exceeds Slack's limits (100 rows / 20 cols / 10k aggregate
- cell chars) or won't parse falls back to aligned monospace
- ``rich_text_preformatted`` so a large table never breaks the message.
- * **Slack caps a message at 50 blocks** and a ``section``/text object at 3000
- characters. :func:`render_blocks` enforces both and, if the content simply
- cannot be expressed within them, returns ``None`` so the caller falls back
- to the plain-text path. A rich render is a nice-to-have; it must never lose
- a message.
- * **Every blocks payload MUST ship a ``text`` fallback.** Slack uses it for
- notifications, screen readers, and old clients. This module only builds the
- ``blocks`` list; the adapter pairs it with the existing mrkdwn string.
- The renderer never raises: any unexpected input degrades to ``None`` (caller
- uses plain text). It is a pure function of its input — no Slack client, no
- adapter state — so it is trivially unit-testable.
- """
- from __future__ import annotations
- import re
- from typing import Any, Dict, List, Optional, Tuple
- # Slack Block Kit hard limits (https://docs.slack.dev/reference/block-kit/blocks)
- MAX_BLOCKS = 50
- MAX_SECTION_TEXT = 3000
- MAX_HEADER_TEXT = 150
- # Native table block limits (https://docs.slack.dev/reference/block-kit/blocks/table-block)
- MAX_TABLE_ROWS = 100
- MAX_TABLE_COLS = 20
- MAX_TABLE_CHARS = 10000 # aggregate across all cells
- Block = dict[str, Any]
- # ----------------------------------------------------------------------------
- # Line classification
- # ----------------------------------------------------------------------------
- _HR_RE = re.compile(r"^\s{0,3}([-*_])(?:\s*\1){2,}\s*$")
- _HEADER_RE = re.compile(r"^\s{0,3}(#{1,6})\s+(.+?)\s*#*\s*$")
- _FENCE_RE = re.compile(r"^\s*(`{3,}|~{3,})(.*)$")
- _ORDERED_RE = re.compile(r"^(\s*)(\d+)[.)]\s+(.*)$")
- _BULLET_RE = re.compile(r"^(\s*)[-*+]\s+(.*)$")
- _QUOTE_RE = re.compile(r"^\s{0,3}>\s?(.*)$")
- _TABLE_SEP_RE = re.compile(r"^\s*\|?\s*:?-{1,}:?\s*(\|\s*:?-{1,}:?\s*)+\|?\s*$")
- def _is_list_line(line: str) -> bool:
- """True if ``line`` is a markdown list item (bullet or ordered)."""
- return bool(_BULLET_RE.match(line) or _ORDERED_RE.match(line))
- def _indent_level(spaces: str) -> int:
- """Map leading whitespace to a nesting level (2 spaces or 1 tab per level)."""
- width = 0
- for ch in spaces:
- width += 4 if ch == "\t" else 1
- return min(width // 2, 5) # Slack rich_text_list supports up to indent 5
- # ----------------------------------------------------------------------------
- # Inline markdown → rich_text elements
- # ----------------------------------------------------------------------------
- # Order matters: code first (opaque), then links, then emphasis.
- _INLINE_CODE_RE = re.compile(r"`([^`]+)`")
- _LINK_RE = re.compile(r"(?<!!)\[([^\]]+)\]\(([^()\s]+(?:\([^()]*\)[^()\s]*)*)\)")
- # Slack mrkdwn autolink: <scheme:target> or <scheme:target|label>.
- _SLACK_LINK_RE = re.compile(
- r"<([a-zA-Z][a-zA-Z0-9+.\-]*:[^>|]+)(?:\|([^>]+))?>"
- )
- # Slack mention tokens carry no scheme: <@U…> (user), <#C…> (channel, with an
- # optional |name label), <!subteam^S…> (usergroup) and <!here|!channel|!everyone>
- # (broadcast). rich_text does not interpret mrkdwn, so these must become
- # user/channel/broadcast/usergroup elements — as plain text Slack renders the
- # token literally.
- _SLACK_MENTION_RE = re.compile(
- r"<(@[A-Z0-9]+|#[A-Z0-9]+|!subteam\^[A-Z0-9]+|!(?:here|channel|everyone))(?:\|[^>]*)?>"
- )
- _BOLD_RE = re.compile(r"(?:\*\*|__)(.+?)(?:\*\*|__)")
- _ITALIC_RE = re.compile(r"(?<![\*_])(?:\*|_)(?![\*_\s])(.+?)(?<![\*_\s])(?:\*|_)(?![\*_])")
- _STRIKE_RE = re.compile(r"~~(.+?)~~")
- def _mention_element(body: str) -> Optional[dict[str, Any]]:
- """Map a Slack mention token body (without the angle brackets) to a rich_text element.
- ``@U…`` -> user, ``#C…`` -> channel, ``!subteam^S…`` -> usergroup,
- ``!here``/``!channel``/``!everyone`` -> broadcast.
- Returns None for anything else, so the caller can emit the raw token as text."""
- if body.startswith("@"):
- return {"type": "user", "user_id": body[1:]}
- if body.startswith("#"):
- return {"type": "channel", "channel_id": body[1:]}
- if body.startswith("!subteam^"):
- return {"type": "usergroup", "usergroup_id": body[len("!subteam^"):]}
- if body.startswith("!"):
- return {"type": "broadcast", "range": body[1:]}
- return None
- def _inline_elements(text: str) -> list[dict[str, Any]]:
- """Parse a run of inline markdown into rich_text section child elements.
- Produces ``text`` elements (optionally styled bold/italic/strike/code) and ``link`` elements.
- Unmatched markup is emitted verbatim as plain text, so this never loses characters."""
- elements: list[dict[str, Any]] = []
- def emit_text(s: str, style: Optional[dict[str, bool]] = None) -> None:
- if not s:
- return
- el: dict[str, Any] = {"type": "text", "text": s}
- if style:
- el["style"] = style
- elements.append(el)
- # Tokenize by the highest-priority markers first using a single scan.
- # We recursively split on code, then links, then emphasis to keep spans
- # from overlapping incorrectly.
- def walk(s: str, style: dict[str, bool]) -> None:
- pos = 0
- # inline code is opaque — no nested styling
- for m in _INLINE_CODE_RE.finditer(s):
- _walk_links(s[pos : m.start()], style)
- emit_text(m.group(1), {**style, "code": True})
- pos = m.end()
- _walk_links(s[pos:], style)
- def _emit_link(url: str, text: str, style: dict[str, bool]) -> None:
- link_el: dict[str, Any] = {"type": "link", "url": url, "text": text}
- if style:
- link_el["style"] = dict(style)
- elements.append(link_el)
- def _walk_links(s: str, style: dict[str, bool]) -> None:
- pos = 0
- for m in _LINK_RE.finditer(s):
- _walk_slack_links(s[pos : m.start()], style)
- _emit_link(m.group(2), m.group(1), style)
- pos = m.end()
- _walk_slack_links(s[pos:], style)
- def _walk_slack_links(s: str, style: dict[str, bool]) -> None:
- # rich_text does not interpret mrkdwn. Agents often emit <url|label>
- # (works in section/mrkdwn; was literal in lists/quotes/table cells).
- # Mention labels are dropped from the element anyway; strip them first
- # so a label like |dev_ops_alerts is not split by the italic regex.
- s = _SLACK_MENTION_RE.sub(lambda m: f"<{m.group(1)}>", s)
- pos = 0
- for m in _SLACK_LINK_RE.finditer(s):
- _walk_emphasis(s[pos : m.start()], style)
- url = m.group(1)
- _emit_link(url, m.group(2) or url, style)
- pos = m.end()
- _walk_emphasis(s[pos:], style)
- def _walk_emphasis(s: str, style: dict[str, bool]) -> None:
- if not s:
- return
- # Try bold, then strike, then italic, recursing into the inner span.
- for rx, key in ((_BOLD_RE, "bold"), (_STRIKE_RE, "strike"), (_ITALIC_RE, "italic")):
- m = rx.search(s)
- if m:
- _walk_emphasis(s[: m.start()], style)
- inner_style = dict(style)
- inner_style[key] = True
- _walk_emphasis(m.group(1), inner_style)
- _walk_emphasis(s[m.end() :], style)
- return
- # Base case: split on mention tokens at the leaf so emphasis spans
- # wrapping a mention are kept intact, while mentions themselves
- # remain unstyled (Slack rejects styled mention elements).
- pos = 0
- for m in _SLACK_MENTION_RE.finditer(s):
- emit_text(s[pos : m.start()], dict(style) if style else None)
- element = _mention_element(m.group(1))
- if element is not None:
- elements.append(element)
- else:
- emit_text(m.group(0), dict(style) if style else None)
- pos = m.end()
- emit_text(s[pos:], dict(style) if style else None)
- walk(text, {})
- return elements or [{"type": "text", "text": text}]
- # ----------------------------------------------------------------------------
- # Structural block builders
- # ----------------------------------------------------------------------------
- def _nonempty_elements(elements: list[dict[str, Any]]) -> list[dict[str, Any]]:
- """Make a rich_text child-element list safe for Slack.
- Slack rejects any ``rich_text_section`` / ``rich_text_preformatted`` / ``rich_text_quote`` whose
- ``elements`` list is empty or contains a ``text`` element of zero length (``invalid_blocks``:
- "missing element" / "must be more than 0 characters"). Empty content is common — ragged table
- rows are padded with ``""``, agents emit empty code fences around empty tool output, blank quote
- lines and empty list items occur in the wild — so drop zero-length text elements and, if nothing
- remains, substitute a single space, which renders as blank yet stays schema-valid. Used by every
- rich_text builder so empty content can never poison the whole payload."""
- els = [e for e in elements if not (e.get("type") == "text" and not e.get("text"))]
- return els or [{"type": "text", "text": " "}]
- def _header_block(text: str) -> Optional[Block]:
- # header blocks are plain_text only, 150 char cap.
- clean = re.sub(r"[*_~`]", "", text).strip()
- if not clean:
- # Emphasis-/whitespace-only header (e.g. "# ***" or "# ") reduces to
- # empty; Slack rejects an empty plain_text with invalid_blocks. Skip it
- # (caller drops None) rather than poison the whole payload.
- return None
- if len(clean) > MAX_HEADER_TEXT:
- clean = clean[: MAX_HEADER_TEXT - 1] + "…"
- return {"type": "header", "text": {"type": "plain_text", "text": clean, "emoji": True}}
- def _divider_block() -> Block:
- return {"type": "divider"}
- def _rich_text_block(kind: str, children: list[dict[str, Any]]) -> Block:
- """One ``rich_text`` block wrapping a single ``kind`` element (section/quote/preformatted)."""
- return {
- "type": "rich_text", "elements": [{"type": kind, "elements": _nonempty_elements(children)}]}
- def _preformatted_block(text: str) -> Block:
- # rich_text_preformatted renders monospace; used for code fences + tables.
- return _rich_text_block("rich_text_preformatted", [{"type": "text", "text": text.rstrip("\n")}])
- def _quote_block(lines: list[str]) -> Block:
- section_children: list[dict[str, Any]] = []
- for i, ln in enumerate(lines):
- if i:
- section_children.append({"type": "text", "text": "\n"})
- section_children.extend(_inline_elements(ln))
- return _rich_text_block("rich_text_quote", section_children)
- def _list_block(items: list[tuple[int, bool, str]]) -> Block:
- """Build ONE rich_text block from consecutive list items.
- ``items`` is a list of ``(indent, ordered, text)``. Each contiguous run sharing the same
- (indent, ordered) becomes a ``rich_text_list`` element; indentation changes start a new element,
- which is how Slack renders true nesting."""
- elements: list[dict[str, Any]] = []
- cur: Optional[dict[str, Any]] = None
- cur_key: Optional[tuple[int, bool]] = None
- for indent, ordered, text in items:
- key = (indent, ordered)
- if key != cur_key:
- cur = {
- "type": "rich_text_list", "style": "ordered" if ordered else "bullet",
- "indent": indent, "elements": []}
- elements.append(cur)
- cur_key = key
- cur["elements"].append(
- {"type": "rich_text_section", "elements": _nonempty_elements(_inline_elements(text))})
- return {"type": "rich_text", "elements": elements}
- def _section_block(text: str) -> Block:
- return {"type": "section", "text": {"type": "mrkdwn", "text": text}}
- # ----------------------------------------------------------------------------
- # Table handling — native Block Kit ``table`` block, monospace fallback
- # ----------------------------------------------------------------------------
- def _parse_alignment(sep_line: str) -> list[str]:
- """Parse a markdown separator row (``|:--|:-:|--:|``) into column aligns.
- Returns a list of ``"left"``/``"center"``/``"right"`` per column."""
- aligns: list[str] = []
- for cell in sep_line.strip().strip("|").split("|"):
- c = cell.strip()
- left, right = c.startswith(":"), c.endswith(":")
- aligns.append("center" if left and right else "right" if right else "left")
- return aligns
- def _split_row(row: str) -> list[str]:
- """Split a markdown table row into trimmed cell strings.
- Respects backslash-escaped pipes (``\\|``) so they aren't treated as column separators."""
- # Temporarily protect escaped pipes, split on real ones, then restore.
- protected = row.strip().strip("|").replace(r"\|", "\x00PIPE\x00")
- return [c.strip().replace("\x00PIPE\x00", "|") for c in protected.split("|")]
- def _rich_text_cell(text: str) -> dict[str, Any]:
- """A ``rich_text`` table cell carrying inline-formatted content.
- Empty cells are common (ragged rows are padded with ``""``); Slack rejects a cell whose section
- is empty or carries a zero-length text element, so the elements are routed through
- ``_nonempty_elements``."""
- return _rich_text_block("rich_text_section", _inline_elements(text))
- def _table_block(rows: list[str], sep_line: str) -> Optional[Block]:
- """Build a native Slack ``table`` block from markdown pipe-table rows.
- ``rows`` includes the header row (index 0) and body rows; ``sep_line`` is the ``|---|``
- alignment row (already consumed by the caller). Returns ``None`` when the table exceeds Slack's
- limits (100 rows / 20 cols / 10,000 aggregate cell chars) or parses to nothing — the caller then
- falls back to the monospace preformatted rendering."""
- parsed = [_split_row(r) for r in rows if r.strip()]
- if not parsed:
- return None
- ncols = max(len(r) for r in parsed)
- # Reject rather than silently truncate beyond Slack's structural limits.
- if len(parsed) > MAX_TABLE_ROWS or ncols > MAX_TABLE_COLS:
- return None
- for r in parsed:
- r.extend([""] * (ncols - len(r)))
- total_chars = sum(len(c) for r in parsed for c in r)
- if total_chars > MAX_TABLE_CHARS:
- return None
- aligns = _parse_alignment(sep_line)
- # Slack requires every provided ``column_settings`` entry to be an object.
- # Missing trailing entries inherit defaults, so only emit settings through
- # the last non-default alignment. Earlier default-left placeholders still
- # need explicit valid objects to preserve positional alignment.
- padded = [aligns[c] if c < len(aligns) else "left" for c in range(ncols)]
- last_non_default = max((c for c, a in enumerate(padded) if a != "left"), default=-1)
- column_settings = [{"align": a} for a in padded[: last_non_default + 1]]
- block: Block = {
- "type": "table", "rows": [[_rich_text_cell(cell) for cell in row] for row in parsed]}
- if column_settings:
- block["column_settings"] = column_settings
- return block
- def _render_table(rows: list[str]) -> str:
- """Render markdown pipe-table rows as aligned monospace text (fallback)."""
- parsed = [_split_row(r) for r in rows]
- if not parsed:
- return "\n".join(rows)
- ncols = max(len(r) for r in parsed)
- for r in parsed:
- r.extend([""] * (ncols - len(r)))
- widths = [max(len(r[c]) for r in parsed) for c in range(ncols)]
- out_lines = []
- for ri, r in enumerate(parsed):
- line = " | ".join(r[c].ljust(widths[c]) for c in range(ncols))
- out_lines.append(line.rstrip())
- if ri == 0: # header underline
- out_lines.append("-+-".join("-" * widths[c] for c in range(ncols)))
- return "\n".join(out_lines)
- # ----------------------------------------------------------------------------
- # Public entry point
- # ----------------------------------------------------------------------------
- def render_blocks(markdown: str, mrkdwn_fn=None) -> Optional[list[Block]]:
- """Convert agent markdown to a Slack Block Kit ``blocks`` list.
- Args:
- markdown: The agent's response text (standard markdown).
- mrkdwn_fn: Optional callable converting a markdown paragraph to Slack
- mrkdwn for ``section`` blocks (the adapter passes
- ``format_message``). When ``None``, the raw paragraph text is used.
- Returns:
- A list of Block Kit block dicts, or ``None`` when the content is empty,
- exceeds Slack's structural limits, or hits an unexpected shape — the
- caller then falls back to the flat ``text`` payload. Never raises."""
- if not markdown or not markdown.strip():
- return None
- fmt = mrkdwn_fn or (lambda s: s)
- try:
- blocks: list[Block] = []
- lines = markdown.replace("\r\n", "\n").split("\n")
- i = 0
- n = len(lines)
- para: list[str] = []
- def flush_para() -> None:
- if not para:
- return
- text = "\n".join(para).strip()
- para.clear()
- if not text:
- return
- rendered = fmt(text)
- # Split oversized sections on the 3000-char limit.
- for chunk in _split_text(rendered, MAX_SECTION_TEXT):
- blocks.append(_section_block(chunk))
- while i < n:
- line = lines[i]
- # Blank line: paragraph boundary
- if not line.strip():
- flush_para()
- i += 1
- continue
- # Fenced code block
- fence = _FENCE_RE.match(line)
- if fence:
- flush_para()
- marker = fence.group(1)
- body: list[str] = []
- i += 1
- while i < n and not lines[i].lstrip().startswith(marker):
- body.append(lines[i])
- i += 1
- i += 1 # consume closing fence
- blocks.append(_preformatted_block("\n".join(body)))
- continue
- # Horizontal rule → divider
- if _HR_RE.match(line):
- flush_para()
- blocks.append(_divider_block())
- i += 1
- continue
- # ATX header
- hm = _HEADER_RE.match(line)
- if hm:
- flush_para()
- header = _header_block(hm.group(2))
- if header is not None:
- blocks.append(header)
- i += 1
- continue
- # Pipe table: current line has a pipe AND next line is a separator
- if "|" in line and i + 1 < n and _TABLE_SEP_RE.match(lines[i + 1]):
- flush_para()
- header_row = line
- sep_line = lines[i + 1]
- trows = [header_row]
- i += 2 # skip header + separator
- while i < n and "|" in lines[i] and lines[i].strip():
- trows.append(lines[i])
- i += 1
- # Prefer a native Block Kit table; fall back to aligned
- # monospace when it exceeds Slack's table limits or won't parse.
- table = _table_block(trows, sep_line)
- if table is not None:
- blocks.append(table)
- else:
- blocks.append(_preformatted_block(_render_table(trows)))
- continue
- # Blockquote group
- if _QUOTE_RE.match(line):
- flush_para()
- qlines: list[str] = []
- while i < n:
- qm = _QUOTE_RE.match(lines[i])
- if not qm:
- break
- qlines.append(qm.group(1))
- i += 1
- blocks.append(_quote_block(qlines))
- continue
- # List group (bullets + ordered, with nesting)
- if _is_list_line(line):
- flush_para()
- items: list[tuple[int, bool, str]] = []
- while i < n:
- bm = _BULLET_RE.match(lines[i])
- om = _ORDERED_RE.match(lines[i])
- if bm:
- items.append((_indent_level(bm.group(1)), False, bm.group(2)))
- i += 1
- elif om:
- items.append((_indent_level(om.group(1)), True, om.group(3)))
- i += 1
- elif lines[i].strip() and lines[i].startswith((" ", "\t")) and items:
- # continuation line of the previous item
- indent, ordered, txt = items[-1]
- items[-1] = (indent, ordered, txt + " " + lines[i].strip())
- i += 1
- elif not lines[i].strip() and items:
- # Blank line inside a list run. LLM-authored ordered
- # lists commonly separate items with a blank line; if
- # the next non-blank line is another list item, treat
- # the blank(s) as a soft separator and keep the run
- # going so the items stay in one rich_text_list (Slack
- # numbers each list independently, so splitting would
- # restart every item at "1."). Otherwise the blank
- # ends the list.
- j = i + 1
- while j < n and not lines[j].strip():
- j += 1
- if j < n and _is_list_line(lines[j]):
- i = j
- else:
- break
- else:
- break
- blocks.append(_list_block(items))
- continue
- # Default: accumulate into a paragraph
- para.append(line)
- i += 1
- flush_para()
- if not blocks:
- return None
- if len(blocks) > MAX_BLOCKS:
- # Too structurally complex to express safely — let the caller fall
- # back to plain text rather than truncating and losing content.
- return None
- return blocks
- except Exception:
- # Never let a rendering bug drop a message.
- return None
- def _split_text(text: str, limit: int) -> list[str]:
- """Split ``text`` into <= ``limit``-char chunks on line, then hard, boundaries.
- Chunks are fence-balanced: when a split lands inside a ``` code span that survived into section
- text (the renderer normally routes fenced blocks to ``rich_text_preformatted``, but mrkdwn text
- can still carry fences), the fence is closed at the end of the chunk and reopened on the next so
- each section renders correctly on its own."""
- if len(text) <= limit:
- return [text]
- # Reserve headroom for the close/reopen markers the balancing pass adds.
- split_limit = max(limit - 8, limit // 2, 1) if "```" in text else limit
- out: list[str] = []
- remaining = text
- while len(remaining) > split_limit:
- cut = remaining.rfind("\n", 0, split_limit)
- if cut <= 0:
- cut = split_limit
- out.append(remaining[:cut])
- remaining = remaining[cut:].lstrip("\n")
- if remaining:
- out.append(remaining)
- if len(out) > 1 and "```" in text:
- balanced: list[str] = []
- reopen = False
- for chunk in out:
- if reopen:
- chunk = "```\n" + chunk
- odd = chunk.count("```") % 2 == 1
- if odd:
- chunk += "\n```"
- reopen = odd
- balanced.append(chunk)
- out = balanced
- return out
- # ----------------------------------------------------------------------------
- # Outbound payload boundary — last-resort clamp before the Slack API
- # ----------------------------------------------------------------------------
- def _clamp_text_obj(text_obj: dict[str, Any], limit: int) -> dict[str, Any]:
- """Return ``text_obj`` with its ``text`` clamped to ``limit`` chars."""
- txt = text_obj.get("text") or ""
- if len(txt) <= limit:
- return text_obj
- clamped = dict(text_obj)
- clamped["text"] = txt[: limit - 1].rstrip() + "…"
- return clamped
- def sanitize_blocks(blocks: Optional[list[Block]]) -> Optional[list[Block]]:
- """Clamp an outbound ``blocks`` payload to Slack's hard limits.
- Defensive boundary applied wherever the adapter attaches ``blocks`` to
- ``chat.postMessage`` / ``chat.update``. One oversized or malformed block
- fails the WHOLE call with ``invalid_blocks`` — approval cards then never
- update and messages silently drop — so instead of trusting every builder,
- the payload is normalized just before the API call:
- * ``section`` / ``context`` text objects are truncated to the 3000-char
- cap with an ellipsis (Slack HTML-escapes ``< > &`` on storage, so text
- echoed back through interaction payloads can exceed the limit that the
- send path originally budgeted for — see #53693 / #62054).
- * ``header`` text is truncated to its 150-char cap.
- * Empty blocks (no text / no elements / no rows) are dropped — Slack
- rejects zero-length text objects and empty element lists.
- * ``table.column_settings`` entries must all be objects; ``null`` entries
- (emitted by older renderers, per the "use null to skip" misreading of
- the schema) are replaced with ``{}`` and default trailing entries are
- trimmed (#56615).
- * The payload is capped at Slack's 50-block maximum.
- Returns the sanitized list, or ``None`` when nothing valid remains — the
- caller then sends the plain ``text`` fallback alone. Never raises."""
- if not blocks:
- return None
- try:
- out: list[Block] = []
- for block in blocks:
- if not isinstance(block, dict) or not block.get("type"):
- continue
- btype = block["type"]
- if btype == "section":
- text_obj = block.get("text")
- has_body = bool(block.get("fields")) or bool(block.get("accessory"))
- if isinstance(text_obj, dict):
- if not (text_obj.get("text") or "").strip() and not has_body:
- continue
- clamped = _clamp_text_obj(text_obj, MAX_SECTION_TEXT)
- if clamped is not text_obj:
- block = dict(block)
- block["text"] = clamped
- elif not has_body:
- continue
- elif btype == "header":
- text_obj = block.get("text")
- if not isinstance(text_obj, dict) or not (text_obj.get("text") or "").strip():
- continue
- clamped = _clamp_text_obj(text_obj, MAX_HEADER_TEXT)
- if clamped is not text_obj:
- block = dict(block)
- block["text"] = clamped
- elif btype == "context":
- elements = block.get("elements") or []
- if not elements:
- continue
- clamped_els = [
- _clamp_text_obj(el, MAX_SECTION_TEXT)
- if isinstance(el, dict) and el.get("type") in ("mrkdwn", "plain_text")
- else el
- for el in elements]
- if any(c is not e for c, e in zip(clamped_els, elements)):
- block = dict(block)
- block["elements"] = clamped_els
- elif btype in ("rich_text", "actions", "context_actions"):
- if not block.get("elements"):
- continue
- elif btype == "table":
- if not block.get("rows"):
- continue
- settings = block.get("column_settings")
- if isinstance(settings, list) and any(not isinstance(cs, dict) for cs in settings):
- fixed = [cs if isinstance(cs, dict) else {} for cs in settings]
- while fixed and not fixed[-1]:
- fixed.pop()
- block = dict(block)
- if fixed:
- block["column_settings"] = fixed
- else:
- block.pop("column_settings", None)
- out.append(block)
- if not out:
- return None
- return out[:MAX_BLOCKS]
- except Exception:
- # A sanitizer bug must never take down the send path.
- return None
|