@langchain/core 0.2.15 → 0.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/caches.cjs +1 -1
  2. package/caches.d.cts +1 -1
  3. package/caches.d.ts +1 -1
  4. package/caches.js +1 -1
  5. package/callbacks/dispatch/web.cjs +1 -0
  6. package/callbacks/dispatch/web.d.cts +1 -0
  7. package/callbacks/dispatch/web.d.ts +1 -0
  8. package/callbacks/dispatch/web.js +1 -0
  9. package/callbacks/dispatch.cjs +1 -0
  10. package/callbacks/dispatch.d.cts +1 -0
  11. package/callbacks/dispatch.d.ts +1 -0
  12. package/callbacks/dispatch.js +1 -0
  13. package/dist/{caches.cjs → caches/base.cjs} +6 -6
  14. package/dist/{caches.d.ts → caches/base.d.ts} +7 -7
  15. package/dist/{caches.js → caches/base.js} +6 -6
  16. package/dist/caches/tests/in_memory_cache.test.d.ts +1 -0
  17. package/dist/caches/tests/in_memory_cache.test.js +33 -0
  18. package/dist/callbacks/base.cjs +8 -0
  19. package/dist/callbacks/base.d.ts +16 -10
  20. package/dist/callbacks/base.js +8 -0
  21. package/dist/callbacks/dispatch/index.cjs +49 -0
  22. package/dist/callbacks/dispatch/index.d.ts +35 -0
  23. package/dist/callbacks/dispatch/index.js +45 -0
  24. package/dist/callbacks/dispatch/web.cjs +61 -0
  25. package/dist/callbacks/dispatch/web.d.ts +32 -0
  26. package/dist/callbacks/dispatch/web.js +57 -0
  27. package/dist/callbacks/manager.cjs +20 -0
  28. package/dist/callbacks/manager.d.ts +2 -1
  29. package/dist/callbacks/manager.js +20 -0
  30. package/dist/language_models/base.cjs +4 -4
  31. package/dist/language_models/base.d.ts +2 -2
  32. package/dist/language_models/base.js +1 -1
  33. package/dist/language_models/chat_models.d.ts +4 -4
  34. package/dist/language_models/llms.d.ts +1 -1
  35. package/dist/language_models/tests/chat_models.test.js +33 -0
  36. package/dist/load/import_map.cjs +2 -2
  37. package/dist/load/import_map.d.ts +2 -2
  38. package/dist/load/import_map.js +2 -2
  39. package/dist/messages/ai.cjs +2 -0
  40. package/dist/messages/ai.js +2 -0
  41. package/dist/messages/base.cjs +45 -5
  42. package/dist/messages/base.d.ts +1 -0
  43. package/dist/messages/base.js +43 -4
  44. package/dist/messages/index.d.ts +1 -1
  45. package/dist/messages/tests/base_message.test.js +134 -2
  46. package/dist/messages/tests/message_utils.test.js +54 -2
  47. package/dist/messages/tool.cjs +31 -0
  48. package/dist/messages/tool.d.ts +27 -0
  49. package/dist/messages/tool.js +32 -1
  50. package/dist/messages/transformers.cjs +1 -0
  51. package/dist/messages/transformers.js +1 -0
  52. package/dist/messages/utils.cjs +5 -1
  53. package/dist/messages/utils.js +5 -1
  54. package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +2 -0
  55. package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +2 -0
  56. package/dist/runnables/base.cjs +73 -1
  57. package/dist/runnables/base.d.ts +49 -0
  58. package/dist/runnables/base.js +70 -0
  59. package/dist/runnables/index.cjs +2 -1
  60. package/dist/runnables/index.d.ts +1 -1
  61. package/dist/runnables/index.js +1 -1
  62. package/dist/runnables/tests/runnable_stream_events.test.js +1 -1
  63. package/dist/runnables/tests/runnable_stream_events_v2.test.js +106 -1
  64. package/dist/runnables/tests/runnable_tools.test.d.ts +1 -0
  65. package/dist/runnables/tests/runnable_tools.test.js +111 -0
  66. package/dist/{tools.cjs → tools/index.cjs} +130 -14
  67. package/dist/{tools.d.ts → tools/index.d.ts} +69 -31
  68. package/dist/{tools.js → tools/index.js} +130 -14
  69. package/dist/tools/tests/tools.test.d.ts +1 -0
  70. package/dist/tools/tests/tools.test.js +74 -0
  71. package/dist/tracers/base.cjs +1 -0
  72. package/dist/tracers/base.d.ts +1 -1
  73. package/dist/tracers/base.js +1 -0
  74. package/dist/tracers/event_stream.cjs +15 -0
  75. package/dist/tracers/event_stream.d.ts +1 -0
  76. package/dist/tracers/event_stream.js +15 -0
  77. package/dist/types/zod.cjs +2 -0
  78. package/dist/types/zod.d.ts +2 -0
  79. package/dist/types/zod.js +1 -0
  80. package/dist/utils/function_calling.cjs +38 -10
  81. package/dist/utils/function_calling.d.ts +32 -11
  82. package/dist/utils/function_calling.js +36 -9
  83. package/dist/utils/testing/index.cjs +10 -3
  84. package/dist/utils/testing/index.d.ts +1 -1
  85. package/dist/utils/testing/index.js +9 -2
  86. package/package.json +28 -1
  87. package/tools.cjs +1 -1
  88. package/tools.d.cts +1 -1
  89. package/tools.d.ts +1 -1
  90. package/tools.js +1 -1
@@ -3,6 +3,7 @@ import { filterMessages, mergeMessageRuns, trimMessages, } from "../transformers
3
3
  import { AIMessage } from "../ai.js";
4
4
  import { HumanMessage } from "../human.js";
5
5
  import { SystemMessage } from "../system.js";
6
+ import { getBufferString } from "../utils.js";
6
7
  describe("filterMessage", () => {
7
8
  const getMessages = () => [
8
9
  new SystemMessage("you're a good assistant."),
@@ -97,8 +98,8 @@ describe("mergeMessageRuns", () => {
97
98
  { type: "text", text: "my favorite dish is lasagna" },
98
99
  ],
99
100
  tool_calls: [
100
- { name: "blah_tool", args: { x: 2 }, id: "123" },
101
- { name: "blah_tool", args: { x: -10 }, id: "456" },
101
+ { name: "blah_tool", args: { x: 2 }, id: "123", type: "tool_call" },
102
+ { name: "blah_tool", args: { x: -10 }, id: "456", type: "tool_call" },
102
103
  ],
103
104
  id: "baz",
104
105
  }),
@@ -380,3 +381,54 @@ describe("trimMessages can trim", () => {
380
381
  expect(typeof trimmedMessages.func).toBe("function");
381
382
  });
382
383
  });
384
+ test("getBufferString can handle complex messages", () => {
385
+ const messageArr1 = [new HumanMessage("Hello there!")];
386
+ const messageArr2 = [
387
+ new AIMessage({
388
+ content: [
389
+ {
390
+ type: "text",
391
+ text: "Hello there!",
392
+ },
393
+ ],
394
+ }),
395
+ ];
396
+ const messageArr3 = [
397
+ new HumanMessage({
398
+ content: [
399
+ {
400
+ type: "image_url",
401
+ image_url: {
402
+ url: "https://example.com/image.jpg",
403
+ },
404
+ },
405
+ {
406
+ type: "image_url",
407
+ image_url: "https://example.com/image.jpg",
408
+ },
409
+ ],
410
+ }),
411
+ ];
412
+ const bufferString1 = getBufferString(messageArr1);
413
+ expect(bufferString1).toBe("Human: Hello there!");
414
+ const bufferString2 = getBufferString(messageArr2);
415
+ expect(bufferString2).toBe(`AI: ${JSON.stringify([
416
+ {
417
+ type: "text",
418
+ text: "Hello there!",
419
+ },
420
+ ], null, 2)}`);
421
+ const bufferString3 = getBufferString(messageArr3);
422
+ expect(bufferString3).toBe(`Human: ${JSON.stringify([
423
+ {
424
+ type: "image_url",
425
+ image_url: {
426
+ url: "https://example.com/image.jpg",
427
+ },
428
+ },
429
+ {
430
+ type: "image_url",
431
+ image_url: "https://example.com/image.jpg",
432
+ },
433
+ ], null, 2)}`);
434
+ });
@@ -25,7 +25,22 @@ class ToolMessage extends base_js_1.BaseMessage {
25
25
  writable: true,
26
26
  value: void 0
27
27
  });
28
+ /**
29
+ * Artifact of the Tool execution which is not meant to be sent to the model.
30
+ *
31
+ * Should only be specified if it is different from the message content, e.g. if only
32
+ * a subset of the full tool output is being passed as message content but the full
33
+ * output is needed in other parts of the code.
34
+ */
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ Object.defineProperty(this, "artifact", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
28
42
  this.tool_call_id = fields.tool_call_id;
43
+ this.artifact = fields.artifact;
29
44
  }
30
45
  _getType() {
31
46
  return "tool";
@@ -48,7 +63,22 @@ class ToolMessageChunk extends base_js_1.BaseMessageChunk {
48
63
  writable: true,
49
64
  value: void 0
50
65
  });
66
+ /**
67
+ * Artifact of the Tool execution which is not meant to be sent to the model.
68
+ *
69
+ * Should only be specified if it is different from the message content, e.g. if only
70
+ * a subset of the full tool output is being passed as message content but the full
71
+ * output is needed in other parts of the code.
72
+ */
73
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+ Object.defineProperty(this, "artifact", {
75
+ enumerable: true,
76
+ configurable: true,
77
+ writable: true,
78
+ value: void 0
79
+ });
51
80
  this.tool_call_id = fields.tool_call_id;
81
+ this.artifact = fields.artifact;
52
82
  }
53
83
  static lc_name() {
54
84
  return "ToolMessageChunk";
@@ -61,6 +91,7 @@ class ToolMessageChunk extends base_js_1.BaseMessageChunk {
61
91
  content: (0, base_js_1.mergeContent)(this.content, chunk.content),
62
92
  additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
63
93
  response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
94
+ artifact: (0, base_js_1._mergeObj)(this.artifact, chunk.artifact),
64
95
  tool_call_id: this.tool_call_id,
65
96
  id: this.id ?? chunk.id,
66
97
  });
@@ -1,5 +1,13 @@
1
1
  import { BaseMessage, BaseMessageChunk, type BaseMessageFields, type MessageType } from "./base.js";
2
2
  export interface ToolMessageFieldsWithToolCallId extends BaseMessageFields {
3
+ /**
4
+ * Artifact of the Tool execution which is not meant to be sent to the model.
5
+ *
6
+ * Should only be specified if it is different from the message content, e.g. if only
7
+ * a subset of the full tool output is being passed as message content but the full
8
+ * output is needed in other parts of the code.
9
+ */
10
+ artifact?: any;
3
11
  tool_call_id: string;
4
12
  }
5
13
  /**
@@ -9,6 +17,14 @@ export declare class ToolMessage extends BaseMessage {
9
17
  static lc_name(): string;
10
18
  get lc_aliases(): Record<string, string>;
11
19
  tool_call_id: string;
20
+ /**
21
+ * Artifact of the Tool execution which is not meant to be sent to the model.
22
+ *
23
+ * Should only be specified if it is different from the message content, e.g. if only
24
+ * a subset of the full tool output is being passed as message content but the full
25
+ * output is needed in other parts of the code.
26
+ */
27
+ artifact?: any;
12
28
  constructor(fields: ToolMessageFieldsWithToolCallId);
13
29
  constructor(fields: string | BaseMessageFields, tool_call_id: string, name?: string);
14
30
  _getType(): MessageType;
@@ -20,6 +36,14 @@ export declare class ToolMessage extends BaseMessage {
20
36
  */
21
37
  export declare class ToolMessageChunk extends BaseMessageChunk {
22
38
  tool_call_id: string;
39
+ /**
40
+ * Artifact of the Tool execution which is not meant to be sent to the model.
41
+ *
42
+ * Should only be specified if it is different from the message content, e.g. if only
43
+ * a subset of the full tool output is being passed as message content but the full
44
+ * output is needed in other parts of the code.
45
+ */
46
+ artifact?: any;
23
47
  constructor(fields: ToolMessageFieldsWithToolCallId);
24
48
  static lc_name(): string;
25
49
  _getType(): MessageType;
@@ -35,6 +59,7 @@ export type ToolCall = {
35
59
  name: string;
36
60
  args: Record<string, any>;
37
61
  id?: string;
62
+ type?: "tool_call";
38
63
  };
39
64
  /**
40
65
  * A chunk of a tool call (e.g., as part of a stream).
@@ -91,11 +116,13 @@ export type ToolCallChunk = {
91
116
  args?: string;
92
117
  id?: string;
93
118
  index?: number;
119
+ type?: "tool_call_chunk";
94
120
  };
95
121
  export type InvalidToolCall = {
96
122
  name?: string;
97
123
  args?: string;
98
124
  id?: string;
99
125
  error?: string;
126
+ type?: "invalid_tool_call";
100
127
  };
101
128
  export declare function defaultToolCallParser(rawToolCalls: Record<string, any>[]): [ToolCall[], InvalidToolCall[]];
@@ -1,4 +1,4 @@
1
- import { BaseMessage, BaseMessageChunk, mergeContent, _mergeDicts, } from "./base.js";
1
+ import { BaseMessage, BaseMessageChunk, mergeContent, _mergeDicts, _mergeObj, } from "./base.js";
2
2
  /**
3
3
  * Represents a tool message in a conversation.
4
4
  */
@@ -22,7 +22,22 @@ export class ToolMessage extends BaseMessage {
22
22
  writable: true,
23
23
  value: void 0
24
24
  });
25
+ /**
26
+ * Artifact of the Tool execution which is not meant to be sent to the model.
27
+ *
28
+ * Should only be specified if it is different from the message content, e.g. if only
29
+ * a subset of the full tool output is being passed as message content but the full
30
+ * output is needed in other parts of the code.
31
+ */
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ Object.defineProperty(this, "artifact", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
25
39
  this.tool_call_id = fields.tool_call_id;
40
+ this.artifact = fields.artifact;
26
41
  }
27
42
  _getType() {
28
43
  return "tool";
@@ -44,7 +59,22 @@ export class ToolMessageChunk extends BaseMessageChunk {
44
59
  writable: true,
45
60
  value: void 0
46
61
  });
62
+ /**
63
+ * Artifact of the Tool execution which is not meant to be sent to the model.
64
+ *
65
+ * Should only be specified if it is different from the message content, e.g. if only
66
+ * a subset of the full tool output is being passed as message content but the full
67
+ * output is needed in other parts of the code.
68
+ */
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ Object.defineProperty(this, "artifact", {
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true,
74
+ value: void 0
75
+ });
47
76
  this.tool_call_id = fields.tool_call_id;
77
+ this.artifact = fields.artifact;
48
78
  }
49
79
  static lc_name() {
50
80
  return "ToolMessageChunk";
@@ -57,6 +87,7 @@ export class ToolMessageChunk extends BaseMessageChunk {
57
87
  content: mergeContent(this.content, chunk.content),
58
88
  additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
59
89
  response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
90
+ artifact: _mergeObj(this.artifact, chunk.artifact),
60
91
  tool_call_id: this.tool_call_id,
61
92
  id: this.id ?? chunk.id,
62
93
  });
@@ -326,6 +326,7 @@ function _switchTypeToMessage(messageType, fields, returnChunk) {
326
326
  ...aiChunkFields,
327
327
  tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
328
328
  ...tc,
329
+ type: "tool_call_chunk",
329
330
  index: undefined,
330
331
  args: JSON.stringify(tc.args),
331
332
  })),
@@ -320,6 +320,7 @@ function _switchTypeToMessage(messageType, fields, returnChunk) {
320
320
  ...aiChunkFields,
321
321
  tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
322
322
  ...tc,
323
+ type: "tool_call_chunk",
323
324
  index: undefined,
324
325
  args: JSON.stringify(tc.args),
325
326
  })),
@@ -69,7 +69,10 @@ function getBufferString(messages, humanPrefix = "Human", aiPrefix = "AI") {
69
69
  throw new Error(`Got unsupported message type: ${m._getType()}`);
70
70
  }
71
71
  const nameStr = m.name ? `${m.name}, ` : "";
72
- string_messages.push(`${role}: ${nameStr}${m.content}`);
72
+ const readableContent = typeof m.content === "string"
73
+ ? m.content
74
+ : JSON.stringify(m.content, null, 2);
75
+ string_messages.push(`${role}: ${nameStr}${readableContent}`);
73
76
  }
74
77
  return string_messages.join("\n");
75
78
  }
@@ -166,6 +169,7 @@ function convertToChunk(message) {
166
169
  ...aiChunkFields,
167
170
  tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
168
171
  ...tc,
172
+ type: "tool_call_chunk",
169
173
  index: undefined,
170
174
  args: JSON.stringify(tc.args),
171
175
  })),
@@ -65,7 +65,10 @@ export function getBufferString(messages, humanPrefix = "Human", aiPrefix = "AI"
65
65
  throw new Error(`Got unsupported message type: ${m._getType()}`);
66
66
  }
67
67
  const nameStr = m.name ? `${m.name}, ` : "";
68
- string_messages.push(`${role}: ${nameStr}${m.content}`);
68
+ const readableContent = typeof m.content === "string"
69
+ ? m.content
70
+ : JSON.stringify(m.content, null, 2);
71
+ string_messages.push(`${role}: ${nameStr}${readableContent}`);
69
72
  }
70
73
  return string_messages.join("\n");
71
74
  }
@@ -158,6 +161,7 @@ export function convertToChunk(message) {
158
161
  ...aiChunkFields,
159
162
  tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
160
163
  ...tc,
164
+ type: "tool_call_chunk",
161
165
  index: undefined,
162
166
  args: JSON.stringify(tc.args),
163
167
  })),
@@ -37,6 +37,7 @@ rawToolCall, options) {
37
37
  const parsedToolCall = {
38
38
  name: rawToolCall.function.name,
39
39
  args: functionArgs,
40
+ type: "tool_call",
40
41
  };
41
42
  if (options?.returnId) {
42
43
  parsedToolCall.id = rawToolCall.id;
@@ -66,6 +67,7 @@ rawToolCall, errorMsg) {
66
67
  args: rawToolCall.function?.arguments,
67
68
  id: rawToolCall.id,
68
69
  error: errorMsg,
70
+ type: "invalid_tool_call",
69
71
  };
70
72
  }
71
73
  exports.makeInvalidToolCall = makeInvalidToolCall;
@@ -34,6 +34,7 @@ rawToolCall, options) {
34
34
  const parsedToolCall = {
35
35
  name: rawToolCall.function.name,
36
36
  args: functionArgs,
37
+ type: "tool_call",
37
38
  };
38
39
  if (options?.returnId) {
39
40
  parsedToolCall.id = rawToolCall.id;
@@ -61,6 +62,7 @@ rawToolCall, errorMsg) {
61
62
  args: rawToolCall.function?.arguments,
62
63
  id: rawToolCall.id,
63
64
  error: errorMsg,
65
+ type: "invalid_tool_call",
64
66
  };
65
67
  }
66
68
  /**
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableTraceable = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
6
+ exports.convertRunnableToTool = exports.RunnableToolLike = exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableTraceable = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
7
7
  const zod_1 = require("zod");
8
8
  const p_retry_1 = __importDefault(require("p-retry"));
9
9
  const uuid_1 = require("uuid");
@@ -718,6 +718,21 @@ class Runnable extends serializable_js_1.Serializable {
718
718
  ],
719
719
  });
720
720
  }
721
+ /**
722
+ * Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
723
+ * which contains the runnable, name, description and schema.
724
+ *
725
+ * @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
726
+ *
727
+ * @param fields
728
+ * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
729
+ * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
730
+ * @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
731
+ * @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
732
+ */
733
+ asTool(fields) {
734
+ return convertRunnableToTool(this, fields);
735
+ }
721
736
  }
722
737
  exports.Runnable = Runnable;
723
738
  /**
@@ -1912,3 +1927,60 @@ class RunnablePick extends Runnable {
1912
1927
  }
1913
1928
  }
1914
1929
  exports.RunnablePick = RunnablePick;
1930
+ class RunnableToolLike extends RunnableBinding {
1931
+ constructor(fields) {
1932
+ super({
1933
+ bound: fields.bound,
1934
+ config: fields.config ?? {},
1935
+ });
1936
+ Object.defineProperty(this, "name", {
1937
+ enumerable: true,
1938
+ configurable: true,
1939
+ writable: true,
1940
+ value: void 0
1941
+ });
1942
+ Object.defineProperty(this, "description", {
1943
+ enumerable: true,
1944
+ configurable: true,
1945
+ writable: true,
1946
+ value: void 0
1947
+ });
1948
+ Object.defineProperty(this, "schema", {
1949
+ enumerable: true,
1950
+ configurable: true,
1951
+ writable: true,
1952
+ value: void 0
1953
+ });
1954
+ this.name = fields.name;
1955
+ this.description = fields.description;
1956
+ this.schema = fields.schema;
1957
+ }
1958
+ static lc_name() {
1959
+ return "RunnableToolLike";
1960
+ }
1961
+ }
1962
+ exports.RunnableToolLike = RunnableToolLike;
1963
+ /**
1964
+ * Given a runnable and a Zod schema, convert the runnable to a tool.
1965
+ *
1966
+ * @template RunInput The input type for the runnable.
1967
+ * @template RunOutput The output type for the runnable.
1968
+ *
1969
+ * @param {Runnable<RunInput, RunOutput>} runnable The runnable to convert to a tool.
1970
+ * @param fields
1971
+ * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
1972
+ * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
1973
+ * @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
1974
+ * @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
1975
+ */
1976
+ function convertRunnableToTool(runnable, fields) {
1977
+ const name = fields.name ?? runnable.getName();
1978
+ const description = fields.description ?? fields.schema.description;
1979
+ return new RunnableToolLike({
1980
+ name,
1981
+ description,
1982
+ schema: fields.schema,
1983
+ bound: runnable,
1984
+ });
1985
+ }
1986
+ exports.convertRunnableToTool = convertRunnableToTool;
@@ -1,3 +1,4 @@
1
+ import { z } from "zod";
1
2
  import { type TraceableFunction } from "langsmith/singletons/traceable";
2
3
  import type { RunnableInterface, RunnableBatchOptions } from "./types.js";
3
4
  import { CallbackManagerForChainRun } from "../callbacks/manager.js";
@@ -245,6 +246,23 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
245
246
  onEnd?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
246
247
  onError?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
247
248
  }): Runnable<RunInput, RunOutput, CallOptions>;
249
+ /**
250
+ * Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
251
+ * which contains the runnable, name, description and schema.
252
+ *
253
+ * @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
254
+ *
255
+ * @param fields
256
+ * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
257
+ * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
258
+ * @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
259
+ * @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
260
+ */
261
+ asTool<T extends RunInput = RunInput>(fields: {
262
+ name?: string;
263
+ description?: string;
264
+ schema: z.ZodType<T>;
265
+ }): RunnableToolLike<z.ZodType<T>, RunOutput>;
248
266
  }
249
267
  export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = {
250
268
  bound: Runnable<RunInput, RunOutput, CallOptions>;
@@ -560,3 +578,34 @@ export declare class RunnablePick<RunInput extends Record<string, any> = Record<
560
578
  transform(generator: AsyncGenerator<RunInput>, options?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
561
579
  stream(input: RunInput, options?: Partial<RunnableConfig>): Promise<IterableReadableStream<RunOutput>>;
562
580
  }
581
+ export interface RunnableToolLikeArgs<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends Omit<RunnableBindingArgs<z.infer<RunInput>, RunOutput>, "config"> {
582
+ name: string;
583
+ description?: string;
584
+ schema: RunInput;
585
+ config?: RunnableConfig;
586
+ }
587
+ export declare class RunnableToolLike<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends RunnableBinding<z.infer<RunInput>, RunOutput> {
588
+ name: string;
589
+ description?: string;
590
+ schema: RunInput;
591
+ constructor(fields: RunnableToolLikeArgs<RunInput, RunOutput>);
592
+ static lc_name(): string;
593
+ }
594
+ /**
595
+ * Given a runnable and a Zod schema, convert the runnable to a tool.
596
+ *
597
+ * @template RunInput The input type for the runnable.
598
+ * @template RunOutput The output type for the runnable.
599
+ *
600
+ * @param {Runnable<RunInput, RunOutput>} runnable The runnable to convert to a tool.
601
+ * @param fields
602
+ * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
603
+ * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
604
+ * @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
605
+ * @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
606
+ */
607
+ export declare function convertRunnableToTool<RunInput, RunOutput>(runnable: Runnable<RunInput, RunOutput>, fields: {
608
+ name?: string;
609
+ description?: string;
610
+ schema: z.ZodType<RunInput>;
611
+ }): RunnableToolLike<z.ZodType<RunInput>, RunOutput>;
@@ -711,6 +711,21 @@ export class Runnable extends Serializable {
711
711
  ],
712
712
  });
713
713
  }
714
+ /**
715
+ * Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
716
+ * which contains the runnable, name, description and schema.
717
+ *
718
+ * @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
719
+ *
720
+ * @param fields
721
+ * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
722
+ * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
723
+ * @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
724
+ * @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
725
+ */
726
+ asTool(fields) {
727
+ return convertRunnableToTool(this, fields);
728
+ }
714
729
  }
715
730
  /**
716
731
  * A runnable that delegates calls to another runnable with a set of kwargs.
@@ -1892,3 +1907,58 @@ export class RunnablePick extends Runnable {
1892
1907
  return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
1893
1908
  }
1894
1909
  }
1910
+ export class RunnableToolLike extends RunnableBinding {
1911
+ constructor(fields) {
1912
+ super({
1913
+ bound: fields.bound,
1914
+ config: fields.config ?? {},
1915
+ });
1916
+ Object.defineProperty(this, "name", {
1917
+ enumerable: true,
1918
+ configurable: true,
1919
+ writable: true,
1920
+ value: void 0
1921
+ });
1922
+ Object.defineProperty(this, "description", {
1923
+ enumerable: true,
1924
+ configurable: true,
1925
+ writable: true,
1926
+ value: void 0
1927
+ });
1928
+ Object.defineProperty(this, "schema", {
1929
+ enumerable: true,
1930
+ configurable: true,
1931
+ writable: true,
1932
+ value: void 0
1933
+ });
1934
+ this.name = fields.name;
1935
+ this.description = fields.description;
1936
+ this.schema = fields.schema;
1937
+ }
1938
+ static lc_name() {
1939
+ return "RunnableToolLike";
1940
+ }
1941
+ }
1942
+ /**
1943
+ * Given a runnable and a Zod schema, convert the runnable to a tool.
1944
+ *
1945
+ * @template RunInput The input type for the runnable.
1946
+ * @template RunOutput The output type for the runnable.
1947
+ *
1948
+ * @param {Runnable<RunInput, RunOutput>} runnable The runnable to convert to a tool.
1949
+ * @param fields
1950
+ * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
1951
+ * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
1952
+ * @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
1953
+ * @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
1954
+ */
1955
+ export function convertRunnableToTool(runnable, fields) {
1956
+ const name = fields.name ?? runnable.getName();
1957
+ const description = fields.description ?? fields.schema.description;
1958
+ return new RunnableToolLike({
1959
+ name,
1960
+ description,
1961
+ schema: fields.schema,
1962
+ bound: runnable,
1963
+ });
1964
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.mergeConfigs = exports.ensureConfig = exports.patchConfig = exports.getCallbackManagerForConfig = exports._coerceToRunnable = exports.RunnablePick = exports.RunnableAssign = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableParallel = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
3
+ exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.mergeConfigs = exports.ensureConfig = exports.patchConfig = exports.getCallbackManagerForConfig = exports.RunnableToolLike = exports._coerceToRunnable = exports.RunnablePick = exports.RunnableAssign = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableParallel = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
4
4
  var base_js_1 = require("./base.cjs");
5
5
  Object.defineProperty(exports, "Runnable", { enumerable: true, get: function () { return base_js_1.Runnable; } });
6
6
  Object.defineProperty(exports, "RunnableBinding", { enumerable: true, get: function () { return base_js_1.RunnableBinding; } });
@@ -14,6 +14,7 @@ Object.defineProperty(exports, "RunnableWithFallbacks", { enumerable: true, get:
14
14
  Object.defineProperty(exports, "RunnableAssign", { enumerable: true, get: function () { return base_js_1.RunnableAssign; } });
15
15
  Object.defineProperty(exports, "RunnablePick", { enumerable: true, get: function () { return base_js_1.RunnablePick; } });
16
16
  Object.defineProperty(exports, "_coerceToRunnable", { enumerable: true, get: function () { return base_js_1._coerceToRunnable; } });
17
+ Object.defineProperty(exports, "RunnableToolLike", { enumerable: true, get: function () { return base_js_1.RunnableToolLike; } });
17
18
  var config_js_1 = require("./config.cjs");
18
19
  Object.defineProperty(exports, "getCallbackManagerForConfig", { enumerable: true, get: function () { return config_js_1.getCallbackManagerForConfig; } });
19
20
  Object.defineProperty(exports, "patchConfig", { enumerable: true, get: function () { return config_js_1.patchConfig; } });
@@ -1,4 +1,4 @@
1
- export { type RunnableFunc, type RunnableLike, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
1
+ export { type RunnableFunc, type RunnableLike, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, RunnableToolLike, type RunnableToolLikeArgs, } from "./base.js";
2
2
  export { type RunnableBatchOptions, type RunnableInterface, type RunnableIOSchema, } from "./types.js";
3
3
  export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
4
4
  export { RunnablePassthrough } from "./passthrough.js";
@@ -1,4 +1,4 @@
1
- export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
1
+ export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, RunnableToolLike, } from "./base.js";
2
2
  export { getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
3
3
  export { RunnablePassthrough } from "./passthrough.js";
4
4
  export { RouterRunnable } from "./router.js";
@@ -8,7 +8,7 @@ import { ChatPromptTemplate } from "../../prompts/chat.js";
8
8
  import { FakeListChatModel, FakeRetriever, FakeStreamingLLM, } from "../../utils/testing/index.js";
9
9
  import { AIMessageChunk, HumanMessage, SystemMessage, } from "../../messages/index.js";
10
10
  import { ChatGenerationChunk, GenerationChunk } from "../../outputs.js";
11
- import { DynamicStructuredTool, DynamicTool } from "../../tools.js";
11
+ import { DynamicStructuredTool, DynamicTool } from "../../tools/index.js";
12
12
  import { Document } from "../../documents/document.js";
13
13
  function reverse(s) {
14
14
  // Reverse a string.