fix: dedupe repeated block replies
This commit is contained in:
@@ -182,6 +182,55 @@ describe("subscribeEmbeddedPiSession", () => {
|
|||||||
expect(subscription.assistantTexts).toEqual(["Hello block"]);
|
expect(subscription.assistantTexts).toEqual(["Hello block"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not emit duplicate block replies when text_end repeats", () => {
|
||||||
|
let handler: ((evt: unknown) => void) | undefined;
|
||||||
|
const session: StubSession = {
|
||||||
|
subscribe: (fn) => {
|
||||||
|
handler = fn;
|
||||||
|
return () => {};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const onBlockReply = vi.fn();
|
||||||
|
|
||||||
|
const subscription = subscribeEmbeddedPiSession({
|
||||||
|
session: session as unknown as Parameters<
|
||||||
|
typeof subscribeEmbeddedPiSession
|
||||||
|
>[0]["session"],
|
||||||
|
runId: "run",
|
||||||
|
onBlockReply,
|
||||||
|
blockReplyBreak: "text_end",
|
||||||
|
});
|
||||||
|
|
||||||
|
handler?.({
|
||||||
|
type: "message_update",
|
||||||
|
message: { role: "assistant" },
|
||||||
|
assistantMessageEvent: {
|
||||||
|
type: "text_delta",
|
||||||
|
delta: "Hello block",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
handler?.({
|
||||||
|
type: "message_update",
|
||||||
|
message: { role: "assistant" },
|
||||||
|
assistantMessageEvent: {
|
||||||
|
type: "text_end",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
handler?.({
|
||||||
|
type: "message_update",
|
||||||
|
message: { role: "assistant" },
|
||||||
|
assistantMessageEvent: {
|
||||||
|
type: "text_end",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(onBlockReply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(subscription.assistantTexts).toEqual(["Hello block"]);
|
||||||
|
});
|
||||||
|
|
||||||
it("waits for auto-compaction retry and clears buffered text", async () => {
|
it("waits for auto-compaction retry and clears buffered text", async () => {
|
||||||
const listeners: SessionEventHandler[] = [];
|
const listeners: SessionEventHandler[] = [];
|
||||||
const session = {
|
const session = {
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
const blockReplyBreak = params.blockReplyBreak ?? "text_end";
|
const blockReplyBreak = params.blockReplyBreak ?? "text_end";
|
||||||
let deltaBuffer = "";
|
let deltaBuffer = "";
|
||||||
let lastStreamedAssistant: string | undefined;
|
let lastStreamedAssistant: string | undefined;
|
||||||
|
let lastBlockReplyText: string | undefined;
|
||||||
let assistantTextBaseline = 0;
|
let assistantTextBaseline = 0;
|
||||||
let compactionInFlight = false;
|
let compactionInFlight = false;
|
||||||
let pendingCompactionRetry = 0;
|
let pendingCompactionRetry = 0;
|
||||||
@@ -152,6 +153,7 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
toolMetaById.clear();
|
toolMetaById.clear();
|
||||||
deltaBuffer = "";
|
deltaBuffer = "";
|
||||||
lastStreamedAssistant = undefined;
|
lastStreamedAssistant = undefined;
|
||||||
|
lastBlockReplyText = undefined;
|
||||||
assistantTextBaseline = 0;
|
assistantTextBaseline = 0;
|
||||||
toolDebouncer.flush();
|
toolDebouncer.flush();
|
||||||
};
|
};
|
||||||
@@ -304,6 +306,12 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (evtType === "text_end" && blockReplyBreak === "text_end") {
|
if (evtType === "text_end" && blockReplyBreak === "text_end") {
|
||||||
|
if (next && next === lastBlockReplyText) {
|
||||||
|
deltaBuffer = "";
|
||||||
|
lastStreamedAssistant = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lastBlockReplyText = next || undefined;
|
||||||
if (next) assistantTexts.push(next);
|
if (next) assistantTexts.push(next);
|
||||||
if (next && params.onBlockReply) {
|
if (next && params.onBlockReply) {
|
||||||
const { text: cleanedText, mediaUrls } =
|
const { text: cleanedText, mediaUrls } =
|
||||||
@@ -349,6 +357,12 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
text &&
|
text &&
|
||||||
params.onBlockReply
|
params.onBlockReply
|
||||||
) {
|
) {
|
||||||
|
if (text === lastBlockReplyText) {
|
||||||
|
deltaBuffer = "";
|
||||||
|
lastStreamedAssistant = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lastBlockReplyText = text;
|
||||||
const { text: cleanedText, mediaUrls } = splitMediaFromOutput(text);
|
const { text: cleanedText, mediaUrls } = splitMediaFromOutput(text);
|
||||||
if (cleanedText || (mediaUrls && mediaUrls.length > 0)) {
|
if (cleanedText || (mediaUrls && mediaUrls.length > 0)) {
|
||||||
void params.onBlockReply({
|
void params.onBlockReply({
|
||||||
@@ -359,6 +373,7 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
}
|
}
|
||||||
deltaBuffer = "";
|
deltaBuffer = "";
|
||||||
lastStreamedAssistant = undefined;
|
lastStreamedAssistant = undefined;
|
||||||
|
lastBlockReplyText = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user