diff --git a/internal/channels/telegram/live.go b/internal/channels/telegram/live.go index 16141ac56..bea66541b 100644 --- a/internal/channels/telegram/live.go +++ b/internal/channels/telegram/live.go @@ -510,6 +510,7 @@ func (l *liveReply) body() string { // Keeping the block outside the clamp makes the live frame, an abandoned // frame, and the finished message agree on shape throughout the turn. func (l *liveReply) framedText(answer string) string { + answer = normalizeTelegramText(answer) if len(l.toolLines) == 0 { return clampToOneMessage(answer) } @@ -561,7 +562,7 @@ func clampToRunes(s string, maxRunes int) string { // fact. An empty reply with no tool activity either is left to the // content == "" path in finalize, which abandons instead of sending. func (l *liveReply) finalText(reply string) string { - reply = strings.TrimSpace(reply) + reply = normalizeTelegramText(strings.TrimSpace(reply)) if len(l.toolLines) == 0 { return reply } @@ -583,8 +584,16 @@ func (l *liveReply) open(ctx context.Context, body string) { } msg, err := l.b.SendRichMessage(ctx, params) if err != nil { - l.g.log.Debug("live reply send failed", "error", err) - return + l.g.log.Debug("live reply rich send failed, retrying unformatted", "error", err) + plain := &bot.SendMessageParams{ChatID: l.chatID, Text: body} + if l.messageThreadID != 0 { + plain.MessageThreadID = l.messageThreadID + } + msg, err = l.b.SendMessage(ctx, plain) + if err != nil { + l.g.log.Debug("live reply send failed", "error", err) + return + } } l.mu.Lock() l.messageID = msg.ID diff --git a/internal/channels/telegram/live_test.go b/internal/channels/telegram/live_test.go index 97d27367c..217112ba0 100644 --- a/internal/channels/telegram/live_test.go +++ b/internal/channels/telegram/live_test.go @@ -92,6 +92,23 @@ func newTestLiveReply(t *testing.T, showToolCalls bool) (*liveReply, *[]apiCall) // The whole point of the canonical buffer: every update carries the complete // text so far, and it replaces the message rather than adding to it. +func TestLiveReplySeparatesURLFromFollowingStreamedProse(t *testing.T) { + live, calls := newTestLiveReply(t, false) + const url = "https://github.com/samcharles93/archie-core/issues/513" + + live.Delta(url) + live.flushRendering() + live.Delta("It includes model-independent safeguards...") + live.finalize(context.Background(), url+"It includes model-independent safeguards...") + + if got := (*calls)[len(*calls)-2].markdown; got != url+"\n\nIt includes model-independent safeguards... ▌" { + t.Fatalf("live render = %q, want URL and prose separated", got) + } + if got := (*calls)[len(*calls)-1].markdown; got != url+"\n\nIt includes model-independent safeguards..." { + t.Fatalf("final render = %q, want URL and prose separated", got) + } +} + func TestLiveReplySendsOnceThenEditsWithTheWholeBuffer(t *testing.T) { live, calls := newTestLiveReply(t, false) ctx := context.Background() @@ -334,6 +351,44 @@ func TestLiveReplyAbandonWithoutAMessageIsSilent(t *testing.T) { // Rich messages are a recent Bot API addition, and an edit that carries only // a rich body is rejected by a server that does not support it. The reply // must still arrive, unformatted, rather than being lost to a failed edit. +func TestLiveReplyOpenFallsBackToPlainText(t *testing.T) { + calls := &[]apiCall{} + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + call := capturedCall(t, r) + *calls = append(*calls, call) + w.Header().Set("Content-Type", "application/json") + if call.method == "sendRichMessage" && call.rich { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"ok":false,"error_code":400,"description":"Bad Request: rich_message is not supported"}`)) + return + } + _, _ = w.Write([]byte(`{"ok":true,"result":{"message_id":55,"date":1,"chat":{"id":7,"type":"private"}}}`)) + })) + t.Cleanup(api.Close) + b, err := bot.New("1:test", bot.WithServerURL(api.URL), bot.WithSkipGetMe()) + if err != nil { + t.Fatalf("new test bot: %v", err) + } + g := New("1:test", "", "", []int64{42}, slog.New(slog.DiscardHandler)) + live := g.newLiveReply(context.Background(), b, 7, 0, false) + t.Cleanup(live.stopRendering) + live.interval = 0 + + live.Delta("streamed") + live.flushRendering() + live.finalize(context.Background(), "the answer") + + if len(*calls) < 3 { + t.Fatalf("calls = %+v, want rich send, plain send fallback, and final edit", *calls) + } + if (*calls)[0].method != "sendRichMessage" || !(*calls)[0].rich { + t.Fatalf("first call = %+v, want rich send", (*calls)[0]) + } + if (*calls)[1].method != "sendMessage" || (*calls)[1].rich { + t.Fatalf("second call = %+v, want plain send fallback", (*calls)[1]) + } +} + func TestLiveReplyEditFallsBackToPlainText(t *testing.T) { calls := &[]apiCall{} api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/channels/telegram/telegram.go b/internal/channels/telegram/telegram.go index e48d7a4eb..d1219c00b 100644 --- a/internal/channels/telegram/telegram.go +++ b/internal/channels/telegram/telegram.go @@ -10,12 +10,14 @@ import ( "fmt" "log/slog" "net/http" + "regexp" "runtime/debug" "slices" "strings" "sync" "sync/atomic" "time" + "unicode" "unicode/utf8" "github.com/go-telegram/bot" @@ -694,8 +696,166 @@ func (g *Gateway) isSenderAllowed(userID int64) bool { // limit, with room for the continuation marker split adds. const messageMaxLen = 4000 +// bareGitHubIssueURL identifies the stable part of a GitHub issue or pull +// request URL. Telegram's URL detector ends a bare link at whitespace, so a +// streamed URL followed immediately by prose becomes one malformed link. We +// recognize the numeric issue/PR boundary rather than trying to parse every +// possible URL, whose path segments can legitimately contain letters. +var bareGitHubIssueURL = regexp.MustCompile(`(?i)https?://github\.com/[^\s<>()]+/(?:issues|pull)/[0-9]+`) + +// normalizeTelegramText preserves a paragraph boundary when a bare GitHub +// issue or pull-request URL is immediately followed by prose. This is +// deliberately idempotent because text passes through both the live renderer +// and the final send/split path. +func normalizeTelegramText(text string) string { + matches := bareGitHubIssueURL.FindAllStringIndex(text, -1) + if len(matches) == 0 { + return text + } + + var b strings.Builder + cursor := 0 + for _, match := range matches { + boundaryEnd, ok := telegramURLBoundary(text, match) + if !ok { + continue + } + b.WriteString(text[cursor:boundaryEnd]) + b.WriteString("\n\n") + cursor = boundaryEnd + } + if cursor == 0 { + return text + } + b.WriteString(text[cursor:]) + return b.String() +} + +// telegramURLBoundary returns the end of the URL or its surrounding Markdown +// delimiter when prose follows immediately. It refuses to edit a URL in a +// code span unless the closing delimiter is immediately after it, and avoids +// treating an alphanumeric URL suffix as prose. +func telegramURLBoundary(text string, match []int) (int, bool) { + start, end := match[0], match[1] + boundaryEnd := end + if delimiter, inside := markdownCodeDelimiterAt(text, start); inside { + if !strings.HasPrefix(text[end:], delimiter) { + return 0, false + } + boundaryEnd += len(delimiter) + } else { + // A URL inside Markdown link, autolink, or emphasis syntax is + // followed by a closing delimiter. Insert after that delimiter, + // never inside it, so the rich-message parser keeps the link intact. + boundaryEnd = markdownClosingDelimiterEnd(text, start, end) + } + if boundaryEnd >= len(text) { + return 0, false + } + next, _ := utf8.DecodeRuneInString(text[boundaryEnd:]) + if unicode.IsSpace(next) { + return 0, false + } + if !unicode.IsLetter(next) { + return 0, false + } + if unicode.IsUpper(next) { + return boundaryEnd, true + } + // Lower-case prose is valid too, but a path suffix such as "123abc" + // has no whitespace before it ends. A short look-ahead distinguishes the + // two without pretending to parse arbitrary URL grammars. + if slices.ContainsFunc([]rune(text[boundaryEnd:]), unicode.IsSpace) { + return boundaryEnd, true + } + return 0, false +} + +func markdownClosingDelimiterEnd(text string, start, end int) int { + if boundary, ok := markdownBracketEnd(text, start, end); ok { + return boundary + } + if boundary, ok := markdownEmphasisEnd(text, start, end); ok { + return boundary + } + return end +} + +func markdownBracketEnd(text string, start, end int) (int, bool) { + if end >= len(text) || start == 0 { + return 0, false + } + switch text[end] { + case ')': + if text[start-1] == '(' { + return end + 1, true + } + case '>': + if text[start-1] == '<' { + return end + 1, true + } + case ']': + if text[start-1] == '[' { + return end + 1, true + } + } + return 0, false +} + +func markdownEmphasisEnd(text string, start, end int) (int, bool) { + if end >= len(text) { + return 0, false + } + for _, delimiter := range []string{"***", "___", "**", "__", "~~"} { + if start >= len(delimiter) && + strings.HasPrefix(text[end:], delimiter) && + text[start-len(delimiter):start] == delimiter { + return end + len(delimiter), true + } + } + if start > 0 && ((text[end] == '*' && text[start-1] == '*') || + (text[end] == '_' && text[start-1] == '_')) { + return end + 1, true + } + return 0, false +} + +func markdownCodeDelimiterAt(text string, position int) (string, bool) { + var delimiter string + for i := 0; i < position; { + if text[i] == '\\' { + _, size := utf8.DecodeRuneInString(text[i:]) + i += size + if i < position { + _, size = utf8.DecodeRuneInString(text[i:]) + i += size + } + continue + } + if text[i] != '`' { + _, size := utf8.DecodeRuneInString(text[i:]) + i += size + continue + } + j := i + for j < position && text[j] == '`' { + j++ + } + run := text[i:j] + switch delimiter { + case "": + delimiter = run + case run: + delimiter = "" + } + i = j + } + return delimiter, delimiter != "" +} + func (g *Gateway) sendMessage(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, text string) { - if len(text) <= messageMaxLen { + text = normalizeTelegramText(text) + if utf8.RuneCountInString(text) <= messageMaxLen { g.send(ctx, b, chatID, messageThreadID, text, "send message failed") return } @@ -721,6 +881,7 @@ func (g *Gateway) sendMessage(ctx context.Context, b *bot.Bot, chatID int64, mes // treated as "unsupported" rather than fatal: fall back to a plain send so // the user still receives the reply, unformatted, instead of silence. func (g *Gateway) send(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, text, errMsg string) { + text = normalizeTelegramText(text) rich := &bot.SendRichMessageParams{ ChatID: chatID, RichMessage: models.InputRichMessage{Markdown: text}, @@ -744,45 +905,30 @@ func (g *Gateway) send(ctx context.Context, b *bot.Bot, chatID int64, messageThr } // splitLongMessage divides text into parts of at most maxLen runes each, -// preferring to cut on newlines. Bounds are counted in runes, not bytes, -// and an oversized line is itself cut on rune boundaries -- slicing by byte -// offset instead would, for any line containing multi-byte UTF-8 (CJK, -// emoji, ...), land a cut inside a rune's continuation bytes and produce a -// part that is not valid UTF-8 on either side of the cut. +// preferring to cut immediately after a newline. Bounds are counted in +// runes, not bytes, and every rune is retained, including blank lines and a +// trailing newline. Preserving those boundaries matters for both Markdown +// paragraphs and bare URLs that were separated from following prose. func splitLongMessage(text string, maxLen int) []string { - if utf8.RuneCountInString(text) <= maxLen { + text = normalizeTelegramText(text) + if maxLen <= 0 || utf8.RuneCountInString(text) <= maxLen { return []string{text} } - var parts []string - lines := strings.Split(text, "\n") - var cur strings.Builder - curRunes := 0 - for _, line := range lines { - lineRunes := utf8.RuneCountInString(line) - if curRunes+lineRunes+1 > maxLen { - if cur.Len() > 0 { - parts = append(parts, cur.String()) - cur.Reset() - curRunes = 0 - } - if lineRunes > maxLen { - runes := []rune(line) - for i := 0; i < len(runes); i += maxLen { - end := min(i+maxLen, len(runes)) - parts = append(parts, string(runes[i:end])) + + runes := []rune(text) + parts := make([]string, 0, (len(runes)+maxLen-1)/maxLen) + for start := 0; start < len(runes); { + end := min(start+maxLen, len(runes)) + if end < len(runes) { + for i := end - 1; i > start; i-- { + if runes[i] == '\n' { + end = i + break } - continue } } - if cur.Len() > 0 { - cur.WriteByte('\n') - curRunes++ - } - cur.WriteString(line) - curRunes += lineRunes - } - if cur.Len() > 0 { - parts = append(parts, cur.String()) + parts = append(parts, string(runes[start:end])) + start = end } return parts } diff --git a/internal/channels/telegram/telegram_test.go b/internal/channels/telegram/telegram_test.go index f2274c59e..53e038ce7 100644 --- a/internal/channels/telegram/telegram_test.go +++ b/internal/channels/telegram/telegram_test.go @@ -296,6 +296,108 @@ var ( // ── Regression tests for previously untested paths ──────────────────── +func TestSendMessageSeparatesBareURLFromFollowingProse(t *testing.T) { + b, requests := newTelegramTestBot(t) + g := New("1:test", "", "", []int64{42}, slog.Default()) + const url = "https://github.com/samcharles93/archie-core/issues/513" + g.sendMessage(context.Background(), b, 7, 0, url+"It includes model-independent safeguards...") + + if len(*requests) != 1 { + t.Fatalf("requests = %d, want one send", len(*requests)) + } + var rich models.InputRichMessage + if err := json.Unmarshal([]byte((*requests)[0].form["rich_message"]), &rich); err != nil { + t.Fatalf("decode rich message: %v", err) + } + want := url + "\n\nIt includes model-independent safeguards..." + if rich.Markdown != want { + t.Errorf("sent markdown = %q, want %q", rich.Markdown, want) + } +} + +func TestNormalizeTelegramTextSeparatesBareURLsFromFollowingProse(t *testing.T) { + const url = "https://github.com/samcharles93/archie-core/issues/513" + + tests := []struct { + name string + text string + want string + }{ + { + name: "github issue URL followed by prose", + text: url + "It includes model-independent safeguards...", + want: url + "\n\nIt includes model-independent safeguards...", + }, + { + name: "already separated URL is unchanged", + text: url + "\n\nIt includes model-independent safeguards...", + want: url + "\n\nIt includes model-independent safeguards...", + }, + { + name: "URL at end is unchanged", + text: "See " + url, + want: "See " + url, + }, + { + name: "markdown link keeps its closing delimiter", + text: "[issue](" + url + ")It includes model-independent safeguards...", + want: "[issue](" + url + ")\n\nIt includes model-independent safeguards...", + }, + { + name: "autolink keeps its closing delimiter", + text: "<" + url + ">It includes model-independent safeguards...", + want: "<" + url + ">\n\nIt includes model-independent safeguards...", + }, + { + name: "inline code keeps both delimiters", + text: "`" + url + "`It includes model-independent safeguards...", + want: "`" + url + "`\n\nIt includes model-independent safeguards...", + }, + { + name: "emphasis keeps its closing delimiter", + text: "**" + url + "**It includes model-independent safeguards...", + want: "**" + url + "**\n\nIt includes model-independent safeguards...", + }, + { + name: "single emphasis keeps its closing delimiter", + text: "*" + url + "*It includes model-independent safeguards...", + want: "*" + url + "*\n\nIt includes model-independent safeguards...", + }, + { + name: "underscore emphasis keeps its closing delimiter", + text: "_" + url + "_It includes model-independent safeguards...", + want: "_" + url + "_\n\nIt includes model-independent safeguards...", + }, + { + name: "triple emphasis keeps its closing delimiter", + text: "***" + url + "***It includes model-independent safeguards...", + want: "***" + url + "***\n\nIt includes model-independent safeguards...", + }, + { + name: "triple underscore emphasis keeps its closing delimiter", + text: "___" + url + "___It includes model-independent safeguards...", + want: "___" + url + "___\n\nIt includes model-independent safeguards...", + }, + { + name: "nested inline code remains inside a fenced code block", + text: "```\n`" + url + "`It includes model-independent safeguards...\n```", + want: "```\n`" + url + "`It includes model-independent safeguards...\n```", + }, + { + name: "numeric URL prefix is not split from its path suffix", + text: url + "abc", + want: url + "abc", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := normalizeTelegramText(tt.text); got != tt.want { + t.Errorf("normalizeTelegramText() = %q, want %q", got, tt.want) + } + }) + } +} + func TestSplitLongMessage(t *testing.T) { t.Run("empty string", func(t *testing.T) { parts := splitLongMessage("", 10) @@ -311,6 +413,47 @@ func TestSplitLongMessage(t *testing.T) { } }) + t.Run("keeps URL independent when splitting at a paragraph boundary", func(t *testing.T) { + const url = "https://github.com/samcharles93/archie-core/issues/513" + parts := splitLongMessage(url+"It includes model-independent safeguards...", utf8.RuneCountInString(url)+1) + if len(parts) != 2 { + t.Fatalf("parts = %q, want URL and prose in separate parts", parts) + } + if strings.TrimRight(parts[0], "\n") != url { + t.Errorf("URL part = %q, want %q with only a boundary newline", parts[0], url) + } + if parts[1] != "\n\nIt includes model-independent safeguards..." { + t.Errorf("prose part = %q, want the remaining paragraph boundary preserved", parts[1]) + } + }) + + t.Run("newline-only content is preserved", func(t *testing.T) { + text := strings.Repeat("\n", 17) + parts := splitLongMessage(text, 5) + var rebuilt strings.Builder + for _, part := range parts { + if part == "" { + t.Fatal("split produced an empty part") + } + rebuilt.WriteString(part) + } + if rebuilt.String() != text { + t.Errorf("rebuilt = %q, want %q", rebuilt.String(), text) + } + }) + + t.Run("trailing newline is preserved", func(t *testing.T) { + text := "aaaa\n" + parts := splitLongMessage(text, 4) + var rebuilt strings.Builder + for _, part := range parts { + rebuilt.WriteString(part) + } + if rebuilt.String() != text { + t.Errorf("rebuilt = %q, want %q", rebuilt.String(), text) + } + }) + t.Run("exactly at limit", func(t *testing.T) { s := strings.Repeat("x", 10) parts := splitLongMessage(s, 10)