@axflow/models
Advanced tools
Comparing version 0.0.1-alpha.6 to 0.0.1-alpha.7
@@ -16,11 +16,16 @@ declare class HttpError extends Error { | ||
} | Array<JSONValueType>; | ||
type NdJsonValueType = { | ||
type: 'chunk' | 'data'; | ||
data: Record<string, JSONValueType>; | ||
}; | ||
declare class NdJsonStream { | ||
static headers: Readonly<{ | ||
'content-type': "application/x-ndjson"; | ||
'content-type': "application/x-ndjson; charset=utf-8"; | ||
}>; | ||
static from<T extends { | ||
[x: string]: JSONValueType; | ||
}>(stream: ReadableStream<T>, data?: Record<string, JSONValueType> | Record<string, JSONValueType>[]): ReadableStream<Uint8Array>; | ||
static encode<T extends Record<string, JSONValueType>>(stream: ReadableStream<T>, options?: { | ||
data?: Record<string, JSONValueType>[]; | ||
}): ReadableStream<Uint8Array>; | ||
static decode(stream: ReadableStream<Uint8Array>): ReadableStream<NdJsonValueType>; | ||
} | ||
export { HttpError, NdJsonStream, POST, StreamToIterable, isHttpError }; | ||
export { HttpError, NdJsonStream, NdJsonValueType, POST, StreamToIterable, isHttpError }; |
@@ -79,5 +79,5 @@ "use strict"; | ||
var NdJsonStream = class { | ||
static headers = Object.freeze({ "content-type": "application/x-ndjson" }); | ||
static headers = Object.freeze({ "content-type": "application/x-ndjson; charset=utf-8" }); | ||
/** | ||
* Converts a stream of JSON-serializable objects to newline-delimited JSON. | ||
* Transforms a stream of JSON-serializable objects to stream of newline-delimited JSON. | ||
* | ||
@@ -95,3 +95,3 @@ * Each object is wrapped with an object that specifies the `type` and references | ||
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }}); | ||
* const ndJsonStream = NdJsonStream.from(stream); | ||
* const ndJsonStream = NdJsonStream.encode(stream); | ||
* const entries = []; | ||
@@ -108,3 +108,3 @@ * for await (const chunk of stream) { | ||
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }}); | ||
* const ndJsonStream = NdJsonStream.from(stream, [{ extra: 'data' }]); | ||
* const ndJsonStream = NdJsonStream.encode(stream, { data: [{ extra: 'data' }] }); | ||
* const entries = []; | ||
@@ -118,6 +118,7 @@ * for await (const chunk of stream) { | ||
* @param stream A readable stream of JSON-serializable chunks to encode as ndjson | ||
* @param data Optional, additional data to prepend to the output stream | ||
* @param options | ||
* @param options.data Additional data to prepend to the output stream | ||
* @returns A readable stream of newline-delimited JSON | ||
*/ | ||
static from(stream, data) { | ||
static encode(stream, options) { | ||
const encoder = new TextEncoder(); | ||
@@ -131,11 +132,9 @@ function serialize(obj) { | ||
start(controller) { | ||
if (!data) { | ||
return; | ||
const data = options?.data || []; | ||
for (const value of data) { | ||
controller.enqueue(serialize({ type: "data", value })); | ||
} | ||
for (const entry of Array.isArray(data) ? data : [data]) { | ||
controller.enqueue(serialize({ type: "data", value: entry })); | ||
} | ||
}, | ||
transform(chunk, controller) { | ||
controller.enqueue(serialize({ type: "chunk", value: chunk })); | ||
transform(value, controller) { | ||
controller.enqueue(serialize({ type: "chunk", value })); | ||
} | ||
@@ -145,2 +144,28 @@ }); | ||
} | ||
/** | ||
* Transforms a stream of newline-delimited JSON to a stream of objects. | ||
* | ||
* @param stream A readable stream of newline-delimited JSON objects | ||
* @returns A readable stream of objects | ||
*/ | ||
static decode(stream) { | ||
let buffer = []; | ||
const decoder = new TextDecoder(); | ||
const parser = new TransformStream({ | ||
transform(bytes, controller) { | ||
const chunk = decoder.decode(bytes); | ||
for (let i = 0, len = chunk.length; i < len; ++i) { | ||
const isChunkSeparator = chunk[i] === "\n"; | ||
if (!isChunkSeparator) { | ||
buffer.push(chunk[i]); | ||
continue; | ||
} | ||
const line = buffer.join("").trimEnd(); | ||
controller.enqueue(JSON.parse(line)); | ||
buffer = []; | ||
} | ||
} | ||
}); | ||
return stream.pipeThrough(parser); | ||
} | ||
}; | ||
@@ -147,0 +172,0 @@ // Annotate the CommonJS export names for ESM import in node: |
{ | ||
"name": "@axflow/models", | ||
"version": "0.0.1-alpha.6", | ||
"version": "0.0.1-alpha.7", | ||
"description": "Zero-dependency module to run, stream, and render results across the most popular LLMs and embedding models", | ||
@@ -123,3 +123,3 @@ "author": "Axilla (https://axilla.io)", | ||
}, | ||
"gitHead": "b929430e216d68aa9367ed03acc3f0314890c72e" | ||
"gitHead": "78274e24cdccc818c27e4bffec566c943181aa1f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
76377
1673