modelfusion 0.7.0 → 0.8.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 (73) hide show
  1. package/README.md +14 -4
  2. package/composed-function/index.cjs +0 -3
  3. package/composed-function/index.d.ts +0 -3
  4. package/composed-function/index.js +0 -3
  5. package/index.cjs +1 -0
  6. package/index.d.ts +1 -0
  7. package/index.js +1 -0
  8. package/model-function/Model.d.ts +2 -2
  9. package/model-function/ModelCallEvent.d.ts +4 -6
  10. package/model-function/SuccessfulModelCall.cjs +6 -3
  11. package/model-function/SuccessfulModelCall.d.ts +3 -3
  12. package/model-function/SuccessfulModelCall.js +6 -3
  13. package/model-function/executeCall.cjs +6 -6
  14. package/model-function/executeCall.js +6 -6
  15. package/model-function/generate-json/JsonGenerationEvent.d.ts +2 -2
  16. package/model-function/generate-json/generateJsonOrText.cjs +4 -4
  17. package/model-function/generate-json/generateJsonOrText.js +4 -4
  18. package/model-function/generate-text/streamText.cjs +7 -7
  19. package/model-function/generate-text/streamText.js +7 -7
  20. package/model-function/index.cjs +1 -1
  21. package/model-function/index.d.ts +1 -1
  22. package/model-function/index.js +1 -1
  23. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  24. package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +1 -1
  25. package/package.json +1 -1
  26. package/run/ConsoleLogger.cjs +2 -2
  27. package/run/ConsoleLogger.d.ts +5 -5
  28. package/run/ConsoleLogger.js +2 -2
  29. package/run/DefaultRun.cjs +7 -7
  30. package/run/DefaultRun.d.ts +6 -6
  31. package/run/DefaultRun.js +7 -7
  32. package/run/Run.d.ts +2 -2
  33. package/run/RunFunction.d.ts +0 -4
  34. package/run/RunFunctionEvent.d.ts +12 -0
  35. package/{model-function/ModelCallEventSource.cjs → run/RunFunctionEventSource.cjs} +7 -7
  36. package/run/RunFunctionEventSource.d.ts +13 -0
  37. package/{model-function/ModelCallEventSource.js → run/RunFunctionEventSource.js} +5 -5
  38. package/run/RunFunctionObserver.cjs +2 -0
  39. package/run/RunFunctionObserver.d.ts +5 -0
  40. package/run/RunFunctionObserver.js +1 -0
  41. package/run/index.cjs +3 -0
  42. package/run/index.d.ts +3 -0
  43. package/run/index.js +3 -0
  44. package/tool/ExecuteToolEvent.cjs +2 -0
  45. package/tool/ExecuteToolEvent.d.ts +22 -0
  46. package/tool/ExecuteToolEvent.js +1 -0
  47. package/{composed-function/use-tool → tool}/Tool.cjs +7 -0
  48. package/{composed-function/use-tool → tool}/Tool.d.ts +5 -2
  49. package/{composed-function/use-tool → tool}/Tool.js +7 -0
  50. package/tool/ToolExecutionError.cjs +31 -0
  51. package/tool/ToolExecutionError.d.ts +11 -0
  52. package/tool/ToolExecutionError.js +27 -0
  53. package/tool/executeTool.cjs +79 -0
  54. package/tool/executeTool.d.ts +20 -0
  55. package/tool/executeTool.js +75 -0
  56. package/tool/index.cjs +22 -0
  57. package/tool/index.d.ts +6 -0
  58. package/tool/index.js +6 -0
  59. package/tool/useTool.cjs +33 -0
  60. package/tool/useTool.d.ts +15 -0
  61. package/tool/useTool.js +29 -0
  62. package/tool/useToolOrGenerateText.cjs +38 -0
  63. package/{composed-function/use-tool/useTool.d.ts → tool/useToolOrGenerateText.d.ts} +2 -15
  64. package/tool/useToolOrGenerateText.js +34 -0
  65. package/composed-function/use-tool/useTool.cjs +0 -62
  66. package/composed-function/use-tool/useTool.js +0 -57
  67. package/model-function/ModelCallEventSource.d.ts +0 -13
  68. package/model-function/ModelCallObserver.d.ts +0 -5
  69. /package/{model-function/ModelCallObserver.cjs → run/RunFunctionEvent.cjs} +0 -0
  70. /package/{model-function/ModelCallObserver.js → run/RunFunctionEvent.js} +0 -0
  71. /package/{composed-function/use-tool → tool}/NoSuchToolError.cjs +0 -0
  72. /package/{composed-function/use-tool → tool}/NoSuchToolError.d.ts +0 -0
  73. /package/{composed-function/use-tool → tool}/NoSuchToolError.js +0 -0
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultRun = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
- const SuccessfulModelCall_js_1 = require("../model-function/SuccessfulModelCall.cjs");
6
5
  const calculateCost_js_1 = require("../cost/calculateCost.cjs");
6
+ const SuccessfulModelCall_js_1 = require("../model-function/SuccessfulModelCall.cjs");
7
7
  class DefaultRun {
8
8
  constructor({ runId = (0, nanoid_1.nanoid)(), sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
9
9
  Object.defineProperty(this, "runId", {
@@ -36,7 +36,7 @@ class DefaultRun {
36
36
  writable: true,
37
37
  value: void 0
38
38
  });
39
- Object.defineProperty(this, "modelCallEvents", {
39
+ Object.defineProperty(this, "events", {
40
40
  enumerable: true,
41
41
  configurable: true,
42
42
  writable: true,
@@ -55,18 +55,18 @@ class DefaultRun {
55
55
  this.costCalculators = costCalculators;
56
56
  this.observers = [
57
57
  {
58
- onModelCallStarted: (event) => {
59
- this.modelCallEvents.push(event);
58
+ onRunFunctionStarted: (event) => {
59
+ this.events.push(event);
60
60
  },
61
- onModelCallFinished: (event) => {
62
- this.modelCallEvents.push(event);
61
+ onRunFunctionFinished: (event) => {
62
+ this.events.push(event);
63
63
  },
64
64
  },
65
65
  ...(observers ?? []),
66
66
  ];
67
67
  }
68
68
  get successfulModelCalls() {
69
- return (0, SuccessfulModelCall_js_1.extractSuccessfulModelCalls)(this.modelCallEvents);
69
+ return (0, SuccessfulModelCall_js_1.extractSuccessfulModelCalls)(this.events);
70
70
  }
71
71
  calculateCost() {
72
72
  return (0, calculateCost_js_1.calculateCost)({
@@ -1,22 +1,22 @@
1
+ import { CostCalculator } from "../cost/CostCalculator.js";
1
2
  import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
2
- import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
3
- import { ModelCallObserver } from "../model-function/ModelCallObserver.js";
4
3
  import { Run } from "./Run.js";
5
- import { CostCalculator } from "../cost/CostCalculator.js";
4
+ import { RunFunctionEvent } from "./RunFunctionEvent.js";
5
+ import { RunFunctionObserver } from "./RunFunctionObserver.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 modelCallEvents: (ModelCallFinishedEvent | ModelCallStartedEvent)[];
13
- readonly observers?: ModelCallObserver[];
12
+ readonly events: RunFunctionEvent[];
13
+ readonly observers?: RunFunctionObserver[];
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?: ModelCallObserver[];
19
+ observers?: RunFunctionObserver[];
20
20
  costCalculators?: CostCalculator[];
21
21
  });
22
22
  get successfulModelCalls(): Array<SuccessfulModelCall>;
package/run/DefaultRun.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { nanoid as createId } from "nanoid";
2
- import { extractSuccessfulModelCalls, } from "../model-function/SuccessfulModelCall.js";
3
2
  import { calculateCost } from "../cost/calculateCost.js";
3
+ import { extractSuccessfulModelCalls, } from "../model-function/SuccessfulModelCall.js";
4
4
  export class DefaultRun {
5
5
  constructor({ runId = createId(), sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
6
6
  Object.defineProperty(this, "runId", {
@@ -33,7 +33,7 @@ export class DefaultRun {
33
33
  writable: true,
34
34
  value: void 0
35
35
  });
36
- Object.defineProperty(this, "modelCallEvents", {
36
+ Object.defineProperty(this, "events", {
37
37
  enumerable: true,
38
38
  configurable: true,
39
39
  writable: true,
@@ -52,18 +52,18 @@ export class DefaultRun {
52
52
  this.costCalculators = costCalculators;
53
53
  this.observers = [
54
54
  {
55
- onModelCallStarted: (event) => {
56
- this.modelCallEvents.push(event);
55
+ onRunFunctionStarted: (event) => {
56
+ this.events.push(event);
57
57
  },
58
- onModelCallFinished: (event) => {
59
- this.modelCallEvents.push(event);
58
+ onRunFunctionFinished: (event) => {
59
+ this.events.push(event);
60
60
  },
61
61
  },
62
62
  ...(observers ?? []),
63
63
  ];
64
64
  }
65
65
  get successfulModelCalls() {
66
- return extractSuccessfulModelCalls(this.modelCallEvents);
66
+ return extractSuccessfulModelCalls(this.events);
67
67
  }
68
68
  calculateCost() {
69
69
  return calculateCost({
package/run/Run.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ErrorHandler } from "../util/ErrorHandler.js";
2
- import { ModelCallObserver } from "../model-function/ModelCallObserver.js";
2
+ import { RunFunctionObserver } from "./RunFunctionObserver.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?: ModelCallObserver[];
25
+ observers?: RunFunctionObserver[];
26
26
  errorHandler?: ErrorHandler;
27
27
  }
@@ -1,4 +1,3 @@
1
- import { SafeResult } from "../util/SafeResult.js";
2
1
  import { Run } from "./Run.js";
3
2
  /**
4
3
  * Standardized function signature for functions that are part of a run.
@@ -8,6 +7,3 @@ import { Run } from "./Run.js";
8
7
  export type RunFunction<INPUT, OUTPUT> = (input: INPUT, options?: {
9
8
  run?: Run;
10
9
  }) => PromiseLike<OUTPUT>;
11
- export type SafeRunFunction<INPUT, OUTPUT> = (input: INPUT, options?: {
12
- run?: Run;
13
- }) => PromiseLike<SafeResult<OUTPUT>>;
@@ -0,0 +1,12 @@
1
+ import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "tool/ExecuteToolEvent.js";
2
+ import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
3
+ import { IdMetadata } from "./IdMetadata.js";
4
+ export type RunFunctionEvent = RunFunctionStartedEvent | RunFunctionFinishedEvent;
5
+ export type RunFunctionStartedEventMetadata = IdMetadata & {
6
+ startEpochSeconds: number;
7
+ };
8
+ export type RunFunctionStartedEvent = ModelCallStartedEvent | ExecuteToolStartedEvent;
9
+ export type RunFunctionFinishedEventMetadata = RunFunctionStartedEventMetadata & {
10
+ durationInMs: number;
11
+ };
12
+ export type RunFunctionFinishedEvent = ModelCallFinishedEvent | ExecuteToolFinishedEvent;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelCallEventSource = void 0;
4
- class ModelCallEventSource {
3
+ exports.RunFunctionEventSource = void 0;
4
+ class RunFunctionEventSource {
5
5
  constructor({ observers, errorHandler, }) {
6
6
  Object.defineProperty(this, "observers", {
7
7
  enumerable: true,
@@ -18,20 +18,20 @@ class ModelCallEventSource {
18
18
  this.observers = observers;
19
19
  this.errorHandler = errorHandler ?? ((error) => console.error(error));
20
20
  }
21
- notifyModelCallStarted(event) {
21
+ notifyRunFunctionStarted(event) {
22
22
  for (const observer of this.observers) {
23
23
  try {
24
- observer.onModelCallStarted?.(event);
24
+ observer.onRunFunctionStarted?.(event);
25
25
  }
26
26
  catch (error) {
27
27
  this.errorHandler(error);
28
28
  }
29
29
  }
30
30
  }
31
- notifyModelCallFinished(event) {
31
+ notifyRunFunctionFinished(event) {
32
32
  for (const observer of this.observers) {
33
33
  try {
34
- observer.onModelCallFinished?.(event);
34
+ observer.onRunFunctionFinished?.(event);
35
35
  }
36
36
  catch (error) {
37
37
  this.errorHandler(error);
@@ -39,4 +39,4 @@ class ModelCallEventSource {
39
39
  }
40
40
  }
41
41
  }
42
- exports.ModelCallEventSource = ModelCallEventSource;
42
+ exports.RunFunctionEventSource = RunFunctionEventSource;
@@ -0,0 +1,13 @@
1
+ import { ErrorHandler } from "../util/ErrorHandler.js";
2
+ import { RunFunctionFinishedEvent, RunFunctionStartedEvent } from "./RunFunctionEvent.js";
3
+ import { RunFunctionObserver } from "./RunFunctionObserver.js";
4
+ export declare class RunFunctionEventSource {
5
+ readonly observers: RunFunctionObserver[];
6
+ readonly errorHandler: ErrorHandler;
7
+ constructor({ observers, errorHandler, }: {
8
+ observers: RunFunctionObserver[];
9
+ errorHandler?: ErrorHandler;
10
+ });
11
+ notifyRunFunctionStarted(event: RunFunctionStartedEvent): void;
12
+ notifyRunFunctionFinished(event: RunFunctionFinishedEvent): void;
13
+ }
@@ -1,4 +1,4 @@
1
- export class ModelCallEventSource {
1
+ export class RunFunctionEventSource {
2
2
  constructor({ observers, errorHandler, }) {
3
3
  Object.defineProperty(this, "observers", {
4
4
  enumerable: true,
@@ -15,20 +15,20 @@ export class ModelCallEventSource {
15
15
  this.observers = observers;
16
16
  this.errorHandler = errorHandler ?? ((error) => console.error(error));
17
17
  }
18
- notifyModelCallStarted(event) {
18
+ notifyRunFunctionStarted(event) {
19
19
  for (const observer of this.observers) {
20
20
  try {
21
- observer.onModelCallStarted?.(event);
21
+ observer.onRunFunctionStarted?.(event);
22
22
  }
23
23
  catch (error) {
24
24
  this.errorHandler(error);
25
25
  }
26
26
  }
27
27
  }
28
- notifyModelCallFinished(event) {
28
+ notifyRunFunctionFinished(event) {
29
29
  for (const observer of this.observers) {
30
30
  try {
31
- observer.onModelCallFinished?.(event);
31
+ observer.onRunFunctionFinished?.(event);
32
32
  }
33
33
  catch (error) {
34
34
  this.errorHandler(error);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import { RunFunctionFinishedEvent, RunFunctionStartedEvent } from "./RunFunctionEvent.js";
2
+ export type RunFunctionObserver = {
3
+ onRunFunctionStarted?: (event: RunFunctionStartedEvent) => void;
4
+ onRunFunctionFinished?: (event: RunFunctionFinishedEvent) => void;
5
+ };
@@ -0,0 +1 @@
1
+ export {};
package/run/index.cjs CHANGED
@@ -19,4 +19,7 @@ __exportStar(require("./DefaultRun.cjs"), exports);
19
19
  __exportStar(require("./IdMetadata.cjs"), exports);
20
20
  __exportStar(require("./Run.cjs"), exports);
21
21
  __exportStar(require("./RunFunction.cjs"), exports);
22
+ __exportStar(require("./RunFunctionEvent.cjs"), exports);
23
+ __exportStar(require("./RunFunctionObserver.cjs"), exports);
24
+ __exportStar(require("./RunFunctionEventSource.cjs"), exports);
22
25
  __exportStar(require("./Vector.cjs"), exports);
package/run/index.d.ts CHANGED
@@ -3,4 +3,7 @@ export * from "./DefaultRun.js";
3
3
  export * from "./IdMetadata.js";
4
4
  export * from "./Run.js";
5
5
  export * from "./RunFunction.js";
6
+ export * from "./RunFunctionEvent.js";
7
+ export * from "./RunFunctionObserver.js";
8
+ export * from "./RunFunctionEventSource.js";
6
9
  export * from "./Vector.js";
package/run/index.js CHANGED
@@ -3,4 +3,7 @@ export * from "./DefaultRun.js";
3
3
  export * from "./IdMetadata.js";
4
4
  export * from "./Run.js";
5
5
  export * from "./RunFunction.js";
6
+ export * from "./RunFunctionEvent.js";
7
+ export * from "./RunFunctionObserver.js";
8
+ export * from "./RunFunctionEventSource.js";
6
9
  export * from "./Vector.js";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,22 @@
1
+ import { RunFunctionFinishedEventMetadata, RunFunctionStartedEventMetadata } from "../run/RunFunctionEvent.js";
2
+ import { Tool } from "./Tool.js";
3
+ export type ExecuteToolStartedEvent = {
4
+ type: "execute-tool-started";
5
+ metadata: RunFunctionStartedEventMetadata;
6
+ tool: Tool<string, unknown, unknown>;
7
+ input: unknown;
8
+ };
9
+ export type ExecuteToolFinishedEvent = {
10
+ type: "execute-tool-finished";
11
+ metadata: RunFunctionFinishedEventMetadata;
12
+ tool: Tool<string, unknown, unknown>;
13
+ input: unknown;
14
+ } & ({
15
+ status: "success";
16
+ output: unknown;
17
+ } | {
18
+ status: "failure";
19
+ error: unknown;
20
+ } | {
21
+ status: "abort";
22
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -21,6 +21,12 @@ class Tool {
21
21
  writable: true,
22
22
  value: void 0
23
23
  });
24
+ Object.defineProperty(this, "outputSchema", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
24
30
  Object.defineProperty(this, "execute", {
25
31
  enumerable: true,
26
32
  configurable: true,
@@ -30,6 +36,7 @@ class Tool {
30
36
  this.name = options.name;
31
37
  this.description = options.description;
32
38
  this.inputSchema = options.inputSchema;
39
+ this.outputSchema = options.outputSchema;
33
40
  this.execute = options.execute;
34
41
  }
35
42
  get inputSchemaDefinition() {
@@ -1,14 +1,17 @@
1
1
  import { z } from "zod";
2
- import { SchemaDefinition } from "../../model-function/generate-json/SchemaDefinition.js";
2
+ import { SchemaDefinition } from "../model-function/generate-json/SchemaDefinition.js";
3
+ import { RunFunction } from "../run/RunFunction.js";
3
4
  export declare class Tool<NAME extends string, INPUT, OUTPUT> {
4
5
  readonly name: NAME;
5
6
  readonly description: string;
6
7
  readonly inputSchema: z.ZodSchema<INPUT>;
7
- readonly execute: (input: INPUT) => PromiseLike<OUTPUT>;
8
+ readonly outputSchema?: z.ZodSchema<OUTPUT>;
9
+ readonly execute: RunFunction<INPUT, OUTPUT>;
8
10
  constructor(options: {
9
11
  name: NAME;
10
12
  description: string;
11
13
  inputSchema: z.ZodSchema<INPUT>;
14
+ outputSchema?: z.ZodSchema<OUTPUT>;
12
15
  execute(input: INPUT): Promise<OUTPUT>;
13
16
  });
14
17
  get inputSchemaDefinition(): SchemaDefinition<NAME, INPUT>;
@@ -18,6 +18,12 @@ export class Tool {
18
18
  writable: true,
19
19
  value: void 0
20
20
  });
21
+ Object.defineProperty(this, "outputSchema", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
21
27
  Object.defineProperty(this, "execute", {
22
28
  enumerable: true,
23
29
  configurable: true,
@@ -27,6 +33,7 @@ export class Tool {
27
33
  this.name = options.name;
28
34
  this.description = options.description;
29
35
  this.inputSchema = options.inputSchema;
36
+ this.outputSchema = options.outputSchema;
30
37
  this.execute = options.execute;
31
38
  }
32
39
  get inputSchemaDefinition() {
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ToolExecutionError = void 0;
4
+ class ToolExecutionError extends Error {
5
+ constructor({ message = "unknown error", toolName, input, cause, }) {
6
+ super(`Error executing tool ${toolName}: ${message}`);
7
+ Object.defineProperty(this, "toolName", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "input", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "cause", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.name = "ToolExecutionError";
26
+ this.toolName = toolName;
27
+ this.input = input;
28
+ this.cause = cause;
29
+ }
30
+ }
31
+ exports.ToolExecutionError = ToolExecutionError;
@@ -0,0 +1,11 @@
1
+ export declare class ToolExecutionError extends Error {
2
+ readonly toolName: string;
3
+ readonly input: unknown;
4
+ readonly cause: unknown;
5
+ constructor({ message, toolName, input, cause, }: {
6
+ toolName: string;
7
+ input: unknown;
8
+ message: string | undefined;
9
+ cause: unknown | undefined;
10
+ });
11
+ }
@@ -0,0 +1,27 @@
1
+ export class ToolExecutionError extends Error {
2
+ constructor({ message = "unknown error", toolName, input, cause, }) {
3
+ super(`Error executing tool ${toolName}: ${message}`);
4
+ Object.defineProperty(this, "toolName", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
10
+ Object.defineProperty(this, "input", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "cause", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ this.name = "ToolExecutionError";
23
+ this.toolName = toolName;
24
+ this.input = input;
25
+ this.cause = cause;
26
+ }
27
+ }
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executeTool = void 0;
4
+ const nanoid_1 = require("nanoid");
5
+ const RunFunctionEventSource_js_1 = require("../run/RunFunctionEventSource.cjs");
6
+ const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
7
+ const AbortError_js_1 = require("../util/api/AbortError.cjs");
8
+ const runSafe_js_1 = require("../util/runSafe.cjs");
9
+ const ToolExecutionError_js_1 = require("./ToolExecutionError.cjs");
10
+ async function executeTool(tool, input, options) {
11
+ const run = options?.run;
12
+ const eventSource = new RunFunctionEventSource_js_1.RunFunctionEventSource({
13
+ observers: run?.observers ?? [],
14
+ errorHandler: run?.errorHandler,
15
+ });
16
+ const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
17
+ const startMetadata = {
18
+ callId: `call-${(0, nanoid_1.nanoid)()}`,
19
+ runId: run?.runId,
20
+ sessionId: run?.sessionId,
21
+ userId: run?.userId,
22
+ functionId: options?.functionId,
23
+ startEpochSeconds: durationMeasurement.startEpochSeconds,
24
+ };
25
+ eventSource.notifyRunFunctionStarted({
26
+ type: "execute-tool-started",
27
+ metadata: startMetadata,
28
+ tool: tool,
29
+ input,
30
+ });
31
+ const result = await (0, runSafe_js_1.runSafe)(() => tool.execute(input, options));
32
+ const finishMetadata = {
33
+ ...startMetadata,
34
+ durationInMs: durationMeasurement.durationInMs,
35
+ };
36
+ if (!result.ok) {
37
+ if (result.isAborted) {
38
+ eventSource.notifyRunFunctionFinished({
39
+ type: "execute-tool-finished",
40
+ status: "abort",
41
+ metadata: finishMetadata,
42
+ tool: tool,
43
+ input,
44
+ });
45
+ throw new AbortError_js_1.AbortError();
46
+ }
47
+ eventSource.notifyRunFunctionFinished({
48
+ type: "execute-tool-finished",
49
+ status: "failure",
50
+ metadata: finishMetadata,
51
+ tool: tool,
52
+ input,
53
+ error: result.error,
54
+ });
55
+ throw new ToolExecutionError_js_1.ToolExecutionError({
56
+ toolName: tool.name,
57
+ input,
58
+ cause: result.error,
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ message: result.error?.message,
61
+ });
62
+ }
63
+ const output = result.output;
64
+ eventSource.notifyRunFunctionFinished({
65
+ type: "execute-tool-finished",
66
+ status: "success",
67
+ metadata: finishMetadata,
68
+ tool: tool,
69
+ input,
70
+ output,
71
+ });
72
+ return options?.fullResponse === true
73
+ ? {
74
+ output,
75
+ metadata: finishMetadata,
76
+ }
77
+ : output;
78
+ }
79
+ exports.executeTool = executeTool;
@@ -0,0 +1,20 @@
1
+ import { FunctionOptions } from "../model-function/FunctionOptions.js";
2
+ import { Tool } from "./Tool.js";
3
+ export type ExecuteToolMetadata = {
4
+ callId: string;
5
+ runId?: string;
6
+ sessionId?: string;
7
+ userId?: string;
8
+ functionId?: string;
9
+ startEpochSeconds: number;
10
+ durationInMs: number;
11
+ };
12
+ export declare function executeTool<INPUT, OUTPUT>(tool: Tool<string, INPUT, OUTPUT>, input: INPUT, options: FunctionOptions<undefined> & {
13
+ fullResponse: true;
14
+ }): Promise<{
15
+ output: OUTPUT;
16
+ metadata: ExecuteToolMetadata;
17
+ }>;
18
+ export declare function executeTool<INPUT, OUTPUT>(tool: Tool<string, INPUT, OUTPUT>, input: INPUT, options?: FunctionOptions<undefined> & {
19
+ fullResponse?: false;
20
+ }): Promise<OUTPUT>;
@@ -0,0 +1,75 @@
1
+ import { nanoid as createId } from "nanoid";
2
+ import { RunFunctionEventSource } from "../run/RunFunctionEventSource.js";
3
+ import { startDurationMeasurement } from "../util/DurationMeasurement.js";
4
+ import { AbortError } from "../util/api/AbortError.js";
5
+ import { runSafe } from "../util/runSafe.js";
6
+ import { ToolExecutionError } from "./ToolExecutionError.js";
7
+ export async function executeTool(tool, input, options) {
8
+ const run = options?.run;
9
+ const eventSource = new RunFunctionEventSource({
10
+ observers: run?.observers ?? [],
11
+ errorHandler: run?.errorHandler,
12
+ });
13
+ const durationMeasurement = startDurationMeasurement();
14
+ const startMetadata = {
15
+ callId: `call-${createId()}`,
16
+ runId: run?.runId,
17
+ sessionId: run?.sessionId,
18
+ userId: run?.userId,
19
+ functionId: options?.functionId,
20
+ startEpochSeconds: durationMeasurement.startEpochSeconds,
21
+ };
22
+ eventSource.notifyRunFunctionStarted({
23
+ type: "execute-tool-started",
24
+ metadata: startMetadata,
25
+ tool: tool,
26
+ input,
27
+ });
28
+ const result = await runSafe(() => tool.execute(input, options));
29
+ const finishMetadata = {
30
+ ...startMetadata,
31
+ durationInMs: durationMeasurement.durationInMs,
32
+ };
33
+ if (!result.ok) {
34
+ if (result.isAborted) {
35
+ eventSource.notifyRunFunctionFinished({
36
+ type: "execute-tool-finished",
37
+ status: "abort",
38
+ metadata: finishMetadata,
39
+ tool: tool,
40
+ input,
41
+ });
42
+ throw new AbortError();
43
+ }
44
+ eventSource.notifyRunFunctionFinished({
45
+ type: "execute-tool-finished",
46
+ status: "failure",
47
+ metadata: finishMetadata,
48
+ tool: tool,
49
+ input,
50
+ error: result.error,
51
+ });
52
+ throw new ToolExecutionError({
53
+ toolName: tool.name,
54
+ input,
55
+ cause: result.error,
56
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
+ message: result.error?.message,
58
+ });
59
+ }
60
+ const output = result.output;
61
+ eventSource.notifyRunFunctionFinished({
62
+ type: "execute-tool-finished",
63
+ status: "success",
64
+ metadata: finishMetadata,
65
+ tool: tool,
66
+ input,
67
+ output,
68
+ });
69
+ return options?.fullResponse === true
70
+ ? {
71
+ output,
72
+ metadata: finishMetadata,
73
+ }
74
+ : output;
75
+ }
package/tool/index.cjs ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./NoSuchToolError.cjs"), exports);
18
+ __exportStar(require("./Tool.cjs"), exports);
19
+ __exportStar(require("./ToolExecutionError.cjs"), exports);
20
+ __exportStar(require("./executeTool.cjs"), exports);
21
+ __exportStar(require("./useTool.cjs"), exports);
22
+ __exportStar(require("./useToolOrGenerateText.cjs"), exports);
@@ -0,0 +1,6 @@
1
+ export * from "./NoSuchToolError.js";
2
+ export * from "./Tool.js";
3
+ export * from "./ToolExecutionError.js";
4
+ export * from "./executeTool.js";
5
+ export * from "./useTool.js";
6
+ export * from "./useToolOrGenerateText.js";