modelfusion 0.68.1 → 0.70.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 (95) hide show
  1. package/README.md +22 -9
  2. package/core/schema/JSONParseError.cjs +1 -1
  3. package/core/schema/JSONParseError.d.ts +1 -1
  4. package/core/schema/JSONParseError.js +1 -1
  5. package/model-function/ModelCallEvent.d.ts +3 -2
  6. package/model-function/generate-structure/StructureValidationError.cjs +11 -0
  7. package/model-function/generate-structure/StructureValidationError.d.ts +9 -0
  8. package/model-function/generate-structure/StructureValidationError.js +11 -0
  9. package/model-function/generate-text/index.cjs +1 -8
  10. package/model-function/generate-text/index.d.ts +1 -8
  11. package/model-function/generate-text/index.js +1 -8
  12. package/model-function/generate-text/prompt-format/AlpacaPromptFormat.cjs +31 -3
  13. package/model-function/generate-text/prompt-format/AlpacaPromptFormat.d.ts +29 -1
  14. package/model-function/generate-text/prompt-format/AlpacaPromptFormat.js +29 -1
  15. package/model-function/generate-text/prompt-format/ChatMLPromptFormat.cjs +79 -0
  16. package/model-function/generate-text/prompt-format/ChatMLPromptFormat.d.ts +31 -0
  17. package/model-function/generate-text/prompt-format/ChatMLPromptFormat.js +74 -0
  18. package/model-function/generate-text/prompt-format/ChatPrompt.d.ts +28 -23
  19. package/model-function/generate-text/prompt-format/ChatPromptValidationError.cjs +17 -0
  20. package/model-function/generate-text/prompt-format/ChatPromptValidationError.d.ts +8 -0
  21. package/model-function/generate-text/prompt-format/ChatPromptValidationError.js +13 -0
  22. package/model-function/generate-text/prompt-format/Llama2PromptFormat.cjs +41 -27
  23. package/model-function/generate-text/prompt-format/Llama2PromptFormat.d.ts +20 -2
  24. package/model-function/generate-text/prompt-format/Llama2PromptFormat.js +38 -24
  25. package/model-function/generate-text/prompt-format/TextPromptFormat.cjs +27 -30
  26. package/model-function/generate-text/prompt-format/TextPromptFormat.d.ts +7 -5
  27. package/model-function/generate-text/prompt-format/TextPromptFormat.js +24 -27
  28. package/model-function/generate-text/prompt-format/VicunaPromptFormat.cjs +21 -29
  29. package/model-function/generate-text/prompt-format/VicunaPromptFormat.d.ts +2 -2
  30. package/model-function/generate-text/prompt-format/VicunaPromptFormat.js +19 -27
  31. package/model-function/generate-text/prompt-format/index.cjs +39 -0
  32. package/model-function/generate-text/prompt-format/index.d.ts +10 -0
  33. package/model-function/generate-text/prompt-format/index.js +10 -0
  34. package/model-function/generate-text/prompt-format/trimChatPrompt.cjs +17 -22
  35. package/model-function/generate-text/prompt-format/trimChatPrompt.js +17 -22
  36. package/model-function/generate-text/prompt-format/validateChatPrompt.cjs +12 -24
  37. package/model-function/generate-text/prompt-format/validateChatPrompt.d.ts +0 -3
  38. package/model-function/generate-text/prompt-format/validateChatPrompt.js +10 -21
  39. package/model-function/generate-tool-call/ToolCallDefinition.cjs +2 -0
  40. package/model-function/generate-tool-call/ToolCallDefinition.d.ts +7 -0
  41. package/model-function/generate-tool-call/ToolCallDefinition.js +1 -0
  42. package/model-function/generate-tool-call/ToolCallGenerationError.cjs +35 -0
  43. package/model-function/generate-tool-call/ToolCallGenerationError.d.ts +15 -0
  44. package/model-function/generate-tool-call/ToolCallGenerationError.js +31 -0
  45. package/model-function/generate-tool-call/ToolCallGenerationEvent.cjs +2 -0
  46. package/model-function/generate-tool-call/ToolCallGenerationEvent.d.ts +23 -0
  47. package/model-function/generate-tool-call/ToolCallGenerationEvent.js +1 -0
  48. package/model-function/generate-tool-call/ToolCallGenerationModel.cjs +2 -0
  49. package/model-function/generate-tool-call/ToolCallGenerationModel.d.ts +19 -0
  50. package/model-function/generate-tool-call/ToolCallGenerationModel.js +1 -0
  51. package/model-function/generate-tool-call/ToolCallParametersValidationError.cjs +44 -0
  52. package/model-function/generate-tool-call/ToolCallParametersValidationError.d.ts +18 -0
  53. package/model-function/generate-tool-call/ToolCallParametersValidationError.js +40 -0
  54. package/model-function/generate-tool-call/generateToolCall.cjs +58 -0
  55. package/model-function/generate-tool-call/generateToolCall.d.ts +20 -0
  56. package/model-function/generate-tool-call/generateToolCall.js +54 -0
  57. package/model-function/generate-tool-call/index.cjs +21 -0
  58. package/model-function/generate-tool-call/index.d.ts +5 -0
  59. package/model-function/generate-tool-call/index.js +5 -0
  60. package/model-function/index.cjs +1 -0
  61. package/model-function/index.d.ts +1 -0
  62. package/model-function/index.js +1 -0
  63. package/model-provider/anthropic/AnthropicPromptFormat.cjs +22 -26
  64. package/model-provider/anthropic/AnthropicPromptFormat.d.ts +4 -2
  65. package/model-provider/anthropic/AnthropicPromptFormat.js +19 -23
  66. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +2 -2
  67. package/model-provider/anthropic/AnthropicTextGenerationModel.js +3 -3
  68. package/model-provider/anthropic/index.cjs +14 -2
  69. package/model-provider/anthropic/index.d.ts +1 -1
  70. package/model-provider/anthropic/index.js +1 -1
  71. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  72. package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -3
  73. package/model-provider/cohere/CohereTextGenerationModel.d.ts +1 -1
  74. package/model-provider/cohere/CohereTextGenerationModel.js +4 -4
  75. package/model-provider/llamacpp/{mapInstructionPromptToBakLLaVA1ForLlamaCppFormat.cjs → LlamaCppBakLLaVA1Format.cjs} +4 -4
  76. package/model-provider/llamacpp/{mapInstructionPromptToBakLLaVA1ForLlamaCppFormat.d.ts → LlamaCppBakLLaVA1Format.d.ts} +2 -2
  77. package/model-provider/llamacpp/{mapInstructionPromptToBakLLaVA1ForLlamaCppFormat.js → LlamaCppBakLLaVA1Format.js} +2 -2
  78. package/model-provider/llamacpp/index.cjs +14 -2
  79. package/model-provider/llamacpp/index.d.ts +1 -1
  80. package/model-provider/llamacpp/index.js +1 -1
  81. package/model-provider/openai/OpenAICompletionModel.cjs +4 -4
  82. package/model-provider/openai/OpenAICompletionModel.d.ts +1 -1
  83. package/model-provider/openai/OpenAICompletionModel.js +5 -5
  84. package/model-provider/openai/chat/OpenAIChatModel.cjs +50 -4
  85. package/model-provider/openai/chat/OpenAIChatModel.d.ts +192 -20
  86. package/model-provider/openai/chat/OpenAIChatModel.js +51 -5
  87. package/model-provider/openai/chat/OpenAIChatPromptFormat.cjs +22 -34
  88. package/model-provider/openai/chat/OpenAIChatPromptFormat.d.ts +2 -2
  89. package/model-provider/openai/chat/OpenAIChatPromptFormat.js +19 -31
  90. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +10 -0
  91. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +10 -0
  92. package/model-provider/openai/index.cjs +14 -2
  93. package/model-provider/openai/index.d.ts +1 -1
  94. package/model-provider/openai/index.js +1 -1
  95. package/package.json +3 -3
@@ -15,39 +15,34 @@ const validateChatPrompt_js_1 = require("./validateChatPrompt.cjs");
15
15
  async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
16
16
  (model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), }) {
17
17
  (0, validateChatPrompt_js_1.validateChatPrompt)(prompt);
18
- const startsWithSystemMessage = "system" in prompt[0];
19
- const systemMessage = startsWithSystemMessage ? [prompt[0]] : [];
20
- let messages = [];
21
- // add the last message (final user message) to the prompt
22
- messages.push(prompt[prompt.length - 1]);
18
+ let minimalPrompt = {
19
+ system: prompt.system,
20
+ messages: [prompt.messages[prompt.messages.length - 1]], // last user message
21
+ };
23
22
  // check if the minimal prompt is already too long
24
- const promptTokenCount = await model.countPromptTokens([
25
- ...systemMessage,
26
- ...messages,
27
- ]);
23
+ const promptTokenCount = await model.countPromptTokens(minimalPrompt);
28
24
  // the minimal chat prompt is already over the token limit and cannot be trimmed further:
29
25
  if (promptTokenCount > tokenLimit) {
30
- return [...systemMessage, prompt[prompt.length - 1]];
26
+ return minimalPrompt;
31
27
  }
32
28
  // inner messages
33
- const innerMessages = prompt.slice(startsWithSystemMessage ? 1 : 0, -1);
29
+ const innerMessages = prompt.messages.slice(0, -1);
34
30
  // taking always a pair of user-message and ai-message from the end, moving backwards
35
31
  for (let i = innerMessages.length - 1; i >= 0; i -= 2) {
36
- const aiMessage = innerMessages[i];
32
+ const assistantMessage = innerMessages[i];
37
33
  const userMessage = innerMessages[i - 1];
38
- // create a temporary array and check if it fits within the token limit
39
- const tokenCount = await model.countPromptTokens([
40
- ...systemMessage,
41
- userMessage,
42
- aiMessage,
43
- ...messages,
44
- ]);
34
+ // create a temporary prompt and check if it fits within the token limit
35
+ const attemptedPrompt = {
36
+ system: prompt.system,
37
+ messages: [userMessage, assistantMessage, ...minimalPrompt.messages],
38
+ };
39
+ const tokenCount = await model.countPromptTokens(attemptedPrompt);
45
40
  if (tokenCount > tokenLimit) {
46
41
  break;
47
42
  }
48
- // if it fits, add the messages to the messages array
49
- messages = [userMessage, aiMessage, ...messages];
43
+ // if it fits, its the new minimal prompt
44
+ minimalPrompt = attemptedPrompt;
50
45
  }
51
- return [...systemMessage, ...messages];
46
+ return minimalPrompt;
52
47
  }
53
48
  exports.trimChatPrompt = trimChatPrompt;
@@ -12,38 +12,33 @@ import { validateChatPrompt } from "./validateChatPrompt.js";
12
12
  export async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
13
13
  (model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), }) {
14
14
  validateChatPrompt(prompt);
15
- const startsWithSystemMessage = "system" in prompt[0];
16
- const systemMessage = startsWithSystemMessage ? [prompt[0]] : [];
17
- let messages = [];
18
- // add the last message (final user message) to the prompt
19
- messages.push(prompt[prompt.length - 1]);
15
+ let minimalPrompt = {
16
+ system: prompt.system,
17
+ messages: [prompt.messages[prompt.messages.length - 1]], // last user message
18
+ };
20
19
  // check if the minimal prompt is already too long
21
- const promptTokenCount = await model.countPromptTokens([
22
- ...systemMessage,
23
- ...messages,
24
- ]);
20
+ const promptTokenCount = await model.countPromptTokens(minimalPrompt);
25
21
  // the minimal chat prompt is already over the token limit and cannot be trimmed further:
26
22
  if (promptTokenCount > tokenLimit) {
27
- return [...systemMessage, prompt[prompt.length - 1]];
23
+ return minimalPrompt;
28
24
  }
29
25
  // inner messages
30
- const innerMessages = prompt.slice(startsWithSystemMessage ? 1 : 0, -1);
26
+ const innerMessages = prompt.messages.slice(0, -1);
31
27
  // taking always a pair of user-message and ai-message from the end, moving backwards
32
28
  for (let i = innerMessages.length - 1; i >= 0; i -= 2) {
33
- const aiMessage = innerMessages[i];
29
+ const assistantMessage = innerMessages[i];
34
30
  const userMessage = innerMessages[i - 1];
35
- // create a temporary array and check if it fits within the token limit
36
- const tokenCount = await model.countPromptTokens([
37
- ...systemMessage,
38
- userMessage,
39
- aiMessage,
40
- ...messages,
41
- ]);
31
+ // create a temporary prompt and check if it fits within the token limit
32
+ const attemptedPrompt = {
33
+ system: prompt.system,
34
+ messages: [userMessage, assistantMessage, ...minimalPrompt.messages],
35
+ };
36
+ const tokenCount = await model.countPromptTokens(attemptedPrompt);
42
37
  if (tokenCount > tokenLimit) {
43
38
  break;
44
39
  }
45
- // if it fits, add the messages to the messages array
46
- messages = [userMessage, aiMessage, ...messages];
40
+ // if it fits, its the new minimal prompt
41
+ minimalPrompt = attemptedPrompt;
47
42
  }
48
- return [...systemMessage, ...messages];
43
+ return minimalPrompt;
49
44
  }
@@ -1,36 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateChatPrompt = exports.ChatPromptValidationError = void 0;
4
- class ChatPromptValidationError extends Error {
5
- constructor(message) {
6
- super(message);
7
- this.name = "ChatPromptValidationError";
8
- }
9
- }
10
- exports.ChatPromptValidationError = ChatPromptValidationError;
3
+ exports.validateChatPrompt = void 0;
4
+ const ChatPromptValidationError_js_1 = require("./ChatPromptValidationError.cjs");
11
5
  /**
12
6
  * Checks if a chat prompt is valid. Throws a `ChatPromptValidationError` if it's not.
13
7
  */
14
8
  function validateChatPrompt(chatPrompt) {
15
- if (chatPrompt.length < 1) {
16
- throw new ChatPromptValidationError("ChatPrompt should have at least one message.");
17
- }
18
- const initialType = "system" in chatPrompt[0] ? "system" : "user";
19
- if (initialType === "system" && chatPrompt.length === 1) {
20
- throw new ChatPromptValidationError("A system message should be followed by a user message.");
9
+ const messages = chatPrompt.messages;
10
+ if (messages.length < 1) {
11
+ throw new ChatPromptValidationError_js_1.ChatPromptValidationError("ChatPrompt should have at least one message.");
21
12
  }
22
- let expectedType = initialType === "system" ? "user" : "ai";
23
- for (let i = 1; i < chatPrompt.length; i++) {
24
- const messageType = "user" in chatPrompt[i] ? "user" : "ai";
25
- if (messageType !== expectedType) {
26
- throw new ChatPromptValidationError(`Message at index ${i} should be a ${expectedType} message, but it's a ${messageType} message.`);
13
+ for (let i = 0; i < messages.length; i++) {
14
+ const expectedRole = i % 2 === 0 ? "user" : "assistant";
15
+ const role = messages[i].role;
16
+ if (role !== expectedRole) {
17
+ throw new ChatPromptValidationError_js_1.ChatPromptValidationError(`Message at index ${i} should have role '${expectedRole}', but has role '${role}'.`);
27
18
  }
28
- // Flip the expected type for the next iteration.
29
- expectedType = expectedType === "user" ? "ai" : "user";
30
19
  }
31
- // If the last message is not a user message, throw an error.
32
- if (expectedType !== "ai") {
33
- throw new ChatPromptValidationError("The last message should be a user message.");
20
+ if (messages.length % 2 === 0) {
21
+ throw new ChatPromptValidationError_js_1.ChatPromptValidationError("The last message must be a user message.");
34
22
  }
35
23
  }
36
24
  exports.validateChatPrompt = validateChatPrompt;
@@ -1,7 +1,4 @@
1
1
  import { ChatPrompt } from "./ChatPrompt.js";
2
- export declare class ChatPromptValidationError extends Error {
3
- constructor(message: string);
4
- }
5
2
  /**
6
3
  * Checks if a chat prompt is valid. Throws a `ChatPromptValidationError` if it's not.
7
4
  */
@@ -1,31 +1,20 @@
1
- export class ChatPromptValidationError extends Error {
2
- constructor(message) {
3
- super(message);
4
- this.name = "ChatPromptValidationError";
5
- }
6
- }
1
+ import { ChatPromptValidationError } from "./ChatPromptValidationError.js";
7
2
  /**
8
3
  * Checks if a chat prompt is valid. Throws a `ChatPromptValidationError` if it's not.
9
4
  */
10
5
  export function validateChatPrompt(chatPrompt) {
11
- if (chatPrompt.length < 1) {
6
+ const messages = chatPrompt.messages;
7
+ if (messages.length < 1) {
12
8
  throw new ChatPromptValidationError("ChatPrompt should have at least one message.");
13
9
  }
14
- const initialType = "system" in chatPrompt[0] ? "system" : "user";
15
- if (initialType === "system" && chatPrompt.length === 1) {
16
- throw new ChatPromptValidationError("A system message should be followed by a user message.");
17
- }
18
- let expectedType = initialType === "system" ? "user" : "ai";
19
- for (let i = 1; i < chatPrompt.length; i++) {
20
- const messageType = "user" in chatPrompt[i] ? "user" : "ai";
21
- if (messageType !== expectedType) {
22
- throw new ChatPromptValidationError(`Message at index ${i} should be a ${expectedType} message, but it's a ${messageType} message.`);
10
+ for (let i = 0; i < messages.length; i++) {
11
+ const expectedRole = i % 2 === 0 ? "user" : "assistant";
12
+ const role = messages[i].role;
13
+ if (role !== expectedRole) {
14
+ throw new ChatPromptValidationError(`Message at index ${i} should have role '${expectedRole}', but has role '${role}'.`);
23
15
  }
24
- // Flip the expected type for the next iteration.
25
- expectedType = expectedType === "user" ? "ai" : "user";
26
16
  }
27
- // If the last message is not a user message, throw an error.
28
- if (expectedType !== "ai") {
29
- throw new ChatPromptValidationError("The last message should be a user message.");
17
+ if (messages.length % 2 === 0) {
18
+ throw new ChatPromptValidationError("The last message must be a user message.");
30
19
  }
31
20
  }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
2
+ import { Schema } from "../../core/schema/Schema.js";
3
+ export interface ToolCallDefinition<NAME extends string, PARAMETERS> {
4
+ name: NAME;
5
+ description?: string;
6
+ parameters: Schema<PARAMETERS> & JsonSchemaProducer;
7
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ToolCallsGenerationError = void 0;
4
+ const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
+ class ToolCallsGenerationError extends Error {
6
+ constructor({ toolName, cause }) {
7
+ super(`Tool call generation failed for tool '${toolName}'. ` +
8
+ `Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
9
+ Object.defineProperty(this, "toolName", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ Object.defineProperty(this, "cause", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ this.name = "ToolCallsGenerationError";
22
+ this.toolName = toolName;
23
+ this.cause = cause;
24
+ }
25
+ toJSON() {
26
+ return {
27
+ name: this.name,
28
+ message: this.message,
29
+ cause: this.cause,
30
+ stack: this.stack,
31
+ toolName: this.toolName,
32
+ };
33
+ }
34
+ }
35
+ exports.ToolCallsGenerationError = ToolCallsGenerationError;
@@ -0,0 +1,15 @@
1
+ export declare class ToolCallsGenerationError extends Error {
2
+ readonly toolName: string;
3
+ readonly cause: unknown;
4
+ constructor({ toolName, cause }: {
5
+ toolName: string;
6
+ cause: unknown;
7
+ });
8
+ toJSON(): {
9
+ name: string;
10
+ message: string;
11
+ cause: unknown;
12
+ stack: string | undefined;
13
+ toolName: string;
14
+ };
15
+ }
@@ -0,0 +1,31 @@
1
+ import { getErrorMessage } from "../../util/getErrorMessage.js";
2
+ export class ToolCallsGenerationError extends Error {
3
+ constructor({ toolName, cause }) {
4
+ super(`Tool call generation failed for tool '${toolName}'. ` +
5
+ `Error message: ${getErrorMessage(cause)}`);
6
+ Object.defineProperty(this, "toolName", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: void 0
11
+ });
12
+ Object.defineProperty(this, "cause", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ this.name = "ToolCallsGenerationError";
19
+ this.toolName = toolName;
20
+ this.cause = cause;
21
+ }
22
+ toJSON() {
23
+ return {
24
+ name: this.name,
25
+ message: this.message,
26
+ cause: this.cause,
27
+ stack: this.stack,
28
+ toolName: this.toolName,
29
+ };
30
+ }
31
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,23 @@
1
+ import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
2
+ export interface ToolCallGenerationStartedEvent extends BaseModelCallStartedEvent {
3
+ functionType: "generate-tool-call";
4
+ }
5
+ export type ToolCallGenerationFinishedEventResult = {
6
+ status: "success";
7
+ response: unknown;
8
+ value: unknown;
9
+ usage?: {
10
+ promptTokens: number;
11
+ completionTokens: number;
12
+ totalTokens: number;
13
+ };
14
+ } | {
15
+ status: "error";
16
+ error: unknown;
17
+ } | {
18
+ status: "abort";
19
+ };
20
+ export type ToolCallGenerationFinishedEvent = BaseModelCallFinishedEvent & {
21
+ functionType: "generate-tool-call";
22
+ result: ToolCallGenerationFinishedEventResult;
23
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,19 @@
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { Model, ModelSettings } from "../Model.js";
3
+ import { ToolCallDefinition } from "./ToolCallDefinition.js";
4
+ export interface ToolCallGenerationModelSettings extends ModelSettings {
5
+ }
6
+ export interface ToolCallGenerationModel<PROMPT, SETTINGS extends ToolCallGenerationModelSettings = ToolCallGenerationModelSettings> extends Model<SETTINGS> {
7
+ doGenerateToolCall(tool: ToolCallDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
8
+ response: unknown;
9
+ value: {
10
+ id: string;
11
+ parameters: unknown;
12
+ } | null;
13
+ usage?: {
14
+ promptTokens: number;
15
+ completionTokens: number;
16
+ totalTokens: number;
17
+ };
18
+ }>;
19
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ToolCallParametersValidationError = void 0;
4
+ const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
+ class ToolCallParametersValidationError extends Error {
6
+ constructor({ toolName, parameters, cause, }) {
7
+ super(`Parameter validation failed for tool '${toolName}'. ` +
8
+ `Value: ${JSON.stringify(parameters)}.\n` +
9
+ `Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
10
+ Object.defineProperty(this, "toolName", {
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
+ Object.defineProperty(this, "parameters", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.name = "ToolCallParametersValidationError";
29
+ this.toolName = toolName;
30
+ this.cause = cause;
31
+ this.parameters = parameters;
32
+ }
33
+ toJSON() {
34
+ return {
35
+ name: this.name,
36
+ message: this.message,
37
+ cause: this.cause,
38
+ stack: this.stack,
39
+ toolName: this.toolName,
40
+ parameter: this.parameters,
41
+ };
42
+ }
43
+ }
44
+ exports.ToolCallParametersValidationError = ToolCallParametersValidationError;
@@ -0,0 +1,18 @@
1
+ export declare class ToolCallParametersValidationError extends Error {
2
+ readonly toolName: string;
3
+ readonly cause: unknown;
4
+ readonly parameters: unknown;
5
+ constructor({ toolName, parameters, cause, }: {
6
+ toolName: string;
7
+ parameters: unknown;
8
+ cause: unknown;
9
+ });
10
+ toJSON(): {
11
+ name: string;
12
+ message: string;
13
+ cause: unknown;
14
+ stack: string | undefined;
15
+ toolName: string;
16
+ parameter: unknown;
17
+ };
18
+ }
@@ -0,0 +1,40 @@
1
+ import { getErrorMessage } from "../../util/getErrorMessage.js";
2
+ export class ToolCallParametersValidationError extends Error {
3
+ constructor({ toolName, parameters, cause, }) {
4
+ super(`Parameter validation failed for tool '${toolName}'. ` +
5
+ `Value: ${JSON.stringify(parameters)}.\n` +
6
+ `Error message: ${getErrorMessage(cause)}`);
7
+ Object.defineProperty(this, "toolName", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "cause", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "parameters", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.name = "ToolCallParametersValidationError";
26
+ this.toolName = toolName;
27
+ this.cause = cause;
28
+ this.parameters = parameters;
29
+ }
30
+ toJSON() {
31
+ return {
32
+ name: this.name,
33
+ message: this.message,
34
+ cause: this.cause,
35
+ stack: this.stack,
36
+ toolName: this.toolName,
37
+ parameter: this.parameters,
38
+ };
39
+ }
40
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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");
7
+ async function generateToolCall(model, tool, prompt, options) {
8
+ // Note: PROMPT must not be a function.
9
+ const expandedPrompt = typeof prompt === "function"
10
+ ? prompt(tool)
11
+ : prompt;
12
+ const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
13
+ functionType: "generate-tool-call",
14
+ input: expandedPrompt,
15
+ model,
16
+ options,
17
+ generateResponse: async (options) => {
18
+ try {
19
+ const result = await model.doGenerateToolCall(tool, expandedPrompt, options);
20
+ const toolCall = result.value;
21
+ if (toolCall === null) {
22
+ throw new ToolCallGenerationError_js_1.ToolCallsGenerationError({
23
+ toolName: tool.name,
24
+ cause: "No tool call generated.",
25
+ });
26
+ }
27
+ const parseResult = tool.parameters.validate(toolCall.parameters);
28
+ if (!parseResult.success) {
29
+ throw new ToolCallParametersValidationError_js_1.ToolCallParametersValidationError({
30
+ toolName: tool.name,
31
+ parameters: toolCall.parameters,
32
+ cause: parseResult.error,
33
+ });
34
+ }
35
+ return {
36
+ response: result.response,
37
+ extractedValue: {
38
+ id: toolCall.id,
39
+ parameters: parseResult.data,
40
+ },
41
+ usage: result.usage,
42
+ };
43
+ }
44
+ catch (error) {
45
+ if (error instanceof ToolCallParametersValidationError_js_1.ToolCallParametersValidationError ||
46
+ error instanceof ToolCallGenerationError_js_1.ToolCallsGenerationError) {
47
+ throw error;
48
+ }
49
+ throw new ToolCallGenerationError_js_1.ToolCallsGenerationError({
50
+ toolName: tool.name,
51
+ cause: error,
52
+ });
53
+ }
54
+ },
55
+ });
56
+ return options?.returnType === "full" ? fullResponse : fullResponse.value;
57
+ }
58
+ exports.generateToolCall = generateToolCall;
@@ -0,0 +1,20 @@
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { ModelCallMetadata } from "../ModelCallMetadata.js";
3
+ import { ToolCallDefinition } from "./ToolCallDefinition.js";
4
+ import { ToolCallGenerationModel, ToolCallGenerationModelSettings } from "./ToolCallGenerationModel.js";
5
+ export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(model: ToolCallGenerationModel<PROMPT, SETTINGS>, tool: ToolCallDefinition<NAME, PARAMETERS>, prompt: PROMPT | ((tool: ToolCallDefinition<NAME, PARAMETERS>) => PROMPT), options?: FunctionOptions & {
6
+ returnType?: "structure";
7
+ }): Promise<{
8
+ id: string;
9
+ parameters: PARAMETERS;
10
+ }>;
11
+ export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(model: ToolCallGenerationModel<PROMPT, SETTINGS>, tool: ToolCallDefinition<NAME, PARAMETERS>, prompt: PROMPT | ((tool: ToolCallDefinition<NAME, PARAMETERS>) => PROMPT), options: FunctionOptions & {
12
+ returnType: "full";
13
+ }): Promise<{
14
+ value: {
15
+ id: string;
16
+ parameters: PARAMETERS;
17
+ };
18
+ response: unknown;
19
+ metadata: ModelCallMetadata;
20
+ }>;
@@ -0,0 +1,54 @@
1
+ import { executeStandardCall } from "../executeStandardCall.js";
2
+ import { ToolCallParametersValidationError } from "./ToolCallParametersValidationError.js";
3
+ import { ToolCallsGenerationError } from "./ToolCallGenerationError.js";
4
+ export async function generateToolCall(model, tool, prompt, options) {
5
+ // Note: PROMPT must not be a function.
6
+ const expandedPrompt = typeof prompt === "function"
7
+ ? prompt(tool)
8
+ : prompt;
9
+ const fullResponse = await executeStandardCall({
10
+ functionType: "generate-tool-call",
11
+ input: expandedPrompt,
12
+ model,
13
+ options,
14
+ generateResponse: async (options) => {
15
+ try {
16
+ const result = await model.doGenerateToolCall(tool, expandedPrompt, options);
17
+ const toolCall = result.value;
18
+ if (toolCall === null) {
19
+ throw new ToolCallsGenerationError({
20
+ toolName: tool.name,
21
+ cause: "No tool call generated.",
22
+ });
23
+ }
24
+ const parseResult = tool.parameters.validate(toolCall.parameters);
25
+ if (!parseResult.success) {
26
+ throw new ToolCallParametersValidationError({
27
+ toolName: tool.name,
28
+ parameters: toolCall.parameters,
29
+ cause: parseResult.error,
30
+ });
31
+ }
32
+ return {
33
+ response: result.response,
34
+ extractedValue: {
35
+ id: toolCall.id,
36
+ parameters: parseResult.data,
37
+ },
38
+ usage: result.usage,
39
+ };
40
+ }
41
+ catch (error) {
42
+ if (error instanceof ToolCallParametersValidationError ||
43
+ error instanceof ToolCallsGenerationError) {
44
+ throw error;
45
+ }
46
+ throw new ToolCallsGenerationError({
47
+ toolName: tool.name,
48
+ cause: error,
49
+ });
50
+ }
51
+ },
52
+ });
53
+ return options?.returnType === "full" ? fullResponse : fullResponse.value;
54
+ }
@@ -0,0 +1,21 @@
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("./ToolCallDefinition.cjs"), exports);
18
+ __exportStar(require("./ToolCallParametersValidationError.cjs"), exports);
19
+ __exportStar(require("./ToolCallGenerationEvent.cjs"), exports);
20
+ __exportStar(require("./ToolCallGenerationModel.cjs"), exports);
21
+ __exportStar(require("./generateToolCall.cjs"), exports);
@@ -0,0 +1,5 @@
1
+ export * from "./ToolCallDefinition.js";
2
+ export * from "./ToolCallParametersValidationError.js";
3
+ export * from "./ToolCallGenerationEvent.js";
4
+ export * from "./ToolCallGenerationModel.js";
5
+ export * from "./generateToolCall.js";