modelfusion 0.20.0 → 0.21.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 (131) hide show
  1. package/README.md +6 -8
  2. package/composed-function/summarize/SummarizationFunction.d.ts +3 -3
  3. package/model-function/Model.d.ts +5 -2
  4. package/model-function/ModelCallEvent.d.ts +3 -3
  5. package/model-function/ModelFunctionOptions.d.ts +4 -0
  6. package/model-function/SuccessfulModelCall.cjs +3 -15
  7. package/model-function/SuccessfulModelCall.d.ts +2 -2
  8. package/model-function/SuccessfulModelCall.js +3 -15
  9. package/model-function/embed-text/TextEmbeddingEvent.d.ts +6 -8
  10. package/model-function/embed-text/TextEmbeddingModel.d.ts +2 -2
  11. package/model-function/embed-text/embedText.cjs +16 -22
  12. package/model-function/embed-text/embedText.d.ts +3 -3
  13. package/model-function/embed-text/embedText.js +16 -22
  14. package/model-function/executeCall.cjs +30 -8
  15. package/model-function/executeCall.d.ts +16 -9
  16. package/model-function/executeCall.js +30 -8
  17. package/model-function/generate-image/ImageGenerationEvent.d.ts +6 -8
  18. package/model-function/generate-image/ImageGenerationModel.d.ts +2 -2
  19. package/model-function/generate-image/generateImage.cjs +8 -11
  20. package/model-function/generate-image/generateImage.d.ts +2 -2
  21. package/model-function/generate-image/generateImage.js +8 -11
  22. package/model-function/generate-json/GenerateJsonModel.d.ts +2 -2
  23. package/model-function/generate-json/GenerateJsonOrTextModel.d.ts +2 -2
  24. package/model-function/generate-json/JsonGenerationEvent.d.ts +6 -8
  25. package/model-function/generate-json/JsonTextGenerationModel.d.ts +2 -2
  26. package/model-function/generate-json/generateJson.cjs +8 -11
  27. package/model-function/generate-json/generateJson.d.ts +2 -2
  28. package/model-function/generate-json/generateJson.js +8 -11
  29. package/model-function/generate-json/generateJsonOrText.cjs +8 -11
  30. package/model-function/generate-json/generateJsonOrText.d.ts +2 -2
  31. package/model-function/generate-json/generateJsonOrText.js +8 -11
  32. package/model-function/generate-text/TextGenerationEvent.d.ts +6 -8
  33. package/model-function/generate-text/TextGenerationModel.d.ts +3 -3
  34. package/model-function/generate-text/TextStreamingEvent.d.ts +6 -8
  35. package/model-function/generate-text/generateText.cjs +8 -11
  36. package/model-function/generate-text/generateText.d.ts +2 -2
  37. package/model-function/generate-text/generateText.js +8 -11
  38. package/model-function/generate-text/streamText.cjs +38 -23
  39. package/model-function/generate-text/streamText.d.ts +5 -5
  40. package/model-function/generate-text/streamText.js +38 -23
  41. package/model-function/index.cjs +1 -1
  42. package/model-function/index.d.ts +1 -1
  43. package/model-function/index.js +1 -1
  44. package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +6 -8
  45. package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +2 -2
  46. package/model-function/synthesize-speech/synthesizeSpeech.cjs +8 -10
  47. package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -2
  48. package/model-function/synthesize-speech/synthesizeSpeech.js +8 -10
  49. package/model-function/transcribe-speech/TranscriptionEvent.d.ts +6 -8
  50. package/model-function/transcribe-speech/TranscriptionModel.d.ts +2 -2
  51. package/model-function/transcribe-speech/transcribe.cjs +8 -11
  52. package/model-function/transcribe-speech/transcribe.d.ts +2 -2
  53. package/model-function/transcribe-speech/transcribe.js +8 -11
  54. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  55. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  56. package/model-provider/cohere/CohereTextGenerationModel.d.ts +4 -4
  57. package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +2 -2
  58. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -3
  59. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +3 -3
  60. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +3 -3
  61. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +4 -4
  62. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +3 -3
  63. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
  64. package/model-provider/openai/OpenAITextGenerationModel.d.ts +4 -4
  65. package/model-provider/openai/OpenAITranscriptionModel.d.ts +3 -3
  66. package/model-provider/openai/chat/OpenAIChatModel.d.ts +5 -5
  67. package/model-provider/stability/StabilityImageGenerationModel.d.ts +3 -3
  68. package/package.json +1 -1
  69. package/prompt/PromptFormatTextGenerationModel.d.ts +3 -3
  70. package/run/ConsoleLogger.cjs +1 -4
  71. package/run/ConsoleLogger.d.ts +4 -5
  72. package/run/ConsoleLogger.js +1 -4
  73. package/run/DefaultRun.cjs +1 -4
  74. package/run/DefaultRun.d.ts +5 -5
  75. package/run/DefaultRun.js +1 -4
  76. package/run/FunctionEvent.d.ts +51 -0
  77. package/run/{RunFunctionEventSource.js → FunctionEventSource.cjs} +7 -13
  78. package/run/FunctionEventSource.d.ts +12 -0
  79. package/run/{RunFunctionEventSource.cjs → FunctionEventSource.js} +3 -17
  80. package/run/FunctionObserver.d.ts +7 -0
  81. package/run/FunctionOptions.d.ts +19 -0
  82. package/run/GlobalFunctionObservers.cjs +12 -0
  83. package/run/GlobalFunctionObservers.d.ts +3 -0
  84. package/run/GlobalFunctionObservers.js +7 -0
  85. package/run/Run.d.ts +2 -2
  86. package/run/index.cjs +5 -5
  87. package/run/index.d.ts +5 -5
  88. package/run/index.js +5 -5
  89. package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.cjs +1 -0
  90. package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.d.ts +2 -2
  91. package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.js +1 -0
  92. package/text-chunk/retrieve-text-chunks/TextChunkRetriever.d.ts +2 -2
  93. package/text-chunk/retrieve-text-chunks/retrieveTextChunks.d.ts +2 -2
  94. package/text-chunk/split/SplitFunction.d.ts +3 -3
  95. package/text-chunk/upsertTextChunks.d.ts +2 -2
  96. package/tool/ExecuteToolEvent.d.ts +6 -8
  97. package/tool/Tool.d.ts +3 -3
  98. package/tool/WebSearchTool.d.ts +2 -1
  99. package/tool/executeTool.cjs +28 -26
  100. package/tool/executeTool.d.ts +5 -4
  101. package/tool/executeTool.js +28 -26
  102. package/tool/index.cjs +1 -0
  103. package/tool/index.d.ts +1 -0
  104. package/tool/index.js +1 -0
  105. package/tool/useTool.cjs +2 -6
  106. package/tool/useTool.d.ts +2 -2
  107. package/tool/useTool.js +2 -6
  108. package/tool/useToolOrGenerateText.cjs +1 -3
  109. package/tool/useToolOrGenerateText.d.ts +2 -2
  110. package/tool/useToolOrGenerateText.js +1 -3
  111. package/util/DurationMeasurement.cjs +6 -0
  112. package/util/DurationMeasurement.d.ts +1 -0
  113. package/util/DurationMeasurement.js +6 -0
  114. package/util/api/postToApi.cjs +8 -0
  115. package/util/api/postToApi.js +8 -0
  116. package/model-function/FunctionOptions.d.ts +0 -6
  117. package/run/IdMetadata.d.ts +0 -7
  118. package/run/RunFunction.d.ts +0 -9
  119. package/run/RunFunctionEvent.d.ts +0 -12
  120. package/run/RunFunctionEventSource.d.ts +0 -13
  121. package/run/RunFunctionObserver.cjs +0 -2
  122. package/run/RunFunctionObserver.d.ts +0 -5
  123. package/run/RunFunctionObserver.js +0 -1
  124. /package/model-function/{FunctionOptions.cjs → ModelFunctionOptions.cjs} +0 -0
  125. /package/model-function/{FunctionOptions.js → ModelFunctionOptions.js} +0 -0
  126. /package/run/{IdMetadata.cjs → FunctionEvent.cjs} +0 -0
  127. /package/run/{IdMetadata.js → FunctionEvent.js} +0 -0
  128. /package/run/{RunFunction.cjs → FunctionObserver.cjs} +0 -0
  129. /package/run/{RunFunction.js → FunctionObserver.js} +0 -0
  130. /package/run/{RunFunctionEvent.cjs → FunctionOptions.cjs} +0 -0
  131. /package/run/{RunFunctionEvent.js → FunctionOptions.js} +0 -0
package/run/Run.d.ts CHANGED
@@ -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
  }
package/run/index.cjs CHANGED
@@ -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);
package/run/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  export * from "./ConsoleLogger.js";
2
2
  export * from "./DefaultRun.js";
3
- export * from "./IdMetadata.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";
4
8
  export * from "./Run.js";
5
- export * from "./RunFunction.js";
6
- export * from "./RunFunctionEvent.js";
7
- export * from "./RunFunctionObserver.js";
8
- export * from "./RunFunctionEventSource.js";
9
9
  export * from "./Vector.js";
package/run/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  export * from "./ConsoleLogger.js";
2
2
  export * from "./DefaultRun.js";
3
- export * from "./IdMetadata.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";
4
8
  export * from "./Run.js";
5
- export * from "./RunFunction.js";
6
- export * from "./RunFunctionEvent.js";
7
- export * from "./RunFunctionObserver.js";
8
- export * from "./RunFunctionEventSource.js";
9
9
  export * from "./Vector.js";
@@ -33,6 +33,7 @@ class SimilarTextChunksFromVectorIndexRetriever {
33
33
  if (options?.settings != null) {
34
34
  return this.withSettings(options.settings).retrieveTextChunks(query, {
35
35
  functionId: options.functionId,
36
+ observers: options.observers,
36
37
  run: options.run,
37
38
  });
38
39
  }
@@ -1,4 +1,4 @@
1
- import { FunctionOptions } from "../model-function/FunctionOptions.js";
1
+ import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
2
2
  import { TextEmbeddingModel, TextEmbeddingModelSettings } from "../model-function/embed-text/TextEmbeddingModel.js";
3
3
  import { TextChunk } from "./TextChunk.js";
4
4
  import { TextChunkRetriever, TextChunkRetrieverSettings } from "./retrieve-text-chunks/TextChunkRetriever.js";
@@ -15,6 +15,6 @@ export declare class SimilarTextChunksFromVectorIndexRetriever<CHUNK extends Tex
15
15
  vectorIndex: VectorIndex<CHUNK, INDEX>;
16
16
  embeddingModel: TextEmbeddingModel<unknown, SETTINGS>;
17
17
  } & SimilarTextChunksFromVectorIndexRetrieverSettings);
18
- retrieveTextChunks(query: string, options?: FunctionOptions<TextChunkRetrieverSettings>): Promise<CHUNK[]>;
18
+ retrieveTextChunks(query: string, options?: ModelFunctionOptions<TextChunkRetrieverSettings>): Promise<CHUNK[]>;
19
19
  withSettings(additionalSettings: Partial<SimilarTextChunksFromVectorIndexRetrieverSettings>): this;
20
20
  }
@@ -30,6 +30,7 @@ export class SimilarTextChunksFromVectorIndexRetriever {
30
30
  if (options?.settings != null) {
31
31
  return this.withSettings(options.settings).retrieveTextChunks(query, {
32
32
  functionId: options.functionId,
33
+ observers: options.observers,
33
34
  run: options.run,
34
35
  });
35
36
  }
@@ -1,8 +1,8 @@
1
- import { FunctionOptions } from "../../model-function/FunctionOptions.js";
1
+ import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
2
2
  import { TextChunk } from "../TextChunk.js";
3
3
  export interface TextChunkRetrieverSettings {
4
4
  }
5
5
  export interface TextChunkRetriever<CHUNK extends TextChunk, QUERY, SETTINGS extends TextChunkRetrieverSettings> {
6
- retrieveTextChunks(query: QUERY, options?: FunctionOptions<TextChunkRetrieverSettings>): Promise<CHUNK[]>;
6
+ retrieveTextChunks(query: QUERY, options?: ModelFunctionOptions<TextChunkRetrieverSettings>): Promise<CHUNK[]>;
7
7
  withSettings(additionalSettings: Partial<SETTINGS>): this;
8
8
  }
@@ -1,6 +1,6 @@
1
- import { FunctionOptions } from "../../model-function/FunctionOptions.js";
1
+ import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
2
2
  import { TextChunk } from "../TextChunk.js";
3
3
  import { TextChunkRetriever, TextChunkRetrieverSettings } from "./TextChunkRetriever.js";
4
- export declare function retrieveTextChunks<CHUNK extends TextChunk, QUERY, SETTINGS extends TextChunkRetrieverSettings>(retriever: TextChunkRetriever<CHUNK, QUERY, SETTINGS>, query: QUERY, options?: FunctionOptions<SETTINGS>): Promise<{
4
+ export declare function retrieveTextChunks<CHUNK extends TextChunk, QUERY, SETTINGS extends TextChunkRetrieverSettings>(retriever: TextChunkRetriever<CHUNK, QUERY, SETTINGS>, query: QUERY, options?: ModelFunctionOptions<SETTINGS>): Promise<{
5
5
  chunks: CHUNK[];
6
6
  }>;
@@ -1,4 +1,4 @@
1
- import { RunFunction } from "../../run/RunFunction.js";
2
- export type SplitFunction = RunFunction<{
1
+ import { FunctionOptions } from "../../run/FunctionOptions.js";
2
+ export type SplitFunction = (input: {
3
3
  text: string;
4
- }, Array<string>>;
4
+ }, options?: FunctionOptions) => PromiseLike<Array<string>>;
@@ -1,4 +1,4 @@
1
- import { FunctionOptions } from "../model-function/FunctionOptions.js";
1
+ import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
2
2
  import { TextEmbeddingModel, TextEmbeddingModelSettings } from "../model-function/embed-text/TextEmbeddingModel.js";
3
3
  import { TextChunk } from "./TextChunk.js";
4
4
  import { VectorIndex } from "../vector-index/VectorIndex.js";
@@ -8,4 +8,4 @@ export declare function upsertTextChunks<CHUNK extends TextChunk, SETTINGS exten
8
8
  generateId?: () => string;
9
9
  chunks: CHUNK[];
10
10
  ids?: Array<string | undefined>;
11
- }, options?: FunctionOptions<SETTINGS>): Promise<void>;
11
+ }, options?: ModelFunctionOptions<SETTINGS>): Promise<void>;
@@ -1,21 +1,19 @@
1
- import { RunFunctionFinishedEventMetadata, RunFunctionStartedEventMetadata } from "../run/RunFunctionEvent.js";
1
+ import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../run/FunctionEvent.js";
2
2
  import { Tool } from "./Tool.js";
3
- export type ExecuteToolStartedEvent = {
4
- type: "execute-tool-started";
5
- metadata: RunFunctionStartedEventMetadata;
3
+ export type ExecuteToolStartedEvent = BaseFunctionStartedEvent & {
4
+ functionType: "execute-tool";
6
5
  tool: Tool<string, unknown, unknown>;
7
6
  input: unknown;
8
7
  };
9
- export type ExecuteToolFinishedEvent = {
10
- type: "execute-tool-finished";
11
- metadata: RunFunctionFinishedEventMetadata;
8
+ export type ExecuteToolFinishedEvent = BaseFunctionFinishedEvent & {
9
+ functionType: "execute-tool";
12
10
  tool: Tool<string, unknown, unknown>;
13
11
  input: unknown;
14
12
  } & ({
15
13
  status: "success";
16
14
  output: unknown;
17
15
  } | {
18
- status: "failure";
16
+ status: "error";
19
17
  error: unknown;
20
18
  } | {
21
19
  status: "abort";
package/tool/Tool.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { SchemaDefinition } from "../model-function/generate-json/SchemaDefinition.js";
3
- import { RunFunction } from "../run/RunFunction.js";
3
+ import { FunctionOptions } from "../run/FunctionOptions.js";
4
4
  /**
5
5
  * A tool is a function with a name, description and defined inputs that can be used
6
6
  * by agents and chatbots.
@@ -28,13 +28,13 @@ export declare class Tool<NAME extends string, INPUT, OUTPUT> {
28
28
  /**
29
29
  * The actual execution function of the tool.
30
30
  */
31
- readonly execute: RunFunction<INPUT, OUTPUT>;
31
+ readonly execute: (input: INPUT, options?: FunctionOptions) => PromiseLike<OUTPUT>;
32
32
  constructor({ name, description, inputSchema, outputSchema, execute, }: {
33
33
  name: NAME;
34
34
  description: string;
35
35
  inputSchema: z.ZodSchema<INPUT>;
36
36
  outputSchema?: z.ZodSchema<OUTPUT>;
37
- execute(input: INPUT): Promise<OUTPUT>;
37
+ execute(input: INPUT, options?: FunctionOptions): PromiseLike<OUTPUT>;
38
38
  });
39
39
  /**
40
40
  * Provdes a schema definition with the name, description and schema of the input.
@@ -1,3 +1,4 @@
1
+ import { FunctionOptions } from "../run/FunctionOptions.js";
1
2
  import { Tool } from "./Tool.js";
2
3
  export type WebSearchToolInput = {
3
4
  query: string;
@@ -14,6 +15,6 @@ export declare class WebSearchTool<NAME extends string> extends Tool<NAME, WebSe
14
15
  name: NAME;
15
16
  description: string;
16
17
  queryDescription?: string;
17
- execute(input: WebSearchToolInput): Promise<WebSearchToolOutput>;
18
+ execute(input: WebSearchToolInput, options?: FunctionOptions): PromiseLike<WebSearchToolOutput>;
18
19
  });
19
20
  }
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.executeTool = exports.ExecuteToolPromise = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
- const RunFunctionEventSource_js_1 = require("../run/RunFunctionEventSource.cjs");
5
+ const FunctionEventSource_js_1 = require("../run/FunctionEventSource.cjs");
6
+ const GlobalFunctionObservers_js_1 = require("../run/GlobalFunctionObservers.cjs");
6
7
  const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
7
8
  const AbortError_js_1 = require("../util/api/AbortError.cjs");
8
9
  const runSafe_js_1 = require("../util/runSafe.cjs");
@@ -52,47 +53,51 @@ exports.executeTool = executeTool;
52
53
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
54
  async function doExecuteTool(tool, input, options) {
54
55
  const run = options?.run;
55
- const eventSource = new RunFunctionEventSource_js_1.RunFunctionEventSource({
56
- observers: run?.observers ?? [],
56
+ const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
57
+ observers: [
58
+ ...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
59
+ ...(run?.observers ?? []),
60
+ ...(options?.observers ?? []),
61
+ ],
57
62
  errorHandler: run?.errorHandler,
58
63
  });
59
64
  const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
60
- const startMetadata = {
65
+ const metadata = {
66
+ functionType: "execute-tool",
61
67
  callId: `call-${(0, nanoid_1.nanoid)()}`,
62
68
  runId: run?.runId,
63
69
  sessionId: run?.sessionId,
64
70
  userId: run?.userId,
65
71
  functionId: options?.functionId,
66
- startEpochSeconds: durationMeasurement.startEpochSeconds,
67
- };
68
- eventSource.notifyRunFunctionStarted({
69
- type: "execute-tool-started",
70
- metadata: startMetadata,
71
72
  tool: tool,
72
73
  input,
74
+ };
75
+ eventSource.notify({
76
+ ...metadata,
77
+ eventType: "started",
78
+ timestamp: durationMeasurement.startDate,
79
+ startTimestamp: durationMeasurement.startDate,
73
80
  });
74
81
  const result = await (0, runSafe_js_1.runSafe)(() => tool.execute(input, options));
75
82
  const finishMetadata = {
76
- ...startMetadata,
83
+ ...metadata,
84
+ eventType: "finished",
85
+ timestamp: new Date(),
86
+ startTimestamp: durationMeasurement.startDate,
87
+ finishTimestamp: new Date(),
77
88
  durationInMs: durationMeasurement.durationInMs,
78
89
  };
79
90
  if (!result.ok) {
80
91
  if (result.isAborted) {
81
- eventSource.notifyRunFunctionFinished({
82
- type: "execute-tool-finished",
92
+ eventSource.notify({
93
+ ...finishMetadata,
83
94
  status: "abort",
84
- metadata: finishMetadata,
85
- tool: tool,
86
- input,
87
95
  });
88
96
  throw new AbortError_js_1.AbortError();
89
97
  }
90
- eventSource.notifyRunFunctionFinished({
91
- type: "execute-tool-finished",
92
- status: "failure",
93
- metadata: finishMetadata,
94
- tool: tool,
95
- input,
98
+ eventSource.notify({
99
+ ...finishMetadata,
100
+ status: "error",
96
101
  error: result.error,
97
102
  });
98
103
  throw new ToolExecutionError_js_1.ToolExecutionError({
@@ -104,12 +109,9 @@ async function doExecuteTool(tool, input, options) {
104
109
  });
105
110
  }
106
111
  const output = result.output;
107
- eventSource.notifyRunFunctionFinished({
108
- type: "execute-tool-finished",
112
+ eventSource.notify({
113
+ ...finishMetadata,
109
114
  status: "success",
110
- metadata: finishMetadata,
111
- tool: tool,
112
- input,
113
115
  output,
114
116
  });
115
117
  return {
@@ -1,13 +1,14 @@
1
- import { FunctionOptions } from "../model-function/FunctionOptions.js";
2
- import { Tool } from "./Tool.js";
3
1
  import { z } from "zod";
2
+ import { FunctionOptions } from "../run/FunctionOptions.js";
3
+ import { Tool } from "./Tool.js";
4
4
  export type ExecuteToolMetadata = {
5
5
  callId: string;
6
6
  runId?: string;
7
7
  sessionId?: string;
8
8
  userId?: string;
9
9
  functionId?: string;
10
- startEpochSeconds: number;
10
+ startTimestamp: Date;
11
+ finishTimestamp: Date;
11
12
  durationInMs: number;
12
13
  };
13
14
  export declare class ExecuteToolPromise<OUTPUT> extends Promise<OUTPUT> {
@@ -28,4 +29,4 @@ export declare class ExecuteToolPromise<OUTPUT> extends Promise<OUTPUT> {
28
29
  /**
29
30
  * `executeTool` directly executes a tool with the given parameters.
30
31
  */
31
- export declare function executeTool<TOOL extends Tool<any, any, any>>(tool: TOOL, input: z.infer<TOOL["inputSchema"]>, options?: FunctionOptions<undefined>): ExecuteToolPromise<ReturnType<TOOL["execute"]>>;
32
+ export declare function executeTool<TOOL extends Tool<any, any, any>>(tool: TOOL, input: z.infer<TOOL["inputSchema"]>, options?: FunctionOptions): ExecuteToolPromise<ReturnType<TOOL["execute"]>>;
@@ -1,5 +1,6 @@
1
1
  import { nanoid as createId } from "nanoid";
2
- import { RunFunctionEventSource } from "../run/RunFunctionEventSource.js";
2
+ import { FunctionEventSource } from "../run/FunctionEventSource.js";
3
+ import { getGlobalFunctionObservers } from "../run/GlobalFunctionObservers.js";
3
4
  import { startDurationMeasurement } from "../util/DurationMeasurement.js";
4
5
  import { AbortError } from "../util/api/AbortError.js";
5
6
  import { runSafe } from "../util/runSafe.js";
@@ -47,47 +48,51 @@ export function executeTool(tool, input, options) {
47
48
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
49
  async function doExecuteTool(tool, input, options) {
49
50
  const run = options?.run;
50
- const eventSource = new RunFunctionEventSource({
51
- observers: run?.observers ?? [],
51
+ const eventSource = new FunctionEventSource({
52
+ observers: [
53
+ ...getGlobalFunctionObservers(),
54
+ ...(run?.observers ?? []),
55
+ ...(options?.observers ?? []),
56
+ ],
52
57
  errorHandler: run?.errorHandler,
53
58
  });
54
59
  const durationMeasurement = startDurationMeasurement();
55
- const startMetadata = {
60
+ const metadata = {
61
+ functionType: "execute-tool",
56
62
  callId: `call-${createId()}`,
57
63
  runId: run?.runId,
58
64
  sessionId: run?.sessionId,
59
65
  userId: run?.userId,
60
66
  functionId: options?.functionId,
61
- startEpochSeconds: durationMeasurement.startEpochSeconds,
62
- };
63
- eventSource.notifyRunFunctionStarted({
64
- type: "execute-tool-started",
65
- metadata: startMetadata,
66
67
  tool: tool,
67
68
  input,
69
+ };
70
+ eventSource.notify({
71
+ ...metadata,
72
+ eventType: "started",
73
+ timestamp: durationMeasurement.startDate,
74
+ startTimestamp: durationMeasurement.startDate,
68
75
  });
69
76
  const result = await runSafe(() => tool.execute(input, options));
70
77
  const finishMetadata = {
71
- ...startMetadata,
78
+ ...metadata,
79
+ eventType: "finished",
80
+ timestamp: new Date(),
81
+ startTimestamp: durationMeasurement.startDate,
82
+ finishTimestamp: new Date(),
72
83
  durationInMs: durationMeasurement.durationInMs,
73
84
  };
74
85
  if (!result.ok) {
75
86
  if (result.isAborted) {
76
- eventSource.notifyRunFunctionFinished({
77
- type: "execute-tool-finished",
87
+ eventSource.notify({
88
+ ...finishMetadata,
78
89
  status: "abort",
79
- metadata: finishMetadata,
80
- tool: tool,
81
- input,
82
90
  });
83
91
  throw new AbortError();
84
92
  }
85
- eventSource.notifyRunFunctionFinished({
86
- type: "execute-tool-finished",
87
- status: "failure",
88
- metadata: finishMetadata,
89
- tool: tool,
90
- input,
93
+ eventSource.notify({
94
+ ...finishMetadata,
95
+ status: "error",
91
96
  error: result.error,
92
97
  });
93
98
  throw new ToolExecutionError({
@@ -99,12 +104,9 @@ async function doExecuteTool(tool, input, options) {
99
104
  });
100
105
  }
101
106
  const output = result.output;
102
- eventSource.notifyRunFunctionFinished({
103
- type: "execute-tool-finished",
107
+ eventSource.notify({
108
+ ...finishMetadata,
104
109
  status: "success",
105
- metadata: finishMetadata,
106
- tool: tool,
107
- input,
108
110
  output,
109
111
  });
110
112
  return {
package/tool/index.cjs CHANGED
@@ -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("./ExecuteToolEvent.cjs"), exports);
17
18
  __exportStar(require("./InvalidToolNameError.cjs"), exports);
18
19
  __exportStar(require("./NoSuchToolError.cjs"), exports);
19
20
  __exportStar(require("./Tool.cjs"), exports);
package/tool/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./ExecuteToolEvent.js";
1
2
  export * from "./InvalidToolNameError.js";
2
3
  export * from "./NoSuchToolError.js";
3
4
  export * from "./Tool.js";
package/tool/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./ExecuteToolEvent.js";
1
2
  export * from "./InvalidToolNameError.js";
2
3
  export * from "./NoSuchToolError.js";
3
4
  export * from "./Tool.js";
package/tool/useTool.cjs CHANGED
@@ -18,15 +18,11 @@ async function useTool(model, tool, prompt, options) {
18
18
  name: tool.name,
19
19
  description: tool.description,
20
20
  schema: tool.inputSchema,
21
- }, () => prompt(tool), {
22
- ...(options ?? {}),
23
- }).asFullResponse();
21
+ }, () => prompt(tool), options).asFullResponse();
24
22
  return {
25
23
  tool: tool.name,
26
24
  parameters: value,
27
- result: await (0, executeTool_js_1.executeTool)(tool, value, {
28
- run: options?.run,
29
- }),
25
+ result: await (0, executeTool_js_1.executeTool)(tool, value, options),
30
26
  };
31
27
  }
32
28
  exports.useTool = useTool;
package/tool/useTool.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { FunctionOptions } from "../model-function/FunctionOptions.js";
1
+ import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
2
2
  import { GenerateJsonModel, GenerateJsonModelSettings } from "../model-function/generate-json/GenerateJsonModel.js";
3
3
  import { Tool } from "./Tool.js";
4
4
  /**
@@ -8,7 +8,7 @@ import { Tool } from "./Tool.js";
8
8
  * the parameters (`parameters` property, typed),
9
9
  * and the result of the tool execution (`result` property, typed).
10
10
  */
11
- export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT, options?: FunctionOptions<SETTINGS>): Promise<{
11
+ export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT, options?: ModelFunctionOptions<SETTINGS>): Promise<{
12
12
  tool: TOOL["name"];
13
13
  parameters: TOOL["inputSchema"];
14
14
  result: Awaited<ReturnType<TOOL["execute"]>>;
package/tool/useTool.js CHANGED
@@ -15,14 +15,10 @@ export async function useTool(model, tool, prompt, options) {
15
15
  name: tool.name,
16
16
  description: tool.description,
17
17
  schema: tool.inputSchema,
18
- }, () => prompt(tool), {
19
- ...(options ?? {}),
20
- }).asFullResponse();
18
+ }, () => prompt(tool), options).asFullResponse();
21
19
  return {
22
20
  tool: tool.name,
23
21
  parameters: value,
24
- result: await executeTool(tool, value, {
25
- run: options?.run,
26
- }),
22
+ result: await executeTool(tool, value, options),
27
23
  };
28
24
  }
@@ -25,9 +25,7 @@ async function useToolOrGenerateText(model, tools, prompt, options) {
25
25
  throw new NoSuchToolError_js_1.NoSuchToolError(schema.toString());
26
26
  }
27
27
  const toolParameters = modelResponse.value;
28
- const result = await (0, executeTool_js_1.executeTool)(tool, toolParameters, {
29
- run: options?.run,
30
- });
28
+ const result = await (0, executeTool_js_1.executeTool)(tool, toolParameters, options);
31
29
  return {
32
30
  tool: schema,
33
31
  result,
@@ -1,4 +1,4 @@
1
- import { FunctionOptions } from "../model-function/FunctionOptions.js";
1
+ import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
2
2
  import { GenerateJsonOrTextModel, GenerateJsonOrTextModelSettings, GenerateJsonOrTextPrompt } from "../model-function/generate-json/GenerateJsonOrTextModel.js";
3
3
  import { Tool } from "./Tool.js";
4
4
  type ToolArray<T extends Tool<any, any, any>[]> = T;
@@ -14,7 +14,7 @@ type ToToolUnion<T> = {
14
14
  } : never;
15
15
  }[keyof T];
16
16
  type ToOutputValue<TOOLS extends ToolArray<Tool<any, any, any>[]>> = ToToolUnion<ToToolMap<TOOLS>>;
17
- export declare function useToolOrGenerateText<PROMPT, RESPONSE, SETTINGS extends GenerateJsonOrTextModelSettings, TOOLS extends Array<Tool<any, any, any>>>(model: GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS>, tools: TOOLS, prompt: (tools: TOOLS) => PROMPT & GenerateJsonOrTextPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): Promise<{
17
+ export declare function useToolOrGenerateText<PROMPT, RESPONSE, SETTINGS extends GenerateJsonOrTextModelSettings, TOOLS extends Array<Tool<any, any, any>>>(model: GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS>, tools: TOOLS, prompt: (tools: TOOLS) => PROMPT & GenerateJsonOrTextPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): Promise<{
18
18
  tool: null;
19
19
  parameters: null;
20
20
  result: null;
@@ -22,9 +22,7 @@ export async function useToolOrGenerateText(model, tools, prompt, options) {
22
22
  throw new NoSuchToolError(schema.toString());
23
23
  }
24
24
  const toolParameters = modelResponse.value;
25
- const result = await executeTool(tool, toolParameters, {
26
- run: options?.run,
27
- });
25
+ const result = await executeTool(tool, toolParameters, options);
28
26
  return {
29
27
  tool: schema,
30
28
  result,
@@ -20,6 +20,9 @@ class PerformanceNowDurationMeasurement {
20
20
  get startEpochSeconds() {
21
21
  return Math.floor((globalThis.performance.timeOrigin + this.startTime) / 1000);
22
22
  }
23
+ get startDate() {
24
+ return new Date(this.startEpochSeconds * 1000);
25
+ }
23
26
  get durationInMs() {
24
27
  return Math.ceil(globalThis.performance.now() - this.startTime);
25
28
  }
@@ -36,6 +39,9 @@ class DateDurationMeasurement {
36
39
  get startEpochSeconds() {
37
40
  return Math.floor(this.startTime / 1000);
38
41
  }
42
+ get startDate() {
43
+ return new Date(this.startTime);
44
+ }
39
45
  get durationInMs() {
40
46
  return Date.now() - this.startTime;
41
47
  }
@@ -1,5 +1,6 @@
1
1
  export declare function startDurationMeasurement(): DurationMeasurement;
2
2
  export interface DurationMeasurement {
3
3
  startEpochSeconds: number;
4
+ startDate: Date;
4
5
  durationInMs: number;
5
6
  }
@@ -16,6 +16,9 @@ class PerformanceNowDurationMeasurement {
16
16
  get startEpochSeconds() {
17
17
  return Math.floor((globalThis.performance.timeOrigin + this.startTime) / 1000);
18
18
  }
19
+ get startDate() {
20
+ return new Date(this.startEpochSeconds * 1000);
21
+ }
19
22
  get durationInMs() {
20
23
  return Math.ceil(globalThis.performance.now() - this.startTime);
21
24
  }
@@ -32,6 +35,9 @@ class DateDurationMeasurement {
32
35
  get startEpochSeconds() {
33
36
  return Math.floor(this.startTime / 1000);
34
37
  }
38
+ get startDate() {
39
+ return new Date(this.startTime);
40
+ }
35
41
  get durationInMs() {
36
42
  return Date.now() - this.startTime;
37
43
  }
@@ -105,6 +105,14 @@ const postToApi = async ({ url, headers = {}, body, successfulResponseHandler, f
105
105
  throw error;
106
106
  }
107
107
  }
108
+ // unwrap original error when fetch failed (for easier debugging):
109
+ if (error instanceof TypeError && error.message === "fetch failed") {
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
+ if (error.cause != null) {
112
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
+ throw error.cause;
114
+ }
115
+ }
108
116
  throw error;
109
117
  }
110
118
  };
@@ -98,6 +98,14 @@ export const postToApi = async ({ url, headers = {}, body, successfulResponseHan
98
98
  throw error;
99
99
  }
100
100
  }
101
+ // unwrap original error when fetch failed (for easier debugging):
102
+ if (error instanceof TypeError && error.message === "fetch failed") {
103
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
104
+ if (error.cause != null) {
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
+ throw error.cause;
107
+ }
108
+ }
101
109
  throw error;
102
110
  }
103
111
  };
@@ -1,6 +0,0 @@
1
- import { Run } from "../run/Run.js";
2
- export interface FunctionOptions<SETTINGS> {
3
- functionId?: string;
4
- settings?: Partial<SETTINGS>;
5
- run?: Run;
6
- }
@@ -1,7 +0,0 @@
1
- export type IdMetadata = {
2
- callId?: string | undefined;
3
- functionId?: string | undefined;
4
- runId?: string | undefined;
5
- sessionId?: string | undefined;
6
- userId?: string | undefined;
7
- };