Skip to content

Commit 9f56093

Browse files
fix(telegram): preserve tool progress under preambles (#108394)
* fix(telegram): keep tool rows below progress preambles * fix(telegram): preserve text tool progress under preambles * fix(telegram): preserve memory progress under preambles --------- Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 4b61b02 commit 9f56093

7 files changed

Lines changed: 248 additions & 36 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
d79413170fd0a7a9601c36231fe72c691248cf667e02c80232756e0baad44549 plugin-sdk-api-baseline.json
2-
3b0b2398c5c26b6605c7e7b851c77073137b6a2655c2355cca6340f3acc38278 plugin-sdk-api-baseline.jsonl
1+
bc217b6821feb0891c64e0aaa9453e57cdbe4b17ce831fd9a3fdb7423d255424 plugin-sdk-api-baseline.json
2+
4033a75ff63c730a4d2340b555d3eafecd6a168e350ebe2a902fc470bf687ee9 plugin-sdk-api-baseline.jsonl

‎extensions/telegram/src/bot-message-dispatch-progress.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ function buildTelegramThinkingProgressLine(progressTokens: number): ChannelProgr
4040
};
4141
}
4242

43+
function buildTelegramTextToolProgressLine(text: string): ChannelProgressDraftLine {
44+
return {
45+
kind: "item",
46+
label: "",
47+
text,
48+
prefix: false,
49+
};
50+
}
51+
4352
export function createTelegramProgressController(params: {
4453
accountId: string;
4554
chatId: TelegramMessageContext["chatId"];
@@ -73,6 +82,7 @@ export function createTelegramProgressController(params: {
7382
reasoningLinePrefix: "🧠 ",
7483
commentaryLinePrefix: "💬 ",
7584
commentaryItalics: false,
85+
updateOnLineChange: true,
7686
update: async (streamText, options) => {
7787
draftEverRendered = true;
7888
await params.draft.prepareAnswerLaneForToolProgress();
@@ -113,7 +123,10 @@ export function createTelegramProgressController(params: {
113123
if (!canPushToolProgress()) {
114124
return false;
115125
}
116-
return await compositor.pushToolProgress(line, options);
126+
return await compositor.pushToolProgress(
127+
typeof line === "string" ? buildTelegramTextToolProgressLine(line) : line,
128+
options,
129+
);
117130
};
118131
const pushReasoningProgress = async (payload: {
119132
text?: string;

‎extensions/telegram/src/bot-message-dispatch.progress-rendering.test.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,42 @@ describeTelegramDispatch("dispatchTelegramMessage progress-rendering", () => {
111111

112112
expect(draftStream.updatePreview).toHaveBeenLastCalledWith(
113113
telegramProgressPreview(
114-
"Shelling\n\nChecking recent context",
115-
"<b>Shelling</b>\nChecking recent context",
114+
"Shelling\n\nChecking recent context\n🛠️ Exec",
115+
"<b>Shelling</b>\nChecking recent context\n<b>🛠️ Exec</b>",
116116
),
117117
);
118118
});
119119

120+
it("keeps memory-search tool rows beneath a preamble", async () => {
121+
const draftStream = createSequencedDraftStream(2001);
122+
createTelegramDraftStream.mockReturnValue(draftStream);
123+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ replyOptions }) => {
124+
await replyOptions?.onReplyStart?.();
125+
await replyOptions?.onItemEvent?.({
126+
kind: "preamble",
127+
itemId: "preamble-1",
128+
progressText: "Checking memory",
129+
});
130+
await replyOptions?.onToolStart?.({
131+
name: "memory_search",
132+
phase: "start",
133+
toolCallId: "memory-search-1",
134+
args: { query: "release status" },
135+
});
136+
return { queuedFinal: false };
137+
});
138+
139+
await dispatchWithContext({
140+
context: createContext(),
141+
streamMode: "progress",
142+
telegramCfg: { streaming: { mode: "progress" } },
143+
});
144+
145+
const preview = draftStream.updatePreview.mock.calls.at(-1)?.[0];
146+
expect(preview?.text).toContain("Checking memory");
147+
expect(preview?.text).toContain("Memory Search");
148+
});
149+
120150
it("keeps the progress draft label when tool progress lines are hidden", async () => {
121151
const draftStream = createSequencedDraftStream(2001);
122152
createTelegramDraftStream.mockReturnValue(draftStream);

‎extensions/telegram/src/bot-message-dispatch.progress-updates.test.ts‎

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,57 @@ describeTelegramDispatch("dispatchTelegramMessage progress-updates", () => {
255255
expect(draftStream.flush).toHaveBeenCalled();
256256
});
257257

258+
it("keeps eight rolling tool rows beneath a preamble with verbose off", async () => {
259+
const draftStream = createSequencedDraftStream(2001);
260+
createTelegramDraftStream.mockReturnValue(draftStream);
261+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(
262+
async ({ dispatcherOptions, replyOptions }) => {
263+
await replyOptions?.onReplyStart?.();
264+
await replyOptions?.onAssistantMessageStart?.();
265+
await replyOptions?.onItemEvent?.({
266+
kind: "preamble",
267+
itemId: "preamble-1",
268+
progressText: "I'll make exactly 10 harmless, read-only tool calls.",
269+
});
270+
for (let index = 1; index <= 10; index += 1) {
271+
await replyOptions?.onToolStart?.({
272+
phase: "start",
273+
name: "exec",
274+
toolCallId: `exec-${index}`,
275+
args: { command: `command-${index}` },
276+
});
277+
}
278+
await dispatcherOptions.deliver({ text: "Done" }, { kind: "final" });
279+
return { queuedFinal: true };
280+
},
281+
);
282+
283+
await dispatchWithContext({
284+
context: createContext(),
285+
cfg: { agents: { defaults: { verboseDefault: "off" } } },
286+
streamMode: "progress",
287+
telegramCfg: {
288+
streaming: {
289+
mode: "progress",
290+
progress: { commandText: "raw", maxLines: 8, toolProgress: true },
291+
},
292+
},
293+
});
294+
295+
const rollingPreview = draftStream.updatePreview.mock.calls.at(-1)?.[0];
296+
expect(rollingPreview?.text).toContain("I'll make exactly 10 harmless, read-only tool calls.");
297+
expect(rollingPreview?.text).not.toContain("<code>command-1</code>");
298+
expect(rollingPreview?.text).not.toContain("<code>command-2</code>");
299+
for (let index = 3; index <= 10; index += 1) {
300+
expect(rollingPreview?.text).toContain(`command-${index}`);
301+
}
302+
expectDeliveredReply(0, { text: "Done" });
303+
const collapsePreview = draftStream.finalizeToPreview.mock.calls.at(-1)?.[0] as
304+
| { text?: string }
305+
| undefined;
306+
expect(collapsePreview?.text).toMatch(/^🛠️ 10 tool calls · ⏱️ \d+s$/u);
307+
});
308+
258309
it("renders command status without command output in Telegram progress draft previews", async () => {
259310
const draftStream = createSequencedDraftStream(2001);
260311
createTelegramDraftStream.mockReturnValue(draftStream);
@@ -561,10 +612,78 @@ describeTelegramDispatch("dispatchTelegramMessage progress-updates", () => {
561612

562613
expect(draftStream.updatePreview).toHaveBeenCalledWith(
563614
telegramProgressPreview(
564-
"Shelling\n\nChecking recent context",
565-
"<b>Shelling</b>\nChecking recent context",
615+
"Shelling\n\nChecking recent context\n🛠️ Exec",
616+
"<b>Shelling</b>\nChecking recent context\n<b>🛠️ Exec</b>",
617+
),
618+
);
619+
});
620+
621+
it("keeps fast-mode string progress beneath a Telegram preamble", async () => {
622+
const draftStream = createSequencedDraftStream(2001);
623+
createTelegramDraftStream.mockReturnValue(draftStream);
624+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(
625+
async ({ dispatcherOptions, replyOptions }) => {
626+
await replyOptions?.onReplyStart?.();
627+
await replyOptions?.onItemEvent?.({
628+
kind: "preamble",
629+
itemId: "preamble-1",
630+
progressText: "Checking recent context",
631+
});
632+
await dispatcherOptions.deliver(
633+
{
634+
text: "Fast mode enabled",
635+
channelData: { openclawProgressKind: "fast-mode-auto" },
636+
},
637+
{ kind: "tool" },
638+
);
639+
return { queuedFinal: false };
640+
},
641+
);
642+
643+
await dispatchWithContext({
644+
context: createContext(),
645+
streamMode: "progress",
646+
telegramCfg: {
647+
streaming: { mode: "progress", progress: { label: "Shelling" } },
648+
},
649+
});
650+
651+
expect(draftStream.updatePreview).toHaveBeenLastCalledWith(
652+
telegramProgressPreview(
653+
"Shelling\n\nChecking recent context\nFast mode enabled",
654+
"<b>Shelling</b>\nChecking recent context\nFast mode enabled",
566655
),
567656
);
657+
expect(deliverReplies).not.toHaveBeenCalled();
658+
});
659+
660+
it("keeps string tool-result progress beneath a Telegram preamble", async () => {
661+
const draftStream = createSequencedDraftStream(2001);
662+
createTelegramDraftStream.mockReturnValue(draftStream);
663+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ replyOptions }) => {
664+
await replyOptions?.onReplyStart?.();
665+
await replyOptions?.onItemEvent?.({
666+
kind: "preamble",
667+
itemId: "preamble-1",
668+
progressText: "Checking recent context",
669+
});
670+
await replyOptions?.onToolResult?.({ text: "Background task still running" });
671+
return { queuedFinal: false };
672+
});
673+
674+
await dispatchWithContext({
675+
context: createContext(),
676+
streamMode: "progress",
677+
telegramCfg: {
678+
richMessages: true,
679+
streaming: { mode: "progress", progress: { label: "Shelling" } },
680+
},
681+
});
682+
683+
const preview = draftStream.updatePreview.mock.calls.at(-1)?.[0];
684+
expect(preview?.text).toBe("Shelling\nChecking recent context\nBackground task still running");
685+
expect(JSON.stringify(preview?.richMessage)).toContain("Background task still running");
686+
expect(deliverReplies).not.toHaveBeenCalled();
568687
});
569688

570689
it("retracts the Telegram preamble headline by item identity", async () => {

‎extensions/telegram/src/progress-draft-preview.ts‎

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ function renderTelegramProgressStringLine(text: string): string {
5050
return renderTelegramHtmlText(clipped);
5151
}
5252

53+
function renderTelegramProgressText(text: string): string {
54+
return text.split(/\r?\n/u).map(renderTelegramProgressStringLine).filter(Boolean).join("<br>");
55+
}
56+
5357
function renderTelegramProgressLine(line: ChannelProgressDraftCompositorLine): string {
5458
if (typeof line === "string") {
55-
return line.split(/\r?\n/u).map(renderTelegramProgressStringLine).filter(Boolean).join("<br>");
59+
return renderTelegramProgressText(line);
5660
}
57-
if (!line.icon && line.label === "Commentary") {
58-
return line.text
59-
.split(/\r?\n/u)
60-
.map(renderTelegramProgressStringLine)
61-
.filter(Boolean)
62-
.join("<br>");
61+
if (!line.icon && (!line.label || line.label === "Commentary")) {
62+
return renderTelegramProgressText(line.text);
6363
}
6464
const label = [line.icon, line.label].filter(Boolean).join(" ");
6565
const parts = [`<b>${escapeTelegramProgressHtml(label)}</b>`];
@@ -109,20 +109,20 @@ function markdownLineToRichText(text: string): RichText {
109109
return clipped;
110110
}
111111

112+
function progressTextToRichText(text: string): RichText | undefined {
113+
const parts = text
114+
.split(/\r?\n/u)
115+
.map(markdownLineToRichText)
116+
.filter((part) => part !== "");
117+
return parts.length ? joinRichText(parts, "\n") : undefined;
118+
}
119+
112120
function progressLineToRichText(line: ChannelProgressDraftCompositorLine): RichText | undefined {
113121
if (typeof line === "string") {
114-
const parts = line
115-
.split(/\r?\n/u)
116-
.map(markdownLineToRichText)
117-
.filter((part) => part !== "");
118-
return parts.length ? joinRichText(parts, "\n") : undefined;
122+
return progressTextToRichText(line);
119123
}
120-
if (!line.icon && line.label === "Commentary") {
121-
const parts = line.text
122-
.split(/\r?\n/u)
123-
.map(markdownLineToRichText)
124-
.filter((part) => part !== "");
125-
return parts.length ? joinRichText(parts, "\n") : undefined;
124+
if (!line.icon && (!line.label || line.label === "Commentary")) {
125+
return progressTextToRichText(line.text);
126126
}
127127
const label = [line.icon, line.label].filter(Boolean).join(" ");
128128
const parts: RichText[] = [boldRichText(label)];
@@ -145,6 +145,15 @@ function buildProgressRichBlocks(parts: RichText[]): InputRichBlock[] {
145145
return [paragraphBlock(joinRichText(parts, "\n"))];
146146
}
147147

148+
function isStatusHeadlineWorkLine(
149+
line: ChannelProgressDraftCompositorLine,
150+
): line is Exclude<ChannelProgressDraftCompositorLine, string> {
151+
if (typeof line === "string") {
152+
return false;
153+
}
154+
return !line.id?.startsWith("reasoning:") && !line.id?.startsWith("commentary:");
155+
}
156+
148157
export function renderTelegramProgressDraftPreview(
149158
text: string,
150159
lines: readonly ChannelProgressDraftCompositorLine[],
@@ -157,26 +166,39 @@ export function renderTelegramProgressDraftPreview(
157166
.split(/\r?\n/u)
158167
.map((line) => line.trim())
159168
.filter(Boolean);
169+
const workLines = lines.filter(isStatusHeadlineWorkLine);
170+
const renderedLines = workLines.map(renderTelegramProgressLine).filter(Boolean);
160171
if (!richMessages) {
161-
const html =
172+
const renderedStatusLines =
162173
statusLines.length > 1
163174
? [
164175
`<b>${escapeTelegramProgressHtml(statusLines[0] ?? "")}</b>`,
165176
...statusLines.slice(1).map(renderTelegramProgressStringLine),
166-
].join("<br>")
167-
: statusLines.map(renderTelegramProgressStringLine).join("<br>");
168-
return { text: html, parseMode: "HTML" };
177+
]
178+
: statusLines.map(renderTelegramProgressStringLine);
179+
return { text: [...renderedStatusLines, ...renderedLines].join("<br>"), parseMode: "HTML" };
169180
}
170-
const richParts: RichText[] =
181+
const richStatusParts: RichText[] =
171182
statusLines.length > 1
172183
? [boldRichText(statusLines[0] ?? ""), ...statusLines.slice(1).map(markdownLineToRichText)]
173184
: statusLines.map(markdownLineToRichText);
185+
const richLineParts = workLines
186+
.map(progressLineToRichText)
187+
.filter((part): part is RichText => part !== undefined);
188+
const plainLineTexts = workLines
189+
.map((line) => line.text)
190+
.map((line) => line.trim())
191+
.filter(Boolean);
192+
const plainText = [...statusLines, ...plainLineTexts].join("\n");
174193
return {
175-
text: trimmed,
176-
richMessage: buildTelegramRichBlocksPlan(buildProgressRichBlocks(richParts), {
177-
skipEntityDetection: true,
178-
plainText: trimmed,
179-
}).richMessage,
194+
text: plainText,
195+
richMessage: buildTelegramRichBlocksPlan(
196+
buildProgressRichBlocks([...richStatusParts, ...richLineParts]),
197+
{
198+
skipEntityDetection: true,
199+
plainText,
200+
},
201+
).richMessage,
180202
};
181203
}
182204
const renderedLines = lines.map(renderTelegramProgressLine).filter(Boolean);

‎src/channels/progress-draft-compositor.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,26 @@ describe("createChannelProgressDraftCompositor", () => {
422422
expect(update).toHaveBeenCalledWith("Reading the workspace.", { flush: true, lines: [] });
423423
});
424424

425+
it("publishes rolling tool-line changes beneath a stable preamble headline", async () => {
426+
const update = vi.fn();
427+
const progress = createChannelProgressDraftCompositor({
428+
entry: { streaming: { mode: "progress", progress: { maxLines: 8 } } },
429+
mode: "progress",
430+
active: true,
431+
seed: "test",
432+
updateOnLineChange: true,
433+
update,
434+
});
435+
436+
await progress.pushPreambleHeadline("Reading the workspace.");
437+
await progress.pushToolProgress("🛠️ Exec one", { startImmediately: true });
438+
await progress.pushToolProgress("🛠️ Exec two", { startImmediately: true });
439+
440+
expect(update).toHaveBeenLastCalledWith("Reading the workspace.", {
441+
lines: ["🛠️ Exec one", "🛠️ Exec two"],
442+
});
443+
});
444+
425445
it("rejects control-only preambles without clobbering a valid headline", async () => {
426446
let nowMs = 0;
427447
const update = vi.fn();

0 commit comments

Comments
 (0)