modelfusion 0.20.1 → 0.22.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 (149) hide show
  1. package/README.md +5 -7
  2. package/composed-function/summarize/SummarizationFunction.d.ts +3 -3
  3. package/composed-function/summarize/summarizeRecursively.d.ts +1 -1
  4. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +1 -1
  5. package/{run → core}/ConsoleLogger.cjs +1 -4
  6. package/core/ConsoleLogger.d.ts +5 -0
  7. package/core/ConsoleLogger.js +5 -0
  8. package/{run → core}/DefaultRun.cjs +2 -5
  9. package/{run → core}/DefaultRun.d.ts +5 -5
  10. package/{run → core}/DefaultRun.js +2 -5
  11. package/core/FunctionEvent.d.ts +75 -0
  12. package/{run/RunFunctionEventSource.js → core/FunctionEventSource.cjs} +7 -13
  13. package/core/FunctionEventSource.d.ts +12 -0
  14. package/{run/RunFunctionEventSource.cjs → core/FunctionEventSource.js} +3 -17
  15. package/core/FunctionObserver.d.ts +7 -0
  16. package/core/FunctionOptions.d.ts +19 -0
  17. package/core/GlobalFunctionObservers.cjs +12 -0
  18. package/core/GlobalFunctionObservers.d.ts +3 -0
  19. package/core/GlobalFunctionObservers.js +7 -0
  20. package/{run → core}/Run.d.ts +2 -2
  21. package/{run → core}/index.cjs +5 -5
  22. package/core/index.d.ts +9 -0
  23. package/core/index.js +9 -0
  24. package/index.cjs +1 -1
  25. package/index.d.ts +1 -1
  26. package/index.js +1 -1
  27. package/model-function/Model.d.ts +5 -2
  28. package/model-function/ModelCallEvent.d.ts +20 -5
  29. package/model-function/ModelFunctionOptions.d.ts +4 -0
  30. package/model-function/SuccessfulModelCall.cjs +6 -16
  31. package/model-function/SuccessfulModelCall.d.ts +2 -2
  32. package/model-function/SuccessfulModelCall.js +6 -16
  33. package/model-function/embed-text/TextEmbeddingEvent.d.ts +15 -17
  34. package/model-function/embed-text/TextEmbeddingModel.d.ts +3 -3
  35. package/model-function/embed-text/embedText.cjs +6 -67
  36. package/model-function/embed-text/embedText.d.ts +4 -4
  37. package/model-function/embed-text/embedText.js +6 -67
  38. package/model-function/executeCall.cjs +49 -14
  39. package/model-function/executeCall.d.ts +9 -10
  40. package/model-function/executeCall.js +49 -14
  41. package/model-function/generate-image/ImageGenerationEvent.d.ts +12 -16
  42. package/model-function/generate-image/ImageGenerationModel.d.ts +2 -2
  43. package/model-function/generate-image/generateImage.cjs +2 -30
  44. package/model-function/generate-image/generateImage.d.ts +2 -2
  45. package/model-function/generate-image/generateImage.js +2 -30
  46. package/model-function/generate-json/GenerateJsonModel.d.ts +2 -2
  47. package/model-function/generate-json/GenerateJsonOrTextModel.d.ts +2 -2
  48. package/model-function/generate-json/JsonGenerationEvent.d.ts +6 -21
  49. package/model-function/generate-json/JsonTextGenerationModel.d.ts +2 -2
  50. package/model-function/generate-json/generateJson.cjs +2 -30
  51. package/model-function/generate-json/generateJson.d.ts +2 -2
  52. package/model-function/generate-json/generateJson.js +2 -30
  53. package/model-function/generate-json/generateJsonOrText.cjs +2 -30
  54. package/model-function/generate-json/generateJsonOrText.d.ts +2 -2
  55. package/model-function/generate-json/generateJsonOrText.js +2 -30
  56. package/model-function/generate-text/TextGenerationEvent.d.ts +13 -15
  57. package/model-function/generate-text/TextGenerationModel.d.ts +3 -3
  58. package/model-function/generate-text/TextStreamingEvent.d.ts +7 -22
  59. package/model-function/generate-text/generateText.cjs +2 -30
  60. package/model-function/generate-text/generateText.d.ts +2 -2
  61. package/model-function/generate-text/generateText.js +2 -30
  62. package/model-function/generate-text/streamText.cjs +52 -42
  63. package/model-function/generate-text/streamText.d.ts +5 -5
  64. package/model-function/generate-text/streamText.js +52 -42
  65. package/model-function/index.cjs +1 -1
  66. package/model-function/index.d.ts +1 -1
  67. package/model-function/index.js +1 -1
  68. package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +14 -15
  69. package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +2 -2
  70. package/model-function/synthesize-speech/synthesizeSpeech.cjs +2 -30
  71. package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -2
  72. package/model-function/synthesize-speech/synthesizeSpeech.js +2 -30
  73. package/model-function/transcribe-speech/TranscriptionEvent.d.ts +12 -16
  74. package/model-function/transcribe-speech/TranscriptionModel.d.ts +2 -2
  75. package/model-function/transcribe-speech/transcribe.cjs +2 -30
  76. package/model-function/transcribe-speech/transcribe.d.ts +2 -2
  77. package/model-function/transcribe-speech/transcribe.js +2 -30
  78. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  79. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +6 -6
  80. package/model-provider/cohere/CohereTextGenerationModel.d.ts +4 -4
  81. package/model-provider/cohere/CohereTokenizer.d.ts +1 -1
  82. package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +2 -2
  83. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -3
  84. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +3 -3
  85. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +3 -3
  86. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +4 -4
  87. package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +1 -1
  88. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +3 -3
  89. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
  90. package/model-provider/openai/OpenAITextGenerationModel.d.ts +4 -4
  91. package/model-provider/openai/OpenAITranscriptionModel.d.ts +3 -3
  92. package/model-provider/openai/chat/OpenAIChatModel.d.ts +5 -5
  93. package/model-provider/stability/StabilityImageGenerationModel.d.ts +3 -3
  94. package/package.json +1 -1
  95. package/prompt/PromptFormatTextGenerationModel.d.ts +3 -3
  96. package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.cjs +1 -0
  97. package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.d.ts +2 -2
  98. package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.js +1 -0
  99. package/text-chunk/retrieve-text-chunks/TextChunkRetriever.d.ts +2 -2
  100. package/text-chunk/retrieve-text-chunks/retrieveTextChunks.d.ts +2 -2
  101. package/text-chunk/split/SplitFunction.d.ts +3 -3
  102. package/text-chunk/upsertTextChunks.d.ts +2 -2
  103. package/tool/ExecuteToolEvent.d.ts +9 -20
  104. package/tool/Tool.d.ts +3 -3
  105. package/tool/WebSearchTool.cjs +25 -0
  106. package/tool/WebSearchTool.d.ts +57 -1
  107. package/tool/WebSearchTool.js +25 -0
  108. package/tool/executeTool.cjs +39 -31
  109. package/tool/executeTool.d.ts +5 -4
  110. package/tool/executeTool.js +39 -31
  111. package/tool/useTool.cjs +2 -6
  112. package/tool/useTool.d.ts +2 -2
  113. package/tool/useTool.js +2 -6
  114. package/tool/useToolOrGenerateText.cjs +1 -3
  115. package/tool/useToolOrGenerateText.d.ts +2 -2
  116. package/tool/useToolOrGenerateText.js +1 -3
  117. package/util/DurationMeasurement.cjs +6 -0
  118. package/util/DurationMeasurement.d.ts +1 -0
  119. package/util/DurationMeasurement.js +6 -0
  120. package/util/api/postToApi.cjs +8 -0
  121. package/util/api/postToApi.js +8 -0
  122. package/vector-index/VectorIndex.d.ts +1 -1
  123. package/vector-index/memory/MemoryVectorIndex.d.ts +1 -1
  124. package/vector-index/pinecone/PineconeVectorIndex.d.ts +1 -1
  125. package/model-function/FunctionOptions.d.ts +0 -6
  126. package/run/ConsoleLogger.d.ts +0 -6
  127. package/run/ConsoleLogger.js +0 -8
  128. package/run/IdMetadata.d.ts +0 -7
  129. package/run/RunFunction.d.ts +0 -9
  130. package/run/RunFunctionEvent.d.ts +0 -12
  131. package/run/RunFunctionEventSource.d.ts +0 -13
  132. package/run/RunFunctionObserver.cjs +0 -2
  133. package/run/RunFunctionObserver.d.ts +0 -5
  134. package/run/RunFunctionObserver.js +0 -1
  135. package/run/index.d.ts +0 -9
  136. package/run/index.js +0 -9
  137. /package/{model-function/FunctionOptions.cjs → core/FunctionEvent.cjs} +0 -0
  138. /package/{model-function/FunctionOptions.js → core/FunctionEvent.js} +0 -0
  139. /package/{run/IdMetadata.cjs → core/FunctionObserver.cjs} +0 -0
  140. /package/{run/IdMetadata.js → core/FunctionObserver.js} +0 -0
  141. /package/{run/Run.cjs → core/FunctionOptions.cjs} +0 -0
  142. /package/{run/Run.js → core/FunctionOptions.js} +0 -0
  143. /package/{run/RunFunction.cjs → core/Run.cjs} +0 -0
  144. /package/{run/RunFunction.js → core/Run.js} +0 -0
  145. /package/{run → core}/Vector.cjs +0 -0
  146. /package/{run → core}/Vector.d.ts +0 -0
  147. /package/{run → core}/Vector.js +0 -0
  148. /package/{run/RunFunctionEvent.cjs → model-function/ModelFunctionOptions.cjs} +0 -0
  149. /package/{run/RunFunctionEvent.js → model-function/ModelFunctionOptions.js} +0 -0
package/README.md CHANGED
@@ -372,18 +372,16 @@ const { chunks } = await retrieveTextChunks(
372
372
  - [Transcribe Speech](https://modelfusion.dev/guide/function/transcribe-speech)
373
373
  - [Synthesize Speech](https://modelfusion.dev/guide/function/synthesize-speech)
374
374
  - [Generate images](https://modelfusion.dev/guide/function/generate-image)
375
- - Summarize text
376
375
  - [Tools](https://modelfusion.dev/guide/tools)
377
376
  - [Text Chunks](https://modelfusion.dev/guide/text-chunk/)
378
377
  - [Split Text](https://modelfusion.dev/guide/text-chunk/split)
379
- - [Run abstraction](https://modelfusion.dev/guide/run/)
380
- - [Abort signals](https://modelfusion.dev/guide/run/abort)
381
- - [Cost calculation](https://modelfusion.dev/guide/run/cost-calculation)
382
- - Call recording
383
- - Utilities
378
+ - [Utilities](https://modelfusion.dev/guide/util/)
379
+ - [Function observers](https://modelfusion.dev/guide/util/observer)
384
380
  - [Retry strategies](https://modelfusion.dev/guide/util/retry)
385
381
  - [Throttling strategies](https://modelfusion.dev/guide/util/throttle)
386
- - Error handling
382
+ - [Run abstraction](https://modelfusion.dev/guide/util/run)
383
+ - [Abort signals](https://modelfusion.dev/guide/util/abort)
384
+ - [Cost calculation](https://modelfusion.dev/guide/util/cost-calculation)
387
385
 
388
386
  ## Integrations
389
387
 
@@ -1,4 +1,4 @@
1
- import { RunFunction } from "../../run/RunFunction.js";
2
- export type SummarizationFunction = RunFunction<{
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ export type SummarizationFunction = (input: {
3
3
  text: string;
4
- }, string>;
4
+ }, options?: FunctionOptions) => PromiseLike<string>;
@@ -1,4 +1,4 @@
1
- import { Run } from "../../run/Run.js";
1
+ import { Run } from "../../core/Run.js";
2
2
  import { SplitFunction } from "../../text-chunk/split/SplitFunction.js";
3
3
  import { SummarizationFunction } from "./SummarizationFunction.js";
4
4
  export declare function summarizeRecursively({ summarize, split, join, text, }: {
@@ -1,6 +1,6 @@
1
1
  import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
2
2
  import { FullTokenizer } from "../../model-function/tokenize-text/Tokenizer.js";
3
- import { Run } from "../../run/Run.js";
3
+ import { Run } from "../../core/Run.js";
4
4
  /**
5
5
  * Recursively summarizes a text using a text generation model, e.g. for summarization or text extraction.
6
6
  * It automatically splits the text into optimal chunks that are small enough to be processed by the model,
@@ -2,10 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConsoleLogger = void 0;
4
4
  class ConsoleLogger {
5
- onRunFunctionStarted(event) {
6
- console.log(JSON.stringify(event, null, 2));
7
- }
8
- onRunFunctionFinished(event) {
5
+ onFunctionEvent(event) {
9
6
  console.log(JSON.stringify(event, null, 2));
10
7
  }
11
8
  }
@@ -0,0 +1,5 @@
1
+ import { FunctionEvent } from "./FunctionEvent.js";
2
+ import { FunctionObserver } from "./FunctionObserver.js";
3
+ export declare class ConsoleLogger implements FunctionObserver {
4
+ onFunctionEvent(event: FunctionEvent): void;
5
+ }
@@ -0,0 +1,5 @@
1
+ export class ConsoleLogger {
2
+ onFunctionEvent(event) {
3
+ console.log(JSON.stringify(event, null, 2));
4
+ }
5
+ }
@@ -5,7 +5,7 @@ const nanoid_1 = require("nanoid");
5
5
  const calculateCost_js_1 = require("../cost/calculateCost.cjs");
6
6
  const SuccessfulModelCall_js_1 = require("../model-function/SuccessfulModelCall.cjs");
7
7
  class DefaultRun {
8
- constructor({ runId = (0, nanoid_1.nanoid)(), sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
8
+ constructor({ runId = `run-${(0, nanoid_1.nanoid)()}`, sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
9
9
  Object.defineProperty(this, "runId", {
10
10
  enumerable: true,
11
11
  configurable: true,
@@ -55,10 +55,7 @@ class DefaultRun {
55
55
  this.costCalculators = costCalculators;
56
56
  this.observers = [
57
57
  {
58
- onRunFunctionStarted: (event) => {
59
- this.events.push(event);
60
- },
61
- onRunFunctionFinished: (event) => {
58
+ onFunctionEvent: (event) => {
62
59
  this.events.push(event);
63
60
  },
64
61
  },
@@ -1,22 +1,22 @@
1
1
  import { CostCalculator } from "../cost/CostCalculator.js";
2
2
  import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
3
3
  import { Run } from "./Run.js";
4
- import { RunFunctionEvent } from "./RunFunctionEvent.js";
5
- import { RunFunctionObserver } from "./RunFunctionObserver.js";
4
+ import { FunctionEvent } from "./FunctionEvent.js";
5
+ import { FunctionObserver } from "./FunctionObserver.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
11
  readonly costCalculators: CostCalculator[];
12
- readonly events: RunFunctionEvent[];
13
- readonly observers?: RunFunctionObserver[];
12
+ readonly events: FunctionEvent[];
13
+ readonly observers?: FunctionObserver[];
14
14
  constructor({ runId, sessionId, userId, abortSignal, observers, costCalculators, }?: {
15
15
  runId?: string;
16
16
  sessionId?: string;
17
17
  userId?: string;
18
18
  abortSignal?: AbortSignal;
19
- observers?: RunFunctionObserver[];
19
+ observers?: FunctionObserver[];
20
20
  costCalculators?: CostCalculator[];
21
21
  });
22
22
  get successfulModelCalls(): Array<SuccessfulModelCall>;
@@ -2,7 +2,7 @@ import { nanoid as createId } from "nanoid";
2
2
  import { calculateCost } from "../cost/calculateCost.js";
3
3
  import { extractSuccessfulModelCalls, } from "../model-function/SuccessfulModelCall.js";
4
4
  export class DefaultRun {
5
- constructor({ runId = createId(), sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
5
+ constructor({ runId = `run-${createId()}`, sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
6
6
  Object.defineProperty(this, "runId", {
7
7
  enumerable: true,
8
8
  configurable: true,
@@ -52,10 +52,7 @@ export class DefaultRun {
52
52
  this.costCalculators = costCalculators;
53
53
  this.observers = [
54
54
  {
55
- onRunFunctionStarted: (event) => {
56
- this.events.push(event);
57
- },
58
- onRunFunctionFinished: (event) => {
55
+ onFunctionEvent: (event) => {
59
56
  this.events.push(event);
60
57
  },
61
58
  },
@@ -0,0 +1,75 @@
1
+ import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/ExecuteToolEvent.js";
2
+ import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
3
+ export interface BaseFunctionEvent {
4
+ /**
5
+ * Unique identifier for the function call.
6
+ */
7
+ callId: string | undefined;
8
+ /**
9
+ * Optional unique identifier for the function.
10
+ */
11
+ functionId?: string | undefined;
12
+ /**
13
+ * Unique identifier for the run.
14
+ * Only available if the function is part of a run.
15
+ */
16
+ runId?: string | undefined;
17
+ /**
18
+ * Unique identifier for the session.
19
+ * Only available if the function is part of a run with a session.
20
+ */
21
+ sessionId?: string | undefined;
22
+ /**
23
+ * Unique identifier for the user.
24
+ * Only available if the function is part of a run with a user.
25
+ */
26
+ userId?: string | undefined;
27
+ /**
28
+ * Timestamp of the event.
29
+ */
30
+ timestamp: Date;
31
+ /**
32
+ * Type of the event. Defined in the subclasses.
33
+ */
34
+ eventType: "started" | "finished";
35
+ /**
36
+ * Type of the function. Defined in the subclasses.
37
+ */
38
+ functionType: string;
39
+ }
40
+ export interface BaseFunctionStartedEvent extends BaseFunctionEvent {
41
+ eventType: "started";
42
+ /**
43
+ * Timestamp when the function call started.
44
+ */
45
+ startTimestamp: Date;
46
+ }
47
+ export type BaseFunctionFinishedEventResult = {
48
+ status: "success";
49
+ output: unknown;
50
+ } | {
51
+ status: "error";
52
+ error: unknown;
53
+ } | {
54
+ status: "abort";
55
+ };
56
+ export interface BaseFunctionFinishedEvent extends BaseFunctionEvent {
57
+ eventType: "finished";
58
+ /**
59
+ * Timestamp when the function call started.
60
+ */
61
+ startTimestamp: Date;
62
+ /**
63
+ * Timestamp when the function call finished.
64
+ */
65
+ finishTimestamp: Date;
66
+ /**
67
+ * Duration of the function call in milliseconds.
68
+ */
69
+ durationInMs: number;
70
+ /**
71
+ * Result of the function call.
72
+ */
73
+ result: BaseFunctionFinishedEventResult;
74
+ }
75
+ export type FunctionEvent = ModelCallStartedEvent | ExecuteToolStartedEvent | ModelCallFinishedEvent | ExecuteToolFinishedEvent;
@@ -1,4 +1,7 @@
1
- export class RunFunctionEventSource {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FunctionEventSource = void 0;
4
+ class FunctionEventSource {
2
5
  constructor({ observers, errorHandler, }) {
3
6
  Object.defineProperty(this, "observers", {
4
7
  enumerable: true,
@@ -15,20 +18,10 @@ export class RunFunctionEventSource {
15
18
  this.observers = observers;
16
19
  this.errorHandler = errorHandler ?? ((error) => console.error(error));
17
20
  }
18
- notifyRunFunctionStarted(event) {
21
+ notify(event) {
19
22
  for (const observer of this.observers) {
20
23
  try {
21
- observer.onRunFunctionStarted?.(event);
22
- }
23
- catch (error) {
24
- this.errorHandler(error);
25
- }
26
- }
27
- }
28
- notifyRunFunctionFinished(event) {
29
- for (const observer of this.observers) {
30
- try {
31
- observer.onRunFunctionFinished?.(event);
24
+ observer.onFunctionEvent(event);
32
25
  }
33
26
  catch (error) {
34
27
  this.errorHandler(error);
@@ -36,3 +29,4 @@ export class RunFunctionEventSource {
36
29
  }
37
30
  }
38
31
  }
32
+ exports.FunctionEventSource = FunctionEventSource;
@@ -0,0 +1,12 @@
1
+ import { ErrorHandler } from "../util/ErrorHandler.js";
2
+ import { FunctionEvent } from "./FunctionEvent.js";
3
+ import { FunctionObserver } from "./FunctionObserver.js";
4
+ export declare class FunctionEventSource {
5
+ readonly observers: FunctionObserver[];
6
+ readonly errorHandler: ErrorHandler;
7
+ constructor({ observers, errorHandler, }: {
8
+ observers: FunctionObserver[];
9
+ errorHandler?: ErrorHandler;
10
+ });
11
+ notify(event: FunctionEvent): void;
12
+ }
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RunFunctionEventSource = void 0;
4
- class RunFunctionEventSource {
1
+ export class FunctionEventSource {
5
2
  constructor({ observers, errorHandler, }) {
6
3
  Object.defineProperty(this, "observers", {
7
4
  enumerable: true,
@@ -18,20 +15,10 @@ class RunFunctionEventSource {
18
15
  this.observers = observers;
19
16
  this.errorHandler = errorHandler ?? ((error) => console.error(error));
20
17
  }
21
- notifyRunFunctionStarted(event) {
18
+ notify(event) {
22
19
  for (const observer of this.observers) {
23
20
  try {
24
- observer.onRunFunctionStarted?.(event);
25
- }
26
- catch (error) {
27
- this.errorHandler(error);
28
- }
29
- }
30
- }
31
- notifyRunFunctionFinished(event) {
32
- for (const observer of this.observers) {
33
- try {
34
- observer.onRunFunctionFinished?.(event);
21
+ observer.onFunctionEvent(event);
35
22
  }
36
23
  catch (error) {
37
24
  this.errorHandler(error);
@@ -39,4 +26,3 @@ class RunFunctionEventSource {
39
26
  }
40
27
  }
41
28
  }
42
- exports.RunFunctionEventSource = RunFunctionEventSource;
@@ -0,0 +1,7 @@
1
+ import { FunctionEvent } from "./FunctionEvent.js";
2
+ export interface FunctionObserver {
3
+ /**
4
+ * Called when a function event occurs.
5
+ */
6
+ onFunctionEvent(event: FunctionEvent): void;
7
+ }
@@ -0,0 +1,19 @@
1
+ import { Run } from "./Run.js";
2
+ import { FunctionObserver } from "./FunctionObserver.js";
3
+ /**
4
+ * Additional settings for ModelFusion functions.
5
+ */
6
+ export type FunctionOptions = {
7
+ /**
8
+ * Optional function identifier that is used in events to identify the function.
9
+ */
10
+ functionId?: string;
11
+ /**
12
+ * Optional observers that are called when the function is invoked.
13
+ */
14
+ observers?: Array<FunctionObserver>;
15
+ /**
16
+ * Optional run as part of which this function is called.
17
+ */
18
+ run?: Run;
19
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getGlobalFunctionObservers = exports.setGlobalFunctionObservers = void 0;
4
+ let globalFunctionObservers = [];
5
+ function setGlobalFunctionObservers(functionObservers) {
6
+ globalFunctionObservers = functionObservers;
7
+ }
8
+ exports.setGlobalFunctionObservers = setGlobalFunctionObservers;
9
+ function getGlobalFunctionObservers() {
10
+ return globalFunctionObservers;
11
+ }
12
+ exports.getGlobalFunctionObservers = getGlobalFunctionObservers;
@@ -0,0 +1,3 @@
1
+ import { FunctionObserver } from "./FunctionObserver.js";
2
+ export declare function setGlobalFunctionObservers(functionObservers: FunctionObserver[]): void;
3
+ export declare function getGlobalFunctionObservers(): FunctionObserver[];
@@ -0,0 +1,7 @@
1
+ let globalFunctionObservers = [];
2
+ export function setGlobalFunctionObservers(functionObservers) {
3
+ globalFunctionObservers = functionObservers;
4
+ }
5
+ export function getGlobalFunctionObservers() {
6
+ return globalFunctionObservers;
7
+ }
@@ -1,5 +1,5 @@
1
1
  import { ErrorHandler } from "../util/ErrorHandler.js";
2
- import { RunFunctionObserver } from "./RunFunctionObserver.js";
2
+ import { FunctionObserver } from "./FunctionObserver.js";
3
3
  export interface Run {
4
4
  /**
5
5
  * Unique identifier for a specific run. Primarily utilized for efficient referencing
@@ -22,6 +22,6 @@ export interface Run {
22
22
  * to the run.
23
23
  */
24
24
  abortSignal?: AbortSignal;
25
- observers?: RunFunctionObserver[];
25
+ observers?: FunctionObserver[];
26
26
  errorHandler?: ErrorHandler;
27
27
  }
@@ -16,10 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./ConsoleLogger.cjs"), exports);
18
18
  __exportStar(require("./DefaultRun.cjs"), exports);
19
- __exportStar(require("./IdMetadata.cjs"), exports);
19
+ __exportStar(require("./FunctionEvent.cjs"), exports);
20
+ __exportStar(require("./FunctionEventSource.cjs"), exports);
21
+ __exportStar(require("./FunctionObserver.cjs"), exports);
22
+ __exportStar(require("./FunctionOptions.cjs"), exports);
23
+ __exportStar(require("./GlobalFunctionObservers.cjs"), exports);
20
24
  __exportStar(require("./Run.cjs"), exports);
21
- __exportStar(require("./RunFunction.cjs"), exports);
22
- __exportStar(require("./RunFunctionEvent.cjs"), exports);
23
- __exportStar(require("./RunFunctionObserver.cjs"), exports);
24
- __exportStar(require("./RunFunctionEventSource.cjs"), exports);
25
25
  __exportStar(require("./Vector.cjs"), exports);
@@ -0,0 +1,9 @@
1
+ export * from "./ConsoleLogger.js";
2
+ export * from "./DefaultRun.js";
3
+ export * from "./FunctionEvent.js";
4
+ export * from "./FunctionEventSource.js";
5
+ export * from "./FunctionObserver.js";
6
+ export * from "./FunctionOptions.js";
7
+ export * from "./GlobalFunctionObservers.js";
8
+ export * from "./Run.js";
9
+ export * from "./Vector.js";
package/core/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export * from "./ConsoleLogger.js";
2
+ export * from "./DefaultRun.js";
3
+ export * from "./FunctionEvent.js";
4
+ export * from "./FunctionEventSource.js";
5
+ export * from "./FunctionObserver.js";
6
+ export * from "./FunctionOptions.js";
7
+ export * from "./GlobalFunctionObservers.js";
8
+ export * from "./Run.js";
9
+ export * from "./Vector.js";
package/index.cjs CHANGED
@@ -15,11 +15,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./composed-function/index.cjs"), exports);
18
+ __exportStar(require("./core/index.cjs"), exports);
18
19
  __exportStar(require("./cost/index.cjs"), exports);
19
20
  __exportStar(require("./model-function/index.cjs"), exports);
20
21
  __exportStar(require("./model-provider/index.cjs"), exports);
21
22
  __exportStar(require("./prompt/index.cjs"), exports);
22
- __exportStar(require("./run/index.cjs"), exports);
23
23
  __exportStar(require("./text-chunk/index.cjs"), exports);
24
24
  __exportStar(require("./tool/index.cjs"), exports);
25
25
  __exportStar(require("./util/index.cjs"), exports);
package/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  export * from "./composed-function/index.js";
2
+ export * from "./core/index.js";
2
3
  export * from "./cost/index.js";
3
4
  export * from "./model-function/index.js";
4
5
  export * from "./model-provider/index.js";
5
6
  export * from "./prompt/index.js";
6
- export * from "./run/index.js";
7
7
  export * from "./text-chunk/index.js";
8
8
  export * from "./tool/index.js";
9
9
  export * from "./util/index.js";
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  export * from "./composed-function/index.js";
2
+ export * from "./core/index.js";
2
3
  export * from "./cost/index.js";
3
4
  export * from "./model-function/index.js";
4
5
  export * from "./model-provider/index.js";
5
6
  export * from "./prompt/index.js";
6
- export * from "./run/index.js";
7
7
  export * from "./text-chunk/index.js";
8
8
  export * from "./tool/index.js";
9
9
  export * from "./util/index.js";
@@ -1,7 +1,10 @@
1
1
  import { ModelInformation } from "./ModelInformation.js";
2
- import { RunFunctionObserver } from "../run/RunFunctionObserver.js";
2
+ import { FunctionObserver } from "../core/FunctionObserver.js";
3
3
  export interface ModelSettings {
4
- observers?: Array<RunFunctionObserver>;
4
+ /**
5
+ * Observers that are called when the model is used in run functions.
6
+ */
7
+ observers?: Array<FunctionObserver>;
5
8
  }
6
9
  export interface Model<SETTINGS> {
7
10
  modelInformation: ModelInformation;
@@ -1,4 +1,4 @@
1
- import { RunFunctionFinishedEventMetadata, RunFunctionStartedEventMetadata } from "../run/RunFunctionEvent.js";
1
+ import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
2
2
  import { ModelInformation } from "./ModelInformation.js";
3
3
  import { TextEmbeddingFinishedEvent, TextEmbeddingStartedEvent } from "./embed-text/TextEmbeddingEvent.js";
4
4
  import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
@@ -7,11 +7,26 @@ import { TextGenerationFinishedEvent, TextGenerationStartedEvent } from "./gener
7
7
  import { TextStreamingFinishedEvent, TextStreamingStartedEvent } from "./generate-text/TextStreamingEvent.js";
8
8
  import { SpeechSynthesisFinishedEvent, SpeechSynthesisStartedEvent } from "./synthesize-speech/SpeechSynthesisEvent.js";
9
9
  import { TranscriptionFinishedEvent, TranscriptionStartedEvent } from "./transcribe-speech/TranscriptionEvent.js";
10
- export type ModelCallStartedEventMetadata = RunFunctionStartedEventMetadata & {
10
+ export interface BaseModelCallStartedEvent extends BaseFunctionStartedEvent {
11
11
  model: ModelInformation;
12
+ settings: unknown;
13
+ input: unknown;
14
+ }
15
+ export type BaseModelCallFinishedEventResult = {
16
+ status: "success";
17
+ response: unknown;
18
+ output: unknown;
19
+ } | {
20
+ status: "error";
21
+ error: unknown;
22
+ } | {
23
+ status: "abort";
12
24
  };
13
- export type ModelCallStartedEvent = ImageGenerationStartedEvent | JsonGenerationStartedEvent | SpeechSynthesisStartedEvent | TextEmbeddingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
14
- export type ModelCallFinishedEventMetadata = RunFunctionFinishedEventMetadata & {
25
+ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
15
26
  model: ModelInformation;
16
- };
27
+ settings: unknown;
28
+ input: unknown;
29
+ result: BaseModelCallFinishedEventResult;
30
+ }
31
+ export type ModelCallStartedEvent = ImageGenerationStartedEvent | JsonGenerationStartedEvent | SpeechSynthesisStartedEvent | TextEmbeddingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
17
32
  export type ModelCallFinishedEvent = ImageGenerationFinishedEvent | JsonGenerationFinishedEvent | SpeechSynthesisFinishedEvent | TextEmbeddingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | TranscriptionFinishedEvent;
@@ -0,0 +1,4 @@
1
+ import { FunctionOptions } from "../core/FunctionOptions.js";
2
+ export interface ModelFunctionOptions<SETTINGS> extends FunctionOptions {
3
+ settings?: Partial<SETTINGS>;
4
+ }
@@ -3,24 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.extractSuccessfulModelCalls = void 0;
4
4
  function extractSuccessfulModelCalls(runFunctionEvents) {
5
5
  return runFunctionEvents
6
- .filter((event) => Object.keys(eventTypeToCostType).includes(event.type) &&
7
- "status" in event &&
8
- event.status === "success")
6
+ .filter((event) => "result" in event &&
7
+ "status" in event.result &&
8
+ event.result.status === "success")
9
9
  .map((event) => ({
10
- model: event.metadata.model,
10
+ model: event.model,
11
11
  settings: event.settings,
12
- response: event.response,
13
- type: eventTypeToCostType[event.type],
12
+ response: event.result.response,
13
+ type: event.functionType,
14
14
  }));
15
15
  }
16
16
  exports.extractSuccessfulModelCalls = extractSuccessfulModelCalls;
17
- const eventTypeToCostType = {
18
- "image-generation-finished": "image-generation",
19
- "json-generation-finished": "json-generation",
20
- "json-or-text-generation-finished": "json-or-text-generation",
21
- "speech-synthesis-finished": "speech-synthesis",
22
- "text-embedding-finished": "text-embedding",
23
- "text-generation-finished": "text-generation",
24
- "text-streaming-finished": "text-streaming",
25
- "transcription-finished": "transcription",
26
- };
@@ -1,4 +1,4 @@
1
- import { RunFunctionEvent } from "../run/RunFunctionEvent.js";
1
+ import { FunctionEvent } from "../core/FunctionEvent.js";
2
2
  import { ModelInformation } from "./ModelInformation.js";
3
3
  export type SuccessfulModelCall = {
4
4
  type: "image-generation" | "json-generation" | "json-or-text-generation" | "speech-synthesis" | "text-embedding" | "text-generation" | "text-streaming" | "transcription";
@@ -6,4 +6,4 @@ export type SuccessfulModelCall = {
6
6
  settings: unknown;
7
7
  response: unknown;
8
8
  };
9
- export declare function extractSuccessfulModelCalls(runFunctionEvents: RunFunctionEvent[]): SuccessfulModelCall[];
9
+ export declare function extractSuccessfulModelCalls(runFunctionEvents: FunctionEvent[]): SuccessfulModelCall[];
@@ -1,22 +1,12 @@
1
1
  export function extractSuccessfulModelCalls(runFunctionEvents) {
2
2
  return runFunctionEvents
3
- .filter((event) => Object.keys(eventTypeToCostType).includes(event.type) &&
4
- "status" in event &&
5
- event.status === "success")
3
+ .filter((event) => "result" in event &&
4
+ "status" in event.result &&
5
+ event.result.status === "success")
6
6
  .map((event) => ({
7
- model: event.metadata.model,
7
+ model: event.model,
8
8
  settings: event.settings,
9
- response: event.response,
10
- type: eventTypeToCostType[event.type],
9
+ response: event.result.response,
10
+ type: event.functionType,
11
11
  }));
12
12
  }
13
- const eventTypeToCostType = {
14
- "image-generation-finished": "image-generation",
15
- "json-generation-finished": "json-generation",
16
- "json-or-text-generation-finished": "json-or-text-generation",
17
- "speech-synthesis-finished": "speech-synthesis",
18
- "text-embedding-finished": "text-embedding",
19
- "text-generation-finished": "text-generation",
20
- "text-streaming-finished": "text-streaming",
21
- "transcription-finished": "transcription",
22
- };
@@ -1,23 +1,21 @@
1
- import { Vector } from "../../run/Vector.js";
2
- import { ModelCallFinishedEventMetadata, ModelCallStartedEventMetadata } from "../ModelCallEvent.js";
3
- export type TextEmbeddingStartedEvent = {
4
- type: "text-embedding-started";
5
- metadata: ModelCallStartedEventMetadata;
6
- settings: unknown;
7
- texts: Array<string>;
8
- };
9
- export type TextEmbeddingFinishedEvent = {
10
- type: "text-embedding-finished";
11
- metadata: ModelCallFinishedEventMetadata;
12
- settings: unknown;
13
- texts: Array<string>;
14
- } & ({
1
+ import { Vector } from "../../core/Vector.js";
2
+ import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
3
+ export interface TextEmbeddingStartedEvent extends BaseModelCallStartedEvent {
4
+ functionType: "text-embedding";
5
+ input: string | Array<string>;
6
+ }
7
+ export type TextEmbeddingFinishedEventResult = {
15
8
  status: "success";
16
9
  response: unknown;
17
- generatedEmbeddings: Array<Vector>;
10
+ output: Vector | Array<Vector>;
18
11
  } | {
19
- status: "failure";
12
+ status: "error";
20
13
  error: unknown;
21
14
  } | {
22
15
  status: "abort";
23
- });
16
+ };
17
+ export interface TextEmbeddingFinishedEvent extends BaseModelCallFinishedEvent {
18
+ functionType: "text-embedding";
19
+ input: string | Array<string>;
20
+ result: TextEmbeddingFinishedEventResult;
21
+ }