UNPKG

zon-format

Version:

ZON: The most token-efficient serialization format for LLMs - beats CSV, TOON, JSON, and all competitors

40 lines (39 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseZonStream = parseZonStream; exports.streamZon = streamZon; const stream_1 = require("../core/stream"); /** * Parses a stream of ZON text (e.g. from Vercel AI SDK's useChat) into objects. * * @param stream - The ReadableStream of text chunks. * @returns An AsyncGenerator yielding parsed objects. */ async function* parseZonStream(stream) { yield* streamZon(stream); } async function* streamToAsyncIterable(stream) { const reader = stream.getReader(); try { while (true) { const { done, value } = await reader.read(); if (done) break; if (value) yield value; } } finally { reader.releaseLock(); } } /** * Wrapper to use ZonStreamDecoder with a stream. */ function streamZon(stream) { const decoder = new stream_1.ZonStreamDecoder(); const iterator = stream[Symbol.asyncIterator] ? stream : streamToAsyncIterable(stream); return decoder.decode(iterator); }