

























@@ -700,13 +700,18 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
700700return output;
701701};
702702703-const emitBlockChunk = (text: string, options?: { assistantMessageIndex?: number }) => {
703+const emitBlockChunk = (
704+text: string,
705+options?: { assistantMessageIndex?: number; final?: boolean },
706+) => {
704707if (state.suppressBlockChunks || params.silentExpected) {
705708return;
706709}
707710// Strip <think> and <final> blocks across chunk boundaries to avoid leaking reasoning.
708711// Also strip downgraded tool call text ([Tool Call: ...], [Historical context: ...], etc.).
709-const chunk = stripDowngradedToolCallText(stripBlockTags(text, state.blockState)).trimEnd();
712+const chunk = stripDowngradedToolCallText(
713+stripBlockTags(text, state.blockState, { final: options?.final === true }),
714+).trimEnd();
710715if (!chunk) {
711716return;
712717}
@@ -769,17 +774,42 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
769774770775const flushBlockReplyBuffer = (options?: {
771776assistantMessageIndex?: number;
777+final?: boolean;
772778}): void | Promise<void> => {
773779if (!params.onBlockReply) {
774780return;
775781}
776782if (blockChunker?.hasBuffered()) {
777-blockChunker.drain({ force: true, emit: (text) => emitBlockChunk(text, options) });
783+if (options?.final) {
784+let pendingChunk: string | undefined;
785+blockChunker.drain({
786+force: true,
787+emit: (text) => {
788+if (pendingChunk !== undefined) {
789+emitBlockChunk(pendingChunk, {
790+assistantMessageIndex: options.assistantMessageIndex,
791+});
792+}
793+pendingChunk = text;
794+},
795+});
796+if (pendingChunk !== undefined) {
797+emitBlockChunk(pendingChunk, {
798+assistantMessageIndex: options.assistantMessageIndex,
799+final: true,
800+});
801+}
802+} else {
803+blockChunker.drain({ force: true, emit: (text) => emitBlockChunk(text, options) });
804+}
778805blockChunker.reset();
779806} else if (state.blockBuffer.length > 0) {
780807emitBlockChunk(state.blockBuffer, options);
781808state.blockBuffer = "";
782809}
810+if (options?.final) {
811+emitBlockChunk("", options);
812+}
783813if (pendingBlockReplyTasks.size === 0) {
784814return;
785815}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。