modelfusion 0.70.0 → 0.72.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.
- package/README.md +61 -49
- package/core/FunctionEvent.d.ts +5 -5
- package/guard/guard.cjs +5 -4
- package/guard/guard.d.ts +1 -1
- package/guard/guard.js +5 -4
- package/model-function/ModelCallEvent.d.ts +4 -3
- package/model-function/generate-structure/StructureGenerationEvent.d.ts +2 -2
- package/model-function/generate-structure/StructureParseError.cjs +10 -0
- package/model-function/generate-structure/StructureParseError.d.ts +8 -0
- package/model-function/generate-structure/StructureParseError.js +10 -0
- package/model-function/generate-structure/index.cjs +0 -3
- package/model-function/generate-structure/index.d.ts +0 -3
- package/model-function/generate-structure/index.js +0 -3
- package/model-function/index.cjs +0 -1
- package/model-function/index.d.ts +0 -1
- package/model-function/index.js +0 -1
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatMessage.cjs +16 -25
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +14 -29
- package/model-provider/openai/chat/OpenAIChatMessage.js +16 -25
- package/model-provider/openai/chat/OpenAIChatModel.cjs +28 -48
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +23 -68
- package/model-provider/openai/chat/OpenAIChatModel.js +28 -48
- package/package.json +2 -2
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.js → tool/NoSuchToolDefinitionError.cjs} +9 -8
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.d.ts → tool/NoSuchToolDefinitionError.d.ts} +2 -3
- package/tool/NoSuchToolDefinitionError.js +37 -0
- package/tool/Tool.cjs +5 -16
- package/tool/Tool.d.ts +9 -14
- package/tool/Tool.js +5 -16
- package/tool/ToolCall.d.ts +15 -0
- package/tool/ToolCallArgumentsValidationError.cjs +49 -0
- package/tool/ToolCallArgumentsValidationError.d.ts +23 -0
- package/tool/ToolCallArgumentsValidationError.js +45 -0
- package/tool/ToolCallError.cjs +34 -0
- package/tool/ToolCallError.d.ts +17 -0
- package/tool/ToolCallError.js +30 -0
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.cjs +4 -4
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.d.ts +1 -1
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.js +2 -2
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.cjs → tool/ToolCallParseError.cjs} +13 -13
- package/tool/ToolCallParseError.d.ts +18 -0
- package/tool/ToolCallParseError.js +40 -0
- package/tool/ToolCallResult.d.ts +13 -0
- package/tool/ToolDefinition.d.ts +7 -0
- package/tool/ToolExecutionError.cjs +5 -4
- package/tool/ToolExecutionError.d.ts +4 -4
- package/tool/ToolExecutionError.js +5 -4
- package/tool/WebSearchTool.cjs +4 -4
- package/tool/WebSearchTool.d.ts +2 -2
- package/tool/WebSearchTool.js +4 -4
- package/tool/{ExecuteToolEvent.d.ts → execute-tool/ExecuteToolEvent.d.ts} +1 -1
- package/tool/{executeTool.cjs → execute-tool/executeTool.cjs} +15 -15
- package/tool/{executeTool.d.ts → execute-tool/executeTool.d.ts} +6 -6
- package/tool/{executeTool.js → execute-tool/executeTool.js} +15 -15
- package/tool/execute-tool/index.cjs +18 -0
- package/tool/execute-tool/index.d.ts +2 -0
- package/tool/execute-tool/index.js +2 -0
- package/tool/execute-tool/safeExecuteToolCall.cjs +34 -0
- package/tool/execute-tool/safeExecuteToolCall.d.ts +5 -0
- package/tool/execute-tool/safeExecuteToolCall.js +30 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +59 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +35 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.js +55 -0
- package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.d.ts +1 -1
- package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.d.ts +5 -5
- package/{model-function → tool}/generate-tool-call/generateToolCall.cjs +13 -12
- package/{model-function → tool}/generate-tool-call/generateToolCall.d.ts +7 -12
- package/{model-function → tool}/generate-tool-call/generateToolCall.js +13 -12
- package/{model-function → tool}/generate-tool-call/index.cjs +1 -2
- package/{model-function → tool}/generate-tool-call/index.d.ts +1 -2
- package/{model-function → tool}/generate-tool-call/index.js +1 -2
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.d.ts +23 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.cjs +2 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.d.ts +21 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.js +1 -0
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.cjs +63 -0
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.d.ts +33 -0
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.js +59 -0
- package/tool/generate-tool-calls-or-text/index.cjs +19 -0
- package/tool/generate-tool-calls-or-text/index.d.ts +3 -0
- package/tool/generate-tool-calls-or-text/index.js +3 -0
- package/tool/index.cjs +12 -7
- package/tool/index.d.ts +12 -7
- package/tool/index.js +12 -7
- package/tool/use-tool/UseToolEvent.cjs +2 -0
- package/tool/{UseToolEvent.d.ts → use-tool/UseToolEvent.d.ts} +1 -1
- package/tool/use-tool/UseToolEvent.js +1 -0
- package/tool/use-tool/index.cjs +18 -0
- package/tool/use-tool/index.d.ts +2 -0
- package/tool/use-tool/index.js +2 -0
- package/tool/use-tool/useTool.cjs +30 -0
- package/tool/use-tool/useTool.d.ts +16 -0
- package/tool/use-tool/useTool.js +26 -0
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.cjs +2 -0
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.d.ts +7 -0
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js +1 -0
- package/tool/use-tools-or-generate-text/index.cjs +18 -0
- package/tool/use-tools-or-generate-text/index.d.ts +2 -0
- package/tool/use-tools-or-generate-text/index.js +2 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.cjs +48 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.d.ts +18 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.js +44 -0
- package/util/getErrorMessage.cjs +3 -0
- package/util/getErrorMessage.d.ts +1 -1
- package/util/getErrorMessage.js +3 -0
- package/model-function/generate-structure/NoSuchStructureError.cjs +0 -17
- package/model-function/generate-structure/NoSuchStructureError.d.ts +0 -4
- package/model-function/generate-structure/NoSuchStructureError.js +0 -13
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +0 -26
- package/model-function/generate-structure/generateStructureOrText.cjs +0 -54
- package/model-function/generate-structure/generateStructureOrText.d.ts +0 -97
- package/model-function/generate-structure/generateStructureOrText.js +0 -50
- package/model-function/generate-tool-call/ToolCallDefinition.d.ts +0 -7
- package/tool/NoSuchToolError.cjs +0 -25
- package/tool/NoSuchToolError.d.ts +0 -10
- package/tool/NoSuchToolError.js +0 -21
- package/tool/UseToolOrGenerateTextEvent.d.ts +0 -7
- package/tool/useTool.cjs +0 -40
- package/tool/useTool.d.ts +0 -15
- package/tool/useTool.js +0 -36
- package/tool/useToolOrGenerateText.cjs +0 -50
- package/tool/useToolOrGenerateText.d.ts +0 -23
- package/tool/useToolOrGenerateText.js +0 -46
- /package/{model-function/generate-structure/StructureOrTextGenerationModel.cjs → tool/ToolCall.cjs} +0 -0
- /package/{model-function/generate-structure/StructureOrTextGenerationModel.js → tool/ToolCall.js} +0 -0
- /package/{model-function/generate-tool-call/ToolCallDefinition.cjs → tool/ToolCallResult.cjs} +0 -0
- /package/{model-function/generate-tool-call/ToolCallDefinition.js → tool/ToolCallResult.js} +0 -0
- /package/{model-function/generate-tool-call/ToolCallGenerationEvent.cjs → tool/ToolDefinition.cjs} +0 -0
- /package/{model-function/generate-tool-call/ToolCallGenerationEvent.js → tool/ToolDefinition.js} +0 -0
- /package/tool/{ExecuteToolEvent.cjs → execute-tool/ExecuteToolEvent.cjs} +0 -0
- /package/tool/{ExecuteToolEvent.js → execute-tool/ExecuteToolEvent.js} +0 -0
- /package/{model-function/generate-tool-call/ToolCallGenerationModel.cjs → tool/generate-tool-call/ToolCallGenerationEvent.cjs} +0 -0
- /package/{model-function/generate-tool-call/ToolCallGenerationModel.js → tool/generate-tool-call/ToolCallGenerationEvent.js} +0 -0
- /package/tool/{UseToolEvent.cjs → generate-tool-call/ToolCallGenerationModel.cjs} +0 -0
- /package/tool/{UseToolEvent.js → generate-tool-call/ToolCallGenerationModel.js} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.cjs → generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.cjs} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.js → generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.js} +0 -0
@@ -0,0 +1,33 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { ModelCallMetadata } from "../../model-function/ModelCallMetadata.js";
|
3
|
+
import { ToolDefinition } from "../ToolDefinition.js";
|
4
|
+
import { ToolCallsOrTextGenerationModel, ToolCallsOrTextGenerationModelSettings } from "./ToolCallsOrTextGenerationModel.js";
|
5
|
+
type ToolCallDefinitionArray<T extends ToolDefinition<any, any>[]> = T;
|
6
|
+
type ToToolCallDefinitionMap<T extends ToolCallDefinitionArray<ToolDefinition<any, any>[]>> = {
|
7
|
+
[K in T[number]["name"]]: Extract<T[number], ToolDefinition<K, any>>;
|
8
|
+
};
|
9
|
+
type ToToolCallUnion<T> = {
|
10
|
+
[KEY in keyof T]: T[KEY] extends ToolDefinition<any, infer PARAMETERS> ? {
|
11
|
+
id: string;
|
12
|
+
name: KEY;
|
13
|
+
args: PARAMETERS;
|
14
|
+
} : never;
|
15
|
+
}[keyof T];
|
16
|
+
type ToOutputValue<TOOL_CALLS extends ToolCallDefinitionArray<ToolDefinition<any, any>[]>> = ToToolCallUnion<ToToolCallDefinitionMap<TOOL_CALLS>>;
|
17
|
+
export declare function generateToolCallsOrText<TOOLS extends Array<ToolDefinition<any, any>>, PROMPT>(model: ToolCallsOrTextGenerationModel<PROMPT, ToolCallsOrTextGenerationModelSettings>, tools: TOOLS, prompt: PROMPT | ((structureDefinitions: TOOLS) => PROMPT), options?: FunctionOptions & {
|
18
|
+
returnType?: "structure";
|
19
|
+
}): Promise<{
|
20
|
+
text: string | null;
|
21
|
+
toolCalls: Array<ToOutputValue<TOOLS>> | null;
|
22
|
+
}>;
|
23
|
+
export declare function generateToolCallsOrText<TOOLS extends ToolDefinition<any, any>[], PROMPT>(model: ToolCallsOrTextGenerationModel<PROMPT, ToolCallsOrTextGenerationModelSettings>, tools: TOOLS, prompt: PROMPT | ((structureDefinitions: TOOLS) => PROMPT), options: FunctionOptions & {
|
24
|
+
returnType?: "full";
|
25
|
+
}): Promise<{
|
26
|
+
value: {
|
27
|
+
text: string | null;
|
28
|
+
toolCalls: Array<ToOutputValue<TOOLS>>;
|
29
|
+
};
|
30
|
+
response: unknown;
|
31
|
+
metadata: ModelCallMetadata;
|
32
|
+
}>;
|
33
|
+
export {};
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import { executeStandardCall } from "../../model-function/executeStandardCall.js";
|
2
|
+
import { NoSuchToolDefinitionError } from "../NoSuchToolDefinitionError.js";
|
3
|
+
import { ToolCallArgumentsValidationError } from "../ToolCallArgumentsValidationError.js";
|
4
|
+
export async function generateToolCallsOrText(model, tools, prompt, options) {
|
5
|
+
// Note: PROMPT must not be a function.
|
6
|
+
const expandedPrompt = typeof prompt === "function"
|
7
|
+
? prompt(tools)
|
8
|
+
: prompt;
|
9
|
+
const fullResponse = await executeStandardCall({
|
10
|
+
functionType: "generate-tool-calls-or-text",
|
11
|
+
input: expandedPrompt,
|
12
|
+
model,
|
13
|
+
options,
|
14
|
+
generateResponse: async (options) => {
|
15
|
+
const result = await model.doGenerateToolCallsOrText(tools, expandedPrompt, options);
|
16
|
+
const { text, toolCalls: rawToolCalls } = result;
|
17
|
+
// no tool calls:
|
18
|
+
if (rawToolCalls == null) {
|
19
|
+
return {
|
20
|
+
response: result.response,
|
21
|
+
extractedValue: { text, toolCalls: null },
|
22
|
+
usage: result.usage,
|
23
|
+
};
|
24
|
+
}
|
25
|
+
// map tool calls:
|
26
|
+
const toolCalls = rawToolCalls.map((rawToolCall) => {
|
27
|
+
const tool = tools.find((tool) => tool.name === rawToolCall.name);
|
28
|
+
if (tool == undefined) {
|
29
|
+
throw new NoSuchToolDefinitionError({
|
30
|
+
toolName: rawToolCall.name,
|
31
|
+
parameters: rawToolCall.args,
|
32
|
+
});
|
33
|
+
}
|
34
|
+
const parseResult = tool.parameters.validate(rawToolCall.args);
|
35
|
+
if (!parseResult.success) {
|
36
|
+
throw new ToolCallArgumentsValidationError({
|
37
|
+
toolName: tool.name,
|
38
|
+
args: rawToolCall.args,
|
39
|
+
cause: parseResult.error,
|
40
|
+
});
|
41
|
+
}
|
42
|
+
return {
|
43
|
+
id: rawToolCall.id,
|
44
|
+
name: tool.name,
|
45
|
+
args: parseResult.data,
|
46
|
+
};
|
47
|
+
});
|
48
|
+
return {
|
49
|
+
response: result.response,
|
50
|
+
extractedValue: {
|
51
|
+
text,
|
52
|
+
toolCalls: toolCalls,
|
53
|
+
},
|
54
|
+
usage: result.usage,
|
55
|
+
};
|
56
|
+
},
|
57
|
+
});
|
58
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
59
|
+
}
|
@@ -0,0 +1,19 @@
|
|
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("./ToolCallsOrTextGenerationEvent.cjs"), exports);
|
18
|
+
__exportStar(require("./ToolCallsOrTextGenerationModel.cjs"), exports);
|
19
|
+
__exportStar(require("./generateToolCallsOrText.cjs"), exports);
|
package/tool/index.cjs
CHANGED
@@ -14,14 +14,19 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./ExecuteToolEvent.cjs"), exports);
|
18
17
|
__exportStar(require("./InvalidToolNameError.cjs"), exports);
|
19
|
-
__exportStar(require("./
|
18
|
+
__exportStar(require("./NoSuchToolDefinitionError.cjs"), exports);
|
20
19
|
__exportStar(require("./Tool.cjs"), exports);
|
20
|
+
__exportStar(require("./ToolCall.cjs"), exports);
|
21
|
+
__exportStar(require("./ToolCallArgumentsValidationError.cjs"), exports);
|
22
|
+
__exportStar(require("./ToolCallError.cjs"), exports);
|
23
|
+
__exportStar(require("./ToolCallGenerationError.cjs"), exports);
|
24
|
+
__exportStar(require("./ToolCallResult.cjs"), exports);
|
25
|
+
__exportStar(require("./ToolDefinition.cjs"), exports);
|
21
26
|
__exportStar(require("./ToolExecutionError.cjs"), exports);
|
22
|
-
__exportStar(require("./UseToolEvent.cjs"), exports);
|
23
|
-
__exportStar(require("./UseToolOrGenerateTextEvent.cjs"), exports);
|
24
27
|
__exportStar(require("./WebSearchTool.cjs"), exports);
|
25
|
-
__exportStar(require("./
|
26
|
-
__exportStar(require("./
|
27
|
-
__exportStar(require("./
|
28
|
+
__exportStar(require("./execute-tool/index.cjs"), exports);
|
29
|
+
__exportStar(require("./generate-tool-call/index.cjs"), exports);
|
30
|
+
__exportStar(require("./generate-tool-calls-or-text/index.cjs"), exports);
|
31
|
+
__exportStar(require("./use-tool/index.cjs"), exports);
|
32
|
+
__exportStar(require("./use-tools-or-generate-text/index.cjs"), exports);
|
package/tool/index.d.ts
CHANGED
@@ -1,11 +1,16 @@
|
|
1
|
-
export * from "./ExecuteToolEvent.js";
|
2
1
|
export * from "./InvalidToolNameError.js";
|
3
|
-
export * from "./
|
2
|
+
export * from "./NoSuchToolDefinitionError.js";
|
4
3
|
export * from "./Tool.js";
|
4
|
+
export * from "./ToolCall.js";
|
5
|
+
export * from "./ToolCallArgumentsValidationError.js";
|
6
|
+
export * from "./ToolCallError.js";
|
7
|
+
export * from "./ToolCallGenerationError.js";
|
8
|
+
export * from "./ToolCallResult.js";
|
9
|
+
export * from "./ToolDefinition.js";
|
5
10
|
export * from "./ToolExecutionError.js";
|
6
|
-
export * from "./UseToolEvent.js";
|
7
|
-
export * from "./UseToolOrGenerateTextEvent.js";
|
8
11
|
export * from "./WebSearchTool.js";
|
9
|
-
export * from "./
|
10
|
-
export * from "./
|
11
|
-
export * from "./
|
12
|
+
export * from "./execute-tool/index.js";
|
13
|
+
export * from "./generate-tool-call/index.js";
|
14
|
+
export * from "./generate-tool-calls-or-text/index.js";
|
15
|
+
export * from "./use-tool/index.js";
|
16
|
+
export * from "./use-tools-or-generate-text/index.js";
|
package/tool/index.js
CHANGED
@@ -1,11 +1,16 @@
|
|
1
|
-
export * from "./ExecuteToolEvent.js";
|
2
1
|
export * from "./InvalidToolNameError.js";
|
3
|
-
export * from "./
|
2
|
+
export * from "./NoSuchToolDefinitionError.js";
|
4
3
|
export * from "./Tool.js";
|
4
|
+
export * from "./ToolCall.js";
|
5
|
+
export * from "./ToolCallArgumentsValidationError.js";
|
6
|
+
export * from "./ToolCallError.js";
|
7
|
+
export * from "./ToolCallGenerationError.js";
|
8
|
+
export * from "./ToolCallResult.js";
|
9
|
+
export * from "./ToolDefinition.js";
|
5
10
|
export * from "./ToolExecutionError.js";
|
6
|
-
export * from "./UseToolEvent.js";
|
7
|
-
export * from "./UseToolOrGenerateTextEvent.js";
|
8
11
|
export * from "./WebSearchTool.js";
|
9
|
-
export * from "./
|
10
|
-
export * from "./
|
11
|
-
export * from "./
|
12
|
+
export * from "./execute-tool/index.js";
|
13
|
+
export * from "./generate-tool-call/index.js";
|
14
|
+
export * from "./generate-tool-calls-or-text/index.js";
|
15
|
+
export * from "./use-tool/index.js";
|
16
|
+
export * from "./use-tools-or-generate-text/index.js";
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "
|
1
|
+
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../../core/FunctionEvent.js";
|
2
2
|
export interface UseToolStartedEvent extends BaseFunctionStartedEvent {
|
3
3
|
functionType: "use-tool";
|
4
4
|
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -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("./UseToolEvent.cjs"), exports);
|
18
|
+
__exportStar(require("./useTool.cjs"), exports);
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useTool = void 0;
|
4
|
+
const executeFunctionCall_js_1 = require("../../core/executeFunctionCall.cjs");
|
5
|
+
const safeExecuteToolCall_js_1 = require("../execute-tool/safeExecuteToolCall.cjs");
|
6
|
+
const generateToolCall_js_1 = require("../generate-tool-call/generateToolCall.cjs");
|
7
|
+
/**
|
8
|
+
* `useTool` uses `generateToolCall` to generate parameters for a tool and
|
9
|
+
* then executes the tool with the parameters using `executeTool`.
|
10
|
+
*
|
11
|
+
* @returns The result contains the name of the tool (`tool` property),
|
12
|
+
* the parameters (`parameters` property, typed),
|
13
|
+
* and the result of the tool execution (`result` property, typed).
|
14
|
+
*
|
15
|
+
* @see {@link generateToolCall}
|
16
|
+
* @see {@link executeTool}
|
17
|
+
*/
|
18
|
+
async function useTool(model, tool, prompt, options) {
|
19
|
+
// Note: PROMPT must not be a function.
|
20
|
+
const expandedPrompt = typeof prompt === "function"
|
21
|
+
? prompt(tool)
|
22
|
+
: prompt;
|
23
|
+
return (0, executeFunctionCall_js_1.executeFunctionCall)({
|
24
|
+
options,
|
25
|
+
input: expandedPrompt,
|
26
|
+
functionType: "use-tool",
|
27
|
+
execute: async (options) => (0, safeExecuteToolCall_js_1.safeExecuteToolCall)(tool, await (0, generateToolCall_js_1.generateToolCall)(model, tool, expandedPrompt, options), options),
|
28
|
+
});
|
29
|
+
}
|
30
|
+
exports.useTool = useTool;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { Tool } from "../Tool.js";
|
3
|
+
import { ToolCallResult } from "../ToolCallResult.js";
|
4
|
+
import { ToolCallGenerationModel, ToolCallGenerationModelSettings } from "../generate-tool-call/ToolCallGenerationModel.js";
|
5
|
+
/**
|
6
|
+
* `useTool` uses `generateToolCall` to generate parameters for a tool and
|
7
|
+
* then executes the tool with the parameters using `executeTool`.
|
8
|
+
*
|
9
|
+
* @returns The result contains the name of the tool (`tool` property),
|
10
|
+
* the parameters (`parameters` property, typed),
|
11
|
+
* and the result of the tool execution (`result` property, typed).
|
12
|
+
*
|
13
|
+
* @see {@link generateToolCall}
|
14
|
+
* @see {@link executeTool}
|
15
|
+
*/
|
16
|
+
export declare function useTool<PROMPT, TOOL extends Tool<string, any, any>>(model: ToolCallGenerationModel<PROMPT, ToolCallGenerationModelSettings>, tool: TOOL, prompt: PROMPT | ((tool: TOOL) => PROMPT), options?: FunctionOptions): Promise<ToolCallResult<TOOL["name"], TOOL["parameters"], Awaited<ReturnType<TOOL["execute"]>>>>;
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { executeFunctionCall } from "../../core/executeFunctionCall.js";
|
2
|
+
import { safeExecuteToolCall } from "../execute-tool/safeExecuteToolCall.js";
|
3
|
+
import { generateToolCall } from "../generate-tool-call/generateToolCall.js";
|
4
|
+
/**
|
5
|
+
* `useTool` uses `generateToolCall` to generate parameters for a tool and
|
6
|
+
* then executes the tool with the parameters using `executeTool`.
|
7
|
+
*
|
8
|
+
* @returns The result contains the name of the tool (`tool` property),
|
9
|
+
* the parameters (`parameters` property, typed),
|
10
|
+
* and the result of the tool execution (`result` property, typed).
|
11
|
+
*
|
12
|
+
* @see {@link generateToolCall}
|
13
|
+
* @see {@link executeTool}
|
14
|
+
*/
|
15
|
+
export async function useTool(model, tool, prompt, options) {
|
16
|
+
// Note: PROMPT must not be a function.
|
17
|
+
const expandedPrompt = typeof prompt === "function"
|
18
|
+
? prompt(tool)
|
19
|
+
: prompt;
|
20
|
+
return executeFunctionCall({
|
21
|
+
options,
|
22
|
+
input: expandedPrompt,
|
23
|
+
functionType: "use-tool",
|
24
|
+
execute: async (options) => safeExecuteToolCall(tool, await generateToolCall(model, tool, expandedPrompt, options), options),
|
25
|
+
});
|
26
|
+
}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../../core/FunctionEvent.js";
|
2
|
+
export interface UseToolsOrGenerateTextStartedEvent extends BaseFunctionStartedEvent {
|
3
|
+
functionType: "use-tools-or-generate-text";
|
4
|
+
}
|
5
|
+
export interface UseToolsOrGenerateTextFinishedEvent extends BaseFunctionFinishedEvent {
|
6
|
+
functionType: "use-tools-or-generate-text";
|
7
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -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("./UseToolsOrGenerateTextEvent.cjs"), exports);
|
18
|
+
__exportStar(require("./useToolsOrGenerateText.cjs"), exports);
|
@@ -0,0 +1,48 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useToolsOrGenerateText = void 0;
|
4
|
+
const executeFunctionCall_js_1 = require("../../core/executeFunctionCall.cjs");
|
5
|
+
const ToolCallError_js_1 = require("../ToolCallError.cjs");
|
6
|
+
const safeExecuteToolCall_js_1 = require("../execute-tool/safeExecuteToolCall.cjs");
|
7
|
+
const generateToolCallsOrText_js_1 = require("../generate-tool-calls-or-text/generateToolCallsOrText.cjs");
|
8
|
+
async function useToolsOrGenerateText(model, tools, prompt, options) {
|
9
|
+
// Note: PROMPT must not be a function.
|
10
|
+
const expandedPrompt = typeof prompt === "function"
|
11
|
+
? prompt(tools)
|
12
|
+
: prompt;
|
13
|
+
return (0, executeFunctionCall_js_1.executeFunctionCall)({
|
14
|
+
options,
|
15
|
+
input: expandedPrompt,
|
16
|
+
functionType: "use-tools-or-generate-text",
|
17
|
+
execute: async (options) => {
|
18
|
+
const modelResponse = await (0, generateToolCallsOrText_js_1.generateToolCallsOrText)(model, tools, expandedPrompt, { ...options, returnType: "structure" });
|
19
|
+
const { toolCalls, text } = modelResponse;
|
20
|
+
// no tool calls:
|
21
|
+
if (toolCalls == null) {
|
22
|
+
return { text, toolResults: null };
|
23
|
+
}
|
24
|
+
// execute tools in parallel:
|
25
|
+
const toolResults = await Promise.all(toolCalls.map(async (toolCall) => {
|
26
|
+
const tool = tools.find((tool) => tool.name === toolCall.name);
|
27
|
+
if (tool == null) {
|
28
|
+
return {
|
29
|
+
tool: toolCall.name,
|
30
|
+
toolCall,
|
31
|
+
args: toolCall.args,
|
32
|
+
ok: false,
|
33
|
+
result: new ToolCallError_js_1.ToolCallError({
|
34
|
+
message: `No tool with name '${toolCall.name}' found.`,
|
35
|
+
toolCall,
|
36
|
+
}),
|
37
|
+
};
|
38
|
+
}
|
39
|
+
return await (0, safeExecuteToolCall_js_1.safeExecuteToolCall)(tool, toolCall, options);
|
40
|
+
}));
|
41
|
+
return {
|
42
|
+
text,
|
43
|
+
toolResults: toolResults,
|
44
|
+
};
|
45
|
+
},
|
46
|
+
});
|
47
|
+
}
|
48
|
+
exports.useToolsOrGenerateText = useToolsOrGenerateText;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { Tool } from "../Tool.js";
|
3
|
+
import { ToolCallResult } from "../ToolCallResult.js";
|
4
|
+
import { ToolCallsOrTextGenerationModel, ToolCallsOrTextGenerationModelSettings } from "../generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.js";
|
5
|
+
type ToolArray<T extends Tool<any, any, any>[]> = T;
|
6
|
+
type ToToolMap<T extends ToolArray<Tool<any, any, any>[]>> = {
|
7
|
+
[K in T[number]["name"]]: Extract<T[number], Tool<K, any, any>>;
|
8
|
+
};
|
9
|
+
type StringKeys<T> = Extract<keyof T, string>;
|
10
|
+
type ToToolCallUnion<T> = {
|
11
|
+
[KEY in StringKeys<T>]: T[KEY] extends Tool<any, infer PARAMETERS, infer OUTPUT> ? ToolCallResult<KEY, PARAMETERS, OUTPUT> : never;
|
12
|
+
}[StringKeys<T>];
|
13
|
+
type ToOutputValue<TOOLS extends ToolArray<Tool<any, any, any>[]>> = ToToolCallUnion<ToToolMap<TOOLS>>;
|
14
|
+
export declare function useToolsOrGenerateText<PROMPT, TOOLS extends Array<Tool<any, any, any>>>(model: ToolCallsOrTextGenerationModel<PROMPT, ToolCallsOrTextGenerationModelSettings>, tools: TOOLS, prompt: PROMPT | ((tools: TOOLS) => PROMPT), options?: FunctionOptions): Promise<{
|
15
|
+
text: string | null;
|
16
|
+
toolResults: Array<ToOutputValue<TOOLS>> | null;
|
17
|
+
}>;
|
18
|
+
export {};
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { executeFunctionCall } from "../../core/executeFunctionCall.js";
|
2
|
+
import { ToolCallError } from "../ToolCallError.js";
|
3
|
+
import { safeExecuteToolCall } from "../execute-tool/safeExecuteToolCall.js";
|
4
|
+
import { generateToolCallsOrText } from "../generate-tool-calls-or-text/generateToolCallsOrText.js";
|
5
|
+
export async function useToolsOrGenerateText(model, tools, prompt, options) {
|
6
|
+
// Note: PROMPT must not be a function.
|
7
|
+
const expandedPrompt = typeof prompt === "function"
|
8
|
+
? prompt(tools)
|
9
|
+
: prompt;
|
10
|
+
return executeFunctionCall({
|
11
|
+
options,
|
12
|
+
input: expandedPrompt,
|
13
|
+
functionType: "use-tools-or-generate-text",
|
14
|
+
execute: async (options) => {
|
15
|
+
const modelResponse = await generateToolCallsOrText(model, tools, expandedPrompt, { ...options, returnType: "structure" });
|
16
|
+
const { toolCalls, text } = modelResponse;
|
17
|
+
// no tool calls:
|
18
|
+
if (toolCalls == null) {
|
19
|
+
return { text, toolResults: null };
|
20
|
+
}
|
21
|
+
// execute tools in parallel:
|
22
|
+
const toolResults = await Promise.all(toolCalls.map(async (toolCall) => {
|
23
|
+
const tool = tools.find((tool) => tool.name === toolCall.name);
|
24
|
+
if (tool == null) {
|
25
|
+
return {
|
26
|
+
tool: toolCall.name,
|
27
|
+
toolCall,
|
28
|
+
args: toolCall.args,
|
29
|
+
ok: false,
|
30
|
+
result: new ToolCallError({
|
31
|
+
message: `No tool with name '${toolCall.name}' found.`,
|
32
|
+
toolCall,
|
33
|
+
}),
|
34
|
+
};
|
35
|
+
}
|
36
|
+
return await safeExecuteToolCall(tool, toolCall, options);
|
37
|
+
}));
|
38
|
+
return {
|
39
|
+
text,
|
40
|
+
toolResults: toolResults,
|
41
|
+
};
|
42
|
+
},
|
43
|
+
});
|
44
|
+
}
|
package/util/getErrorMessage.cjs
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export declare function getErrorMessage(error: unknown): string;
|
1
|
+
export declare function getErrorMessage(error: unknown | undefined): string;
|
package/util/getErrorMessage.js
CHANGED
@@ -1,17 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.NoSuchStructureError = void 0;
|
4
|
-
class NoSuchStructureError extends Error {
|
5
|
-
constructor(structureName) {
|
6
|
-
super(`No such structure: ${structureName}`);
|
7
|
-
Object.defineProperty(this, "structureName", {
|
8
|
-
enumerable: true,
|
9
|
-
configurable: true,
|
10
|
-
writable: true,
|
11
|
-
value: void 0
|
12
|
-
});
|
13
|
-
this.name = "NoSuchStructureError";
|
14
|
-
this.structureName = structureName;
|
15
|
-
}
|
16
|
-
}
|
17
|
-
exports.NoSuchStructureError = NoSuchStructureError;
|
@@ -1,13 +0,0 @@
|
|
1
|
-
export class NoSuchStructureError extends Error {
|
2
|
-
constructor(structureName) {
|
3
|
-
super(`No such structure: ${structureName}`);
|
4
|
-
Object.defineProperty(this, "structureName", {
|
5
|
-
enumerable: true,
|
6
|
-
configurable: true,
|
7
|
-
writable: true,
|
8
|
-
value: void 0
|
9
|
-
});
|
10
|
-
this.name = "NoSuchStructureError";
|
11
|
-
this.structureName = structureName;
|
12
|
-
}
|
13
|
-
}
|
@@ -1,26 +0,0 @@
|
|
1
|
-
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
-
import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
|
3
|
-
import { Model, ModelSettings } from "../Model.js";
|
4
|
-
export interface StructureOrTextGenerationModelSettings extends ModelSettings {
|
5
|
-
}
|
6
|
-
export interface StructureOrTextGenerationModel<PROMPT, SETTINGS extends StructureOrTextGenerationModelSettings> extends Model<SETTINGS> {
|
7
|
-
doGenerateStructureOrText(structureDefinitions: Array<StructureDefinition<string, unknown>>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
8
|
-
response: unknown;
|
9
|
-
structureAndText: {
|
10
|
-
structure: null;
|
11
|
-
value: null;
|
12
|
-
valueText: null;
|
13
|
-
text: string;
|
14
|
-
} | {
|
15
|
-
structure: string;
|
16
|
-
value: unknown;
|
17
|
-
valueText: string;
|
18
|
-
text: string | null;
|
19
|
-
};
|
20
|
-
usage?: {
|
21
|
-
promptTokens: number;
|
22
|
-
completionTokens: number;
|
23
|
-
totalTokens: number;
|
24
|
-
};
|
25
|
-
}>;
|
26
|
-
}
|
@@ -1,54 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.generateStructureOrText = void 0;
|
4
|
-
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
|
-
const NoSuchStructureError_js_1 = require("./NoSuchStructureError.cjs");
|
6
|
-
const StructureValidationError_js_1 = require("./StructureValidationError.cjs");
|
7
|
-
async function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
8
|
-
// Note: PROMPT must not be a function.
|
9
|
-
const expandedPrompt = typeof prompt === "function"
|
10
|
-
? prompt(structureDefinitions)
|
11
|
-
: prompt;
|
12
|
-
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
13
|
-
functionType: "generate-structure-or-text",
|
14
|
-
input: expandedPrompt,
|
15
|
-
model,
|
16
|
-
options,
|
17
|
-
generateResponse: async (options) => {
|
18
|
-
const result = await model.doGenerateStructureOrText(structureDefinitions, expandedPrompt, options);
|
19
|
-
const { structure, value, text } = result.structureAndText;
|
20
|
-
// text generation:
|
21
|
-
if (structure == null) {
|
22
|
-
return {
|
23
|
-
response: result.response,
|
24
|
-
extractedValue: { structure, value, text },
|
25
|
-
usage: result.usage,
|
26
|
-
};
|
27
|
-
}
|
28
|
-
const definition = structureDefinitions.find((d) => d.name === structure);
|
29
|
-
if (definition == undefined) {
|
30
|
-
throw new NoSuchStructureError_js_1.NoSuchStructureError(structure);
|
31
|
-
}
|
32
|
-
const parseResult = definition.schema.validate(value);
|
33
|
-
if (!parseResult.success) {
|
34
|
-
throw new StructureValidationError_js_1.StructureValidationError({
|
35
|
-
structureName: structure,
|
36
|
-
value,
|
37
|
-
valueText: result.structureAndText.valueText,
|
38
|
-
cause: parseResult.error,
|
39
|
-
});
|
40
|
-
}
|
41
|
-
return {
|
42
|
-
response: result.response,
|
43
|
-
extractedValue: {
|
44
|
-
structure: structure,
|
45
|
-
value: parseResult.data,
|
46
|
-
text: text, // text is string | null, which is part of the response for schema values
|
47
|
-
},
|
48
|
-
usage: result.usage,
|
49
|
-
};
|
50
|
-
},
|
51
|
-
});
|
52
|
-
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
53
|
-
}
|
54
|
-
exports.generateStructureOrText = generateStructureOrText;
|