ai 2.1.14 → 2.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +40 -11
- package/dist/index.mjs +40 -11
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
@@ -91,7 +91,7 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
|
|
91
91
|
|
92
92
|
declare function HuggingFaceStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacks): ReadableStream;
|
93
93
|
|
94
|
-
declare function CohereStream(
|
94
|
+
declare function CohereStream(reader: Response, callbacks?: AIStreamCallbacks): ReadableStream;
|
95
95
|
|
96
96
|
declare function AnthropicStream(res: Response, cb?: AIStreamCallbacks): ReadableStream;
|
97
97
|
|
package/dist/index.js
CHANGED
@@ -244,27 +244,56 @@ function HuggingFaceStream(res, callbacks) {
|
|
244
244
|
}
|
245
245
|
|
246
246
|
// streams/cohere-stream.ts
|
247
|
+
var utf8Decoder = new TextDecoder("utf-8");
|
248
|
+
function processLines(lines, controller) {
|
249
|
+
return __async(this, null, function* () {
|
250
|
+
for (const line of lines) {
|
251
|
+
const { text, is_finished } = JSON.parse(line);
|
252
|
+
if (is_finished === true) {
|
253
|
+
controller.close();
|
254
|
+
} else {
|
255
|
+
controller.enqueue(text);
|
256
|
+
}
|
257
|
+
}
|
258
|
+
});
|
259
|
+
}
|
260
|
+
function readAndProcessLines(reader, controller) {
|
261
|
+
return __async(this, null, function* () {
|
262
|
+
let segment = "";
|
263
|
+
while (true) {
|
264
|
+
const { value: chunk, done } = yield reader.read();
|
265
|
+
if (done) {
|
266
|
+
break;
|
267
|
+
}
|
268
|
+
segment += utf8Decoder.decode(chunk, { stream: true });
|
269
|
+
const linesArray = segment.split(/\r\n|\n|\r/g);
|
270
|
+
segment = linesArray.pop() || "";
|
271
|
+
yield processLines(linesArray, controller);
|
272
|
+
}
|
273
|
+
if (segment) {
|
274
|
+
const linesArray = [segment];
|
275
|
+
yield processLines(linesArray, controller);
|
276
|
+
}
|
277
|
+
controller.close();
|
278
|
+
});
|
279
|
+
}
|
247
280
|
function createParser3(res) {
|
281
|
+
var _a;
|
282
|
+
const reader = (_a = res.body) == null ? void 0 : _a.getReader();
|
248
283
|
return new ReadableStream({
|
249
|
-
|
284
|
+
start(controller) {
|
250
285
|
return __async(this, null, function* () {
|
251
|
-
|
252
|
-
if (done) {
|
286
|
+
if (!reader) {
|
253
287
|
controller.close();
|
254
288
|
return;
|
255
289
|
}
|
256
|
-
|
257
|
-
if (is_finished === true) {
|
258
|
-
controller.close();
|
259
|
-
} else {
|
260
|
-
controller.enqueue(text);
|
261
|
-
}
|
290
|
+
yield readAndProcessLines(reader, controller);
|
262
291
|
});
|
263
292
|
}
|
264
293
|
});
|
265
294
|
}
|
266
|
-
function CohereStream(
|
267
|
-
return createParser3(
|
295
|
+
function CohereStream(reader, callbacks) {
|
296
|
+
return createParser3(reader).pipeThrough(createCallbacksTransformer(callbacks));
|
268
297
|
}
|
269
298
|
|
270
299
|
// streams/anthropic-stream.ts
|
package/dist/index.mjs
CHANGED
@@ -211,27 +211,56 @@ function HuggingFaceStream(res, callbacks) {
|
|
211
211
|
}
|
212
212
|
|
213
213
|
// streams/cohere-stream.ts
|
214
|
+
var utf8Decoder = new TextDecoder("utf-8");
|
215
|
+
function processLines(lines, controller) {
|
216
|
+
return __async(this, null, function* () {
|
217
|
+
for (const line of lines) {
|
218
|
+
const { text, is_finished } = JSON.parse(line);
|
219
|
+
if (is_finished === true) {
|
220
|
+
controller.close();
|
221
|
+
} else {
|
222
|
+
controller.enqueue(text);
|
223
|
+
}
|
224
|
+
}
|
225
|
+
});
|
226
|
+
}
|
227
|
+
function readAndProcessLines(reader, controller) {
|
228
|
+
return __async(this, null, function* () {
|
229
|
+
let segment = "";
|
230
|
+
while (true) {
|
231
|
+
const { value: chunk, done } = yield reader.read();
|
232
|
+
if (done) {
|
233
|
+
break;
|
234
|
+
}
|
235
|
+
segment += utf8Decoder.decode(chunk, { stream: true });
|
236
|
+
const linesArray = segment.split(/\r\n|\n|\r/g);
|
237
|
+
segment = linesArray.pop() || "";
|
238
|
+
yield processLines(linesArray, controller);
|
239
|
+
}
|
240
|
+
if (segment) {
|
241
|
+
const linesArray = [segment];
|
242
|
+
yield processLines(linesArray, controller);
|
243
|
+
}
|
244
|
+
controller.close();
|
245
|
+
});
|
246
|
+
}
|
214
247
|
function createParser3(res) {
|
248
|
+
var _a;
|
249
|
+
const reader = (_a = res.body) == null ? void 0 : _a.getReader();
|
215
250
|
return new ReadableStream({
|
216
|
-
|
251
|
+
start(controller) {
|
217
252
|
return __async(this, null, function* () {
|
218
|
-
|
219
|
-
if (done) {
|
253
|
+
if (!reader) {
|
220
254
|
controller.close();
|
221
255
|
return;
|
222
256
|
}
|
223
|
-
|
224
|
-
if (is_finished === true) {
|
225
|
-
controller.close();
|
226
|
-
} else {
|
227
|
-
controller.enqueue(text);
|
228
|
-
}
|
257
|
+
yield readAndProcessLines(reader, controller);
|
229
258
|
});
|
230
259
|
}
|
231
260
|
});
|
232
261
|
}
|
233
|
-
function CohereStream(
|
234
|
-
return createParser3(
|
262
|
+
function CohereStream(reader, callbacks) {
|
263
|
+
return createParser3(reader).pipeThrough(createCallbacksTransformer(callbacks));
|
235
264
|
}
|
236
265
|
|
237
266
|
// streams/anthropic-stream.ts
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ai",
|
3
|
-
"version": "2.1.
|
3
|
+
"version": "2.1.16",
|
4
4
|
"license": "Apache-2.0",
|
5
5
|
"sideEffects": false,
|
6
6
|
"main": "./dist/index.js",
|
@@ -68,7 +68,7 @@
|
|
68
68
|
},
|
69
69
|
"peerDependencies": {
|
70
70
|
"react": "^18.2.0",
|
71
|
-
"svelte": "^4.0.0",
|
71
|
+
"svelte": "^3.0.0 || ^4.0.0",
|
72
72
|
"vue": "^3.3.4"
|
73
73
|
},
|
74
74
|
"peerDependenciesMeta": {
|