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
@@ -1,11 +1,12 @@
1
1
  import { z } from "zod";
2
2
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
- import { AsyncQueue } from "../../util/AsyncQueue.js";
5
- import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
4
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
6
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
7
6
  import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
8
- import { parseJsonWithZod } from "../../util/parseJSON.js";
7
+ import { AsyncQueue } from "../../util/AsyncQueue.js";
8
+ import { parseJSON } from "../../core/schema/parseJSON.js";
9
+ import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
9
10
  import { LlamaCppApiConfiguration } from "./LlamaCppApiConfiguration.js";
10
11
  import { failedLlamaCppCallResponseHandler } from "./LlamaCppError.js";
11
12
  import { LlamaCppTokenizer } from "./LlamaCppTokenizer.js";
@@ -168,13 +169,13 @@ const llamaCppTextGenerationResponseSchema = z.object({
168
169
  tokens_predicted: z.number(),
169
170
  truncated: z.boolean(),
170
171
  });
171
- const llamaCppTextStreamingResponseSchema = z.discriminatedUnion("stop", [
172
+ const llamaCppTextStreamingResponseSchema = new ZodSchema(z.discriminatedUnion("stop", [
172
173
  z.object({
173
174
  content: z.string(),
174
175
  stop: z.literal(false),
175
176
  }),
176
177
  llamaCppTextGenerationResponseSchema,
177
- ]);
178
+ ]));
178
179
  async function callLlamaCppTextGenerationAPI({ api = new LlamaCppApiConfiguration(), abortSignal, responseFormat, prompt, temperature, topK, topP, nPredict, nKeep, stop, tfsZ, typicalP, repeatPenalty, repeatLastN, penalizeNl, mirostat, mirostatTau, mirostatEta, seed, ignoreEos, logitBias, }) {
179
180
  return postJsonToApi({
180
181
  url: api.assembleUrl(`/completion`),
@@ -220,7 +221,10 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
220
221
  try {
221
222
  for await (const event of events) {
222
223
  const data = event.data;
223
- const eventData = parseJsonWithZod(data, llamaCppTextStreamingResponseSchema);
224
+ const eventData = parseJSON({
225
+ text: data,
226
+ schema: llamaCppTextStreamingResponseSchema,
227
+ });
224
228
  content += eventData.content;
225
229
  queue.push({
226
230
  type: "delta",
@@ -4,6 +4,7 @@ exports.OllamaTextGenerationResponseFormat = exports.OllamaTextGenerationModel =
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 ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
7
8
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
8
9
  const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
9
10
  const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
@@ -120,7 +121,7 @@ const ollamaTextGenerationResponseSchema = zod_1.z.object({
120
121
  eval_duration: zod_1.z.number(),
121
122
  context: zod_1.z.array(zod_1.z.number()),
122
123
  });
123
- const ollamaTextStreamingResponseSchema = zod_1.z.discriminatedUnion("done", [
124
+ const ollamaTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("done", [
124
125
  zod_1.z.object({
125
126
  done: zod_1.z.literal(false),
126
127
  model: zod_1.z.string(),
@@ -141,7 +142,7 @@ const ollamaTextStreamingResponseSchema = zod_1.z.discriminatedUnion("done", [
141
142
  eval_duration: zod_1.z.number(),
142
143
  context: zod_1.z.array(zod_1.z.number()),
143
144
  }),
144
- ]);
145
+ ]));
145
146
  async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration_js_1.OllamaApiConfiguration(), abortSignal, responseFormat, prompt, model, format, contextWindowSize, maxCompletionTokens, mirostat, mirostat_eta, mirostat_tau, num_gpu, num_gqa, num_threads, repeat_last_n, repeat_penalty, seed, stopSequences, temperature, tfs_z, top_k, top_p, system, template, context, raw, }) {
146
147
  return (0, postToApi_js_1.postJsonToApi)({
147
148
  url: api.assembleUrl(`/api/generate`),
@@ -1,6 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
5
6
  import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
6
7
  import { AsyncQueue } from "../../util/AsyncQueue.js";
@@ -116,7 +117,7 @@ const ollamaTextGenerationResponseSchema = z.object({
116
117
  eval_duration: z.number(),
117
118
  context: z.array(z.number()),
118
119
  });
119
- const ollamaTextStreamingResponseSchema = z.discriminatedUnion("done", [
120
+ const ollamaTextStreamingResponseSchema = new ZodSchema(z.discriminatedUnion("done", [
120
121
  z.object({
121
122
  done: z.literal(false),
122
123
  model: z.string(),
@@ -137,7 +138,7 @@ const ollamaTextStreamingResponseSchema = z.discriminatedUnion("done", [
137
138
  eval_duration: z.number(),
138
139
  context: z.array(z.number()),
139
140
  }),
140
- ]);
141
+ ]));
141
142
  async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration(), abortSignal, responseFormat, prompt, model, format, contextWindowSize, maxCompletionTokens, mirostat, mirostat_eta, mirostat_tau, num_gpu, num_gqa, num_threads, repeat_last_n, repeat_penalty, seed, stopSequences, temperature, tfs_z, top_k, top_p, system, template, context, raw, }) {
142
143
  return postJsonToApi({
143
144
  url: api.assembleUrl(`/api/generate`),
@@ -9,11 +9,12 @@ const PromptFormatTextStreamingModel_js_1 = require("../../model-function/genera
9
9
  const TextPromptFormat_js_1 = require("../../model-function/generate-text/prompt-format/TextPromptFormat.cjs");
10
10
  const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
11
11
  const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
12
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
12
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
13
13
  const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
14
14
  const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
15
15
  const OpenAIError_js_1 = require("./OpenAIError.cjs");
16
16
  const TikTokenTokenizer_js_1 = require("./TikTokenTokenizer.cjs");
17
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
17
18
  /**
18
19
  * @see https://platform.openai.com/docs/models/
19
20
  * @see https://openai.com/pricing
@@ -335,7 +336,7 @@ exports.OpenAITextResponseFormat = {
335
336
  handler: async ({ response }) => createOpenAITextFullDeltaIterableQueue(response.body),
336
337
  },
337
338
  };
338
- const textResponseStreamEventSchema = zod_1.z.object({
339
+ const textResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
339
340
  choices: zod_1.z.array(zod_1.z.object({
340
341
  text: zod_1.z.string(),
341
342
  finish_reason: zod_1.z
@@ -349,7 +350,7 @@ const textResponseStreamEventSchema = zod_1.z.object({
349
350
  model: zod_1.z.string(),
350
351
  system_fingerprint: zod_1.z.string().optional(),
351
352
  object: zod_1.z.literal("text_completion"),
352
- });
353
+ }));
353
354
  async function createOpenAITextFullDeltaIterableQueue(stream) {
354
355
  const queue = new AsyncQueue_js_1.AsyncQueue();
355
356
  const streamDelta = [];
@@ -363,7 +364,10 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
363
364
  queue.close();
364
365
  return;
365
366
  }
366
- const eventData = (0, parseJSON_js_1.parseJsonWithZod)(data, textResponseStreamEventSchema);
367
+ const eventData = (0, parseJSON_js_1.parseJSON)({
368
+ text: data,
369
+ schema: textResponseStreamEventSchema,
370
+ });
367
371
  for (let i = 0; i < eventData.choices.length; i++) {
368
372
  const eventChoice = eventData.choices[i];
369
373
  const delta = eventChoice.text;
@@ -6,11 +6,12 @@ import { PromptFormatTextStreamingModel } from "../../model-function/generate-te
6
6
  import { mapChatPromptToTextFormat, mapInstructionPromptToTextFormat, } from "../../model-function/generate-text/prompt-format/TextPromptFormat.js";
7
7
  import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
8
8
  import { AsyncQueue } from "../../util/AsyncQueue.js";
9
- import { parseJsonWithZod } from "../../util/parseJSON.js";
9
+ import { parseJSON } from "../../core/schema/parseJSON.js";
10
10
  import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
11
11
  import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
12
12
  import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
13
13
  import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
14
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
14
15
  /**
15
16
  * @see https://platform.openai.com/docs/models/
16
17
  * @see https://openai.com/pricing
@@ -328,7 +329,7 @@ export const OpenAITextResponseFormat = {
328
329
  handler: async ({ response }) => createOpenAITextFullDeltaIterableQueue(response.body),
329
330
  },
330
331
  };
331
- const textResponseStreamEventSchema = z.object({
332
+ const textResponseStreamEventSchema = new ZodSchema(z.object({
332
333
  choices: z.array(z.object({
333
334
  text: z.string(),
334
335
  finish_reason: z
@@ -342,7 +343,7 @@ const textResponseStreamEventSchema = z.object({
342
343
  model: z.string(),
343
344
  system_fingerprint: z.string().optional(),
344
345
  object: z.literal("text_completion"),
345
- });
346
+ }));
346
347
  async function createOpenAITextFullDeltaIterableQueue(stream) {
347
348
  const queue = new AsyncQueue();
348
349
  const streamDelta = [];
@@ -356,7 +357,10 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
356
357
  queue.close();
357
358
  return;
358
359
  }
359
- const eventData = parseJsonWithZod(data, textResponseStreamEventSchema);
360
+ const eventData = parseJSON({
361
+ text: data,
362
+ schema: textResponseStreamEventSchema,
363
+ });
360
364
  for (let i = 0; i < eventData.choices.length; i++) {
361
365
  const eventChoice = eventData.choices[i];
362
366
  const delta = eventChoice.text;
@@ -3,15 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.failedOpenAICallResponseHandler = exports.OpenAIError = exports.openAIErrorDataSchema = 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.openAIErrorDataSchema = 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.openAIErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
9
  error: zod_1.z.object({
9
10
  message: zod_1.z.string(),
10
11
  type: zod_1.z.string(),
11
12
  param: zod_1.z.any().nullable(),
12
13
  code: zod_1.z.string().nullable(),
13
14
  }),
14
- });
15
+ }));
15
16
  class OpenAIError extends ApiCallError_js_1.ApiCallError {
16
17
  constructor({ data, statusCode, url, requestBodyValues, message, }) {
17
18
  super({
@@ -38,7 +39,10 @@ const failedOpenAICallResponseHandler = async ({ response, url, requestBodyValue
38
39
  const responseBody = await response.text();
39
40
  // resilient parsing in case the response is not JSON or does not match the schema:
40
41
  try {
41
- const parsedError = (0, parseJSON_js_1.parseJsonWithZod)(responseBody, exports.openAIErrorDataSchema);
42
+ const parsedError = (0, parseJSON_js_1.parseJSON)({
43
+ text: responseBody,
44
+ schema: exports.openAIErrorDataSchema,
45
+ });
42
46
  return new OpenAIError({
43
47
  url,
44
48
  requestBodyValues,
@@ -1,31 +1,7 @@
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 openAIErrorDataSchema: z.ZodObject<{
5
- error: z.ZodObject<{
6
- message: z.ZodString;
7
- type: z.ZodString;
8
- param: z.ZodNullable<z.ZodAny>;
9
- code: z.ZodNullable<z.ZodString>;
10
- }, "strip", z.ZodTypeAny, {
11
- message: string;
12
- code: string | null;
13
- type: string;
14
- param?: any;
15
- }, {
16
- message: string;
17
- code: string | null;
18
- type: string;
19
- param?: any;
20
- }>;
21
- }, "strip", z.ZodTypeAny, {
22
- error: {
23
- message: string;
24
- code: string | null;
25
- type: string;
26
- param?: any;
27
- };
28
- }, {
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ export declare const openAIErrorDataSchema: ZodSchema<{
29
5
  error: {
30
6
  message: string;
31
7
  code: string | null;
@@ -33,7 +9,7 @@ export declare const openAIErrorDataSchema: z.ZodObject<{
33
9
  param?: any;
34
10
  };
35
11
  }>;
36
- export type OpenAIErrorData = z.infer<typeof openAIErrorDataSchema>;
12
+ export type OpenAIErrorData = (typeof openAIErrorDataSchema)["_type"];
37
13
  export declare class OpenAIError extends ApiCallError {
38
14
  readonly data?: OpenAIErrorData;
39
15
  constructor({ data, statusCode, url, requestBodyValues, message, }: {
@@ -1,14 +1,15 @@
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 openAIErrorDataSchema = z.object({
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ import { parseJSON } from "../../core/schema/parseJSON.js";
5
+ export const openAIErrorDataSchema = new ZodSchema(z.object({
5
6
  error: z.object({
6
7
  message: z.string(),
7
8
  type: z.string(),
8
9
  param: z.any().nullable(),
9
10
  code: z.string().nullable(),
10
11
  }),
11
- });
12
+ }));
12
13
  export class OpenAIError extends ApiCallError {
13
14
  constructor({ data, statusCode, url, requestBodyValues, message, }) {
14
15
  super({
@@ -34,7 +35,10 @@ export const failedOpenAICallResponseHandler = async ({ response, url, requestBo
34
35
  const responseBody = await response.text();
35
36
  // resilient parsing in case the response is not JSON or does not match the schema:
36
37
  try {
37
- const parsedError = parseJsonWithZod(responseBody, openAIErrorDataSchema);
38
+ const parsedError = parseJSON({
39
+ text: responseBody,
40
+ schema: openAIErrorDataSchema,
41
+ });
38
42
  return new OpenAIError({
39
43
  url,
40
44
  requestBodyValues,
@@ -2,7 +2,7 @@ import { z } from "zod";
2
2
  import { FunctionOptions } from "../../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../../core/api/ApiConfiguration.js";
4
4
  import { ResponseHandler } from "../../../core/api/postToApi.js";
5
- import { StructureDefinition } from "../../../core/structure/StructureDefinition.js";
5
+ import { StructureDefinition } from "../../../core/schema/StructureDefinition.js";
6
6
  import { AbstractModel } from "../../../model-function/AbstractModel.js";
7
7
  import { Delta } from "../../../model-function/Delta.js";
8
8
  import { StructureGenerationModel } from "../../../model-function/generate-structure/StructureGenerationModel.js";
@@ -4,8 +4,9 @@ exports.createOpenAIChatDeltaIterableQueue = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const AsyncQueue_js_1 = require("../../../util/AsyncQueue.cjs");
6
6
  const parseEventSourceStream_js_1 = require("../../../util/streaming/parseEventSourceStream.cjs");
7
- const parseJSON_js_1 = require("../../../util/parseJSON.cjs");
8
- const chatResponseStreamEventSchema = zod_1.z.object({
7
+ const parseJSON_js_1 = require("../../../core/schema/parseJSON.cjs");
8
+ const ZodSchema_js_1 = require("../../../core/schema/ZodSchema.cjs");
9
+ const chatResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
9
10
  id: zod_1.z.string(),
10
11
  choices: zod_1.z.array(zod_1.z.object({
11
12
  delta: zod_1.z.object({
@@ -34,7 +35,7 @@ const chatResponseStreamEventSchema = zod_1.z.object({
34
35
  model: zod_1.z.string(),
35
36
  system_fingerprint: zod_1.z.string().optional(),
36
37
  object: zod_1.z.literal("chat.completion.chunk"),
37
- });
38
+ }));
38
39
  async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
39
40
  const queue = new AsyncQueue_js_1.AsyncQueue();
40
41
  const streamDelta = [];
@@ -48,7 +49,10 @@ async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
48
49
  queue.close();
49
50
  return;
50
51
  }
51
- const parseResult = (0, parseJSON_js_1.safeParseJsonWithZod)(data, chatResponseStreamEventSchema);
52
+ const parseResult = (0, parseJSON_js_1.safeParseJSON)({
53
+ text: data,
54
+ schema: chatResponseStreamEventSchema,
55
+ });
52
56
  if (!parseResult.success) {
53
57
  queue.push({
54
58
  type: "error",
@@ -1,8 +1,9 @@
1
1
  import { z } from "zod";
2
2
  import { AsyncQueue } from "../../../util/AsyncQueue.js";
3
3
  import { parseEventSourceStream } from "../../../util/streaming/parseEventSourceStream.js";
4
- import { safeParseJsonWithZod } from "../../../util/parseJSON.js";
5
- const chatResponseStreamEventSchema = z.object({
4
+ import { safeParseJSON } from "../../../core/schema/parseJSON.js";
5
+ import { ZodSchema } from "../../../core/schema/ZodSchema.js";
6
+ const chatResponseStreamEventSchema = new ZodSchema(z.object({
6
7
  id: z.string(),
7
8
  choices: z.array(z.object({
8
9
  delta: z.object({
@@ -31,7 +32,7 @@ const chatResponseStreamEventSchema = z.object({
31
32
  model: z.string(),
32
33
  system_fingerprint: z.string().optional(),
33
34
  object: z.literal("chat.completion.chunk"),
34
- });
35
+ }));
35
36
  export async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
36
37
  const queue = new AsyncQueue();
37
38
  const streamDelta = [];
@@ -45,7 +46,10 @@ export async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaVal
45
46
  queue.close();
46
47
  return;
47
48
  }
48
- const parseResult = safeParseJsonWithZod(data, chatResponseStreamEventSchema);
49
+ const parseResult = safeParseJSON({
50
+ text: data,
51
+ schema: chatResponseStreamEventSchema,
52
+ });
49
53
  if (!parseResult.success) {
50
54
  queue.push({
51
55
  type: "error",
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.failedStabilityCallResponseHandler = exports.StabilityError = exports.stabilityErrorDataSchema = 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.stabilityErrorDataSchema = 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.stabilityErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
9
  message: zod_1.z.string(),
9
- });
10
+ }));
10
11
  class StabilityError extends ApiCallError_js_1.ApiCallError {
11
12
  constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
12
13
  super({ message, statusCode, requestBodyValues, url });
@@ -22,7 +23,10 @@ class StabilityError extends ApiCallError_js_1.ApiCallError {
22
23
  exports.StabilityError = StabilityError;
23
24
  const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
24
25
  const responseBody = await response.text();
25
- const parsedError = (0, parseJSON_js_1.parseJsonWithZod)(responseBody, exports.stabilityErrorDataSchema);
26
+ const parsedError = (0, parseJSON_js_1.parseJSON)({
27
+ text: responseBody,
28
+ schema: exports.stabilityErrorDataSchema,
29
+ });
26
30
  return new StabilityError({
27
31
  url,
28
32
  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 stabilityErrorDataSchema: 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 stabilityErrorDataSchema: ZodSchema<{
9
5
  message: string;
10
6
  }>;
11
- export type StabilityErrorData = z.infer<typeof stabilityErrorDataSchema>;
7
+ export type StabilityErrorData = (typeof stabilityErrorDataSchema)["_type"];
12
8
  export declare class StabilityError extends ApiCallError {
13
9
  readonly data: StabilityErrorData;
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 stabilityErrorDataSchema = z.object({
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ import { parseJSON } from "../../core/schema/parseJSON.js";
5
+ export const stabilityErrorDataSchema = new ZodSchema(z.object({
5
6
  message: z.string(),
6
- });
7
+ }));
7
8
  export class StabilityError extends ApiCallError {
8
9
  constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
9
10
  super({ message, statusCode, requestBodyValues, url });
@@ -18,7 +19,10 @@ export class StabilityError extends ApiCallError {
18
19
  }
19
20
  export const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
20
21
  const responseBody = await response.text();
21
- const parsedError = parseJsonWithZod(responseBody, stabilityErrorDataSchema);
22
+ const parsedError = parseJSON({
23
+ text: responseBody,
24
+ schema: stabilityErrorDataSchema,
25
+ });
22
26
  return new StabilityError({
23
27
  url,
24
28
  requestBodyValues,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.67.0",
4
+ "version": "0.68.1",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
package/tool/Tool.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { FunctionOptions } from "../core/FunctionOptions.js";
2
- import { JsonSchemaProducer } from "../core/structure/JsonSchemaProducer.js";
3
- import { Schema } from "../core/structure/Schema.js";
4
- import { StructureDefinition } from "../core/structure/StructureDefinition.js";
2
+ import { JsonSchemaProducer } from "../core/schema/JsonSchemaProducer.js";
3
+ import { Schema } from "../core/schema/Schema.js";
4
+ import { StructureDefinition } from "../core/schema/StructureDefinition.js";
5
5
  /**
6
6
  * A tool is a function with a name, description and defined inputs that can be used
7
7
  * by agents and chatbots.
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WebSearchTool = void 0;
4
4
  const zod_1 = require("zod");
5
- const ZodSchema_js_1 = require("../core/structure/ZodSchema.cjs");
5
+ const ZodSchema_js_1 = require("../core/schema/ZodSchema.cjs");
6
6
  const Tool_js_1 = require("./Tool.cjs");
7
7
  const OUTPUT_SCHEMA = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
8
  results: zod_1.z.array(zod_1.z.object({
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../core/FunctionOptions.js";
2
- import { ZodSchema } from "../core/structure/ZodSchema.js";
2
+ import { ZodSchema } from "../core/schema/ZodSchema.js";
3
3
  import { Tool } from "./Tool.js";
4
4
  declare const OUTPUT_SCHEMA: ZodSchema<{
5
5
  results: {
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { ZodSchema } from "../core/structure/ZodSchema.js";
2
+ import { ZodSchema } from "../core/schema/ZodSchema.js";
3
3
  import { Tool } from "./Tool.js";
4
4
  const OUTPUT_SCHEMA = new ZodSchema(z.object({
5
5
  results: z.array(z.object({
package/util/index.cjs CHANGED
@@ -15,9 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./AsyncQueue.cjs"), exports);
18
- __exportStar(require("./JSONParseError.cjs"), exports);
19
18
  __exportStar(require("./cosineSimilarity.cjs"), exports);
20
19
  __exportStar(require("./delay.cjs"), exports);
21
20
  __exportStar(require("./getAudioFileExtension.cjs"), exports);
22
- __exportStar(require("./parseJSON.cjs"), exports);
23
21
  __exportStar(require("./streaming/index.cjs"), exports);
package/util/index.d.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  export * from "./AsyncQueue.js";
2
- export * from "./JSONParseError.js";
3
2
  export * from "./cosineSimilarity.js";
4
3
  export * from "./delay.js";
5
4
  export * from "./getAudioFileExtension.js";
6
- export * from "./parseJSON.js";
7
5
  export * from "./streaming/index.js";
package/util/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  export * from "./AsyncQueue.js";
2
- export * from "./JSONParseError.js";
3
2
  export * from "./cosineSimilarity.js";
4
3
  export * from "./delay.js";
5
4
  export * from "./getAudioFileExtension.js";
6
- export * from "./parseJSON.js";
7
5
  export * from "./streaming/index.js";
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseJsonStream = void 0;
4
- const parseJSON_js_1 = require("../parseJSON.cjs");
4
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
5
5
  function parseJsonStream({ schema, stream, process, onDone, }) {
6
6
  function processLine(line) {
7
- process((0, parseJSON_js_1.parseJsonWithZod)(line, schema));
7
+ process((0, parseJSON_js_1.parseJSON)({ text: line, schema }));
8
8
  }
9
9
  return (async () => {
10
10
  try {
@@ -1,5 +1,6 @@
1
+ import { Schema } from "../../core/schema/Schema.js";
1
2
  export declare function parseJsonStream<T>({ schema, stream, process, onDone, }: {
2
- schema: Zod.Schema<T>;
3
+ schema: Schema<T>;
3
4
  stream: ReadableStream<Uint8Array>;
4
5
  process: (event: T) => void;
5
6
  onDone?: () => void;
@@ -1,7 +1,7 @@
1
- import { parseJsonWithZod } from "../parseJSON.js";
1
+ import { parseJSON } from "../../core/schema/parseJSON.js";
2
2
  export function parseJsonStream({ schema, stream, process, onDone, }) {
3
3
  function processLine(line) {
4
- process(parseJsonWithZod(line, schema));
4
+ process(parseJSON({ text: line, schema }));
5
5
  }
6
6
  return (async () => {
7
7
  try {
@@ -2,13 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MemoryVectorIndex = void 0;
4
4
  const zod_1 = require("zod");
5
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
5
6
  const cosineSimilarity_js_1 = require("../../util/cosineSimilarity.cjs");
6
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
7
- const jsonDataSchema = zod_1.z.array(zod_1.z.object({
7
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
8
+ const jsonDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.array(zod_1.z.object({
8
9
  id: zod_1.z.string(),
9
10
  vector: zod_1.z.array(zod_1.z.number()),
10
11
  data: zod_1.z.unknown(),
11
- }));
12
+ })));
12
13
  /**
13
14
  * A very simple vector index that stores all entries in memory. Useful when you only have
14
15
  * a small number of entries and don't want to set up a real database, e.g. for conversational memory
@@ -25,7 +26,7 @@ class MemoryVectorIndex {
25
26
  }
26
27
  static async deserialize({ serializedData, schema, }) {
27
28
  // validate the outer structure:
28
- const json = (0, parseJSON_js_1.parseJsonWithZod)(serializedData, jsonDataSchema);
29
+ const json = (0, parseJSON_js_1.parseJSON)({ text: serializedData, schema: jsonDataSchema });
29
30
  if (schema != null) {
30
31
  // when a schema is provided, validate all entries:
31
32
  for (const entry of json) {
@@ -1,5 +1,5 @@
1
1
  import { Vector } from "../../core/Vector.js";
2
- import { Schema } from "../../core/structure/Schema.js";
2
+ import { Schema } from "../../core/schema/Schema.js";
3
3
  import { VectorIndex } from "../VectorIndex.js";
4
4
  /**
5
5
  * A very simple vector index that stores all entries in memory. Useful when you only have
@@ -1,11 +1,12 @@
1
1
  import { z } from "zod";
2
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
2
3
  import { cosineSimilarity } from "../../util/cosineSimilarity.js";
3
- import { parseJsonWithZod } from "../../util/parseJSON.js";
4
- const jsonDataSchema = z.array(z.object({
4
+ import { parseJSON } from "../../core/schema/parseJSON.js";
5
+ const jsonDataSchema = new ZodSchema(z.array(z.object({
5
6
  id: z.string(),
6
7
  vector: z.array(z.number()),
7
8
  data: z.unknown(),
8
- }));
9
+ })));
9
10
  /**
10
11
  * A very simple vector index that stores all entries in memory. Useful when you only have
11
12
  * a small number of entries and don't want to set up a real database, e.g. for conversational memory
@@ -22,7 +23,7 @@ export class MemoryVectorIndex {
22
23
  }
23
24
  static async deserialize({ serializedData, schema, }) {
24
25
  // validate the outer structure:
25
- const json = parseJsonWithZod(serializedData, jsonDataSchema);
26
+ const json = parseJSON({ text: serializedData, schema: jsonDataSchema });
26
27
  if (schema != null) {
27
28
  // when a schema is provided, validate all entries:
28
29
  for (const entry of json) {