Skip to content

Commit 13dad83

Browse files
committed
Telegram: separate verbose tool results from final answers
1 parent 7bf87cc commit 13dad83

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

extensions/telegram/src/bot-message-dispatch.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,25 @@ describe("dispatchTelegramMessage draft streaming", () => {
11401140
expect(rotationOrder).toBeLessThan(finalUpdateOrder);
11411141
});
11421142

1143+
it("rotates a verbose tool result draft before streaming the final answer", async () => {
1144+
const { answerDraftStream } = setupDraftStreams({ answerMessageId: 2001 });
1145+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ dispatcherOptions }) => {
1146+
await dispatcherOptions.deliver({ text: "🛠️ Exec: pnpm test" }, { kind: "tool" });
1147+
await dispatcherOptions.deliver({ text: "Tests passed" }, { kind: "final" });
1148+
return { queuedFinal: true };
1149+
});
1150+
1151+
await dispatchWithContext({ context: createContext() });
1152+
1153+
expect(answerDraftStream.update).toHaveBeenNthCalledWith(1, "🛠️ Exec: pnpm test");
1154+
expect(answerDraftStream.update).toHaveBeenNthCalledWith(2, "Tests passed");
1155+
expect(answerDraftStream.forceNewMessage).toHaveBeenCalledTimes(1);
1156+
expect(answerDraftStream.clear).not.toHaveBeenCalled();
1157+
const rotationOrder = answerDraftStream.forceNewMessage.mock.invocationCallOrder[0];
1158+
const finalUpdateOrder = answerDraftStream.update.mock.invocationCallOrder[1];
1159+
expect(rotationOrder).toBeLessThan(finalUpdateOrder);
1160+
});
1161+
11431162
it("keeps progress updates in a draft and sends the final answer normally", async () => {
11441163
const { answerDraftStream } = setupDraftStreams({ answerMessageId: 2001 });
11451164
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,18 @@ export const dispatchTelegramMessage = async ({
589589
let streamToolProgressLines: string[] = [];
590590
let lastAnswerPartialText = "";
591591
let activeAnswerDraftIsToolProgressOnly = false;
592-
const noteAnswerToolProgressDraft = () => {
593-
activeAnswerDraftIsToolProgressOnly =
594-
!answerLane.hasStreamedMessage || activeAnswerDraftIsToolProgressOnly;
595-
};
596-
const resetAnswerToolProgressDraft = () => {
592+
function resetAnswerToolProgressDraft() {
597593
activeAnswerDraftIsToolProgressOnly = false;
598-
};
594+
}
595+
async function prepareAnswerLaneForToolProgress() {
596+
if (activeAnswerDraftIsToolProgressOnly) {
597+
return;
598+
}
599+
if (answerLane.hasStreamedMessage) {
600+
await rotateLaneForNewMessage(answerLane);
601+
}
602+
activeAnswerDraftIsToolProgressOnly = true;
603+
}
599604
const renderProgressDraft = async (options?: { flush?: boolean }) => {
600605
if (!answerLane.stream || streamMode !== "progress") {
601606
return;
@@ -609,7 +614,7 @@ export const dispatchTelegramMessage = async ({
609614
if (!streamText || streamText === answerLane.lastPartialText) {
610615
return;
611616
}
612-
noteAnswerToolProgressDraft();
617+
await prepareAnswerLaneForToolProgress();
613618
answerLane.lastPartialText = streamText;
614619
answerLane.hasStreamedMessage = true;
615620
answerLane.finalized = false;
@@ -649,7 +654,7 @@ export const dispatchTelegramMessage = async ({
649654
seed: progressSeed,
650655
formatLine: formatProgressAsMarkdownCode,
651656
});
652-
noteAnswerToolProgressDraft();
657+
await prepareAnswerLaneForToolProgress();
653658
answerLane.lastPartialText = streamText;
654659
answerLane.hasStreamedMessage = true;
655660
answerLane.finalized = false;
@@ -1302,6 +1307,9 @@ export const dispatchTelegramMessage = async ({
13021307
if (segment.lane === "reasoning") {
13031308
reasoningStepState.noteReasoningHint();
13041309
}
1310+
if (segment.lane === "answer" && info.kind === "tool") {
1311+
await prepareAnswerLaneForToolProgress();
1312+
}
13051313
const result =
13061314
segment.lane === "answer" && info.kind === "final"
13071315
? await deliverFinalAnswerText(

0 commit comments

Comments
 (0)