fix: harden block stream dedupe

This commit is contained in:
Peter Steinberger
2026-01-03 18:44:07 +01:00
parent 73fa2e10bc
commit 72b34f7d03
8 changed files with 162 additions and 38 deletions

View File

@@ -259,6 +259,11 @@ export function subscribeEmbeddedPiSession(params: {
if (!blockChunking) return;
const minChars = Math.max(1, Math.floor(blockChunking.minChars));
const maxChars = Math.max(minChars, Math.floor(blockChunking.maxChars));
if (force && blockBuffer.length > 0 && blockBuffer.length <= maxChars) {
emitBlockChunk(blockBuffer);
blockBuffer = "";
return;
}
if (blockBuffer.length < minChars && !force) return;
while (blockBuffer.length >= minChars || (force && blockBuffer.length > 0)) {
const breakIdx = pickBreakIndex(blockBuffer);
@@ -437,12 +442,30 @@ export function subscribeEmbeddedPiSession(params: {
evtType === "text_start" ||
evtType === "text_end"
) {
const chunk =
const delta =
typeof assistantRecord?.delta === "string"
? assistantRecord.delta
: typeof assistantRecord?.content === "string"
? assistantRecord.content
: "";
: "";
const content =
typeof assistantRecord?.content === "string"
? assistantRecord.content
: "";
let chunk = "";
if (evtType === "text_delta") {
chunk = delta;
} else if (evtType === "text_start" || evtType === "text_end") {
if (delta) {
chunk = delta;
} else if (content) {
if (content.startsWith(deltaBuffer)) {
chunk = content.slice(deltaBuffer.length);
} else if (deltaBuffer.startsWith(content)) {
chunk = "";
} else if (!deltaBuffer.includes(content)) {
chunk = content;
}
}
}
if (chunk) {
deltaBuffer += chunk;
blockBuffer += chunk;