fix(telegram): finalize stop-created draft preview edits

This commit is contained in:
Ayaan Zaidi
2026-02-19 15:59:41 +05:30
parent 2947c69ae4
commit e57a4884dc
2 changed files with 41 additions and 0 deletions

View File

@@ -687,6 +687,45 @@ describe("dispatchTelegramMessage draft streaming", () => {
expect(reasoningDraftStream.forceNewMessage).toHaveBeenCalledTimes(1);
});
it("edits stop-created preview when final text is shorter than buffered draft", async () => {
let answerMessageId: number | undefined;
const answerDraftStream = {
update: vi.fn(),
flush: vi.fn().mockResolvedValue(undefined),
messageId: vi.fn().mockImplementation(() => answerMessageId),
clear: vi.fn().mockResolvedValue(undefined),
stop: vi.fn().mockImplementation(async () => {
answerMessageId = 999;
}),
forceNewMessage: vi.fn(),
};
const reasoningDraftStream = createDraftStream();
createTelegramDraftStream
.mockImplementationOnce(() => answerDraftStream)
.mockImplementationOnce(() => reasoningDraftStream);
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(
async ({ dispatcherOptions, replyOptions }) => {
await replyOptions?.onPartialReply?.({
text: "Let me check that file and confirm details for you.",
});
await dispatcherOptions.deliver({ text: "Let me check that file." }, { kind: "final" });
return { queuedFinal: true };
},
);
deliverReplies.mockResolvedValue({ delivered: true });
editMessageTelegram.mockResolvedValue({ ok: true, chatId: "123", messageId: "999" });
await dispatchWithContext({ context: createContext(), streamMode: "block" });
expect(editMessageTelegram).toHaveBeenCalledWith(
123,
999,
"Let me check that file.",
expect.any(Object),
);
expect(deliverReplies).not.toHaveBeenCalled();
});
it("does not edit preview message when final payload is an error", async () => {
const draftStream = createDraftStream(999);
createTelegramDraftStream.mockReturnValue(draftStream);

View File

@@ -365,6 +365,7 @@ export const dispatchTelegramMessage = async ({
if (!lane.stream) {
return false;
}
const hadPreviewMessage = typeof lane.stream.messageId() === "number";
const currentPreviewText = streamMode === "block" ? lane.draftText : lane.lastPartialText;
await lane.stream.stop();
const previewMessageId = lane.stream.messageId();
@@ -372,6 +373,7 @@ export const dispatchTelegramMessage = async ({
return false;
}
if (
hadPreviewMessage &&
currentPreviewText &&
currentPreviewText.startsWith(finalText) &&
finalText.length < currentPreviewText.length