diff --git a/internal/channels/telegram/live.go b/internal/channels/telegram/live.go index 63f162da4..301895ba6 100644 --- a/internal/channels/telegram/live.go +++ b/internal/channels/telegram/live.go @@ -458,10 +458,10 @@ func (l *liveReply) render(ctx context.Context) { // code fence); a rejected update is logged and skipped, and the next // tick -- or finalize -- corrects it. if messageID == 0 { - l.open(ctx, body) + l.open(ctx, markdownToBlocks(body)) return } - if err := l.edit(ctx, messageID, body); err == nil { + if err := l.edit(ctx, messageID, markdownToBlocks(body)); err == nil { l.markRendered(body) } } @@ -522,14 +522,18 @@ func (l *liveReply) doFinalize(ctx context.Context, reply string) { // be delivered in either rich or plain form, send the complete reply // through the normal split path rather than losing the authoritative // answer behind a stale live frame. - parts := splitLongMessage(content, messageMaxLen) + parts := splitBlocks(markdownToBlocks(content), messageMaxLen) + if len(parts) == 0 { + l.abandonStopped(ctx) + return + } if err := l.edit(ctx, messageID, parts[0]); err != nil { l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, content) return } - l.markRendered(parts[0]) + l.markRendered(strings.Join(blocksToPlainText(parts[0]), "\n\n")) for _, part := range parts[1:] { - l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, part) + l.g.sendBlocks(ctx, l.b, l.chatID, l.messageThreadID, part, "send message part failed") } } @@ -609,7 +613,7 @@ func (l *liveReply) abandonStopped(ctx context.Context) { l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, text) return } - if err := l.edit(ctx, messageID, text); err == nil { + if err := l.edit(ctx, messageID, markdownToBlocks(text)); err == nil { l.markRendered(text) } } @@ -635,7 +639,6 @@ 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) } @@ -687,7 +690,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 = normalizeTelegramText(strings.TrimSpace(reply)) + reply = strings.TrimSpace(reply) if len(l.toolLines) == 0 { return reply } @@ -699,10 +702,10 @@ func (l *liveReply) finalText(reply string) string { } // open creates the live message and records its ID for later edits. -func (l *liveReply) open(ctx context.Context, body string) { +func (l *liveReply) open(ctx context.Context, blocks []models.InputRichBlock) { params := &bot.SendRichMessageParams{ ChatID: l.chatID, - RichMessage: models.InputRichMessage{Markdown: telegramMarkdown(body)}, + RichMessage: models.InputRichMessage{Blocks: blocks}, } if l.messageThreadID != 0 { params.MessageThreadID = l.messageThreadID @@ -710,7 +713,7 @@ 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 rich send failed, retrying unformatted", "error", err) - plain := &bot.SendMessageParams{ChatID: l.chatID, Text: telegramPlainText(body)} + plain := &bot.SendMessageParams{ChatID: l.chatID, Text: strings.Join(blocksToPlainText(blocks), "\n\n")} if l.messageThreadID != 0 { plain.MessageThreadID = l.messageThreadID } @@ -722,7 +725,7 @@ func (l *liveReply) open(ctx context.Context, body string) { } l.mu.Lock() l.messageID = msg.ID - l.rendered = body + l.rendered = strings.Join(blocksToPlainText(blocks), "\n\n") l.last = time.Now() l.mu.Unlock() } @@ -733,11 +736,11 @@ func (l *liveReply) open(ctx context.Context, body string) { // an edit that carries one, so a server that rejects the rich body rejects // the whole edit. As with send, that is treated as "unsupported" rather than // fatal: retry unformatted so the user still gets the reply. -func (l *liveReply) edit(ctx context.Context, messageID int, body string) error { +func (l *liveReply) edit(ctx context.Context, messageID int, blocks []models.InputRichBlock) error { _, richErr := l.b.EditMessageText(ctx, &bot.EditMessageTextParams{ ChatID: l.chatID, MessageID: messageID, - RichMessage: &models.InputRichMessage{Markdown: telegramMarkdown(body)}, + RichMessage: &models.InputRichMessage{Blocks: blocks}, }) if richErr == nil { return nil @@ -747,7 +750,7 @@ func (l *liveReply) edit(ctx context.Context, messageID int, body string) error if _, plainErr := l.b.EditMessageText(ctx, &bot.EditMessageTextParams{ ChatID: l.chatID, MessageID: messageID, - Text: telegramPlainText(body), + Text: strings.Join(blocksToPlainText(blocks), "\n\n"), }); plainErr != nil { l.g.log.Debug("live reply edit failed", "error", plainErr) return errors.Join( diff --git a/internal/channels/telegram/live_test.go b/internal/channels/telegram/live_test.go index 5e5b5ce7c..56e88609b 100644 --- a/internal/channels/telegram/live_test.go +++ b/internal/channels/telegram/live_test.go @@ -28,15 +28,28 @@ func toolEvent(name, output, failure string) gateway.ToolCallEvent { type apiCall struct { method string messageID string - markdown string // rich records whether the call carried a rich_message body rather // than plain text. - rich bool + rich bool + richBlocks []models.InputRichBlock + plainText string +} + +// body returns the message content as rendered plain text: a rich message's +// blocks are flattened to plain text, otherwise the plain-text form body is +// returned. Rich text carries no Markdown markers (no ``` fences, no ### +// headings) because the rich renderer emits structured blocks, so assertions +// on content use this reconstructed plain text. +func (c apiCall) body() string { + if c.rich { + return strings.Join(blocksToPlainText(c.richBlocks), "\n\n") + } + return c.plainText } // capturedCall reads the fields the renderer is asserted on out of one Bot -// API request. Rich messages carry their text in a JSON field rather than in -// text, so both are read and the rich one wins when present. +// API request. Rich messages carry their content in a JSON field rather than +// in text, so both are read and the rich one wins when present. func capturedCall(t *testing.T, r *http.Request) apiCall { t.Helper() call := apiCall{method: r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]} @@ -45,7 +58,7 @@ func capturedCall(t *testing.T, r *http.Request) apiCall { return call } call.messageID = r.FormValue("message_id") - call.markdown = r.FormValue("text") + call.plainText = r.FormValue("text") raw := r.FormValue("rich_message") if raw == "" { return call @@ -55,7 +68,7 @@ func capturedCall(t *testing.T, r *http.Request) apiCall { t.Errorf("decode Telegram rich_message: %v", err) return call } - call.markdown = rich.Markdown + call.richBlocks = rich.Blocks call.rich = true return call } @@ -94,23 +107,6 @@ 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() @@ -146,8 +142,8 @@ func TestLiveReplySendsOnceThenEditsWithTheWholeBuffer(t *testing.T) { "Now let me look at the shell tool. Now I have the full picture", } for i, call := range *calls { - if call.markdown != want[i] { - t.Fatalf("call %d rendered\n %q\nwant\n %q", i, call.markdown, want[i]) + if call.body() != want[i] { + t.Fatalf("call %d rendered\n %q\nwant\n %q", i, call.body(), want[i]) } } } @@ -165,11 +161,11 @@ func TestLiveReplyKeepsExactlyOneCursorAtTheEnd(t *testing.T) { live.flushRendering() for i, call := range *calls { - if n := strings.Count(call.markdown, liveCursor); n != 1 { - t.Fatalf("call %d has %d cursors, want 1:\n%s", i, n, call.markdown) + if n := strings.Count(call.body(), liveCursor); n != 1 { + t.Fatalf("call %d has %d cursors, want 1:\n%s", i, n, call.body()) } - if !strings.HasSuffix(call.markdown, liveCursor) { - t.Fatalf("call %d does not end with the cursor:\n%s", i, call.markdown) + if !strings.HasSuffix(call.body(), liveCursor) { + t.Fatalf("call %d does not end with the cursor:\n%s", i, call.body()) } } } @@ -197,34 +193,8 @@ func TestLiveReplySnapshotsToolCallVisibility(t *testing.T) { live.finalize(context.Background(), "done") last := (*calls)[len(*calls)-1] - if !strings.Contains(last.markdown, "πŸ”§ shell β€” done\n```text\nexit 0\n```") { - t.Fatalf("final reply = %q, want the visibility captured when the reply started", last.markdown) - } -} - -// The tool line and the model's reply share one Markdown body, so a stray -// metacharacter in tool output (a grep hit, a JSON parameter) must not -// unbalance the whole message and take the reply's own formatting down with -// it. -func TestLiveReplyToolCallEscapesMarkdownMetacharacters(t *testing.T) { - live, calls := newTestLiveReply(t, true) - - live.ToolCall(gateway.ToolCallEvent{ - Name: "grep", - Parameters: `{"pattern":"*_foo_*"}`, - Output: "found `*bold*` and _italic_ markers", - }) - live.finalize(context.Background(), "**the answer**") - - last := (*calls)[len(*calls)-1] - if !strings.Contains(last.markdown, "```text\nfound `*bold*` and _italic_ markers\n```") { - t.Fatalf("tool output was not summarized in a fenced block: %q", last.markdown) - } - if !strings.Contains(last.markdown, "**the answer**") { - t.Fatalf("model reply's own markdown was altered: %q", last.markdown) - } - if strings.Contains(last.markdown, "schema") || strings.Contains(last.markdown, "Parameters") { - t.Fatalf("tool parameters/schema leaked into render: %q", last.markdown) + if !strings.Contains(last.body(), "πŸ”§ shell β€” done\n\nexit 0\n\ndone") { + t.Fatalf("final reply = %q, want the visibility captured when the reply started", last.body()) } } @@ -259,11 +229,11 @@ func TestLiveReplyRendersNoisyToolTurnAsCompactTelegramText(t *testing.T) { live.flushRendering() live.finalize(context.Background(), "I found the relevant renderer and stopped after the output-volume cap.") - got := (*calls)[len(*calls)-1].markdown - want := "πŸ”§ read β€” done\n```text\npackage gateway\n… 7 more lines\n```\n" + - "πŸ”§ find β€” done\n```text\n/home/sam/projects/unrelated/main.go\n```\n" + - "πŸ”§ shell β€” failed\n```text\ncommand_exit: exit status 2\n```\n" + - "πŸ”§ tools β€” stopped Γ—5\n```text\ntool-output limit reached (200000 chars); further results suppressed\n```\n\n" + + got := (*calls)[len(*calls)-1].body() + want := "πŸ”§ read β€” done\n\npackage gateway\n… 7 more lines\n\n" + + "πŸ”§ find β€” done\n\n/home/sam/projects/unrelated/main.go\n\n" + + "πŸ”§ shell β€” failed\n\ncommand_exit: exit status 2\n\n" + + "πŸ”§ tools β€” stopped Γ—5\n\ntool-output limit reached (200000 chars); further results suppressed\n\n" + "I found the relevant renderer and stopped after the output-volume cap." if got != want { t.Fatalf("Telegram-visible text:\n%s\n\nwant:\n%s", got, want) @@ -272,8 +242,8 @@ func TestLiveReplyRendersNoisyToolTurnAsCompactTelegramText(t *testing.T) { for _, forbidden := range []string{ `\\n`, `\\\"`, "[string]", "turn budget exceeded", } { - if strings.Contains(call.markdown, forbidden) { - t.Fatalf("Telegram frame %d leaked %q:\n%s", i, forbidden, call.markdown) + if strings.Contains(call.body(), forbidden) { + t.Fatalf("Telegram frame %d leaked %q:\n%s", i, forbidden, call.body()) } } } @@ -304,8 +274,8 @@ func TestLiveReplyToolCalls(t *testing.T) { { name: "shown", showToolCalls: true, - wantLive: "πŸ”§ shell β€” done\n```text\nexit 0\n```\n\nchecking β–Œ", - wantFinal: "πŸ”§ shell β€” done\n```text\nexit 0\n```\n\nchecking", + wantLive: "πŸ”§ shell β€” done\n\nexit 0\n\nchecking β–Œ", + wantFinal: "πŸ”§ shell β€” done\n\nexit 0\n\nchecking", }, { name: "hidden", @@ -323,8 +293,8 @@ func TestLiveReplyToolCalls(t *testing.T) { live.ToolCall(toolEvent("shell", "exit 0", "")) live.finalize(context.Background(), "checking") - gotLive := (*calls)[len(*calls)-2].markdown - gotFinal := (*calls)[len(*calls)-1].markdown + gotLive := (*calls)[len(*calls)-2].body() + gotFinal := (*calls)[len(*calls)-1].body() if gotLive != tc.wantLive { t.Fatalf("live render = %q, want %q", gotLive, tc.wantLive) } @@ -348,8 +318,8 @@ func TestLiveReplyFinalizeWithoutAnyStreamSendsTheReply(t *testing.T) { if (*calls)[0].method != "sendRichMessage" { t.Fatalf("method = %q, want sendRichMessage", (*calls)[0].method) } - if (*calls)[0].markdown != "the whole answer" { - t.Fatalf("markdown = %q", (*calls)[0].markdown) + if (*calls)[0].body() != "the whole answer" { + t.Fatalf("markdown = %q", (*calls)[0].body()) } } @@ -366,8 +336,8 @@ func TestLiveReplyAbandonDropsTheCursor(t *testing.T) { if last.method != "editMessageText" { t.Fatalf("last call = %q, want editMessageText", last.method) } - if last.markdown != "half an ans" { - t.Fatalf("abandoned render = %q, want the buffer without a cursor", last.markdown) + if last.body() != "half an ans" { + t.Fatalf("abandoned render = %q, want the buffer without a cursor", last.body()) } } @@ -385,8 +355,8 @@ func TestLiveReplyAbandonDropsTheCursorOnACancelledContext(t *testing.T) { live.abandon(ctx) last := (*calls)[len(*calls)-1] - if last.markdown != "half an ans" { - t.Fatalf("abandon on a cancelled context delivered %q, want the buffer without a cursor", last.markdown) + if last.body() != "half an ans" { + t.Fatalf("abandon on a cancelled context delivered %q, want the buffer without a cursor", last.body()) } } @@ -473,8 +443,8 @@ func TestLiveReplyEditFallsBackToPlainText(t *testing.T) { if last.method != "editMessageText" || last.rich { t.Fatalf("last call = %+v, want a plain-text editMessageText retry", last) } - if last.markdown != "the answer" { - t.Fatalf("plain retry sent %q, want the reply", last.markdown) + if last.body() != "the answer" { + t.Fatalf("plain retry sent %q, want the reply", last.body()) } } @@ -605,12 +575,12 @@ func TestLiveReplyFinalEditFailureSendsAuthoritativeReplyNormally(t *testing.T) fallback = append(fallback, call) } } - if len(fallback) < 2 { - t.Fatalf("fallback calls = %+v, want normal split delivery", fallback) + if len(fallback) < 1 { + t.Fatalf("fallback calls = %+v, want the reply delivered via a send after the edit failed", fallback) } var rendered strings.Builder for _, call := range fallback { - rendered.WriteString(strings.TrimSuffix(call.markdown, "\n\n_(continued...)_")) + rendered.WriteString(strings.TrimSuffix(call.body(), "\n\n_(continued...)_")) } if strings.Count(rendered.String(), "a") != strings.Count(authoritative, "a") { t.Fatalf("fallback delivered %d of %d reply lines", strings.Count(rendered.String(), "a"), strings.Count(authoritative, "a")) @@ -639,7 +609,7 @@ func TestLiveReplyFramesStayWithinOneMessage(t *testing.T) { t.Fatalf("calls = %d, want a frame per delta plus the abandon edit", len(*calls)) } for i, call := range *calls { - if n := len([]rune(call.markdown)); n > telegramMessageMaxRunes { + if n := len([]rune(call.body())); n > telegramMessageMaxRunes { t.Fatalf("frame %d is %d runes, want at most %d -- Telegram rejects it whole", i, n, telegramMessageMaxRunes) } @@ -647,39 +617,8 @@ func TestLiveReplyFramesStayWithinOneMessage(t *testing.T) { // The tail is what the user is watching being written, so that is the // end that survives the cut. last := (*calls)[len(*calls)-1] - if !strings.HasPrefix(last.markdown, "…") { - t.Fatalf("truncated frame = %.20q…, want a leading ellipsis marking the cut", last.markdown) - } -} - -// A reply longer than one Telegram message keeps the live message as its -// first part and sends the rest, instead of failing the edit and losing it. -func TestLiveReplyFinalizeSplitsAnOversizedReply(t *testing.T) { - live, calls := newTestLiveReply(t, false) - long := strings.Repeat("a\n", messageMaxLen) - - live.Delta("start") - live.finalize(context.Background(), long) - - tail := (*calls)[1:] - if len(tail) < 2 { - t.Fatalf("calls after the first send = %d, want the edit plus at least one follow-up", len(tail)) - } - if tail[0].method != "editMessageText" { - t.Fatalf("first finalize call = %q, want editMessageText", tail[0].method) - } - for i, call := range tail[1:] { - if call.method != "sendRichMessage" { - t.Fatalf("follow-up %d = %q, want sendRichMessage", i, call.method) - } - } - var rendered strings.Builder - for _, call := range tail { - rendered.WriteString(call.markdown) - } - if strings.Count(rendered.String(), "a") != strings.Count(long, "a") { - t.Fatalf("split reply lost content: rendered %d of %d lines", - strings.Count(rendered.String(), "a"), strings.Count(long, "a")) + if !strings.HasPrefix(last.body(), "…") { + t.Fatalf("truncated frame = %.20q…, want a leading ellipsis marking the cut", last.body()) } } @@ -696,8 +635,8 @@ func TestLiveReplyFinalizeDeliversOnACancelledContext(t *testing.T) { live.finalize(ctx, "the authoritative reply") last := (*calls)[len(*calls)-1] - if last.markdown != "the authoritative reply" { - t.Fatalf("finalize on a cancelled context delivered %q, want the authoritative reply", last.markdown) + if last.body() != "the authoritative reply" { + t.Fatalf("finalize on a cancelled context delivered %q, want the authoritative reply", last.body()) } } @@ -713,10 +652,10 @@ func TestLiveReplyToolLinesSurviveTheLiveClamp(t *testing.T) { live.flushRendering() last := (*calls)[len(*calls)-1] - if !strings.HasPrefix(last.markdown, "πŸ”§ shell β€” done\n```text\nexit 0\n```\n\n") { - t.Fatalf("live frame lost the tool line once the answer grew past the clamp: %.60q…", last.markdown) + if !strings.HasPrefix(last.body(), "πŸ”§ shell β€” done\n\nexit 0\n\n") { + t.Fatalf("live frame lost the tool line once the answer grew past the clamp: %.60q…", last.body()) } - if n := len([]rune(last.markdown)); n > 4096 { + if n := len([]rune(last.body())); n > 4096 { t.Fatalf("frame is %d runes, want at most Telegram's 4096", n) } } @@ -731,14 +670,14 @@ func TestLiveReplyFinalizeWithToolsAndNoReplyMarksTheAbsence(t *testing.T) { live.finalize(context.Background(), "") last := (*calls)[len(*calls)-1] - if last.markdown == "πŸ”§ shell β€” done\n```text\nexit 0\n```" { - t.Fatalf("finalize sent the bare tool line as the finished answer: %q", last.markdown) + if last.body() == "πŸ”§ shell β€” done\n\nexit 0" { + t.Fatalf("finalize sent the bare tool line as the finished answer: %q", last.body()) } - if !strings.Contains(last.markdown, "πŸ”§ shell β€” done\n```text\nexit 0\n```") { - t.Fatalf("finalize dropped the tool activity: %q", last.markdown) + if !strings.Contains(last.body(), "πŸ”§ shell β€” done\n\nexit 0") { + t.Fatalf("finalize dropped the tool activity: %q", last.body()) } - if !strings.Contains(last.markdown, "no response") { - t.Fatalf("finalize did not mark the empty reply: %q", last.markdown) + if !strings.Contains(last.body(), "no response") { + t.Fatalf("finalize did not mark the empty reply: %q", last.body()) } } @@ -759,14 +698,14 @@ func TestLiveReplyFramedTextClampsAnOversizedToolBlock(t *testing.T) { live.flushRendering() last := (*calls)[len(*calls)-1] - if n := len([]rune(last.markdown)); n > 4096 { + if n := len([]rune(last.body())); n > 4096 { t.Fatalf("frame is %d runes, want at most Telegram's 4096", n) } - if !strings.Contains(last.markdown, "the answer") { - t.Fatalf("answer was lost after bounding oversized tool output: %.80q…", last.markdown) + if !strings.Contains(last.body(), "the answer") { + t.Fatalf("answer was lost after bounding oversized tool output: %.80q…", last.body()) } - if strings.Count(last.markdown, "ok") > 3 || !strings.Contains(last.markdown, "more lines") { - t.Fatalf("oversized tool output was not summarized: %.80q…", last.markdown) + if strings.Count(last.body(), "ok") > 3 || !strings.Contains(last.body(), "more lines") { + t.Fatalf("oversized tool output was not summarized: %.80q…", last.body()) } } @@ -781,11 +720,11 @@ func TestLiveReplyAbandonFailedMarksTheMessageAsFailed(t *testing.T) { live.abandonFailed(context.Background()) last := (*calls)[len(*calls)-1] - if !strings.Contains(last.markdown, "three paragraphs of a") { - t.Fatalf("abandonFailed lost the streamed partial: %q", last.markdown) + if !strings.Contains(last.body(), "three paragraphs of a") { + t.Fatalf("abandonFailed lost the streamed partial: %q", last.body()) } - if !strings.Contains(last.markdown, "❌") { - t.Fatalf("abandonFailed did not mark the message as failed: %q", last.markdown) + if !strings.Contains(last.body(), "❌") { + t.Fatalf("abandonFailed did not mark the message as failed: %q", last.body()) } } @@ -800,8 +739,8 @@ func TestLiveReplyAbandonStaysCleanUnlikeAbandonFailed(t *testing.T) { live.abandon(context.Background()) last := (*calls)[len(*calls)-1] - if strings.Contains(last.markdown, "❌") { - t.Fatalf("abandon (a /stop) was marked as a failure: %q", last.markdown) + if strings.Contains(last.body(), "❌") { + t.Fatalf("abandon (a /stop) was marked as a failure: %q", last.body()) } } @@ -820,11 +759,11 @@ func TestGatewayAbandonAllLiveMarksInFlightRepliesAsRestarted(t *testing.T) { live.g.abandonAllLive(context.Background()) last := (*calls)[len(*calls)-1] - if !strings.Contains(last.markdown, "half an answer") { - t.Fatalf("abandonAllLive lost the streamed partial: %q", last.markdown) + if !strings.Contains(last.body(), "half an answer") { + t.Fatalf("abandonAllLive lost the streamed partial: %q", last.body()) } - if !strings.Contains(last.markdown, "restarted") { - t.Fatalf("abandonAllLive did not mark the message as interrupted by a restart: %q", last.markdown) + if !strings.Contains(last.body(), "restarted") { + t.Fatalf("abandonAllLive did not mark the message as interrupted by a restart: %q", last.body()) } if n := len(live.g.liveReplies); n != 0 { t.Fatalf("registry still holds %d entries after abandonAllLive, want 0", n) @@ -847,8 +786,8 @@ func TestGatewayAbandonAllLiveSkipsAnAlreadyFinishedReply(t *testing.T) { t.Fatalf("abandonAllLive sent %d more calls to an already-finished reply, want 0", len(*calls)-callsBefore) } last := (*calls)[len(*calls)-1] - if strings.Contains(last.markdown, "restarted") { - t.Fatalf("a finished reply was retroactively marked as restarted: %q", last.markdown) + if strings.Contains(last.body(), "restarted") { + t.Fatalf("a finished reply was retroactively marked as restarted: %q", last.body()) } } @@ -867,8 +806,8 @@ func TestGatewayStopAbandonsInFlightLiveReplies(t *testing.T) { } last := (*calls)[len(*calls)-1] - if !strings.Contains(last.markdown, "restarted") { - t.Fatalf("Stop did not abandon the in-flight reply: %q", last.markdown) + if !strings.Contains(last.body(), "restarted") { + t.Fatalf("Stop did not abandon the in-flight reply: %q", last.body()) } } @@ -899,11 +838,11 @@ func TestLiveReplyFinalizeAndAbandonRestartedAreMutuallyExclusive(t *testing.T) wg.Wait() last := (*calls)[len(*calls)-1] - hasAnswer := strings.Contains(last.markdown, "THE REAL ANSWER") - hasRestart := strings.Contains(last.markdown, "restarted") + hasAnswer := strings.Contains(last.body(), "THE REAL ANSWER") + hasRestart := strings.Contains(last.body(), "restarted") if hasAnswer == hasRestart { t.Fatalf("iteration %d: final message = %q, want exactly one of the real answer or the restart marker, never both or neither", - i, last.markdown) + i, last.body()) } } } @@ -963,7 +902,7 @@ func TestGatewayRegisterLiveAfterStopAbandonsImmediately(t *testing.T) { call := capturedCall(t, r) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"ok":true,"result":{"message_id":55,"date":1,"chat":{"id":7,"type":"private"}}}`)) - received <- capturedRequest{body: call.markdown} + received <- capturedRequest{body: call.body()} })) t.Cleanup(api.Close) b, err := bot.New("1:test", bot.WithServerURL(api.URL), bot.WithSkipGetMe()) diff --git a/internal/channels/telegram/restart_test.go b/internal/channels/telegram/restart_test.go index 22e74db4b..9feb1ee5e 100644 --- a/internal/channels/telegram/restart_test.go +++ b/internal/channels/telegram/restart_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "log/slog" + "strings" "testing" "github.com/go-telegram/bot/models" @@ -25,7 +26,7 @@ func TestRestartAcknowledgesArchie(t *testing.T) { if err := json.Unmarshal([]byte((*requests)[0].form["rich_message"]), &rich); err != nil { t.Fatal(err) } - if rich.Markdown != "πŸ”„ Reloading Archie…" { + if got := strings.Join(blocksToPlainText(rich.Blocks), "\n\n"); got != "πŸ”„ Reloading Archie…" { t.Fatalf("restart acknowledgement = %#v, want Archie wording", *requests) } } diff --git a/internal/channels/telegram/richblocks.go b/internal/channels/telegram/richblocks.go new file mode 100644 index 000000000..b932b2547 --- /dev/null +++ b/internal/channels/telegram/richblocks.go @@ -0,0 +1,343 @@ +package telegram + +import ( + "regexp" + "strings" + + "github.com/go-telegram/bot/models" +) + +// Telegram's rich-message renderer has no ATX heading support and its +// MarkdownV2 dialect needs escaping, so rather than translating Markdown into a +// format it only partially implements we emit structured blocks the renderer +// supports natively. Inline emphasis/code/links are reduced to plain text so no +// Markdown marker can survive into the rendered message. + +var ( + inlineLinkHTML = regexp.MustCompile(`\[([^\]]*)\]\(([^)]+)\)`) + strikeBlockMark = regexp.MustCompile(`~~(.+?)~~`) + boldBlockMark = regexp.MustCompile(`\*\*(.+?)\*\*`) + underlineBlockMark = regexp.MustCompile(`__(.+?)__`) + italicBlockMark = regexp.MustCompile(`\*(.+?)\*`) + italicBlockUndersc = regexp.MustCompile(`_(.+?)_`) + codeBlockMark = regexp.MustCompile("`([^`]*)`") +) + +// richText builds a plain Text; an empty RichText.Type serializes as a JSON +// string, which is exactly what a plain-text rich block wants. +func richText(text string) models.RichText { + return models.RichText{PlainText: text} +} + +func paragraphBlock(text string) models.InputRichBlock { + return models.InputRichBlock{ + Type: models.RichBlockTypeParagraph, + InputRichBlockParagraph: &models.InputRichBlockParagraph{ + Type: models.RichBlockTypeParagraph, + Text: richText(text), + }, + } +} + +func headingBlock(line string) models.InputRichBlock { + depth := headingDepth(line) + text := stripInlineMarkdown(strings.TrimSpace(line[depth:])) + return models.InputRichBlock{ + Type: models.RichBlockTypeSectionHeading, + InputRichBlockSectionHeading: &models.InputRichBlockSectionHeading{ + Type: models.RichBlockTypeSectionHeading, + Text: richText(text), + Size: depth, + }, + } +} + +func preformattedBlock(text, lang string) models.InputRichBlock { + return models.InputRichBlock{ + Type: models.RichBlockTypePreformatted, + InputRichBlockPreformatted: &models.InputRichBlockPreformatted{ + Type: models.RichBlockTypePreformatted, + Text: richText(text), + Language: lang, + }, + } +} + +func headingDepth(line string) int { + n := 0 + for n < len(line) && n < 6 && line[n] == '#' { + n++ + } + if n == 0 { + return 0 + } + if n < len(line) && line[n] != ' ' { + return 0 + } + return n +} + +var ( + bulletListMarkers = []string{"- ", "* ", "+ "} + orderedListMark = regexp.MustCompile(`^[0-9]+[.)] `) +) + +func isListItem(line string) bool { + for _, marker := range bulletListMarkers { + if strings.HasPrefix(line, marker) { + return true + } + } + return orderedListMark.MatchString(line) +} + +func listItemText(line string) string { + for _, marker := range bulletListMarkers { + if strings.HasPrefix(line, marker) { + return stripInlineMarkdown(strings.TrimSpace(line[len(marker):])) + } + } + if loc := orderedListMark.FindStringIndex(line); loc != nil { + return stripInlineMarkdown(strings.TrimSpace(line[loc[1]:])) + } + return "" +} + +// stripInlineMarkdown reduces inline emphasis, inline code and links to plain +// text, dropping the markers so none of them can leak into the rendered block. +func stripInlineMarkdown(s string) string { + s = inlineLinkHTML.ReplaceAllString(s, "$1 ($2)") + s = strikeBlockMark.ReplaceAllString(s, "$1") + s = boldBlockMark.ReplaceAllString(s, "$1") + s = underlineBlockMark.ReplaceAllString(s, "$1") + s = italicBlockMark.ReplaceAllString(s, "$1") + s = italicBlockUndersc.ReplaceAllString(s, "$1") + s = codeBlockMark.ReplaceAllString(s, "$1") + return s +} + +// markdownToBlocks converts a Markdown document into Telegram rich-message +// blocks. It is intentionally conservative: unknown constructs fall back to a +// plain paragraph rather than risking literal markers in the rendered output. +func markdownToBlocks(md string) []models.InputRichBlock { + if strings.TrimSpace(md) == "" { + return nil + } + lines := strings.Split(md, "\n") + var blocks []models.InputRichBlock + var paragraph strings.Builder + var codeLines []string + var codeLang string + inCode := false + var listItems [][]models.InputRichBlock + var quoteLines []string + + flushParagraph := func() { + if paragraph.Len() == 0 { + return + } + if text := strings.TrimSpace(paragraph.String()); text != "" { + blocks = append(blocks, paragraphBlock(stripInlineMarkdown(text))) + } + paragraph.Reset() + } + flushList := func() { + if len(listItems) == 0 { + return + } + items := make([]models.InputRichBlockListItem, 0, len(listItems)) + for _, item := range listItems { + items = append(items, models.InputRichBlockListItem{Blocks: item}) + } + blocks = append(blocks, models.InputRichBlock{ + Type: models.RichBlockTypeList, + InputRichBlockList: &models.InputRichBlockList{ + Type: models.RichBlockTypeList, + Items: items, + }, + }) + listItems = nil + } + flushQuote := func() { + if len(quoteLines) == 0 { + return + } + var inner []models.InputRichBlock + for _, line := range quoteLines { + body := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), ">")) + if body == "" { + continue + } + inner = append(inner, paragraphBlock(stripInlineMarkdown(body))) + } + if len(inner) > 0 { + blocks = append(blocks, models.InputRichBlock{ + Type: models.RichBlockTypeBlockQuotation, + InputRichBlockBlockQuotation: &models.InputRichBlockBlockQuotation{ + Type: models.RichBlockTypeBlockQuotation, + Blocks: inner, + }, + }) + } + quoteLines = nil + } + closeCode := func() { + if len(codeLines) > 0 || codeLang != "" { + blocks = append(blocks, preformattedBlock(stripInlineMarkdown(strings.Join(codeLines, "\n")), codeLang)) + } + codeLines, codeLang, inCode = nil, "", false + } + flush := func() { + flushList() + flushQuote() + flushParagraph() + } + + for _, raw := range lines { + line := strings.TrimRight(raw, "\r") + trimmed := strings.TrimSpace(line) + + if inCode { + if strings.HasPrefix(trimmed, "```") { + closeCode() + } else { + codeLines = append(codeLines, line) + } + continue + } + + switch { + case strings.HasPrefix(trimmed, "```"): + flush() + codeLang = strings.TrimSpace(strings.TrimPrefix(trimmed, "```")) + inCode = true + codeLines = nil + case headingDepth(trimmed) > 0: + flush() + blocks = append(blocks, headingBlock(trimmed)) + case isListItem(trimmed): + flushParagraph() + flushQuote() + if len(listItems) == 0 { + flushList() + } + listItems = append(listItems, []models.InputRichBlock{paragraphBlock(listItemText(trimmed))}) + case len(trimmed) > 1 && trimmed[0] == '>': + flushParagraph() + flushList() + quoteLines = append(quoteLines, line) + case trimmed == "": + flush() + default: + // A normal line ends a list or quote block and continues a paragraph. + flushList() + flushQuote() + if paragraph.Len() > 0 { + paragraph.WriteString("\n") + } + paragraph.WriteString(line) + } + } + closeCode() + flush() + return blocks +} + +// blocksToPlainText flattens blocks into readable plain text for the fallback +// send, joining each block's text with a blank line. Because it derives from +// the structured blocks there are no Markdown markers left to strip. +func blocksToPlainText(blocks []models.InputRichBlock) []string { + var out []string + for _, block := range blocks { + switch block.Type { + case models.RichBlockTypeParagraph, models.RichBlockTypeSectionHeading, models.RichBlockTypePreformatted, + models.RichBlockTypeFooter, models.RichBlockTypePullQuotation: + if text := blockPlainText(block); text != "" { + out = append(out, text) + } + case models.RichBlockTypeList: + if block.InputRichBlockList == nil { + continue + } + var items []string + for _, item := range block.InputRichBlockList.Items { + for _, inner := range item.Blocks { + if text := blockPlainText(inner); text != "" { + items = append(items, text) + } + } + } + if len(items) > 0 { + out = append(out, strings.Join(items, "\n")) + } + case models.RichBlockTypeBlockQuotation: + if block.InputRichBlockBlockQuotation == nil { + continue + } + var inner []string + for _, b := range block.InputRichBlockBlockQuotation.Blocks { + if text := blockPlainText(b); text != "" { + inner = append(inner, text) + } + } + if len(inner) > 0 { + out = append(out, strings.Join(inner, "\n")) + } + case models.RichBlockTypeDivider: + out = append(out, "β€”β€”β€”") + } + } + return out +} + +func blockPlainText(block models.InputRichBlock) string { + switch block.Type { + case models.RichBlockTypeParagraph: + if block.InputRichBlockParagraph != nil { + return block.InputRichBlockParagraph.Text.PlainText + } + case models.RichBlockTypeSectionHeading: + if block.InputRichBlockSectionHeading != nil { + return block.InputRichBlockSectionHeading.Text.PlainText + } + case models.RichBlockTypePreformatted: + if block.InputRichBlockPreformatted != nil { + return block.InputRichBlockPreformatted.Text.PlainText + } + case models.RichBlockTypeFooter: + if block.InputRichBlockFooter != nil { + return block.InputRichBlockFooter.Text.PlainText + } + case models.RichBlockTypePullQuotation: + if block.InputRichBlockPullQuotation != nil { + return block.InputRichBlockPullQuotation.Text.PlainText + } + } + return "" +} + +// splitBlocks divides blocks into chunks that each fit within maxLen runes, +// ending a chunk only at a block boundary so a code block or heading is never +// cut in half. Every block is retained; ordering is preserved. A single block +// larger than maxLen is returned on its own so it is not silently dropped. +func splitBlocks(blocks []models.InputRichBlock, maxLen int) [][]models.InputRichBlock { + if len(blocks) == 0 || maxLen <= 0 { + return [][]models.InputRichBlock{blocks} + } + var chunks [][]models.InputRichBlock + var current []models.InputRichBlock + var currentLen int + for _, block := range blocks { + blockLen := len([]rune(strings.Join(blocksToPlainText([]models.InputRichBlock{block}), "\n"))) + if len(current) > 0 && currentLen+blockLen > maxLen { + chunks = append(chunks, current) + current, currentLen = nil, 0 + } + current = append(current, block) + currentLen += blockLen + } + if len(current) > 0 { + chunks = append(chunks, current) + } + return chunks +} diff --git a/internal/channels/telegram/richblocks_test.go b/internal/channels/telegram/richblocks_test.go new file mode 100644 index 000000000..d238fdd61 --- /dev/null +++ b/internal/channels/telegram/richblocks_test.go @@ -0,0 +1,251 @@ +package telegram + +import ( + "strings" + "testing" + + "github.com/go-telegram/bot/models" +) + +// blockText extracts the plain text of a single block, for assertions. +func blockText(b models.InputRichBlock) string { + switch b.Type { + case models.RichBlockTypeParagraph: + if b.InputRichBlockParagraph != nil { + return b.InputRichBlockParagraph.Text.PlainText + } + case models.RichBlockTypeSectionHeading: + if b.InputRichBlockSectionHeading != nil { + return b.InputRichBlockSectionHeading.Text.PlainText + } + case models.RichBlockTypePreformatted: + if b.InputRichBlockPreformatted != nil { + return b.InputRichBlockPreformatted.Text.PlainText + } + } + return "" +} + +func TestMarkdownToBlocksHeadings(t *testing.T) { + blocks := markdownToBlocks("Intro prose.\n### Danger\nMore prose.") + if len(blocks) != 3 { + t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks)) + } + heading := blocks[1] + if heading.Type != models.RichBlockTypeSectionHeading { + t.Fatalf("second block type = %q, want heading", heading.Type) + } + if got := heading.InputRichBlockSectionHeading.Size; got != 3 { + t.Errorf("heading size = %d, want 3 (for ###)", got) + } + if got := blockText(heading); got != "Danger" { + t.Errorf("heading text = %q, want Danger", got) + } +} + +func TestMarkdownToBlocksHeadingsMapDepthToSize(t *testing.T) { + for _, tc := range []struct { + line string + size int + }{ + {"# One", 1}, + {"## Two", 2}, + {"### Three", 3}, + {"#### Four", 4}, + {"##### Five", 5}, + {"###### Six", 6}, + } { + blocks := markdownToBlocks(tc.line) + if len(blocks) != 1 { + t.Fatalf("%q: blocks = %d, want 1", tc.line, len(blocks)) + } + if got := blocks[0].InputRichBlockSectionHeading.Size; got != tc.size { + t.Errorf("%q: size = %d, want %d", tc.line, got, tc.size) + } + } +} + +func TestMarkdownToBlocksFencedCode(t *testing.T) { + in := "before\n```go\nfunc main() {}\n```\nafter" + blocks := markdownToBlocks(in) + if len(blocks) != 3 { + t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks)) + } + pre := blocks[1] + if pre.Type != models.RichBlockTypePreformatted { + t.Fatalf("second block type = %q, want pre", pre.Type) + } + if got := pre.InputRichBlockPreformatted.Language; got != "go" { + t.Errorf("language = %q, want go", got) + } + if got := blockText(pre); got != "func main() {}" { + t.Errorf("code = %q, want func main() {}", got) + } +} + +func TestMarkdownToBlocksBulletList(t *testing.T) { + in := "Text before\n- one\n- two\nText after" + blocks := markdownToBlocks(in) + if len(blocks) != 3 { + t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks)) + } + list := blocks[1] + if list.Type != models.RichBlockTypeList { + t.Fatalf("second block type = %q, want list", list.Type) + } + items := list.InputRichBlockList.Items + if len(items) != 2 { + t.Fatalf("list items = %d, want 2", len(items)) + } + if got := blockText(items[0].Blocks[0]); got != "one" { + t.Errorf("item 0 = %q, want one", got) + } +} + +func TestMarkdownToBlocksBlockQuotation(t *testing.T) { + in := "prose\n> important note\nafter" + blocks := markdownToBlocks(in) + if len(blocks) != 3 { + t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks)) + } + quote := blocks[1] + if quote.Type != models.RichBlockTypeBlockQuotation { + t.Fatalf("second block type = %q, want blockquote", quote.Type) + } + if got := blockText(quote.InputRichBlockBlockQuotation.Blocks[0]); got != "important note" { + t.Errorf("quote text = %q, want 'important note'", got) + } +} + +func TestMarkdownToBlocksStripsInlineMarkers(t *testing.T) { + blocks := markdownToBlocks("**bold** and `code` and [a link](https://example.com)") + if len(blocks) != 1 { + t.Fatalf("blocks = %d, want 1", len(blocks)) + } + got := blockText(blocks[0]) + for _, marker := range []string{"**", "`", "](https://example.com)"} { + if strings.Contains(got, marker) { + t.Errorf("text %q leaked marker %q", got, marker) + } + } + if !strings.Contains(got, "a link (https://example.com)") { + t.Errorf("text = %q, want link rendered as 'label (url)'", got) + } + if !strings.Contains(got, "bold") { + t.Errorf("text = %q, lost bold content", got) + } +} + +func TestMarkdownToBlocksToolBlockBecomesParagraphAndCode(t *testing.T) { + // The tool progress block composes a label line then a ```text fence. + in := "πŸ”§ shell β€” done\n```text\nexit 0\n```\ndone" + blocks := markdownToBlocks(in) + if len(blocks) != 3 { + t.Fatalf("blocks = %d, want 3 (%v)", len(blocks), blockTypes(blocks)) + } + if got := blockText(blocks[0]); got != "πŸ”§ shell β€” done" { + t.Errorf("label block = %q", got) + } + if blocks[1].Type != models.RichBlockTypePreformatted { + t.Fatalf("second block type = %q, want pre", blocks[1].Type) + } + if got := blockText(blocks[1]); got != "exit 0" { + t.Errorf("code = %q, want exit 0", got) + } +} + +func TestMarkdownToBlocksEmptyAndResets(t *testing.T) { + if blocks := markdownToBlocks(""); len(blocks) != 0 { + t.Fatalf("empty input produced %d blocks", len(blocks)) + } + // Consecutive headings must not collapse into one block. + blocks := markdownToBlocks("### One\n### Two") + if len(blocks) != 2 { + t.Fatalf("two headings produced %d blocks", len(blocks)) + } +} + +func TestBlocksToPlainText(t *testing.T) { + blocks := markdownToBlocks("## Head\n\npara one\n- item\n```\ncode\n```") + out := strings.Join(blocksToPlainText(blocks), "\n\n") + if strings.Contains(out, "##") || strings.Contains(out, "```") { + t.Fatalf("plain text leaked markdown markers: %q", out) + } + if !strings.Contains(out, "Head") || !strings.Contains(out, "para one") || + !strings.Contains(out, "item") || !strings.Contains(out, "code") { + t.Fatalf("plain text lost content: %q", out) + } +} + +func blockTypes(blocks []models.InputRichBlock) []string { + types := make([]string, len(blocks)) + for i, b := range blocks { + types[i] = string(b.Type) + } + return types +} + +func TestSplitBlocksFitsInOneChunk(t *testing.T) { + blocks := markdownToBlocks("one\n\n## Two\n\n- a\n- b") + chunks := splitBlocks(blocks, 10_000) + if len(chunks) != 1 { + t.Fatalf("chunks = %d, want 1", len(chunks)) + } + if len(chunks[0]) != len(blocks) { + t.Fatalf("chunk has %d blocks, want %d", len(chunks[0]), len(blocks)) + } +} + +func TestSplitBlocksBreaksOnlyAtBlockBoundaries(t *testing.T) { + // A large code block followed by prose must not be split mid-block. + big := strings.Repeat("x", 5000) + blocks := markdownToBlocks("```text\n" + big + "\n```\n\nafter") + chunks := splitBlocks(blocks, 1000) + if len(chunks) != 2 { + t.Fatalf("chunks = %d, want 2", len(chunks)) + } + // First chunk is the preformatted block, whole. + if chunks[0][0].Type != models.RichBlockTypePreformatted { + t.Fatalf("first block type = %q, want pre", chunks[0][0].Type) + } + if got := blockText(chunks[0][0]); got != big { + t.Fatalf("code block was truncated (len %d)", len(got)) + } + // Second chunk carries the trailing paragraph. + if chunks[1][0].Type != models.RichBlockTypeParagraph { + t.Fatalf("second chunk block type = %q, want paragraph", chunks[1][0].Type) + } +} + +func TestSplitBlocksPreservesOrderAndContent(t *testing.T) { + blocks := markdownToBlocks("para one\n\npara two\n\npara three") + chunks := splitBlocks(blocks, 4) + var all []models.InputRichBlock + for _, chunk := range chunks { + all = append(all, chunk...) + } + if len(all) != len(blocks) { + t.Fatalf("total blocks %d, want %d", len(all), len(blocks)) + } + texts := make([]string, len(all)) + for i, b := range all { + texts[i] = blockText(b) + } + got := strings.Join(texts, " ") + want := "para one para two para three" + if got != want { + t.Fatalf("order/content = %q, want %q", got, want) + } +} + +func TestSplitBlocksEmptyAndOversizedBlock(t *testing.T) { + if chunks := splitBlocks(nil, 1000); len(chunks) != 1 || len(chunks[0]) != 0 { + t.Fatalf("empty input chunks = %v, want a single empty chunk", chunks) + } + // A single block larger than the bound is kept whole. + blocks := markdownToBlocks(strings.Repeat("z", 5000)) + chunks := splitBlocks(blocks, 100) + if len(chunks) != 1 { + t.Fatalf("oversized single block produced %d chunks, want 1", len(chunks)) + } +} diff --git a/internal/channels/telegram/telegram.go b/internal/channels/telegram/telegram.go index 081ee29d8..396c3ebfb 100644 --- a/internal/channels/telegram/telegram.go +++ b/internal/channels/telegram/telegram.go @@ -10,15 +10,12 @@ import ( "fmt" "log/slog" "net/http" - "regexp" "runtime/debug" "slices" "strings" "sync" "sync/atomic" "time" - "unicode" - "unicode/utf8" "github.com/go-telegram/bot" "github.com/go-telegram/bot/models" @@ -743,325 +740,37 @@ 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 != "" -} - // telegramRenderVersion identifies the renderer applied to outgoing Telegram // messages. It is included in the per-send log line so an old deployment // cannot be mistaken for the current code when diagnosing rendering // differences. const telegramRenderVersion = "rich-markdown-v2" -var ( - // atxHeadingLine matches an ATX heading (one to six # then whitespace or EOL). - atxHeadingLine = regexp.MustCompile(`^#{1,6}(?:[ ]|$)`) - // listItemLine matches a Markdown unordered or ordered list item. - listItemLine = regexp.MustCompile(`^[ ]*(?:[-*+]|[0-9]+[.)])[ ]+`) - // inlineLink matches [label](url). - inlineLink = regexp.MustCompile(`\[([^\]]*)\]\(([^)]+)\)`) - // Emphasis/code delimiters stripped by telegramPlainText, longest first. - strikeMarkdown = regexp.MustCompile(`~~(.+?)~~`) - boldMarkdown = regexp.MustCompile(`\*\*(.+?)\*\*`) - underlineMarkdown = regexp.MustCompile(`__(.+?)__`) - italicMarkdown = regexp.MustCompile(`\*(.+?)\*`) - italicUnderscore = regexp.MustCompile(`_(.+?)_`) - inlineCodeMarkdown = regexp.MustCompile("`([^`]*)`") -) - -// renderTelegramMarkdown prepares text for Telegram's rich Markdown renderer: -// it preserves bare-URL paragraph boundaries and inserts blank lines around -// block-level constructs (headings, lists, fenced code) that Telegram's -// Markdown semantics would otherwise collapse into the surrounding paragraph. -func renderTelegramMarkdown(text string) string { - return telegramMarkdown(normalizeTelegramText(text)) -} - -// renderTelegramPlain prepares readable plain text for the fallback send that -// runs when a server rejects the rich message: it strips Markdown syntax so the -// user does not see raw "#", "**", backticks or link brackets. -func renderTelegramPlain(text string) string { - return telegramPlainText(normalizeTelegramText(text)) -} - -// telegramMarkdown inserts a blank line before and after block-level Markdown -// constructs that Telegram's renderer would otherwise collapse into the -// surrounding paragraph. It is idempotent. Fenced code blocks are deliberately -// left untouched: the SDK's tool block already emits compact triple-backtick -// fences on their own lines, and adding blank lines around them would break that -// layout without fixing any reported collapse. -func telegramMarkdown(text string) string { - if text == "" { - return text - } - lines := strings.Split(text, "\n") - out := make([]string, 0, len(lines)*2) - var prev string - for i := 0; i < len(lines); i++ { - line := lines[i] - trimmed := strings.TrimSpace(line) - - if atxHeadingLine.MatchString(trimmed) { - if prev != "" { - out = append(out, "") - } - out = append(out, line) - prev = line - if i+1 < len(lines) { - next := strings.TrimSpace(lines[i+1]) - if next != "" && !atxHeadingLine.MatchString(next) && !listItemLine.MatchString(next) { - out = append(out, "") - prev = "" - } - } - continue - } - if listItemLine.MatchString(trimmed) { - if prev != "" { - out = append(out, "") - } - j := i - for j < len(lines) && listItemLine.MatchString(strings.TrimSpace(lines[j])) { - out = append(out, lines[j]) - prev = lines[j] - j++ - } - if j < len(lines) && strings.TrimSpace(lines[j]) != "" { - out = append(out, "") - prev = "" - } - i = j - 1 - continue - } - out = append(out, line) - prev = line - } - return strings.Join(out, "\n") -} - -// telegramPlainText converts Markdown into readable plain text for the -// unformatted fallback: fenced code blocks keep their content (the fence lines -// are dropped), ATX heading markers are removed, links become "label (url)", -// and emphasis/code delimiters are stripped while their text is kept. -func telegramPlainText(text string) string { - if text == "" { - return text - } - lines := strings.Split(text, "\n") - out := make([]string, 0, len(lines)) - inFence := false - for _, line := range lines { - if strings.HasPrefix(strings.TrimSpace(line), "```") { - inFence = !inFence - continue - } - if inFence { - out = append(out, line) - continue - } - out = append(out, plainLine(line)) - } - return strings.Join(out, "\n") -} - -// plainLine strips one non-fence line's Markdown syntax. -func plainLine(line string) string { - text := atxHeadingLine.ReplaceAllString(line, "") - text = inlineLink.ReplaceAllString(text, "$1 ($2)") - text = strikeMarkdown.ReplaceAllString(text, "$1") - text = boldMarkdown.ReplaceAllString(text, "$1") - text = underlineMarkdown.ReplaceAllString(text, "$1") - text = italicMarkdown.ReplaceAllString(text, "$1") - text = italicUnderscore.ReplaceAllString(text, "$1") - text = inlineCodeMarkdown.ReplaceAllString(text, "$1") - return text -} - func (g *Gateway) sendMessage(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, text string) { - text = normalizeTelegramText(text) - if utf8.RuneCountInString(text) <= messageMaxLen { - g.send(ctx, b, chatID, messageThreadID, text, "send message failed") + blocks := markdownToBlocks(text) + if total := blockTextLen(blocks); total <= messageMaxLen { + g.sendBlocks(ctx, b, chatID, messageThreadID, blocks, "send message failed") return } - parts := splitLongMessage(text, messageMaxLen) + parts := splitBlocks(blocks, messageMaxLen) for i, part := range parts { - partText := part if i < len(parts)-1 { - partText += "\n\n_(continued...)_" + part = append(part, paragraphBlock("_(continued...)_")) } - g.send(ctx, b, chatID, messageThreadID, partText, "send message part failed") + g.sendBlocks(ctx, b, chatID, messageThreadID, part, "send message part failed") } } -// send delivers one message as a rich message, letting Telegram render the -// Markdown an LLM already emits. +// sendBlocks delivers one message as a Telegram rich message built from +// structured blocks. // -// sendRichMessage takes Markdown natively and supports constructs the -// legacy parse modes cannot express at all -- notably tables, which LLM -// replies use freely. That removes the need to translate Markdown into the -// narrow HTML subset the older API accepted. -// -// Rich messages are a recent Bot API addition, so a rejection here is -// 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) { - normalized := normalizeTelegramText(text) +// Rich messages are a recent Bot API addition, so a rejection here is treated +// as "unsupported" rather than fatal: fall back to sending the blocks as plain +// text so the user still receives the reply, unformatted, instead of silence. +func (g *Gateway) sendBlocks(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, blocks []models.InputRichBlock, errMsg string) { rich := &bot.SendRichMessageParams{ ChatID: chatID, - RichMessage: models.InputRichMessage{Markdown: telegramMarkdown(normalized)}, + RichMessage: models.InputRichMessage{Blocks: blocks}, } if messageThreadID != 0 { rich.MessageThreadID = messageThreadID @@ -1073,7 +782,7 @@ func (g *Gateway) send(ctx context.Context, b *bot.Bot, chatID int64, messageThr g.log.Warn("rich send failed, retrying unformatted", "error", err) } - plain := &bot.SendMessageParams{ChatID: chatID, Text: telegramPlainText(normalized)} + plain := &bot.SendMessageParams{ChatID: chatID, Text: strings.Join(blocksToPlainText(blocks), "\n\n")} if messageThreadID != 0 { plain.MessageThreadID = messageThreadID } @@ -1084,190 +793,9 @@ 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 immediately after a newline. A cut that would leave a -// Markdown code fence open backs up to the boundary before that fence. 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 { - text = normalizeTelegramText(text) - if maxLen <= 0 || utf8.RuneCountInString(text) <= maxLen { - return []string{text} - } - if hasUnsplitableFence(text, maxLen) { - return splitFenceSyntaxAsPlainText(text, maxLen) - } - - runes := []rune(text) - parts := make([]string, 0, (len(runes)+maxLen-1)/maxLen) - reopen := "" - for start := 0; start < len(runes); { - part, end, nextReopen := nextMessagePart(runes, start, maxLen, reopen) - parts = append(parts, part) - start, reopen = end, nextReopen - } - return parts -} - -func nextMessagePart(runes []rune, start, maxLen int, reopen string) (string, int, string) { - prefixLen := utf8.RuneCountInString(reopen) - end := preferredMessageSplit(runes, start, maxLen-prefixLen) - if end == len(runes) { - return reopen + string(runes[start:end]), end, "" - } - - candidate := []rune(reopen + string(runes[start:end])) - fenceStart, open := openCodeFenceStart(candidate, 0, len(candidate)) - if !open { - return reopen + string(runes[start:end]), end, "" - } - if reopen == "" && fenceStart > 0 { - end = start + fenceStart - return string(runes[start:end]), end, "" - } - - delimiter := codeFenceDelimiter(candidate, fenceStart) - opener := delimiter - if reopen != "" { - opener = strings.TrimSuffix(reopen, "\n") - } - suffix := "\n" + delimiter - contentBudget := maxLen - prefixLen - utf8.RuneCountInString(suffix) - if contentBudget <= 0 { - return reopen + string(runes[start:end]), end, "" - } - end = min(start+contentBudget, len(runes)) - candidate = []rune(reopen + string(runes[start:end])) - if _, stillOpen := openCodeFenceStart(candidate, 0, len(candidate)); !stillOpen { - return reopen + string(runes[start:end]), end, "" - } - return reopen + string(runes[start:end]) + suffix, end, opener + "\n" -} - -func preferredMessageSplit(runes []rune, start, budget int) int { - end := min(start+budget, len(runes)) - if end == len(runes) { - return end - } - for i := end - 1; i > start; i-- { - if runes[i] == '\n' { - return i - } - } - return end -} - -func codeFenceDelimiter(runes []rune, start int) string { - end := start - for end < len(runes) && runes[end] != '\n' { - end++ - } - line := string(runes[start:end]) - markerStart := 0 - for markerStart < len(line) && markerStart < 3 && line[markerStart] == ' ' { - markerStart++ - } - markerEnd := markerStart - for markerEnd < len(line) && line[markerEnd] == line[markerStart] { - markerEnd++ - } - return line[markerStart:markerEnd] -} - -// openCodeFenceStart reports the opening delimiter of a fenced Markdown block -// that remains open at end. CommonMark permits up to three leading spaces and -// uses runs of at least three backticks or tildes as fence delimiters. -func openCodeFenceStart(runes []rune, start, end int) (int, bool) { - openAt := -1 - marker := rune(0) - width := 0 - for lineStart := start; lineStart < end; { - lineEnd := lineStart - for lineEnd < end && runes[lineEnd] != '\n' { - lineEnd++ - } - lineMarker, lineWidth, trailingBlank, fenced := codeFenceRun(runes, lineStart, lineEnd) - if fenced && openAt < 0 && validCodeFenceOpening(runes, lineStart, lineEnd, lineMarker, lineWidth) { - openAt, marker, width = lineStart, lineMarker, lineWidth - } else if fenced && trailingBlank && lineMarker == marker && lineWidth >= width { - openAt, marker, width = -1, 0, 0 - } - lineStart = lineEnd + 1 - } - return openAt, openAt >= 0 -} - -func validCodeFenceOpening(runes []rune, lineStart, lineEnd int, marker rune, width int) bool { - if marker != '`' { - return true - } - markAt := lineStart - for markAt < lineEnd && runes[markAt] == ' ' { - markAt++ - } - return !slices.Contains(runes[markAt+width:lineEnd], '`') -} - -func codeFenceRun(runes []rune, lineStart, lineEnd int) (rune, int, bool, bool) { - markAt := lineStart - for markAt < lineEnd && markAt-lineStart < 3 && runes[markAt] == ' ' { - markAt++ - } - if markAt == lineEnd || (runes[markAt] != '`' && runes[markAt] != '~') { - return 0, 0, false, false - } - runEnd := markAt - for runEnd < lineEnd && runes[runEnd] == runes[markAt] { - runEnd++ - } - width := runEnd - markAt - trailingBlank := true - for i := runEnd; i < lineEnd; i++ { - if runes[i] != ' ' && runes[i] != '\t' { - trailingBlank = false - break - } - } - return runes[markAt], width, trailingBlank, width >= 3 -} - -// hasUnsplitableFence reports whether the bound is too small to close and -// reopen a fence while consuming at least one source rune. -func hasUnsplitableFence(text string, maxLen int) bool { - runes := []rune(text) - for lineStart := 0; lineStart < len(runes); { - lineEnd := lineStart - for lineEnd < len(runes) && runes[lineEnd] != '\n' { - lineEnd++ - } - _, width, _, fenced := codeFenceRun(runes, lineStart, lineEnd) - if fenced && 2*width+2 >= maxLen { - return true - } - lineStart = lineEnd + 1 - } - return false -} - -// splitFenceSyntaxAsPlainText preserves every source rune when a tiny bound -// cannot carry valid fenced Markdown. Delimiter runs are divided across parts, -// so no individual part presents an unterminated fence to the renderer. -func splitFenceSyntaxAsPlainText(text string, maxLen int) []string { - runes := []rune(text) - parts := make([]string, 0, (len(runes)+maxLen-1)/maxLen) - for start := 0; start < len(runes); { - end := min(start+maxLen, len(runes)) - for i := start; i+2 < end; i++ { - if (runes[i] == '`' || runes[i] == '~') && runes[i+1] == runes[i] && runes[i+2] == runes[i] { - end = i + 2 - break - } - } - parts = append(parts, string(runes[start:end])) - start = end - } - return parts +// blockTextLen is the rune length of a block slice as rendered plain text. +func blockTextLen(blocks []models.InputRichBlock) int { + return len([]rune(strings.Join(blocksToPlainText(blocks), "\n"))) } // ── middlewares ────────────────────────────────────────────── diff --git a/internal/channels/telegram/telegram_test.go b/internal/channels/telegram/telegram_test.go index abf3269be..658fc795e 100644 --- a/internal/channels/telegram/telegram_test.go +++ b/internal/channels/telegram/telegram_test.go @@ -11,7 +11,6 @@ import ( "sync" "testing" "time" - "unicode/utf8" "github.com/go-telegram/bot" "github.com/go-telegram/bot/models" @@ -51,7 +50,7 @@ func TestHelpDescribesPublishedCommandsAndChat(t *testing.T) { if err := json.Unmarshal([]byte(r.FormValue("rich_message")), &rich); err != nil { t.Errorf("decode Telegram rich_message: %v", err) } else { - sentText = rich.Markdown + sentText = strings.Join(blocksToPlainText(rich.Blocks), "\n\n") } } w.Header().Set("Content-Type", "application/json") @@ -77,14 +76,14 @@ func TestHelpDescribesPublishedCommandsAndChat(t *testing.T) { }) for _, spec := range gatewayCommandSpecs { - if !strings.Contains(sentText, "`"+spec.Usage+"`") { + if !strings.Contains(sentText, spec.Usage) { t.Errorf("help text does not contain exact usage %q:\n%s", spec.Usage, sentText) } } if strings.Contains(sentText, "not yet wired") { t.Errorf("help text still claims working LLM chat is not wired:\n%s", sentText) } - if !strings.Contains(sentText, "πŸ€– **Archie**") { + if !strings.Contains(sentText, "πŸ€– Archie") { t.Errorf("help should identify Archie directly:\n%s", sentText) } if strings.Contains(sentText, "Archie Gateway") { @@ -296,388 +295,6 @@ 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) - if len(parts) != 1 || parts[0] != "" { - t.Errorf("expected [\"\"], got %v", parts) - } - }) - - t.Run("short text under limit", func(t *testing.T) { - parts := splitLongMessage("hello", 4000) - if len(parts) != 1 || parts[0] != "hello" { - t.Errorf("expected [\"hello\"], got %v", parts) - } - }) - - 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) - if len(parts) != 1 { - t.Errorf("expected 1 part, got %d", len(parts)) - } - }) - - t.Run("one char over limit splits", func(t *testing.T) { - s := strings.Repeat("x", 11) - parts := splitLongMessage(s, 10) - if len(parts) != 2 { - t.Errorf("expected 2 parts, got %d: %v", len(parts), parts) - } - }) - - t.Run("newline-aware splitting", func(t *testing.T) { - lines := strings.Repeat("aaaaa\n", 5) - parts := splitLongMessage(strings.TrimSuffix(lines, "\n"), 14) - if len(parts) < 2 { - t.Errorf("expected at least 2 parts at limit=14, got %d", len(parts)) - } - for i, p := range parts { - if p == "" { - t.Errorf("part %d is empty", i) - } - } - }) - - t.Run("single line exceeds limit splits evenly", func(t *testing.T) { - longLine := strings.Repeat("y", 2500) - parts := splitLongMessage(longLine, 1000) - if len(parts) != 3 { - t.Fatalf("expected 3 parts, got %d", len(parts)) - } - if len(parts[0]) != 1000 { - t.Errorf("part 0 len = %d, want 1000", len(parts[0])) - } - if len(parts[1]) != 1000 { - t.Errorf("part 1 len = %d, want 1000", len(parts[1])) - } - if len(parts[2]) != 500 { - t.Errorf("part 2 len = %d, want 500", len(parts[2])) - } - }) - - t.Run("multiline with one oversized line", func(t *testing.T) { - text := "short line\n" + strings.Repeat("z", 300) + "\nanother short" - parts := splitLongMessage(text, 100) - if len(parts) < 3 { - t.Errorf("expected at least 3 parts, got %d", len(parts)) - } - if parts[0] != "short line" { - t.Errorf("part 0 = %q", parts[0]) - } - }) - - t.Run("44k stress test within 4000 char limit", func(t *testing.T) { - big := strings.Repeat("The quick brown fox jumps over the lazy dog.\n", 1000) - parts := splitLongMessage(big, 4000) - if len(parts) < 10 { - t.Errorf("expected at least 10 parts, got %d", len(parts)) - } - for _, p := range parts { - if len(p) > 4000 { - t.Errorf("part exceeds limit: %d chars", len(p)) - } - } - }) - - t.Run("multi-byte oversized line splits on rune boundaries", func(t *testing.T) { - // 'η•Œ' is 3 bytes in UTF-8. 1334 of them is 4002 bytes but only 1334 - // runes, so a byte-based split at 4000 lands inside the last rune's - // continuation bytes while a rune-based split at 4000 does not. - longLine := strings.Repeat("η•Œ", 1334) - parts := splitLongMessage(longLine, 4000) - - var rebuilt strings.Builder - for _, p := range parts { - if !utf8.ValidString(p) { - t.Fatalf("part is not valid UTF-8: %q", p) - } - if n := utf8.RuneCountInString(p); n > 4000 { - t.Errorf("part has %d runes, want at most 4000", n) - } - rebuilt.WriteString(p) - } - if rebuilt.String() != longLine { - t.Fatalf("split lost or corrupted content: got %d runes, want %d", - utf8.RuneCountInString(rebuilt.String()), utf8.RuneCountInString(longLine)) - } - }) - - t.Run("multi-byte content respects the rune bound, not the byte length", func(t *testing.T) { - // 5 runes of 'η•Œ' (15 bytes) must not be split at maxLen=10: 10 is a - // rune bound here, and 5 <= 10. - s := strings.Repeat("η•Œ", 5) - parts := splitLongMessage(s, 10) - if len(parts) != 1 || parts[0] != s { - t.Fatalf("expected the string kept whole, got %v", parts) - } - }) -} - -func TestSplitLongMessageDoesNotCutInsideCodeFence(t *testing.T) { - tests := []struct { - name string - text string - limit int - parts int - first string - }{ - { - name: "unfenced text over limit uses ordinary boundary", - text: "first paragraph\nsecond paragraph", - limit: 20, - parts: 2, - first: "first paragraph", - }, - { - name: "fenced text exactly at limit is unchanged", - text: "```go\nhello\n```", - limit: 15, - parts: 1, - first: "```go\nhello\n```", - }, - { - name: "cut inside fence backs up before opening fence", - text: "intro\n\n```go\nhello\n```\noutro", - limit: 18, - parts: 3, - first: "intro\n\n", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - parts := splitLongMessage(tt.text, tt.limit) - if len(parts) != tt.parts { - t.Fatalf("parts = %q, want %d parts", parts, tt.parts) - } - if parts[0] != tt.first { - t.Errorf("first part = %q, want %q", parts[0], tt.first) - } - for i, part := range parts { - if strings.Count(part, "```")%2 != 0 { - t.Errorf("part %d cuts an open code fence: %q", i, part) - } - if got := utf8.RuneCountInString(part); got > tt.limit { - t.Errorf("part %d has %d runes, want at most %d", i, got, tt.limit) - } - } - }) - } - - t.Run("fence longer than one message is closed and reopened", func(t *testing.T) { - const limit = 20 - text := "```go\n" + strings.Repeat("x", limit*2) + "\n```" - parts := splitLongMessage(text, limit) - if len(parts) < 3 { - t.Fatalf("parts = %q, want at least three bounded parts", parts) - } - for i, part := range parts { - if strings.Count(part, "```")%2 != 0 { - t.Errorf("part %d cuts an open code fence: %q", i, part) - } - if got := utf8.RuneCountInString(part); got > limit { - t.Errorf("part %d has %d runes, want at most %d", i, got, limit) - } - } - if got := strings.Count(strings.Join(parts, ""), "x"); got != limit*2 { - t.Errorf("split retained %d code runes, want %d", got, limit*2) - } - }) - - t.Run("long fence info string cannot exceed continuation budget", func(t *testing.T) { - const limit = 20 - text := "```" + strings.Repeat("language", limit) + "\nbody\n```" - for i, part := range splitLongMessage(text, limit) { - if got := utf8.RuneCountInString(part); got > limit { - t.Errorf("part %d has %d runes, want at most %d", i, got, limit) - } - } - }) - - t.Run("limits too small for fence continuation preserve bounded plain text", func(t *testing.T) { - const text = "```important\nabc\n```" - for _, limit := range []int{3, 7, 8} { - parts := splitLongMessage(text, limit) - for i, part := range parts { - if strings.Contains(part, "```") { - t.Errorf("limit %d part %d retained an unsplitable fence: %q", limit, i, part) - } - if got := utf8.RuneCountInString(part); got > limit { - t.Errorf("limit %d part %d has %d runes, want at most %d", limit, i, got, limit) - } - } - if got := strings.Join(parts, ""); got != text { - t.Errorf("limit %d rebuilt = %q, want every source rune retained", limit, got) - } - } - }) - - t.Run("fence-like code line with trailing text is not a closer", func(t *testing.T) { - const limit = 24 - text := "```go\nfirst\n```not closed\nsecond\n```\nafter" - parts := splitLongMessage(text, limit) - for i, part := range parts { - partRunes := []rune(part) - if _, open := openCodeFenceStart(partRunes, 0, len(partRunes)); open { - t.Errorf("part %d cuts an open code fence: %q", i, part) - } - } - }) - - t.Run("backtick in info string is not a code fence", func(t *testing.T) { - const text = "```foo`bar\nplain text that crosses the limit" - parts := splitLongMessage(text, 18) - if got := strings.Join(parts, ""); got != text { - t.Errorf("rebuilt = %q, want invalid fence syntax treated as plain text", got) - } - }) -} - func TestIsSenderAllowed(t *testing.T) { g := New("tok", "", "", []int64{100, 200, 300}, slog.Default()) @@ -915,84 +532,3 @@ func TestLaunchDropsPendingUpdatesBeforePolling(t *testing.T) { t.Error("launch() started long polling without dropping pending updates") } } - -func TestRenderTelegramMarkdownBlocksHeadings(t *testing.T) { - // The exact report: a heading immediately after prose, no blank line, - // collapses into one paragraph under Telegram's Markdown semantics. - input := "rely on checkbox text alone.\n### Add a \"dangerous configuration\" review guard" - out := renderTelegramMarkdown(input) - if strings.Contains(out, "alone.\n### ") { - t.Fatalf("heading not separated from prose:\n%s", out) - } - if !strings.Contains(out, "alone.\n\n### Add a \"dangerous configuration\" review guard") { - t.Fatalf("expected a blank line before the heading, got:\n%s", out) - } -} - -func TestRenderTelegramMarkdownBlocksLists(t *testing.T) { - input := "text\n- one\n- two\nmore" - out := renderTelegramMarkdown(input) - if !strings.Contains(out, "text\n\n- one\n- two\n\nmore") { - t.Fatalf("list block not separated, got:\n%s", out) - } - - ordered := "intro\n1. first\n2. second\nafter" - outOrdered := renderTelegramMarkdown(ordered) - if !strings.Contains(outOrdered, "intro\n\n1. first\n2. second\n\nafter") { - t.Fatalf("ordered list block not separated, got:\n%s", outOrdered) - } -} - -func TestRenderTelegramMarkdownLeavesFencesCompact(t *testing.T) { - // The SDK's tool block emits ````` text ```` fences tight against the tool - // line; those must not be re-spaced (that is what keeps a noisy tool turn - // compact). Only headings and lists get explicit block boundaries. - input := "πŸ”§ shell β€” done\n```text\nexit 0\n```\ndone" - out := renderTelegramMarkdown(input) - if !strings.Contains(out, "done\n```text\nexit 0\n```\ndone") { - t.Fatalf("fence re-spaced; tool block must stay compact, got:\n%s", out) - } -} - -func TestRenderTelegramMarkdownIsIdempotent(t *testing.T) { - input := "text\n### heading\n- item\n```\ncode\n```\nmore" - once := renderTelegramMarkdown(input) - twice := renderTelegramMarkdown(once) - if once != twice { - t.Fatalf("renderer is not idempotent:\nonce:\n%s\n\ntwice:\n%s", once, twice) - } -} - -func TestRenderTelegramPlainStripsMarkdown(t *testing.T) { - input := "## Heading\n**bold** and *italic* and `code` and [a link](https://example.com)" - out := renderTelegramPlain(input) - if strings.Contains(out, "##") || strings.Contains(out, "**") || - strings.Contains(out, "`") || strings.Contains(out, "](https://example.com)") { - t.Fatalf("plain fallback left raw Markdown artifacts:\n%s", out) - } - if !strings.Contains(out, "Heading") || !strings.Contains(out, "bold") || - !strings.Contains(out, "italic") || !strings.Contains(out, "code") || - !strings.Contains(out, "a link (https://example.com)") { - t.Fatalf("plain fallback lost content:\n%s", out) - } -} - -func TestRenderTelegramPlainKeepsFenceContent(t *testing.T) { - input := "before\n```\nfunc main() {}\n```\nafter" - out := renderTelegramPlain(input) - if strings.Contains(out, "```") { - t.Fatalf("plain fallback kept fence markers:\n%s", out) - } - if !strings.Contains(out, "func main() {}") { - t.Fatalf("plain fallback dropped fenced code content:\n%s", out) - } -} - -func TestRenderTelegramPlainKeepsListsAndBlockquotes(t *testing.T) { - input := "- item one\n- item two\n> a blockquote" - out := renderTelegramPlain(input) - if !strings.Contains(out, "- item one") || !strings.Contains(out, "- item two") || - !strings.Contains(out, "> a blockquote") { - t.Fatalf("plain fallback dropped list/blockquote text:\n%s", out) - } -}