Repository navigation
fix(slack): render agent replies with native Markdown #2932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
77d4f55
fix(slack): render agent replies with native Markdown
pjrule 1769840
fix(slack): retain long replies with token-based mrkdwn fallback
pjrule ff498c2
Merge branch 'main' into open-swe/slack-native-markdown
pjrule 04e1eb8
fix(slack): normalize token attributes before escaping
pjrule File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from html import escape | ||
|
|
||
| from markdown_it import MarkdownIt | ||
| from markdown_it.token import Token | ||
|
|
||
| _parser = MarkdownIt("commonmark", {"html": False}).enable("strikethrough") | ||
|
|
||
|
|
||
| def markdown_to_mrkdwn(markdown: str) -> str: | ||
| """Convert standard Markdown to Slack mrkdwn.""" | ||
| output: list[str] = [] | ||
| list_stack: list[tuple[bool, int]] = [] | ||
| quote_depth = 0 | ||
| item_prefix = "" | ||
| in_heading = False | ||
|
|
||
| for token in _parser.parse(markdown): | ||
| if token.type == "inline": | ||
| prefix = "" if in_heading else "> " * quote_depth + item_prefix | ||
| output.append(prefix + _render_inline(token.children or [])) | ||
| item_prefix = "" | ||
| elif token.type == "heading_open": | ||
| output.append("> " * quote_depth + "*") | ||
| in_heading = True | ||
| elif token.type == "heading_close": | ||
| output[-1] += "*\n" | ||
| in_heading = False | ||
| elif token.type == "paragraph_close": | ||
| output.append("\n") | ||
| elif token.type == "bullet_list_open": | ||
| list_stack.append((False, 1)) | ||
| elif token.type == "ordered_list_open": | ||
| list_stack.append((True, int(token.attrGet("start") or "1"))) | ||
| elif token.type in {"bullet_list_close", "ordered_list_close"}: | ||
| list_stack.pop() | ||
| elif token.type == "list_item_open": | ||
| ordered, number = list_stack[-1] | ||
| item_prefix = f"{number}. " if ordered else "• " | ||
| if ordered: | ||
| list_stack[-1] = (ordered, number + 1) | ||
| elif token.type == "blockquote_open": | ||
| quote_depth += 1 | ||
| elif token.type == "blockquote_close": | ||
| quote_depth -= 1 | ||
| elif token.type in {"fence", "code_block"}: | ||
| content = escape(token.content, quote=False) | ||
| output.append( | ||
| "> " * quote_depth | ||
| + f"```\n{content}{'' if content.endswith(chr(10)) else chr(10)}```\n" | ||
| ) | ||
| elif token.type == "hr": | ||
| output.append("—\n") | ||
|
|
||
| return "".join(output).rstrip() | ||
|
|
||
|
|
||
| def _render_inline(tokens: list[Token]) -> str: | ||
| output: list[str] = [] | ||
| links: list[str] = [] | ||
| markers = { | ||
| "strong_open": "*", | ||
| "strong_close": "*", | ||
| "em_open": "_", | ||
| "em_close": "_", | ||
| "s_open": "~", | ||
| "s_close": "~", | ||
| } | ||
| for token in tokens: | ||
| if token.type == "text": | ||
| output.append(escape(token.content, quote=False)) | ||
| elif token.type in markers: | ||
| output.append(markers[token.type]) | ||
| elif token.type == "code_inline": | ||
| output.append(f"`{escape(token.content, quote=False)}`") | ||
| elif token.type == "link_open": | ||
| links.append(escape(token.attrGet("href") or "", quote=False)) | ||
| output.append("<" + links[-1] + "|") | ||
| elif token.type == "link_close": | ||
| output.append(">") | ||
| links.pop() | ||
| elif token.type in {"softbreak", "hardbreak"}: | ||
| output.append("\n") | ||
| elif token.type == "image": | ||
| output.append( | ||
| f"<{escape(token.attrGet('src') or '', quote=False)}|{escape(token.content, quote=False)}>" | ||
| ) | ||
| elif token.type == "html_inline": | ||
| output.append(escape(token.content, quote=False)) | ||
| return "".join(output) | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| from agent.slack.markdown import markdown_to_mrkdwn | ||
|
|
||
|
|
||
| def test_markdown_to_mrkdwn_common_constructs() -> None: | ||
| markdown = """# Heading | ||
|
|
||
| - **bold** and *italic* and ~~gone~~ | ||
| - [link](https://example.com?a=1&b=2) | ||
|
|
||
| > quoted | ||
|
|
||
| ```python | ||
| if a < b and c > d: | ||
| print("*literal*") | ||
| ``` | ||
|
|
||
| Use `x < y & z`. | ||
| """ | ||
|
|
||
| assert ( | ||
| markdown_to_mrkdwn(markdown) | ||
| == """*Heading* | ||
| • *bold* and _italic_ and ~gone~ | ||
| • <https://example.com?a=1&b=2|link> | ||
| > quoted | ||
| ```\nif a < b and c > d: | ||
| print("*literal*") | ||
| ``` | ||
| Use `x < y & z`.""" | ||
| ) | ||
|
|
||
|
|
||
| def test_markdown_to_mrkdwn_preserves_html() -> None: | ||
| assert markdown_to_mrkdwn("<div>important content</div>") == ( | ||
| "<div>important content</div>" | ||
| ) | ||
|
|
||
|
|
||
| def test_markdown_to_mrkdwn_quotes_heading_once() -> None: | ||
| assert markdown_to_mrkdwn("> # Heading") == "> *Heading*" | ||
|
|
||
|
|
||
| def test_markdown_to_mrkdwn_numbers_items_not_paragraphs() -> None: | ||
| markdown = "1. first paragraph\n\n same item\n2. second item" | ||
| assert markdown_to_mrkdwn(markdown) == "1. first paragraph\nsame item\n2. second item" | ||
|
|
||
|
|
||
| def test_markdown_to_mrkdwn_separates_unterminated_fence_content() -> None: | ||
| assert markdown_to_mrkdwn("```\ncode") == "```\ncode\n```" |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi I am not reading or testing this carefully, since this is just rough fallback handling for a pretty extreme edge case. if we actually encounter rendering issues on this path, we can revisit