fix: ensure output for non-streaming models (#369)

Co-authored-by: mneves75 <mneves75@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-07 07:47:18 +00:00
parent 34cac1beb0
commit e5dbe1db9d
3 changed files with 39 additions and 2 deletions

View File

@@ -400,6 +400,40 @@ describe("subscribeEmbeddedPiSession", () => {
expect(subscription.assistantTexts).toEqual(["Hello world"]);
});
it("populates assistantTexts for non-streaming models with chunking enabled", () => {
// Non-streaming models (e.g. zai/glm-4.7): no text_delta events; message_end
// must still populate assistantTexts so providers can deliver a final reply.
let handler: SessionEventHandler | undefined;
const session: StubSession = {
subscribe: (fn) => {
handler = fn;
return () => {};
},
};
const subscription = subscribeEmbeddedPiSession({
session: session as unknown as Parameters<
typeof subscribeEmbeddedPiSession
>[0]["session"],
runId: "run",
blockReplyChunking: { minChars: 50, maxChars: 200 }, // Chunking enabled
});
// Simulate non-streaming model: only message_start and message_end, no text_delta
handler?.({ type: "message_start", message: { role: "assistant" } });
const assistantMessage = {
role: "assistant",
content: [{ type: "text", text: "Response from non-streaming model" }],
} as AssistantMessage;
handler?.({ type: "message_end", message: assistantMessage });
expect(subscription.assistantTexts).toEqual([
"Response from non-streaming model",
]);
});
it("does not append when text_end content is a prefix of deltas", () => {
let handler: ((evt: unknown) => void) | undefined;
const session: StubSession = {

View File

@@ -516,8 +516,10 @@ export function subscribeEmbeddedPiSession(params: {
const addedDuringMessage =
assistantTexts.length > assistantTextBaseline;
const chunkingEnabled = Boolean(blockChunking);
if (!chunkingEnabled && !addedDuringMessage && text) {
const chunkerHasBuffered = blockChunker?.hasBuffered() ?? false;
// Non-streaming models (no text_delta): ensure assistantTexts gets the
// final text when the chunker has nothing buffered to drain.
if (!addedDuringMessage && !chunkerHasBuffered && text) {
const last = assistantTexts.at(-1);
if (!last || last !== text) assistantTexts.push(text);
}