modelfusion 0.67.0 → 0.68.1

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 (102) hide show
  1. package/browser/invokeFlow.cjs +4 -4
  2. package/browser/invokeFlow.d.ts +1 -1
  3. package/browser/invokeFlow.js +2 -2
  4. package/browser/readEventSource.cjs +2 -2
  5. package/browser/readEventSource.d.ts +1 -1
  6. package/browser/readEventSource.js +2 -2
  7. package/browser/readEventSourceStream.cjs +2 -2
  8. package/browser/readEventSourceStream.d.ts +1 -1
  9. package/browser/readEventSourceStream.js +2 -2
  10. package/core/index.cjs +1 -1
  11. package/core/index.d.ts +1 -1
  12. package/core/index.js +1 -1
  13. package/{util → core/schema}/JSONParseError.cjs +6 -6
  14. package/{util → core/schema}/JSONParseError.d.ts +3 -3
  15. package/{util → core/schema}/JSONParseError.js +6 -6
  16. package/core/{structure → schema}/ZodStructureDefinition.cjs +1 -1
  17. package/core/{structure → schema}/ZodStructureDefinition.d.ts +1 -1
  18. package/core/{structure → schema}/ZodStructureDefinition.js +1 -1
  19. package/core/{structure → schema}/index.cjs +2 -0
  20. package/core/{structure → schema}/index.d.ts +2 -0
  21. package/core/{structure → schema}/index.js +2 -0
  22. package/core/schema/parseJSON.cjs +56 -0
  23. package/core/schema/parseJSON.d.ts +56 -0
  24. package/core/schema/parseJSON.js +48 -0
  25. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
  26. package/model-function/generate-structure/StructureGenerationModel.d.ts +1 -1
  27. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +1 -1
  28. package/model-function/generate-structure/generateStructure.d.ts +1 -1
  29. package/model-function/generate-structure/generateStructureOrText.d.ts +1 -1
  30. package/model-function/generate-structure/streamStructure.d.ts +1 -1
  31. package/model-provider/anthropic/AnthropicError.cjs +8 -4
  32. package/model-provider/anthropic/AnthropicError.d.ts +3 -19
  33. package/model-provider/anthropic/AnthropicError.js +8 -4
  34. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +8 -4
  35. package/model-provider/anthropic/AnthropicTextGenerationModel.js +8 -4
  36. package/model-provider/automatic1111/Automatic1111Error.cjs +8 -4
  37. package/model-provider/automatic1111/Automatic1111Error.d.ts +3 -13
  38. package/model-provider/automatic1111/Automatic1111Error.js +8 -4
  39. package/model-provider/cohere/CohereError.cjs +8 -4
  40. package/model-provider/cohere/CohereError.d.ts +3 -7
  41. package/model-provider/cohere/CohereError.js +8 -4
  42. package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -2
  43. package/model-provider/cohere/CohereTextGenerationModel.js +3 -2
  44. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +28 -24
  45. package/model-provider/elevenlabs/ElevenLabsSpeechModel.d.ts +1 -1
  46. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +28 -24
  47. package/model-provider/huggingface/HuggingFaceError.cjs +8 -4
  48. package/model-provider/huggingface/HuggingFaceError.d.ts +3 -7
  49. package/model-provider/huggingface/HuggingFaceError.js +8 -4
  50. package/model-provider/llamacpp/LlamaCppError.cjs +8 -4
  51. package/model-provider/llamacpp/LlamaCppError.d.ts +3 -7
  52. package/model-provider/llamacpp/LlamaCppError.js +8 -4
  53. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +10 -6
  54. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +10 -6
  55. package/model-provider/ollama/OllamaTextGenerationModel.cjs +3 -2
  56. package/model-provider/ollama/OllamaTextGenerationModel.js +3 -2
  57. package/model-provider/openai/OpenAICompletionModel.cjs +8 -4
  58. package/model-provider/openai/OpenAICompletionModel.js +8 -4
  59. package/model-provider/openai/OpenAIError.cjs +8 -4
  60. package/model-provider/openai/OpenAIError.d.ts +3 -27
  61. package/model-provider/openai/OpenAIError.js +8 -4
  62. package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
  63. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +8 -4
  64. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +8 -4
  65. package/model-provider/stability/StabilityError.cjs +8 -4
  66. package/model-provider/stability/StabilityError.d.ts +3 -7
  67. package/model-provider/stability/StabilityError.js +8 -4
  68. package/package.json +1 -1
  69. package/tool/Tool.d.ts +3 -3
  70. package/tool/WebSearchTool.cjs +1 -1
  71. package/tool/WebSearchTool.d.ts +1 -1
  72. package/tool/WebSearchTool.js +1 -1
  73. package/util/index.cjs +0 -2
  74. package/util/index.d.ts +0 -2
  75. package/util/index.js +0 -2
  76. package/util/streaming/parseJsonStream.cjs +2 -2
  77. package/util/streaming/parseJsonStream.d.ts +2 -1
  78. package/util/streaming/parseJsonStream.js +2 -2
  79. package/vector-index/memory/MemoryVectorIndex.cjs +5 -4
  80. package/vector-index/memory/MemoryVectorIndex.d.ts +1 -1
  81. package/vector-index/memory/MemoryVectorIndex.js +5 -4
  82. package/util/parseJSON.cjs +0 -67
  83. package/util/parseJSON.d.ts +0 -18
  84. package/util/parseJSON.js +0 -58
  85. /package/core/{structure → schema}/JsonSchemaProducer.cjs +0 -0
  86. /package/core/{structure → schema}/JsonSchemaProducer.d.ts +0 -0
  87. /package/core/{structure → schema}/JsonSchemaProducer.js +0 -0
  88. /package/core/{structure → schema}/Schema.cjs +0 -0
  89. /package/core/{structure → schema}/Schema.d.ts +0 -0
  90. /package/core/{structure → schema}/Schema.js +0 -0
  91. /package/core/{structure → schema}/StructureDefinition.cjs +0 -0
  92. /package/core/{structure → schema}/StructureDefinition.d.ts +0 -0
  93. /package/core/{structure → schema}/StructureDefinition.js +0 -0
  94. /package/core/{structure → schema}/UncheckedSchema.cjs +0 -0
  95. /package/core/{structure → schema}/UncheckedSchema.d.ts +0 -0
  96. /package/core/{structure → schema}/UncheckedSchema.js +0 -0
  97. /package/core/{structure → schema}/UncheckedStructureDefinition.cjs +0 -0
  98. /package/core/{structure → schema}/UncheckedStructureDefinition.d.ts +0 -0
  99. /package/core/{structure → schema}/UncheckedStructureDefinition.js +0 -0
  100. /package/core/{structure → schema}/ZodSchema.cjs +0 -0
  101. /package/core/{structure → schema}/ZodSchema.d.ts +0 -0
  102. /package/core/{structure → schema}/ZodSchema.js +0 -0
@@ -5,7 +5,8 @@ import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStr
5
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
6
  import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
7
7
  import { AsyncQueue } from "../../util/AsyncQueue.js";
8
- import { parseJsonWithZod } from "../../util/parseJSON.js";
8
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
9
+ import { parseJSON } from "../../core/schema/parseJSON.js";
9
10
  import { AnthropicApiConfiguration } from "./AnthropicApiConfiguration.js";
10
11
  import { failedAnthropicCallResponseHandler } from "./AnthropicError.js";
11
12
  import { mapChatPromptToAnthropicFormat, mapInstructionPromptToAnthropicFormat, } from "./AnthropicPromptFormat.js";
@@ -154,11 +155,11 @@ async function callAnthropicTextGenerationAPI({ api = new AnthropicApiConfigurat
154
155
  abortSignal,
155
156
  });
156
157
  }
157
- const anthropicTextStreamingResponseSchema = z.object({
158
+ const anthropicTextStreamingResponseSchema = new ZodSchema(z.object({
158
159
  completion: z.string(),
159
160
  stop_reason: z.string().nullable(),
160
161
  model: z.string(),
161
- });
162
+ }));
162
163
  async function createAnthropicFullDeltaIterableQueue(stream) {
163
164
  const queue = new AsyncQueue();
164
165
  let content = "";
@@ -176,7 +177,10 @@ async function createAnthropicFullDeltaIterableQueue(stream) {
176
177
  continue;
177
178
  }
178
179
  const data = event.data;
179
- const eventData = parseJsonWithZod(data, anthropicTextStreamingResponseSchema);
180
+ const eventData = parseJSON({
181
+ text: data,
182
+ schema: anthropicTextStreamingResponseSchema,
183
+ });
180
184
  content += eventData.completion;
181
185
  queue.push({
182
186
  type: "delta",
@@ -3,13 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.failedAutomatic1111CallResponseHandler = exports.Automatic1111Error = exports.automatic1111ErrorDataSchema = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
6
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
7
- exports.automatic1111ErrorDataSchema = zod_1.z.object({
6
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
7
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
8
+ exports.automatic1111ErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
9
  error: zod_1.z.string(),
9
10
  detail: zod_1.z.string(),
10
11
  body: zod_1.z.string(),
11
12
  errors: zod_1.z.string(),
12
- });
13
+ }));
13
14
  class Automatic1111Error extends ApiCallError_js_1.ApiCallError {
14
15
  constructor({ data, statusCode, url, requestBodyValues, message = data.detail, }) {
15
16
  super({ message, statusCode, requestBodyValues, url });
@@ -25,7 +26,10 @@ class Automatic1111Error extends ApiCallError_js_1.ApiCallError {
25
26
  exports.Automatic1111Error = Automatic1111Error;
26
27
  const failedAutomatic1111CallResponseHandler = async ({ response, url, requestBodyValues }) => {
27
28
  const responseBody = await response.text();
28
- const parsedError = (0, parseJSON_js_1.parseJsonWithZod)(responseBody, exports.automatic1111ErrorDataSchema);
29
+ const parsedError = (0, parseJSON_js_1.parseJSON)({
30
+ text: responseBody,
31
+ schema: exports.automatic1111ErrorDataSchema,
32
+ });
29
33
  return new Automatic1111Error({
30
34
  url,
31
35
  requestBodyValues,
@@ -1,23 +1,13 @@
1
- import { z } from "zod";
2
1
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
2
  import { ResponseHandler } from "../../core/api/postToApi.js";
4
- export declare const automatic1111ErrorDataSchema: z.ZodObject<{
5
- error: z.ZodString;
6
- detail: z.ZodString;
7
- body: z.ZodString;
8
- errors: z.ZodString;
9
- }, "strip", z.ZodTypeAny, {
10
- error: string;
11
- errors: string;
12
- body: string;
13
- detail: string;
14
- }, {
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ export declare const automatic1111ErrorDataSchema: ZodSchema<{
15
5
  error: string;
16
6
  errors: string;
17
7
  body: string;
18
8
  detail: string;
19
9
  }>;
20
- export type Automatic1111ErrorData = z.infer<typeof automatic1111ErrorDataSchema>;
10
+ export type Automatic1111ErrorData = (typeof automatic1111ErrorDataSchema)["_type"];
21
11
  export declare class Automatic1111Error extends ApiCallError {
22
12
  readonly data: Automatic1111ErrorData;
23
13
  constructor({ data, statusCode, url, requestBodyValues, message, }: {
@@ -1,12 +1,13 @@
1
1
  import { z } from "zod";
2
2
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
- import { parseJsonWithZod } from "../../util/parseJSON.js";
4
- export const automatic1111ErrorDataSchema = z.object({
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ import { parseJSON } from "../../core/schema/parseJSON.js";
5
+ export const automatic1111ErrorDataSchema = new ZodSchema(z.object({
5
6
  error: z.string(),
6
7
  detail: z.string(),
7
8
  body: z.string(),
8
9
  errors: z.string(),
9
- });
10
+ }));
10
11
  export class Automatic1111Error extends ApiCallError {
11
12
  constructor({ data, statusCode, url, requestBodyValues, message = data.detail, }) {
12
13
  super({ message, statusCode, requestBodyValues, url });
@@ -21,7 +22,10 @@ export class Automatic1111Error extends ApiCallError {
21
22
  }
22
23
  export const failedAutomatic1111CallResponseHandler = async ({ response, url, requestBodyValues }) => {
23
24
  const responseBody = await response.text();
24
- const parsedError = parseJsonWithZod(responseBody, automatic1111ErrorDataSchema);
25
+ const parsedError = parseJSON({
26
+ text: responseBody,
27
+ schema: automatic1111ErrorDataSchema,
28
+ });
25
29
  return new Automatic1111Error({
26
30
  url,
27
31
  requestBodyValues,
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.failedCohereCallResponseHandler = exports.CohereError = exports.cohereErrorDataSchema = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
6
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
7
- exports.cohereErrorDataSchema = zod_1.z.object({
6
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
7
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
8
+ exports.cohereErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
9
  message: zod_1.z.string(),
9
- });
10
+ }));
10
11
  class CohereError extends ApiCallError_js_1.ApiCallError {
11
12
  constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
12
13
  super({ message, statusCode, requestBodyValues, url });
@@ -33,7 +34,10 @@ const failedCohereCallResponseHandler = async ({ response, url, requestBodyValue
33
34
  },
34
35
  });
35
36
  }
36
- const parsedError = (0, parseJSON_js_1.parseJsonWithZod)(responseBody, exports.cohereErrorDataSchema);
37
+ const parsedError = (0, parseJSON_js_1.parseJSON)({
38
+ text: responseBody,
39
+ schema: exports.cohereErrorDataSchema,
40
+ });
37
41
  return new CohereError({
38
42
  url,
39
43
  requestBodyValues,
@@ -1,14 +1,10 @@
1
- import { z } from "zod";
2
1
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
2
  import { ResponseHandler } from "../../core/api/postToApi.js";
4
- export declare const cohereErrorDataSchema: z.ZodObject<{
5
- message: z.ZodString;
6
- }, "strip", z.ZodTypeAny, {
7
- message: string;
8
- }, {
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ export declare const cohereErrorDataSchema: ZodSchema<{
9
5
  message: string;
10
6
  }>;
11
- export type CohereErrorData = z.infer<typeof cohereErrorDataSchema>;
7
+ export type CohereErrorData = (typeof cohereErrorDataSchema)["_type"];
12
8
  export declare class CohereError extends ApiCallError {
13
9
  readonly data: CohereErrorData;
14
10
  constructor({ data, statusCode, url, requestBodyValues, message, }: {
@@ -1,9 +1,10 @@
1
1
  import { z } from "zod";
2
2
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
- import { parseJsonWithZod } from "../../util/parseJSON.js";
4
- export const cohereErrorDataSchema = z.object({
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ import { parseJSON } from "../../core/schema/parseJSON.js";
5
+ export const cohereErrorDataSchema = new ZodSchema(z.object({
5
6
  message: z.string(),
6
- });
7
+ }));
7
8
  export class CohereError extends ApiCallError {
8
9
  constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
9
10
  super({ message, statusCode, requestBodyValues, url });
@@ -29,7 +30,10 @@ export const failedCohereCallResponseHandler = async ({ response, url, requestBo
29
30
  },
30
31
  });
31
32
  }
32
- const parsedError = parseJsonWithZod(responseBody, cohereErrorDataSchema);
33
+ const parsedError = parseJSON({
34
+ text: responseBody,
35
+ schema: cohereErrorDataSchema,
36
+ });
33
37
  return new CohereError({
34
38
  url,
35
39
  requestBodyValues,
@@ -13,6 +13,7 @@ const parseJsonStream_js_1 = require("../../util/streaming/parseJsonStream.cjs")
13
13
  const CohereApiConfiguration_js_1 = require("./CohereApiConfiguration.cjs");
14
14
  const CohereError_js_1 = require("./CohereError.cjs");
15
15
  const CohereTokenizer_js_1 = require("./CohereTokenizer.cjs");
16
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
16
17
  exports.COHERE_TEXT_GENERATION_MODELS = {
17
18
  command: {
18
19
  contextWindowSize: 2048,
@@ -202,7 +203,7 @@ async function callCohereTextGenerationAPI({ api = new CohereApiConfiguration_js
202
203
  abortSignal,
203
204
  });
204
205
  }
205
- const cohereTextStreamingResponseSchema = zod_1.z.discriminatedUnion("is_finished", [
206
+ const cohereTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("is_finished", [
206
207
  zod_1.z.object({
207
208
  text: zod_1.z.string(),
208
209
  is_finished: zod_1.z.literal(false),
@@ -212,7 +213,7 @@ const cohereTextStreamingResponseSchema = zod_1.z.discriminatedUnion("is_finishe
212
213
  finish_reason: zod_1.z.string(),
213
214
  response: cohereTextGenerationResponseSchema,
214
215
  }),
215
- ]);
216
+ ]));
216
217
  async function createCohereTextGenerationFullDeltaIterableQueue(stream) {
217
218
  const queue = new AsyncQueue_js_1.AsyncQueue();
218
219
  let accumulatedText = "";
@@ -10,6 +10,7 @@ import { parseJsonStream } from "../../util/streaming/parseJsonStream.js";
10
10
  import { CohereApiConfiguration } from "./CohereApiConfiguration.js";
11
11
  import { failedCohereCallResponseHandler } from "./CohereError.js";
12
12
  import { CohereTokenizer } from "./CohereTokenizer.js";
13
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
13
14
  export const COHERE_TEXT_GENERATION_MODELS = {
14
15
  command: {
15
16
  contextWindowSize: 2048,
@@ -198,7 +199,7 @@ async function callCohereTextGenerationAPI({ api = new CohereApiConfiguration(),
198
199
  abortSignal,
199
200
  });
200
201
  }
201
- const cohereTextStreamingResponseSchema = z.discriminatedUnion("is_finished", [
202
+ const cohereTextStreamingResponseSchema = new ZodSchema(z.discriminatedUnion("is_finished", [
202
203
  z.object({
203
204
  text: z.string(),
204
205
  is_finished: z.literal(false),
@@ -208,7 +209,7 @@ const cohereTextStreamingResponseSchema = z.discriminatedUnion("is_finished", [
208
209
  finish_reason: z.string(),
209
210
  response: cohereTextGenerationResponseSchema,
210
211
  }),
211
- ]);
212
+ ]));
212
213
  async function createCohereTextGenerationFullDeltaIterableQueue(stream) {
213
214
  const queue = new AsyncQueue();
214
215
  let accumulatedText = "";
@@ -4,10 +4,11 @@ exports.ElevenLabsSpeechModel = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
- const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
7
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
8
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
+ const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
9
10
  const SimpleWebSocket_js_1 = require("../../util/SimpleWebSocket.cjs");
10
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
11
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
11
12
  const ElevenLabsApiConfiguration_js_1 = require("./ElevenLabsApiConfiguration.cjs");
12
13
  const ElevenLabsError_js_1 = require("./ElevenLabsError.cjs");
13
14
  const elevenLabsModels = [
@@ -17,6 +18,27 @@ const elevenLabsModels = [
17
18
  "eleven_turbo_v2",
18
19
  ];
19
20
  const defaultModel = "eleven_monolingual_v1";
21
+ const streamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.union([
22
+ zod_1.z.object({
23
+ audio: zod_1.z.string(),
24
+ isFinal: zod_1.z.literal(false).nullable(),
25
+ normalizedAlignment: zod_1.z
26
+ .object({
27
+ chars: zod_1.z.array(zod_1.z.string()),
28
+ charStartTimesMs: zod_1.z.array(zod_1.z.number()),
29
+ charDurationsMs: zod_1.z.array(zod_1.z.number()),
30
+ })
31
+ .nullable(),
32
+ }),
33
+ zod_1.z.object({
34
+ isFinal: zod_1.z.literal(true),
35
+ }),
36
+ zod_1.z.object({
37
+ message: zod_1.z.string(),
38
+ error: zod_1.z.string(),
39
+ code: zod_1.z.number(),
40
+ }),
41
+ ]));
20
42
  /**
21
43
  * Synthesize speech using the ElevenLabs Text to Speech API.
22
44
  *
@@ -65,27 +87,6 @@ class ElevenLabsSpeechModel extends AbstractModel_js_1.AbstractModel {
65
87
  async doGenerateSpeechStreamDuplex(textStream
66
88
  // options?: FunctionOptions | undefined
67
89
  ) {
68
- const responseSchema = zod_1.z.union([
69
- zod_1.z.object({
70
- audio: zod_1.z.string(),
71
- isFinal: zod_1.z.literal(false).nullable(),
72
- normalizedAlignment: zod_1.z
73
- .object({
74
- chars: zod_1.z.array(zod_1.z.string()),
75
- charStartTimesMs: zod_1.z.array(zod_1.z.number()),
76
- charDurationsMs: zod_1.z.array(zod_1.z.number()),
77
- })
78
- .nullable(),
79
- }),
80
- zod_1.z.object({
81
- isFinal: zod_1.z.literal(true),
82
- }),
83
- zod_1.z.object({
84
- message: zod_1.z.string(),
85
- error: zod_1.z.string(),
86
- code: zod_1.z.number(),
87
- }),
88
- ]);
89
90
  const queue = new AsyncQueue_js_1.AsyncQueue();
90
91
  const model = this.settings.model ?? defaultModel;
91
92
  const socket = await (0, SimpleWebSocket_js_1.createSimpleWebSocket)(`wss://api.elevenlabs.io/v1/text-to-speech/${this.settings.voice}/stream-input${assembleQuery({
@@ -132,7 +133,10 @@ class ElevenLabsSpeechModel extends AbstractModel_js_1.AbstractModel {
132
133
  socket.send(JSON.stringify({ text: "" }));
133
134
  };
134
135
  socket.onmessage = (event) => {
135
- const parseResult = (0, parseJSON_js_1.safeParseJsonWithZod)(event.data, responseSchema);
136
+ const parseResult = (0, parseJSON_js_1.safeParseJSON)({
137
+ text: event.data,
138
+ schema: streamingResponseSchema,
139
+ });
136
140
  if (!parseResult.success) {
137
141
  queue.push({ type: "error", error: parseResult.error });
138
142
  return;
@@ -3,7 +3,7 @@ import { FunctionOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { Delta } from "../../model-function/Delta.js";
6
- import { StreamingSpeechGenerationModel, SpeechGenerationModelSettings } from "../../model-function/generate-speech/SpeechGenerationModel.js";
6
+ import { SpeechGenerationModelSettings, StreamingSpeechGenerationModel } from "../../model-function/generate-speech/SpeechGenerationModel.js";
7
7
  declare const elevenLabsModels: readonly ["eleven_multilingual_v2", "eleven_multilingual_v1", "eleven_monolingual_v1", "eleven_turbo_v2"];
8
8
  export interface ElevenLabsSpeechModelSettings extends SpeechGenerationModelSettings {
9
9
  api?: ApiConfiguration & {
@@ -1,10 +1,11 @@
1
1
  import { z } from "zod";
2
2
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
3
3
  import { createAudioMpegResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
- import { AsyncQueue } from "../../util/AsyncQueue.js";
4
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
5
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
+ import { AsyncQueue } from "../../util/AsyncQueue.js";
6
7
  import { createSimpleWebSocket } from "../../util/SimpleWebSocket.js";
7
- import { safeParseJsonWithZod } from "../../util/parseJSON.js";
8
+ import { safeParseJSON } from "../../core/schema/parseJSON.js";
8
9
  import { ElevenLabsApiConfiguration } from "./ElevenLabsApiConfiguration.js";
9
10
  import { failedElevenLabsCallResponseHandler } from "./ElevenLabsError.js";
10
11
  const elevenLabsModels = [
@@ -14,6 +15,27 @@ const elevenLabsModels = [
14
15
  "eleven_turbo_v2",
15
16
  ];
16
17
  const defaultModel = "eleven_monolingual_v1";
18
+ const streamingResponseSchema = new ZodSchema(z.union([
19
+ z.object({
20
+ audio: z.string(),
21
+ isFinal: z.literal(false).nullable(),
22
+ normalizedAlignment: z
23
+ .object({
24
+ chars: z.array(z.string()),
25
+ charStartTimesMs: z.array(z.number()),
26
+ charDurationsMs: z.array(z.number()),
27
+ })
28
+ .nullable(),
29
+ }),
30
+ z.object({
31
+ isFinal: z.literal(true),
32
+ }),
33
+ z.object({
34
+ message: z.string(),
35
+ error: z.string(),
36
+ code: z.number(),
37
+ }),
38
+ ]));
17
39
  /**
18
40
  * Synthesize speech using the ElevenLabs Text to Speech API.
19
41
  *
@@ -62,27 +84,6 @@ export class ElevenLabsSpeechModel extends AbstractModel {
62
84
  async doGenerateSpeechStreamDuplex(textStream
63
85
  // options?: FunctionOptions | undefined
64
86
  ) {
65
- const responseSchema = z.union([
66
- z.object({
67
- audio: z.string(),
68
- isFinal: z.literal(false).nullable(),
69
- normalizedAlignment: z
70
- .object({
71
- chars: z.array(z.string()),
72
- charStartTimesMs: z.array(z.number()),
73
- charDurationsMs: z.array(z.number()),
74
- })
75
- .nullable(),
76
- }),
77
- z.object({
78
- isFinal: z.literal(true),
79
- }),
80
- z.object({
81
- message: z.string(),
82
- error: z.string(),
83
- code: z.number(),
84
- }),
85
- ]);
86
87
  const queue = new AsyncQueue();
87
88
  const model = this.settings.model ?? defaultModel;
88
89
  const socket = await createSimpleWebSocket(`wss://api.elevenlabs.io/v1/text-to-speech/${this.settings.voice}/stream-input${assembleQuery({
@@ -129,7 +130,10 @@ export class ElevenLabsSpeechModel extends AbstractModel {
129
130
  socket.send(JSON.stringify({ text: "" }));
130
131
  };
131
132
  socket.onmessage = (event) => {
132
- const parseResult = safeParseJsonWithZod(event.data, responseSchema);
133
+ const parseResult = safeParseJSON({
134
+ text: event.data,
135
+ schema: streamingResponseSchema,
136
+ });
133
137
  if (!parseResult.success) {
134
138
  queue.push({ type: "error", error: parseResult.error });
135
139
  return;
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.failedHuggingFaceCallResponseHandler = exports.HuggingFaceError = exports.huggingFaceErrorDataSchema = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
6
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
7
- exports.huggingFaceErrorDataSchema = zod_1.z.object({
6
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
7
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
+ exports.huggingFaceErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
9
  error: zod_1.z.array(zod_1.z.string()).or(zod_1.z.string()),
9
- });
10
+ }));
10
11
  class HuggingFaceError extends ApiCallError_js_1.ApiCallError {
11
12
  constructor({ data, statusCode, url, requestBodyValues, message = typeof data.error === "string"
12
13
  ? data.error
@@ -26,6 +27,9 @@ const failedHuggingFaceCallResponseHandler = async ({ response, url, requestBody
26
27
  url,
27
28
  requestBodyValues,
28
29
  statusCode: response.status,
29
- data: (0, parseJSON_js_1.parseJsonWithZod)(await response.text(), exports.huggingFaceErrorDataSchema),
30
+ data: (0, parseJSON_js_1.parseJSON)({
31
+ text: await response.text(),
32
+ schema: exports.huggingFaceErrorDataSchema,
33
+ }),
30
34
  });
31
35
  exports.failedHuggingFaceCallResponseHandler = failedHuggingFaceCallResponseHandler;
@@ -1,14 +1,10 @@
1
- import { z } from "zod";
2
1
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
2
  import { ResponseHandler } from "../../core/api/postToApi.js";
4
- export declare const huggingFaceErrorDataSchema: z.ZodObject<{
5
- error: z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString]>;
6
- }, "strip", z.ZodTypeAny, {
7
- error: (string | string[]) & (string | string[] | undefined);
8
- }, {
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ export declare const huggingFaceErrorDataSchema: ZodSchema<{
9
5
  error: (string | string[]) & (string | string[] | undefined);
10
6
  }>;
11
- export type HuggingFaceErrorData = z.infer<typeof huggingFaceErrorDataSchema>;
7
+ export type HuggingFaceErrorData = (typeof huggingFaceErrorDataSchema)["_type"];
12
8
  export declare class HuggingFaceError extends ApiCallError {
13
9
  readonly data: HuggingFaceErrorData;
14
10
  constructor({ data, statusCode, url, requestBodyValues, message, }: {
@@ -1,9 +1,10 @@
1
1
  import { z } from "zod";
2
2
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
- import { parseJsonWithZod } from "../../util/parseJSON.js";
4
- export const huggingFaceErrorDataSchema = z.object({
3
+ import { parseJSON } from "../../core/schema/parseJSON.js";
4
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
5
+ export const huggingFaceErrorDataSchema = new ZodSchema(z.object({
5
6
  error: z.array(z.string()).or(z.string()),
6
- });
7
+ }));
7
8
  export class HuggingFaceError extends ApiCallError {
8
9
  constructor({ data, statusCode, url, requestBodyValues, message = typeof data.error === "string"
9
10
  ? data.error
@@ -22,5 +23,8 @@ export const failedHuggingFaceCallResponseHandler = async ({ response, url, requ
22
23
  url,
23
24
  requestBodyValues,
24
25
  statusCode: response.status,
25
- data: parseJsonWithZod(await response.text(), huggingFaceErrorDataSchema),
26
+ data: parseJSON({
27
+ text: await response.text(),
28
+ schema: huggingFaceErrorDataSchema,
29
+ }),
26
30
  });
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.failedLlamaCppCallResponseHandler = exports.LlamaCppError = exports.llamaCppErrorDataSchema = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
6
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
7
- exports.llamaCppErrorDataSchema = zod_1.z.object({
6
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
7
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
+ exports.llamaCppErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
9
  error: zod_1.z.string(),
9
- });
10
+ }));
10
11
  class LlamaCppError extends ApiCallError_js_1.ApiCallError {
11
12
  constructor({ data, statusCode, url, requestBodyValues, message = data.error, }) {
12
13
  super({ message, statusCode, requestBodyValues, url });
@@ -24,6 +25,9 @@ const failedLlamaCppCallResponseHandler = async ({ response, url, requestBodyVal
24
25
  url,
25
26
  requestBodyValues,
26
27
  statusCode: response.status,
27
- data: (0, parseJSON_js_1.parseJsonWithZod)(await response.text(), exports.llamaCppErrorDataSchema),
28
+ data: (0, parseJSON_js_1.parseJSON)({
29
+ text: await response.text(),
30
+ schema: exports.llamaCppErrorDataSchema,
31
+ }),
28
32
  });
29
33
  exports.failedLlamaCppCallResponseHandler = failedLlamaCppCallResponseHandler;
@@ -1,14 +1,10 @@
1
- import { z } from "zod";
2
1
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
2
  import { ResponseHandler } from "../../core/api/postToApi.js";
4
- export declare const llamaCppErrorDataSchema: z.ZodObject<{
5
- error: z.ZodString;
6
- }, "strip", z.ZodTypeAny, {
7
- error: string;
8
- }, {
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ export declare const llamaCppErrorDataSchema: ZodSchema<{
9
5
  error: string;
10
6
  }>;
11
- export type LlamaCppErrorData = z.infer<typeof llamaCppErrorDataSchema>;
7
+ export type LlamaCppErrorData = (typeof llamaCppErrorDataSchema)["_type"];
12
8
  export declare class LlamaCppError extends ApiCallError {
13
9
  readonly data: LlamaCppErrorData;
14
10
  constructor({ data, statusCode, url, requestBodyValues, message, }: {
@@ -1,9 +1,10 @@
1
1
  import { z } from "zod";
2
2
  import { ApiCallError } from "../../core/api/ApiCallError.js";
3
- import { parseJsonWithZod } from "../../util/parseJSON.js";
4
- export const llamaCppErrorDataSchema = z.object({
3
+ import { parseJSON } from "../../core/schema/parseJSON.js";
4
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
5
+ export const llamaCppErrorDataSchema = new ZodSchema(z.object({
5
6
  error: z.string(),
6
- });
7
+ }));
7
8
  export class LlamaCppError extends ApiCallError {
8
9
  constructor({ data, statusCode, url, requestBodyValues, message = data.error, }) {
9
10
  super({ message, statusCode, requestBodyValues, url });
@@ -20,5 +21,8 @@ export const failedLlamaCppCallResponseHandler = async ({ response, url, request
20
21
  url,
21
22
  requestBodyValues,
22
23
  statusCode: response.status,
23
- data: parseJsonWithZod(await response.text(), llamaCppErrorDataSchema),
24
+ data: parseJSON({
25
+ text: await response.text(),
26
+ schema: llamaCppErrorDataSchema,
27
+ }),
24
28
  });
@@ -4,11 +4,12 @@ exports.LlamaCppTextGenerationResponseFormat = exports.LlamaCppTextGenerationMod
4
4
  const zod_1 = require("zod");
5
5
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
- const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
8
- const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
7
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
9
8
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
10
9
  const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
11
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
10
+ const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
11
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
12
+ const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
12
13
  const LlamaCppApiConfiguration_js_1 = require("./LlamaCppApiConfiguration.cjs");
13
14
  const LlamaCppError_js_1 = require("./LlamaCppError.cjs");
14
15
  const LlamaCppTokenizer_js_1 = require("./LlamaCppTokenizer.cjs");
@@ -172,13 +173,13 @@ const llamaCppTextGenerationResponseSchema = zod_1.z.object({
172
173
  tokens_predicted: zod_1.z.number(),
173
174
  truncated: zod_1.z.boolean(),
174
175
  });
175
- const llamaCppTextStreamingResponseSchema = zod_1.z.discriminatedUnion("stop", [
176
+ const llamaCppTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("stop", [
176
177
  zod_1.z.object({
177
178
  content: zod_1.z.string(),
178
179
  stop: zod_1.z.literal(false),
179
180
  }),
180
181
  llamaCppTextGenerationResponseSchema,
181
- ]);
182
+ ]));
182
183
  async function callLlamaCppTextGenerationAPI({ api = new LlamaCppApiConfiguration_js_1.LlamaCppApiConfiguration(), abortSignal, responseFormat, prompt, temperature, topK, topP, nPredict, nKeep, stop, tfsZ, typicalP, repeatPenalty, repeatLastN, penalizeNl, mirostat, mirostatTau, mirostatEta, seed, ignoreEos, logitBias, }) {
183
184
  return (0, postToApi_js_1.postJsonToApi)({
184
185
  url: api.assembleUrl(`/completion`),
@@ -224,7 +225,10 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
224
225
  try {
225
226
  for await (const event of events) {
226
227
  const data = event.data;
227
- const eventData = (0, parseJSON_js_1.parseJsonWithZod)(data, llamaCppTextStreamingResponseSchema);
228
+ const eventData = (0, parseJSON_js_1.parseJSON)({
229
+ text: data,
230
+ schema: llamaCppTextStreamingResponseSchema,
231
+ });
228
232
  content += eventData.content;
229
233
  queue.push({
230
234
  type: "delta",