fix: harden telegram streaming state

This commit is contained in:
Ayaan Zaidi 2026-01-31 22:08:01 +05:30 committed by Ayaan Zaidi
parent 37721ebd7c
commit a64d8d2d66
3 changed files with 10 additions and 4 deletions

View file

@ -302,4 +302,5 @@ export function handleMessageEnd(
ctx.state.blockState.final = false; ctx.state.blockState.final = false;
ctx.state.blockState.inlineCode = createInlineCodeState(); ctx.state.blockState.inlineCode = createInlineCodeState();
ctx.state.lastStreamedAssistant = undefined; ctx.state.lastStreamedAssistant = undefined;
ctx.state.lastStreamedAssistantCleaned = undefined;
} }

View file

@ -70,11 +70,12 @@ export const dispatchTelegramMessage = async ({
const isPrivateChat = msg.chat.type === "private"; const isPrivateChat = msg.chat.type === "private";
const messageThreadId = (msg as { message_thread_id?: number }).message_thread_id; const messageThreadId = (msg as { message_thread_id?: number }).message_thread_id;
const draftThreadId = replyThreadId ?? messageThreadId;
const draftMaxChars = Math.min(textLimit, 4096); const draftMaxChars = Math.min(textLimit, 4096);
const canStreamDraft = const canStreamDraft =
streamMode !== "off" && streamMode !== "off" &&
isPrivateChat && isPrivateChat &&
typeof messageThreadId === "number" && typeof draftThreadId === "number" &&
(await resolveBotTopicsEnabled(primaryCtx)); (await resolveBotTopicsEnabled(primaryCtx));
const draftStream = canStreamDraft const draftStream = canStreamDraft
? createTelegramDraftStream({ ? createTelegramDraftStream({
@ -82,7 +83,7 @@ export const dispatchTelegramMessage = async ({
chatId, chatId,
draftId: msg.message_id || Date.now(), draftId: msg.message_id || Date.now(),
maxChars: draftMaxChars, maxChars: draftMaxChars,
messageThreadId: replyThreadId, messageThreadId: draftThreadId,
log: logVerbose, log: logVerbose,
warn: logVerbose, warn: logVerbose,
}) })

View file

@ -74,13 +74,17 @@ export function createTelegramDraftStream(params: {
return; return;
} }
const text = pendingText; const text = pendingText;
pendingText = ""; const trimmed = text.trim();
if (!text.trim()) { if (!trimmed) {
if (pendingText === text) {
pendingText = "";
}
if (pendingText) { if (pendingText) {
schedule(); schedule();
} }
return; return;
} }
pendingText = "";
inFlight = true; inFlight = true;
try { try {
await sendDraft(text); await sendDraft(text);