modelfusion 0.57.0 → 0.57.2

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.
@@ -41,5 +41,16 @@ class ApiCallError extends Error {
41
41
  this.cause = cause;
42
42
  this.isRetryable = isRetryable;
43
43
  }
44
+ toJSON() {
45
+ return {
46
+ name: this.name,
47
+ message: this.message,
48
+ url: this.url,
49
+ requestBodyValues: this.requestBodyValues,
50
+ statusCode: this.statusCode,
51
+ cause: this.cause,
52
+ isRetryable: this.isRetryable,
53
+ };
54
+ }
44
55
  }
45
56
  exports.ApiCallError = ApiCallError;
@@ -12,4 +12,13 @@ export declare class ApiCallError extends Error {
12
12
  cause?: unknown;
13
13
  isRetryable?: boolean;
14
14
  });
15
+ toJSON(): {
16
+ name: string;
17
+ message: string;
18
+ url: string;
19
+ requestBodyValues: unknown;
20
+ statusCode: number;
21
+ cause: unknown;
22
+ isRetryable: boolean;
23
+ };
15
24
  }
@@ -38,4 +38,15 @@ export class ApiCallError extends Error {
38
38
  this.cause = cause;
39
39
  this.isRetryable = isRetryable;
40
40
  }
41
+ toJSON() {
42
+ return {
43
+ name: this.name,
44
+ message: this.message,
45
+ url: this.url,
46
+ requestBodyValues: this.requestBodyValues,
47
+ statusCode: this.statusCode,
48
+ cause: this.cause,
49
+ isRetryable: this.isRetryable,
50
+ };
51
+ }
41
52
  }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LoadAPIKeyError = void 0;
4
+ class LoadAPIKeyError extends Error {
5
+ constructor({ message }) {
6
+ super(message);
7
+ this.name = "LoadAPIKeyError";
8
+ }
9
+ toJSON() {
10
+ return {
11
+ name: this.name,
12
+ message: this.message,
13
+ };
14
+ }
15
+ }
16
+ exports.LoadAPIKeyError = LoadAPIKeyError;
@@ -0,0 +1,9 @@
1
+ export declare class LoadAPIKeyError extends Error {
2
+ constructor({ message }: {
3
+ message: string;
4
+ });
5
+ toJSON(): {
6
+ name: string;
7
+ message: string;
8
+ };
9
+ }
@@ -0,0 +1,12 @@
1
+ export class LoadAPIKeyError extends Error {
2
+ constructor({ message }) {
3
+ super(message);
4
+ this.name = "LoadAPIKeyError";
5
+ }
6
+ toJSON() {
7
+ return {
8
+ name: this.name,
9
+ message: this.message,
10
+ };
11
+ }
12
+ }
@@ -20,5 +20,13 @@ class RetryError extends Error {
20
20
  this.reason = reason;
21
21
  this.errors = errors;
22
22
  }
23
+ toJSON() {
24
+ return {
25
+ name: this.name,
26
+ message: this.message,
27
+ reason: this.reason,
28
+ errors: this.errors,
29
+ };
30
+ }
23
31
  }
24
32
  exports.RetryError = RetryError;
@@ -7,4 +7,10 @@ export declare class RetryError extends Error {
7
7
  reason: RetryErrorReason;
8
8
  errors: Array<unknown>;
9
9
  });
10
+ toJSON(): {
11
+ name: string;
12
+ message: string;
13
+ reason: RetryErrorReason;
14
+ errors: unknown[];
15
+ };
10
16
  }
@@ -17,4 +17,12 @@ export class RetryError extends Error {
17
17
  this.reason = reason;
18
18
  this.errors = errors;
19
19
  }
20
+ toJSON() {
21
+ return {
22
+ name: this.name,
23
+ message: this.message,
24
+ reason: this.reason,
25
+ errors: this.errors,
26
+ };
27
+ }
20
28
  }
@@ -1,16 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loadApiKey = void 0;
4
+ const LoadAPIKeyError_js_1 = require("./LoadAPIKeyError.cjs");
4
5
  function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName = "apiKey", description, }) {
5
6
  if (apiKey != null) {
6
7
  return apiKey;
7
8
  }
8
9
  if (typeof process === "undefined") {
9
- throw new Error(`${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration. Environment variables is not supported in this environment.`);
10
+ throw new LoadAPIKeyError_js_1.LoadAPIKeyError({
11
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration. Environment variables is not supported in this environment.`,
12
+ });
10
13
  }
11
14
  apiKey = process.env[environmentVariableName];
12
15
  if (apiKey == null) {
13
- throw new Error(`${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration or set it as an environment variable named ${environmentVariableName}.`);
16
+ throw new LoadAPIKeyError_js_1.LoadAPIKeyError({
17
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration or set it as an environment variable named ${environmentVariableName}.`,
18
+ });
14
19
  }
15
20
  return apiKey;
16
21
  }
@@ -1,13 +1,18 @@
1
+ import { LoadAPIKeyError } from "./LoadAPIKeyError.js";
1
2
  export function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName = "apiKey", description, }) {
2
3
  if (apiKey != null) {
3
4
  return apiKey;
4
5
  }
5
6
  if (typeof process === "undefined") {
6
- throw new Error(`${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration. Environment variables is not supported in this environment.`);
7
+ throw new LoadAPIKeyError({
8
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration. Environment variables is not supported in this environment.`,
9
+ });
7
10
  }
8
11
  apiKey = process.env[environmentVariableName];
9
12
  if (apiKey == null) {
10
- throw new Error(`${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration or set it as an environment variable named ${environmentVariableName}.`);
13
+ throw new LoadAPIKeyError({
14
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter into the API configuration or set it as an environment variable named ${environmentVariableName}.`,
15
+ });
11
16
  }
12
17
  return apiKey;
13
18
  }
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.57.0",
4
+ "version": "0.57.2",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -28,15 +28,16 @@ class FileSystemLogger {
28
28
  });
29
29
  }
30
30
  }
31
- async logError(options) {
31
+ async logError({ run, error, message, }) {
32
32
  const timestamp = Date.now();
33
33
  try {
34
- const logPath = this.logPath(options.run);
35
- return node_fs_1.promises.writeFile((0, node_path_1.join)(logPath, `${timestamp}-error.json`), JSON.stringify({
34
+ const logPath = this.logPath(run);
35
+ await node_fs_1.promises.mkdir(logPath, { recursive: true });
36
+ await node_fs_1.promises.writeFile((0, node_path_1.join)(logPath, `${timestamp}-error.json`), JSON.stringify({
36
37
  timestamp: new Date(timestamp).toISOString(),
37
- runId: options.run.runId,
38
- message: options.message,
39
- error: options.error,
38
+ runId: run.runId,
39
+ message,
40
+ error,
40
41
  }));
41
42
  }
42
43
  catch (error) {
@@ -10,7 +10,7 @@ export declare class FileSystemLogger implements Logger {
10
10
  run: FlowRun<unknown>;
11
11
  event: FunctionEvent;
12
12
  }): Promise<void>;
13
- logError(options: {
13
+ logError({ run, error, message, }: {
14
14
  run: FlowRun<unknown>;
15
15
  message: string;
16
16
  error: unknown;
@@ -25,15 +25,16 @@ export class FileSystemLogger {
25
25
  });
26
26
  }
27
27
  }
28
- async logError(options) {
28
+ async logError({ run, error, message, }) {
29
29
  const timestamp = Date.now();
30
30
  try {
31
- const logPath = this.logPath(options.run);
32
- return fs.writeFile(join(logPath, `${timestamp}-error.json`), JSON.stringify({
31
+ const logPath = this.logPath(run);
32
+ await fs.mkdir(logPath, { recursive: true });
33
+ await fs.writeFile(join(logPath, `${timestamp}-error.json`), JSON.stringify({
33
34
  timestamp: new Date(timestamp).toISOString(),
34
- runId: options.run.runId,
35
- message: options.message,
36
- error: options.error,
35
+ runId: run.runId,
36
+ message,
37
+ error,
37
38
  }));
38
39
  }
39
40
  catch (error) {
@@ -27,6 +27,7 @@ const modelFusionFastifyPlugin = async (fastify, { flow, baseUrl, basePath, asse
27
27
  run,
28
28
  })
29
29
  .catch((error) => {
30
+ console.error("Failed to process flow", error);
30
31
  logger.logError({
31
32
  run,
32
33
  message: "Failed to process flow",
@@ -24,6 +24,7 @@ export const modelFusionFastifyPlugin = async (fastify, { flow, baseUrl, basePat
24
24
  run,
25
25
  })
26
26
  .catch((error) => {
27
+ console.error("Failed to process flow", error);
27
28
  logger.logError({
28
29
  run,
29
30
  message: "Failed to process flow",
@@ -29,5 +29,13 @@ class JSONParseError extends Error {
29
29
  this.cause = cause;
30
30
  this.valueText = valueText;
31
31
  }
32
+ toJSON() {
33
+ return {
34
+ name: this.name,
35
+ message: this.message,
36
+ cause: this.cause,
37
+ valueText: this.valueText,
38
+ };
39
+ }
32
40
  }
33
41
  exports.JSONParseError = JSONParseError;
@@ -6,4 +6,10 @@ export declare class JSONParseError extends Error {
6
6
  valueText: string;
7
7
  cause: unknown;
8
8
  });
9
+ toJSON(): {
10
+ name: string;
11
+ message: string;
12
+ cause: unknown;
13
+ valueText: string;
14
+ };
9
15
  }
@@ -26,4 +26,12 @@ export class JSONParseError extends Error {
26
26
  this.cause = cause;
27
27
  this.valueText = valueText;
28
28
  }
29
+ toJSON() {
30
+ return {
31
+ name: this.name,
32
+ message: this.message,
33
+ cause: this.cause,
34
+ valueText: this.valueText,
35
+ };
36
+ }
29
37
  }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const runSafe_js_1 = require("./runSafe.cjs");
5
+ (0, vitest_1.test)("catch thrown error in sync function", async () => {
6
+ const error = new Error("test error");
7
+ const result = await (0, runSafe_js_1.runSafe)(() => {
8
+ throw error;
9
+ });
10
+ (0, vitest_1.expect)(result).toEqual({
11
+ ok: false,
12
+ error,
13
+ });
14
+ });
15
+ (0, vitest_1.test)("catch thrown error in async function", async () => {
16
+ const error = new Error("test error");
17
+ const result = await (0, runSafe_js_1.runSafe)(async () => {
18
+ throw error;
19
+ });
20
+ (0, vitest_1.expect)(result).toEqual({
21
+ ok: false,
22
+ error,
23
+ });
24
+ });
25
+ (0, vitest_1.test)("catch thrown string", async () => {
26
+ const result = await (0, runSafe_js_1.runSafe)(async () => {
27
+ throw "test error";
28
+ });
29
+ (0, vitest_1.expect)(result).toEqual({
30
+ ok: false,
31
+ error: "test error",
32
+ });
33
+ });
34
+ (0, vitest_1.test)("catch rejected Promise", async () => {
35
+ const error = new Error("test error");
36
+ const result = await (0, runSafe_js_1.runSafe)(async () => {
37
+ return Promise.reject(error);
38
+ });
39
+ (0, vitest_1.expect)(result).toEqual({
40
+ ok: false,
41
+ error,
42
+ });
43
+ });
44
+ (0, vitest_1.test)("no error", async () => {
45
+ const result = await (0, runSafe_js_1.runSafe)(async () => "result");
46
+ (0, vitest_1.expect)(result).toEqual({
47
+ ok: true,
48
+ value: "result",
49
+ });
50
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ import { expect, test } from "vitest";
2
+ import { runSafe } from "./runSafe.js";
3
+ test("catch thrown error in sync function", async () => {
4
+ const error = new Error("test error");
5
+ const result = await runSafe(() => {
6
+ throw error;
7
+ });
8
+ expect(result).toEqual({
9
+ ok: false,
10
+ error,
11
+ });
12
+ });
13
+ test("catch thrown error in async function", async () => {
14
+ const error = new Error("test error");
15
+ const result = await runSafe(async () => {
16
+ throw error;
17
+ });
18
+ expect(result).toEqual({
19
+ ok: false,
20
+ error,
21
+ });
22
+ });
23
+ test("catch thrown string", async () => {
24
+ const result = await runSafe(async () => {
25
+ throw "test error";
26
+ });
27
+ expect(result).toEqual({
28
+ ok: false,
29
+ error: "test error",
30
+ });
31
+ });
32
+ test("catch rejected Promise", async () => {
33
+ const error = new Error("test error");
34
+ const result = await runSafe(async () => {
35
+ return Promise.reject(error);
36
+ });
37
+ expect(result).toEqual({
38
+ ok: false,
39
+ error,
40
+ });
41
+ });
42
+ test("no error", async () => {
43
+ const result = await runSafe(async () => "result");
44
+ expect(result).toEqual({
45
+ ok: true,
46
+ value: "result",
47
+ });
48
+ });