Skip to content

Commit 9873883

Browse files
committed
fix: replace current telegram tool preview
1 parent 8634f31 commit 9873883

2 files changed

Lines changed: 72 additions & 18 deletions

File tree

src/gateway/agent-event-channel-mirror.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,59 @@ describe("agent event channel mirror", () => {
208208
});
209209
});
210210

211+
it("replaces repeated tool progress lines with the latest current tool", async () => {
212+
const { mirror, editProgressPreview } = createHarness({
213+
telegramConfig: { streaming: { mode: "progress", progress: { label: false, maxLines: 8 } } },
214+
});
215+
216+
await mirror(
217+
event({
218+
stream: "item",
219+
data: {
220+
itemId: "read-1",
221+
phase: "end",
222+
kind: "tool",
223+
name: "read",
224+
title: "Read",
225+
progressText: "first 120 lines of memory/gotchas.md",
226+
},
227+
}),
228+
);
229+
await mirror(
230+
event({
231+
seq: 2,
232+
stream: "item",
233+
data: {
234+
itemId: "read-2",
235+
phase: "end",
236+
kind: "tool",
237+
name: "read",
238+
title: "Read",
239+
progressText: "first 120 lines of memory/designs.md",
240+
},
241+
}),
242+
);
243+
await mirror(
244+
event({
245+
seq: 3,
246+
stream: "item",
247+
data: {
248+
itemId: "read-3",
249+
phase: "end",
250+
kind: "tool",
251+
name: "read",
252+
title: "Read",
253+
progressText: "first 120 lines of memory/anti-patterns.md",
254+
},
255+
}),
256+
);
257+
258+
const latestPreviewText = editProgressPreview.mock.calls.at(-1)?.[0].text;
259+
expect(latestPreviewText).not.toContain("gotchas");
260+
expect(latestPreviewText).not.toContain("designs");
261+
expect(latestPreviewText).toContain("anti-patterns");
262+
});
263+
211264
it("bounds progress preview lines and deletes the preview when the run ends", async () => {
212265
const { mirror, sendProgressPreview, editProgressPreview, deleteProgressPreview } =
213266
createHarness({

src/gateway/agent-event-channel-mirror.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ function textFromData(
208208
return text;
209209
}
210210

211+
const CURRENT_TOOL_PROGRESS_LINE_ID = "current-tool";
212+
211213
function lineWithId<TLine extends ChannelProgressDraftLine | undefined>(
212214
line: TLine,
213215
id: string | undefined,
@@ -241,22 +243,23 @@ function formatAgentEventForChannelMirrorLine(
241243
if (data.suppressChannelProgress === true) {
242244
return undefined;
243245
}
244-
return buildChannelProgressDraftLineForEntry(entry, {
245-
event: "item",
246-
itemId: normalizeOptionalString(data.itemId),
247-
itemKind: normalizeOptionalString(data.kind),
248-
title: normalizeOptionalString(data.title),
249-
name: normalizeOptionalString(data.name),
250-
phase: normalizeOptionalString(data.phase),
251-
status: normalizeOptionalString(data.status),
252-
summary: normalizeOptionalString(data.summary),
253-
progressText: normalizeOptionalString(data.progressText),
254-
meta: normalizeOptionalString(data.meta),
255-
});
246+
return lineWithId(
247+
buildChannelProgressDraftLineForEntry(entry, {
248+
event: "item",
249+
itemKind: normalizeOptionalString(data.kind),
250+
title: normalizeOptionalString(data.title),
251+
name: normalizeOptionalString(data.name),
252+
phase: normalizeOptionalString(data.phase),
253+
status: normalizeOptionalString(data.status),
254+
summary: normalizeOptionalString(data.summary),
255+
progressText: normalizeOptionalString(data.progressText),
256+
meta: normalizeOptionalString(data.meta),
257+
}),
258+
CURRENT_TOOL_PROGRESS_LINE_ID,
259+
);
256260
}
257261

258262
if (evt.stream === "command_output") {
259-
const id = normalizeOptionalString(data.itemId) ?? normalizeOptionalString(data.toolCallId);
260263
return lineWithId(
261264
buildChannelProgressDraftLineForEntry(entry, {
262265
event: "command-output",
@@ -266,12 +269,11 @@ function formatAgentEventForChannelMirrorLine(
266269
status: normalizeOptionalString(data.status),
267270
exitCode: typeof data.exitCode === "number" ? data.exitCode : null,
268271
}),
269-
id,
272+
CURRENT_TOOL_PROGRESS_LINE_ID,
270273
);
271274
}
272275

273276
if (evt.stream === "approval") {
274-
const id = normalizeOptionalString(data.approvalId) ?? normalizeOptionalString(data.toolCallId);
275277
return lineWithId(
276278
buildChannelProgressDraftLineForEntry(entry, {
277279
event: "approval",
@@ -281,7 +283,7 @@ function formatAgentEventForChannelMirrorLine(
281283
reason: normalizeOptionalString(data.reason),
282284
message: normalizeOptionalString(data.message),
283285
}),
284-
id,
286+
CURRENT_TOOL_PROGRESS_LINE_ID,
285287
);
286288
}
287289

@@ -298,7 +300,6 @@ function formatAgentEventForChannelMirrorLine(
298300
}
299301

300302
if (evt.stream === "patch") {
301-
const id = normalizeOptionalString(data.itemId) ?? normalizeOptionalString(data.toolCallId);
302303
const stringArray = (value: unknown): string[] | undefined =>
303304
Array.isArray(value)
304305
? value.filter((entry): entry is string => typeof entry === "string")
@@ -314,7 +315,7 @@ function formatAgentEventForChannelMirrorLine(
314315
deleted: stringArray(data.deleted),
315316
summary: normalizeOptionalString(data.summary),
316317
}),
317-
id,
318+
CURRENT_TOOL_PROGRESS_LINE_ID,
318319
);
319320
}
320321

0 commit comments

Comments
 (0)