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.
Files changed (51) hide show
  1. package/core/api/postToApi.d.ts +1 -1
  2. package/{model-function/generate-text → event-source}/AsyncQueue.cjs +11 -10
  3. package/event-source/AsyncQueue.d.ts +8 -0
  4. package/{model-function/generate-text → event-source}/AsyncQueue.js +11 -10
  5. package/event-source/EventSourceParserStream.cjs +34 -0
  6. package/event-source/EventSourceParserStream.d.ts +15 -0
  7. package/event-source/EventSourceParserStream.js +30 -0
  8. package/event-source/convertReadableStreamToAsyncIterable.cjs +19 -0
  9. package/event-source/convertReadableStreamToAsyncIterable.d.ts +1 -0
  10. package/event-source/convertReadableStreamToAsyncIterable.js +15 -0
  11. package/event-source/createEventSourceStream.cjs +15 -0
  12. package/event-source/createEventSourceStream.d.ts +1 -0
  13. package/event-source/createEventSourceStream.js +11 -0
  14. package/event-source/index.cjs +19 -0
  15. package/event-source/index.d.ts +3 -0
  16. package/event-source/index.js +3 -0
  17. package/event-source/parseEventSourceStream.cjs +12 -0
  18. package/event-source/parseEventSourceStream.d.ts +4 -0
  19. package/event-source/parseEventSourceStream.js +8 -0
  20. package/event-source/readEventSourceStream.cjs +33 -0
  21. package/event-source/readEventSourceStream.d.ts +6 -0
  22. package/event-source/readEventSourceStream.js +26 -0
  23. package/index.cjs +1 -0
  24. package/index.d.ts +1 -0
  25. package/index.js +1 -0
  26. package/model-function/index.cjs +0 -1
  27. package/model-function/index.d.ts +0 -1
  28. package/model-function/index.js +0 -1
  29. package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +1 -1
  30. package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +1 -1
  31. package/model-function/synthesize-speech/synthesizeSpeech.d.ts +1 -1
  32. package/model-provider/cohere/CohereTextGenerationModel.cjs +1 -1
  33. package/model-provider/cohere/CohereTextGenerationModel.js +1 -1
  34. package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +1 -1
  35. package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +1 -1
  36. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +21 -21
  37. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +21 -21
  38. package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +1 -1
  39. package/model-provider/openai/OpenAITextGenerationModel.cjs +25 -23
  40. package/model-provider/openai/OpenAITextGenerationModel.js +25 -23
  41. package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
  42. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +27 -24
  43. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +27 -24
  44. package/package.json +5 -5
  45. package/model-function/generate-text/AsyncQueue.d.ts +0 -17
  46. package/model-function/generate-text/TextDeltaEventSource.cjs +0 -54
  47. package/model-function/generate-text/TextDeltaEventSource.d.ts +0 -5
  48. package/model-function/generate-text/TextDeltaEventSource.js +0 -46
  49. package/model-function/generate-text/parseEventSourceReadableStream.cjs +0 -30
  50. package/model-function/generate-text/parseEventSourceReadableStream.d.ts +0 -8
  51. package/model-function/generate-text/parseEventSourceReadableStream.js +0 -26
@@ -1,4 +1,4 @@
1
- /// <reference types="node" resolution-mode="require"/>
1
+ /// <reference types="node" />
2
2
  import { z } from "zod";
3
3
  import { ApiCallError } from "./ApiCallError.js";
4
4
  export type ResponseHandler<T> = (options: {
@@ -1,16 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AsyncQueue = void 0;
4
- /**
5
- * @internal
6
- */
7
4
  class AsyncQueue {
8
5
  constructor() {
9
6
  Object.defineProperty(this, "queue", {
10
7
  enumerable: true,
11
8
  configurable: true,
12
9
  writable: true,
13
- value: void 0
10
+ value: []
14
11
  });
15
12
  Object.defineProperty(this, "resolvers", {
16
13
  enumerable: true,
@@ -22,11 +19,8 @@ class AsyncQueue {
22
19
  enumerable: true,
23
20
  configurable: true,
24
21
  writable: true,
25
- value: void 0
22
+ value: false
26
23
  });
27
- this.queue = [];
28
- this.resolvers = [];
29
- this.closed = false;
30
24
  }
31
25
  push(value) {
32
26
  if (this.closed) {
@@ -43,6 +37,7 @@ class AsyncQueue {
43
37
  close() {
44
38
  while (this.resolvers.length) {
45
39
  const resolve = this.resolvers.shift();
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
41
  resolve?.({ value: undefined, done: true });
47
42
  }
48
43
  this.closed = true;
@@ -51,13 +46,19 @@ class AsyncQueue {
51
46
  return {
52
47
  next: () => {
53
48
  if (this.queue.length > 0) {
54
- return Promise.resolve({ value: this.queue.shift(), done: false });
49
+ return Promise.resolve({
50
+ value: this.queue.shift(),
51
+ done: false,
52
+ });
55
53
  }
56
54
  else if (this.closed) {
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
56
  return Promise.resolve({ value: undefined, done: true });
58
57
  }
59
58
  else {
60
- return new Promise((resolve) => this.resolvers.push(resolve));
59
+ return new Promise((resolve) => {
60
+ this.resolvers.push(resolve);
61
+ });
61
62
  }
62
63
  },
63
64
  };
@@ -0,0 +1,8 @@
1
+ export declare class AsyncQueue<T> implements AsyncIterable<T> {
2
+ private queue;
3
+ private resolvers;
4
+ private closed;
5
+ push(value: T): void;
6
+ close(): void;
7
+ [Symbol.asyncIterator](): AsyncIterator<T>;
8
+ }
@@ -1,13 +1,10 @@
1
- /**
2
- * @internal
3
- */
4
1
  export class AsyncQueue {
5
2
  constructor() {
6
3
  Object.defineProperty(this, "queue", {
7
4
  enumerable: true,
8
5
  configurable: true,
9
6
  writable: true,
10
- value: void 0
7
+ value: []
11
8
  });
12
9
  Object.defineProperty(this, "resolvers", {
13
10
  enumerable: true,
@@ -19,11 +16,8 @@ export class AsyncQueue {
19
16
  enumerable: true,
20
17
  configurable: true,
21
18
  writable: true,
22
- value: void 0
19
+ value: false
23
20
  });
24
- this.queue = [];
25
- this.resolvers = [];
26
- this.closed = false;
27
21
  }
28
22
  push(value) {
29
23
  if (this.closed) {
@@ -40,6 +34,7 @@ export class AsyncQueue {
40
34
  close() {
41
35
  while (this.resolvers.length) {
42
36
  const resolve = this.resolvers.shift();
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
38
  resolve?.({ value: undefined, done: true });
44
39
  }
45
40
  this.closed = true;
@@ -48,13 +43,19 @@ export class AsyncQueue {
48
43
  return {
49
44
  next: () => {
50
45
  if (this.queue.length > 0) {
51
- return Promise.resolve({ value: this.queue.shift(), done: false });
46
+ return Promise.resolve({
47
+ value: this.queue.shift(),
48
+ done: false,
49
+ });
52
50
  }
53
51
  else if (this.closed) {
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
53
  return Promise.resolve({ value: undefined, done: true });
55
54
  }
56
55
  else {
57
- return new Promise((resolve) => this.resolvers.push(resolve));
56
+ return new Promise((resolve) => {
57
+ this.resolvers.push(resolve);
58
+ });
58
59
  }
59
60
  },
60
61
  };
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventSourceParserStream = void 0;
4
+ const eventsource_parser_1 = require("eventsource-parser");
5
+ /**
6
+ * A TransformStream that ingests a stream of strings and produces a stream of ParsedEvents.
7
+ *
8
+ * @example
9
+ * ```
10
+ * const eventStream =
11
+ * response.body
12
+ * .pipeThrough(new TextDecoderStream())
13
+ * .pipeThrough(new EventSourceParserStream())
14
+ * ```
15
+ */
16
+ // Copied from https://github.com/rexxars/eventsource-parser/blob/main/src/stream.ts to avoid issues with the commonjs build.
17
+ class EventSourceParserStream extends TransformStream {
18
+ constructor() {
19
+ let parser;
20
+ super({
21
+ start(controller) {
22
+ parser = (0, eventsource_parser_1.createParser)((event) => {
23
+ if (event.type === "event") {
24
+ controller.enqueue(event);
25
+ }
26
+ });
27
+ },
28
+ transform(chunk) {
29
+ parser.feed(chunk);
30
+ },
31
+ });
32
+ }
33
+ }
34
+ exports.EventSourceParserStream = EventSourceParserStream;
@@ -0,0 +1,15 @@
1
+ import { ParsedEvent } from "eventsource-parser";
2
+ /**
3
+ * A TransformStream that ingests a stream of strings and produces a stream of ParsedEvents.
4
+ *
5
+ * @example
6
+ * ```
7
+ * const eventStream =
8
+ * response.body
9
+ * .pipeThrough(new TextDecoderStream())
10
+ * .pipeThrough(new EventSourceParserStream())
11
+ * ```
12
+ */
13
+ export declare class EventSourceParserStream extends TransformStream<string, ParsedEvent> {
14
+ constructor();
15
+ }
@@ -0,0 +1,30 @@
1
+ import { createParser, } from "eventsource-parser";
2
+ /**
3
+ * A TransformStream that ingests a stream of strings and produces a stream of ParsedEvents.
4
+ *
5
+ * @example
6
+ * ```
7
+ * const eventStream =
8
+ * response.body
9
+ * .pipeThrough(new TextDecoderStream())
10
+ * .pipeThrough(new EventSourceParserStream())
11
+ * ```
12
+ */
13
+ // Copied from https://github.com/rexxars/eventsource-parser/blob/main/src/stream.ts to avoid issues with the commonjs build.
14
+ export class EventSourceParserStream extends TransformStream {
15
+ constructor() {
16
+ let parser;
17
+ super({
18
+ start(controller) {
19
+ parser = createParser((event) => {
20
+ if (event.type === "event") {
21
+ controller.enqueue(event);
22
+ }
23
+ });
24
+ },
25
+ transform(chunk) {
26
+ parser.feed(chunk);
27
+ },
28
+ });
29
+ }
30
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertReadableStreamToAsyncIterable = void 0;
4
+ async function* convertReadableStreamToAsyncIterable(stream) {
5
+ const reader = stream.getReader();
6
+ try {
7
+ while (true) {
8
+ const { done, value } = await reader.read();
9
+ if (done) {
10
+ return; // This will close the generator
11
+ }
12
+ yield value;
13
+ }
14
+ }
15
+ finally {
16
+ reader.releaseLock();
17
+ }
18
+ }
19
+ exports.convertReadableStreamToAsyncIterable = convertReadableStreamToAsyncIterable;
@@ -0,0 +1 @@
1
+ export declare function convertReadableStreamToAsyncIterable<T>(stream: ReadableStream<T>): AsyncIterable<T>;
@@ -0,0 +1,15 @@
1
+ export async function* convertReadableStreamToAsyncIterable(stream) {
2
+ const reader = stream.getReader();
3
+ try {
4
+ while (true) {
5
+ const { done, value } = await reader.read();
6
+ if (done) {
7
+ return; // This will close the generator
8
+ }
9
+ yield value;
10
+ }
11
+ }
12
+ finally {
13
+ reader.releaseLock();
14
+ }
15
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEventSourceStream = void 0;
4
+ const textEncoder = new TextEncoder();
5
+ function createEventSourceStream(events) {
6
+ return new ReadableStream({
7
+ async start(controller) {
8
+ for await (const event of events) {
9
+ controller.enqueue(textEncoder.encode(`data: ${JSON.stringify(event)}\n\n`));
10
+ }
11
+ controller.close();
12
+ },
13
+ });
14
+ }
15
+ exports.createEventSourceStream = createEventSourceStream;
@@ -0,0 +1 @@
1
+ export declare function createEventSourceStream(events: AsyncIterable<unknown>): ReadableStream<any>;
@@ -0,0 +1,11 @@
1
+ const textEncoder = new TextEncoder();
2
+ export function createEventSourceStream(events) {
3
+ return new ReadableStream({
4
+ async start(controller) {
5
+ for await (const event of events) {
6
+ controller.enqueue(textEncoder.encode(`data: ${JSON.stringify(event)}\n\n`));
7
+ }
8
+ controller.close();
9
+ },
10
+ });
11
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./AsyncQueue.cjs"), exports);
18
+ __exportStar(require("./createEventSourceStream.cjs"), exports);
19
+ __exportStar(require("./readEventSourceStream.cjs"), exports);
@@ -0,0 +1,3 @@
1
+ export * from "./AsyncQueue.js";
2
+ export * from "./createEventSourceStream.js";
3
+ export * from "./readEventSourceStream.js";
@@ -0,0 +1,3 @@
1
+ export * from "./AsyncQueue.js";
2
+ export * from "./createEventSourceStream.js";
3
+ export * from "./readEventSourceStream.js";
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseEventSourceStream = void 0;
4
+ const convertReadableStreamToAsyncIterable_js_1 = require("./convertReadableStreamToAsyncIterable.cjs");
5
+ const EventSourceParserStream_js_1 = require("./EventSourceParserStream.cjs");
6
+ async function parseEventSourceStream({ stream, }) {
7
+ const eventStream = stream
8
+ .pipeThrough(new TextDecoderStream())
9
+ .pipeThrough(new EventSourceParserStream_js_1.EventSourceParserStream());
10
+ return (0, convertReadableStreamToAsyncIterable_js_1.convertReadableStreamToAsyncIterable)(eventStream);
11
+ }
12
+ exports.parseEventSourceStream = parseEventSourceStream;
@@ -0,0 +1,4 @@
1
+ import { ParsedEvent } from "eventsource-parser";
2
+ export declare function parseEventSourceStream({ stream, }: {
3
+ stream: ReadableStream<Uint8Array>;
4
+ }): Promise<AsyncIterable<ParsedEvent>>;
@@ -0,0 +1,8 @@
1
+ import { convertReadableStreamToAsyncIterable } from "./convertReadableStreamToAsyncIterable.js";
2
+ import { EventSourceParserStream } from "./EventSourceParserStream.js";
3
+ export async function parseEventSourceStream({ stream, }) {
4
+ const eventStream = stream
5
+ .pipeThrough(new TextDecoderStream())
6
+ .pipeThrough(new EventSourceParserStream());
7
+ return convertReadableStreamToAsyncIterable(eventStream);
8
+ }
@@ -0,0 +1,33 @@
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.readEventSourceStream = void 0;
7
+ const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
+ const AsyncQueue_js_1 = require("./AsyncQueue.cjs");
9
+ const parseEventSourceStream_js_1 = require("./parseEventSourceStream.cjs");
10
+ function readEventSourceStream({ stream, schema, errorHandler, }) {
11
+ const queue = new AsyncQueue_js_1.AsyncQueue();
12
+ // run async (no await on purpose):
13
+ (0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
14
+ .then(async (events) => {
15
+ try {
16
+ for await (const event of events) {
17
+ queue.push(schema.parse(secure_json_parse_1.default.parse(event.data)));
18
+ }
19
+ }
20
+ catch (error) {
21
+ errorHandler?.(error);
22
+ }
23
+ finally {
24
+ queue.close();
25
+ }
26
+ })
27
+ .catch((error) => {
28
+ errorHandler?.(error);
29
+ queue.close();
30
+ });
31
+ return queue;
32
+ }
33
+ exports.readEventSourceStream = readEventSourceStream;
@@ -0,0 +1,6 @@
1
+ import { ErrorHandler } from "../util/ErrorHandler.js";
2
+ export declare function readEventSourceStream<T>({ stream, schema, errorHandler, }: {
3
+ stream: ReadableStream<Uint8Array>;
4
+ schema: Zod.Schema<T>;
5
+ errorHandler?: ErrorHandler;
6
+ }): AsyncIterable<T>;
@@ -0,0 +1,26 @@
1
+ import SecureJSON from "secure-json-parse";
2
+ import { AsyncQueue } from "./AsyncQueue.js";
3
+ import { parseEventSourceStream } from "./parseEventSourceStream.js";
4
+ export function readEventSourceStream({ stream, schema, errorHandler, }) {
5
+ const queue = new AsyncQueue();
6
+ // run async (no await on purpose):
7
+ parseEventSourceStream({ stream })
8
+ .then(async (events) => {
9
+ try {
10
+ for await (const event of events) {
11
+ queue.push(schema.parse(SecureJSON.parse(event.data)));
12
+ }
13
+ }
14
+ catch (error) {
15
+ errorHandler?.(error);
16
+ }
17
+ finally {
18
+ queue.close();
19
+ }
20
+ })
21
+ .catch((error) => {
22
+ errorHandler?.(error);
23
+ queue.close();
24
+ });
25
+ return queue;
26
+ }
package/index.cjs CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./composed-function/index.cjs"), exports);
18
18
  __exportStar(require("./core/index.cjs"), exports);
19
19
  __exportStar(require("./cost/index.cjs"), exports);
20
+ __exportStar(require("./event-source/index.cjs"), exports);
20
21
  __exportStar(require("./model-function/index.cjs"), exports);
21
22
  __exportStar(require("./model-provider/index.cjs"), exports);
22
23
  __exportStar(require("./observability/index.cjs"), exports);
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from "./composed-function/index.js";
2
2
  export * from "./core/index.js";
3
3
  export * from "./cost/index.js";
4
+ export * from "./event-source/index.js";
4
5
  export * from "./model-function/index.js";
5
6
  export * from "./model-provider/index.js";
6
7
  export * from "./observability/index.js";
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from "./composed-function/index.js";
2
2
  export * from "./core/index.js";
3
3
  export * from "./cost/index.js";
4
+ export * from "./event-source/index.js";
4
5
  export * from "./model-function/index.js";
5
6
  export * from "./model-provider/index.js";
6
7
  export * from "./observability/index.js";
@@ -37,7 +37,6 @@ __exportStar(require("./generate-structure/StructureValidationError.cjs"), expor
37
37
  __exportStar(require("./generate-structure/generateStructure.cjs"), exports);
38
38
  __exportStar(require("./generate-structure/generateStructureOrText.cjs"), exports);
39
39
  __exportStar(require("./generate-text/DeltaEvent.cjs"), exports);
40
- __exportStar(require("./generate-text/TextDeltaEventSource.cjs"), exports);
41
40
  __exportStar(require("./generate-text/TextGenerationEvent.cjs"), exports);
42
41
  __exportStar(require("./generate-text/TextGenerationModel.cjs"), exports);
43
42
  __exportStar(require("./generate-text/TextStreamingEvent.cjs"), exports);
@@ -21,7 +21,6 @@ export * from "./generate-structure/StructureValidationError.js";
21
21
  export * from "./generate-structure/generateStructure.js";
22
22
  export * from "./generate-structure/generateStructureOrText.js";
23
23
  export * from "./generate-text/DeltaEvent.js";
24
- export * from "./generate-text/TextDeltaEventSource.js";
25
24
  export * from "./generate-text/TextGenerationEvent.js";
26
25
  export * from "./generate-text/TextGenerationModel.js";
27
26
  export * from "./generate-text/TextStreamingEvent.js";
@@ -21,7 +21,6 @@ export * from "./generate-structure/StructureValidationError.js";
21
21
  export * from "./generate-structure/generateStructure.js";
22
22
  export * from "./generate-structure/generateStructureOrText.js";
23
23
  export * from "./generate-text/DeltaEvent.js";
24
- export * from "./generate-text/TextDeltaEventSource.js";
25
24
  export * from "./generate-text/TextGenerationEvent.js";
26
25
  export * from "./generate-text/TextGenerationModel.js";
27
26
  export * from "./generate-text/TextStreamingEvent.js";
@@ -1,4 +1,4 @@
1
- /// <reference types="node" resolution-mode="require"/>
1
+ /// <reference types="node" />
2
2
  import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
3
3
  export interface SpeechSynthesisStartedEvent extends BaseModelCallStartedEvent {
4
4
  functionType: "speech-synthesis";
@@ -1,4 +1,4 @@
1
- /// <reference types="node" resolution-mode="require"/>
1
+ /// <reference types="node" />
2
2
  import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
3
3
  import { Model, ModelSettings } from "../Model.js";
4
4
  export interface SpeechSynthesisModelSettings extends ModelSettings {
@@ -1,4 +1,4 @@
1
- /// <reference types="node" resolution-mode="require"/>
1
+ /// <reference types="node" />
2
2
  import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
3
3
  import { ModelFunctionPromise } from "../executeCall.js";
4
4
  import { SpeechSynthesisModel, SpeechSynthesisModelSettings } from "./SpeechSynthesisModel.js";
@@ -7,7 +7,7 @@ exports.CohereTextGenerationResponseFormat = exports.CohereTextGenerationModel =
7
7
  const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
8
  const zod_1 = require("zod");
9
9
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
10
- const AsyncQueue_js_1 = require("../../model-function/generate-text/AsyncQueue.cjs");
10
+ const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
11
11
  const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
12
12
  const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
13
13
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
@@ -1,7 +1,7 @@
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 "../../model-function/generate-text/AsyncQueue.js";
4
+ import { AsyncQueue } from "../../event-source/AsyncQueue.js";
5
5
  import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
6
6
  import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
7
7
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
@@ -1,4 +1,4 @@
1
- /// <reference types="node" resolution-mode="require"/>
1
+ /// <reference types="node" />
2
2
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
3
3
  import { AbstractModel } from "../../model-function/AbstractModel.js";
4
4
  import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
@@ -1,4 +1,4 @@
1
- /// <reference types="node" resolution-mode="require"/>
1
+ /// <reference types="node" />
2
2
  import z from "zod";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { AbstractModel } from "../../model-function/AbstractModel.js";
@@ -7,8 +7,8 @@ exports.LlamaCppTextGenerationResponseFormat = exports.LlamaCppTextGenerationMod
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("../../model-function/generate-text/AsyncQueue.cjs");
11
- const parseEventSourceReadableStream_js_1 = require("../../model-function/generate-text/parseEventSourceReadableStream.cjs");
10
+ const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
11
+ const parseEventSourceStream_js_1 = require("../../event-source/parseEventSourceStream.cjs");
12
12
  const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
13
13
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
14
14
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
@@ -215,14 +215,11 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
215
215
  const queue = new AsyncQueue_js_1.AsyncQueue();
216
216
  let content = "";
217
217
  // process the stream asynchonously (no 'await' on purpose):
218
- (0, parseEventSourceReadableStream_js_1.parseEventSourceReadableStream)({
219
- stream,
220
- callback: (event) => {
221
- if (event.type !== "event") {
222
- return;
223
- }
224
- const data = event.data;
225
- try {
218
+ (0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
219
+ .then(async (events) => {
220
+ try {
221
+ for await (const event of events) {
222
+ const data = event.data;
226
223
  const json = secure_json_parse_1.default.parse(data);
227
224
  const parseResult = llamaCppTextStreamingResponseSchema.safeParse(json);
228
225
  if (!parseResult.success) {
@@ -233,26 +230,29 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
233
230
  queue.close();
234
231
  return;
235
232
  }
236
- const event = parseResult.data;
237
- content += event.content;
233
+ const eventData = parseResult.data;
234
+ content += eventData.content;
238
235
  queue.push({
239
236
  type: "delta",
240
237
  fullDelta: {
241
238
  content,
242
- isComplete: event.stop,
243
- delta: event.content,
239
+ isComplete: eventData.stop,
240
+ delta: eventData.content,
244
241
  },
245
242
  });
246
- if (event.stop) {
243
+ if (eventData.stop) {
247
244
  queue.close();
248
245
  }
249
246
  }
250
- catch (error) {
251
- queue.push({ type: "error", error });
252
- queue.close();
253
- return;
254
- }
255
- },
247
+ }
248
+ catch (error) {
249
+ queue.push({ type: "error", error });
250
+ queue.close();
251
+ }
252
+ })
253
+ .catch((error) => {
254
+ queue.push({ type: "error", error });
255
+ queue.close();
256
256
  });
257
257
  return queue;
258
258
  }