modelfusion 0.24.1 → 0.25.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.
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultRun = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
- const calculateCost_js_1 = require("../cost/calculateCost.cjs");
6
5
  const SuccessfulModelCall_js_1 = require("../model-function/SuccessfulModelCall.cjs");
6
+ const FunctionEventSource_js_1 = require("./FunctionEventSource.cjs");
7
7
  class DefaultRun {
8
- constructor({ runId = `run-${(0, nanoid_1.nanoid)()}`, sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
8
+ constructor({ runId = `run-${(0, nanoid_1.nanoid)()}`, sessionId, userId, abortSignal, observers, errorHandler, } = {}) {
9
9
  Object.defineProperty(this, "runId", {
10
10
  enumerable: true,
11
11
  configurable: true,
@@ -30,7 +30,7 @@ class DefaultRun {
30
30
  writable: true,
31
31
  value: void 0
32
32
  });
33
- Object.defineProperty(this, "costCalculators", {
33
+ Object.defineProperty(this, "errorHandler", {
34
34
  enumerable: true,
35
35
  configurable: true,
36
36
  writable: true,
@@ -42,34 +42,35 @@ class DefaultRun {
42
42
  writable: true,
43
43
  value: []
44
44
  });
45
- Object.defineProperty(this, "observers", {
45
+ Object.defineProperty(this, "functionEventSource", {
46
46
  enumerable: true,
47
47
  configurable: true,
48
48
  writable: true,
49
49
  value: void 0
50
50
  });
51
+ Object.defineProperty(this, "functionObserver", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: {
56
+ onFunctionEvent: (event) => {
57
+ this.events.push(event);
58
+ this.functionEventSource.notify(event);
59
+ },
60
+ }
61
+ });
51
62
  this.runId = runId;
52
63
  this.sessionId = sessionId;
53
64
  this.userId = userId;
54
65
  this.abortSignal = abortSignal;
55
- this.costCalculators = costCalculators;
56
- this.observers = [
57
- {
58
- onFunctionEvent: (event) => {
59
- this.events.push(event);
60
- },
61
- },
62
- ...(observers ?? []),
63
- ];
66
+ this.errorHandler = errorHandler ?? ((error) => console.error(error));
67
+ this.functionEventSource = new FunctionEventSource_js_1.FunctionEventSource({
68
+ observers: observers ?? [],
69
+ errorHandler: this.errorHandler.bind(this),
70
+ });
64
71
  }
65
72
  get successfulModelCalls() {
66
73
  return (0, SuccessfulModelCall_js_1.extractSuccessfulModelCalls)(this.events);
67
74
  }
68
- calculateCost() {
69
- return (0, calculateCost_js_1.calculateCost)({
70
- calls: this.successfulModelCalls,
71
- costCalculators: this.costCalculators,
72
- });
73
- }
74
75
  }
75
76
  exports.DefaultRun = DefaultRun;
@@ -1,24 +1,26 @@
1
- import { CostCalculator } from "../cost/CostCalculator.js";
2
1
  import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
3
- import { Run } from "./Run.js";
2
+ import { ErrorHandler } from "../util/ErrorHandler.js";
4
3
  import { FunctionEvent } from "./FunctionEvent.js";
5
4
  import { FunctionObserver } from "./FunctionObserver.js";
5
+ import { Run } from "./Run.js";
6
6
  export declare class DefaultRun implements Run {
7
7
  readonly runId: string;
8
8
  readonly sessionId?: string;
9
9
  readonly userId?: string;
10
10
  readonly abortSignal?: AbortSignal;
11
- readonly costCalculators: CostCalculator[];
11
+ readonly errorHandler: ErrorHandler;
12
12
  readonly events: FunctionEvent[];
13
- readonly observers?: FunctionObserver[];
14
- constructor({ runId, sessionId, userId, abortSignal, observers, costCalculators, }?: {
13
+ private functionEventSource;
14
+ constructor({ runId, sessionId, userId, abortSignal, observers, errorHandler, }?: {
15
15
  runId?: string;
16
16
  sessionId?: string;
17
17
  userId?: string;
18
18
  abortSignal?: AbortSignal;
19
19
  observers?: FunctionObserver[];
20
- costCalculators?: CostCalculator[];
20
+ errorHandler?: ErrorHandler;
21
21
  });
22
+ readonly functionObserver: {
23
+ onFunctionEvent: (event: FunctionEvent) => void;
24
+ };
22
25
  get successfulModelCalls(): Array<SuccessfulModelCall>;
23
- calculateCost(): Promise<import("../index.js").Cost>;
24
26
  }
@@ -1,8 +1,8 @@
1
1
  import { nanoid as createId } from "nanoid";
2
- import { calculateCost } from "../cost/calculateCost.js";
3
2
  import { extractSuccessfulModelCalls, } from "../model-function/SuccessfulModelCall.js";
3
+ import { FunctionEventSource } from "./FunctionEventSource.js";
4
4
  export class DefaultRun {
5
- constructor({ runId = `run-${createId()}`, sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
5
+ constructor({ runId = `run-${createId()}`, sessionId, userId, abortSignal, observers, errorHandler, } = {}) {
6
6
  Object.defineProperty(this, "runId", {
7
7
  enumerable: true,
8
8
  configurable: true,
@@ -27,7 +27,7 @@ export class DefaultRun {
27
27
  writable: true,
28
28
  value: void 0
29
29
  });
30
- Object.defineProperty(this, "costCalculators", {
30
+ Object.defineProperty(this, "errorHandler", {
31
31
  enumerable: true,
32
32
  configurable: true,
33
33
  writable: true,
@@ -39,33 +39,34 @@ export class DefaultRun {
39
39
  writable: true,
40
40
  value: []
41
41
  });
42
- Object.defineProperty(this, "observers", {
42
+ Object.defineProperty(this, "functionEventSource", {
43
43
  enumerable: true,
44
44
  configurable: true,
45
45
  writable: true,
46
46
  value: void 0
47
47
  });
48
+ Object.defineProperty(this, "functionObserver", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: {
53
+ onFunctionEvent: (event) => {
54
+ this.events.push(event);
55
+ this.functionEventSource.notify(event);
56
+ },
57
+ }
58
+ });
48
59
  this.runId = runId;
49
60
  this.sessionId = sessionId;
50
61
  this.userId = userId;
51
62
  this.abortSignal = abortSignal;
52
- this.costCalculators = costCalculators;
53
- this.observers = [
54
- {
55
- onFunctionEvent: (event) => {
56
- this.events.push(event);
57
- },
58
- },
59
- ...(observers ?? []),
60
- ];
63
+ this.errorHandler = errorHandler ?? ((error) => console.error(error));
64
+ this.functionEventSource = new FunctionEventSource({
65
+ observers: observers ?? [],
66
+ errorHandler: this.errorHandler.bind(this),
67
+ });
61
68
  }
62
69
  get successfulModelCalls() {
63
70
  return extractSuccessfulModelCalls(this.events);
64
71
  }
65
- calculateCost() {
66
- return calculateCost({
67
- calls: this.successfulModelCalls,
68
- costCalculators: this.costCalculators,
69
- });
70
- }
71
72
  }
package/core/Run.d.ts CHANGED
@@ -22,6 +22,10 @@ export interface Run {
22
22
  * to the run.
23
23
  */
24
24
  abortSignal?: AbortSignal;
25
- observers?: FunctionObserver[];
25
+ /**
26
+ * Optional field that represents the run as a function observer. When it is defined,
27
+ * the run will be notified of all function events.
28
+ */
29
+ functionObserver?: FunctionObserver;
26
30
  errorHandler?: ErrorHandler;
27
31
  }
@@ -72,7 +72,7 @@ async function doExecuteCall({ model, options, input, functionType, generateResp
72
72
  ...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
73
73
  ...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
74
74
  ...(settings.observers ?? []),
75
- ...(run?.observers ?? []),
75
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
76
76
  ...(options?.observers ?? []),
77
77
  ],
78
78
  errorHandler: run?.errorHandler,
@@ -67,7 +67,7 @@ async function doExecuteCall({ model, options, input, functionType, generateResp
67
67
  ...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
68
68
  ...getGlobalFunctionObservers(),
69
69
  ...(settings.observers ?? []),
70
- ...(run?.observers ?? []),
70
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
71
71
  ...(options?.observers ?? []),
72
72
  ],
73
73
  errorHandler: run?.errorHandler,
@@ -64,7 +64,7 @@ async function doStreamText(model, prompt, options) {
64
64
  ...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
65
65
  ...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
66
66
  ...(settings.observers ?? []),
67
- ...(run?.observers ?? []),
67
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
68
68
  ...(options?.observers ?? []),
69
69
  ],
70
70
  errorHandler: run?.errorHandler,
@@ -59,7 +59,7 @@ async function doStreamText(model, prompt, options) {
59
59
  ...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
60
60
  ...getGlobalFunctionObservers(),
61
61
  ...(settings.observers ?? []),
62
- ...(run?.observers ?? []),
62
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
63
63
  ...(options?.observers ?? []),
64
64
  ],
65
65
  errorHandler: run?.errorHandler,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build AI applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.24.1",
4
+ "version": "0.25.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -68,7 +68,7 @@
68
68
  "eslint-config-prettier": "9.0.0",
69
69
  "husky": "^8.0.3",
70
70
  "lint-staged": "14.0.1",
71
- "prettier": "3.0.2",
71
+ "prettier": "3.0.3",
72
72
  "rimraf": "5.0.1",
73
73
  "typescript": "5.1.6",
74
74
  "zod": "3.22.2",
@@ -59,7 +59,7 @@ async function doExecuteTool(tool, input, options) {
59
59
  observers: [
60
60
  ...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
61
61
  ...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
62
- ...(run?.observers ?? []),
62
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
63
63
  ...(options?.observers ?? []),
64
64
  ],
65
65
  errorHandler: run?.errorHandler,
@@ -54,7 +54,7 @@ async function doExecuteTool(tool, input, options) {
54
54
  observers: [
55
55
  ...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
56
56
  ...getGlobalFunctionObservers(),
57
- ...(run?.observers ?? []),
57
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
58
58
  ...(options?.observers ?? []),
59
59
  ],
60
60
  errorHandler: run?.errorHandler,