fix(telegram): render tool progress as bounded code blocks

This commit is contained in:
sam committed 2026-08-23 15:34:38 +00:00
1 parent 167e8f3891
commit fe7246be33
4 files changed
+72 -24

No files matched your search

+4 -8
View File
@@ -27,9 +27,6 @@ const (
// exactly one cursor and it is always at the current end.
liveCursor = "▌"
// toolCallPrefix opens an inline tool-activity line.
toolCallPrefix = "🔧 "
// fallbackMediaLinePrefix opens the line Media appends when it could
// not deliver an attachment inline, so the user still gets the asset.
fallbackMediaLinePrefix = "📎 "
@@ -320,11 +317,10 @@ func (l *liveReply) ToolCall(event gateway.ToolCallEvent) {
if !l.showToolCalls || event.Name == "" {
return
}
line := toolCallPrefix + bot.EscapeMarkdown(event.Name)
if event.Parameters != "" {
line += " " + bot.EscapeMarkdown(event.Parameters)
}
line += " — " + bot.EscapeMarkdown(event.Summary())
// Render the completed result as a compact fenced block. Do not append the
// raw/JSON-shaped parameters: they are noisy, can expose secrets, and were
// the source of unreadable schema placeholders in Telegram.
line := event.RenderToolCall()
l.mu.Lock()
l.toolLines = append(l.toolLines, line)
+16 -16
View File
@@ -195,8 +195,8 @@ func TestLiveReplySnapshotsToolCallVisibility(t *testing.T) {
live.finalize(context.Background(), "done")
last := (*calls)[len(*calls)-1]
if !strings.Contains(last.markdown, `🔧 shell \{"cmd":"true"\} — exit 0`) {
t.Fatalf("final reply = %q, want the visibility captured when the reply started (with Markdown metacharacters escaped)", last.markdown)
if !strings.Contains(last.markdown, "🔧 shell\n```text\nexit 0\n```") {
t.Fatalf("final reply = %q, want the visibility captured when the reply started", last.markdown)
}
}
@@ -215,14 +215,14 @@ func TestLiveReplyToolCallEscapesMarkdownMetacharacters(t *testing.T) {
live.finalize(context.Background(), "**the answer**")
last := (*calls)[len(*calls)-1]
if strings.Contains(last.markdown, "found `*bold*` and _italic_ markers") {
t.Fatalf("tool summary rendered unescaped: %q", last.markdown)
if !strings.Contains(last.markdown, "```text\nfound `*bold*` and _italic_ markers\n```") {
t.Fatalf("tool output was not rendered 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, "found \\`\\*bold\\*\\` and \\_italic\\_ markers") {
t.Fatalf("tool summary not escaped as expected: %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)
}
}
@@ -251,8 +251,8 @@ func TestLiveReplyToolCalls(t *testing.T) {
{
name: "shown",
showToolCalls: true,
wantLive: "🔧 shell — exit 0\n\nchecking ▌",
wantFinal: "🔧 shell — exit 0\n\nchecking",
wantLive: "🔧 shell\n```text\nexit 0\n```\n\nchecking ▌",
wantFinal: "🔧 shell\n```text\nexit 0\n```\n\nchecking",
},
{
name: "hidden",
@@ -660,7 +660,7 @@ func TestLiveReplyToolLinesSurviveTheLiveClamp(t *testing.T) {
live.flushRendering()
last := (*calls)[len(*calls)-1]
if !strings.HasPrefix(last.markdown, "🔧 shell — exit 0\n\n") {
if !strings.HasPrefix(last.markdown, "🔧 shell\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 n := len([]rune(last.markdown)); n > 4096 {
@@ -678,10 +678,10 @@ func TestLiveReplyFinalizeWithToolsAndNoReplyMarksTheAbsence(t *testing.T) {
live.finalize(context.Background(), "")
last := (*calls)[len(*calls)-1]
if last.markdown == "🔧 shell — exit 0" {
if last.markdown == "🔧 shell\n```text\nexit 0\n```" {
t.Fatalf("finalize sent the bare tool line as the finished answer: %q", last.markdown)
}
if !strings.Contains(last.markdown, "🔧 shell — exit 0") {
if !strings.Contains(last.markdown, "🔧 shell\n```text\nexit 0\n```") {
t.Fatalf("finalize dropped the tool activity: %q", last.markdown)
}
if !strings.Contains(last.markdown, "no response") {
@@ -700,7 +700,7 @@ func TestLiveReplyFramedTextClampsAnOversizedToolBlock(t *testing.T) {
live.ToolCall(gateway.ToolCallEvent{
Name: "shell",
Parameters: strings.Repeat("x", liveBodyMaxRunes*2),
Output: "ok",
Output: strings.Repeat("ok\n", liveBodyMaxRunes*2),
})
live.Delta("the answer")
live.flushRendering()
@@ -709,11 +709,11 @@ func TestLiveReplyFramedTextClampsAnOversizedToolBlock(t *testing.T) {
if n := len([]rune(last.markdown)); 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 text survived an oversized tool block instead of the tool block alone being clamped: %.80q…", last.markdown)
if !strings.Contains(last.markdown, "the answer") {
t.Fatalf("answer was lost after bounding oversized tool output: %.80q…", last.markdown)
}
if !strings.HasPrefix(last.markdown, "…") {
t.Fatalf("oversized tool block was not clamped with the leading ellipsis: %.20q…", last.markdown)
if !strings.Contains(last.markdown, "…") {
t.Fatalf("oversized tool output was not marked as bounded: %.80q…", last.markdown)
}
}
+21
View File
@@ -46,6 +46,27 @@ func (e ToolCallEvent) Summary() string {
return "done"
}
// RenderToolCall returns the channel-neutral compact representation used by
// chat surfaces. Parameters are intentionally omitted: JSON/schema-shaped
// inputs are noisy, often contain secrets, and are not useful progress text.
// The bounded fenced block keeps multiline command output readable without
// allowing one result to bury the answer or exceed Telegram's message limit.
func (e ToolCallEvent) RenderToolCall() string {
header := "🔧 " + strings.TrimSpace(e.Name)
if line := firstNonEmptyLine(e.Err); line != "" {
return header + " — ❌ " + truncateRunes(line, toolSummaryMaxRunes)
}
if strings.TrimSpace(e.Output) == "" {
return header + " — done"
}
const maxOutputRunes = 480
output := strings.TrimSpace(e.Output)
output = truncateRunes(output, maxOutputRunes)
// A nested fence would terminate the block early in Markdown renderers.
output = strings.ReplaceAll(output, "```", "''' ")
return header + "\n```text\n" + output + "\n```"
}
// SummarizeToolParameters returns a bounded JSON shape of tool input. Every
// string value is replaced rather than echoed because credentials can appear
// inside ordinary parameters such as command, content, or body. Values under
+31
View File
@@ -0,0 +1,31 @@
package gateway
import (
"strings"
"testing"
)
func TestToolCallEventRenderToolCallUsesCompactSafeOutput(t *testing.T) {
got := (ToolCallEvent{Name: "shell", Parameters: `{"command":"{schema placeholder}"}`, Output: "line one\nline two"}).RenderToolCall()
if strings.Contains(got, "schema placeholder") || strings.Contains(got, "Parameters") {
t.Fatalf("render leaked input/schema noise: %q", got)
}
want := "🔧 shell\n```text\nline one\nline two\n```"
if got != want {
t.Fatalf("render = %q, want %q", got, want)
}
}
func TestToolCallEventRenderToolCallBoundsOutputAndPreservesFailure(t *testing.T) {
got := (ToolCallEvent{Name: "read", Output: strings.Repeat("x", 2000)}).RenderToolCall()
if len([]rune(got)) > 520 {
t.Fatalf("render has %d runes, want bounded output", len([]rune(got)))
}
if !strings.Contains(got, "…") {
t.Fatal("bounded output lacks truncation marker")
}
failed := (ToolCallEvent{Name: "shell", Output: "ignored", Err: "command_exit: exit status 2"}).RenderToolCall()
if failed != "🔧 shell — ❌ command_exit: exit status 2" {
t.Fatalf("failure render = %q", failed)
}
}