@langchain/core 0.2.16 → 0.2.17
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/dist/language_models/chat_models.d.ts +18 -1
- package/dist/messages/ai.cjs +17 -0
- package/dist/messages/ai.d.ts +2 -0
- package/dist/messages/ai.js +17 -0
- package/dist/messages/base.cjs +50 -0
- package/dist/messages/base.d.ts +4 -1
- package/dist/messages/base.js +50 -0
- package/dist/messages/chat.cjs +12 -0
- package/dist/messages/chat.d.ts +2 -0
- package/dist/messages/chat.js +12 -0
- package/dist/messages/index.cjs +1 -0
- package/dist/messages/index.d.ts +1 -0
- package/dist/messages/index.js +1 -0
- package/dist/messages/modifier.cjs +35 -0
- package/dist/messages/modifier.d.ts +19 -0
- package/dist/messages/modifier.js +31 -0
- package/dist/messages/tool.cjs +14 -0
- package/dist/messages/tool.d.ts +2 -0
- package/dist/messages/tool.js +14 -0
- package/dist/messages/transformers.cjs +5 -0
- package/dist/messages/transformers.d.ts +3 -2
- package/dist/messages/transformers.js +5 -0
- package/dist/runnables/base.cjs +33 -2
- package/dist/runnables/base.d.ts +3 -2
- package/dist/runnables/base.js +33 -2
- package/dist/runnables/tests/runnable_tools.test.js +38 -0
- package/dist/tools/index.cjs +17 -45
- package/dist/tools/index.d.ts +19 -28
- package/dist/tools/index.js +15 -42
- package/dist/tools/tests/tools.test.js +11 -0
- package/dist/tools/utils.cjs +28 -0
- package/dist/tools/utils.d.ts +11 -0
- package/dist/tools/utils.js +23 -0
- package/dist/types/zod.d.ts +1 -1
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ import type { RunnableConfig } from "../runnables/config.js";
|
|
|
8
8
|
import type { BaseCache } from "../caches/base.js";
|
|
9
9
|
import { StructuredToolInterface } from "../tools/index.js";
|
|
10
10
|
import { Runnable, RunnableToolLike } from "../runnables/base.js";
|
|
11
|
+
type ToolChoice = string | Record<string, any> | "auto" | "any";
|
|
11
12
|
/**
|
|
12
13
|
* Represents a serialized chat model.
|
|
13
14
|
*/
|
|
@@ -29,7 +30,23 @@ export type BaseChatModelParams = BaseLanguageModelParams;
|
|
|
29
30
|
/**
|
|
30
31
|
* Represents the call options for a base chat model.
|
|
31
32
|
*/
|
|
32
|
-
export type BaseChatModelCallOptions = BaseLanguageModelCallOptions
|
|
33
|
+
export type BaseChatModelCallOptions = BaseLanguageModelCallOptions & {
|
|
34
|
+
/**
|
|
35
|
+
* Specifies how the chat model should use tools.
|
|
36
|
+
* @default undefined
|
|
37
|
+
*
|
|
38
|
+
* Possible values:
|
|
39
|
+
* - "auto": The model may choose to use any of the provided tools, or none.
|
|
40
|
+
* - "any": The model must use one of the provided tools.
|
|
41
|
+
* - "none": The model must not use any tools.
|
|
42
|
+
* - A string (not "auto", "any", or "none"): The name of a specific tool the model must use.
|
|
43
|
+
* - An object: A custom schema specifying tool choice parameters. Specific to the provider.
|
|
44
|
+
*
|
|
45
|
+
* Note: Not all providers support tool_choice. An error will be thrown
|
|
46
|
+
* if used with an unsupported model.
|
|
47
|
+
*/
|
|
48
|
+
tool_choice?: ToolChoice;
|
|
49
|
+
};
|
|
33
50
|
/**
|
|
34
51
|
* Creates a transform stream for encoding chat message chunks.
|
|
35
52
|
* @deprecated Use {@link BytesOutputParser} instead
|
package/dist/messages/ai.cjs
CHANGED
|
@@ -98,6 +98,14 @@ class AIMessage extends base_js_1.BaseMessage {
|
|
|
98
98
|
_getType() {
|
|
99
99
|
return "ai";
|
|
100
100
|
}
|
|
101
|
+
get _printableFields() {
|
|
102
|
+
return {
|
|
103
|
+
...super._printableFields,
|
|
104
|
+
tool_calls: this.tool_calls,
|
|
105
|
+
invalid_tool_calls: this.invalid_tool_calls,
|
|
106
|
+
usage_metadata: this.usage_metadata,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
101
109
|
}
|
|
102
110
|
exports.AIMessage = AIMessage;
|
|
103
111
|
function isAIMessage(x) {
|
|
@@ -215,6 +223,15 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
215
223
|
_getType() {
|
|
216
224
|
return "ai";
|
|
217
225
|
}
|
|
226
|
+
get _printableFields() {
|
|
227
|
+
return {
|
|
228
|
+
...super._printableFields,
|
|
229
|
+
tool_calls: this.tool_calls,
|
|
230
|
+
tool_call_chunks: this.tool_call_chunks,
|
|
231
|
+
invalid_tool_calls: this.invalid_tool_calls,
|
|
232
|
+
usage_metadata: this.usage_metadata,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
218
235
|
concat(chunk) {
|
|
219
236
|
const combinedFields = {
|
|
220
237
|
content: (0, base_js_1.mergeContent)(this.content, chunk.content),
|
package/dist/messages/ai.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ export declare class AIMessage extends BaseMessage {
|
|
|
38
38
|
kwargs?: Record<string, unknown>);
|
|
39
39
|
static lc_name(): string;
|
|
40
40
|
_getType(): MessageType;
|
|
41
|
+
get _printableFields(): Record<string, unknown>;
|
|
41
42
|
}
|
|
42
43
|
export declare function isAIMessage(x: BaseMessage): x is AIMessage;
|
|
43
44
|
export type AIMessageChunkFields = AIMessageFields & {
|
|
@@ -59,5 +60,6 @@ export declare class AIMessageChunk extends BaseMessageChunk {
|
|
|
59
60
|
get lc_aliases(): Record<string, string>;
|
|
60
61
|
static lc_name(): string;
|
|
61
62
|
_getType(): MessageType;
|
|
63
|
+
get _printableFields(): Record<string, unknown>;
|
|
62
64
|
concat(chunk: AIMessageChunk): AIMessageChunk;
|
|
63
65
|
}
|
package/dist/messages/ai.js
CHANGED
|
@@ -95,6 +95,14 @@ export class AIMessage extends BaseMessage {
|
|
|
95
95
|
_getType() {
|
|
96
96
|
return "ai";
|
|
97
97
|
}
|
|
98
|
+
get _printableFields() {
|
|
99
|
+
return {
|
|
100
|
+
...super._printableFields,
|
|
101
|
+
tool_calls: this.tool_calls,
|
|
102
|
+
invalid_tool_calls: this.invalid_tool_calls,
|
|
103
|
+
usage_metadata: this.usage_metadata,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
98
106
|
}
|
|
99
107
|
export function isAIMessage(x) {
|
|
100
108
|
return x._getType() === "ai";
|
|
@@ -210,6 +218,15 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
210
218
|
_getType() {
|
|
211
219
|
return "ai";
|
|
212
220
|
}
|
|
221
|
+
get _printableFields() {
|
|
222
|
+
return {
|
|
223
|
+
...super._printableFields,
|
|
224
|
+
tool_calls: this.tool_calls,
|
|
225
|
+
tool_call_chunks: this.tool_call_chunks,
|
|
226
|
+
invalid_tool_calls: this.invalid_tool_calls,
|
|
227
|
+
usage_metadata: this.usage_metadata,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
213
230
|
concat(chunk) {
|
|
214
231
|
const combinedFields = {
|
|
215
232
|
content: mergeContent(this.content, chunk.content),
|
package/dist/messages/base.cjs
CHANGED
|
@@ -25,6 +25,30 @@ function mergeContent(firstContent, secondContent) {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
exports.mergeContent = mergeContent;
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
function stringifyWithDepthLimit(obj, depthLimit) {
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
function helper(obj, currentDepth) {
|
|
32
|
+
if (typeof obj !== "object" || obj === null || obj === undefined) {
|
|
33
|
+
return obj;
|
|
34
|
+
}
|
|
35
|
+
if (currentDepth >= depthLimit) {
|
|
36
|
+
if (Array.isArray(obj)) {
|
|
37
|
+
return "[Array]";
|
|
38
|
+
}
|
|
39
|
+
return "[Object]";
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(obj)) {
|
|
42
|
+
return obj.map((item) => helper(item, currentDepth + 1));
|
|
43
|
+
}
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const key of Object.keys(obj)) {
|
|
46
|
+
result[key] = helper(obj[key], currentDepth + 1);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
return JSON.stringify(helper(obj, 0), null, 2);
|
|
51
|
+
}
|
|
28
52
|
/**
|
|
29
53
|
* Base class for all types of messages in a conversation. It includes
|
|
30
54
|
* properties like `content`, `name`, and `additional_kwargs`. It also
|
|
@@ -129,6 +153,32 @@ class BaseMessage extends serializable_js_1.Serializable {
|
|
|
129
153
|
.kwargs,
|
|
130
154
|
};
|
|
131
155
|
}
|
|
156
|
+
static lc_name() {
|
|
157
|
+
return "BaseMessage";
|
|
158
|
+
}
|
|
159
|
+
// Can't be protected for silly reasons
|
|
160
|
+
get _printableFields() {
|
|
161
|
+
return {
|
|
162
|
+
id: this.id,
|
|
163
|
+
content: this.content,
|
|
164
|
+
name: this.name,
|
|
165
|
+
additional_kwargs: this.additional_kwargs,
|
|
166
|
+
response_metadata: this.response_metadata,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
get [Symbol.toStringTag]() {
|
|
170
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
171
|
+
return this.constructor.lc_name();
|
|
172
|
+
}
|
|
173
|
+
// Override the default behavior of console.log
|
|
174
|
+
[Symbol.for("nodejs.util.inspect.custom")](depth) {
|
|
175
|
+
if (depth === null) {
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
const printable = stringifyWithDepthLimit(this._printableFields, Math.max(4, depth));
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
180
|
+
return `${this.constructor.lc_name()} ${printable}`;
|
|
181
|
+
}
|
|
132
182
|
}
|
|
133
183
|
exports.BaseMessage = BaseMessage;
|
|
134
184
|
function isOpenAIToolCallArray(value) {
|
package/dist/messages/base.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export interface StoredMessageV1 {
|
|
|
23
23
|
role: string | undefined;
|
|
24
24
|
text: string;
|
|
25
25
|
}
|
|
26
|
-
export type MessageType = "human" | "ai" | "generic" | "system" | "function" | "tool";
|
|
26
|
+
export type MessageType = "human" | "ai" | "generic" | "system" | "function" | "tool" | "remove";
|
|
27
27
|
export type ImageDetail = "auto" | "low" | "high";
|
|
28
28
|
export type MessageContentText = {
|
|
29
29
|
type: "text";
|
|
@@ -123,6 +123,9 @@ export declare abstract class BaseMessage extends Serializable implements BaseMe
|
|
|
123
123
|
/** @deprecated */
|
|
124
124
|
kwargs?: Record<string, unknown>);
|
|
125
125
|
toDict(): StoredMessage;
|
|
126
|
+
static lc_name(): string;
|
|
127
|
+
get _printableFields(): Record<string, unknown>;
|
|
128
|
+
get [Symbol.toStringTag](): any;
|
|
126
129
|
}
|
|
127
130
|
export type OpenAIToolCall = ToolCall & {
|
|
128
131
|
index: number;
|
package/dist/messages/base.js
CHANGED
|
@@ -21,6 +21,30 @@ export function mergeContent(firstContent, secondContent) {
|
|
|
21
21
|
return [...firstContent, { type: "text", text: secondContent }];
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
function stringifyWithDepthLimit(obj, depthLimit) {
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
function helper(obj, currentDepth) {
|
|
28
|
+
if (typeof obj !== "object" || obj === null || obj === undefined) {
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
31
|
+
if (currentDepth >= depthLimit) {
|
|
32
|
+
if (Array.isArray(obj)) {
|
|
33
|
+
return "[Array]";
|
|
34
|
+
}
|
|
35
|
+
return "[Object]";
|
|
36
|
+
}
|
|
37
|
+
if (Array.isArray(obj)) {
|
|
38
|
+
return obj.map((item) => helper(item, currentDepth + 1));
|
|
39
|
+
}
|
|
40
|
+
const result = {};
|
|
41
|
+
for (const key of Object.keys(obj)) {
|
|
42
|
+
result[key] = helper(obj[key], currentDepth + 1);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
return JSON.stringify(helper(obj, 0), null, 2);
|
|
47
|
+
}
|
|
24
48
|
/**
|
|
25
49
|
* Base class for all types of messages in a conversation. It includes
|
|
26
50
|
* properties like `content`, `name`, and `additional_kwargs`. It also
|
|
@@ -125,6 +149,32 @@ export class BaseMessage extends Serializable {
|
|
|
125
149
|
.kwargs,
|
|
126
150
|
};
|
|
127
151
|
}
|
|
152
|
+
static lc_name() {
|
|
153
|
+
return "BaseMessage";
|
|
154
|
+
}
|
|
155
|
+
// Can't be protected for silly reasons
|
|
156
|
+
get _printableFields() {
|
|
157
|
+
return {
|
|
158
|
+
id: this.id,
|
|
159
|
+
content: this.content,
|
|
160
|
+
name: this.name,
|
|
161
|
+
additional_kwargs: this.additional_kwargs,
|
|
162
|
+
response_metadata: this.response_metadata,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
get [Symbol.toStringTag]() {
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
167
|
+
return this.constructor.lc_name();
|
|
168
|
+
}
|
|
169
|
+
// Override the default behavior of console.log
|
|
170
|
+
[Symbol.for("nodejs.util.inspect.custom")](depth) {
|
|
171
|
+
if (depth === null) {
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
const printable = stringifyWithDepthLimit(this._printableFields, Math.max(4, depth));
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
176
|
+
return `${this.constructor.lc_name()} ${printable}`;
|
|
177
|
+
}
|
|
128
178
|
}
|
|
129
179
|
export function isOpenAIToolCallArray(value) {
|
|
130
180
|
return (Array.isArray(value) &&
|
package/dist/messages/chat.cjs
CHANGED
|
@@ -32,6 +32,12 @@ class ChatMessage extends base_js_1.BaseMessage {
|
|
|
32
32
|
static isInstance(message) {
|
|
33
33
|
return message._getType() === "generic";
|
|
34
34
|
}
|
|
35
|
+
get _printableFields() {
|
|
36
|
+
return {
|
|
37
|
+
...super._printableFields,
|
|
38
|
+
role: this.role,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
35
41
|
}
|
|
36
42
|
exports.ChatMessage = ChatMessage;
|
|
37
43
|
/**
|
|
@@ -68,5 +74,11 @@ class ChatMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
68
74
|
id: this.id ?? chunk.id,
|
|
69
75
|
});
|
|
70
76
|
}
|
|
77
|
+
get _printableFields() {
|
|
78
|
+
return {
|
|
79
|
+
...super._printableFields,
|
|
80
|
+
role: this.role,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
71
83
|
}
|
|
72
84
|
exports.ChatMessageChunk = ChatMessageChunk;
|
package/dist/messages/chat.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare class ChatMessage extends BaseMessage implements ChatMessageField
|
|
|
13
13
|
constructor(fields: ChatMessageFieldsWithRole);
|
|
14
14
|
_getType(): MessageType;
|
|
15
15
|
static isInstance(message: BaseMessage): message is ChatMessage;
|
|
16
|
+
get _printableFields(): Record<string, unknown>;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Represents a chunk of a chat message, which can be concatenated with
|
|
@@ -25,4 +26,5 @@ export declare class ChatMessageChunk extends BaseMessageChunk {
|
|
|
25
26
|
constructor(fields: ChatMessageFieldsWithRole);
|
|
26
27
|
_getType(): MessageType;
|
|
27
28
|
concat(chunk: ChatMessageChunk): ChatMessageChunk;
|
|
29
|
+
get _printableFields(): Record<string, unknown>;
|
|
28
30
|
}
|
package/dist/messages/chat.js
CHANGED
|
@@ -29,6 +29,12 @@ export class ChatMessage extends BaseMessage {
|
|
|
29
29
|
static isInstance(message) {
|
|
30
30
|
return message._getType() === "generic";
|
|
31
31
|
}
|
|
32
|
+
get _printableFields() {
|
|
33
|
+
return {
|
|
34
|
+
...super._printableFields,
|
|
35
|
+
role: this.role,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
34
40
|
* Represents a chunk of a chat message, which can be concatenated with
|
|
@@ -64,4 +70,10 @@ export class ChatMessageChunk extends BaseMessageChunk {
|
|
|
64
70
|
id: this.id ?? chunk.id,
|
|
65
71
|
});
|
|
66
72
|
}
|
|
73
|
+
get _printableFields() {
|
|
74
|
+
return {
|
|
75
|
+
...super._printableFields,
|
|
76
|
+
role: this.role,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
67
79
|
}
|
package/dist/messages/index.cjs
CHANGED
|
@@ -23,6 +23,7 @@ __exportStar(require("./human.cjs"), exports);
|
|
|
23
23
|
__exportStar(require("./system.cjs"), exports);
|
|
24
24
|
__exportStar(require("./utils.cjs"), exports);
|
|
25
25
|
__exportStar(require("./transformers.cjs"), exports);
|
|
26
|
+
__exportStar(require("./modifier.cjs"), exports);
|
|
26
27
|
// TODO: Use a star export when we deprecate the
|
|
27
28
|
// existing "ToolCall" type in "base.js".
|
|
28
29
|
var tool_js_1 = require("./tool.cjs");
|
package/dist/messages/index.d.ts
CHANGED
|
@@ -6,4 +6,5 @@ export * from "./human.js";
|
|
|
6
6
|
export * from "./system.js";
|
|
7
7
|
export * from "./utils.js";
|
|
8
8
|
export * from "./transformers.js";
|
|
9
|
+
export * from "./modifier.js";
|
|
9
10
|
export { type ToolMessageFieldsWithToolCallId, ToolMessage, ToolMessageChunk, type InvalidToolCall, } from "./tool.js";
|
package/dist/messages/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export * from "./human.js";
|
|
|
6
6
|
export * from "./system.js";
|
|
7
7
|
export * from "./utils.js";
|
|
8
8
|
export * from "./transformers.js";
|
|
9
|
+
export * from "./modifier.js";
|
|
9
10
|
// TODO: Use a star export when we deprecate the
|
|
10
11
|
// existing "ToolCall" type in "base.js".
|
|
11
12
|
export { ToolMessage, ToolMessageChunk, } from "./tool.js";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RemoveMessage = void 0;
|
|
4
|
+
const base_js_1 = require("./base.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Message responsible for deleting other messages.
|
|
7
|
+
*/
|
|
8
|
+
class RemoveMessage extends base_js_1.BaseMessage {
|
|
9
|
+
constructor(fields) {
|
|
10
|
+
super({
|
|
11
|
+
...fields,
|
|
12
|
+
content: "",
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* The ID of the message to remove.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(this, "id", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: void 0
|
|
22
|
+
});
|
|
23
|
+
this.id = fields.id;
|
|
24
|
+
}
|
|
25
|
+
_getType() {
|
|
26
|
+
return "remove";
|
|
27
|
+
}
|
|
28
|
+
get _printableFields() {
|
|
29
|
+
return {
|
|
30
|
+
...super._printableFields,
|
|
31
|
+
id: this.id,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.RemoveMessage = RemoveMessage;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BaseMessage, BaseMessageFields, MessageType } from "./base.js";
|
|
2
|
+
export interface RemoveMessageFields extends Omit<BaseMessageFields, "content"> {
|
|
3
|
+
/**
|
|
4
|
+
* The ID of the message to remove.
|
|
5
|
+
*/
|
|
6
|
+
id: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Message responsible for deleting other messages.
|
|
10
|
+
*/
|
|
11
|
+
export declare class RemoveMessage extends BaseMessage {
|
|
12
|
+
/**
|
|
13
|
+
* The ID of the message to remove.
|
|
14
|
+
*/
|
|
15
|
+
id: string;
|
|
16
|
+
constructor(fields: RemoveMessageFields);
|
|
17
|
+
_getType(): MessageType;
|
|
18
|
+
get _printableFields(): Record<string, unknown>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BaseMessage } from "./base.js";
|
|
2
|
+
/**
|
|
3
|
+
* Message responsible for deleting other messages.
|
|
4
|
+
*/
|
|
5
|
+
export class RemoveMessage extends BaseMessage {
|
|
6
|
+
constructor(fields) {
|
|
7
|
+
super({
|
|
8
|
+
...fields,
|
|
9
|
+
content: "",
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* The ID of the message to remove.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(this, "id", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: void 0
|
|
19
|
+
});
|
|
20
|
+
this.id = fields.id;
|
|
21
|
+
}
|
|
22
|
+
_getType() {
|
|
23
|
+
return "remove";
|
|
24
|
+
}
|
|
25
|
+
get _printableFields() {
|
|
26
|
+
return {
|
|
27
|
+
...super._printableFields,
|
|
28
|
+
id: this.id,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
package/dist/messages/tool.cjs
CHANGED
|
@@ -48,6 +48,13 @@ class ToolMessage extends base_js_1.BaseMessage {
|
|
|
48
48
|
static isInstance(message) {
|
|
49
49
|
return message._getType() === "tool";
|
|
50
50
|
}
|
|
51
|
+
get _printableFields() {
|
|
52
|
+
return {
|
|
53
|
+
...super._printableFields,
|
|
54
|
+
tool_call_id: this.tool_call_id,
|
|
55
|
+
artifact: this.artifact,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
51
58
|
}
|
|
52
59
|
exports.ToolMessage = ToolMessage;
|
|
53
60
|
/**
|
|
@@ -96,6 +103,13 @@ class ToolMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
96
103
|
id: this.id ?? chunk.id,
|
|
97
104
|
});
|
|
98
105
|
}
|
|
106
|
+
get _printableFields() {
|
|
107
|
+
return {
|
|
108
|
+
...super._printableFields,
|
|
109
|
+
tool_call_id: this.tool_call_id,
|
|
110
|
+
artifact: this.artifact,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
99
113
|
}
|
|
100
114
|
exports.ToolMessageChunk = ToolMessageChunk;
|
|
101
115
|
function defaultToolCallParser(
|
package/dist/messages/tool.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export declare class ToolMessage extends BaseMessage {
|
|
|
29
29
|
constructor(fields: string | BaseMessageFields, tool_call_id: string, name?: string);
|
|
30
30
|
_getType(): MessageType;
|
|
31
31
|
static isInstance(message: BaseMessage): message is ToolMessage;
|
|
32
|
+
get _printableFields(): Record<string, unknown>;
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
34
35
|
* Represents a chunk of a tool message, which can be concatenated
|
|
@@ -48,6 +49,7 @@ export declare class ToolMessageChunk extends BaseMessageChunk {
|
|
|
48
49
|
static lc_name(): string;
|
|
49
50
|
_getType(): MessageType;
|
|
50
51
|
concat(chunk: ToolMessageChunk): ToolMessageChunk;
|
|
52
|
+
get _printableFields(): Record<string, unknown>;
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
55
|
* A call to a tool.
|
package/dist/messages/tool.js
CHANGED
|
@@ -45,6 +45,13 @@ export class ToolMessage extends BaseMessage {
|
|
|
45
45
|
static isInstance(message) {
|
|
46
46
|
return message._getType() === "tool";
|
|
47
47
|
}
|
|
48
|
+
get _printableFields() {
|
|
49
|
+
return {
|
|
50
|
+
...super._printableFields,
|
|
51
|
+
tool_call_id: this.tool_call_id,
|
|
52
|
+
artifact: this.artifact,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
48
55
|
}
|
|
49
56
|
/**
|
|
50
57
|
* Represents a chunk of a tool message, which can be concatenated
|
|
@@ -92,6 +99,13 @@ export class ToolMessageChunk extends BaseMessageChunk {
|
|
|
92
99
|
id: this.id ?? chunk.id,
|
|
93
100
|
});
|
|
94
101
|
}
|
|
102
|
+
get _printableFields() {
|
|
103
|
+
return {
|
|
104
|
+
...super._printableFields,
|
|
105
|
+
tool_call_id: this.tool_call_id,
|
|
106
|
+
artifact: this.artifact,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
95
109
|
}
|
|
96
110
|
export function defaultToolCallParser(
|
|
97
111
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -6,6 +6,7 @@ const ai_js_1 = require("./ai.cjs");
|
|
|
6
6
|
const chat_js_1 = require("./chat.cjs");
|
|
7
7
|
const function_js_1 = require("./function.cjs");
|
|
8
8
|
const human_js_1 = require("./human.cjs");
|
|
9
|
+
const modifier_js_1 = require("./modifier.cjs");
|
|
9
10
|
const system_js_1 = require("./system.cjs");
|
|
10
11
|
const tool_js_1 = require("./tool.cjs");
|
|
11
12
|
const utils_js_1 = require("./utils.cjs");
|
|
@@ -303,6 +304,10 @@ const _MSG_CHUNK_MAP = {
|
|
|
303
304
|
message: chat_js_1.ChatMessage,
|
|
304
305
|
messageChunk: chat_js_1.ChatMessageChunk,
|
|
305
306
|
},
|
|
307
|
+
remove: {
|
|
308
|
+
message: modifier_js_1.RemoveMessage,
|
|
309
|
+
messageChunk: modifier_js_1.RemoveMessage, // RemoveMessage does not have a chunk class.
|
|
310
|
+
},
|
|
306
311
|
};
|
|
307
312
|
function _switchTypeToMessage(messageType, fields, returnChunk) {
|
|
308
313
|
let chunk;
|
|
@@ -6,10 +6,11 @@ import { BaseMessage, MessageType } from "./base.js";
|
|
|
6
6
|
import { ChatMessage, ChatMessageChunk } from "./chat.js";
|
|
7
7
|
import { FunctionMessage, FunctionMessageChunk } from "./function.js";
|
|
8
8
|
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
9
|
+
import { RemoveMessage } from "./modifier.js";
|
|
9
10
|
import { SystemMessage, SystemMessageChunk } from "./system.js";
|
|
10
11
|
import { ToolMessage, ToolMessageChunk } from "./tool.js";
|
|
11
|
-
export type MessageUnion = typeof HumanMessage | typeof AIMessage | typeof SystemMessage | typeof ChatMessage | typeof FunctionMessage | typeof ToolMessage;
|
|
12
|
-
export type MessageChunkUnion = typeof HumanMessageChunk | typeof AIMessageChunk | typeof SystemMessageChunk | typeof FunctionMessageChunk | typeof ToolMessageChunk | typeof ChatMessageChunk;
|
|
12
|
+
export type MessageUnion = typeof HumanMessage | typeof AIMessage | typeof SystemMessage | typeof ChatMessage | typeof FunctionMessage | typeof ToolMessage | typeof RemoveMessage;
|
|
13
|
+
export type MessageChunkUnion = typeof HumanMessageChunk | typeof AIMessageChunk | typeof SystemMessageChunk | typeof FunctionMessageChunk | typeof ToolMessageChunk | typeof ChatMessageChunk | typeof RemoveMessage;
|
|
13
14
|
export type MessageTypeOrClass = MessageType | MessageUnion | MessageChunkUnion;
|
|
14
15
|
export interface FilterMessagesFields {
|
|
15
16
|
/**
|
|
@@ -3,6 +3,7 @@ import { AIMessage, AIMessageChunk } from "./ai.js";
|
|
|
3
3
|
import { ChatMessage, ChatMessageChunk, } from "./chat.js";
|
|
4
4
|
import { FunctionMessage, FunctionMessageChunk, } from "./function.js";
|
|
5
5
|
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
6
|
+
import { RemoveMessage } from "./modifier.js";
|
|
6
7
|
import { SystemMessage, SystemMessageChunk } from "./system.js";
|
|
7
8
|
import { ToolMessage, ToolMessageChunk, } from "./tool.js";
|
|
8
9
|
import { convertToChunk } from "./utils.js";
|
|
@@ -297,6 +298,10 @@ const _MSG_CHUNK_MAP = {
|
|
|
297
298
|
message: ChatMessage,
|
|
298
299
|
messageChunk: ChatMessageChunk,
|
|
299
300
|
},
|
|
301
|
+
remove: {
|
|
302
|
+
message: RemoveMessage,
|
|
303
|
+
messageChunk: RemoveMessage, // RemoveMessage does not have a chunk class.
|
|
304
|
+
},
|
|
300
305
|
};
|
|
301
306
|
function _switchTypeToMessage(messageType, fields, returnChunk) {
|
|
302
307
|
let chunk;
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -20,6 +20,7 @@ const index_js_1 = require("../singletons/index.cjs");
|
|
|
20
20
|
const graph_js_1 = require("./graph.cjs");
|
|
21
21
|
const wrappers_js_1 = require("./wrappers.cjs");
|
|
22
22
|
const iter_js_1 = require("./iter.cjs");
|
|
23
|
+
const utils_js_2 = require("../tools/utils.cjs");
|
|
23
24
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
25
|
function _coerceToDict(value, defaultKey) {
|
|
25
26
|
return value &&
|
|
@@ -1929,8 +1930,26 @@ class RunnablePick extends Runnable {
|
|
|
1929
1930
|
exports.RunnablePick = RunnablePick;
|
|
1930
1931
|
class RunnableToolLike extends RunnableBinding {
|
|
1931
1932
|
constructor(fields) {
|
|
1933
|
+
const sequence = RunnableSequence.from([
|
|
1934
|
+
RunnableLambda.from(async (input) => {
|
|
1935
|
+
let toolInput;
|
|
1936
|
+
if ((0, utils_js_2._isToolCall)(input)) {
|
|
1937
|
+
try {
|
|
1938
|
+
toolInput = await this.schema.parseAsync(input.args);
|
|
1939
|
+
}
|
|
1940
|
+
catch (e) {
|
|
1941
|
+
throw new utils_js_2.ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
else {
|
|
1945
|
+
toolInput = input;
|
|
1946
|
+
}
|
|
1947
|
+
return toolInput;
|
|
1948
|
+
}).withConfig({ runName: `${fields.name}:parse_input` }),
|
|
1949
|
+
fields.bound,
|
|
1950
|
+
]).withConfig({ runName: fields.name });
|
|
1932
1951
|
super({
|
|
1933
|
-
bound:
|
|
1952
|
+
bound: sequence,
|
|
1934
1953
|
config: fields.config ?? {},
|
|
1935
1954
|
});
|
|
1936
1955
|
Object.defineProperty(this, "name", {
|
|
@@ -1975,7 +1994,19 @@ exports.RunnableToolLike = RunnableToolLike;
|
|
|
1975
1994
|
*/
|
|
1976
1995
|
function convertRunnableToTool(runnable, fields) {
|
|
1977
1996
|
const name = fields.name ?? runnable.getName();
|
|
1978
|
-
const description = fields.description ?? fields.schema
|
|
1997
|
+
const description = fields.description ?? fields.schema?.description;
|
|
1998
|
+
if (fields.schema.constructor === zod_1.z.ZodString) {
|
|
1999
|
+
return new RunnableToolLike({
|
|
2000
|
+
name,
|
|
2001
|
+
description,
|
|
2002
|
+
schema: zod_1.z
|
|
2003
|
+
.object({
|
|
2004
|
+
input: zod_1.z.string(),
|
|
2005
|
+
})
|
|
2006
|
+
.transform((input) => input.input),
|
|
2007
|
+
bound: runnable,
|
|
2008
|
+
});
|
|
2009
|
+
}
|
|
1979
2010
|
return new RunnableToolLike({
|
|
1980
2011
|
name,
|
|
1981
2012
|
description,
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { IterableReadableStream } from "../utils/stream.js";
|
|
|
9
9
|
import { RunnableConfig } from "./config.js";
|
|
10
10
|
import { Run } from "../tracers/base.js";
|
|
11
11
|
import { Graph } from "./graph.js";
|
|
12
|
+
import { ToolCall } from "../messages/tool.js";
|
|
12
13
|
export { type RunnableInterface, RunnableBatchOptions };
|
|
13
14
|
export type RunnableFunc<RunInput, RunOutput> = (input: RunInput, options?: ({
|
|
14
15
|
/** @deprecated Use top-level config fields instead. */
|
|
@@ -262,7 +263,7 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
262
263
|
name?: string;
|
|
263
264
|
description?: string;
|
|
264
265
|
schema: z.ZodType<T>;
|
|
265
|
-
}): RunnableToolLike<z.ZodType<T>, RunOutput>;
|
|
266
|
+
}): RunnableToolLike<z.ZodType<T | ToolCall>, RunOutput>;
|
|
266
267
|
}
|
|
267
268
|
export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = {
|
|
268
269
|
bound: Runnable<RunInput, RunOutput, CallOptions>;
|
|
@@ -608,4 +609,4 @@ export declare function convertRunnableToTool<RunInput, RunOutput>(runnable: Run
|
|
|
608
609
|
name?: string;
|
|
609
610
|
description?: string;
|
|
610
611
|
schema: z.ZodType<RunInput>;
|
|
611
|
-
}): RunnableToolLike<z.ZodType<RunInput>, RunOutput>;
|
|
612
|
+
}): RunnableToolLike<z.ZodType<RunInput | ToolCall>, RunOutput>;
|
package/dist/runnables/base.js
CHANGED
|
@@ -14,6 +14,7 @@ import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
|
14
14
|
import { Graph } from "./graph.js";
|
|
15
15
|
import { convertToHttpEventStream } from "./wrappers.js";
|
|
16
16
|
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterableIterator, isIterator, } from "./iter.js";
|
|
17
|
+
import { _isToolCall, ToolInputParsingException } from "../tools/utils.js";
|
|
17
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
19
|
export function _coerceToDict(value, defaultKey) {
|
|
19
20
|
return value &&
|
|
@@ -1909,8 +1910,26 @@ export class RunnablePick extends Runnable {
|
|
|
1909
1910
|
}
|
|
1910
1911
|
export class RunnableToolLike extends RunnableBinding {
|
|
1911
1912
|
constructor(fields) {
|
|
1913
|
+
const sequence = RunnableSequence.from([
|
|
1914
|
+
RunnableLambda.from(async (input) => {
|
|
1915
|
+
let toolInput;
|
|
1916
|
+
if (_isToolCall(input)) {
|
|
1917
|
+
try {
|
|
1918
|
+
toolInput = await this.schema.parseAsync(input.args);
|
|
1919
|
+
}
|
|
1920
|
+
catch (e) {
|
|
1921
|
+
throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
else {
|
|
1925
|
+
toolInput = input;
|
|
1926
|
+
}
|
|
1927
|
+
return toolInput;
|
|
1928
|
+
}).withConfig({ runName: `${fields.name}:parse_input` }),
|
|
1929
|
+
fields.bound,
|
|
1930
|
+
]).withConfig({ runName: fields.name });
|
|
1912
1931
|
super({
|
|
1913
|
-
bound:
|
|
1932
|
+
bound: sequence,
|
|
1914
1933
|
config: fields.config ?? {},
|
|
1915
1934
|
});
|
|
1916
1935
|
Object.defineProperty(this, "name", {
|
|
@@ -1954,7 +1973,19 @@ export class RunnableToolLike extends RunnableBinding {
|
|
|
1954
1973
|
*/
|
|
1955
1974
|
export function convertRunnableToTool(runnable, fields) {
|
|
1956
1975
|
const name = fields.name ?? runnable.getName();
|
|
1957
|
-
const description = fields.description ?? fields.schema
|
|
1976
|
+
const description = fields.description ?? fields.schema?.description;
|
|
1977
|
+
if (fields.schema.constructor === z.ZodString) {
|
|
1978
|
+
return new RunnableToolLike({
|
|
1979
|
+
name,
|
|
1980
|
+
description,
|
|
1981
|
+
schema: z
|
|
1982
|
+
.object({
|
|
1983
|
+
input: z.string(),
|
|
1984
|
+
})
|
|
1985
|
+
.transform((input) => input.input),
|
|
1986
|
+
bound: runnable,
|
|
1987
|
+
});
|
|
1988
|
+
}
|
|
1958
1989
|
return new RunnableToolLike({
|
|
1959
1990
|
name,
|
|
1960
1991
|
description,
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { RunnableLambda, RunnableToolLike } from "../base.js";
|
|
3
|
+
import { FakeRetriever } from "../../utils/testing/index.js";
|
|
4
|
+
import { Document } from "../../documents/document.js";
|
|
3
5
|
test("Runnable asTool works", async () => {
|
|
4
6
|
const schema = z.object({
|
|
5
7
|
foo: z.string(),
|
|
@@ -109,3 +111,39 @@ test("Runnable asTool uses Zod schema description if not provided", async () =>
|
|
|
109
111
|
});
|
|
110
112
|
expect(tool.description).toBe(description);
|
|
111
113
|
});
|
|
114
|
+
test("Runnable asTool can accept a string zod schema", async () => {
|
|
115
|
+
const lambda = RunnableLambda.from((input) => {
|
|
116
|
+
return `${input}a`;
|
|
117
|
+
}).asTool({
|
|
118
|
+
name: "string_tool",
|
|
119
|
+
description: "A tool that appends 'a' to the input string",
|
|
120
|
+
schema: z.string(),
|
|
121
|
+
});
|
|
122
|
+
const result = await lambda.invoke("b");
|
|
123
|
+
expect(result).toBe("ba");
|
|
124
|
+
});
|
|
125
|
+
test("Runnables which dont accept ToolCalls as inputs can accept ToolCalls", async () => {
|
|
126
|
+
const pageContent = "Dogs are pretty cool, man!";
|
|
127
|
+
const retriever = new FakeRetriever({
|
|
128
|
+
output: [
|
|
129
|
+
new Document({
|
|
130
|
+
pageContent,
|
|
131
|
+
}),
|
|
132
|
+
],
|
|
133
|
+
});
|
|
134
|
+
const tool = retriever.asTool({
|
|
135
|
+
name: "pet_info_retriever",
|
|
136
|
+
description: "Get information about pets.",
|
|
137
|
+
schema: z.string(),
|
|
138
|
+
});
|
|
139
|
+
const result = await tool.invoke({
|
|
140
|
+
type: "tool_call",
|
|
141
|
+
name: "pet_info_retriever",
|
|
142
|
+
args: {
|
|
143
|
+
input: "dogs",
|
|
144
|
+
},
|
|
145
|
+
id: "string",
|
|
146
|
+
});
|
|
147
|
+
expect(result).toHaveLength(1);
|
|
148
|
+
expect(result[0].pageContent).toBe(pageContent);
|
|
149
|
+
});
|
package/dist/tools/index.cjs
CHANGED
|
@@ -7,24 +7,8 @@ const base_js_1 = require("../language_models/base.cjs");
|
|
|
7
7
|
const config_js_1 = require("../runnables/config.cjs");
|
|
8
8
|
const tool_js_1 = require("../messages/tool.cjs");
|
|
9
9
|
const index_js_1 = require("../singletons/index.cjs");
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* It extends the built-in `Error` class and adds an optional `output`
|
|
13
|
-
* property that can hold the output that caused the exception.
|
|
14
|
-
*/
|
|
15
|
-
class ToolInputParsingException extends Error {
|
|
16
|
-
constructor(message, output) {
|
|
17
|
-
super(message);
|
|
18
|
-
Object.defineProperty(this, "output", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: void 0
|
|
23
|
-
});
|
|
24
|
-
this.output = output;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
exports.ToolInputParsingException = ToolInputParsingException;
|
|
10
|
+
const utils_js_1 = require("./utils.cjs");
|
|
11
|
+
Object.defineProperty(exports, "ToolInputParsingException", { enumerable: true, get: function () { return utils_js_1.ToolInputParsingException; } });
|
|
28
12
|
/**
|
|
29
13
|
* Base class for Tools that accept input of any shape defined by a Zod schema.
|
|
30
14
|
*/
|
|
@@ -66,7 +50,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
|
|
|
66
50
|
async invoke(input, config) {
|
|
67
51
|
let tool_call_id;
|
|
68
52
|
let toolInput;
|
|
69
|
-
if (_isToolCall(input)) {
|
|
53
|
+
if ((0, utils_js_1._isToolCall)(input)) {
|
|
70
54
|
tool_call_id = input.id;
|
|
71
55
|
toolInput = input.args;
|
|
72
56
|
}
|
|
@@ -101,7 +85,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
|
|
|
101
85
|
parsed = await this.schema.parseAsync(arg);
|
|
102
86
|
}
|
|
103
87
|
catch (e) {
|
|
104
|
-
throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(arg));
|
|
88
|
+
throw new utils_js_1.ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(arg));
|
|
105
89
|
}
|
|
106
90
|
const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
|
|
107
91
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
|
|
@@ -291,29 +275,23 @@ class BaseToolkit {
|
|
|
291
275
|
}
|
|
292
276
|
}
|
|
293
277
|
exports.BaseToolkit = BaseToolkit;
|
|
294
|
-
/**
|
|
295
|
-
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
|
|
296
|
-
* @function
|
|
297
|
-
* @template {RunInput extends ZodAny = ZodAny} RunInput The input schema for the tool. This corresponds to the input type when the tool is invoked.
|
|
298
|
-
* @template {RunOutput = any} RunOutput The output type for the tool. This corresponds to the output type when the tool is invoked.
|
|
299
|
-
* @template {FuncInput extends z.infer<RunInput> | ToolCall = z.infer<RunInput>} FuncInput The input type for the function.
|
|
300
|
-
*
|
|
301
|
-
* @param {RunnableFunc<z.infer<RunInput> | ToolCall, RunOutput>} func - The function to invoke when the tool is called.
|
|
302
|
-
* @param fields - An object containing the following properties:
|
|
303
|
-
* @param {string} fields.name The name of the tool.
|
|
304
|
-
* @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
|
|
305
|
-
* @param {z.ZodObject<any, any, any, any>} fields.schema The Zod schema defining the input for the tool.
|
|
306
|
-
*
|
|
307
|
-
* @returns {DynamicStructuredTool<RunInput, RunOutput>} A new StructuredTool instance.
|
|
308
|
-
*/
|
|
309
278
|
function tool(func, fields) {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
279
|
+
// If the schema is not provided, or it's a string schema, create a DynamicTool
|
|
280
|
+
if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
|
|
281
|
+
return new DynamicTool({
|
|
282
|
+
name: fields.name,
|
|
283
|
+
description: fields.description ??
|
|
284
|
+
fields.schema?.description ??
|
|
285
|
+
`${fields.name} tool`,
|
|
286
|
+
responseFormat: fields.responseFormat,
|
|
287
|
+
func,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const description = fields.description ?? fields.schema.description ?? `${fields.name} tool`;
|
|
313
291
|
return new DynamicStructuredTool({
|
|
314
292
|
name: fields.name,
|
|
315
293
|
description,
|
|
316
|
-
schema: schema,
|
|
294
|
+
schema: fields.schema,
|
|
317
295
|
// TODO: Consider moving into DynamicStructuredTool constructor
|
|
318
296
|
func: async (input, runManager, config) => {
|
|
319
297
|
return new Promise((resolve, reject) => {
|
|
@@ -334,12 +312,6 @@ function tool(func, fields) {
|
|
|
334
312
|
});
|
|
335
313
|
}
|
|
336
314
|
exports.tool = tool;
|
|
337
|
-
function _isToolCall(toolCall) {
|
|
338
|
-
return !!(toolCall &&
|
|
339
|
-
typeof toolCall === "object" &&
|
|
340
|
-
"type" in toolCall &&
|
|
341
|
-
toolCall.type === "tool_call");
|
|
342
|
-
}
|
|
343
315
|
function _formatToolOutput(params) {
|
|
344
316
|
const { content, artifact, toolCallId } = params;
|
|
345
317
|
if (toolCallId) {
|
package/dist/tools/index.d.ts
CHANGED
|
@@ -4,8 +4,10 @@ import { BaseLangChain, type BaseLangChainParams } from "../language_models/base
|
|
|
4
4
|
import { type RunnableConfig } from "../runnables/config.js";
|
|
5
5
|
import type { RunnableFunc, RunnableInterface } from "../runnables/base.js";
|
|
6
6
|
import { ToolCall } from "../messages/tool.js";
|
|
7
|
-
import {
|
|
7
|
+
import { ZodObjectAny } from "../types/zod.js";
|
|
8
8
|
import { MessageContent } from "../messages/base.js";
|
|
9
|
+
import { ToolInputParsingException } from "./utils.js";
|
|
10
|
+
export { ToolInputParsingException };
|
|
9
11
|
export type ResponseFormat = "content" | "content_and_artifact" | string;
|
|
10
12
|
type ToolReturnType = any;
|
|
11
13
|
export type ContentAndArtifact = [MessageContent, any];
|
|
@@ -24,16 +26,7 @@ export interface ToolParams extends BaseLangChainParams {
|
|
|
24
26
|
*/
|
|
25
27
|
responseFormat?: ResponseFormat;
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
* Custom error class used to handle exceptions related to tool input parsing.
|
|
29
|
-
* It extends the built-in `Error` class and adds an optional `output`
|
|
30
|
-
* property that can hold the output that caused the exception.
|
|
31
|
-
*/
|
|
32
|
-
export declare class ToolInputParsingException extends Error {
|
|
33
|
-
output?: string;
|
|
34
|
-
constructor(message: string, output?: string);
|
|
35
|
-
}
|
|
36
|
-
export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
|
|
29
|
+
export interface StructuredToolInterface<T extends ZodObjectAny = ZodObjectAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
|
|
37
30
|
lc_namespace: string[];
|
|
38
31
|
schema: T | z.ZodEffects<T>;
|
|
39
32
|
/**
|
|
@@ -57,7 +50,7 @@ export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends Runn
|
|
|
57
50
|
/**
|
|
58
51
|
* Base class for Tools that accept input of any shape defined by a Zod schema.
|
|
59
52
|
*/
|
|
60
|
-
export declare abstract class StructuredTool<T extends
|
|
53
|
+
export declare abstract class StructuredTool<T extends ZodObjectAny = ZodObjectAny> extends BaseLangChain<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
|
|
61
54
|
abstract name: string;
|
|
62
55
|
abstract description: string;
|
|
63
56
|
abstract schema: T | z.ZodEffects<T>;
|
|
@@ -97,7 +90,7 @@ export declare abstract class StructuredTool<T extends ZodAny = ZodAny> extends
|
|
|
97
90
|
/** @deprecated */
|
|
98
91
|
tags?: string[]): Promise<ToolReturnType>;
|
|
99
92
|
}
|
|
100
|
-
export interface ToolInterface<T extends
|
|
93
|
+
export interface ToolInterface<T extends ZodObjectAny = ZodObjectAny> extends StructuredToolInterface<T> {
|
|
101
94
|
/**
|
|
102
95
|
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
|
|
103
96
|
*
|
|
@@ -112,7 +105,7 @@ export interface ToolInterface<T extends ZodAny = ZodAny> extends StructuredTool
|
|
|
112
105
|
/**
|
|
113
106
|
* Base class for Tools that accept input as a string.
|
|
114
107
|
*/
|
|
115
|
-
export declare abstract class Tool extends StructuredTool<
|
|
108
|
+
export declare abstract class Tool extends StructuredTool<ZodObjectAny> {
|
|
116
109
|
schema: z.ZodEffects<z.ZodObject<{
|
|
117
110
|
input: z.ZodOptional<z.ZodString>;
|
|
118
111
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -148,7 +141,7 @@ export interface DynamicToolInput extends BaseDynamicToolInput {
|
|
|
148
141
|
/**
|
|
149
142
|
* Interface for the input parameters of the DynamicStructuredTool class.
|
|
150
143
|
*/
|
|
151
|
-
export interface DynamicStructuredToolInput<T extends
|
|
144
|
+
export interface DynamicStructuredToolInput<T extends ZodObjectAny = ZodObjectAny> extends BaseDynamicToolInput {
|
|
152
145
|
func: (input: BaseDynamicToolInput["responseFormat"] extends "content_and_artifact" ? ToolCall : z.infer<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
|
|
153
146
|
schema: T;
|
|
154
147
|
}
|
|
@@ -174,7 +167,7 @@ export declare class DynamicTool extends Tool {
|
|
|
174
167
|
* StructuredTool class and overrides the _call method to execute the
|
|
175
168
|
* provided function when the tool is called.
|
|
176
169
|
*/
|
|
177
|
-
export declare class DynamicStructuredTool<T extends
|
|
170
|
+
export declare class DynamicStructuredTool<T extends ZodObjectAny = ZodObjectAny> extends StructuredTool<T> {
|
|
178
171
|
static lc_name(): string;
|
|
179
172
|
name: string;
|
|
180
173
|
description: string;
|
|
@@ -200,10 +193,9 @@ export declare abstract class BaseToolkit {
|
|
|
200
193
|
}
|
|
201
194
|
/**
|
|
202
195
|
* Parameters for the tool function.
|
|
203
|
-
* @template {
|
|
204
|
-
* @template {any} RunOutput The output type for the tool.
|
|
196
|
+
* @template {ZodObjectAny | z.ZodString = ZodObjectAny} RunInput The input schema for the tool. Either any Zod object, or a Zod string.
|
|
205
197
|
*/
|
|
206
|
-
interface ToolWrapperParams<RunInput extends
|
|
198
|
+
interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString = ZodObjectAny> extends ToolParams {
|
|
207
199
|
/**
|
|
208
200
|
* The name of the tool. If using with an LLM, this
|
|
209
201
|
* will be passed as the tool name.
|
|
@@ -233,18 +225,17 @@ interface ToolWrapperParams<RunInput extends ZodAny = ZodAny> extends ToolParams
|
|
|
233
225
|
}
|
|
234
226
|
/**
|
|
235
227
|
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
|
|
228
|
+
*
|
|
236
229
|
* @function
|
|
237
|
-
* @template {
|
|
238
|
-
* @template {RunOutput = any} RunOutput The output type for the tool. This corresponds to the output type when the tool is invoked.
|
|
239
|
-
* @template {FuncInput extends z.infer<RunInput> | ToolCall = z.infer<RunInput>} FuncInput The input type for the function.
|
|
230
|
+
* @template {ZodObjectAny | z.ZodString = ZodObjectAny} T The input schema for the tool. Either any Zod object, or a Zod string.
|
|
240
231
|
*
|
|
241
|
-
* @param {RunnableFunc<z.
|
|
242
|
-
* @param fields - An object containing the following properties:
|
|
232
|
+
* @param {RunnableFunc<z.output<T>, ToolReturnType>} func - The function to invoke when the tool is called.
|
|
233
|
+
* @param {ToolWrapperParams<T>} fields - An object containing the following properties:
|
|
243
234
|
* @param {string} fields.name The name of the tool.
|
|
244
235
|
* @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
|
|
245
|
-
* @param {z.
|
|
236
|
+
* @param {ZodObjectAny | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.
|
|
246
237
|
*
|
|
247
|
-
* @returns {DynamicStructuredTool<
|
|
238
|
+
* @returns {DynamicStructuredTool<T>} A new StructuredTool instance.
|
|
248
239
|
*/
|
|
249
|
-
export declare function tool<T extends
|
|
250
|
-
export
|
|
240
|
+
export declare function tool<T extends z.ZodString = z.ZodString>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicTool;
|
|
241
|
+
export declare function tool<T extends ZodObjectAny = ZodObjectAny>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
|
package/dist/tools/index.js
CHANGED
|
@@ -4,23 +4,8 @@ import { BaseLangChain, } from "../language_models/base.js";
|
|
|
4
4
|
import { ensureConfig, patchConfig, } from "../runnables/config.js";
|
|
5
5
|
import { ToolMessage } from "../messages/tool.js";
|
|
6
6
|
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* It extends the built-in `Error` class and adds an optional `output`
|
|
10
|
-
* property that can hold the output that caused the exception.
|
|
11
|
-
*/
|
|
12
|
-
export class ToolInputParsingException extends Error {
|
|
13
|
-
constructor(message, output) {
|
|
14
|
-
super(message);
|
|
15
|
-
Object.defineProperty(this, "output", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
writable: true,
|
|
19
|
-
value: void 0
|
|
20
|
-
});
|
|
21
|
-
this.output = output;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
7
|
+
import { _isToolCall, ToolInputParsingException } from "./utils.js";
|
|
8
|
+
export { ToolInputParsingException };
|
|
24
9
|
/**
|
|
25
10
|
* Base class for Tools that accept input of any shape defined by a Zod schema.
|
|
26
11
|
*/
|
|
@@ -282,29 +267,23 @@ export class BaseToolkit {
|
|
|
282
267
|
return this.tools;
|
|
283
268
|
}
|
|
284
269
|
}
|
|
285
|
-
/**
|
|
286
|
-
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
|
|
287
|
-
* @function
|
|
288
|
-
* @template {RunInput extends ZodAny = ZodAny} RunInput The input schema for the tool. This corresponds to the input type when the tool is invoked.
|
|
289
|
-
* @template {RunOutput = any} RunOutput The output type for the tool. This corresponds to the output type when the tool is invoked.
|
|
290
|
-
* @template {FuncInput extends z.infer<RunInput> | ToolCall = z.infer<RunInput>} FuncInput The input type for the function.
|
|
291
|
-
*
|
|
292
|
-
* @param {RunnableFunc<z.infer<RunInput> | ToolCall, RunOutput>} func - The function to invoke when the tool is called.
|
|
293
|
-
* @param fields - An object containing the following properties:
|
|
294
|
-
* @param {string} fields.name The name of the tool.
|
|
295
|
-
* @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
|
|
296
|
-
* @param {z.ZodObject<any, any, any, any>} fields.schema The Zod schema defining the input for the tool.
|
|
297
|
-
*
|
|
298
|
-
* @returns {DynamicStructuredTool<RunInput, RunOutput>} A new StructuredTool instance.
|
|
299
|
-
*/
|
|
300
270
|
export function tool(func, fields) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
271
|
+
// If the schema is not provided, or it's a string schema, create a DynamicTool
|
|
272
|
+
if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
|
|
273
|
+
return new DynamicTool({
|
|
274
|
+
name: fields.name,
|
|
275
|
+
description: fields.description ??
|
|
276
|
+
fields.schema?.description ??
|
|
277
|
+
`${fields.name} tool`,
|
|
278
|
+
responseFormat: fields.responseFormat,
|
|
279
|
+
func,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
const description = fields.description ?? fields.schema.description ?? `${fields.name} tool`;
|
|
304
283
|
return new DynamicStructuredTool({
|
|
305
284
|
name: fields.name,
|
|
306
285
|
description,
|
|
307
|
-
schema: schema,
|
|
286
|
+
schema: fields.schema,
|
|
308
287
|
// TODO: Consider moving into DynamicStructuredTool constructor
|
|
309
288
|
func: async (input, runManager, config) => {
|
|
310
289
|
return new Promise((resolve, reject) => {
|
|
@@ -324,12 +303,6 @@ export function tool(func, fields) {
|
|
|
324
303
|
responseFormat: fields.responseFormat,
|
|
325
304
|
});
|
|
326
305
|
}
|
|
327
|
-
function _isToolCall(toolCall) {
|
|
328
|
-
return !!(toolCall &&
|
|
329
|
-
typeof toolCall === "object" &&
|
|
330
|
-
"type" in toolCall &&
|
|
331
|
-
toolCall.type === "tool_call");
|
|
332
|
-
}
|
|
333
306
|
function _formatToolOutput(params) {
|
|
334
307
|
const { content, artifact, toolCallId } = params;
|
|
335
308
|
if (toolCallId) {
|
|
@@ -72,3 +72,14 @@ test("Returns tool message if responseFormat is content_and_artifact and returns
|
|
|
72
72
|
expect(toolResult.artifact).toEqual({ location: "San Francisco" });
|
|
73
73
|
expect(toolResult.name).toBe("weather");
|
|
74
74
|
});
|
|
75
|
+
test("Tool can accept single string input", async () => {
|
|
76
|
+
const stringTool = tool((input) => {
|
|
77
|
+
return `${input}a`;
|
|
78
|
+
}, {
|
|
79
|
+
name: "string_tool",
|
|
80
|
+
description: "A tool that appends 'a' to the input string",
|
|
81
|
+
schema: z.string(),
|
|
82
|
+
});
|
|
83
|
+
const result = await stringTool.invoke("b");
|
|
84
|
+
expect(result).toBe("ba");
|
|
85
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToolInputParsingException = exports._isToolCall = void 0;
|
|
4
|
+
function _isToolCall(toolCall) {
|
|
5
|
+
return !!(toolCall &&
|
|
6
|
+
typeof toolCall === "object" &&
|
|
7
|
+
"type" in toolCall &&
|
|
8
|
+
toolCall.type === "tool_call");
|
|
9
|
+
}
|
|
10
|
+
exports._isToolCall = _isToolCall;
|
|
11
|
+
/**
|
|
12
|
+
* Custom error class used to handle exceptions related to tool input parsing.
|
|
13
|
+
* It extends the built-in `Error` class and adds an optional `output`
|
|
14
|
+
* property that can hold the output that caused the exception.
|
|
15
|
+
*/
|
|
16
|
+
class ToolInputParsingException extends Error {
|
|
17
|
+
constructor(message, output) {
|
|
18
|
+
super(message);
|
|
19
|
+
Object.defineProperty(this, "output", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: void 0
|
|
24
|
+
});
|
|
25
|
+
this.output = output;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.ToolInputParsingException = ToolInputParsingException;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ToolCall } from "../messages/tool.js";
|
|
2
|
+
export declare function _isToolCall(toolCall?: unknown): toolCall is ToolCall;
|
|
3
|
+
/**
|
|
4
|
+
* Custom error class used to handle exceptions related to tool input parsing.
|
|
5
|
+
* It extends the built-in `Error` class and adds an optional `output`
|
|
6
|
+
* property that can hold the output that caused the exception.
|
|
7
|
+
*/
|
|
8
|
+
export declare class ToolInputParsingException extends Error {
|
|
9
|
+
output?: string;
|
|
10
|
+
constructor(message: string, output?: string);
|
|
11
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function _isToolCall(toolCall) {
|
|
2
|
+
return !!(toolCall &&
|
|
3
|
+
typeof toolCall === "object" &&
|
|
4
|
+
"type" in toolCall &&
|
|
5
|
+
toolCall.type === "tool_call");
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Custom error class used to handle exceptions related to tool input parsing.
|
|
9
|
+
* It extends the built-in `Error` class and adds an optional `output`
|
|
10
|
+
* property that can hold the output that caused the exception.
|
|
11
|
+
*/
|
|
12
|
+
export class ToolInputParsingException extends Error {
|
|
13
|
+
constructor(message, output) {
|
|
14
|
+
super(message);
|
|
15
|
+
Object.defineProperty(this, "output", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
this.output = output;
|
|
22
|
+
}
|
|
23
|
+
}
|
package/dist/types/zod.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { z } from "zod";
|
|
2
|
-
export type
|
|
2
|
+
export type ZodObjectAny = z.ZodObject<any, any, any, any>;
|