fix(telegram): backtick code spans silently dropped from message content#518
Conversation
Telegram parses backtick-delimited code spans and code blocks from message text and stores the formatting as entities (type "code"/"pre"), stripping the backticks from the text field. The broker was ignoring these entities, silently dropping code formatting from inbound messages. Extend resolveUserMentions to reconstruct backtick wrapping from "code" and "pre" entities so agents receive the original formatting. Also fix stripMentions to preserve newlines (previously collapsed all whitespace via strings.Fields), which is needed for multi-line code blocks. Closes #285
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request updates dependencies and implements the preservation of code blocks (pre entities) and inline code spans (code entities) from Telegram messages by re-wrapping them in backticks. It also updates stripMentions to process messages line-by-line to preserve newlines. Feedback on the changes points out that using strings.Fields inside stripMentions collapses consecutive whitespace and strips indentation, which ruins the formatting of restored code blocks. A regex-based replacement is suggested to preserve all whitespace and indentation.
| lines := strings.Split(text, "\n") | ||
| for i, line := range lines { | ||
| var parts []string | ||
| for _, word := range strings.Fields(line) { | ||
| if !strings.HasPrefix(word, "@") { | ||
| parts = append(parts, word) | ||
| continue | ||
| } | ||
| continue | ||
| name := strings.TrimPrefix(word, "@") | ||
| cleaned := strings.TrimRightFunc(name, func(r rune) bool { | ||
| return unicode.IsPunct(r) && r != '_' && r != '-' | ||
| }) | ||
| if remove[strings.ToLower(cleaned)] { | ||
| trailing := name[len(cleaned):] | ||
| if trailing != "" { | ||
| parts = append(parts, trailing) | ||
| } | ||
| continue | ||
| } | ||
| parts = append(parts, word) | ||
| } | ||
| parts = append(parts, word) | ||
| lines[i] = strings.Join(parts, " ") | ||
| } | ||
| return strings.Join(parts, " ") | ||
| return strings.Join(lines, "\n") |
There was a problem hiding this comment.
Using strings.Fields inside stripMentions collapses all consecutive whitespace characters (including spaces and tabs) into a single space, and completely strips leading/trailing whitespace (indentation) on every line of the message. This destroys the formatting and indentation of restored code blocks (pre blocks) and inline code spans, which defeats the purpose of preserving them.
Additionally, your unit tests didn't catch this because the test cases (e.g., TestV2_HandleGroupMessage_PreBlockPreserved) only use unindented code lines.
We can fix this by using a regular expression to find and strip the target mentions while fully preserving all other whitespace and indentation.
mentionRx := regexp.MustCompile("(^|\\s)(@[^\\s]+)")
return mentionRx.ReplaceAllStringFunc(text, func(m string) string {
var prefix string
word := m
r, size := utf8.DecodeRuneInString(m)
if unicode.IsSpace(r) {
prefix = m[:size]
word = m[size:]
}
if !strings.HasPrefix(word, "@") {
return m
}
name := word[1:]
cleaned := strings.TrimRightFunc(name, func(r rune) bool {
return unicode.IsPunct(r) && r != '_' && r != '-'
})
if remove[strings.ToLower(cleaned)] {
trailing := name[len(cleaned):]
return prefix + trailing
}
return m
})Replace strings.Fields-based word processing with regex-based approach that finds and strips target mentions without collapsing whitespace or destroying indentation in code blocks and inline code spans.
…ent (GoogleCloudPlatform#518) * fix(telegram): restore backtick code spans from Telegram entities Telegram parses backtick-delimited code spans and code blocks from message text and stores the formatting as entities (type "code"/"pre"), stripping the backticks from the text field. The broker was ignoring these entities, silently dropping code formatting from inbound messages. Extend resolveUserMentions to reconstruct backtick wrapping from "code" and "pre" entities so agents receive the original formatting. Also fix stripMentions to preserve newlines (previously collapsed all whitespace via strings.Fields), which is needed for multi-line code blocks. Closes GoogleCloudPlatform#285 * fix(telegram): preserve whitespace in stripMentions for code blocks Replace strings.Fields-based word processing with regex-based approach that finds and strips target mentions without collapsing whitespace or destroying indentation in code blocks and inline code spans. --------- Co-authored-by: Scion Agent (mi-dev-b) <agent@scion.dev>
Summary
Fixes issue ptone/scion - 285: backtick-wrapped text (inline code spans) was silently dropped when sending messages via the Telegram integration due to Telegram's markdown parsing.
What changed