modelfusion 0.33.0 → 0.34.0
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/core/api/postToApi.d.ts +1 -1
- package/{model-function/generate-text → event-source}/AsyncQueue.cjs +11 -10
- package/event-source/AsyncQueue.d.ts +8 -0
- package/{model-function/generate-text → event-source}/AsyncQueue.js +11 -10
- package/event-source/EventSourceParserStream.cjs +34 -0
- package/event-source/EventSourceParserStream.d.ts +15 -0
- package/event-source/EventSourceParserStream.js +30 -0
- package/event-source/convertReadableStreamToAsyncIterable.cjs +19 -0
- package/event-source/convertReadableStreamToAsyncIterable.d.ts +1 -0
- package/event-source/convertReadableStreamToAsyncIterable.js +15 -0
- package/event-source/createEventSourceStream.cjs +15 -0
- package/event-source/createEventSourceStream.d.ts +1 -0
- package/event-source/createEventSourceStream.js +11 -0
- package/event-source/index.cjs +19 -0
- package/event-source/index.d.ts +3 -0
- package/event-source/index.js +3 -0
- package/event-source/parseEventSourceStream.cjs +12 -0
- package/event-source/parseEventSourceStream.d.ts +4 -0
- package/event-source/parseEventSourceStream.js +8 -0
- package/event-source/readEventSourceStream.cjs +33 -0
- package/event-source/readEventSourceStream.d.ts +6 -0
- package/event-source/readEventSourceStream.js +26 -0
- package/index.cjs +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/model-function/index.cjs +0 -1
- package/model-function/index.d.ts +0 -1
- package/model-function/index.js +0 -1
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +1 -1
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +1 -1
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.js +1 -1
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +21 -21
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +21 -21
- package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.cjs +25 -23
- package/model-provider/openai/OpenAITextGenerationModel.js +25 -23
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +27 -24
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +27 -24
- package/package.json +5 -5
- package/model-function/generate-text/AsyncQueue.d.ts +0 -17
- package/model-function/generate-text/TextDeltaEventSource.cjs +0 -54
- package/model-function/generate-text/TextDeltaEventSource.d.ts +0 -5
- package/model-function/generate-text/TextDeltaEventSource.js +0 -46
- package/model-function/generate-text/parseEventSourceReadableStream.cjs +0 -30
- package/model-function/generate-text/parseEventSourceReadableStream.d.ts +0 -8
- package/model-function/generate-text/parseEventSourceReadableStream.js +0 -26
@@ -1,8 +1,8 @@
|
|
1
1
|
import SecureJSON from "secure-json-parse";
|
2
2
|
import z from "zod";
|
3
3
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
4
|
-
import { AsyncQueue } from "../../
|
5
|
-
import {
|
4
|
+
import { AsyncQueue } from "../../event-source/AsyncQueue.js";
|
5
|
+
import { parseEventSourceStream } from "../../event-source/parseEventSourceStream.js";
|
6
6
|
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
7
7
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
8
8
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
@@ -208,14 +208,11 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
|
|
208
208
|
const queue = new AsyncQueue();
|
209
209
|
let content = "";
|
210
210
|
// process the stream asynchonously (no 'await' on purpose):
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
}
|
217
|
-
const data = event.data;
|
218
|
-
try {
|
211
|
+
parseEventSourceStream({ stream })
|
212
|
+
.then(async (events) => {
|
213
|
+
try {
|
214
|
+
for await (const event of events) {
|
215
|
+
const data = event.data;
|
219
216
|
const json = SecureJSON.parse(data);
|
220
217
|
const parseResult = llamaCppTextStreamingResponseSchema.safeParse(json);
|
221
218
|
if (!parseResult.success) {
|
@@ -226,26 +223,29 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
|
|
226
223
|
queue.close();
|
227
224
|
return;
|
228
225
|
}
|
229
|
-
const
|
230
|
-
content +=
|
226
|
+
const eventData = parseResult.data;
|
227
|
+
content += eventData.content;
|
231
228
|
queue.push({
|
232
229
|
type: "delta",
|
233
230
|
fullDelta: {
|
234
231
|
content,
|
235
|
-
isComplete:
|
236
|
-
delta:
|
232
|
+
isComplete: eventData.stop,
|
233
|
+
delta: eventData.content,
|
237
234
|
},
|
238
235
|
});
|
239
|
-
if (
|
236
|
+
if (eventData.stop) {
|
240
237
|
queue.close();
|
241
238
|
}
|
242
239
|
}
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
240
|
+
}
|
241
|
+
catch (error) {
|
242
|
+
queue.push({ type: "error", error });
|
243
|
+
queue.close();
|
244
|
+
}
|
245
|
+
})
|
246
|
+
.catch((error) => {
|
247
|
+
queue.push({ type: "error", error });
|
248
|
+
queue.close();
|
249
249
|
});
|
250
250
|
return queue;
|
251
251
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
/// <reference types="node"
|
1
|
+
/// <reference types="node" />
|
2
2
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
3
3
|
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
4
4
|
import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
|
@@ -7,8 +7,8 @@ exports.OpenAITextResponseFormat = exports.OpenAITextGenerationModel = exports.c
|
|
7
7
|
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
8
|
const zod_1 = __importDefault(require("zod"));
|
9
9
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
10
|
-
const AsyncQueue_js_1 = require("../../
|
11
|
-
const
|
10
|
+
const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
|
11
|
+
const parseEventSourceStream_js_1 = require("../../event-source/parseEventSourceStream.cjs");
|
12
12
|
const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
|
13
13
|
const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
|
14
14
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
@@ -335,18 +335,15 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
335
335
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
336
336
|
const streamDelta = [];
|
337
337
|
// process the stream asynchonously (no 'await' on purpose):
|
338
|
-
(0,
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
return;
|
348
|
-
}
|
349
|
-
try {
|
338
|
+
(0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
|
339
|
+
.then(async (events) => {
|
340
|
+
try {
|
341
|
+
for await (const event of events) {
|
342
|
+
const data = event.data;
|
343
|
+
if (data === "[DONE]") {
|
344
|
+
queue.close();
|
345
|
+
return;
|
346
|
+
}
|
350
347
|
const json = secure_json_parse_1.default.parse(data);
|
351
348
|
const parseResult = textResponseStreamEventSchema.safeParse(json);
|
352
349
|
if (!parseResult.success) {
|
@@ -357,9 +354,9 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
357
354
|
queue.close();
|
358
355
|
return;
|
359
356
|
}
|
360
|
-
const
|
361
|
-
for (let i = 0; i <
|
362
|
-
const eventChoice =
|
357
|
+
const eventData = parseResult.data;
|
358
|
+
for (let i = 0; i < eventData.choices.length; i++) {
|
359
|
+
const eventChoice = eventData.choices[i];
|
363
360
|
const delta = eventChoice.text;
|
364
361
|
if (streamDelta[i] == null) {
|
365
362
|
streamDelta[i] = {
|
@@ -383,12 +380,17 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
383
380
|
fullDelta: streamDeltaDeepCopy,
|
384
381
|
});
|
385
382
|
}
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
}
|
383
|
+
}
|
384
|
+
catch (error) {
|
385
|
+
queue.push({ type: "error", error });
|
386
|
+
queue.close();
|
387
|
+
return;
|
388
|
+
}
|
389
|
+
})
|
390
|
+
.catch((error) => {
|
391
|
+
queue.push({ type: "error", error });
|
392
|
+
queue.close();
|
393
|
+
return;
|
392
394
|
});
|
393
395
|
return queue;
|
394
396
|
}
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import SecureJSON from "secure-json-parse";
|
2
2
|
import z from "zod";
|
3
3
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
4
|
-
import { AsyncQueue } from "../../
|
5
|
-
import {
|
4
|
+
import { AsyncQueue } from "../../event-source/AsyncQueue.js";
|
5
|
+
import { parseEventSourceStream } from "../../event-source/parseEventSourceStream.js";
|
6
6
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
7
7
|
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
8
8
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
@@ -325,18 +325,15 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
325
325
|
const queue = new AsyncQueue();
|
326
326
|
const streamDelta = [];
|
327
327
|
// process the stream asynchonously (no 'await' on purpose):
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
return;
|
338
|
-
}
|
339
|
-
try {
|
328
|
+
parseEventSourceStream({ stream })
|
329
|
+
.then(async (events) => {
|
330
|
+
try {
|
331
|
+
for await (const event of events) {
|
332
|
+
const data = event.data;
|
333
|
+
if (data === "[DONE]") {
|
334
|
+
queue.close();
|
335
|
+
return;
|
336
|
+
}
|
340
337
|
const json = SecureJSON.parse(data);
|
341
338
|
const parseResult = textResponseStreamEventSchema.safeParse(json);
|
342
339
|
if (!parseResult.success) {
|
@@ -347,9 +344,9 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
347
344
|
queue.close();
|
348
345
|
return;
|
349
346
|
}
|
350
|
-
const
|
351
|
-
for (let i = 0; i <
|
352
|
-
const eventChoice =
|
347
|
+
const eventData = parseResult.data;
|
348
|
+
for (let i = 0; i < eventData.choices.length; i++) {
|
349
|
+
const eventChoice = eventData.choices[i];
|
353
350
|
const delta = eventChoice.text;
|
354
351
|
if (streamDelta[i] == null) {
|
355
352
|
streamDelta[i] = {
|
@@ -373,12 +370,17 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
373
370
|
fullDelta: streamDeltaDeepCopy,
|
374
371
|
});
|
375
372
|
}
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
}
|
373
|
+
}
|
374
|
+
catch (error) {
|
375
|
+
queue.push({ type: "error", error });
|
376
|
+
queue.close();
|
377
|
+
return;
|
378
|
+
}
|
379
|
+
})
|
380
|
+
.catch((error) => {
|
381
|
+
queue.push({ type: "error", error });
|
382
|
+
queue.close();
|
383
|
+
return;
|
382
384
|
});
|
383
385
|
return queue;
|
384
386
|
}
|
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createOpenAIChatFullDeltaIterableQueue = void 0;
|
7
7
|
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
8
|
const zod_1 = require("zod");
|
9
|
-
const AsyncQueue_js_1 = require("../../../
|
10
|
-
const
|
9
|
+
const AsyncQueue_js_1 = require("../../../event-source/AsyncQueue.cjs");
|
10
|
+
const parseEventSourceStream_js_1 = require("../../../event-source/parseEventSourceStream.cjs");
|
11
11
|
const chatResponseStreamEventSchema = zod_1.z.object({
|
12
12
|
choices: zod_1.z.array(zod_1.z.object({
|
13
13
|
delta: zod_1.z.object({
|
@@ -32,18 +32,15 @@ async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
32
32
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
33
33
|
const streamDelta = [];
|
34
34
|
// process the stream asynchonously (no 'await' on purpose):
|
35
|
-
(0,
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
return;
|
45
|
-
}
|
46
|
-
try {
|
35
|
+
(0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
|
36
|
+
.then(async (events) => {
|
37
|
+
try {
|
38
|
+
for await (const event of events) {
|
39
|
+
const data = event.data;
|
40
|
+
if (data === "[DONE]") {
|
41
|
+
queue.close();
|
42
|
+
return;
|
43
|
+
}
|
47
44
|
const json = secure_json_parse_1.default.parse(data);
|
48
45
|
const parseResult = chatResponseStreamEventSchema.safeParse(json);
|
49
46
|
if (!parseResult.success) {
|
@@ -54,9 +51,9 @@ async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
54
51
|
queue.close();
|
55
52
|
return;
|
56
53
|
}
|
57
|
-
const
|
58
|
-
for (let i = 0; i <
|
59
|
-
const eventChoice =
|
54
|
+
const eventData = parseResult.data;
|
55
|
+
for (let i = 0; i < eventData.choices.length; i++) {
|
56
|
+
const eventChoice = eventData.choices[i];
|
60
57
|
const delta = eventChoice.delta;
|
61
58
|
if (streamDelta[i] == null) {
|
62
59
|
streamDelta[i] = {
|
@@ -85,7 +82,8 @@ async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
85
82
|
choice.function_call.name += delta.function_call.name;
|
86
83
|
}
|
87
84
|
if (delta.function_call.arguments != undefined) {
|
88
|
-
choice.function_call.arguments +=
|
85
|
+
choice.function_call.arguments +=
|
86
|
+
delta.function_call.arguments;
|
89
87
|
}
|
90
88
|
}
|
91
89
|
if (delta.role != undefined) {
|
@@ -100,12 +98,17 @@ async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
100
98
|
fullDelta: streamDeltaDeepCopy,
|
101
99
|
});
|
102
100
|
}
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
}
|
101
|
+
}
|
102
|
+
catch (error) {
|
103
|
+
queue.push({ type: "error", error });
|
104
|
+
queue.close();
|
105
|
+
return;
|
106
|
+
}
|
107
|
+
})
|
108
|
+
.catch((error) => {
|
109
|
+
queue.push({ type: "error", error });
|
110
|
+
queue.close();
|
111
|
+
return;
|
109
112
|
});
|
110
113
|
return queue;
|
111
114
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import SecureJSON from "secure-json-parse";
|
2
2
|
import { z } from "zod";
|
3
|
-
import { AsyncQueue } from "../../../
|
4
|
-
import {
|
3
|
+
import { AsyncQueue } from "../../../event-source/AsyncQueue.js";
|
4
|
+
import { parseEventSourceStream } from "../../../event-source/parseEventSourceStream.js";
|
5
5
|
const chatResponseStreamEventSchema = z.object({
|
6
6
|
choices: z.array(z.object({
|
7
7
|
delta: z.object({
|
@@ -26,18 +26,15 @@ export async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
26
26
|
const queue = new AsyncQueue();
|
27
27
|
const streamDelta = [];
|
28
28
|
// process the stream asynchonously (no 'await' on purpose):
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
return;
|
39
|
-
}
|
40
|
-
try {
|
29
|
+
parseEventSourceStream({ stream })
|
30
|
+
.then(async (events) => {
|
31
|
+
try {
|
32
|
+
for await (const event of events) {
|
33
|
+
const data = event.data;
|
34
|
+
if (data === "[DONE]") {
|
35
|
+
queue.close();
|
36
|
+
return;
|
37
|
+
}
|
41
38
|
const json = SecureJSON.parse(data);
|
42
39
|
const parseResult = chatResponseStreamEventSchema.safeParse(json);
|
43
40
|
if (!parseResult.success) {
|
@@ -48,9 +45,9 @@ export async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
48
45
|
queue.close();
|
49
46
|
return;
|
50
47
|
}
|
51
|
-
const
|
52
|
-
for (let i = 0; i <
|
53
|
-
const eventChoice =
|
48
|
+
const eventData = parseResult.data;
|
49
|
+
for (let i = 0; i < eventData.choices.length; i++) {
|
50
|
+
const eventChoice = eventData.choices[i];
|
54
51
|
const delta = eventChoice.delta;
|
55
52
|
if (streamDelta[i] == null) {
|
56
53
|
streamDelta[i] = {
|
@@ -79,7 +76,8 @@ export async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
79
76
|
choice.function_call.name += delta.function_call.name;
|
80
77
|
}
|
81
78
|
if (delta.function_call.arguments != undefined) {
|
82
|
-
choice.function_call.arguments +=
|
79
|
+
choice.function_call.arguments +=
|
80
|
+
delta.function_call.arguments;
|
83
81
|
}
|
84
82
|
}
|
85
83
|
if (delta.role != undefined) {
|
@@ -94,12 +92,17 @@ export async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
|
94
92
|
fullDelta: streamDeltaDeepCopy,
|
95
93
|
});
|
96
94
|
}
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
}
|
95
|
+
}
|
96
|
+
catch (error) {
|
97
|
+
queue.push({ type: "error", error });
|
98
|
+
queue.close();
|
99
|
+
return;
|
100
|
+
}
|
101
|
+
})
|
102
|
+
.catch((error) => {
|
103
|
+
queue.push({ type: "error", error });
|
104
|
+
queue.close();
|
105
|
+
return;
|
103
106
|
});
|
104
107
|
return queue;
|
105
108
|
}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
3
|
"description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.34.0",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -51,7 +51,7 @@
|
|
51
51
|
"dist": "npm run clean && npm run lint && npm run build && npm run dist:copy-files"
|
52
52
|
},
|
53
53
|
"dependencies": {
|
54
|
-
"eventsource-parser": "1.
|
54
|
+
"eventsource-parser": "1.1.1",
|
55
55
|
"js-tiktoken": "1.0.7",
|
56
56
|
"nanoid": "3.3.6",
|
57
57
|
"secure-json-parse": "2.7.0",
|
@@ -60,7 +60,7 @@
|
|
60
60
|
},
|
61
61
|
"devDependencies": {
|
62
62
|
"@pinecone-database/pinecone": "^0.1.6",
|
63
|
-
"@tsconfig/recommended": "1.0.
|
63
|
+
"@tsconfig/recommended": "1.0.3",
|
64
64
|
"@types/node": "18.11.9",
|
65
65
|
"@typescript-eslint/eslint-plugin": "^6.1.0",
|
66
66
|
"@typescript-eslint/parser": "^6.1.0",
|
@@ -71,10 +71,10 @@
|
|
71
71
|
"lint-staged": "14.0.1",
|
72
72
|
"prettier": "3.0.3",
|
73
73
|
"rimraf": "5.0.1",
|
74
|
-
"typescript": "5.
|
74
|
+
"typescript": "5.2.2"
|
75
75
|
},
|
76
76
|
"peerDependencies": {
|
77
|
-
"@pinecone-database/pinecone": "
|
77
|
+
"@pinecone-database/pinecone": "0.1.6"
|
78
78
|
},
|
79
79
|
"peerDependenciesMeta": {
|
80
80
|
"@pinecone-database/pinecone": {
|
@@ -1,17 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @internal
|
3
|
-
*/
|
4
|
-
export declare class AsyncQueue<T> implements AsyncIterable<T | undefined> {
|
5
|
-
queue: T[];
|
6
|
-
resolvers: Array<(options: {
|
7
|
-
value: T | undefined;
|
8
|
-
done: boolean;
|
9
|
-
}) => void>;
|
10
|
-
closed: boolean;
|
11
|
-
constructor();
|
12
|
-
push(value: T): void;
|
13
|
-
close(): void;
|
14
|
-
[Symbol.asyncIterator](): {
|
15
|
-
next: () => Promise<IteratorResult<T | undefined, T | undefined>>;
|
16
|
-
};
|
17
|
-
}
|
@@ -1,54 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.parseTextDeltaEventSource = exports.createTextDeltaEventSource = void 0;
|
7
|
-
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
|
-
const zod_1 = require("zod");
|
9
|
-
const AsyncQueue_js_1 = require("./AsyncQueue.cjs");
|
10
|
-
const parseEventSourceReadableStream_js_1 = require("./parseEventSourceReadableStream.cjs");
|
11
|
-
const textEncoder = new TextEncoder();
|
12
|
-
const textDeltaEventDataSchema = zod_1.z.object({
|
13
|
-
textDelta: zod_1.z.string().optional(),
|
14
|
-
isFinished: zod_1.z.boolean(),
|
15
|
-
});
|
16
|
-
function enqueueData(controller, data) {
|
17
|
-
controller.enqueue(textEncoder.encode(`data: ${JSON.stringify(data)}\n\n`));
|
18
|
-
}
|
19
|
-
function createTextDeltaEventSource(textDeltas) {
|
20
|
-
return new ReadableStream({
|
21
|
-
async start(controller) {
|
22
|
-
for await (const textDelta of textDeltas) {
|
23
|
-
enqueueData(controller, { textDelta, isFinished: false });
|
24
|
-
}
|
25
|
-
enqueueData(controller, { isFinished: true });
|
26
|
-
},
|
27
|
-
});
|
28
|
-
}
|
29
|
-
exports.createTextDeltaEventSource = createTextDeltaEventSource;
|
30
|
-
function parseTextDeltaEventSource(stream, options) {
|
31
|
-
const queue = new AsyncQueue_js_1.AsyncQueue();
|
32
|
-
// run async (no await on purpose):
|
33
|
-
(0, parseEventSourceReadableStream_js_1.parseEventSourceReadableStream)({
|
34
|
-
stream,
|
35
|
-
callback: (event) => {
|
36
|
-
if (event.type !== "event") {
|
37
|
-
return;
|
38
|
-
}
|
39
|
-
try {
|
40
|
-
const data = textDeltaEventDataSchema.parse(secure_json_parse_1.default.parse(event.data));
|
41
|
-
queue.push(data.textDelta);
|
42
|
-
if (data.isFinished) {
|
43
|
-
queue.close();
|
44
|
-
}
|
45
|
-
}
|
46
|
-
catch (error) {
|
47
|
-
options?.errorHandler(error);
|
48
|
-
queue.close();
|
49
|
-
}
|
50
|
-
},
|
51
|
-
});
|
52
|
-
return queue;
|
53
|
-
}
|
54
|
-
exports.parseTextDeltaEventSource = parseTextDeltaEventSource;
|
@@ -1,5 +0,0 @@
|
|
1
|
-
import { ErrorHandler } from "../../util/ErrorHandler.js";
|
2
|
-
export declare function createTextDeltaEventSource(textDeltas: AsyncIterable<string>): ReadableStream<any>;
|
3
|
-
export declare function parseTextDeltaEventSource(stream: ReadableStream<Uint8Array>, options?: {
|
4
|
-
errorHandler: ErrorHandler;
|
5
|
-
}): AsyncIterable<string | undefined>;
|
@@ -1,46 +0,0 @@
|
|
1
|
-
import SecureJSON from "secure-json-parse";
|
2
|
-
import { z } from "zod";
|
3
|
-
import { AsyncQueue } from "./AsyncQueue.js";
|
4
|
-
import { parseEventSourceReadableStream } from "./parseEventSourceReadableStream.js";
|
5
|
-
const textEncoder = new TextEncoder();
|
6
|
-
const textDeltaEventDataSchema = z.object({
|
7
|
-
textDelta: z.string().optional(),
|
8
|
-
isFinished: z.boolean(),
|
9
|
-
});
|
10
|
-
function enqueueData(controller, data) {
|
11
|
-
controller.enqueue(textEncoder.encode(`data: ${JSON.stringify(data)}\n\n`));
|
12
|
-
}
|
13
|
-
export function createTextDeltaEventSource(textDeltas) {
|
14
|
-
return new ReadableStream({
|
15
|
-
async start(controller) {
|
16
|
-
for await (const textDelta of textDeltas) {
|
17
|
-
enqueueData(controller, { textDelta, isFinished: false });
|
18
|
-
}
|
19
|
-
enqueueData(controller, { isFinished: true });
|
20
|
-
},
|
21
|
-
});
|
22
|
-
}
|
23
|
-
export function parseTextDeltaEventSource(stream, options) {
|
24
|
-
const queue = new AsyncQueue();
|
25
|
-
// run async (no await on purpose):
|
26
|
-
parseEventSourceReadableStream({
|
27
|
-
stream,
|
28
|
-
callback: (event) => {
|
29
|
-
if (event.type !== "event") {
|
30
|
-
return;
|
31
|
-
}
|
32
|
-
try {
|
33
|
-
const data = textDeltaEventDataSchema.parse(SecureJSON.parse(event.data));
|
34
|
-
queue.push(data.textDelta);
|
35
|
-
if (data.isFinished) {
|
36
|
-
queue.close();
|
37
|
-
}
|
38
|
-
}
|
39
|
-
catch (error) {
|
40
|
-
options?.errorHandler(error);
|
41
|
-
queue.close();
|
42
|
-
}
|
43
|
-
},
|
44
|
-
});
|
45
|
-
return queue;
|
46
|
-
}
|
@@ -1,30 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.parseEventSourceReadableStream = void 0;
|
4
|
-
const eventsource_parser_1 = require("eventsource-parser");
|
5
|
-
async function* convertReadableStreamToAsyncIterable(reader) {
|
6
|
-
while (true) {
|
7
|
-
const result = await reader.read();
|
8
|
-
if (result.done) {
|
9
|
-
break;
|
10
|
-
}
|
11
|
-
yield result.value;
|
12
|
-
}
|
13
|
-
}
|
14
|
-
/**
|
15
|
-
* @internal
|
16
|
-
*/
|
17
|
-
async function parseEventSourceReadableStream({ stream, callback, }) {
|
18
|
-
try {
|
19
|
-
const parser = (0, eventsource_parser_1.createParser)(callback);
|
20
|
-
const decoder = new TextDecoder();
|
21
|
-
const iterable = convertReadableStreamToAsyncIterable(stream.getReader());
|
22
|
-
for await (const value of iterable) {
|
23
|
-
parser.feed(decoder.decode(value));
|
24
|
-
}
|
25
|
-
}
|
26
|
-
catch (error) {
|
27
|
-
console.error(error); // TODO introduce error handler param
|
28
|
-
}
|
29
|
-
}
|
30
|
-
exports.parseEventSourceReadableStream = parseEventSourceReadableStream;
|
@@ -1,8 +0,0 @@
|
|
1
|
-
import { EventSourceParseCallback } from "eventsource-parser";
|
2
|
-
/**
|
3
|
-
* @internal
|
4
|
-
*/
|
5
|
-
export declare function parseEventSourceReadableStream({ stream, callback, }: {
|
6
|
-
stream: ReadableStream<Uint8Array>;
|
7
|
-
callback: EventSourceParseCallback;
|
8
|
-
}): Promise<void>;
|
@@ -1,26 +0,0 @@
|
|
1
|
-
import { createParser } from "eventsource-parser";
|
2
|
-
async function* convertReadableStreamToAsyncIterable(reader) {
|
3
|
-
while (true) {
|
4
|
-
const result = await reader.read();
|
5
|
-
if (result.done) {
|
6
|
-
break;
|
7
|
-
}
|
8
|
-
yield result.value;
|
9
|
-
}
|
10
|
-
}
|
11
|
-
/**
|
12
|
-
* @internal
|
13
|
-
*/
|
14
|
-
export async function parseEventSourceReadableStream({ stream, callback, }) {
|
15
|
-
try {
|
16
|
-
const parser = createParser(callback);
|
17
|
-
const decoder = new TextDecoder();
|
18
|
-
const iterable = convertReadableStreamToAsyncIterable(stream.getReader());
|
19
|
-
for await (const value of iterable) {
|
20
|
-
parser.feed(decoder.decode(value));
|
21
|
-
}
|
22
|
-
}
|
23
|
-
catch (error) {
|
24
|
-
console.error(error); // TODO introduce error handler param
|
25
|
-
}
|
26
|
-
}
|