Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(sdk): emit the final SSE event when the stream has no trailing newline (#3581)
Shubham Srivastavaebc0b363e40e
diff --git a/sdk/typescript/src/utils/stream.ts b/sdk/typescript/src/utils/stream.tsindex c8a0bd0aa..7d465ff7f 100644--- a/sdk/typescript/src/utils/stream.ts+++ b/sdk/typescript/src/utils/stream.ts@@ -15,11 +15,13 @@ export async function* parseSSE<T = any>(try {while (true) {const { done, value } = await reader.read();- if (done) break;-- buffer += decoder.decode(value, { stream: true });+ // On the final read, flush any buffered multi-byte remainder and process+ // every remaining line — including a last `data:` line that arrived+ // without a trailing newline, which would otherwise be held back in+ // `buffer` and dropped when the loop exits.+ buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });const lines = buffer.split("\n");- buffer = lines.pop()!;+ buffer = done ? "" : lines.pop()!;for (const line of lines) {if (line.startsWith("data: ")) {@@ -32,6 +34,8 @@ export async function* parseSSE<T = any>(}}}++ if (done) break;}} finally {reader.releaseLock();diff --git a/sdk/typescript/test/utils/stream.test.ts b/sdk/typescript/test/utils/stream.test.tsnew file mode 100644index 000000000..13d580d4e--- /dev/null+++ b/sdk/typescript/test/utils/stream.test.ts@@ -0,0 +1,72 @@+/**+ * Tests for the SSE stream parser.+ */+import { describe, it, expect } from "vitest";+import { parseSSE, collectStream } from "../../src/utils/stream.js";++/** Build a Response whose body streams the given raw string chunks verbatim. */+function sseResponse(chunks: string[]): Response {+ const encoder = new TextEncoder();+ const stream = new ReadableStream<Uint8Array>({+ start(controller) {+ for (const chunk of chunks) controller.enqueue(encoder.encode(chunk));+ controller.close();+ },+ });+ return new Response(stream);+}++describe("parseSSE", () => {+ it("yields the final event when the stream has no trailing newline", async () => {+ // Regression: the last `data:` line was held back in the buffer and dropped+ // when the read loop exited on `done`.+ const events = await collectStream(+ parseSSE(sseResponse(['data: {"a":1}\n', 'data: {"b":2}'])),+ );+ expect(events).toEqual([{ a: 1 }, { b: 2 }]);+ });++ it("parses multiple newline-terminated events", async () => {+ const events = await collectStream(+ parseSSE(sseResponse(['data: {"a":1}\n', 'data: {"b":2}\n'])),+ );+ expect(events).toEqual([{ a: 1 }, { b: 2 }]);+ });++ it("reassembles an event split across chunk boundaries", async () => {+ const events = await collectStream(+ parseSSE(sseResponse(['data: {"a"', ":1}\n"])),+ );+ expect(events).toEqual([{ a: 1 }]);+ });++ it("stops at the [DONE] sentinel and ignores anything after it", async () => {+ const events = await collectStream(+ parseSSE(sseResponse(['data: {"a":1}\n', "data: [DONE]\n", 'data: {"b":2}\n'])),+ );+ expect(events).toEqual([{ a: 1 }]);+ });++ it("skips non-JSON data lines", async () => {+ const events = await collectStream(+ parseSSE(sseResponse(["event: ping\n", "data: not-json\n", 'data: {"a":1}\n'])),+ );+ expect(events).toEqual([{ a: 1 }]);+ });++ it("decodes a multi-byte character split across chunk boundaries", async () => {+ // "é" is 0xC3 0xA9; split the two bytes across reads so the flush matters.+ const encoder = new TextEncoder();+ const bytes = encoder.encode('data: {"s":"é"}');+ const stream = new ReadableStream<Uint8Array>({+ start(controller) {+ const split = bytes.indexOf(0xa9);+ controller.enqueue(bytes.slice(0, split));+ controller.enqueue(bytes.slice(split));+ controller.close();+ },+ });+ const events = await collectStream(parseSSE(new Response(stream)));+ expect(events).toEqual([{ s: "é" }]);+ });+});