modelfusion 0.67.0 → 0.68.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 (101) hide show
  1. package/browser/invokeFlow.cjs +1 -1
  2. package/browser/invokeFlow.js +1 -1
  3. package/browser/readEventSource.cjs +2 -2
  4. package/browser/readEventSource.d.ts +1 -1
  5. package/browser/readEventSource.js +2 -2
  6. package/browser/readEventSourceStream.cjs +2 -2
  7. package/browser/readEventSourceStream.d.ts +1 -1
  8. package/browser/readEventSourceStream.js +2 -2
  9. package/core/index.cjs +1 -1
  10. package/core/index.d.ts +1 -1
  11. package/core/index.js +1 -1
  12. package/{util → core/schema}/JSONParseError.cjs +6 -6
  13. package/{util → core/schema}/JSONParseError.d.ts +3 -3
  14. package/{util → core/schema}/JSONParseError.js +6 -6
  15. package/core/{structure → schema}/ZodStructureDefinition.cjs +1 -1
  16. package/core/{structure → schema}/ZodStructureDefinition.d.ts +1 -1
  17. package/core/{structure → schema}/ZodStructureDefinition.js +1 -1
  18. package/core/{structure → schema}/index.cjs +2 -0
  19. package/core/{structure → schema}/index.d.ts +2 -0
  20. package/core/{structure → schema}/index.js +2 -0
  21. package/core/schema/parseJSON.cjs +56 -0
  22. package/core/schema/parseJSON.d.ts +58 -0
  23. package/core/schema/parseJSON.js +48 -0
  24. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
  25. package/model-function/generate-structure/StructureGenerationModel.d.ts +1 -1
  26. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +1 -1
  27. package/model-function/generate-structure/generateStructure.d.ts +1 -1
  28. package/model-function/generate-structure/generateStructureOrText.d.ts +1 -1
  29. package/model-function/generate-structure/streamStructure.d.ts +1 -1
  30. package/model-provider/anthropic/AnthropicError.cjs +8 -4
  31. package/model-provider/anthropic/AnthropicError.d.ts +3 -19
  32. package/model-provider/anthropic/AnthropicError.js +8 -4
  33. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +8 -4
  34. package/model-provider/anthropic/AnthropicTextGenerationModel.js +8 -4
  35. package/model-provider/automatic1111/Automatic1111Error.cjs +8 -4
  36. package/model-provider/automatic1111/Automatic1111Error.d.ts +3 -13
  37. package/model-provider/automatic1111/Automatic1111Error.js +8 -4
  38. package/model-provider/cohere/CohereError.cjs +8 -4
  39. package/model-provider/cohere/CohereError.d.ts +3 -7
  40. package/model-provider/cohere/CohereError.js +8 -4
  41. package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -2
  42. package/model-provider/cohere/CohereTextGenerationModel.js +3 -2
  43. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +28 -24
  44. package/model-provider/elevenlabs/ElevenLabsSpeechModel.d.ts +1 -1
  45. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +28 -24
  46. package/model-provider/huggingface/HuggingFaceError.cjs +8 -4
  47. package/model-provider/huggingface/HuggingFaceError.d.ts +3 -7
  48. package/model-provider/huggingface/HuggingFaceError.js +8 -4
  49. package/model-provider/llamacpp/LlamaCppError.cjs +8 -4
  50. package/model-provider/llamacpp/LlamaCppError.d.ts +3 -7
  51. package/model-provider/llamacpp/LlamaCppError.js +8 -4
  52. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +10 -6
  53. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +10 -6
  54. package/model-provider/ollama/OllamaTextGenerationModel.cjs +3 -2
  55. package/model-provider/ollama/OllamaTextGenerationModel.js +3 -2
  56. package/model-provider/openai/OpenAICompletionModel.cjs +8 -4
  57. package/model-provider/openai/OpenAICompletionModel.js +8 -4
  58. package/model-provider/openai/OpenAIError.cjs +8 -4
  59. package/model-provider/openai/OpenAIError.d.ts +3 -27
  60. package/model-provider/openai/OpenAIError.js +8 -4
  61. package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
  62. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +8 -4
  63. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +8 -4
  64. package/model-provider/stability/StabilityError.cjs +8 -4
  65. package/model-provider/stability/StabilityError.d.ts +3 -7
  66. package/model-provider/stability/StabilityError.js +8 -4
  67. package/package.json +1 -1
  68. package/tool/Tool.d.ts +3 -3
  69. package/tool/WebSearchTool.cjs +1 -1
  70. package/tool/WebSearchTool.d.ts +1 -1
  71. package/tool/WebSearchTool.js +1 -1
  72. package/util/index.cjs +0 -2
  73. package/util/index.d.ts +0 -2
  74. package/util/index.js +0 -2
  75. package/util/streaming/parseJsonStream.cjs +2 -2
  76. package/util/streaming/parseJsonStream.d.ts +2 -1
  77. package/util/streaming/parseJsonStream.js +2 -2
  78. package/vector-index/memory/MemoryVectorIndex.cjs +5 -4
  79. package/vector-index/memory/MemoryVectorIndex.d.ts +1 -1
  80. package/vector-index/memory/MemoryVectorIndex.js +5 -4
  81. package/util/parseJSON.cjs +0 -67
  82. package/util/parseJSON.d.ts +0 -18
  83. package/util/parseJSON.js +0 -58
  84. /package/core/{structure → schema}/JsonSchemaProducer.cjs +0 -0
  85. /package/core/{structure → schema}/JsonSchemaProducer.d.ts +0 -0
  86. /package/core/{structure → schema}/JsonSchemaProducer.js +0 -0
  87. /package/core/{structure → schema}/Schema.cjs +0 -0
  88. /package/core/{structure → schema}/Schema.d.ts +0 -0
  89. /package/core/{structure → schema}/Schema.js +0 -0
  90. /package/core/{structure → schema}/StructureDefinition.cjs +0 -0
  91. /package/core/{structure → schema}/StructureDefinition.d.ts +0 -0
  92. /package/core/{structure → schema}/StructureDefinition.js +0 -0
  93. /package/core/{structure → schema}/UncheckedSchema.cjs +0 -0
  94. /package/core/{structure → schema}/UncheckedSchema.d.ts +0 -0
  95. /package/core/{structure → schema}/UncheckedSchema.js +0 -0
  96. /package/core/{structure → schema}/UncheckedStructureDefinition.cjs +0 -0
  97. /package/core/{structure → schema}/UncheckedStructureDefinition.d.ts +0 -0
  98. /package/core/{structure → schema}/UncheckedStructureDefinition.js +0 -0
  99. /package/core/{structure → schema}/ZodSchema.cjs +0 -0
  100. /package/core/{structure → schema}/ZodSchema.d.ts +0 -0
  101. /package/core/{structure → schema}/ZodSchema.js +0 -0
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.invokeFlow = void 0;
4
- const ZodSchema_1 = require("../core/structure/ZodSchema");
4
+ const ZodSchema_1 = require("../core/schema/ZodSchema");
5
5
  const readEventSource_1 = require("./readEventSource");
6
6
  async function invokeFlow({ url, input, schema, onEvent, onStop, }) {
7
7
  const response = await fetch(url, {
@@ -1,4 +1,4 @@
1
- import { ZodSchema } from "../core/structure/ZodSchema";
1
+ import { ZodSchema } from "../core/schema/ZodSchema";
2
2
  import { readEventSource } from "./readEventSource";
3
3
  export async function invokeFlow({ url, input, schema, onEvent, onStop, }) {
4
4
  const response = await fetch(url, {
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.readEventSource = void 0;
4
- const parseJSON_js_1 = require("../util/parseJSON.cjs");
4
+ const parseJSON_js_1 = require("../core/schema/parseJSON.cjs");
5
5
  function readEventSource({ url, schema, onEvent, onError = console.error, onStop, isStopEvent, }) {
6
6
  const eventSource = new EventSource(url);
7
7
  eventSource.onmessage = (event) => {
@@ -11,7 +11,7 @@ function readEventSource({ url, schema, onEvent, onError = console.error, onStop
11
11
  onStop?.(eventSource);
12
12
  return;
13
13
  }
14
- const parseResult = (0, parseJSON_js_1.safeParseJsonWithSchema)(event.data, schema);
14
+ const parseResult = (0, parseJSON_js_1.safeParseJSON)({ text: event.data, schema });
15
15
  if (!parseResult.success) {
16
16
  onError(parseResult.error, eventSource);
17
17
  return;
@@ -1,4 +1,4 @@
1
- import { Schema } from "../core/structure/Schema.js";
1
+ import { Schema } from "../core/schema/Schema.js";
2
2
  export declare function readEventSource<T>({ url, schema, onEvent, onError, onStop, isStopEvent, }: {
3
3
  url: string;
4
4
  schema: Schema<T>;
@@ -1,4 +1,4 @@
1
- import { safeParseJsonWithSchema } from "../util/parseJSON.js";
1
+ import { safeParseJSON } from "../core/schema/parseJSON.js";
2
2
  export function readEventSource({ url, schema, onEvent, onError = console.error, onStop, isStopEvent, }) {
3
3
  const eventSource = new EventSource(url);
4
4
  eventSource.onmessage = (event) => {
@@ -8,7 +8,7 @@ export function readEventSource({ url, schema, onEvent, onError = console.error,
8
8
  onStop?.(eventSource);
9
9
  return;
10
10
  }
11
- const parseResult = safeParseJsonWithSchema(event.data, schema);
11
+ const parseResult = safeParseJSON({ text: event.data, schema });
12
12
  if (!parseResult.success) {
13
13
  onError(parseResult.error, eventSource);
14
14
  return;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.readEventSourceStream = void 0;
4
- const parseJSON_js_1 = require("../util/parseJSON.cjs");
4
+ const parseJSON_js_1 = require("../core/schema/parseJSON.cjs");
5
5
  const AsyncQueue_js_1 = require("../util/AsyncQueue.cjs");
6
6
  const parseEventSourceStream_js_1 = require("../util/streaming/parseEventSourceStream.cjs");
7
7
  function readEventSourceStream({ stream, schema, errorHandler, }) {
@@ -11,7 +11,7 @@ function readEventSourceStream({ stream, schema, errorHandler, }) {
11
11
  .then(async (events) => {
12
12
  try {
13
13
  for await (const event of events) {
14
- const validationResult = (0, parseJSON_js_1.safeParseJsonWithSchema)(event.data, schema);
14
+ const validationResult = (0, parseJSON_js_1.safeParseJSON)({ text: event.data, schema });
15
15
  if (!validationResult.success) {
16
16
  errorHandler?.(validationResult.error);
17
17
  continue;
@@ -1,4 +1,4 @@
1
- import { Schema } from "../core/structure/Schema.js";
1
+ import { Schema } from "../core/schema/Schema.js";
2
2
  import { ErrorHandler } from "../util/ErrorHandler.js";
3
3
  export declare function readEventSourceStream<T>({ stream, schema, errorHandler, }: {
4
4
  stream: ReadableStream<Uint8Array>;
@@ -1,4 +1,4 @@
1
- import { safeParseJsonWithSchema } from "../util/parseJSON.js";
1
+ import { safeParseJSON } from "../core/schema/parseJSON.js";
2
2
  import { AsyncQueue } from "../util/AsyncQueue.js";
3
3
  import { parseEventSourceStream } from "../util/streaming/parseEventSourceStream.js";
4
4
  export function readEventSourceStream({ stream, schema, errorHandler, }) {
@@ -8,7 +8,7 @@ export function readEventSourceStream({ stream, schema, errorHandler, }) {
8
8
  .then(async (events) => {
9
9
  try {
10
10
  for await (const event of events) {
11
- const validationResult = safeParseJsonWithSchema(event.data, schema);
11
+ const validationResult = safeParseJSON({ text: event.data, schema });
12
12
  if (!validationResult.success) {
13
13
  errorHandler?.(validationResult.error);
14
14
  continue;
package/core/index.cjs CHANGED
@@ -27,4 +27,4 @@ __exportStar(require("./Vector.cjs"), exports);
27
27
  __exportStar(require("./api/index.cjs"), exports);
28
28
  __exportStar(require("./executeFunction.cjs"), exports);
29
29
  __exportStar(require("./getRun.cjs"), exports);
30
- __exportStar(require("./structure/index.cjs"), exports);
30
+ __exportStar(require("./schema/index.cjs"), exports);
package/core/index.d.ts CHANGED
@@ -11,4 +11,4 @@ export * from "./Vector.js";
11
11
  export * from "./api/index.js";
12
12
  export * from "./executeFunction.js";
13
13
  export * from "./getRun.js";
14
- export * from "./structure/index.js";
14
+ export * from "./schema/index.js";
package/core/index.js CHANGED
@@ -11,4 +11,4 @@ export * from "./Vector.js";
11
11
  export * from "./api/index.js";
12
12
  export * from "./executeFunction.js";
13
13
  export * from "./getRun.js";
14
- export * from "./structure/index.js";
14
+ export * from "./schema/index.js";
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.JSONParseError = void 0;
4
- const getErrorMessage_js_1 = require("./getErrorMessage.cjs");
4
+ const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
5
  class JSONParseError extends Error {
6
- constructor({ valueText, cause }) {
6
+ constructor({ text, cause }) {
7
7
  super(`JSON parsing failed: ` +
8
- `Value: ${valueText}.\n` +
8
+ `Text: ${text}.\n` +
9
9
  `Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
10
10
  Object.defineProperty(this, "structureName", {
11
11
  enumerable: true,
@@ -19,7 +19,7 @@ class JSONParseError extends Error {
19
19
  writable: true,
20
20
  value: void 0
21
21
  });
22
- Object.defineProperty(this, "valueText", {
22
+ Object.defineProperty(this, "text", {
23
23
  enumerable: true,
24
24
  configurable: true,
25
25
  writable: true,
@@ -27,14 +27,14 @@ class JSONParseError extends Error {
27
27
  });
28
28
  this.name = "JSONParseError";
29
29
  this.cause = cause;
30
- this.valueText = valueText;
30
+ this.text = text;
31
31
  }
32
32
  toJSON() {
33
33
  return {
34
34
  name: this.name,
35
35
  message: this.message,
36
36
  cause: this.cause,
37
- valueText: this.valueText,
37
+ valueText: this.text,
38
38
  stack: this.stack,
39
39
  };
40
40
  }
@@ -1,9 +1,9 @@
1
1
  export declare class JSONParseError extends Error {
2
2
  readonly structureName: string;
3
3
  readonly cause: unknown;
4
- readonly valueText: string;
5
- constructor({ valueText, cause }: {
6
- valueText: string;
4
+ readonly text: string;
5
+ constructor({ text, cause }: {
6
+ text: string;
7
7
  cause: unknown;
8
8
  });
9
9
  toJSON(): {
@@ -1,8 +1,8 @@
1
- import { getErrorMessage } from "./getErrorMessage.js";
1
+ import { getErrorMessage } from "../../util/getErrorMessage.js";
2
2
  export class JSONParseError extends Error {
3
- constructor({ valueText, cause }) {
3
+ constructor({ text, cause }) {
4
4
  super(`JSON parsing failed: ` +
5
- `Value: ${valueText}.\n` +
5
+ `Text: ${text}.\n` +
6
6
  `Error message: ${getErrorMessage(cause)}`);
7
7
  Object.defineProperty(this, "structureName", {
8
8
  enumerable: true,
@@ -16,7 +16,7 @@ export class JSONParseError extends Error {
16
16
  writable: true,
17
17
  value: void 0
18
18
  });
19
- Object.defineProperty(this, "valueText", {
19
+ Object.defineProperty(this, "text", {
20
20
  enumerable: true,
21
21
  configurable: true,
22
22
  writable: true,
@@ -24,14 +24,14 @@ export class JSONParseError extends Error {
24
24
  });
25
25
  this.name = "JSONParseError";
26
26
  this.cause = cause;
27
- this.valueText = valueText;
27
+ this.text = text;
28
28
  }
29
29
  toJSON() {
30
30
  return {
31
31
  name: this.name,
32
32
  message: this.message,
33
33
  cause: this.cause,
34
- valueText: this.valueText,
34
+ valueText: this.text,
35
35
  stack: this.stack,
36
36
  };
37
37
  }
@@ -24,7 +24,7 @@ class ZodStructureDefinition {
24
24
  });
25
25
  this.name = name;
26
26
  this.description = description;
27
- this.schema = new ZodSchema_js_1.ZodSchema(schema);
27
+ this.schema = schema instanceof ZodSchema_js_1.ZodSchema ? schema : new ZodSchema_js_1.ZodSchema(schema);
28
28
  }
29
29
  }
30
30
  exports.ZodStructureDefinition = ZodStructureDefinition;
@@ -8,6 +8,6 @@ export declare class ZodStructureDefinition<NAME extends string, STRUCTURE> impl
8
8
  constructor({ name, description, schema, }: {
9
9
  name: NAME;
10
10
  description?: string;
11
- schema: z.Schema<STRUCTURE>;
11
+ schema: z.Schema<STRUCTURE> | ZodSchema<STRUCTURE>;
12
12
  });
13
13
  }
@@ -21,6 +21,6 @@ export class ZodStructureDefinition {
21
21
  });
22
22
  this.name = name;
23
23
  this.description = description;
24
- this.schema = new ZodSchema(schema);
24
+ this.schema = schema instanceof ZodSchema ? schema : new ZodSchema(schema);
25
25
  }
26
26
  }
@@ -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("./JSONParseError.cjs"), exports);
17
18
  __exportStar(require("./JsonSchemaProducer.cjs"), exports);
18
19
  __exportStar(require("./Schema.cjs"), exports);
19
20
  __exportStar(require("./StructureDefinition.cjs"), exports);
@@ -21,3 +22,4 @@ __exportStar(require("./UncheckedSchema.cjs"), exports);
21
22
  __exportStar(require("./UncheckedStructureDefinition.cjs"), exports);
22
23
  __exportStar(require("./ZodSchema.cjs"), exports);
23
24
  __exportStar(require("./ZodStructureDefinition.cjs"), exports);
25
+ __exportStar(require("./parseJSON.cjs"), exports);
@@ -1,3 +1,4 @@
1
+ export * from "./JSONParseError.js";
1
2
  export * from "./JsonSchemaProducer.js";
2
3
  export * from "./Schema.js";
3
4
  export * from "./StructureDefinition.js";
@@ -5,3 +6,4 @@ export * from "./UncheckedSchema.js";
5
6
  export * from "./UncheckedStructureDefinition.js";
6
7
  export * from "./ZodSchema.js";
7
8
  export * from "./ZodStructureDefinition.js";
9
+ export * from "./parseJSON.js";
@@ -1,3 +1,4 @@
1
+ export * from "./JSONParseError.js";
1
2
  export * from "./JsonSchemaProducer.js";
2
3
  export * from "./Schema.js";
3
4
  export * from "./StructureDefinition.js";
@@ -5,3 +6,4 @@ export * from "./UncheckedSchema.js";
5
6
  export * from "./UncheckedStructureDefinition.js";
6
7
  export * from "./ZodSchema.js";
7
8
  export * from "./ZodStructureDefinition.js";
9
+ export * from "./parseJSON.js";
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.safeParseJSON = exports.parseJSON = void 0;
7
+ const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
+ const JSONParseError_js_1 = require("./JSONParseError.cjs");
9
+ function parseJSON({ text, schema, }) {
10
+ try {
11
+ const json = secure_json_parse_1.default.parse(text);
12
+ if (schema == null) {
13
+ return json;
14
+ }
15
+ const validationResult = schema.validate(json);
16
+ if (!validationResult.success) {
17
+ throw new JSONParseError_js_1.JSONParseError({ text, cause: validationResult.error });
18
+ }
19
+ return validationResult.data;
20
+ }
21
+ catch (error) {
22
+ if (error instanceof JSONParseError_js_1.JSONParseError) {
23
+ throw error;
24
+ }
25
+ throw new JSONParseError_js_1.JSONParseError({ text, cause: error });
26
+ }
27
+ }
28
+ exports.parseJSON = parseJSON;
29
+ function safeParseJSON({ text, schema, }) {
30
+ try {
31
+ const json = secure_json_parse_1.default.parse(text);
32
+ if (schema == null) {
33
+ return {
34
+ success: true,
35
+ data: json,
36
+ };
37
+ }
38
+ const validationResult = schema.validate(json);
39
+ if (validationResult.success) {
40
+ return validationResult;
41
+ }
42
+ return {
43
+ success: false,
44
+ error: new JSONParseError_js_1.JSONParseError({ text, cause: validationResult.error }),
45
+ };
46
+ }
47
+ catch (error) {
48
+ return {
49
+ success: false,
50
+ error: error instanceof JSONParseError_js_1.JSONParseError
51
+ ? error
52
+ : new JSONParseError_js_1.JSONParseError({ text, cause: error }),
53
+ };
54
+ }
55
+ }
56
+ exports.safeParseJSON = safeParseJSON;
@@ -0,0 +1,58 @@
1
+ import { JSONParseError } from "./JSONParseError.js";
2
+ import { Schema } from "./Schema.js";
3
+ /**
4
+ * Parses a JSON string into an unknown object.
5
+ *
6
+ * @param text - The JSON string to parse.
7
+ * @returns {unknown} - The parsed JSON object.
8
+ */
9
+ export declare function parseJSON({ text, schema, }: {
10
+ text: string;
11
+ schema?: undefined;
12
+ }): unknown;
13
+ /**
14
+ * Parses a JSON string into a strongly-typed object using the provided schema.
15
+ *
16
+ * @template T - The type of the object to parse the JSON into.
17
+ * @param {string} text - The JSON string to parse.
18
+ * @param {Schema<T>} schema - The schema to use for parsing the JSON.
19
+ * @returns {T} - The parsed object.
20
+ */
21
+ export declare function parseJSON<T>({ text, schema, }: {
22
+ text: string;
23
+ schema: Schema<T>;
24
+ }): T;
25
+ /**
26
+ * Safely parses a JSON string and returns the result as an object of type `unknown`.
27
+ *
28
+ * @param text - The JSON string to parse.
29
+ * @returns {object} Either an object with `success: true` and the parsed data, or an object with `success: false` and the error that occurred.
30
+ */
31
+ export declare function safeParseJSON({ text, schema, }: {
32
+ text: string;
33
+ schema?: undefined;
34
+ }): {
35
+ success: true;
36
+ data: unknown;
37
+ } | {
38
+ success: false;
39
+ error: JSONParseError;
40
+ };
41
+ /**
42
+ * Safely parses a JSON string into a strongly-typed object, using a provided schema to validate the structure.
43
+ *
44
+ * @template T - The type of the object to parse the JSON into.
45
+ * @param {string} text - The JSON string to parse.
46
+ * @param {Schema<T>} schema - The schema to use for parsing the JSON.
47
+ * @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
48
+ */
49
+ export declare function safeParseJSON<T>({ text, schema, }: {
50
+ text: string;
51
+ schema: Schema<T>;
52
+ }): {
53
+ success: true;
54
+ data: T;
55
+ } | {
56
+ success: false;
57
+ error: JSONParseError;
58
+ };
@@ -0,0 +1,48 @@
1
+ import SecureJSON from "secure-json-parse";
2
+ import { JSONParseError } from "./JSONParseError.js";
3
+ export function parseJSON({ text, schema, }) {
4
+ try {
5
+ const json = SecureJSON.parse(text);
6
+ if (schema == null) {
7
+ return json;
8
+ }
9
+ const validationResult = schema.validate(json);
10
+ if (!validationResult.success) {
11
+ throw new JSONParseError({ text, cause: validationResult.error });
12
+ }
13
+ return validationResult.data;
14
+ }
15
+ catch (error) {
16
+ if (error instanceof JSONParseError) {
17
+ throw error;
18
+ }
19
+ throw new JSONParseError({ text, cause: error });
20
+ }
21
+ }
22
+ export function safeParseJSON({ text, schema, }) {
23
+ try {
24
+ const json = SecureJSON.parse(text);
25
+ if (schema == null) {
26
+ return {
27
+ success: true,
28
+ data: json,
29
+ };
30
+ }
31
+ const validationResult = schema.validate(json);
32
+ if (validationResult.success) {
33
+ return validationResult;
34
+ }
35
+ return {
36
+ success: false,
37
+ error: new JSONParseError({ text, cause: validationResult.error }),
38
+ };
39
+ }
40
+ catch (error) {
41
+ return {
42
+ success: false,
43
+ error: error instanceof JSONParseError
44
+ ? error
45
+ : new JSONParseError({ text, cause: error }),
46
+ };
47
+ }
48
+ }
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
2
+ import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
3
3
  import { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
4
4
  import { StructureGenerationModel } from "./StructureGenerationModel.js";
5
5
  export type StructureFromTextPromptFormat<PROMPT> = {
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
2
+ import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
3
3
  import { Delta } from "../../model-function/Delta.js";
4
4
  import { Model, ModelSettings } from "../Model.js";
5
5
  export interface StructureGenerationModelSettings extends ModelSettings {
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
2
+ import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
3
3
  import { Model, ModelSettings } from "../Model.js";
4
4
  export interface StructureOrTextGenerationModelSettings extends ModelSettings {
5
5
  }
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
2
+ import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
3
3
  import { ModelCallMetadata } from "../ModelCallMetadata.js";
4
4
  import { StructureGenerationModel, StructureGenerationModelSettings } from "./StructureGenerationModel.js";
5
5
  /**
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
2
+ import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
3
3
  import { ModelCallMetadata } from "../ModelCallMetadata.js";
4
4
  import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings } from "./StructureOrTextGenerationModel.js";
5
5
  type StructureDefinitionArray<T extends StructureDefinition<any, any>[]> = T;
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
2
+ import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
3
3
  import { ModelCallMetadata } from "../ModelCallMetadata.js";
4
4
  import { StructureStreamingModel } from "./StructureGenerationModel.js";
5
5
  export type StructureStreamPart<STRUCTURE> = {
@@ -3,13 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.failedAnthropicCallResponseHandler = exports.AnthropicError = exports.anthropicErrorDataSchema = 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.anthropicErrorDataSchema = 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.anthropicErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
8
9
  error: zod_1.z.object({
9
10
  type: zod_1.z.string(),
10
11
  message: zod_1.z.string(),
11
12
  }),
12
- });
13
+ }));
13
14
  class AnthropicError extends ApiCallError_js_1.ApiCallError {
14
15
  constructor({ data, statusCode, url, requestBodyValues, message = data.error.message, }) {
15
16
  super({ message, statusCode, requestBodyValues, url });
@@ -27,6 +28,9 @@ const failedAnthropicCallResponseHandler = async ({ response, url, requestBodyVa
27
28
  url,
28
29
  requestBodyValues,
29
30
  statusCode: response.status,
30
- data: (0, parseJSON_js_1.parseJsonWithZod)(await response.text(), exports.anthropicErrorDataSchema),
31
+ data: (0, parseJSON_js_1.parseJSON)({
32
+ text: await response.text(),
33
+ schema: exports.anthropicErrorDataSchema,
34
+ }),
31
35
  });
32
36
  exports.failedAnthropicCallResponseHandler = failedAnthropicCallResponseHandler;
@@ -1,29 +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 anthropicErrorDataSchema: z.ZodObject<{
5
- error: z.ZodObject<{
6
- type: z.ZodString;
7
- message: z.ZodString;
8
- }, "strip", z.ZodTypeAny, {
9
- message: string;
10
- type: string;
11
- }, {
12
- message: string;
13
- type: string;
14
- }>;
15
- }, "strip", z.ZodTypeAny, {
16
- error: {
17
- message: string;
18
- type: string;
19
- };
20
- }, {
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ export declare const anthropicErrorDataSchema: ZodSchema<{
21
5
  error: {
22
6
  message: string;
23
7
  type: string;
24
8
  };
25
9
  }>;
26
- export type AnthropicErrorData = z.infer<typeof anthropicErrorDataSchema>;
10
+ export type AnthropicErrorData = (typeof anthropicErrorDataSchema)["_type"];
27
11
  export declare class AnthropicError extends ApiCallError {
28
12
  readonly data: AnthropicErrorData;
29
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 anthropicErrorDataSchema = z.object({
3
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ import { parseJSON } from "../../core/schema/parseJSON.js";
5
+ export const anthropicErrorDataSchema = new ZodSchema(z.object({
5
6
  error: z.object({
6
7
  type: z.string(),
7
8
  message: z.string(),
8
9
  }),
9
- });
10
+ }));
10
11
  export class AnthropicError extends ApiCallError {
11
12
  constructor({ data, statusCode, url, requestBodyValues, message = data.error.message, }) {
12
13
  super({ message, statusCode, requestBodyValues, url });
@@ -23,5 +24,8 @@ export const failedAnthropicCallResponseHandler = async ({ response, url, reques
23
24
  url,
24
25
  requestBodyValues,
25
26
  statusCode: response.status,
26
- data: parseJsonWithZod(await response.text(), anthropicErrorDataSchema),
27
+ data: parseJSON({
28
+ text: await response.text(),
29
+ schema: anthropicErrorDataSchema,
30
+ }),
27
31
  });
@@ -8,7 +8,8 @@ const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSour
8
8
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
9
  const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
10
10
  const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
11
- const parseJSON_js_1 = require("../../util/parseJSON.cjs");
11
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
12
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
12
13
  const AnthropicApiConfiguration_js_1 = require("./AnthropicApiConfiguration.cjs");
13
14
  const AnthropicError_js_1 = require("./AnthropicError.cjs");
14
15
  const AnthropicPromptFormat_js_1 = require("./AnthropicPromptFormat.cjs");
@@ -158,11 +159,11 @@ async function callAnthropicTextGenerationAPI({ api = new AnthropicApiConfigurat
158
159
  abortSignal,
159
160
  });
160
161
  }
161
- const anthropicTextStreamingResponseSchema = zod_1.z.object({
162
+ const anthropicTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
162
163
  completion: zod_1.z.string(),
163
164
  stop_reason: zod_1.z.string().nullable(),
164
165
  model: zod_1.z.string(),
165
- });
166
+ }));
166
167
  async function createAnthropicFullDeltaIterableQueue(stream) {
167
168
  const queue = new AsyncQueue_js_1.AsyncQueue();
168
169
  let content = "";
@@ -180,7 +181,10 @@ async function createAnthropicFullDeltaIterableQueue(stream) {
180
181
  continue;
181
182
  }
182
183
  const data = event.data;
183
- const eventData = (0, parseJSON_js_1.parseJsonWithZod)(data, anthropicTextStreamingResponseSchema);
184
+ const eventData = (0, parseJSON_js_1.parseJSON)({
185
+ text: data,
186
+ schema: anthropicTextStreamingResponseSchema,
187
+ });
184
188
  content += eventData.completion;
185
189
  queue.push({
186
190
  type: "delta",