@@ -52,7 +52,8 @@ import {
5252 type TelegramPromptContextProjectionSequence,
5353 type TelegramPromptContextSource,
5454} from "./prompt-context-projection.js";
55- import { editMessageTelegram } from "./send.js";
55+ import { registerTelegramQuestionDelivery } from "./question-finalization.js";
56+ import { editMessageReplyMarkupTelegram, editMessageTelegram } from "./send.js";
5657import { resolveTelegramTargetChatType } from "./targets.js";
5758
5859type TelegramDeliveryConfig = TurnConfig & {
@@ -331,10 +332,7 @@ export async function sendPayload(
331332 }
332333}
333334
334- export async function emitPreviewFinalizedHook(
335- turn: Turn,
336- result: LaneDeliveryResult,
337- ): Promise<void> {
335+ async function emitPreviewFinalizedHook(turn: Turn, result: LaneDeliveryResult): Promise<void> {
338336 if (
339337 turn.isSuperseded() ||
340338 (result.kind !== "preview-finalized" && result.kind !== "preview-finalized-partial")
@@ -348,7 +346,7 @@ export async function emitPreviewFinalizedHook(
348346 chatId: String(turn.context.chatId),
349347 accountId: turn.context.route.accountId,
350348 content: result.delivery.content,
351- success: true ,
349+ success: result.kind === "preview-finalized" ,
352350 messageId: result.delivery.messageId,
353351 isGroup: turn.context.isGroup,
354352 groupId: turn.context.isGroup ? String(turn.context.chatId) : undefined,
@@ -361,6 +359,55 @@ export async function emitPreviewFinalizedHook(
361359 }
362360}
363361
362+ export async function handlePreviewFinalizedResult(
363+ turn: Turn,
364+ result: LaneDeliveryResult,
365+ ): Promise<void> {
366+ if (result.kind !== "preview-finalized" && result.kind !== "preview-finalized-partial") {
367+ return;
368+ }
369+ await emitPreviewFinalizedHook(turn, result);
370+ if (result.kind === "preview-finalized-partial") {
371+ // The preview is already visible, so this failure is terminal: preserve its
372+ // receipt and prevent outer fallback delivery from duplicating the message.
373+ markFinalDelivered(turn);
374+ throw mergeTelegramPartialDeliveryError(result.error, {
375+ receipt: result.delivery.receipt,
376+ content: result.delivery.content,
377+ messageIds: result.delivery.receipt.platformMessageIds,
378+ visibleReplySent: true,
379+ });
380+ }
381+ }
382+
383+ export function registerTelegramQuestionDeliveryForMessage(
384+ turn: Turn,
385+ payload: ReplyPayload,
386+ delivery: { messageId: number; text: string },
387+ ): void {
388+ const { chatId } = turn.context;
389+ const accountId = turn.context.route.accountId;
390+ const api = turn.bot.api;
391+ const cfg = turn.cfg;
392+ const linkPreview = turn.telegramCfg.linkPreview;
393+ const editText = turn.telegramDeps.editMessageTelegram ?? editMessageTelegram;
394+ const { messageId, text } = delivery;
395+ registerTelegramQuestionDelivery({
396+ accountId,
397+ chatId: String(chatId),
398+ messageId,
399+ payload,
400+ text,
401+ textLimit: turn.textLimit,
402+ clearButtons: async () => {
403+ await editMessageReplyMarkupTelegram(chatId, messageId, [], { api, cfg, accountId });
404+ },
405+ annotate: async (finalText) => {
406+ await editText(chatId, messageId, finalText, { api, cfg, accountId, linkPreview });
407+ },
408+ });
409+ }
410+
364411async function materializeAnswerLaneBeforeRotation(turn: Turn): Promise<void> {
365412 const block = turn.activeAnswerBlockDelivery;
366413 const lane = turn.answerLane;
@@ -387,7 +434,7 @@ async function materializeAnswerLaneBeforeRotation(turn: Turn): Promise<void> {
387434 durable: false,
388435 });
389436 turn.activeAnswerBlockDelivery = undefined;
390- await emitPreviewFinalizedHook (turn, result);
437+ await handlePreviewFinalizedResult (turn, result);
391438}
392439
393440async function deliverTelegramProgressModeFinalAnswer(
@@ -485,15 +532,11 @@ export async function deliverFinalAnswerText(
485532 markFinalDelivered(turn);
486533 }
487534 }
488- if (result.kind === "preview-finalized" || result.kind === "preview-finalized-partial") {
489- await emitPreviewFinalizedHook(turn, result);
490- }
491- if (result.kind === "preview-finalized-partial") {
492- throw mergeTelegramPartialDeliveryError(result.error, {
493- receipt: result.delivery.receipt,
494- content: result.delivery.content,
495- messageIds: result.delivery.receipt.platformMessageIds,
496- visibleReplySent: true,
535+ await handlePreviewFinalizedResult(turn, result);
536+ if (result.kind === "preview-finalized") {
537+ registerTelegramQuestionDeliveryForMessage(turn, answerPayload, {
538+ messageId: result.delivery.messageId,
539+ text: result.delivery.content,
497540 });
498541 }
499542 return result;
0 commit comments