modelfusion 0.71.0 → 0.73.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 (151) hide show
  1. package/README.md +57 -45
  2. package/core/FunctionEvent.d.ts +5 -5
  3. package/core/api/ApiCallError.cjs +2 -1
  4. package/core/api/ApiCallError.d.ts +3 -3
  5. package/core/api/ApiCallError.js +2 -1
  6. package/core/api/postToApi.cjs +10 -3
  7. package/core/api/postToApi.js +10 -3
  8. package/guard/guard.cjs +5 -4
  9. package/guard/guard.d.ts +1 -1
  10. package/guard/guard.js +5 -4
  11. package/model-function/ModelCallEvent.d.ts +2 -2
  12. package/model-function/generate-structure/StructureGenerationEvent.d.ts +2 -2
  13. package/model-function/generate-structure/StructureParseError.cjs +10 -0
  14. package/model-function/generate-structure/StructureParseError.d.ts +8 -0
  15. package/model-function/generate-structure/StructureParseError.js +10 -0
  16. package/model-function/generate-structure/index.cjs +0 -3
  17. package/model-function/generate-structure/index.d.ts +0 -3
  18. package/model-function/generate-structure/index.js +0 -3
  19. package/model-function/index.cjs +0 -1
  20. package/model-function/index.d.ts +0 -1
  21. package/model-function/index.js +0 -1
  22. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  23. package/model-provider/ollama/OllamaError.cjs +21 -7
  24. package/model-provider/ollama/OllamaError.d.ts +10 -3
  25. package/model-provider/ollama/OllamaError.js +21 -7
  26. package/model-provider/ollama/OllamaTextGenerationModel.cjs +2 -2
  27. package/model-provider/ollama/OllamaTextGenerationModel.d.ts +5 -5
  28. package/model-provider/ollama/OllamaTextGenerationModel.js +2 -2
  29. package/model-provider/openai/chat/OpenAIChatMessage.cjs +16 -25
  30. package/model-provider/openai/chat/OpenAIChatMessage.d.ts +10 -28
  31. package/model-provider/openai/chat/OpenAIChatMessage.js +16 -25
  32. package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -48
  33. package/model-provider/openai/chat/OpenAIChatModel.d.ts +15 -107
  34. package/model-provider/openai/chat/OpenAIChatModel.js +2 -48
  35. package/package.json +1 -1
  36. package/tool/Tool.cjs +5 -16
  37. package/tool/Tool.d.ts +9 -14
  38. package/tool/Tool.js +5 -16
  39. package/tool/ToolCall.d.ts +15 -0
  40. package/tool/ToolCallArgumentsValidationError.cjs +49 -0
  41. package/tool/ToolCallArgumentsValidationError.d.ts +23 -0
  42. package/tool/ToolCallArgumentsValidationError.js +45 -0
  43. package/tool/ToolCallError.cjs +34 -0
  44. package/tool/ToolCallError.d.ts +17 -0
  45. package/tool/ToolCallError.js +30 -0
  46. package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.cjs +4 -4
  47. package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.d.ts +1 -1
  48. package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.js +2 -2
  49. package/{model-function/generate-tool-call/ToolCallParametersValidationError.cjs → tool/ToolCallParseError.cjs} +13 -13
  50. package/{model-function/generate-tool-call/ToolCallParametersValidationError.d.ts → tool/ToolCallParseError.d.ts} +6 -6
  51. package/{model-function/generate-tool-call/ToolCallParametersValidationError.js → tool/ToolCallParseError.js} +11 -11
  52. package/tool/ToolCallResult.d.ts +13 -0
  53. package/{model-function/generate-tool-call → tool}/ToolDefinition.d.ts +2 -2
  54. package/tool/ToolExecutionError.cjs +5 -4
  55. package/tool/ToolExecutionError.d.ts +4 -4
  56. package/tool/ToolExecutionError.js +5 -4
  57. package/tool/WebSearchTool.cjs +4 -4
  58. package/tool/WebSearchTool.d.ts +2 -2
  59. package/tool/WebSearchTool.js +4 -4
  60. package/tool/{ExecuteToolEvent.d.ts → execute-tool/ExecuteToolEvent.d.ts} +1 -1
  61. package/tool/{executeTool.cjs → execute-tool/executeTool.cjs} +15 -15
  62. package/tool/{executeTool.d.ts → execute-tool/executeTool.d.ts} +6 -6
  63. package/tool/{executeTool.js → execute-tool/executeTool.js} +15 -15
  64. package/tool/execute-tool/index.cjs +18 -0
  65. package/tool/execute-tool/index.d.ts +2 -0
  66. package/tool/execute-tool/index.js +2 -0
  67. package/tool/execute-tool/safeExecuteToolCall.cjs +34 -0
  68. package/tool/execute-tool/safeExecuteToolCall.d.ts +5 -0
  69. package/tool/execute-tool/safeExecuteToolCall.js +30 -0
  70. package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +59 -0
  71. package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +35 -0
  72. package/tool/generate-tool-call/TextGenerationToolCallModel.js +55 -0
  73. package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.d.ts +1 -1
  74. package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.d.ts +3 -3
  75. package/{model-function → tool}/generate-tool-call/generateToolCall.cjs +11 -11
  76. package/{model-function → tool}/generate-tool-call/generateToolCall.d.ts +3 -3
  77. package/{model-function → tool}/generate-tool-call/generateToolCall.js +11 -11
  78. package/tool/generate-tool-call/index.cjs +20 -0
  79. package/tool/generate-tool-call/index.d.ts +4 -0
  80. package/tool/generate-tool-call/index.js +4 -0
  81. package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.d.ts +1 -1
  82. package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.d.ts +3 -3
  83. package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.cjs +8 -8
  84. package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.d.ts +3 -3
  85. package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.js +8 -8
  86. package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/index.cjs +0 -7
  87. package/tool/generate-tool-calls-or-text/index.d.ts +3 -0
  88. package/tool/generate-tool-calls-or-text/index.js +3 -0
  89. package/tool/index.cjs +12 -7
  90. package/tool/index.d.ts +12 -7
  91. package/tool/index.js +12 -7
  92. package/tool/{UseToolEvent.d.ts → use-tool/UseToolEvent.d.ts} +1 -1
  93. package/tool/use-tool/index.cjs +18 -0
  94. package/tool/use-tool/index.d.ts +2 -0
  95. package/tool/use-tool/index.js +2 -0
  96. package/tool/use-tool/useTool.cjs +30 -0
  97. package/tool/use-tool/useTool.d.ts +16 -0
  98. package/tool/use-tool/useTool.js +26 -0
  99. package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.d.ts +7 -0
  100. package/tool/use-tools-or-generate-text/index.cjs +18 -0
  101. package/tool/use-tools-or-generate-text/index.d.ts +2 -0
  102. package/tool/use-tools-or-generate-text/index.js +2 -0
  103. package/tool/use-tools-or-generate-text/useToolsOrGenerateText.cjs +48 -0
  104. package/tool/use-tools-or-generate-text/useToolsOrGenerateText.d.ts +18 -0
  105. package/tool/use-tools-or-generate-text/useToolsOrGenerateText.js +44 -0
  106. package/util/getErrorMessage.cjs +3 -0
  107. package/util/getErrorMessage.d.ts +1 -1
  108. package/util/getErrorMessage.js +3 -0
  109. package/model-function/generate-structure/NoSuchStructureError.cjs +0 -17
  110. package/model-function/generate-structure/NoSuchStructureError.d.ts +0 -4
  111. package/model-function/generate-structure/NoSuchStructureError.js +0 -13
  112. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +0 -26
  113. package/model-function/generate-structure/generateStructureOrText.cjs +0 -54
  114. package/model-function/generate-structure/generateStructureOrText.d.ts +0 -97
  115. package/model-function/generate-structure/generateStructureOrText.js +0 -50
  116. package/model-function/generate-tool-call/ToolCall.d.ts +0 -5
  117. package/model-function/generate-tool-call/index.d.ts +0 -10
  118. package/model-function/generate-tool-call/index.js +0 -10
  119. package/tool/NoSuchToolError.cjs +0 -25
  120. package/tool/NoSuchToolError.d.ts +0 -10
  121. package/tool/NoSuchToolError.js +0 -21
  122. package/tool/UseToolOrGenerateTextEvent.d.ts +0 -7
  123. package/tool/useTool.cjs +0 -40
  124. package/tool/useTool.d.ts +0 -15
  125. package/tool/useTool.js +0 -36
  126. package/tool/useToolOrGenerateText.cjs +0 -50
  127. package/tool/useToolOrGenerateText.d.ts +0 -23
  128. package/tool/useToolOrGenerateText.js +0 -46
  129. /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.cjs +0 -0
  130. /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.d.ts +0 -0
  131. /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.js +0 -0
  132. /package/{model-function/generate-tool-call → tool}/ToolCall.cjs +0 -0
  133. /package/{model-function/generate-tool-call → tool}/ToolCall.js +0 -0
  134. /package/{model-function/generate-structure/StructureOrTextGenerationModel.cjs → tool/ToolCallResult.cjs} +0 -0
  135. /package/{model-function/generate-structure/StructureOrTextGenerationModel.js → tool/ToolCallResult.js} +0 -0
  136. /package/{model-function/generate-tool-call → tool}/ToolDefinition.cjs +0 -0
  137. /package/{model-function/generate-tool-call → tool}/ToolDefinition.js +0 -0
  138. /package/tool/{ExecuteToolEvent.cjs → execute-tool/ExecuteToolEvent.cjs} +0 -0
  139. /package/tool/{ExecuteToolEvent.js → execute-tool/ExecuteToolEvent.js} +0 -0
  140. /package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.cjs +0 -0
  141. /package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.js +0 -0
  142. /package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.cjs +0 -0
  143. /package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.js +0 -0
  144. /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.cjs +0 -0
  145. /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.js +0 -0
  146. /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.cjs +0 -0
  147. /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.js +0 -0
  148. /package/tool/{UseToolEvent.cjs → use-tool/UseToolEvent.cjs} +0 -0
  149. /package/tool/{UseToolEvent.js → use-tool/UseToolEvent.js} +0 -0
  150. /package/tool/{UseToolOrGenerateTextEvent.cjs → use-tools-or-generate-text/UseToolsOrGenerateTextEvent.cjs} +0 -0
  151. /package/tool/{UseToolOrGenerateTextEvent.js → use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js} +0 -0
@@ -1,7 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { ZodSchema } from "../core/schema/ZodSchema.js";
3
3
  import { Tool } from "./Tool.js";
4
- const OUTPUT_SCHEMA = new ZodSchema(z.object({
4
+ const RETURN_TYPE_SCHEMA = new ZodSchema(z.object({
5
5
  results: z.array(z.object({
6
6
  title: z.string(),
7
7
  link: z.string().url(),
@@ -9,7 +9,7 @@ const OUTPUT_SCHEMA = new ZodSchema(z.object({
9
9
  })),
10
10
  }));
11
11
  // expose the schemas to library consumers:
12
- const createInputSchema = (description) =>
12
+ const createParameters = (description) =>
13
13
  // same structure, but with description:
14
14
  new ZodSchema(z.object({
15
15
  query: z.string().describe(description),
@@ -44,8 +44,8 @@ export class WebSearchTool extends Tool {
44
44
  super({
45
45
  name,
46
46
  description,
47
- inputSchema: createInputSchema(queryDescription),
48
- outputSchema: OUTPUT_SCHEMA,
47
+ parameters: createParameters(queryDescription),
48
+ returnType: RETURN_TYPE_SCHEMA,
49
49
  execute,
50
50
  });
51
51
  }
@@ -1,4 +1,4 @@
1
- import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
1
+ import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../../core/FunctionEvent.js";
2
2
  export interface ExecuteToolStartedEvent extends BaseFunctionStartedEvent {
3
3
  functionType: "execute-tool";
4
4
  toolName: string;
@@ -2,23 +2,23 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.executeTool = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
- const FunctionEventSource_js_1 = require("../core/FunctionEventSource.cjs");
6
- const GlobalFunctionLogging_js_1 = require("../core/GlobalFunctionLogging.cjs");
7
- const GlobalFunctionObservers_js_1 = require("../core/GlobalFunctionObservers.cjs");
8
- const AbortError_js_1 = require("../core/api/AbortError.cjs");
9
- const getFunctionCallLogger_js_1 = require("../core/getFunctionCallLogger.cjs");
10
- const getRun_js_1 = require("../core/getRun.cjs");
11
- const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
12
- const runSafe_js_1 = require("../util/runSafe.cjs");
13
- const ToolExecutionError_js_1 = require("./ToolExecutionError.cjs");
5
+ const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
6
+ const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs");
7
+ const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
8
+ const AbortError_js_1 = require("../../core/api/AbortError.cjs");
9
+ const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
10
+ const getRun_js_1 = require("../../core/getRun.cjs");
11
+ const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
12
+ const runSafe_js_1 = require("../../util/runSafe.cjs");
13
+ const ToolExecutionError_js_1 = require("../ToolExecutionError.cjs");
14
14
  async function executeTool(// eslint-disable-line @typescript-eslint/no-explicit-any
15
- tool, input, options) {
16
- const fullResponse = await doExecuteTool(tool, input, options);
15
+ tool, args, options) {
16
+ const fullResponse = await doExecuteTool(tool, args, options);
17
17
  return options?.returnType === "full" ? fullResponse : fullResponse.output;
18
18
  }
19
19
  exports.executeTool = executeTool;
20
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
- async function doExecuteTool(tool, input, options) {
21
+ async function doExecuteTool(tool, args, options) {
22
22
  const run = await (0, getRun_js_1.getRun)(options?.run);
23
23
  const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
24
24
  observers: [
@@ -39,7 +39,7 @@ async function doExecuteTool(tool, input, options) {
39
39
  userId: run?.userId,
40
40
  functionId: options?.functionId,
41
41
  toolName: tool.name,
42
- input,
42
+ input: args,
43
43
  };
44
44
  eventSource.notify({
45
45
  ...metadata,
@@ -47,7 +47,7 @@ async function doExecuteTool(tool, input, options) {
47
47
  timestamp: durationMeasurement.startDate,
48
48
  startTimestamp: durationMeasurement.startDate,
49
49
  });
50
- const result = await (0, runSafe_js_1.runSafe)(() => tool.execute(input, {
50
+ const result = await (0, runSafe_js_1.runSafe)(() => tool.execute(args, {
51
51
  functionId: options?.functionId,
52
52
  logging: options?.logging,
53
53
  observers: options?.observers,
@@ -81,7 +81,7 @@ async function doExecuteTool(tool, input, options) {
81
81
  });
82
82
  throw new ToolExecutionError_js_1.ToolExecutionError({
83
83
  toolName: tool.name,
84
- input,
84
+ input: args,
85
85
  cause: result.error,
86
86
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
87
  message: result.error?.message,
@@ -1,5 +1,5 @@
1
- import { FunctionOptions } from "../core/FunctionOptions.js";
2
- import { Tool } from "./Tool.js";
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { Tool } from "../Tool.js";
3
3
  export type ExecuteToolMetadata = {
4
4
  callId: string;
5
5
  runId?: string;
@@ -11,16 +11,16 @@ export type ExecuteToolMetadata = {
11
11
  durationInMs: number;
12
12
  };
13
13
  /**
14
- * `executeTool` directly executes a tool with the given parameters.
14
+ * `executeTool` executes a tool with the given parameters.
15
15
  */
16
16
  export declare function executeTool<TOOL extends Tool<any, any, any>>(// eslint-disable-line @typescript-eslint/no-explicit-any
17
- tool: TOOL, input: TOOL["inputSchema"]["_type"], options?: FunctionOptions & {
17
+ tool: TOOL, args: TOOL["parameters"]["_type"], options?: FunctionOptions & {
18
18
  returnType?: "output";
19
19
  }): Promise<ReturnType<TOOL["execute"]>>;
20
20
  export declare function executeTool<TOOL extends Tool<any, any, any>>(// eslint-disable-line @typescript-eslint/no-explicit-any
21
- tool: TOOL, input: TOOL["inputSchema"]["_type"], options: FunctionOptions & {
21
+ tool: TOOL, args: TOOL["parameters"]["_type"], options: FunctionOptions & {
22
22
  returnType: "full";
23
23
  }): Promise<{
24
- output: ReturnType<TOOL["execute"]>;
24
+ output: Awaited<ReturnType<TOOL["execute"]>>;
25
25
  metadata: ExecuteToolMetadata;
26
26
  }>;
@@ -1,20 +1,20 @@
1
1
  import { nanoid as createId } from "nanoid";
2
- import { FunctionEventSource } from "../core/FunctionEventSource.js";
3
- import { getGlobalFunctionLogging } from "../core/GlobalFunctionLogging.js";
4
- import { getGlobalFunctionObservers } from "../core/GlobalFunctionObservers.js";
5
- import { AbortError } from "../core/api/AbortError.js";
6
- import { getFunctionCallLogger } from "../core/getFunctionCallLogger.js";
7
- import { getRun } from "../core/getRun.js";
8
- import { startDurationMeasurement } from "../util/DurationMeasurement.js";
9
- import { runSafe } from "../util/runSafe.js";
10
- import { ToolExecutionError } from "./ToolExecutionError.js";
2
+ import { FunctionEventSource } from "../../core/FunctionEventSource.js";
3
+ import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
4
+ import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
5
+ import { AbortError } from "../../core/api/AbortError.js";
6
+ import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
7
+ import { getRun } from "../../core/getRun.js";
8
+ import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
9
+ import { runSafe } from "../../util/runSafe.js";
10
+ import { ToolExecutionError } from "../ToolExecutionError.js";
11
11
  export async function executeTool(// eslint-disable-line @typescript-eslint/no-explicit-any
12
- tool, input, options) {
13
- const fullResponse = await doExecuteTool(tool, input, options);
12
+ tool, args, options) {
13
+ const fullResponse = await doExecuteTool(tool, args, options);
14
14
  return options?.returnType === "full" ? fullResponse : fullResponse.output;
15
15
  }
16
16
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- async function doExecuteTool(tool, input, options) {
17
+ async function doExecuteTool(tool, args, options) {
18
18
  const run = await getRun(options?.run);
19
19
  const eventSource = new FunctionEventSource({
20
20
  observers: [
@@ -35,7 +35,7 @@ async function doExecuteTool(tool, input, options) {
35
35
  userId: run?.userId,
36
36
  functionId: options?.functionId,
37
37
  toolName: tool.name,
38
- input,
38
+ input: args,
39
39
  };
40
40
  eventSource.notify({
41
41
  ...metadata,
@@ -43,7 +43,7 @@ async function doExecuteTool(tool, input, options) {
43
43
  timestamp: durationMeasurement.startDate,
44
44
  startTimestamp: durationMeasurement.startDate,
45
45
  });
46
- const result = await runSafe(() => tool.execute(input, {
46
+ const result = await runSafe(() => tool.execute(args, {
47
47
  functionId: options?.functionId,
48
48
  logging: options?.logging,
49
49
  observers: options?.observers,
@@ -77,7 +77,7 @@ async function doExecuteTool(tool, input, options) {
77
77
  });
78
78
  throw new ToolExecutionError({
79
79
  toolName: tool.name,
80
- input,
80
+ input: args,
81
81
  cause: result.error,
82
82
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
83
  message: result.error?.message,
@@ -0,0 +1,18 @@
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("./ExecuteToolEvent.cjs"), exports);
18
+ __exportStar(require("./executeTool.cjs"), exports);
@@ -0,0 +1,2 @@
1
+ export * from "./ExecuteToolEvent.js";
2
+ export * from "./executeTool.js";
@@ -0,0 +1,2 @@
1
+ export * from "./ExecuteToolEvent.js";
2
+ export * from "./executeTool.js";
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.safeExecuteToolCall = void 0;
4
+ const ToolCallError_js_1 = require("../ToolCallError.cjs");
5
+ const ToolExecutionError_js_1 = require("../ToolExecutionError.cjs");
6
+ const executeTool_js_1 = require("./executeTool.cjs");
7
+ async function safeExecuteToolCall(tool, toolCall, options) {
8
+ try {
9
+ return {
10
+ tool: toolCall.name,
11
+ toolCall,
12
+ args: toolCall.args,
13
+ ok: true,
14
+ result: await (0, executeTool_js_1.executeTool)(tool, toolCall.args, options),
15
+ };
16
+ }
17
+ catch (error) {
18
+ // If the error is an AbortError, rethrow it.
19
+ if (error instanceof Error && error.name === "AbortError") {
20
+ throw error;
21
+ }
22
+ return {
23
+ tool: toolCall.name,
24
+ toolCall,
25
+ args: toolCall.args,
26
+ ok: false,
27
+ result: new ToolCallError_js_1.ToolCallError({
28
+ toolCall,
29
+ cause: error instanceof ToolExecutionError_js_1.ToolExecutionError ? error.cause : error,
30
+ }),
31
+ };
32
+ }
33
+ }
34
+ exports.safeExecuteToolCall = safeExecuteToolCall;
@@ -0,0 +1,5 @@
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { Tool } from "../Tool.js";
3
+ import { ToolCall } from "../ToolCall.js";
4
+ import { ToolCallResult } from "../ToolCallResult.js";
5
+ export declare function safeExecuteToolCall<TOOL extends Tool<string, unknown, any>>(tool: TOOL, toolCall: ToolCall<TOOL["name"], TOOL["parameters"]>, options?: FunctionOptions): Promise<ToolCallResult<TOOL["name"], TOOL["parameters"], Awaited<ReturnType<TOOL["execute"]>>>>;
@@ -0,0 +1,30 @@
1
+ import { ToolCallError } from "../ToolCallError.js";
2
+ import { ToolExecutionError } from "../ToolExecutionError.js";
3
+ import { executeTool } from "./executeTool.js";
4
+ export async function safeExecuteToolCall(tool, toolCall, options) {
5
+ try {
6
+ return {
7
+ tool: toolCall.name,
8
+ toolCall,
9
+ args: toolCall.args,
10
+ ok: true,
11
+ result: await executeTool(tool, toolCall.args, options),
12
+ };
13
+ }
14
+ catch (error) {
15
+ // If the error is an AbortError, rethrow it.
16
+ if (error instanceof Error && error.name === "AbortError") {
17
+ throw error;
18
+ }
19
+ return {
20
+ tool: toolCall.name,
21
+ toolCall,
22
+ args: toolCall.args,
23
+ ok: false,
24
+ result: new ToolCallError({
25
+ toolCall,
26
+ cause: error instanceof ToolExecutionError ? error.cause : error,
27
+ }),
28
+ };
29
+ }
30
+ }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TextGenerationToolCallModel = void 0;
4
+ const generateText_js_1 = require("../../model-function/generate-text/generateText.cjs");
5
+ const ToolCallParseError_js_1 = require("../ToolCallParseError.cjs");
6
+ class TextGenerationToolCallModel {
7
+ constructor({ model, format, }) {
8
+ Object.defineProperty(this, "model", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: void 0
13
+ });
14
+ Object.defineProperty(this, "format", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ this.model = model;
21
+ this.format = format;
22
+ }
23
+ get modelInformation() {
24
+ return this.model.modelInformation;
25
+ }
26
+ get settings() {
27
+ return this.model.settings;
28
+ }
29
+ get settingsForEvent() {
30
+ return this.model.settingsForEvent;
31
+ }
32
+ async doGenerateToolCall(tool, prompt, options) {
33
+ const { response, value, metadata } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, tool), {
34
+ ...options,
35
+ returnType: "full",
36
+ });
37
+ try {
38
+ return {
39
+ response,
40
+ toolCall: this.format.extractToolCall(value),
41
+ usage: metadata?.usage,
42
+ };
43
+ }
44
+ catch (error) {
45
+ throw new ToolCallParseError_js_1.ToolCallParseError({
46
+ toolName: tool.name,
47
+ valueText: value,
48
+ cause: error,
49
+ });
50
+ }
51
+ }
52
+ withSettings(additionalSettings) {
53
+ return new TextGenerationToolCallModel({
54
+ model: this.model.withSettings(additionalSettings),
55
+ format: this.format,
56
+ });
57
+ }
58
+ }
59
+ exports.TextGenerationToolCallModel = TextGenerationToolCallModel;
@@ -0,0 +1,35 @@
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
3
+ import { ToolDefinition } from "../ToolDefinition.js";
4
+ import { ToolCallGenerationModel } from "./ToolCallGenerationModel.js";
5
+ export interface ToolCallTextPromptFormat<PROMPT> {
6
+ createPrompt: (prompt: PROMPT, tool: ToolDefinition<string, unknown>) => string;
7
+ extractToolCall: (response: string) => {
8
+ id: string;
9
+ args: unknown;
10
+ } | null;
11
+ }
12
+ export declare class TextGenerationToolCallModel<PROMPT, MODEL extends TextGenerationModel<string, TextGenerationModelSettings>> implements ToolCallGenerationModel<PROMPT, MODEL["settings"]> {
13
+ private readonly model;
14
+ private readonly format;
15
+ constructor({ model, format, }: {
16
+ model: MODEL;
17
+ format: ToolCallTextPromptFormat<PROMPT>;
18
+ });
19
+ get modelInformation(): import("../../index.js").ModelInformation;
20
+ get settings(): TextGenerationModelSettings;
21
+ get settingsForEvent(): Partial<MODEL["settings"]>;
22
+ doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): Promise<{
23
+ response: unknown;
24
+ toolCall: {
25
+ id: string;
26
+ args: unknown;
27
+ } | null;
28
+ usage: {
29
+ promptTokens: number;
30
+ completionTokens: number;
31
+ totalTokens: number;
32
+ } | undefined;
33
+ }>;
34
+ withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
35
+ }
@@ -0,0 +1,55 @@
1
+ import { generateText } from "../../model-function/generate-text/generateText.js";
2
+ import { ToolCallParseError } from "../ToolCallParseError.js";
3
+ export class TextGenerationToolCallModel {
4
+ constructor({ model, format, }) {
5
+ Object.defineProperty(this, "model", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: void 0
10
+ });
11
+ Object.defineProperty(this, "format", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ this.model = model;
18
+ this.format = format;
19
+ }
20
+ get modelInformation() {
21
+ return this.model.modelInformation;
22
+ }
23
+ get settings() {
24
+ return this.model.settings;
25
+ }
26
+ get settingsForEvent() {
27
+ return this.model.settingsForEvent;
28
+ }
29
+ async doGenerateToolCall(tool, prompt, options) {
30
+ const { response, value, metadata } = await generateText(this.model, this.format.createPrompt(prompt, tool), {
31
+ ...options,
32
+ returnType: "full",
33
+ });
34
+ try {
35
+ return {
36
+ response,
37
+ toolCall: this.format.extractToolCall(value),
38
+ usage: metadata?.usage,
39
+ };
40
+ }
41
+ catch (error) {
42
+ throw new ToolCallParseError({
43
+ toolName: tool.name,
44
+ valueText: value,
45
+ cause: error,
46
+ });
47
+ }
48
+ }
49
+ withSettings(additionalSettings) {
50
+ return new TextGenerationToolCallModel({
51
+ model: this.model.withSettings(additionalSettings),
52
+ format: this.format,
53
+ });
54
+ }
55
+ }
@@ -1,4 +1,4 @@
1
- import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
1
+ import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../../model-function/ModelCallEvent.js";
2
2
  export interface ToolCallGenerationStartedEvent extends BaseModelCallStartedEvent {
3
3
  functionType: "generate-tool-call";
4
4
  }
@@ -1,6 +1,6 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { Model, ModelSettings } from "../Model.js";
3
- import { ToolDefinition } from "./ToolDefinition.js";
2
+ import { Model, ModelSettings } from "../../model-function/Model.js";
3
+ import { ToolDefinition } from "../ToolDefinition.js";
4
4
  export interface ToolCallGenerationModelSettings extends ModelSettings {
5
5
  }
6
6
  export interface ToolCallGenerationModel<PROMPT, SETTINGS extends ToolCallGenerationModelSettings = ToolCallGenerationModelSettings> extends Model<SETTINGS> {
@@ -8,7 +8,7 @@ export interface ToolCallGenerationModel<PROMPT, SETTINGS extends ToolCallGenera
8
8
  response: unknown;
9
9
  toolCall: {
10
10
  id: string;
11
- parameters: unknown;
11
+ args: unknown;
12
12
  } | null;
13
13
  usage?: {
14
14
  promptTokens: number;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateToolCall = void 0;
4
- const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
5
- const ToolCallParametersValidationError_js_1 = require("./ToolCallParametersValidationError.cjs");
6
- const ToolCallGenerationError_js_1 = require("./ToolCallGenerationError.cjs");
4
+ const executeStandardCall_js_1 = require("../../model-function/executeStandardCall.cjs");
5
+ const ToolCallArgumentsValidationError_js_1 = require("../ToolCallArgumentsValidationError.cjs");
6
+ const ToolCallGenerationError_js_1 = require("../ToolCallGenerationError.cjs");
7
7
  async function generateToolCall(model, tool, prompt, options) {
8
8
  // Note: PROMPT must not be a function.
9
9
  const expandedPrompt = typeof prompt === "function"
@@ -19,16 +19,16 @@ async function generateToolCall(model, tool, prompt, options) {
19
19
  const result = await model.doGenerateToolCall(tool, expandedPrompt, options);
20
20
  const toolCall = result.toolCall;
21
21
  if (toolCall === null) {
22
- throw new ToolCallGenerationError_js_1.ToolCallsGenerationError({
22
+ throw new ToolCallGenerationError_js_1.ToolCallGenerationError({
23
23
  toolName: tool.name,
24
24
  cause: "No tool call generated.",
25
25
  });
26
26
  }
27
- const parseResult = tool.parameters.validate(toolCall.parameters);
27
+ const parseResult = tool.parameters.validate(toolCall.args);
28
28
  if (!parseResult.success) {
29
- throw new ToolCallParametersValidationError_js_1.ToolCallParametersValidationError({
29
+ throw new ToolCallArgumentsValidationError_js_1.ToolCallArgumentsValidationError({
30
30
  toolName: tool.name,
31
- parameters: toolCall.parameters,
31
+ args: toolCall.args,
32
32
  cause: parseResult.error,
33
33
  });
34
34
  }
@@ -37,17 +37,17 @@ async function generateToolCall(model, tool, prompt, options) {
37
37
  extractedValue: {
38
38
  id: toolCall.id,
39
39
  name: tool.name,
40
- parameters: parseResult.data,
40
+ args: parseResult.data,
41
41
  },
42
42
  usage: result.usage,
43
43
  };
44
44
  }
45
45
  catch (error) {
46
- if (error instanceof ToolCallParametersValidationError_js_1.ToolCallParametersValidationError ||
47
- error instanceof ToolCallGenerationError_js_1.ToolCallsGenerationError) {
46
+ if (error instanceof ToolCallArgumentsValidationError_js_1.ToolCallArgumentsValidationError ||
47
+ error instanceof ToolCallGenerationError_js_1.ToolCallGenerationError) {
48
48
  throw error;
49
49
  }
50
- throw new ToolCallGenerationError_js_1.ToolCallsGenerationError({
50
+ throw new ToolCallGenerationError_js_1.ToolCallGenerationError({
51
51
  toolName: tool.name,
52
52
  cause: error,
53
53
  });
@@ -1,8 +1,8 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { ModelCallMetadata } from "../ModelCallMetadata.js";
3
- import { ToolDefinition } from "./ToolDefinition.js";
2
+ import { ModelCallMetadata } from "../../model-function/ModelCallMetadata.js";
3
+ import { ToolCall } from "../ToolCall.js";
4
+ import { ToolDefinition } from "../ToolDefinition.js";
4
5
  import { ToolCallGenerationModel, ToolCallGenerationModelSettings } from "./ToolCallGenerationModel.js";
5
- import { ToolCall } from "./ToolCall.js";
6
6
  export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(model: ToolCallGenerationModel<PROMPT, SETTINGS>, tool: ToolDefinition<NAME, PARAMETERS>, prompt: PROMPT | ((tool: ToolDefinition<NAME, PARAMETERS>) => PROMPT), options?: FunctionOptions & {
7
7
  returnType?: "structure";
8
8
  }): Promise<ToolCall<NAME, PARAMETERS>>;
@@ -1,6 +1,6 @@
1
- import { executeStandardCall } from "../executeStandardCall.js";
2
- import { ToolCallParametersValidationError } from "./ToolCallParametersValidationError.js";
3
- import { ToolCallsGenerationError } from "./ToolCallGenerationError.js";
1
+ import { executeStandardCall } from "../../model-function/executeStandardCall.js";
2
+ import { ToolCallArgumentsValidationError } from "../ToolCallArgumentsValidationError.js";
3
+ import { ToolCallGenerationError } from "../ToolCallGenerationError.js";
4
4
  export async function generateToolCall(model, tool, prompt, options) {
5
5
  // Note: PROMPT must not be a function.
6
6
  const expandedPrompt = typeof prompt === "function"
@@ -16,16 +16,16 @@ export async function generateToolCall(model, tool, prompt, options) {
16
16
  const result = await model.doGenerateToolCall(tool, expandedPrompt, options);
17
17
  const toolCall = result.toolCall;
18
18
  if (toolCall === null) {
19
- throw new ToolCallsGenerationError({
19
+ throw new ToolCallGenerationError({
20
20
  toolName: tool.name,
21
21
  cause: "No tool call generated.",
22
22
  });
23
23
  }
24
- const parseResult = tool.parameters.validate(toolCall.parameters);
24
+ const parseResult = tool.parameters.validate(toolCall.args);
25
25
  if (!parseResult.success) {
26
- throw new ToolCallParametersValidationError({
26
+ throw new ToolCallArgumentsValidationError({
27
27
  toolName: tool.name,
28
- parameters: toolCall.parameters,
28
+ args: toolCall.args,
29
29
  cause: parseResult.error,
30
30
  });
31
31
  }
@@ -34,17 +34,17 @@ export async function generateToolCall(model, tool, prompt, options) {
34
34
  extractedValue: {
35
35
  id: toolCall.id,
36
36
  name: tool.name,
37
- parameters: parseResult.data,
37
+ args: parseResult.data,
38
38
  },
39
39
  usage: result.usage,
40
40
  };
41
41
  }
42
42
  catch (error) {
43
- if (error instanceof ToolCallParametersValidationError ||
44
- error instanceof ToolCallsGenerationError) {
43
+ if (error instanceof ToolCallArgumentsValidationError ||
44
+ error instanceof ToolCallGenerationError) {
45
45
  throw error;
46
46
  }
47
- throw new ToolCallsGenerationError({
47
+ throw new ToolCallGenerationError({
48
48
  toolName: tool.name,
49
49
  cause: error,
50
50
  });
@@ -0,0 +1,20 @@
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("./TextGenerationToolCallModel.cjs"), exports);
18
+ __exportStar(require("./ToolCallGenerationEvent.cjs"), exports);
19
+ __exportStar(require("./ToolCallGenerationModel.cjs"), exports);
20
+ __exportStar(require("./generateToolCall.cjs"), exports);
@@ -0,0 +1,4 @@
1
+ export * from "./TextGenerationToolCallModel.js";
2
+ export * from "./ToolCallGenerationEvent.js";
3
+ export * from "./ToolCallGenerationModel.js";
4
+ export * from "./generateToolCall.js";
@@ -0,0 +1,4 @@
1
+ export * from "./TextGenerationToolCallModel.js";
2
+ export * from "./ToolCallGenerationEvent.js";
3
+ export * from "./ToolCallGenerationModel.js";
4
+ export * from "./generateToolCall.js";
@@ -1,4 +1,4 @@
1
- import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
1
+ import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../../model-function/ModelCallEvent.js";
2
2
  export interface ToolCallsOrTextGenerationStartedEvent extends BaseModelCallStartedEvent {
3
3
  functionType: "generate-tool-calls-or-text";
4
4
  }