Skip to content

Commit f123c42

Browse files
committed
fix(telegram): clear split reasoning previews
1 parent 961ce49 commit f123c42

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,6 +3435,36 @@ describe("dispatchTelegramMessage draft streaming", () => {
34353435
expect(updates.join("\n")).not.toContain("CheckingReading");
34363436
});
34373437

3438+
it("clears the prior reasoning preview before splitting into a new reasoning message", async () => {
3439+
const answerDraftStream = createDraftStream(2001);
3440+
const reasoningDraftStream = createTestDraftStream({
3441+
messageId: 3001,
3442+
clearMessageIdOnForceNew: true,
3443+
});
3444+
createTelegramDraftStream
3445+
.mockImplementationOnce(() => answerDraftStream)
3446+
.mockImplementationOnce(() => reasoningDraftStream);
3447+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ replyOptions }) => {
3448+
await replyOptions?.onReasoningStream?.({ text: "<think>First thought</think>" });
3449+
await replyOptions?.onReasoningEnd?.();
3450+
await replyOptions?.onReasoningStream?.({ text: "<think>Second thought</think>" });
3451+
return { queuedFinal: false };
3452+
});
3453+
3454+
await dispatchWithContext({ context: createReasoningStreamContext() });
3455+
3456+
expect(reasoningDraftStream.update).toHaveBeenCalledWith("Thinking\n\n_First thought_");
3457+
expect(reasoningDraftStream.update).toHaveBeenCalledWith("Thinking\n\n_Second thought_");
3458+
expect(reasoningDraftStream.forceNewMessage).toHaveBeenCalledTimes(1);
3459+
expect(reasoningDraftStream.clear).toHaveBeenCalledTimes(2);
3460+
expect(reasoningDraftStream.clear.mock.invocationCallOrder[0]).toBeLessThan(
3461+
reasoningDraftStream.forceNewMessage.mock.invocationCallOrder[0],
3462+
);
3463+
expect(reasoningDraftStream.clear.mock.invocationCallOrder[0]).toBeLessThan(
3464+
reasoningDraftStream.update.mock.invocationCallOrder[1],
3465+
);
3466+
});
3467+
34383468
it("streams reasoning from configured defaults", async () => {
34393469
const { answerDraftStream, reasoningDraftStream } = setupDraftStreams({
34403470
answerMessageId: 2001,

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,18 @@ export const dispatchTelegramMessage = async ({
12031203
lane.stream?.forceNewMessage();
12041204
resetDraftLaneState(lane);
12051205
};
1206+
const clearReasoningLaneForNewMessage = async () => {
1207+
if (
1208+
!reasoningLane.hasStreamedMessage &&
1209+
typeof reasoningLane.stream?.messageId() !== "number"
1210+
) {
1211+
resetDraftLaneState(reasoningLane);
1212+
return;
1213+
}
1214+
await reasoningLane.stream?.clear();
1215+
reasoningLane.stream?.forceNewMessage();
1216+
resetDraftLaneState(reasoningLane);
1217+
};
12061218
const rotateAnswerLaneForNewMessage = async () => {
12071219
if (materializeAnswerLaneBeforeRotation) {
12081220
await materializeAnswerLaneBeforeRotation();
@@ -2358,8 +2370,7 @@ export const dispatchTelegramMessage = async ({
23582370
? (payload) =>
23592371
enqueueDraftLaneEvent(async () => {
23602372
if (splitReasoningOnNextStream) {
2361-
reasoningLane.stream?.forceNewMessage();
2362-
resetDraftLaneState(reasoningLane);
2373+
await clearReasoningLaneForNewMessage();
23632374
splitReasoningOnNextStream = false;
23642375
}
23652376
await ingestDraftLaneSegments(payload, true);

0 commit comments

Comments
 (0)