modelfusion 0.35.2 → 0.36.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.
@@ -1,5 +1,6 @@
1
- import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/ExecuteToolEvent.js";
2
1
  import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
2
+ import { RetrieveFinishedEvent, RetrieveStartedEvent } from "../retriever/RetrieveEvent.js";
3
+ import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/ExecuteToolEvent.js";
3
4
  export interface BaseFunctionEvent {
4
5
  /**
5
6
  * Unique identifier for the function call.
@@ -72,4 +73,4 @@ export interface BaseFunctionFinishedEvent extends BaseFunctionEvent {
72
73
  */
73
74
  result: BaseFunctionFinishedEventResult;
74
75
  }
75
- export type FunctionEvent = ModelCallStartedEvent | ExecuteToolStartedEvent | ModelCallFinishedEvent | ExecuteToolFinishedEvent;
76
+ export type FunctionEvent = ModelCallStartedEvent | ExecuteToolStartedEvent | RetrieveStartedEvent | ModelCallFinishedEvent | ExecuteToolFinishedEvent | RetrieveFinishedEvent;
@@ -17,14 +17,15 @@ function getFunctionCallLogger(logging) {
17
17
  exports.getFunctionCallLogger = getFunctionCallLogger;
18
18
  const basicTextObserver = {
19
19
  onFunctionEvent(event) {
20
+ const text = `[${event.timestamp.toISOString()}] ${event.callId}${event.functionId != null ? ` (${event.functionId})` : ""} - ${event.functionType} ${event.eventType}`;
20
21
  // log based on event type:
21
22
  switch (event.eventType) {
22
23
  case "started": {
23
- console.log(`[${event.timestamp.toISOString()}] ${event.callId} - ${event.functionType} ${event.eventType}`);
24
+ console.log(text);
24
25
  break;
25
26
  }
26
27
  case "finished": {
27
- console.log(`[${event.timestamp.toISOString()}] ${event.callId} - ${event.functionType} ${event.eventType} in ${event.durationInMs}ms`);
28
+ console.log(`${text} in ${event.durationInMs}ms`);
28
29
  break;
29
30
  }
30
31
  }
@@ -13,14 +13,15 @@ export function getFunctionCallLogger(logging) {
13
13
  }
14
14
  const basicTextObserver = {
15
15
  onFunctionEvent(event) {
16
+ const text = `[${event.timestamp.toISOString()}] ${event.callId}${event.functionId != null ? ` (${event.functionId})` : ""} - ${event.functionType} ${event.eventType}`;
16
17
  // log based on event type:
17
18
  switch (event.eventType) {
18
19
  case "started": {
19
- console.log(`[${event.timestamp.toISOString()}] ${event.callId} - ${event.functionType} ${event.eventType}`);
20
+ console.log(text);
20
21
  break;
21
22
  }
22
23
  case "finished": {
23
- console.log(`[${event.timestamp.toISOString()}] ${event.callId} - ${event.functionType} ${event.eventType} in ${event.durationInMs}ms`);
24
+ console.log(`${text} in ${event.durationInMs}ms`);
24
25
  break;
25
26
  }
26
27
  }
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.35.2",
4
+ "version": "0.36.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
2
+ export interface RetrieveStartedEvent extends BaseFunctionStartedEvent {
3
+ functionType: "retrieve";
4
+ query: unknown;
5
+ }
6
+ export interface RetrieveFinishedEvent extends BaseFunctionFinishedEvent {
7
+ functionType: "retrieve";
8
+ query: unknown;
9
+ results: unknown;
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,9 +1,77 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.retrieve = void 0;
4
+ const nanoid_1 = require("nanoid");
5
+ const FunctionEventSource_js_1 = require("../core/FunctionEventSource.cjs");
6
+ const GlobalFunctionLogging_js_1 = require("../core/GlobalFunctionLogging.cjs");
7
+ const GlobalFunctionObservers_js_1 = require("../core/GlobalFunctionObservers.cjs");
8
+ const AbortError_js_1 = require("../core/api/AbortError.cjs");
9
+ const getFunctionCallLogger_js_1 = require("../core/getFunctionCallLogger.cjs");
10
+ const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
11
+ const runSafe_js_1 = require("../util/runSafe.cjs");
4
12
  async function retrieve(retriever, query, options) {
5
- // TODO add error handling, events, duration tracking, etc.
6
- // TODO metadata handling
7
- return retriever.retrieve(query, options);
13
+ const run = options?.run;
14
+ const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
15
+ observers: [
16
+ ...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
17
+ ...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
18
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
19
+ ...(options?.observers ?? []),
20
+ ],
21
+ errorHandler: run?.errorHandler,
22
+ });
23
+ const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
24
+ const startMetadata = {
25
+ functionType: "retrieve",
26
+ callId: `call-${(0, nanoid_1.nanoid)()}`,
27
+ runId: run?.runId,
28
+ sessionId: run?.sessionId,
29
+ userId: run?.userId,
30
+ functionId: options?.functionId,
31
+ query,
32
+ timestamp: durationMeasurement.startDate,
33
+ startTimestamp: durationMeasurement.startDate,
34
+ };
35
+ eventSource.notify({
36
+ eventType: "started",
37
+ ...startMetadata,
38
+ });
39
+ const result = await (0, runSafe_js_1.runSafe)(() => retriever.retrieve(query, options));
40
+ const finishMetadata = {
41
+ eventType: "finished",
42
+ ...startMetadata,
43
+ finishTimestamp: new Date(),
44
+ durationInMs: durationMeasurement.durationInMs,
45
+ };
46
+ if (!result.ok) {
47
+ if (result.isAborted) {
48
+ eventSource.notify({
49
+ ...finishMetadata,
50
+ eventType: "finished",
51
+ result: {
52
+ status: "abort",
53
+ },
54
+ });
55
+ throw new AbortError_js_1.AbortError();
56
+ }
57
+ eventSource.notify({
58
+ ...finishMetadata,
59
+ eventType: "finished",
60
+ result: {
61
+ status: "error",
62
+ error: result.error,
63
+ },
64
+ });
65
+ throw result.error;
66
+ }
67
+ eventSource.notify({
68
+ ...finishMetadata,
69
+ eventType: "finished",
70
+ result: {
71
+ status: "success",
72
+ output: result.output,
73
+ },
74
+ });
75
+ return result.output;
8
76
  }
9
77
  exports.retrieve = retrieve;
@@ -1,5 +1,73 @@
1
+ import { nanoid as createId } from "nanoid";
2
+ import { FunctionEventSource } from "../core/FunctionEventSource.js";
3
+ import { getGlobalFunctionLogging } from "../core/GlobalFunctionLogging.js";
4
+ import { getGlobalFunctionObservers } from "../core/GlobalFunctionObservers.js";
5
+ import { AbortError } from "../core/api/AbortError.js";
6
+ import { getFunctionCallLogger } from "../core/getFunctionCallLogger.js";
7
+ import { startDurationMeasurement } from "../util/DurationMeasurement.js";
8
+ import { runSafe } from "../util/runSafe.js";
1
9
  export async function retrieve(retriever, query, options) {
2
- // TODO add error handling, events, duration tracking, etc.
3
- // TODO metadata handling
4
- return retriever.retrieve(query, options);
10
+ const run = options?.run;
11
+ const eventSource = new FunctionEventSource({
12
+ observers: [
13
+ ...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
14
+ ...getGlobalFunctionObservers(),
15
+ ...(run?.functionObserver != null ? [run.functionObserver] : []),
16
+ ...(options?.observers ?? []),
17
+ ],
18
+ errorHandler: run?.errorHandler,
19
+ });
20
+ const durationMeasurement = startDurationMeasurement();
21
+ const startMetadata = {
22
+ functionType: "retrieve",
23
+ callId: `call-${createId()}`,
24
+ runId: run?.runId,
25
+ sessionId: run?.sessionId,
26
+ userId: run?.userId,
27
+ functionId: options?.functionId,
28
+ query,
29
+ timestamp: durationMeasurement.startDate,
30
+ startTimestamp: durationMeasurement.startDate,
31
+ };
32
+ eventSource.notify({
33
+ eventType: "started",
34
+ ...startMetadata,
35
+ });
36
+ const result = await runSafe(() => retriever.retrieve(query, options));
37
+ const finishMetadata = {
38
+ eventType: "finished",
39
+ ...startMetadata,
40
+ finishTimestamp: new Date(),
41
+ durationInMs: durationMeasurement.durationInMs,
42
+ };
43
+ if (!result.ok) {
44
+ if (result.isAborted) {
45
+ eventSource.notify({
46
+ ...finishMetadata,
47
+ eventType: "finished",
48
+ result: {
49
+ status: "abort",
50
+ },
51
+ });
52
+ throw new AbortError();
53
+ }
54
+ eventSource.notify({
55
+ ...finishMetadata,
56
+ eventType: "finished",
57
+ result: {
58
+ status: "error",
59
+ error: result.error,
60
+ },
61
+ });
62
+ throw result.error;
63
+ }
64
+ eventSource.notify({
65
+ ...finishMetadata,
66
+ eventType: "finished",
67
+ result: {
68
+ status: "success",
69
+ output: result.output,
70
+ },
71
+ });
72
+ return result.output;
5
73
  }