modelfusion 0.33.1 → 0.35.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 (92) hide show
  1. package/README.md +47 -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/AsyncIterableResultPromise.cjs +37 -0
  27. package/model-function/AsyncIterableResultPromise.d.ts +16 -0
  28. package/model-function/AsyncIterableResultPromise.js +33 -0
  29. package/model-function/{generate-text/DeltaEvent.d.ts → DeltaEvent.d.ts} +1 -1
  30. package/model-function/ModelCallEvent.d.ts +3 -2
  31. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
  32. package/model-function/generate-structure/StructureGenerationModel.d.ts +10 -1
  33. package/model-function/generate-structure/StructureStreamingEvent.cjs +2 -0
  34. package/model-function/generate-structure/StructureStreamingEvent.d.ts +7 -0
  35. package/model-function/generate-structure/StructureStreamingEvent.js +1 -0
  36. package/model-function/generate-structure/fixJson.cjs +215 -0
  37. package/model-function/generate-structure/fixJson.d.ts +1 -0
  38. package/model-function/generate-structure/fixJson.js +211 -0
  39. package/model-function/generate-structure/fixJson.test.cjs +130 -0
  40. package/model-function/generate-structure/fixJson.test.d.ts +1 -0
  41. package/model-function/generate-structure/fixJson.test.js +128 -0
  42. package/model-function/generate-structure/generateStructure.cjs +3 -1
  43. package/model-function/generate-structure/generateStructure.d.ts +1 -1
  44. package/model-function/generate-structure/generateStructure.js +3 -1
  45. package/model-function/generate-structure/parsePartialJson.cjs +29 -0
  46. package/model-function/generate-structure/parsePartialJson.d.ts +1 -0
  47. package/model-function/generate-structure/parsePartialJson.js +22 -0
  48. package/model-function/generate-structure/streamStructure.cjs +167 -0
  49. package/model-function/generate-structure/streamStructure.d.ts +17 -0
  50. package/model-function/generate-structure/streamStructure.js +160 -0
  51. package/model-function/generate-text/TextGenerationModel.d.ts +4 -4
  52. package/model-function/generate-text/streamText.cjs +47 -68
  53. package/model-function/generate-text/streamText.d.ts +3 -18
  54. package/model-function/generate-text/streamText.js +46 -66
  55. package/model-function/index.cjs +3 -2
  56. package/model-function/index.d.ts +3 -2
  57. package/model-function/index.js +3 -2
  58. package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -3
  59. package/model-provider/cohere/CohereTextGenerationModel.d.ts +3 -3
  60. package/model-provider/cohere/CohereTextGenerationModel.js +3 -3
  61. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +0 -12
  62. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +0 -2
  63. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +0 -12
  64. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +23 -23
  65. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +3 -3
  66. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +23 -23
  67. package/model-provider/openai/OpenAITextGenerationModel.cjs +27 -25
  68. package/model-provider/openai/OpenAITextGenerationModel.d.ts +3 -3
  69. package/model-provider/openai/OpenAITextGenerationModel.js +27 -25
  70. package/model-provider/openai/chat/OpenAIChatModel.cjs +23 -2
  71. package/model-provider/openai/chat/OpenAIChatModel.d.ts +4 -2
  72. package/model-provider/openai/chat/OpenAIChatModel.js +23 -2
  73. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +27 -24
  74. package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +1 -1
  75. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +27 -24
  76. package/package.json +9 -5
  77. package/prompt/PromptFormatTextGenerationModel.d.ts +1 -1
  78. package/tool/useTool.cjs +3 -4
  79. package/tool/useTool.d.ts +1 -1
  80. package/tool/useTool.js +3 -4
  81. package/model-function/generate-text/AsyncQueue.d.ts +0 -17
  82. package/model-function/generate-text/TextDeltaEventSource.cjs +0 -54
  83. package/model-function/generate-text/TextDeltaEventSource.d.ts +0 -5
  84. package/model-function/generate-text/TextDeltaEventSource.js +0 -46
  85. package/model-function/generate-text/extractTextDeltas.cjs +0 -23
  86. package/model-function/generate-text/extractTextDeltas.d.ts +0 -7
  87. package/model-function/generate-text/extractTextDeltas.js +0 -19
  88. package/model-function/generate-text/parseEventSourceReadableStream.cjs +0 -30
  89. package/model-function/generate-text/parseEventSourceReadableStream.d.ts +0 -8
  90. package/model-function/generate-text/parseEventSourceReadableStream.js +0 -26
  91. /package/model-function/{generate-text/DeltaEvent.cjs → DeltaEvent.cjs} +0 -0
  92. /package/model-function/{generate-text/DeltaEvent.js → DeltaEvent.js} +0 -0
@@ -1,51 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.streamText = exports.StreamTextPromise = void 0;
3
+ exports.streamText = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
5
  const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
6
6
  const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs");
7
7
  const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
8
+ const AbortError_js_1 = require("../../core/api/AbortError.cjs");
8
9
  const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
9
10
  const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
10
- const AbortError_js_1 = require("../../core/api/AbortError.cjs");
11
11
  const runSafe_js_1 = require("../../util/runSafe.cjs");
12
- const extractTextDeltas_js_1 = require("./extractTextDeltas.cjs");
13
- class StreamTextPromise extends Promise {
14
- constructor(fullPromise) {
15
- super((resolve) => {
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- resolve(null); // we override the resolve function
18
- });
19
- Object.defineProperty(this, "fullPromise", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: fullPromise
24
- });
25
- Object.defineProperty(this, "outputPromise", {
26
- enumerable: true,
27
- configurable: true,
28
- writable: true,
29
- value: void 0
30
- });
31
- this.outputPromise = fullPromise.then((result) => result.output);
32
- }
33
- asFullResponse() {
34
- return this.fullPromise;
35
- }
36
- then(onfulfilled, onrejected) {
37
- return this.outputPromise.then(onfulfilled, onrejected);
38
- }
39
- catch(onrejected) {
40
- return this.outputPromise.catch(onrejected);
41
- }
42
- finally(onfinally) {
43
- return this.outputPromise.finally(onfinally);
44
- }
45
- }
46
- exports.StreamTextPromise = StreamTextPromise;
12
+ const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
47
13
  function streamText(model, prompt, options) {
48
- return new StreamTextPromise(doStreamText(model, prompt, options));
14
+ return new AsyncIterableResultPromise_js_1.AsyncIterableResultPromise(doStreamText(model, prompt, options));
49
15
  }
50
16
  exports.streamText = streamText;
51
17
  async function doStreamText(model, prompt, options) {
@@ -87,14 +53,49 @@ async function doStreamText(model, prompt, options) {
87
53
  eventType: "started",
88
54
  ...startMetadata,
89
55
  });
90
- const result = await (0, runSafe_js_1.runSafe)(async () => (0, extractTextDeltas_js_1.extractTextDeltas)({
91
- deltaIterable: await model.generateDeltaStreamResponse(prompt, {
56
+ const result = await (0, runSafe_js_1.runSafe)(async () => {
57
+ const deltaIterable = await model.generateDeltaStreamResponse(prompt, {
92
58
  functionId: options?.functionId,
93
59
  settings,
94
60
  run,
95
- }),
96
- extractDelta: (fullDelta) => model.extractTextDelta(fullDelta),
97
- onDone: (fullText, lastFullDelta) => {
61
+ });
62
+ return (async function* () {
63
+ let accumulatedText = "";
64
+ let lastFullDelta;
65
+ for await (const event of deltaIterable) {
66
+ if (event?.type === "error") {
67
+ const error = event.error;
68
+ const finishMetadata = {
69
+ eventType: "finished",
70
+ ...startMetadata,
71
+ finishTimestamp: new Date(),
72
+ durationInMs: durationMeasurement.durationInMs,
73
+ };
74
+ eventSource.notify(error instanceof AbortError_js_1.AbortError
75
+ ? {
76
+ ...finishMetadata,
77
+ result: {
78
+ status: "abort",
79
+ },
80
+ }
81
+ : {
82
+ ...finishMetadata,
83
+ result: {
84
+ status: "error",
85
+ error,
86
+ },
87
+ });
88
+ throw error;
89
+ }
90
+ if (event?.type === "delta") {
91
+ lastFullDelta = event.fullDelta;
92
+ const delta = model.extractTextDelta(lastFullDelta);
93
+ if (delta != null && delta.length > 0) {
94
+ accumulatedText += delta;
95
+ yield delta;
96
+ }
97
+ }
98
+ }
98
99
  const finishMetadata = {
99
100
  eventType: "finished",
100
101
  ...startMetadata,
@@ -106,33 +107,11 @@ async function doStreamText(model, prompt, options) {
106
107
  result: {
107
108
  status: "success",
108
109
  response: lastFullDelta,
109
- output: fullText,
110
+ output: accumulatedText,
110
111
  },
111
112
  });
112
- },
113
- onError: (error) => {
114
- const finishMetadata = {
115
- eventType: "finished",
116
- ...startMetadata,
117
- finishTimestamp: new Date(),
118
- durationInMs: durationMeasurement.durationInMs,
119
- };
120
- eventSource.notify(error instanceof AbortError_js_1.AbortError
121
- ? {
122
- ...finishMetadata,
123
- result: {
124
- status: "abort",
125
- },
126
- }
127
- : {
128
- ...finishMetadata,
129
- result: {
130
- status: "error",
131
- error,
132
- },
133
- });
134
- },
135
- }));
113
+ })();
114
+ });
136
115
  if (!result.ok) {
137
116
  const finishMetadata = {
138
117
  eventType: "finished",
@@ -1,23 +1,8 @@
1
+ import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
2
+ import { DeltaEvent } from "../DeltaEvent.js";
1
3
  import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
2
- import { ModelCallMetadata } from "../executeCall.js";
3
- import { DeltaEvent } from "./DeltaEvent.js";
4
4
  import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
5
- export declare class StreamTextPromise extends Promise<AsyncIterable<string>> {
6
- private fullPromise;
7
- private outputPromise;
8
- constructor(fullPromise: Promise<{
9
- output: AsyncIterable<string>;
10
- metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
11
- }>);
12
- asFullResponse(): Promise<{
13
- output: AsyncIterable<string>;
14
- metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
15
- }>;
16
- then<TResult1 = AsyncIterable<string>, TResult2 = never>(onfulfilled?: ((value: AsyncIterable<string>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
17
- catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<AsyncIterable<string> | TResult>;
18
- finally(onfinally?: (() => void) | undefined | null): Promise<AsyncIterable<string>>;
19
- }
20
5
  export declare function streamText<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS> & {
21
6
  generateDeltaStreamResponse: (prompt: PROMPT, options: ModelFunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
22
7
  extractTextDelta: (fullDelta: FULL_DELTA) => string | undefined;
23
- }, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): StreamTextPromise;
8
+ }, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): AsyncIterableResultPromise<string>;
@@ -2,46 +2,13 @@ import { nanoid as createId } from "nanoid";
2
2
  import { FunctionEventSource } from "../../core/FunctionEventSource.js";
3
3
  import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
4
4
  import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
5
+ import { AbortError } from "../../core/api/AbortError.js";
5
6
  import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
6
7
  import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
7
- import { AbortError } from "../../core/api/AbortError.js";
8
8
  import { runSafe } from "../../util/runSafe.js";
9
- import { extractTextDeltas } from "./extractTextDeltas.js";
10
- export class StreamTextPromise extends Promise {
11
- constructor(fullPromise) {
12
- super((resolve) => {
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- resolve(null); // we override the resolve function
15
- });
16
- Object.defineProperty(this, "fullPromise", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: fullPromise
21
- });
22
- Object.defineProperty(this, "outputPromise", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: void 0
27
- });
28
- this.outputPromise = fullPromise.then((result) => result.output);
29
- }
30
- asFullResponse() {
31
- return this.fullPromise;
32
- }
33
- then(onfulfilled, onrejected) {
34
- return this.outputPromise.then(onfulfilled, onrejected);
35
- }
36
- catch(onrejected) {
37
- return this.outputPromise.catch(onrejected);
38
- }
39
- finally(onfinally) {
40
- return this.outputPromise.finally(onfinally);
41
- }
42
- }
9
+ import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
43
10
  export function streamText(model, prompt, options) {
44
- return new StreamTextPromise(doStreamText(model, prompt, options));
11
+ return new AsyncIterableResultPromise(doStreamText(model, prompt, options));
45
12
  }
46
13
  async function doStreamText(model, prompt, options) {
47
14
  if (options?.settings != null) {
@@ -82,14 +49,49 @@ async function doStreamText(model, prompt, options) {
82
49
  eventType: "started",
83
50
  ...startMetadata,
84
51
  });
85
- const result = await runSafe(async () => extractTextDeltas({
86
- deltaIterable: await model.generateDeltaStreamResponse(prompt, {
52
+ const result = await runSafe(async () => {
53
+ const deltaIterable = await model.generateDeltaStreamResponse(prompt, {
87
54
  functionId: options?.functionId,
88
55
  settings,
89
56
  run,
90
- }),
91
- extractDelta: (fullDelta) => model.extractTextDelta(fullDelta),
92
- onDone: (fullText, lastFullDelta) => {
57
+ });
58
+ return (async function* () {
59
+ let accumulatedText = "";
60
+ let lastFullDelta;
61
+ for await (const event of deltaIterable) {
62
+ if (event?.type === "error") {
63
+ const error = event.error;
64
+ const finishMetadata = {
65
+ eventType: "finished",
66
+ ...startMetadata,
67
+ finishTimestamp: new Date(),
68
+ durationInMs: durationMeasurement.durationInMs,
69
+ };
70
+ eventSource.notify(error instanceof AbortError
71
+ ? {
72
+ ...finishMetadata,
73
+ result: {
74
+ status: "abort",
75
+ },
76
+ }
77
+ : {
78
+ ...finishMetadata,
79
+ result: {
80
+ status: "error",
81
+ error,
82
+ },
83
+ });
84
+ throw error;
85
+ }
86
+ if (event?.type === "delta") {
87
+ lastFullDelta = event.fullDelta;
88
+ const delta = model.extractTextDelta(lastFullDelta);
89
+ if (delta != null && delta.length > 0) {
90
+ accumulatedText += delta;
91
+ yield delta;
92
+ }
93
+ }
94
+ }
93
95
  const finishMetadata = {
94
96
  eventType: "finished",
95
97
  ...startMetadata,
@@ -101,33 +103,11 @@ async function doStreamText(model, prompt, options) {
101
103
  result: {
102
104
  status: "success",
103
105
  response: lastFullDelta,
104
- output: fullText,
106
+ output: accumulatedText,
105
107
  },
106
108
  });
107
- },
108
- onError: (error) => {
109
- const finishMetadata = {
110
- eventType: "finished",
111
- ...startMetadata,
112
- finishTimestamp: new Date(),
113
- durationInMs: durationMeasurement.durationInMs,
114
- };
115
- eventSource.notify(error instanceof AbortError
116
- ? {
117
- ...finishMetadata,
118
- result: {
119
- status: "abort",
120
- },
121
- }
122
- : {
123
- ...finishMetadata,
124
- result: {
125
- status: "error",
126
- error,
127
- },
128
- });
129
- },
130
- }));
109
+ })();
110
+ });
131
111
  if (!result.ok) {
132
112
  const finishMetadata = {
133
113
  eventType: "finished",
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./DeltaEvent.cjs"), exports);
17
18
  __exportStar(require("./Model.cjs"), exports);
18
19
  __exportStar(require("./ModelCallEvent.cjs"), exports);
19
20
  __exportStar(require("./ModelFunctionOptions.cjs"), exports);
@@ -33,11 +34,11 @@ __exportStar(require("./generate-structure/StructureFromTextGenerationModel.cjs"
33
34
  __exportStar(require("./generate-structure/StructureGenerationEvent.cjs"), exports);
34
35
  __exportStar(require("./generate-structure/StructureGenerationModel.cjs"), exports);
35
36
  __exportStar(require("./generate-structure/StructureOrTextGenerationModel.cjs"), exports);
37
+ __exportStar(require("./generate-structure/StructureStreamingEvent.cjs"), exports);
36
38
  __exportStar(require("./generate-structure/StructureValidationError.cjs"), exports);
37
39
  __exportStar(require("./generate-structure/generateStructure.cjs"), exports);
38
40
  __exportStar(require("./generate-structure/generateStructureOrText.cjs"), exports);
39
- __exportStar(require("./generate-text/DeltaEvent.cjs"), exports);
40
- __exportStar(require("./generate-text/TextDeltaEventSource.cjs"), exports);
41
+ __exportStar(require("./generate-structure/streamStructure.cjs"), exports);
41
42
  __exportStar(require("./generate-text/TextGenerationEvent.cjs"), exports);
42
43
  __exportStar(require("./generate-text/TextGenerationModel.cjs"), exports);
43
44
  __exportStar(require("./generate-text/TextStreamingEvent.cjs"), exports);
@@ -1,3 +1,4 @@
1
+ export * from "./DeltaEvent.js";
1
2
  export * from "./Model.js";
2
3
  export * from "./ModelCallEvent.js";
3
4
  export * from "./ModelFunctionOptions.js";
@@ -17,11 +18,11 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
17
18
  export * from "./generate-structure/StructureGenerationEvent.js";
18
19
  export * from "./generate-structure/StructureGenerationModel.js";
19
20
  export * from "./generate-structure/StructureOrTextGenerationModel.js";
21
+ export * from "./generate-structure/StructureStreamingEvent.js";
20
22
  export * from "./generate-structure/StructureValidationError.js";
21
23
  export * from "./generate-structure/generateStructure.js";
22
24
  export * from "./generate-structure/generateStructureOrText.js";
23
- export * from "./generate-text/DeltaEvent.js";
24
- export * from "./generate-text/TextDeltaEventSource.js";
25
+ export * from "./generate-structure/streamStructure.js";
25
26
  export * from "./generate-text/TextGenerationEvent.js";
26
27
  export * from "./generate-text/TextGenerationModel.js";
27
28
  export * from "./generate-text/TextStreamingEvent.js";
@@ -1,3 +1,4 @@
1
+ export * from "./DeltaEvent.js";
1
2
  export * from "./Model.js";
2
3
  export * from "./ModelCallEvent.js";
3
4
  export * from "./ModelFunctionOptions.js";
@@ -17,11 +18,11 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
17
18
  export * from "./generate-structure/StructureGenerationEvent.js";
18
19
  export * from "./generate-structure/StructureGenerationModel.js";
19
20
  export * from "./generate-structure/StructureOrTextGenerationModel.js";
21
+ export * from "./generate-structure/StructureStreamingEvent.js";
20
22
  export * from "./generate-structure/StructureValidationError.js";
21
23
  export * from "./generate-structure/generateStructure.js";
22
24
  export * from "./generate-structure/generateStructureOrText.js";
23
- export * from "./generate-text/DeltaEvent.js";
24
- export * from "./generate-text/TextDeltaEventSource.js";
25
+ export * from "./generate-structure/streamStructure.js";
25
26
  export * from "./generate-text/TextGenerationEvent.js";
26
27
  export * from "./generate-text/TextGenerationModel.js";
27
28
  export * from "./generate-text/TextStreamingEvent.js";
@@ -6,12 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.CohereTextGenerationResponseFormat = exports.CohereTextGenerationModel = exports.COHERE_TEXT_GENERATION_MODELS = void 0;
7
7
  const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
8
  const zod_1 = require("zod");
9
+ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
10
+ const postToApi_js_1 = require("../../core/api/postToApi.cjs");
11
+ const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
9
12
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
10
- const AsyncQueue_js_1 = require("../../model-function/generate-text/AsyncQueue.cjs");
11
13
  const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
12
14
  const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
13
- const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
14
- const postToApi_js_1 = require("../../core/api/postToApi.cjs");
15
15
  const CohereApiConfiguration_js_1 = require("./CohereApiConfiguration.cjs");
16
16
  const CohereError_js_1 = require("./CohereError.cjs");
17
17
  const CohereTokenizer_js_1 = require("./CohereTokenizer.cjs");
@@ -1,12 +1,12 @@
1
1
  import { z } from "zod";
2
- import { AbstractModel } from "../../model-function/AbstractModel.js";
3
2
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
3
+ import { ResponseHandler } from "../../core/api/postToApi.js";
4
+ import { AbstractModel } from "../../model-function/AbstractModel.js";
5
+ import { DeltaEvent } from "../../model-function/DeltaEvent.js";
4
6
  import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
5
- import { DeltaEvent } from "../../model-function/generate-text/DeltaEvent.js";
6
7
  import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
7
8
  import { PromptFormat } from "../../prompt/PromptFormat.js";
8
9
  import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
9
- import { ResponseHandler } from "../../core/api/postToApi.js";
10
10
  import { CohereTokenizer } from "./CohereTokenizer.js";
11
11
  export declare const COHERE_TEXT_GENERATION_MODELS: {
12
12
  command: {
@@ -1,11 +1,11 @@
1
1
  import SecureJSON from "secure-json-parse";
2
2
  import { z } from "zod";
3
+ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
4
+ import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
5
+ import { AsyncQueue } from "../../event-source/AsyncQueue.js";
3
6
  import { AbstractModel } from "../../model-function/AbstractModel.js";
4
- import { AsyncQueue } from "../../model-function/generate-text/AsyncQueue.js";
5
7
  import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
6
8
  import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
7
- import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
8
- import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
9
9
  import { CohereApiConfiguration } from "./CohereApiConfiguration.js";
10
10
  import { failedCohereCallResponseHandler } from "./CohereError.js";
11
11
  import { CohereTokenizer } from "./CohereTokenizer.js";
@@ -56,18 +56,6 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
56
56
  writable: true,
57
57
  value: undefined
58
58
  });
59
- Object.defineProperty(this, "generateDeltaStreamResponse", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: undefined
64
- });
65
- Object.defineProperty(this, "extractTextDelta", {
66
- enumerable: true,
67
- configurable: true,
68
- writable: true,
69
- value: undefined
70
- });
71
59
  }
72
60
  get modelName() {
73
61
  return this.settings.model;
@@ -51,8 +51,6 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
51
51
  generated_text: string;
52
52
  }[]>;
53
53
  extractText(response: HuggingFaceTextGenerationResponse): string;
54
- generateDeltaStreamResponse: undefined;
55
- extractTextDelta: undefined;
56
54
  withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, string>): PromptFormatTextGenerationModel<INPUT_PROMPT, string, HuggingFaceTextGenerationResponse, undefined, HuggingFaceTextGenerationModelSettings, this>;
57
55
  withSettings(additionalSettings: Partial<HuggingFaceTextGenerationModelSettings>): this;
58
56
  }
@@ -50,18 +50,6 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
50
50
  writable: true,
51
51
  value: undefined
52
52
  });
53
- Object.defineProperty(this, "generateDeltaStreamResponse", {
54
- enumerable: true,
55
- configurable: true,
56
- writable: true,
57
- value: undefined
58
- });
59
- Object.defineProperty(this, "extractTextDelta", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: undefined
64
- });
65
53
  }
66
54
  get modelName() {
67
55
  return this.settings.model;
@@ -6,12 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.LlamaCppTextGenerationResponseFormat = exports.LlamaCppTextGenerationModel = void 0;
7
7
  const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
8
  const zod_1 = __importDefault(require("zod"));
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");
12
- const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
13
9
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
14
10
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
11
+ const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
12
+ const parseEventSourceStream_js_1 = require("../../event-source/parseEventSourceStream.cjs");
13
+ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
14
+ const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
15
15
  const LlamaCppApiConfiguration_js_1 = require("./LlamaCppApiConfiguration.cjs");
16
16
  const LlamaCppError_js_1 = require("./LlamaCppError.cjs");
17
17
  const LlamaCppTokenizer_js_1 = require("./LlamaCppTokenizer.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
  }
@@ -1,12 +1,12 @@
1
1
  import z from "zod";
2
- import { AbstractModel } from "../../model-function/AbstractModel.js";
3
2
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
3
+ import { ResponseHandler } from "../../core/api/postToApi.js";
4
+ import { AbstractModel } from "../../model-function/AbstractModel.js";
5
+ import { DeltaEvent } from "../../model-function/DeltaEvent.js";
4
6
  import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
5
- import { DeltaEvent } from "../../model-function/generate-text/DeltaEvent.js";
6
7
  import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
7
8
  import { PromptFormat } from "../../prompt/PromptFormat.js";
8
9
  import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
9
- import { ResponseHandler } from "../../core/api/postToApi.js";
10
10
  import { LlamaCppTokenizer } from "./LlamaCppTokenizer.js";
11
11
  export interface LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE extends number | undefined> extends TextGenerationModelSettings {
12
12
  api?: ApiConfiguration;
@@ -1,11 +1,11 @@
1
1
  import SecureJSON from "secure-json-parse";
2
2
  import z from "zod";
3
- import { AbstractModel } from "../../model-function/AbstractModel.js";
4
- import { AsyncQueue } from "../../model-function/generate-text/AsyncQueue.js";
5
- import { parseEventSourceReadableStream } from "../../model-function/generate-text/parseEventSourceReadableStream.js";
6
- import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
7
3
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
8
4
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
5
+ import { AsyncQueue } from "../../event-source/AsyncQueue.js";
6
+ import { parseEventSourceStream } from "../../event-source/parseEventSourceStream.js";
7
+ import { AbstractModel } from "../../model-function/AbstractModel.js";
8
+ import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
9
9
  import { LlamaCppApiConfiguration } from "./LlamaCppApiConfiguration.js";
10
10
  import { failedLlamaCppCallResponseHandler } from "./LlamaCppError.js";
11
11
  import { LlamaCppTokenizer } from "./LlamaCppTokenizer.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
- parseEventSourceReadableStream({
212
- stream,
213
- callback: (event) => {
214
- if (event.type !== "event") {
215
- return;
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 event = parseResult.data;
230
- content += event.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: event.stop,
236
- delta: event.content,
232
+ isComplete: eventData.stop,
233
+ delta: eventData.content,
237
234
  },
238
235
  });
239
- if (event.stop) {
236
+ if (eventData.stop) {
240
237
  queue.close();
241
238
  }
242
239
  }
243
- catch (error) {
244
- queue.push({ type: "error", error });
245
- queue.close();
246
- return;
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
  }