UNPKG

openai

Version:

The official TypeScript library for the OpenAI API

27 lines • 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeChatCompletionChunk = normalizeChatCompletionChunk; exports.normalizeChatCompletionStream = normalizeChatCompletionStream; function normalizeChatCompletionChunk(chunk) { if (!Array.isArray(chunk?.choices)) { // SAFETY: This is an opaque provider record, with no choices to normalize. // Preserve the existing SSE pass-through; the SDK does not validate these records. return chunk; } return { ...chunk, choices: chunk.choices.map((choice) => ({ ...choice, finish_reason: choice.finish_reason ?? null })), }; } function normalizeChatCompletionStream(stream) { const iterator = stream[Symbol.asyncIterator].bind(stream); stream[Symbol.asyncIterator] = async function* normalizedChunks() { const source = { [Symbol.asyncIterator]: iterator }; for await (const chunk of source) { yield normalizeChatCompletionChunk(chunk); } }; // SAFETY: The installed iterator normalizes every wire chunk before exposing it. return stream; } //# sourceMappingURL=streaming.js.map