modelfusion 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -16
- package/composed-function/index.cjs +0 -3
- package/composed-function/index.d.ts +0 -3
- package/composed-function/index.js +0 -3
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +1 -1
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +1 -1
- package/index.cjs +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/model-function/Model.d.ts +2 -2
- package/model-function/ModelCallEvent.d.ts +4 -6
- package/model-function/SuccessfulModelCall.cjs +6 -3
- package/model-function/SuccessfulModelCall.d.ts +3 -3
- package/model-function/SuccessfulModelCall.js +6 -3
- package/model-function/embed-text/embedText.cjs +16 -30
- package/model-function/embed-text/embedText.d.ts +14 -4
- package/model-function/embed-text/embedText.js +16 -30
- package/model-function/executeCall.cjs +6 -6
- package/model-function/executeCall.js +6 -6
- package/model-function/generate-image/generateImage.cjs +7 -20
- package/model-function/generate-image/generateImage.d.ts +7 -2
- package/model-function/generate-image/generateImage.js +7 -20
- package/model-function/generate-json/JsonGenerationEvent.d.ts +2 -2
- package/model-function/generate-json/generateJson.cjs +7 -5
- package/model-function/generate-json/generateJson.d.ts +6 -1
- package/model-function/generate-json/generateJson.js +7 -5
- package/model-function/generate-json/generateJsonOrText.cjs +11 -9
- package/model-function/generate-json/generateJsonOrText.d.ts +10 -1
- package/model-function/generate-json/generateJsonOrText.js +11 -9
- package/model-function/generate-text/generateText.cjs +7 -17
- package/model-function/generate-text/generateText.d.ts +7 -2
- package/model-function/generate-text/generateText.js +7 -17
- package/model-function/generate-text/streamText.cjs +13 -11
- package/model-function/generate-text/streamText.d.ts +9 -1
- package/model-function/generate-text/streamText.js +13 -11
- package/model-function/index.cjs +1 -1
- package/model-function/index.d.ts +1 -1
- package/model-function/index.js +1 -1
- package/model-function/transcribe-audio/transcribe.cjs +7 -19
- package/model-function/transcribe-audio/transcribe.d.ts +7 -2
- package/model-function/transcribe-audio/transcribe.js +7 -19
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +1 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +1 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.js +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +1 -1
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +1 -1
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +1 -1
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +1 -1
- package/model-provider/openai/OpenAIImageGenerationModel.js +1 -1
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +1 -1
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +1 -1
- package/model-provider/openai/OpenAITextEmbeddingModel.js +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.cjs +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.js +1 -1
- package/model-provider/openai/OpenAITranscriptionModel.cjs +1 -1
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
- package/model-provider/openai/OpenAITranscriptionModel.js +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.js +1 -1
- package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +1 -1
- package/model-provider/stability/StabilityImageGenerationModel.cjs +1 -1
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +1 -1
- package/model-provider/stability/StabilityImageGenerationModel.js +1 -1
- package/package.json +1 -1
- package/run/ConsoleLogger.cjs +2 -2
- package/run/ConsoleLogger.d.ts +5 -5
- package/run/ConsoleLogger.js +2 -2
- package/run/DefaultRun.cjs +7 -7
- package/run/DefaultRun.d.ts +6 -6
- package/run/DefaultRun.js +7 -7
- package/run/Run.d.ts +2 -2
- package/run/RunFunction.d.ts +0 -4
- package/run/RunFunctionEvent.d.ts +12 -0
- package/{model-function/ModelCallEventSource.cjs → run/RunFunctionEventSource.cjs} +7 -7
- package/run/RunFunctionEventSource.d.ts +13 -0
- package/{model-function/ModelCallEventSource.js → run/RunFunctionEventSource.js} +5 -5
- package/run/RunFunctionObserver.cjs +2 -0
- package/run/RunFunctionObserver.d.ts +5 -0
- package/run/RunFunctionObserver.js +1 -0
- package/run/index.cjs +3 -0
- package/run/index.d.ts +3 -0
- package/run/index.js +3 -0
- package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.cjs +1 -1
- package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.js +1 -1
- package/text-chunk/upsertTextChunks.cjs +1 -1
- package/text-chunk/upsertTextChunks.js +1 -1
- package/tool/ExecuteToolEvent.cjs +2 -0
- package/tool/ExecuteToolEvent.d.ts +22 -0
- package/tool/ExecuteToolEvent.js +1 -0
- package/{composed-function/use-tool → tool}/Tool.cjs +7 -0
- package/{composed-function/use-tool → tool}/Tool.d.ts +5 -2
- package/{composed-function/use-tool → tool}/Tool.js +7 -0
- package/tool/ToolExecutionError.cjs +31 -0
- package/tool/ToolExecutionError.d.ts +11 -0
- package/tool/ToolExecutionError.js +27 -0
- package/tool/executeTool.cjs +79 -0
- package/tool/executeTool.d.ts +20 -0
- package/tool/executeTool.js +75 -0
- package/tool/index.cjs +22 -0
- package/tool/index.d.ts +6 -0
- package/tool/index.js +6 -0
- package/tool/useTool.cjs +33 -0
- package/tool/useTool.d.ts +15 -0
- package/tool/useTool.js +29 -0
- package/tool/useToolOrGenerateText.cjs +38 -0
- package/{composed-function/use-tool/useTool.d.ts → tool/useToolOrGenerateText.d.ts} +2 -15
- package/tool/useToolOrGenerateText.js +34 -0
- package/composed-function/use-tool/useTool.cjs +0 -59
- package/composed-function/use-tool/useTool.js +0 -54
- package/model-function/ModelCallEventSource.d.ts +0 -13
- package/model-function/ModelCallObserver.d.ts +0 -5
- /package/{model-function/ModelCallObserver.cjs → run/RunFunctionEvent.cjs} +0 -0
- /package/{model-function/ModelCallObserver.js → run/RunFunctionEvent.js} +0 -0
- /package/{composed-function/use-tool → tool}/NoSuchToolError.cjs +0 -0
- /package/{composed-function/use-tool → tool}/NoSuchToolError.d.ts +0 -0
- /package/{composed-function/use-tool → tool}/NoSuchToolError.js +0 -0
package/run/index.d.ts
CHANGED
@@ -3,4 +3,7 @@ export * from "./DefaultRun.js";
|
|
3
3
|
export * from "./IdMetadata.js";
|
4
4
|
export * from "./Run.js";
|
5
5
|
export * from "./RunFunction.js";
|
6
|
+
export * from "./RunFunctionEvent.js";
|
7
|
+
export * from "./RunFunctionObserver.js";
|
8
|
+
export * from "./RunFunctionEventSource.js";
|
6
9
|
export * from "./Vector.js";
|
package/run/index.js
CHANGED
@@ -3,4 +3,7 @@ export * from "./DefaultRun.js";
|
|
3
3
|
export * from "./IdMetadata.js";
|
4
4
|
export * from "./Run.js";
|
5
5
|
export * from "./RunFunction.js";
|
6
|
+
export * from "./RunFunctionEvent.js";
|
7
|
+
export * from "./RunFunctionObserver.js";
|
8
|
+
export * from "./RunFunctionEventSource.js";
|
6
9
|
export * from "./Vector.js";
|
@@ -36,7 +36,7 @@ class SimilarTextChunksFromVectorIndexRetriever {
|
|
36
36
|
run: options.run,
|
37
37
|
});
|
38
38
|
}
|
39
|
-
const
|
39
|
+
const embedding = await (0, embedText_js_1.embedText)(this.embeddingModel, query, {
|
40
40
|
functionId: options?.functionId,
|
41
41
|
run: options?.run,
|
42
42
|
});
|
@@ -33,7 +33,7 @@ export class SimilarTextChunksFromVectorIndexRetriever {
|
|
33
33
|
run: options.run,
|
34
34
|
});
|
35
35
|
}
|
36
|
-
const
|
36
|
+
const embedding = await embedText(this.embeddingModel, query, {
|
37
37
|
functionId: options?.functionId,
|
38
38
|
run: options?.run,
|
39
39
|
});
|
@@ -5,7 +5,7 @@ const nanoid_1 = require("nanoid");
|
|
5
5
|
const embedText_js_1 = require("../model-function/embed-text/embedText.cjs");
|
6
6
|
async function upsertTextChunks({ vectorIndex, embeddingModel, generateId = nanoid_1.nanoid, chunks, ids, }, options) {
|
7
7
|
// many embedding models support bulk embedding, so we first embed all texts:
|
8
|
-
const
|
8
|
+
const embeddings = await (0, embedText_js_1.embedTexts)(embeddingModel, chunks.map((chunk) => chunk.text), options);
|
9
9
|
await vectorIndex.upsertMany(chunks.map((chunk, i) => ({
|
10
10
|
id: ids?.[i] ?? generateId(),
|
11
11
|
vector: embeddings[i],
|
@@ -2,7 +2,7 @@ import { nanoid as createId } from "nanoid";
|
|
2
2
|
import { embedTexts } from "../model-function/embed-text/embedText.js";
|
3
3
|
export async function upsertTextChunks({ vectorIndex, embeddingModel, generateId = createId, chunks, ids, }, options) {
|
4
4
|
// many embedding models support bulk embedding, so we first embed all texts:
|
5
|
-
const
|
5
|
+
const embeddings = await embedTexts(embeddingModel, chunks.map((chunk) => chunk.text), options);
|
6
6
|
await vectorIndex.upsertMany(chunks.map((chunk, i) => ({
|
7
7
|
id: ids?.[i] ?? generateId(),
|
8
8
|
vector: embeddings[i],
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { RunFunctionFinishedEventMetadata, RunFunctionStartedEventMetadata } from "../run/RunFunctionEvent.js";
|
2
|
+
import { Tool } from "./Tool.js";
|
3
|
+
export type ExecuteToolStartedEvent = {
|
4
|
+
type: "execute-tool-started";
|
5
|
+
metadata: RunFunctionStartedEventMetadata;
|
6
|
+
tool: Tool<string, unknown, unknown>;
|
7
|
+
input: unknown;
|
8
|
+
};
|
9
|
+
export type ExecuteToolFinishedEvent = {
|
10
|
+
type: "execute-tool-finished";
|
11
|
+
metadata: RunFunctionFinishedEventMetadata;
|
12
|
+
tool: Tool<string, unknown, unknown>;
|
13
|
+
input: unknown;
|
14
|
+
} & ({
|
15
|
+
status: "success";
|
16
|
+
output: unknown;
|
17
|
+
} | {
|
18
|
+
status: "failure";
|
19
|
+
error: unknown;
|
20
|
+
} | {
|
21
|
+
status: "abort";
|
22
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -21,6 +21,12 @@ class Tool {
|
|
21
21
|
writable: true,
|
22
22
|
value: void 0
|
23
23
|
});
|
24
|
+
Object.defineProperty(this, "outputSchema", {
|
25
|
+
enumerable: true,
|
26
|
+
configurable: true,
|
27
|
+
writable: true,
|
28
|
+
value: void 0
|
29
|
+
});
|
24
30
|
Object.defineProperty(this, "execute", {
|
25
31
|
enumerable: true,
|
26
32
|
configurable: true,
|
@@ -30,6 +36,7 @@ class Tool {
|
|
30
36
|
this.name = options.name;
|
31
37
|
this.description = options.description;
|
32
38
|
this.inputSchema = options.inputSchema;
|
39
|
+
this.outputSchema = options.outputSchema;
|
33
40
|
this.execute = options.execute;
|
34
41
|
}
|
35
42
|
get inputSchemaDefinition() {
|
@@ -1,14 +1,17 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import { SchemaDefinition } from "
|
2
|
+
import { SchemaDefinition } from "../model-function/generate-json/SchemaDefinition.js";
|
3
|
+
import { RunFunction } from "../run/RunFunction.js";
|
3
4
|
export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
4
5
|
readonly name: NAME;
|
5
6
|
readonly description: string;
|
6
7
|
readonly inputSchema: z.ZodSchema<INPUT>;
|
7
|
-
readonly
|
8
|
+
readonly outputSchema?: z.ZodSchema<OUTPUT>;
|
9
|
+
readonly execute: RunFunction<INPUT, OUTPUT>;
|
8
10
|
constructor(options: {
|
9
11
|
name: NAME;
|
10
12
|
description: string;
|
11
13
|
inputSchema: z.ZodSchema<INPUT>;
|
14
|
+
outputSchema?: z.ZodSchema<OUTPUT>;
|
12
15
|
execute(input: INPUT): Promise<OUTPUT>;
|
13
16
|
});
|
14
17
|
get inputSchemaDefinition(): SchemaDefinition<NAME, INPUT>;
|
@@ -18,6 +18,12 @@ export class Tool {
|
|
18
18
|
writable: true,
|
19
19
|
value: void 0
|
20
20
|
});
|
21
|
+
Object.defineProperty(this, "outputSchema", {
|
22
|
+
enumerable: true,
|
23
|
+
configurable: true,
|
24
|
+
writable: true,
|
25
|
+
value: void 0
|
26
|
+
});
|
21
27
|
Object.defineProperty(this, "execute", {
|
22
28
|
enumerable: true,
|
23
29
|
configurable: true,
|
@@ -27,6 +33,7 @@ export class Tool {
|
|
27
33
|
this.name = options.name;
|
28
34
|
this.description = options.description;
|
29
35
|
this.inputSchema = options.inputSchema;
|
36
|
+
this.outputSchema = options.outputSchema;
|
30
37
|
this.execute = options.execute;
|
31
38
|
}
|
32
39
|
get inputSchemaDefinition() {
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ToolExecutionError = void 0;
|
4
|
+
class ToolExecutionError extends Error {
|
5
|
+
constructor({ message = "unknown error", toolName, input, cause, }) {
|
6
|
+
super(`Error executing tool ${toolName}: ${message}`);
|
7
|
+
Object.defineProperty(this, "toolName", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "input", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
Object.defineProperty(this, "cause", {
|
20
|
+
enumerable: true,
|
21
|
+
configurable: true,
|
22
|
+
writable: true,
|
23
|
+
value: void 0
|
24
|
+
});
|
25
|
+
this.name = "ToolExecutionError";
|
26
|
+
this.toolName = toolName;
|
27
|
+
this.input = input;
|
28
|
+
this.cause = cause;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
exports.ToolExecutionError = ToolExecutionError;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export declare class ToolExecutionError extends Error {
|
2
|
+
readonly toolName: string;
|
3
|
+
readonly input: unknown;
|
4
|
+
readonly cause: unknown;
|
5
|
+
constructor({ message, toolName, input, cause, }: {
|
6
|
+
toolName: string;
|
7
|
+
input: unknown;
|
8
|
+
message: string | undefined;
|
9
|
+
cause: unknown | undefined;
|
10
|
+
});
|
11
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
export class ToolExecutionError extends Error {
|
2
|
+
constructor({ message = "unknown error", toolName, input, cause, }) {
|
3
|
+
super(`Error executing tool ${toolName}: ${message}`);
|
4
|
+
Object.defineProperty(this, "toolName", {
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true,
|
8
|
+
value: void 0
|
9
|
+
});
|
10
|
+
Object.defineProperty(this, "input", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
Object.defineProperty(this, "cause", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: void 0
|
21
|
+
});
|
22
|
+
this.name = "ToolExecutionError";
|
23
|
+
this.toolName = toolName;
|
24
|
+
this.input = input;
|
25
|
+
this.cause = cause;
|
26
|
+
}
|
27
|
+
}
|
@@ -0,0 +1,79 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.executeTool = void 0;
|
4
|
+
const nanoid_1 = require("nanoid");
|
5
|
+
const RunFunctionEventSource_js_1 = require("../run/RunFunctionEventSource.cjs");
|
6
|
+
const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
|
7
|
+
const AbortError_js_1 = require("../util/api/AbortError.cjs");
|
8
|
+
const runSafe_js_1 = require("../util/runSafe.cjs");
|
9
|
+
const ToolExecutionError_js_1 = require("./ToolExecutionError.cjs");
|
10
|
+
async function executeTool(tool, input, options) {
|
11
|
+
const run = options?.run;
|
12
|
+
const eventSource = new RunFunctionEventSource_js_1.RunFunctionEventSource({
|
13
|
+
observers: run?.observers ?? [],
|
14
|
+
errorHandler: run?.errorHandler,
|
15
|
+
});
|
16
|
+
const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
|
17
|
+
const startMetadata = {
|
18
|
+
callId: `call-${(0, nanoid_1.nanoid)()}`,
|
19
|
+
runId: run?.runId,
|
20
|
+
sessionId: run?.sessionId,
|
21
|
+
userId: run?.userId,
|
22
|
+
functionId: options?.functionId,
|
23
|
+
startEpochSeconds: durationMeasurement.startEpochSeconds,
|
24
|
+
};
|
25
|
+
eventSource.notifyRunFunctionStarted({
|
26
|
+
type: "execute-tool-started",
|
27
|
+
metadata: startMetadata,
|
28
|
+
tool: tool,
|
29
|
+
input,
|
30
|
+
});
|
31
|
+
const result = await (0, runSafe_js_1.runSafe)(() => tool.execute(input, options));
|
32
|
+
const finishMetadata = {
|
33
|
+
...startMetadata,
|
34
|
+
durationInMs: durationMeasurement.durationInMs,
|
35
|
+
};
|
36
|
+
if (!result.ok) {
|
37
|
+
if (result.isAborted) {
|
38
|
+
eventSource.notifyRunFunctionFinished({
|
39
|
+
type: "execute-tool-finished",
|
40
|
+
status: "abort",
|
41
|
+
metadata: finishMetadata,
|
42
|
+
tool: tool,
|
43
|
+
input,
|
44
|
+
});
|
45
|
+
throw new AbortError_js_1.AbortError();
|
46
|
+
}
|
47
|
+
eventSource.notifyRunFunctionFinished({
|
48
|
+
type: "execute-tool-finished",
|
49
|
+
status: "failure",
|
50
|
+
metadata: finishMetadata,
|
51
|
+
tool: tool,
|
52
|
+
input,
|
53
|
+
error: result.error,
|
54
|
+
});
|
55
|
+
throw new ToolExecutionError_js_1.ToolExecutionError({
|
56
|
+
toolName: tool.name,
|
57
|
+
input,
|
58
|
+
cause: result.error,
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
60
|
+
message: result.error?.message,
|
61
|
+
});
|
62
|
+
}
|
63
|
+
const output = result.output;
|
64
|
+
eventSource.notifyRunFunctionFinished({
|
65
|
+
type: "execute-tool-finished",
|
66
|
+
status: "success",
|
67
|
+
metadata: finishMetadata,
|
68
|
+
tool: tool,
|
69
|
+
input,
|
70
|
+
output,
|
71
|
+
});
|
72
|
+
return options?.fullResponse === true
|
73
|
+
? {
|
74
|
+
output,
|
75
|
+
metadata: finishMetadata,
|
76
|
+
}
|
77
|
+
: output;
|
78
|
+
}
|
79
|
+
exports.executeTool = executeTool;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { FunctionOptions } from "../model-function/FunctionOptions.js";
|
2
|
+
import { Tool } from "./Tool.js";
|
3
|
+
export type ExecuteToolMetadata = {
|
4
|
+
callId: string;
|
5
|
+
runId?: string;
|
6
|
+
sessionId?: string;
|
7
|
+
userId?: string;
|
8
|
+
functionId?: string;
|
9
|
+
startEpochSeconds: number;
|
10
|
+
durationInMs: number;
|
11
|
+
};
|
12
|
+
export declare function executeTool<INPUT, OUTPUT>(tool: Tool<string, INPUT, OUTPUT>, input: INPUT, options: FunctionOptions<undefined> & {
|
13
|
+
fullResponse: true;
|
14
|
+
}): Promise<{
|
15
|
+
output: OUTPUT;
|
16
|
+
metadata: ExecuteToolMetadata;
|
17
|
+
}>;
|
18
|
+
export declare function executeTool<INPUT, OUTPUT>(tool: Tool<string, INPUT, OUTPUT>, input: INPUT, options?: FunctionOptions<undefined> & {
|
19
|
+
fullResponse?: false;
|
20
|
+
}): Promise<OUTPUT>;
|
@@ -0,0 +1,75 @@
|
|
1
|
+
import { nanoid as createId } from "nanoid";
|
2
|
+
import { RunFunctionEventSource } from "../run/RunFunctionEventSource.js";
|
3
|
+
import { startDurationMeasurement } from "../util/DurationMeasurement.js";
|
4
|
+
import { AbortError } from "../util/api/AbortError.js";
|
5
|
+
import { runSafe } from "../util/runSafe.js";
|
6
|
+
import { ToolExecutionError } from "./ToolExecutionError.js";
|
7
|
+
export async function executeTool(tool, input, options) {
|
8
|
+
const run = options?.run;
|
9
|
+
const eventSource = new RunFunctionEventSource({
|
10
|
+
observers: run?.observers ?? [],
|
11
|
+
errorHandler: run?.errorHandler,
|
12
|
+
});
|
13
|
+
const durationMeasurement = startDurationMeasurement();
|
14
|
+
const startMetadata = {
|
15
|
+
callId: `call-${createId()}`,
|
16
|
+
runId: run?.runId,
|
17
|
+
sessionId: run?.sessionId,
|
18
|
+
userId: run?.userId,
|
19
|
+
functionId: options?.functionId,
|
20
|
+
startEpochSeconds: durationMeasurement.startEpochSeconds,
|
21
|
+
};
|
22
|
+
eventSource.notifyRunFunctionStarted({
|
23
|
+
type: "execute-tool-started",
|
24
|
+
metadata: startMetadata,
|
25
|
+
tool: tool,
|
26
|
+
input,
|
27
|
+
});
|
28
|
+
const result = await runSafe(() => tool.execute(input, options));
|
29
|
+
const finishMetadata = {
|
30
|
+
...startMetadata,
|
31
|
+
durationInMs: durationMeasurement.durationInMs,
|
32
|
+
};
|
33
|
+
if (!result.ok) {
|
34
|
+
if (result.isAborted) {
|
35
|
+
eventSource.notifyRunFunctionFinished({
|
36
|
+
type: "execute-tool-finished",
|
37
|
+
status: "abort",
|
38
|
+
metadata: finishMetadata,
|
39
|
+
tool: tool,
|
40
|
+
input,
|
41
|
+
});
|
42
|
+
throw new AbortError();
|
43
|
+
}
|
44
|
+
eventSource.notifyRunFunctionFinished({
|
45
|
+
type: "execute-tool-finished",
|
46
|
+
status: "failure",
|
47
|
+
metadata: finishMetadata,
|
48
|
+
tool: tool,
|
49
|
+
input,
|
50
|
+
error: result.error,
|
51
|
+
});
|
52
|
+
throw new ToolExecutionError({
|
53
|
+
toolName: tool.name,
|
54
|
+
input,
|
55
|
+
cause: result.error,
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
57
|
+
message: result.error?.message,
|
58
|
+
});
|
59
|
+
}
|
60
|
+
const output = result.output;
|
61
|
+
eventSource.notifyRunFunctionFinished({
|
62
|
+
type: "execute-tool-finished",
|
63
|
+
status: "success",
|
64
|
+
metadata: finishMetadata,
|
65
|
+
tool: tool,
|
66
|
+
input,
|
67
|
+
output,
|
68
|
+
});
|
69
|
+
return options?.fullResponse === true
|
70
|
+
? {
|
71
|
+
output,
|
72
|
+
metadata: finishMetadata,
|
73
|
+
}
|
74
|
+
: output;
|
75
|
+
}
|
package/tool/index.cjs
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./NoSuchToolError.cjs"), exports);
|
18
|
+
__exportStar(require("./Tool.cjs"), exports);
|
19
|
+
__exportStar(require("./ToolExecutionError.cjs"), exports);
|
20
|
+
__exportStar(require("./executeTool.cjs"), exports);
|
21
|
+
__exportStar(require("./useTool.cjs"), exports);
|
22
|
+
__exportStar(require("./useToolOrGenerateText.cjs"), exports);
|
package/tool/index.d.ts
ADDED
package/tool/index.js
ADDED
package/tool/useTool.cjs
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useTool = void 0;
|
4
|
+
const generateJson_js_1 = require("../model-function/generate-json/generateJson.cjs");
|
5
|
+
const executeTool_js_1 = require("./executeTool.cjs");
|
6
|
+
// In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
|
7
|
+
// retrieved through lookups such as TOOL["name"], such that any does not affect any client.
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
9
|
+
/**
|
10
|
+
* `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
|
11
|
+
*
|
12
|
+
* @returns The result contains the name of the tool (`tool` property),
|
13
|
+
* the parameters (`parameters` property, typed),
|
14
|
+
* and the result of the tool execution (`result` property, typed).
|
15
|
+
*/
|
16
|
+
async function useTool(model, tool, prompt, options) {
|
17
|
+
const { value } = await (0, generateJson_js_1.generateJson)(model, {
|
18
|
+
name: tool.name,
|
19
|
+
description: tool.description,
|
20
|
+
schema: tool.inputSchema,
|
21
|
+
}, () => prompt(tool), {
|
22
|
+
...(options ?? {}),
|
23
|
+
fullResponse: true,
|
24
|
+
});
|
25
|
+
return {
|
26
|
+
tool: tool.name,
|
27
|
+
parameters: value,
|
28
|
+
result: await (0, executeTool_js_1.executeTool)(tool, value, {
|
29
|
+
run: options?.run,
|
30
|
+
}),
|
31
|
+
};
|
32
|
+
}
|
33
|
+
exports.useTool = useTool;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { FunctionOptions } from "../model-function/FunctionOptions.js";
|
2
|
+
import { GenerateJsonModel, GenerateJsonModelSettings, GenerateJsonPrompt } from "../model-function/generate-json/GenerateJsonModel.js";
|
3
|
+
import { Tool } from "./Tool.js";
|
4
|
+
/**
|
5
|
+
* `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
|
6
|
+
*
|
7
|
+
* @returns The result contains the name of the tool (`tool` property),
|
8
|
+
* the parameters (`parameters` property, typed),
|
9
|
+
* and the result of the tool execution (`result` property, typed).
|
10
|
+
*/
|
11
|
+
export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT & GenerateJsonPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): Promise<{
|
12
|
+
tool: TOOL["name"];
|
13
|
+
parameters: TOOL["inputSchema"];
|
14
|
+
result: Awaited<ReturnType<TOOL["execute"]>>;
|
15
|
+
}>;
|
package/tool/useTool.js
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
import { generateJson } from "../model-function/generate-json/generateJson.js";
|
2
|
+
import { executeTool } from "./executeTool.js";
|
3
|
+
// In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
|
4
|
+
// retrieved through lookups such as TOOL["name"], such that any does not affect any client.
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
6
|
+
/**
|
7
|
+
* `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
|
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
|
+
export async function useTool(model, tool, prompt, options) {
|
14
|
+
const { value } = await generateJson(model, {
|
15
|
+
name: tool.name,
|
16
|
+
description: tool.description,
|
17
|
+
schema: tool.inputSchema,
|
18
|
+
}, () => prompt(tool), {
|
19
|
+
...(options ?? {}),
|
20
|
+
fullResponse: true,
|
21
|
+
});
|
22
|
+
return {
|
23
|
+
tool: tool.name,
|
24
|
+
parameters: value,
|
25
|
+
result: await executeTool(tool, value, {
|
26
|
+
run: options?.run,
|
27
|
+
}),
|
28
|
+
};
|
29
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useToolOrGenerateText = void 0;
|
4
|
+
const generateJsonOrText_js_1 = require("../model-function/generate-json/generateJsonOrText.cjs");
|
5
|
+
const NoSuchToolError_js_1 = require("./NoSuchToolError.cjs");
|
6
|
+
const executeTool_js_1 = require("./executeTool.cjs");
|
7
|
+
async function useToolOrGenerateText(model, tools, prompt, options) {
|
8
|
+
const expandedPrompt = prompt(tools);
|
9
|
+
const modelResponse = await (0, generateJsonOrText_js_1.generateJsonOrText)(model, tools.map((tool) => ({
|
10
|
+
name: tool.name,
|
11
|
+
description: tool.description,
|
12
|
+
schema: tool.inputSchema,
|
13
|
+
})), () => expandedPrompt, options);
|
14
|
+
const { schema, text } = modelResponse;
|
15
|
+
if (schema == null) {
|
16
|
+
return {
|
17
|
+
tool: null,
|
18
|
+
parameters: null,
|
19
|
+
result: null,
|
20
|
+
text,
|
21
|
+
};
|
22
|
+
}
|
23
|
+
const tool = tools.find((tool) => tool.name === schema);
|
24
|
+
if (tool == null) {
|
25
|
+
throw new NoSuchToolError_js_1.NoSuchToolError(schema.toString());
|
26
|
+
}
|
27
|
+
const toolParameters = modelResponse.value;
|
28
|
+
const result = await (0, executeTool_js_1.executeTool)(tool, toolParameters, {
|
29
|
+
run: options?.run,
|
30
|
+
});
|
31
|
+
return {
|
32
|
+
tool: schema,
|
33
|
+
result,
|
34
|
+
parameters: toolParameters,
|
35
|
+
text: text, // string | null is the expected value here
|
36
|
+
};
|
37
|
+
}
|
38
|
+
exports.useToolOrGenerateText = useToolOrGenerateText;
|
@@ -1,19 +1,6 @@
|
|
1
|
-
import { FunctionOptions } from "
|
2
|
-
import {
|
3
|
-
import { GenerateJsonOrTextModel, GenerateJsonOrTextModelSettings, GenerateJsonOrTextPrompt } from "../../model-function/generate-json/GenerateJsonOrTextModel.js";
|
1
|
+
import { FunctionOptions } from "../model-function/FunctionOptions.js";
|
2
|
+
import { GenerateJsonOrTextModel, GenerateJsonOrTextModelSettings, GenerateJsonOrTextPrompt } from "../model-function/generate-json/GenerateJsonOrTextModel.js";
|
4
3
|
import { Tool } from "./Tool.js";
|
5
|
-
/**
|
6
|
-
* `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
|
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
|
-
export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT & GenerateJsonPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): Promise<{
|
13
|
-
tool: TOOL["name"];
|
14
|
-
parameters: TOOL["inputSchema"];
|
15
|
-
result: Awaited<ReturnType<TOOL["execute"]>>;
|
16
|
-
}>;
|
17
4
|
type ToolArray<T extends Tool<any, any, any>[]> = T;
|
18
5
|
type ToToolMap<T extends ToolArray<Tool<any, any, any>[]>> = {
|
19
6
|
[K in T[number]["name"]]: Extract<T[number], Tool<K, any, any>>;
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { generateJsonOrText } from "../model-function/generate-json/generateJsonOrText.js";
|
2
|
+
import { NoSuchToolError } from "./NoSuchToolError.js";
|
3
|
+
import { executeTool } from "./executeTool.js";
|
4
|
+
export async function useToolOrGenerateText(model, tools, prompt, options) {
|
5
|
+
const expandedPrompt = prompt(tools);
|
6
|
+
const modelResponse = await generateJsonOrText(model, tools.map((tool) => ({
|
7
|
+
name: tool.name,
|
8
|
+
description: tool.description,
|
9
|
+
schema: tool.inputSchema,
|
10
|
+
})), () => expandedPrompt, options);
|
11
|
+
const { schema, text } = modelResponse;
|
12
|
+
if (schema == null) {
|
13
|
+
return {
|
14
|
+
tool: null,
|
15
|
+
parameters: null,
|
16
|
+
result: null,
|
17
|
+
text,
|
18
|
+
};
|
19
|
+
}
|
20
|
+
const tool = tools.find((tool) => tool.name === schema);
|
21
|
+
if (tool == null) {
|
22
|
+
throw new NoSuchToolError(schema.toString());
|
23
|
+
}
|
24
|
+
const toolParameters = modelResponse.value;
|
25
|
+
const result = await executeTool(tool, toolParameters, {
|
26
|
+
run: options?.run,
|
27
|
+
});
|
28
|
+
return {
|
29
|
+
tool: schema,
|
30
|
+
result,
|
31
|
+
parameters: toolParameters,
|
32
|
+
text: text, // string | null is the expected value here
|
33
|
+
};
|
34
|
+
}
|