modelfusion 0.19.0 → 0.20.1

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 (39) hide show
  1. package/README.md +5 -6
  2. package/model-function/embed-text/embedText.cjs +64 -18
  3. package/model-function/embed-text/embedText.d.ts +3 -19
  4. package/model-function/embed-text/embedText.js +64 -18
  5. package/model-function/executeCall.cjs +49 -3
  6. package/model-function/executeCall.d.ts +18 -5
  7. package/model-function/executeCall.js +46 -1
  8. package/model-function/generate-image/generateImage.cjs +17 -9
  9. package/model-function/generate-image/generateImage.d.ts +2 -11
  10. package/model-function/generate-image/generateImage.js +17 -9
  11. package/model-function/generate-json/generateJson.cjs +2 -9
  12. package/model-function/generate-json/generateJson.d.ts +2 -11
  13. package/model-function/generate-json/generateJson.js +2 -9
  14. package/model-function/generate-json/generateJsonOrText.cjs +2 -9
  15. package/model-function/generate-json/generateJsonOrText.d.ts +3 -15
  16. package/model-function/generate-json/generateJsonOrText.js +2 -9
  17. package/model-function/generate-text/generateText.cjs +14 -9
  18. package/model-function/generate-text/generateText.d.ts +2 -11
  19. package/model-function/generate-text/generateText.js +14 -9
  20. package/model-function/generate-text/streamText.cjs +44 -9
  21. package/model-function/generate-text/streamText.d.ts +16 -12
  22. package/model-function/generate-text/streamText.js +41 -7
  23. package/model-function/synthesize-speech/synthesizeSpeech.cjs +5 -8
  24. package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -10
  25. package/model-function/synthesize-speech/synthesizeSpeech.js +5 -8
  26. package/model-function/transcribe-speech/transcribe.cjs +16 -9
  27. package/model-function/transcribe-speech/transcribe.d.ts +2 -11
  28. package/model-function/transcribe-speech/transcribe.js +16 -9
  29. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  30. package/package.json +1 -1
  31. package/run/RunFunctionEvent.d.ts +1 -1
  32. package/tool/executeTool.cjs +48 -9
  33. package/tool/executeTool.d.ts +16 -9
  34. package/tool/executeTool.js +45 -7
  35. package/tool/index.cjs +1 -0
  36. package/tool/index.d.ts +1 -0
  37. package/tool/index.js +1 -0
  38. package/tool/useTool.cjs +2 -3
  39. package/tool/useTool.js +2 -3
@@ -2,10 +2,22 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateText = void 0;
4
4
  const executeCall_js_1 = require("../executeCall.cjs");
5
- async function generateText(
5
+ /**
6
+ * Generates a text using a prompt.
7
+ * The prompt format depends on the model.
8
+ * For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
9
+ *
10
+ * @example
11
+ * const model = new OpenAITextGenerationModel(...);
12
+ *
13
+ * const text = await model.generateText(
14
+ * "Write a short story about a robot learning to love:\n\n"
15
+ * );
16
+ */
17
+ function generateText(
6
18
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
19
  model, prompt, options) {
8
- const result = await (0, executeCall_js_1.executeCall)({
20
+ return (0, executeCall_js_1.executeCall)({
9
21
  model,
10
22
  options,
11
23
  generateResponse: (options) => model.generateTextResponse(prompt, options),
@@ -46,12 +58,5 @@ model, prompt, options) {
46
58
  generatedText: output,
47
59
  }),
48
60
  });
49
- return options?.fullResponse === true
50
- ? {
51
- text: result.output,
52
- response: result.response,
53
- metadata: result.metadata,
54
- }
55
- : result.output;
56
61
  }
57
62
  exports.generateText = generateText;
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../FunctionOptions.js";
2
- import { CallMetadata } from "../executeCall.js";
2
+ import { ModelFunctionPromise } from "../executeCall.js";
3
3
  import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
4
4
  /**
5
5
  * Generates a text using a prompt.
@@ -13,13 +13,4 @@ import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerati
13
13
  * "Write a short story about a robot learning to love:\n\n"
14
14
  * );
15
15
  */
16
- export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options: FunctionOptions<SETTINGS> & {
17
- fullResponse: true;
18
- }): Promise<{
19
- text: string;
20
- response: RESPONSE;
21
- metadata: CallMetadata<TextGenerationModel<PROMPT, RESPONSE, unknown, SETTINGS>>;
22
- }>;
23
- export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options?: FunctionOptions<SETTINGS> & {
24
- fullResponse?: false;
25
- }): Promise<string>;
16
+ export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, string, RESPONSE>;
@@ -1,8 +1,20 @@
1
1
  import { executeCall } from "../executeCall.js";
2
- export async function generateText(
2
+ /**
3
+ * Generates a text using a prompt.
4
+ * The prompt format depends on the model.
5
+ * For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
6
+ *
7
+ * @example
8
+ * const model = new OpenAITextGenerationModel(...);
9
+ *
10
+ * const text = await model.generateText(
11
+ * "Write a short story about a robot learning to love:\n\n"
12
+ * );
13
+ */
14
+ export function generateText(
3
15
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
16
  model, prompt, options) {
5
- const result = await executeCall({
17
+ return executeCall({
6
18
  model,
7
19
  options,
8
20
  generateResponse: (options) => model.generateTextResponse(prompt, options),
@@ -43,11 +55,4 @@ model, prompt, options) {
43
55
  generatedText: output,
44
56
  }),
45
57
  });
46
- return options?.fullResponse === true
47
- ? {
48
- text: result.output,
49
- response: result.response,
50
- metadata: result.metadata,
51
- }
52
- : result.output;
53
58
  }
@@ -1,13 +1,51 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.streamText = void 0;
3
+ exports.streamText = exports.StreamTextPromise = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
5
  const RunFunctionEventSource_js_1 = require("../../run/RunFunctionEventSource.cjs");
6
6
  const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
7
7
  const AbortError_js_1 = require("../../util/api/AbortError.cjs");
8
8
  const runSafe_js_1 = require("../../util/runSafe.cjs");
9
9
  const extractTextDeltas_js_1 = require("./extractTextDeltas.cjs");
10
- async function streamText(model, prompt, options) {
10
+ class StreamTextPromise extends Promise {
11
+ constructor(fullPromise) {
12
+ super((resolve) => {
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ resolve(null); // we override the resolve function
15
+ });
16
+ Object.defineProperty(this, "fullPromise", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: fullPromise
21
+ });
22
+ Object.defineProperty(this, "outputPromise", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.outputPromise = fullPromise.then((result) => result.output);
29
+ }
30
+ asFullResponse() {
31
+ return this.fullPromise;
32
+ }
33
+ then(onfulfilled, onrejected) {
34
+ return this.outputPromise.then(onfulfilled, onrejected);
35
+ }
36
+ catch(onrejected) {
37
+ return this.outputPromise.catch(onrejected);
38
+ }
39
+ finally(onfinally) {
40
+ return this.outputPromise.finally(onfinally);
41
+ }
42
+ }
43
+ exports.StreamTextPromise = StreamTextPromise;
44
+ function streamText(model, prompt, options) {
45
+ return new StreamTextPromise(doStreamText(model, prompt, options));
46
+ }
47
+ exports.streamText = streamText;
48
+ async function doStreamText(model, prompt, options) {
11
49
  if (options?.settings != null) {
12
50
  model = model.withSettings(options.settings);
13
51
  options = {
@@ -107,11 +145,8 @@ async function streamText(model, prompt, options) {
107
145
  });
108
146
  throw result.error;
109
147
  }
110
- return options?.fullResponse === true
111
- ? {
112
- textStream: result.output,
113
- metadata: startMetadata,
114
- }
115
- : result.output;
148
+ return {
149
+ output: result.output,
150
+ metadata: startMetadata,
151
+ };
116
152
  }
117
- exports.streamText = streamText;
@@ -2,18 +2,22 @@ import { FunctionOptions } from "../FunctionOptions.js";
2
2
  import { CallMetadata } from "../executeCall.js";
3
3
  import { DeltaEvent } from "./DeltaEvent.js";
4
4
  import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
5
+ export declare class StreamTextPromise<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings> extends Promise<AsyncIterable<string>> {
6
+ private fullPromise;
7
+ private outputPromise;
8
+ constructor(fullPromise: Promise<{
9
+ output: AsyncIterable<string>;
10
+ metadata: Omit<CallMetadata<TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS>>, "durationInMs">;
11
+ }>);
12
+ asFullResponse(): Promise<{
13
+ output: AsyncIterable<string>;
14
+ metadata: Omit<CallMetadata<TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS>>, "durationInMs">;
15
+ }>;
16
+ then<TResult1 = AsyncIterable<string>, TResult2 = never>(onfulfilled?: ((value: AsyncIterable<string>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
17
+ catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<AsyncIterable<string> | TResult>;
18
+ finally(onfinally?: (() => void) | undefined | null): Promise<AsyncIterable<string>>;
19
+ }
5
20
  export declare function streamText<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS> & {
6
21
  generateDeltaStreamResponse: (prompt: PROMPT, options: FunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
7
22
  extractTextDelta: (fullDelta: FULL_DELTA) => string | undefined;
8
- }, prompt: PROMPT, options?: FunctionOptions<SETTINGS> & {
9
- fullResponse?: false;
10
- }): Promise<AsyncIterable<string>>;
11
- export declare function streamText<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS> & {
12
- generateDeltaStreamResponse: (prompt: PROMPT, options: FunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
13
- extractTextDelta: (fullDelta: FULL_DELTA) => string | undefined;
14
- }, prompt: PROMPT, options: FunctionOptions<SETTINGS> & {
15
- fullResponse: true;
16
- }): Promise<{
17
- textStream: AsyncIterable<string>;
18
- metadata: Omit<CallMetadata<TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS>>, "durationInMs">;
19
- }>;
23
+ }, prompt: PROMPT, options?: FunctionOptions<SETTINGS>): StreamTextPromise<PROMPT, FULL_DELTA, SETTINGS>;
@@ -4,7 +4,43 @@ import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
4
4
  import { AbortError } from "../../util/api/AbortError.js";
5
5
  import { runSafe } from "../../util/runSafe.js";
6
6
  import { extractTextDeltas } from "./extractTextDeltas.js";
7
- export async function streamText(model, prompt, options) {
7
+ export class StreamTextPromise extends Promise {
8
+ constructor(fullPromise) {
9
+ super((resolve) => {
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ resolve(null); // we override the resolve function
12
+ });
13
+ Object.defineProperty(this, "fullPromise", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: fullPromise
18
+ });
19
+ Object.defineProperty(this, "outputPromise", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.outputPromise = fullPromise.then((result) => result.output);
26
+ }
27
+ asFullResponse() {
28
+ return this.fullPromise;
29
+ }
30
+ then(onfulfilled, onrejected) {
31
+ return this.outputPromise.then(onfulfilled, onrejected);
32
+ }
33
+ catch(onrejected) {
34
+ return this.outputPromise.catch(onrejected);
35
+ }
36
+ finally(onfinally) {
37
+ return this.outputPromise.finally(onfinally);
38
+ }
39
+ }
40
+ export function streamText(model, prompt, options) {
41
+ return new StreamTextPromise(doStreamText(model, prompt, options));
42
+ }
43
+ async function doStreamText(model, prompt, options) {
8
44
  if (options?.settings != null) {
9
45
  model = model.withSettings(options.settings);
10
46
  options = {
@@ -104,10 +140,8 @@ export async function streamText(model, prompt, options) {
104
140
  });
105
141
  throw result.error;
106
142
  }
107
- return options?.fullResponse === true
108
- ? {
109
- textStream: result.output,
110
- metadata: startMetadata,
111
- }
112
- : result.output;
143
+ return {
144
+ output: result.output,
145
+ metadata: startMetadata,
146
+ };
113
147
  }
@@ -2,8 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.synthesizeSpeech = void 0;
4
4
  const executeCall_js_1 = require("../executeCall.cjs");
5
- async function synthesizeSpeech(model, text, options) {
6
- const result = await (0, executeCall_js_1.executeCall)({
5
+ /**
6
+ * Synthesizes speech from text.
7
+ */
8
+ function synthesizeSpeech(model, text, options) {
9
+ return (0, executeCall_js_1.executeCall)({
7
10
  model,
8
11
  options,
9
12
  generateResponse: (options) => model.generateSpeechResponse(text, options),
@@ -39,11 +42,5 @@ async function synthesizeSpeech(model, text, options) {
39
42
  speech: output,
40
43
  }),
41
44
  });
42
- return options?.fullResponse === true
43
- ? {
44
- speech: result.output,
45
- metadata: result.metadata,
46
- }
47
- : result.output;
48
45
  }
49
46
  exports.synthesizeSpeech = synthesizeSpeech;
@@ -1,16 +1,8 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import { FunctionOptions } from "../FunctionOptions.js";
3
- import { CallMetadata } from "../executeCall.js";
3
+ import { ModelFunctionPromise } from "../executeCall.js";
4
4
  import { SpeechSynthesisModel, SpeechSynthesisModelSettings } from "./SpeechSynthesisModel.js";
5
5
  /**
6
6
  * Synthesizes speech from text.
7
7
  */
8
- export declare function synthesizeSpeech<SETTINGS extends SpeechSynthesisModelSettings>(model: SpeechSynthesisModel<SETTINGS>, text: string, options: FunctionOptions<SETTINGS> & {
9
- fullResponse: true;
10
- }): Promise<{
11
- speech: Buffer;
12
- metadata: CallMetadata<SpeechSynthesisModel<SETTINGS>>;
13
- }>;
14
- export declare function synthesizeSpeech<SETTINGS extends SpeechSynthesisModelSettings>(model: SpeechSynthesisModel<SETTINGS>, text: string, options?: FunctionOptions<SETTINGS> & {
15
- fullResponse?: false;
16
- }): Promise<Buffer>;
8
+ export declare function synthesizeSpeech<SETTINGS extends SpeechSynthesisModelSettings>(model: SpeechSynthesisModel<SETTINGS>, text: string, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<SpeechSynthesisModel<SETTINGS>, Buffer, Buffer>;
@@ -1,6 +1,9 @@
1
1
  import { executeCall } from "../executeCall.js";
2
- export async function synthesizeSpeech(model, text, options) {
3
- const result = await executeCall({
2
+ /**
3
+ * Synthesizes speech from text.
4
+ */
5
+ export function synthesizeSpeech(model, text, options) {
6
+ return executeCall({
4
7
  model,
5
8
  options,
6
9
  generateResponse: (options) => model.generateSpeechResponse(text, options),
@@ -36,10 +39,4 @@ export async function synthesizeSpeech(model, text, options) {
36
39
  speech: output,
37
40
  }),
38
41
  });
39
- return options?.fullResponse === true
40
- ? {
41
- speech: result.output,
42
- metadata: result.metadata,
43
- }
44
- : result.output;
45
42
  }
@@ -2,8 +2,22 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transcribe = void 0;
4
4
  const executeCall_js_1 = require("../executeCall.cjs");
5
- async function transcribe(model, data, options) {
6
- const result = await (0, executeCall_js_1.executeCall)({
5
+ /**
6
+ * Transcribe audio data into text.
7
+ *
8
+ * @example
9
+ * const data = await fs.promises.readFile("data/test.mp3");
10
+ *
11
+ * const transcription = await transcribe(
12
+ * new OpenAITranscriptionModel({ model: "whisper-1" }),
13
+ * {
14
+ * type: "mp3",
15
+ * data,
16
+ * }
17
+ * );
18
+ */
19
+ function transcribe(model, data, options) {
20
+ return (0, executeCall_js_1.executeCall)({
7
21
  model,
8
22
  options,
9
23
  generateResponse: (options) => model.generateTranscriptionResponse(data, options),
@@ -39,12 +53,5 @@ async function transcribe(model, data, options) {
39
53
  transcription: output,
40
54
  }),
41
55
  });
42
- return options?.fullResponse === true
43
- ? {
44
- transcription: result.output,
45
- response: result.response,
46
- metadata: result.metadata,
47
- }
48
- : result.output;
49
56
  }
50
57
  exports.transcribe = transcribe;
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../FunctionOptions.js";
2
- import { CallMetadata } from "../executeCall.js";
2
+ import { ModelFunctionPromise } from "../executeCall.js";
3
3
  import { TranscriptionModel, TranscriptionModelSettings } from "./TranscriptionModel.js";
4
4
  /**
5
5
  * Transcribe audio data into text.
@@ -15,13 +15,4 @@ import { TranscriptionModel, TranscriptionModelSettings } from "./TranscriptionM
15
15
  * }
16
16
  * );
17
17
  */
18
- export declare function transcribe<DATA, RESPONSE, SETTINGS extends TranscriptionModelSettings>(model: TranscriptionModel<DATA, RESPONSE, SETTINGS>, data: DATA, options: FunctionOptions<SETTINGS> & {
19
- fullResponse: true;
20
- }): Promise<{
21
- transcription: string;
22
- response: RESPONSE;
23
- metadata: CallMetadata<TranscriptionModel<DATA, RESPONSE, SETTINGS>>;
24
- }>;
25
- export declare function transcribe<DATA, RESPONSE, SETTINGS extends TranscriptionModelSettings>(model: TranscriptionModel<DATA, RESPONSE, SETTINGS>, data: DATA, options?: FunctionOptions<SETTINGS> & {
26
- fullResponse?: false;
27
- }): Promise<string>;
18
+ export declare function transcribe<DATA, RESPONSE, SETTINGS extends TranscriptionModelSettings>(model: TranscriptionModel<DATA, RESPONSE, SETTINGS>, data: DATA, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<TranscriptionModel<DATA, RESPONSE, SETTINGS>, string, RESPONSE>;
@@ -1,6 +1,20 @@
1
1
  import { executeCall } from "../executeCall.js";
2
- export async function transcribe(model, data, options) {
3
- const result = await executeCall({
2
+ /**
3
+ * Transcribe audio data into text.
4
+ *
5
+ * @example
6
+ * const data = await fs.promises.readFile("data/test.mp3");
7
+ *
8
+ * const transcription = await transcribe(
9
+ * new OpenAITranscriptionModel({ model: "whisper-1" }),
10
+ * {
11
+ * type: "mp3",
12
+ * data,
13
+ * }
14
+ * );
15
+ */
16
+ export function transcribe(model, data, options) {
17
+ return executeCall({
4
18
  model,
5
19
  options,
6
20
  generateResponse: (options) => model.generateTranscriptionResponse(data, options),
@@ -36,11 +50,4 @@ export async function transcribe(model, data, options) {
36
50
  transcription: output,
37
51
  }),
38
52
  });
39
- return options?.fullResponse === true
40
- ? {
41
- transcription: result.output,
42
- response: result.response,
43
- metadata: result.metadata,
44
- }
45
- : result.output;
46
53
  }
@@ -64,13 +64,13 @@ export declare class CohereTextEmbeddingModel extends AbstractModel<CohereTextEm
64
64
  callAPI(texts: Array<string>, options?: FunctionOptions<CohereTextEmbeddingModelSettings>): Promise<CohereTextEmbeddingResponse>;
65
65
  generateEmbeddingResponse(texts: string[], options?: FunctionOptions<CohereTextEmbeddingModelSettings>): Promise<{
66
66
  texts: string[];
67
- embeddings: number[][];
68
67
  id: string;
69
68
  meta: {
70
69
  api_version: {
71
70
  version: string;
72
71
  };
73
72
  };
73
+ embeddings: number[][];
74
74
  }>;
75
75
  extractEmbeddings(response: CohereTextEmbeddingResponse): number[][];
76
76
  withSettings(additionalSettings: Partial<CohereTextEmbeddingModelSettings>): this;
@@ -98,22 +98,22 @@ declare const cohereTextEmbeddingResponseSchema: z.ZodObject<{
98
98
  }>;
99
99
  }, "strip", z.ZodTypeAny, {
100
100
  texts: string[];
101
- embeddings: number[][];
102
101
  id: string;
103
102
  meta: {
104
103
  api_version: {
105
104
  version: string;
106
105
  };
107
106
  };
107
+ embeddings: number[][];
108
108
  }, {
109
109
  texts: string[];
110
- embeddings: number[][];
111
110
  id: string;
112
111
  meta: {
113
112
  api_version: {
114
113
  version: string;
115
114
  };
116
115
  };
116
+ embeddings: number[][];
117
117
  }>;
118
118
  export type CohereTextEmbeddingResponse = z.infer<typeof cohereTextEmbeddingResponseSchema>;
119
119
  export {};
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.19.0",
4
+ "version": "0.20.1",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -1,4 +1,4 @@
1
- import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "tool/ExecuteToolEvent.js";
1
+ import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/ExecuteToolEvent.js";
2
2
  import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
3
3
  import { IdMetadata } from "./IdMetadata.js";
4
4
  export type RunFunctionEvent = RunFunctionStartedEvent | RunFunctionFinishedEvent;
@@ -1,14 +1,56 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.executeTool = void 0;
3
+ exports.executeTool = exports.ExecuteToolPromise = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
5
  const RunFunctionEventSource_js_1 = require("../run/RunFunctionEventSource.cjs");
6
6
  const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
7
7
  const AbortError_js_1 = require("../util/api/AbortError.cjs");
8
8
  const runSafe_js_1 = require("../util/runSafe.cjs");
9
9
  const ToolExecutionError_js_1 = require("./ToolExecutionError.cjs");
10
+ class ExecuteToolPromise extends Promise {
11
+ constructor(fullPromise) {
12
+ super((resolve) => {
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ resolve(null); // we override the resolve function
15
+ });
16
+ Object.defineProperty(this, "fullPromise", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: fullPromise
21
+ });
22
+ Object.defineProperty(this, "outputPromise", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.outputPromise = fullPromise.then((result) => result.output);
29
+ }
30
+ asFullResponse() {
31
+ return this.fullPromise;
32
+ }
33
+ then(onfulfilled, onrejected) {
34
+ return this.outputPromise.then(onfulfilled, onrejected);
35
+ }
36
+ catch(onrejected) {
37
+ return this.outputPromise.catch(onrejected);
38
+ }
39
+ finally(onfinally) {
40
+ return this.outputPromise.finally(onfinally);
41
+ }
42
+ }
43
+ exports.ExecuteToolPromise = ExecuteToolPromise;
44
+ /**
45
+ * `executeTool` directly executes a tool with the given parameters.
46
+ */
10
47
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- async function executeTool(tool, input, options) {
48
+ function executeTool(tool, input, options) {
49
+ return new ExecuteToolPromise(doExecuteTool(tool, input, options));
50
+ }
51
+ exports.executeTool = executeTool;
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ async function doExecuteTool(tool, input, options) {
12
54
  const run = options?.run;
13
55
  const eventSource = new RunFunctionEventSource_js_1.RunFunctionEventSource({
14
56
  observers: run?.observers ?? [],
@@ -70,11 +112,8 @@ async function executeTool(tool, input, options) {
70
112
  input,
71
113
  output,
72
114
  });
73
- return options?.fullResponse === true
74
- ? {
75
- output,
76
- metadata: finishMetadata,
77
- }
78
- : output;
115
+ return {
116
+ output,
117
+ metadata: finishMetadata,
118
+ };
79
119
  }
80
- exports.executeTool = executeTool;
@@ -10,15 +10,22 @@ export type ExecuteToolMetadata = {
10
10
  startEpochSeconds: number;
11
11
  durationInMs: number;
12
12
  };
13
+ export declare class ExecuteToolPromise<OUTPUT> extends Promise<OUTPUT> {
14
+ private fullPromise;
15
+ private outputPromise;
16
+ constructor(fullPromise: Promise<{
17
+ output: OUTPUT;
18
+ metadata: ExecuteToolMetadata;
19
+ }>);
20
+ asFullResponse(): Promise<{
21
+ output: OUTPUT;
22
+ metadata: ExecuteToolMetadata;
23
+ }>;
24
+ then<TResult1 = OUTPUT, TResult2 = never>(onfulfilled?: ((value: OUTPUT) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
25
+ catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<OUTPUT | TResult>;
26
+ finally(onfinally?: (() => void) | undefined | null): Promise<OUTPUT>;
27
+ }
13
28
  /**
14
29
  * `executeTool` directly executes a tool with the given parameters.
15
30
  */
16
- export declare function executeTool<TOOL extends Tool<any, any, any>>(tool: TOOL, input: z.infer<TOOL["inputSchema"]>, options: FunctionOptions<undefined> & {
17
- fullResponse: true;
18
- }): Promise<{
19
- output: Awaited<ReturnType<TOOL["execute"]>>;
20
- metadata: ExecuteToolMetadata;
21
- }>;
22
- export declare function executeTool<TOOL extends Tool<any, any, any>>(tool: TOOL, input: z.infer<TOOL["inputSchema"]>, options?: FunctionOptions<undefined> & {
23
- fullResponse?: false;
24
- }): Promise<ReturnType<TOOL["execute"]>>;
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"]>>;
@@ -4,8 +4,48 @@ import { startDurationMeasurement } from "../util/DurationMeasurement.js";
4
4
  import { AbortError } from "../util/api/AbortError.js";
5
5
  import { runSafe } from "../util/runSafe.js";
6
6
  import { ToolExecutionError } from "./ToolExecutionError.js";
7
+ export class ExecuteToolPromise extends Promise {
8
+ constructor(fullPromise) {
9
+ super((resolve) => {
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ resolve(null); // we override the resolve function
12
+ });
13
+ Object.defineProperty(this, "fullPromise", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: fullPromise
18
+ });
19
+ Object.defineProperty(this, "outputPromise", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.outputPromise = fullPromise.then((result) => result.output);
26
+ }
27
+ asFullResponse() {
28
+ return this.fullPromise;
29
+ }
30
+ then(onfulfilled, onrejected) {
31
+ return this.outputPromise.then(onfulfilled, onrejected);
32
+ }
33
+ catch(onrejected) {
34
+ return this.outputPromise.catch(onrejected);
35
+ }
36
+ finally(onfinally) {
37
+ return this.outputPromise.finally(onfinally);
38
+ }
39
+ }
40
+ /**
41
+ * `executeTool` directly executes a tool with the given parameters.
42
+ */
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ export function executeTool(tool, input, options) {
45
+ return new ExecuteToolPromise(doExecuteTool(tool, input, options));
46
+ }
7
47
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- export async function executeTool(tool, input, options) {
48
+ async function doExecuteTool(tool, input, options) {
9
49
  const run = options?.run;
10
50
  const eventSource = new RunFunctionEventSource({
11
51
  observers: run?.observers ?? [],
@@ -67,10 +107,8 @@ export async function executeTool(tool, input, options) {
67
107
  input,
68
108
  output,
69
109
  });
70
- return options?.fullResponse === true
71
- ? {
72
- output,
73
- metadata: finishMetadata,
74
- }
75
- : output;
110
+ return {
111
+ output,
112
+ metadata: finishMetadata,
113
+ };
76
114
  }
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);