Skip to content

Commit 2a39e6a

Browse files
committed
refactor(telegram): distill retained preview finalization
1 parent 16edda7 commit 2a39e6a

2 files changed

Lines changed: 14 additions & 16 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,7 +3577,6 @@ describe("dispatchTelegramMessage draft streaming", () => {
35773577
const { answerDraftStream } = setupDraftStreams({ answerMessageId: 2001 });
35783578
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(
35793579
async ({ dispatcherOptions, replyOptions }) => {
3580-
// Force a generic rotation (tool-progress handoff)
35813580
await replyOptions?.onToolStart?.({ name: "exec", phase: "start" });
35823581
await dispatcherOptions.deliver(
35833582
{ text: "A".repeat(4000) + "B".repeat(4000) },
@@ -3592,7 +3591,6 @@ describe("dispatchTelegramMessage draft streaming", () => {
35923591
textLimit: 4000,
35933592
});
35943593

3595-
// The stream should receive the first chunk ("A"s), not skip it because of activeChunkIndex being falsely incremented
35963594
expect(answerDraftStream.update).toHaveBeenCalledWith("A".repeat(4000));
35973595
});
35983596

@@ -3604,11 +3602,12 @@ describe("dispatchTelegramMessage draft streaming", () => {
36043602
return { queuedFinal: true };
36053603
});
36063604

3607-
// Provide a config that disables partial answer streams (e.g. block mode explicitly enabled)
36083605
await dispatchWithContext({
36093606
context: createContext(),
36103607
streamMode: "partial",
3611-
telegramCfg: { streaming: { mode: "partial", block: { enabled: true } } } as any,
3608+
telegramCfg: {
3609+
streaming: { mode: "partial", block: { enabled: true } },
3610+
} satisfies Parameters<typeof dispatchTelegramMessage>[0]["telegramCfg"],
36123611
});
36133612

36143613
const deliveredTexts = deliverReplies.mock.calls.flatMap((call) =>

extensions/telegram/src/lane-delivery-text-deliverer.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,12 @@ export function createLaneTextDeliverer(params: CreateLaneTextDelivererParams) {
278278
: [text];
279279

280280
const clampActiveChunkIndex = () =>
281-
Math.min(lane.activeChunkIndex ?? 0, Math.max(0, chunks.length - 1));
281+
Math.min(lane.activeChunkIndex, Math.max(0, chunks.length - 1));
282282
const activeChunkIndex = clampActiveChunkIndex();
283-
const firstChunk = chunks[activeChunkIndex];
283+
const activeChunk = chunks[activeChunkIndex];
284284
const remainingChunks = chunks.slice(activeChunkIndex + 1);
285285

286-
if (!firstChunk || firstChunk.length > params.draftMaxChars) {
286+
if (!activeChunk || activeChunk.length > params.draftMaxChars) {
287287
return undefined;
288288
}
289289

@@ -297,12 +297,11 @@ export function createLaneTextDeliverer(params: CreateLaneTextDelivererParams) {
297297
deliveredText: deliveredStreamTextBeforeUpdate,
298298
finalText,
299299
}) &&
300-
deliveredStreamTextBeforeUpdate.length > firstChunk.trimEnd().length;
300+
deliveredStreamTextBeforeUpdate.length > activeChunk.trimEnd().length;
301301

302302
const finalizeDeliveredPrefix = async (
303303
deliveredStreamText: string,
304304
messageId: number,
305-
suffixSourceText = activeFullText,
306305
): Promise<LaneDeliveryResult> => {
307306
lane.finalized = true;
308307
params.markDelivered();
@@ -321,7 +320,7 @@ export function createLaneTextDeliverer(params: CreateLaneTextDelivererParams) {
321320
}
322321
}
323322
}
324-
const suffix = suffixSourceText.slice(deliveredStreamText.length);
323+
const suffix = activeFullText.slice(deliveredStreamText.length);
325324
if (suffix.trim().length > 0) {
326325
for (const chunk of compactChunks(params.splitFinalTextForStream?.(suffix) ?? [])) {
327326
if (chunk.trim().length === 0) {
@@ -408,18 +407,18 @@ export function createLaneTextDeliverer(params: CreateLaneTextDelivererParams) {
408407
}
409408

410409
if (!deliveredPrefixBeforeUpdate) {
411-
lane.lastPartialText = firstChunk;
410+
lane.lastPartialText = activeChunk;
412411
lane.hasStreamedMessage = true;
413412
lane.finalized = false;
414-
stream.update(firstChunk);
413+
stream.update(activeChunk);
415414
}
416415
if (isFinal) {
417416
await params.stopDraftLane(lane);
418417
} else {
419418
await params.flushDraftLane(lane);
420419
}
421420
const activeChunkIndexAfterStop = isFinal ? clampActiveChunkIndex() : activeChunkIndex;
422-
const activeChunkAfterStop = chunks[activeChunkIndexAfterStop] ?? firstChunk;
421+
const activeChunkAfterStop = chunks[activeChunkIndexAfterStop] ?? activeChunk;
423422
const remainingChunksAfterStop = chunks.slice(activeChunkIndexAfterStop + 1);
424423

425424
const messageId = stream.messageId();
@@ -434,14 +433,14 @@ export function createLaneTextDeliverer(params: CreateLaneTextDelivererParams) {
434433

435434
const deliveredStreamTextAfterStop = stream.lastDeliveredText?.();
436435
const activeChunkTextAfterStop = activeChunkAfterStop.trimEnd();
437-
const retainedFirstChunkAfterStop =
436+
const retainedActiveChunkAfterStop =
438437
activeChunkIndexAfterStop !== activeChunkIndex &&
439-
deliveredStreamTextAfterStop === firstChunk.trimEnd();
438+
deliveredStreamTextAfterStop === activeChunk.trimEnd();
440439
if (
441440
isFinal &&
442441
deliveredStreamTextAfterStop !== undefined &&
443442
deliveredStreamTextAfterStop !== activeChunkTextAfterStop &&
444-
!retainedFirstChunkAfterStop
443+
!retainedActiveChunkAfterStop
445444
) {
446445
if (
447446
isDeliveredPrefix({ deliveredText: deliveredStreamTextAfterStop, finalText }) &&

0 commit comments

Comments
 (0)