Comparing version 2.1.14 to 2.1.15
@@ -247,17 +247,46 @@ "use strict"; | ||
// streams/cohere-stream.ts | ||
var utf8Decoder = new TextDecoder("utf-8"); | ||
function processLines(lines, controller) { | ||
return __async(this, null, function* () { | ||
for (const line of lines) { | ||
const { text, is_finished } = JSON.parse(line); | ||
if (is_finished === true) { | ||
controller.close(); | ||
} else { | ||
controller.enqueue(text); | ||
} | ||
} | ||
}); | ||
} | ||
function readAndProcessLines(reader, controller) { | ||
return __async(this, null, function* () { | ||
let segment = ""; | ||
while (true) { | ||
const { value: chunk, done } = yield reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
segment += utf8Decoder.decode(chunk, { stream: true }); | ||
const linesArray = segment.split(/\r\n|\n|\r/g); | ||
segment = linesArray.pop() || ""; | ||
yield processLines(linesArray, controller); | ||
} | ||
if (segment) { | ||
const linesArray = [segment]; | ||
yield processLines(linesArray, controller); | ||
} | ||
controller.close(); | ||
}); | ||
} | ||
function createParser3(res) { | ||
var _a; | ||
const reader = (_a = res.body) == null ? void 0 : _a.getReader(); | ||
return new ReadableStream({ | ||
pull(controller) { | ||
start(controller) { | ||
return __async(this, null, function* () { | ||
const { value, done } = yield res.next(); | ||
if (done) { | ||
if (!reader) { | ||
controller.close(); | ||
return; | ||
} | ||
const { text, is_finished } = JSON.parse(value); | ||
if (is_finished === true) { | ||
controller.close(); | ||
} else { | ||
controller.enqueue(text); | ||
} | ||
yield readAndProcessLines(reader, controller); | ||
}); | ||
@@ -267,4 +296,4 @@ } | ||
} | ||
function CohereStream(res, callbacks) { | ||
return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks)); | ||
function CohereStream(reader, callbacks) { | ||
return createParser3(reader).pipeThrough(createCallbacksTransformer(callbacks)); | ||
} | ||
@@ -271,0 +300,0 @@ |
@@ -94,3 +94,3 @@ import { ServerResponse } from 'node:http'; | ||
declare function CohereStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacks): ReadableStream; | ||
declare function CohereStream(reader: Response, callbacks?: AIStreamCallbacks): ReadableStream; | ||
@@ -97,0 +97,0 @@ declare function AnthropicStream(res: Response, cb?: AIStreamCallbacks): ReadableStream; |
@@ -247,17 +247,46 @@ "use strict"; | ||
// streams/cohere-stream.ts | ||
var utf8Decoder = new TextDecoder("utf-8"); | ||
function processLines(lines, controller) { | ||
return __async(this, null, function* () { | ||
for (const line of lines) { | ||
const { text, is_finished } = JSON.parse(line); | ||
if (is_finished === true) { | ||
controller.close(); | ||
} else { | ||
controller.enqueue(text); | ||
} | ||
} | ||
}); | ||
} | ||
function readAndProcessLines(reader, controller) { | ||
return __async(this, null, function* () { | ||
let segment = ""; | ||
while (true) { | ||
const { value: chunk, done } = yield reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
segment += utf8Decoder.decode(chunk, { stream: true }); | ||
const linesArray = segment.split(/\r\n|\n|\r/g); | ||
segment = linesArray.pop() || ""; | ||
yield processLines(linesArray, controller); | ||
} | ||
if (segment) { | ||
const linesArray = [segment]; | ||
yield processLines(linesArray, controller); | ||
} | ||
controller.close(); | ||
}); | ||
} | ||
function createParser3(res) { | ||
var _a; | ||
const reader = (_a = res.body) == null ? void 0 : _a.getReader(); | ||
return new ReadableStream({ | ||
pull(controller) { | ||
start(controller) { | ||
return __async(this, null, function* () { | ||
const { value, done } = yield res.next(); | ||
if (done) { | ||
if (!reader) { | ||
controller.close(); | ||
return; | ||
} | ||
const { text, is_finished } = JSON.parse(value); | ||
if (is_finished === true) { | ||
controller.close(); | ||
} else { | ||
controller.enqueue(text); | ||
} | ||
yield readAndProcessLines(reader, controller); | ||
}); | ||
@@ -267,4 +296,4 @@ } | ||
} | ||
function CohereStream(res, callbacks) { | ||
return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks)); | ||
function CohereStream(reader, callbacks) { | ||
return createParser3(reader).pipeThrough(createCallbacksTransformer(callbacks)); | ||
} | ||
@@ -271,0 +300,0 @@ |
{ | ||
"name": "ai", | ||
"version": "2.1.14", | ||
"version": "2.1.15", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
181844
5572