modelfusion 0.7.0 → 0.9.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 +14 -4
- package/composed-function/index.cjs +0 -3
- package/composed-function/index.d.ts +0 -3
- package/composed-function/index.js +0 -3
- 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/executeCall.cjs +6 -6
- package/model-function/executeCall.js +6 -6
- package/model-function/generate-json/JsonGenerationEvent.d.ts +2 -2
- package/model-function/generate-json/generateJsonOrText.cjs +4 -4
- package/model-function/generate-json/generateJsonOrText.js +4 -4
- package/model-function/generate-text/streamText.cjs +7 -7
- package/model-function/generate-text/streamText.js +7 -7
- package/model-function/index.cjs +1 -1
- package/model-function/index.d.ts +1 -1
- package/model-function/index.js +1 -1
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +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/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 +31 -0
- package/tool/Tool.d.ts +42 -0
- package/tool/Tool.js +70 -0
- package/tool/ToolExecutionError.cjs +31 -0
- package/tool/ToolExecutionError.d.ts +11 -0
- package/tool/ToolExecutionError.js +27 -0
- package/tool/WebSearchTool.cjs +43 -0
- package/tool/WebSearchTool.d.ts +81 -0
- package/tool/WebSearchTool.js +39 -0
- package/tool/executeTool.cjs +79 -0
- package/tool/executeTool.d.ts +23 -0
- package/tool/executeTool.js +75 -0
- package/tool/index.cjs +23 -0
- package/tool/index.d.ts +7 -0
- package/tool/index.js +7 -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/Tool.d.ts +0 -15
- package/composed-function/use-tool/Tool.js +0 -39
- package/composed-function/use-tool/useTool.cjs +0 -62
- package/composed-function/use-tool/useTool.js +0 -57
- 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/DefaultRun.cjs
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.DefaultRun = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
|
-
const SuccessfulModelCall_js_1 = require("../model-function/SuccessfulModelCall.cjs");
|
6
5
|
const calculateCost_js_1 = require("../cost/calculateCost.cjs");
|
6
|
+
const SuccessfulModelCall_js_1 = require("../model-function/SuccessfulModelCall.cjs");
|
7
7
|
class DefaultRun {
|
8
8
|
constructor({ runId = (0, nanoid_1.nanoid)(), sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
|
9
9
|
Object.defineProperty(this, "runId", {
|
@@ -36,7 +36,7 @@ class DefaultRun {
|
|
36
36
|
writable: true,
|
37
37
|
value: void 0
|
38
38
|
});
|
39
|
-
Object.defineProperty(this, "
|
39
|
+
Object.defineProperty(this, "events", {
|
40
40
|
enumerable: true,
|
41
41
|
configurable: true,
|
42
42
|
writable: true,
|
@@ -55,18 +55,18 @@ class DefaultRun {
|
|
55
55
|
this.costCalculators = costCalculators;
|
56
56
|
this.observers = [
|
57
57
|
{
|
58
|
-
|
59
|
-
this.
|
58
|
+
onRunFunctionStarted: (event) => {
|
59
|
+
this.events.push(event);
|
60
60
|
},
|
61
|
-
|
62
|
-
this.
|
61
|
+
onRunFunctionFinished: (event) => {
|
62
|
+
this.events.push(event);
|
63
63
|
},
|
64
64
|
},
|
65
65
|
...(observers ?? []),
|
66
66
|
];
|
67
67
|
}
|
68
68
|
get successfulModelCalls() {
|
69
|
-
return (0, SuccessfulModelCall_js_1.extractSuccessfulModelCalls)(this.
|
69
|
+
return (0, SuccessfulModelCall_js_1.extractSuccessfulModelCalls)(this.events);
|
70
70
|
}
|
71
71
|
calculateCost() {
|
72
72
|
return (0, calculateCost_js_1.calculateCost)({
|
package/run/DefaultRun.d.ts
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
+
import { CostCalculator } from "../cost/CostCalculator.js";
|
1
2
|
import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
|
2
|
-
import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
|
3
|
-
import { ModelCallObserver } from "../model-function/ModelCallObserver.js";
|
4
3
|
import { Run } from "./Run.js";
|
5
|
-
import {
|
4
|
+
import { RunFunctionEvent } from "./RunFunctionEvent.js";
|
5
|
+
import { RunFunctionObserver } from "./RunFunctionObserver.js";
|
6
6
|
export declare class DefaultRun implements Run {
|
7
7
|
readonly runId: string;
|
8
8
|
readonly sessionId?: string;
|
9
9
|
readonly userId?: string;
|
10
10
|
readonly abortSignal?: AbortSignal;
|
11
11
|
readonly costCalculators: CostCalculator[];
|
12
|
-
readonly
|
13
|
-
readonly observers?:
|
12
|
+
readonly events: RunFunctionEvent[];
|
13
|
+
readonly observers?: RunFunctionObserver[];
|
14
14
|
constructor({ runId, sessionId, userId, abortSignal, observers, costCalculators, }?: {
|
15
15
|
runId?: string;
|
16
16
|
sessionId?: string;
|
17
17
|
userId?: string;
|
18
18
|
abortSignal?: AbortSignal;
|
19
|
-
observers?:
|
19
|
+
observers?: RunFunctionObserver[];
|
20
20
|
costCalculators?: CostCalculator[];
|
21
21
|
});
|
22
22
|
get successfulModelCalls(): Array<SuccessfulModelCall>;
|
package/run/DefaultRun.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { nanoid as createId } from "nanoid";
|
2
|
-
import { extractSuccessfulModelCalls, } from "../model-function/SuccessfulModelCall.js";
|
3
2
|
import { calculateCost } from "../cost/calculateCost.js";
|
3
|
+
import { extractSuccessfulModelCalls, } from "../model-function/SuccessfulModelCall.js";
|
4
4
|
export class DefaultRun {
|
5
5
|
constructor({ runId = createId(), sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
|
6
6
|
Object.defineProperty(this, "runId", {
|
@@ -33,7 +33,7 @@ export class DefaultRun {
|
|
33
33
|
writable: true,
|
34
34
|
value: void 0
|
35
35
|
});
|
36
|
-
Object.defineProperty(this, "
|
36
|
+
Object.defineProperty(this, "events", {
|
37
37
|
enumerable: true,
|
38
38
|
configurable: true,
|
39
39
|
writable: true,
|
@@ -52,18 +52,18 @@ export class DefaultRun {
|
|
52
52
|
this.costCalculators = costCalculators;
|
53
53
|
this.observers = [
|
54
54
|
{
|
55
|
-
|
56
|
-
this.
|
55
|
+
onRunFunctionStarted: (event) => {
|
56
|
+
this.events.push(event);
|
57
57
|
},
|
58
|
-
|
59
|
-
this.
|
58
|
+
onRunFunctionFinished: (event) => {
|
59
|
+
this.events.push(event);
|
60
60
|
},
|
61
61
|
},
|
62
62
|
...(observers ?? []),
|
63
63
|
];
|
64
64
|
}
|
65
65
|
get successfulModelCalls() {
|
66
|
-
return extractSuccessfulModelCalls(this.
|
66
|
+
return extractSuccessfulModelCalls(this.events);
|
67
67
|
}
|
68
68
|
calculateCost() {
|
69
69
|
return calculateCost({
|
package/run/Run.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { ErrorHandler } from "../util/ErrorHandler.js";
|
2
|
-
import {
|
2
|
+
import { RunFunctionObserver } from "./RunFunctionObserver.js";
|
3
3
|
export interface Run {
|
4
4
|
/**
|
5
5
|
* Unique identifier for a specific run. Primarily utilized for efficient referencing
|
@@ -22,6 +22,6 @@ export interface Run {
|
|
22
22
|
* to the run.
|
23
23
|
*/
|
24
24
|
abortSignal?: AbortSignal;
|
25
|
-
observers?:
|
25
|
+
observers?: RunFunctionObserver[];
|
26
26
|
errorHandler?: ErrorHandler;
|
27
27
|
}
|
package/run/RunFunction.d.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import { SafeResult } from "../util/SafeResult.js";
|
2
1
|
import { Run } from "./Run.js";
|
3
2
|
/**
|
4
3
|
* Standardized function signature for functions that are part of a run.
|
@@ -8,6 +7,3 @@ import { Run } from "./Run.js";
|
|
8
7
|
export type RunFunction<INPUT, OUTPUT> = (input: INPUT, options?: {
|
9
8
|
run?: Run;
|
10
9
|
}) => PromiseLike<OUTPUT>;
|
11
|
-
export type SafeRunFunction<INPUT, OUTPUT> = (input: INPUT, options?: {
|
12
|
-
run?: Run;
|
13
|
-
}) => PromiseLike<SafeResult<OUTPUT>>;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "tool/ExecuteToolEvent.js";
|
2
|
+
import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
|
3
|
+
import { IdMetadata } from "./IdMetadata.js";
|
4
|
+
export type RunFunctionEvent = RunFunctionStartedEvent | RunFunctionFinishedEvent;
|
5
|
+
export type RunFunctionStartedEventMetadata = IdMetadata & {
|
6
|
+
startEpochSeconds: number;
|
7
|
+
};
|
8
|
+
export type RunFunctionStartedEvent = ModelCallStartedEvent | ExecuteToolStartedEvent;
|
9
|
+
export type RunFunctionFinishedEventMetadata = RunFunctionStartedEventMetadata & {
|
10
|
+
durationInMs: number;
|
11
|
+
};
|
12
|
+
export type RunFunctionFinishedEvent = ModelCallFinishedEvent | ExecuteToolFinishedEvent;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
class
|
3
|
+
exports.RunFunctionEventSource = void 0;
|
4
|
+
class RunFunctionEventSource {
|
5
5
|
constructor({ observers, errorHandler, }) {
|
6
6
|
Object.defineProperty(this, "observers", {
|
7
7
|
enumerable: true,
|
@@ -18,20 +18,20 @@ class ModelCallEventSource {
|
|
18
18
|
this.observers = observers;
|
19
19
|
this.errorHandler = errorHandler ?? ((error) => console.error(error));
|
20
20
|
}
|
21
|
-
|
21
|
+
notifyRunFunctionStarted(event) {
|
22
22
|
for (const observer of this.observers) {
|
23
23
|
try {
|
24
|
-
observer.
|
24
|
+
observer.onRunFunctionStarted?.(event);
|
25
25
|
}
|
26
26
|
catch (error) {
|
27
27
|
this.errorHandler(error);
|
28
28
|
}
|
29
29
|
}
|
30
30
|
}
|
31
|
-
|
31
|
+
notifyRunFunctionFinished(event) {
|
32
32
|
for (const observer of this.observers) {
|
33
33
|
try {
|
34
|
-
observer.
|
34
|
+
observer.onRunFunctionFinished?.(event);
|
35
35
|
}
|
36
36
|
catch (error) {
|
37
37
|
this.errorHandler(error);
|
@@ -39,4 +39,4 @@ class ModelCallEventSource {
|
|
39
39
|
}
|
40
40
|
}
|
41
41
|
}
|
42
|
-
exports.
|
42
|
+
exports.RunFunctionEventSource = RunFunctionEventSource;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { ErrorHandler } from "../util/ErrorHandler.js";
|
2
|
+
import { RunFunctionFinishedEvent, RunFunctionStartedEvent } from "./RunFunctionEvent.js";
|
3
|
+
import { RunFunctionObserver } from "./RunFunctionObserver.js";
|
4
|
+
export declare class RunFunctionEventSource {
|
5
|
+
readonly observers: RunFunctionObserver[];
|
6
|
+
readonly errorHandler: ErrorHandler;
|
7
|
+
constructor({ observers, errorHandler, }: {
|
8
|
+
observers: RunFunctionObserver[];
|
9
|
+
errorHandler?: ErrorHandler;
|
10
|
+
});
|
11
|
+
notifyRunFunctionStarted(event: RunFunctionStartedEvent): void;
|
12
|
+
notifyRunFunctionFinished(event: RunFunctionFinishedEvent): void;
|
13
|
+
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
export class
|
1
|
+
export class RunFunctionEventSource {
|
2
2
|
constructor({ observers, errorHandler, }) {
|
3
3
|
Object.defineProperty(this, "observers", {
|
4
4
|
enumerable: true,
|
@@ -15,20 +15,20 @@ export class ModelCallEventSource {
|
|
15
15
|
this.observers = observers;
|
16
16
|
this.errorHandler = errorHandler ?? ((error) => console.error(error));
|
17
17
|
}
|
18
|
-
|
18
|
+
notifyRunFunctionStarted(event) {
|
19
19
|
for (const observer of this.observers) {
|
20
20
|
try {
|
21
|
-
observer.
|
21
|
+
observer.onRunFunctionStarted?.(event);
|
22
22
|
}
|
23
23
|
catch (error) {
|
24
24
|
this.errorHandler(error);
|
25
25
|
}
|
26
26
|
}
|
27
27
|
}
|
28
|
-
|
28
|
+
notifyRunFunctionFinished(event) {
|
29
29
|
for (const observer of this.observers) {
|
30
30
|
try {
|
31
|
-
observer.
|
31
|
+
observer.onRunFunctionFinished?.(event);
|
32
32
|
}
|
33
33
|
catch (error) {
|
34
34
|
this.errorHandler(error);
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/run/index.cjs
CHANGED
@@ -19,4 +19,7 @@ __exportStar(require("./DefaultRun.cjs"), exports);
|
|
19
19
|
__exportStar(require("./IdMetadata.cjs"), exports);
|
20
20
|
__exportStar(require("./Run.cjs"), exports);
|
21
21
|
__exportStar(require("./RunFunction.cjs"), exports);
|
22
|
+
__exportStar(require("./RunFunctionEvent.cjs"), exports);
|
23
|
+
__exportStar(require("./RunFunctionObserver.cjs"), exports);
|
24
|
+
__exportStar(require("./RunFunctionEventSource.cjs"), exports);
|
22
25
|
__exportStar(require("./Vector.cjs"), exports);
|
package/run/index.d.ts
CHANGED
@@ -3,4 +3,7 @@ export * from "./DefaultRun.js";
|
|
3
3
|
export * from "./IdMetadata.js";
|
4
4
|
export * from "./Run.js";
|
5
5
|
export * from "./RunFunction.js";
|
6
|
+
export * from "./RunFunctionEvent.js";
|
7
|
+
export * from "./RunFunctionObserver.js";
|
8
|
+
export * from "./RunFunctionEventSource.js";
|
6
9
|
export * from "./Vector.js";
|
package/run/index.js
CHANGED
@@ -3,4 +3,7 @@ export * from "./DefaultRun.js";
|
|
3
3
|
export * from "./IdMetadata.js";
|
4
4
|
export * from "./Run.js";
|
5
5
|
export * from "./RunFunction.js";
|
6
|
+
export * from "./RunFunctionEvent.js";
|
7
|
+
export * from "./RunFunctionObserver.js";
|
8
|
+
export * from "./RunFunctionEventSource.js";
|
6
9
|
export * from "./Vector.js";
|
@@ -0,0 +1,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 {};
|
@@ -1,26 +1,52 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.Tool = void 0;
|
4
|
+
/**
|
5
|
+
* A tool is a function with a name, description and defined inputs that can be used
|
6
|
+
* by agents and chatbots.
|
7
|
+
*/
|
4
8
|
class Tool {
|
5
9
|
constructor(options) {
|
10
|
+
/**
|
11
|
+
* The name of the tool. Should be understandable for language models and unique among the tools that they know.
|
12
|
+
*/
|
6
13
|
Object.defineProperty(this, "name", {
|
7
14
|
enumerable: true,
|
8
15
|
configurable: true,
|
9
16
|
writable: true,
|
10
17
|
value: void 0
|
11
18
|
});
|
19
|
+
/**
|
20
|
+
* A description of what the tool does. Will be used by the language model to decide whether to use the tool.
|
21
|
+
*/
|
12
22
|
Object.defineProperty(this, "description", {
|
13
23
|
enumerable: true,
|
14
24
|
configurable: true,
|
15
25
|
writable: true,
|
16
26
|
value: void 0
|
17
27
|
});
|
28
|
+
/**
|
29
|
+
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
30
|
+
* Use descriptions to make the input understandable for the language model.
|
31
|
+
*/
|
18
32
|
Object.defineProperty(this, "inputSchema", {
|
19
33
|
enumerable: true,
|
20
34
|
configurable: true,
|
21
35
|
writable: true,
|
22
36
|
value: void 0
|
23
37
|
});
|
38
|
+
/**
|
39
|
+
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
40
|
+
*/
|
41
|
+
Object.defineProperty(this, "outputSchema", {
|
42
|
+
enumerable: true,
|
43
|
+
configurable: true,
|
44
|
+
writable: true,
|
45
|
+
value: void 0
|
46
|
+
});
|
47
|
+
/**
|
48
|
+
* The actual execution function of the tool.
|
49
|
+
*/
|
24
50
|
Object.defineProperty(this, "execute", {
|
25
51
|
enumerable: true,
|
26
52
|
configurable: true,
|
@@ -30,8 +56,13 @@ class Tool {
|
|
30
56
|
this.name = options.name;
|
31
57
|
this.description = options.description;
|
32
58
|
this.inputSchema = options.inputSchema;
|
59
|
+
this.outputSchema = options.outputSchema;
|
33
60
|
this.execute = options.execute;
|
34
61
|
}
|
62
|
+
/**
|
63
|
+
* Provdes a schema definition with the name, description and schema of the input.
|
64
|
+
* This is used by `useTool`.
|
65
|
+
*/
|
35
66
|
get inputSchemaDefinition() {
|
36
67
|
return {
|
37
68
|
name: this.name,
|
package/tool/Tool.d.ts
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { SchemaDefinition } from "../model-function/generate-json/SchemaDefinition.js";
|
3
|
+
import { RunFunction } from "../run/RunFunction.js";
|
4
|
+
/**
|
5
|
+
* A tool is a function with a name, description and defined inputs that can be used
|
6
|
+
* by agents and chatbots.
|
7
|
+
*/
|
8
|
+
export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
9
|
+
/**
|
10
|
+
* The name of the tool. Should be understandable for language models and unique among the tools that they know.
|
11
|
+
*/
|
12
|
+
readonly name: NAME;
|
13
|
+
/**
|
14
|
+
* A description of what the tool does. Will be used by the language model to decide whether to use the tool.
|
15
|
+
*/
|
16
|
+
readonly description: string;
|
17
|
+
/**
|
18
|
+
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
19
|
+
* Use descriptions to make the input understandable for the language model.
|
20
|
+
*/
|
21
|
+
readonly inputSchema: z.ZodSchema<INPUT>;
|
22
|
+
/**
|
23
|
+
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
24
|
+
*/
|
25
|
+
readonly outputSchema?: z.ZodSchema<OUTPUT>;
|
26
|
+
/**
|
27
|
+
* The actual execution function of the tool.
|
28
|
+
*/
|
29
|
+
readonly execute: RunFunction<INPUT, OUTPUT>;
|
30
|
+
constructor(options: {
|
31
|
+
name: NAME;
|
32
|
+
description: string;
|
33
|
+
inputSchema: z.ZodSchema<INPUT>;
|
34
|
+
outputSchema?: z.ZodSchema<OUTPUT>;
|
35
|
+
execute(input: INPUT): Promise<OUTPUT>;
|
36
|
+
});
|
37
|
+
/**
|
38
|
+
* Provdes a schema definition with the name, description and schema of the input.
|
39
|
+
* This is used by `useTool`.
|
40
|
+
*/
|
41
|
+
get inputSchemaDefinition(): SchemaDefinition<NAME, INPUT>;
|
42
|
+
}
|
package/tool/Tool.js
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
/**
|
2
|
+
* A tool is a function with a name, description and defined inputs that can be used
|
3
|
+
* by agents and chatbots.
|
4
|
+
*/
|
5
|
+
export class Tool {
|
6
|
+
constructor(options) {
|
7
|
+
/**
|
8
|
+
* The name of the tool. Should be understandable for language models and unique among the tools that they know.
|
9
|
+
*/
|
10
|
+
Object.defineProperty(this, "name", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
/**
|
17
|
+
* A description of what the tool does. Will be used by the language model to decide whether to use the tool.
|
18
|
+
*/
|
19
|
+
Object.defineProperty(this, "description", {
|
20
|
+
enumerable: true,
|
21
|
+
configurable: true,
|
22
|
+
writable: true,
|
23
|
+
value: void 0
|
24
|
+
});
|
25
|
+
/**
|
26
|
+
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
27
|
+
* Use descriptions to make the input understandable for the language model.
|
28
|
+
*/
|
29
|
+
Object.defineProperty(this, "inputSchema", {
|
30
|
+
enumerable: true,
|
31
|
+
configurable: true,
|
32
|
+
writable: true,
|
33
|
+
value: void 0
|
34
|
+
});
|
35
|
+
/**
|
36
|
+
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
37
|
+
*/
|
38
|
+
Object.defineProperty(this, "outputSchema", {
|
39
|
+
enumerable: true,
|
40
|
+
configurable: true,
|
41
|
+
writable: true,
|
42
|
+
value: void 0
|
43
|
+
});
|
44
|
+
/**
|
45
|
+
* The actual execution function of the tool.
|
46
|
+
*/
|
47
|
+
Object.defineProperty(this, "execute", {
|
48
|
+
enumerable: true,
|
49
|
+
configurable: true,
|
50
|
+
writable: true,
|
51
|
+
value: void 0
|
52
|
+
});
|
53
|
+
this.name = options.name;
|
54
|
+
this.description = options.description;
|
55
|
+
this.inputSchema = options.inputSchema;
|
56
|
+
this.outputSchema = options.outputSchema;
|
57
|
+
this.execute = options.execute;
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Provdes a schema definition with the name, description and schema of the input.
|
61
|
+
* This is used by `useTool`.
|
62
|
+
*/
|
63
|
+
get inputSchemaDefinition() {
|
64
|
+
return {
|
65
|
+
name: this.name,
|
66
|
+
description: this.description,
|
67
|
+
schema: this.inputSchema,
|
68
|
+
};
|
69
|
+
}
|
70
|
+
}
|
@@ -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,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.WebSearchTool = void 0;
|
4
|
+
const zod_1 = require("zod");
|
5
|
+
const Tool_js_1 = require("./Tool.cjs");
|
6
|
+
const INPUT_SCHEMA = zod_1.z.object({
|
7
|
+
query: zod_1.z.string(),
|
8
|
+
});
|
9
|
+
const OUTPUT_SCHEMA = zod_1.z.object({
|
10
|
+
results: zod_1.z.array(zod_1.z.object({
|
11
|
+
title: zod_1.z.string(),
|
12
|
+
link: zod_1.z.string().url(),
|
13
|
+
snippet: zod_1.z.string(),
|
14
|
+
})),
|
15
|
+
});
|
16
|
+
/**
|
17
|
+
* @see https://serpapi.com/search-api
|
18
|
+
*/
|
19
|
+
class WebSearchTool extends Tool_js_1.Tool {
|
20
|
+
constructor(options) {
|
21
|
+
super({
|
22
|
+
name: options.name,
|
23
|
+
description: options.description,
|
24
|
+
inputSchema: INPUT_SCHEMA,
|
25
|
+
outputSchema: OUTPUT_SCHEMA,
|
26
|
+
execute: options.execute,
|
27
|
+
});
|
28
|
+
}
|
29
|
+
}
|
30
|
+
exports.WebSearchTool = WebSearchTool;
|
31
|
+
// expose the schemas to library consumers:
|
32
|
+
Object.defineProperty(WebSearchTool, "INPUT_SCHEMA", {
|
33
|
+
enumerable: true,
|
34
|
+
configurable: true,
|
35
|
+
writable: true,
|
36
|
+
value: INPUT_SCHEMA
|
37
|
+
});
|
38
|
+
Object.defineProperty(WebSearchTool, "OUTPUT_SCHEMA", {
|
39
|
+
enumerable: true,
|
40
|
+
configurable: true,
|
41
|
+
writable: true,
|
42
|
+
value: OUTPUT_SCHEMA
|
43
|
+
});
|