@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
@@ -7,10 +7,12 @@ import { RunnableLambda, RunnableMap, RunnablePassthrough, RunnablePick, } from
7
7
  import { ChatPromptTemplate } from "../../prompts/chat.js";
8
8
  import { FakeChatModel, FakeLLM, FakeListChatModel, FakeRetriever, FakeStreamingLLM, } from "../../utils/testing/index.js";
9
9
  import { AIMessage, AIMessageChunk, HumanMessage, SystemMessage, } from "../../messages/index.js";
10
- import { DynamicStructuredTool, DynamicTool, tool } from "../../tools.js";
10
+ import { DynamicStructuredTool, DynamicTool, tool } from "../../tools/index.js";
11
11
  import { Document } from "../../documents/document.js";
12
12
  import { PromptTemplate } from "../../prompts/prompt.js";
13
13
  import { GenerationChunk } from "../../outputs.js";
14
+ // Import from web to avoid side-effects from AsyncLocalStorage
15
+ import { dispatchCustomEvent } from "../../callbacks/dispatch/web.js";
14
16
  function reverse(s) {
15
17
  // Reverse a string.
16
18
  return s.split("").reverse().join("");
@@ -1748,6 +1750,109 @@ test("Runnable streamEvents method with simple tools", async () => {
1748
1750
  },
1749
1751
  ]);
1750
1752
  });
1753
+ test("Runnable streamEvents method with a custom event", async () => {
1754
+ const lambda = RunnableLambda.from(async (params, config) => {
1755
+ await dispatchCustomEvent("testEvent", { someval: "test" }, config);
1756
+ await dispatchCustomEvent("testEvent", { someval: "test2" }, config);
1757
+ return JSON.stringify({ x: params.x, y: params.y });
1758
+ });
1759
+ const events = [];
1760
+ const eventStream = await lambda.streamEvents({ x: 1, y: "2" }, { version: "v2" });
1761
+ for await (const event of eventStream) {
1762
+ events.push(event);
1763
+ }
1764
+ expect(events).toEqual([
1765
+ {
1766
+ event: "on_chain_start",
1767
+ data: { input: { x: 1, y: "2" } },
1768
+ name: "RunnableLambda",
1769
+ tags: [],
1770
+ run_id: expect.any(String),
1771
+ metadata: {},
1772
+ },
1773
+ {
1774
+ event: "on_custom_event",
1775
+ run_id: expect.any(String),
1776
+ name: "testEvent",
1777
+ tags: [],
1778
+ metadata: {},
1779
+ data: { someval: "test" },
1780
+ },
1781
+ {
1782
+ event: "on_custom_event",
1783
+ run_id: expect.any(String),
1784
+ name: "testEvent",
1785
+ tags: [],
1786
+ metadata: {},
1787
+ data: { someval: "test2" },
1788
+ },
1789
+ {
1790
+ event: "on_chain_stream",
1791
+ run_id: expect.any(String),
1792
+ name: "RunnableLambda",
1793
+ tags: [],
1794
+ metadata: {},
1795
+ data: { chunk: '{"x":1,"y":"2"}' },
1796
+ },
1797
+ {
1798
+ event: "on_chain_end",
1799
+ data: { output: '{"x":1,"y":"2"}' },
1800
+ run_id: expect.any(String),
1801
+ name: "RunnableLambda",
1802
+ tags: [],
1803
+ metadata: {},
1804
+ },
1805
+ ]);
1806
+ });
1807
+ test("Custom event inside a custom tool", async () => {
1808
+ const customTool = tool(async (params, config) => {
1809
+ await dispatchCustomEvent("testEvent", { someval: "test" }, config);
1810
+ await dispatchCustomEvent("testEvent", { someval: "test2" }, config);
1811
+ return JSON.stringify({ x: params.x, y: params.y });
1812
+ }, {
1813
+ schema: z.object({ x: z.number(), y: z.string() }),
1814
+ name: "testtool",
1815
+ });
1816
+ const events = [];
1817
+ const eventStream = await customTool.streamEvents({ x: 1, y: "2" }, { version: "v2" });
1818
+ for await (const event of eventStream) {
1819
+ events.push(event);
1820
+ }
1821
+ expect(events).toEqual([
1822
+ {
1823
+ event: "on_tool_start",
1824
+ data: { input: { x: 1, y: "2" } },
1825
+ name: "testtool",
1826
+ tags: [],
1827
+ run_id: expect.any(String),
1828
+ metadata: {},
1829
+ },
1830
+ {
1831
+ event: "on_custom_event",
1832
+ run_id: expect.any(String),
1833
+ name: "testEvent",
1834
+ tags: [],
1835
+ metadata: {},
1836
+ data: { someval: "test" },
1837
+ },
1838
+ {
1839
+ event: "on_custom_event",
1840
+ run_id: expect.any(String),
1841
+ name: "testEvent",
1842
+ tags: [],
1843
+ metadata: {},
1844
+ data: { someval: "test2" },
1845
+ },
1846
+ {
1847
+ event: "on_tool_end",
1848
+ data: { output: '{"x":1,"y":"2"}' },
1849
+ run_id: expect.any(String),
1850
+ name: "testtool",
1851
+ tags: [],
1852
+ metadata: {},
1853
+ },
1854
+ ]);
1855
+ });
1751
1856
  test("Runnable streamEvents method with tools that return objects", async () => {
1752
1857
  const adderFunc = (_params) => {
1753
1858
  return JSON.stringify({ sum: 3 });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,111 @@
1
+ import { z } from "zod";
2
+ import { RunnableLambda, RunnableToolLike } from "../base.js";
3
+ test("Runnable asTool works", async () => {
4
+ const schema = z.object({
5
+ foo: z.string(),
6
+ });
7
+ const runnable = RunnableLambda.from((input, config) => {
8
+ return `${input.foo}${config?.configurable.foo}`;
9
+ });
10
+ const tool = runnable.asTool({
11
+ schema,
12
+ });
13
+ expect(tool).toBeInstanceOf(RunnableToolLike);
14
+ expect(tool.schema).toBe(schema);
15
+ expect(tool.name).toBe(runnable.getName());
16
+ });
17
+ test("Runnable asTool works with all populated fields", async () => {
18
+ const schema = z.object({
19
+ foo: z.string(),
20
+ });
21
+ const runnable = RunnableLambda.from((input, config) => {
22
+ return `${input.foo}${config?.configurable.foo}`;
23
+ });
24
+ const tool = runnable.asTool({
25
+ schema,
26
+ name: "test",
27
+ description: "test",
28
+ });
29
+ expect(tool).toBeInstanceOf(RunnableToolLike);
30
+ expect(tool.schema).toBe(schema);
31
+ expect(tool.description).toBe("test");
32
+ expect(tool.name).toBe("test");
33
+ });
34
+ test("Runnable asTool can invoke", async () => {
35
+ const schema = z.object({
36
+ foo: z.string(),
37
+ });
38
+ const runnable = RunnableLambda.from((input, config) => {
39
+ return `${input.foo}${config?.configurable.foo}`;
40
+ });
41
+ const tool = runnable.asTool({
42
+ schema,
43
+ });
44
+ const toolResponse = await tool.invoke({
45
+ foo: "bar",
46
+ }, {
47
+ configurable: {
48
+ foo: "bar",
49
+ },
50
+ });
51
+ expect(toolResponse).toBe("barbar");
52
+ });
53
+ test("asTool should type error with mismatched schema", async () => {
54
+ // asTool infers the type of the Zod schema from the existing runnable's RunInput generic.
55
+ // If the Zod schema does not match the RunInput, it should throw a type error.
56
+ const schema = z.object({
57
+ foo: z.string(),
58
+ });
59
+ const runnable = RunnableLambda.from((input, config) => {
60
+ return `${input.bar}${config?.configurable.foo}`;
61
+ });
62
+ runnable.asTool({
63
+ // @ts-expect-error - Should error. If this does not give a type error, the generics/typing of `asTool` is broken.
64
+ schema,
65
+ });
66
+ });
67
+ test("Create a runnable tool directly from RunnableToolLike", async () => {
68
+ const schema = z.object({
69
+ foo: z.string(),
70
+ });
71
+ const adderFunc = (_) => {
72
+ return Promise.resolve(true);
73
+ };
74
+ const tool = new RunnableToolLike({
75
+ schema,
76
+ name: "test",
77
+ description: "test",
78
+ bound: RunnableLambda.from(adderFunc),
79
+ });
80
+ const result = await tool.invoke({ foo: "bar" });
81
+ expect(result).toBe(true);
82
+ });
83
+ test("asTool can take a single string input", async () => {
84
+ const firstRunnable = RunnableLambda.from((input) => {
85
+ return `${input}a`;
86
+ });
87
+ const secondRunnable = RunnableLambda.from((input) => {
88
+ return `${input}z`;
89
+ });
90
+ const runnable = firstRunnable.pipe(secondRunnable);
91
+ const asTool = runnable.asTool({
92
+ schema: z.string(),
93
+ });
94
+ const result = await asTool.invoke("b");
95
+ expect(result).toBe("baz");
96
+ });
97
+ test("Runnable asTool uses Zod schema description if not provided", async () => {
98
+ const description = "Test schema";
99
+ const schema = z
100
+ .object({
101
+ foo: z.string(),
102
+ })
103
+ .describe(description);
104
+ const runnable = RunnableLambda.from((input, config) => {
105
+ return `${input.foo}${config?.configurable.foo}`;
106
+ });
107
+ const tool = runnable.asTool({
108
+ schema,
109
+ });
110
+ expect(tool.description).toBe(description);
111
+ });
@@ -2,9 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.tool = exports.BaseToolkit = exports.DynamicStructuredTool = exports.DynamicTool = exports.Tool = exports.StructuredTool = exports.ToolInputParsingException = void 0;
4
4
  const zod_1 = require("zod");
5
- const manager_js_1 = require("./callbacks/manager.cjs");
6
- const base_js_1 = require("./language_models/base.cjs");
7
- const config_js_1 = require("./runnables/config.cjs");
5
+ const manager_js_1 = require("../callbacks/manager.cjs");
6
+ const base_js_1 = require("../language_models/base.cjs");
7
+ const config_js_1 = require("../runnables/config.cjs");
8
+ const tool_js_1 = require("../messages/tool.cjs");
9
+ const index_js_1 = require("../singletons/index.cjs");
8
10
  /**
9
11
  * Custom error class used to handle exceptions related to tool input parsing.
10
12
  * It extends the built-in `Error` class and adds an optional `output`
@@ -38,6 +40,22 @@ class StructuredTool extends base_js_1.BaseLangChain {
38
40
  writable: true,
39
41
  value: false
40
42
  });
43
+ /**
44
+ * The tool response format.
45
+ *
46
+ * If "content" then the output of the tool is interpreted as the contents of a
47
+ * ToolMessage. If "content_and_artifact" then the output is expected to be a
48
+ * two-tuple corresponding to the (content, artifact) of a ToolMessage.
49
+ *
50
+ * @default "content"
51
+ */
52
+ Object.defineProperty(this, "responseFormat", {
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true,
56
+ value: "content"
57
+ });
58
+ this.responseFormat = fields?.responseFormat ?? this.responseFormat;
41
59
  }
42
60
  /**
43
61
  * Invokes the tool with the provided input and configuration.
@@ -46,7 +64,23 @@ class StructuredTool extends base_js_1.BaseLangChain {
46
64
  * @returns A Promise that resolves with a string.
47
65
  */
48
66
  async invoke(input, config) {
49
- return this.call(input, (0, config_js_1.ensureConfig)(config));
67
+ let tool_call_id;
68
+ let toolInput;
69
+ if (_isToolCall(input)) {
70
+ tool_call_id = input.id;
71
+ toolInput = input.args;
72
+ }
73
+ else {
74
+ toolInput = input;
75
+ }
76
+ const ensuredConfig = (0, config_js_1.ensureConfig)(config);
77
+ return this.call(toolInput, {
78
+ ...ensuredConfig,
79
+ configurable: {
80
+ ...ensuredConfig.configurable,
81
+ tool_call_id,
82
+ },
83
+ });
50
84
  }
51
85
  /**
52
86
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -81,8 +115,32 @@ class StructuredTool extends base_js_1.BaseLangChain {
81
115
  await runManager?.handleToolError(e);
82
116
  throw e;
83
117
  }
84
- await runManager?.handleToolEnd(result);
85
- return result;
118
+ let content;
119
+ let artifact;
120
+ if (this.responseFormat === "content_and_artifact") {
121
+ if (Array.isArray(result) && result.length === 2) {
122
+ [content, artifact] = result;
123
+ }
124
+ else {
125
+ throw new Error(`Tool response format is "content_and_artifact" but the output was not a two-tuple.\nResult: ${JSON.stringify(result)}`);
126
+ }
127
+ }
128
+ else {
129
+ content = result;
130
+ }
131
+ let toolCallId;
132
+ if (config && "configurable" in config) {
133
+ toolCallId = config.configurable
134
+ .tool_call_id;
135
+ }
136
+ const formattedOutput = _formatToolOutput({
137
+ content,
138
+ artifact,
139
+ toolCallId,
140
+ name: this.name,
141
+ });
142
+ await runManager?.handleToolEnd(formattedOutput);
143
+ return formattedOutput;
86
144
  }
87
145
  }
88
146
  exports.StructuredTool = StructuredTool;
@@ -158,8 +216,8 @@ class DynamicTool extends Tool {
158
216
  return super.call(arg, config);
159
217
  }
160
218
  /** @ignore */
161
- async _call(input, runManager, config) {
162
- return this.func(input, runManager, config);
219
+ async _call(input, runManager, parentConfig) {
220
+ return this.func(input, runManager, parentConfig);
163
221
  }
164
222
  }
165
223
  exports.DynamicTool = DynamicTool;
@@ -217,8 +275,8 @@ class DynamicStructuredTool extends StructuredTool {
217
275
  }
218
276
  return super.call(arg, config, tags);
219
277
  }
220
- _call(arg, runManager, config) {
221
- return this.func(arg, runManager, config);
278
+ _call(arg, runManager, parentConfig) {
279
+ return this.func(arg, runManager, parentConfig);
222
280
  }
223
281
  }
224
282
  exports.DynamicStructuredTool = DynamicStructuredTool;
@@ -236,15 +294,17 @@ exports.BaseToolkit = BaseToolkit;
236
294
  /**
237
295
  * Creates a new StructuredTool instance with the provided function, name, description, and schema.
238
296
  * @function
239
- * @template {ZodAny} RunInput The input schema for the tool.
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.
240
300
  *
241
- * @param {RunnableFunc<RunInput, string>} func - The function to invoke when the tool is called.
301
+ * @param {RunnableFunc<z.infer<RunInput> | ToolCall, RunOutput>} func - The function to invoke when the tool is called.
242
302
  * @param fields - An object containing the following properties:
243
303
  * @param {string} fields.name The name of the tool.
244
304
  * @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
245
305
  * @param {z.ZodObject<any, any, any, any>} fields.schema The Zod schema defining the input for the tool.
246
306
  *
247
- * @returns {StructuredTool<RunInput, string>} A new StructuredTool instance.
307
+ * @returns {DynamicStructuredTool<RunInput, RunOutput>} A new StructuredTool instance.
248
308
  */
249
309
  function tool(func, fields) {
250
310
  const schema = fields.schema ??
@@ -254,7 +314,63 @@ function tool(func, fields) {
254
314
  name: fields.name,
255
315
  description,
256
316
  schema: schema,
257
- func: async (input, _runManager, config) => func(input, config),
317
+ // TODO: Consider moving into DynamicStructuredTool constructor
318
+ func: async (input, runManager, config) => {
319
+ return new Promise((resolve, reject) => {
320
+ const childConfig = (0, config_js_1.patchConfig)(config, {
321
+ callbacks: runManager?.getChild(),
322
+ });
323
+ void index_js_1.AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
324
+ try {
325
+ resolve(func(input, childConfig));
326
+ }
327
+ catch (e) {
328
+ reject(e);
329
+ }
330
+ });
331
+ });
332
+ },
333
+ responseFormat: fields.responseFormat,
258
334
  });
259
335
  }
260
336
  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
+ function _formatToolOutput(params) {
344
+ const { content, artifact, toolCallId } = params;
345
+ if (toolCallId) {
346
+ if (typeof content === "string" ||
347
+ (Array.isArray(content) &&
348
+ content.every((item) => typeof item === "object"))) {
349
+ return new tool_js_1.ToolMessage({
350
+ content,
351
+ artifact,
352
+ tool_call_id: toolCallId,
353
+ name: params.name,
354
+ });
355
+ }
356
+ else {
357
+ return new tool_js_1.ToolMessage({
358
+ content: _stringify(content),
359
+ artifact,
360
+ tool_call_id: toolCallId,
361
+ name: params.name,
362
+ });
363
+ }
364
+ }
365
+ else {
366
+ return content;
367
+ }
368
+ }
369
+ function _stringify(content) {
370
+ try {
371
+ return JSON.stringify(content, null, 2);
372
+ }
373
+ catch (_noOp) {
374
+ return `${content}`;
375
+ }
376
+ }
@@ -1,13 +1,28 @@
1
1
  import { z } from "zod";
2
- import { CallbackManagerForToolRun, Callbacks } from "./callbacks/manager.js";
3
- import { BaseLangChain, type BaseLangChainParams } from "./language_models/base.js";
4
- import { type RunnableConfig } from "./runnables/config.js";
5
- import type { RunnableFunc, RunnableInterface } from "./runnables/base.js";
6
- type ZodAny = z.ZodObject<any, any, any, any>;
2
+ import { CallbackManagerForToolRun, Callbacks } from "../callbacks/manager.js";
3
+ import { BaseLangChain, type BaseLangChainParams } from "../language_models/base.js";
4
+ import { type RunnableConfig } from "../runnables/config.js";
5
+ import type { RunnableFunc, RunnableInterface } from "../runnables/base.js";
6
+ import { ToolCall } from "../messages/tool.js";
7
+ import { ZodAny } from "../types/zod.js";
8
+ import { MessageContent } from "../messages/base.js";
9
+ export type ResponseFormat = "content" | "content_and_artifact" | string;
10
+ type ToolReturnType = any;
11
+ export type ContentAndArtifact = [MessageContent, any];
7
12
  /**
8
13
  * Parameters for the Tool classes.
9
14
  */
10
15
  export interface ToolParams extends BaseLangChainParams {
16
+ /**
17
+ * The tool response format.
18
+ *
19
+ * If "content" then the output of the tool is interpreted as the contents of a
20
+ * ToolMessage. If "content_and_artifact" then the output is expected to be a
21
+ * two-tuple corresponding to the (content, artifact) of a ToolMessage.
22
+ *
23
+ * @default "content"
24
+ */
25
+ responseFormat?: ResponseFormat;
11
26
  }
12
27
  /**
13
28
  * Custom error class used to handle exceptions related to tool input parsing.
@@ -18,7 +33,7 @@ export declare class ToolInputParsingException extends Error {
18
33
  output?: string;
19
34
  constructor(message: string, output?: string);
20
35
  }
21
- export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T>, string> {
36
+ export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
22
37
  lc_namespace: string[];
23
38
  schema: T | z.ZodEffects<T>;
24
39
  /**
@@ -32,9 +47,9 @@ export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends Runn
32
47
  * @param tags Optional tags for the tool.
33
48
  * @returns A Promise that resolves with a string.
34
49
  */
35
- call(arg: (z.output<T> extends string ? string : never) | z.input<T>, configArg?: Callbacks | RunnableConfig,
50
+ call(arg: (z.output<T> extends string ? string : never) | z.input<T> | ToolCall, configArg?: Callbacks | RunnableConfig,
36
51
  /** @deprecated */
37
- tags?: string[]): Promise<string>;
52
+ tags?: string[]): Promise<ToolReturnType>;
38
53
  name: string;
39
54
  description: string;
40
55
  returnDirect: boolean;
@@ -42,18 +57,31 @@ export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends Runn
42
57
  /**
43
58
  * Base class for Tools that accept input of any shape defined by a Zod schema.
44
59
  */
45
- export declare abstract class StructuredTool<T extends ZodAny = ZodAny> extends BaseLangChain<(z.output<T> extends string ? string : never) | z.input<T>, string> {
60
+ export declare abstract class StructuredTool<T extends ZodAny = ZodAny> extends BaseLangChain<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
61
+ abstract name: string;
62
+ abstract description: string;
46
63
  abstract schema: T | z.ZodEffects<T>;
64
+ returnDirect: boolean;
47
65
  get lc_namespace(): string[];
66
+ /**
67
+ * The tool response format.
68
+ *
69
+ * If "content" then the output of the tool is interpreted as the contents of a
70
+ * ToolMessage. If "content_and_artifact" then the output is expected to be a
71
+ * two-tuple corresponding to the (content, artifact) of a ToolMessage.
72
+ *
73
+ * @default "content"
74
+ */
75
+ responseFormat?: ResponseFormat;
48
76
  constructor(fields?: ToolParams);
49
- protected abstract _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig): Promise<string>;
77
+ protected abstract _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
50
78
  /**
51
79
  * Invokes the tool with the provided input and configuration.
52
80
  * @param input The input for the tool.
53
81
  * @param config Optional configuration for the tool.
54
82
  * @returns A Promise that resolves with a string.
55
83
  */
56
- invoke(input: (z.output<T> extends string ? string : never) | z.input<T>, config?: RunnableConfig): Promise<string>;
84
+ invoke(input: (z.output<T> extends string ? string : never) | z.input<T> | ToolCall, config?: RunnableConfig): Promise<ToolReturnType>;
57
85
  /**
58
86
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
59
87
  *
@@ -65,14 +93,11 @@ export declare abstract class StructuredTool<T extends ZodAny = ZodAny> extends
65
93
  * @param tags Optional tags for the tool.
66
94
  * @returns A Promise that resolves with a string.
67
95
  */
68
- call(arg: (z.output<T> extends string ? string : never) | z.input<T>, configArg?: Callbacks | RunnableConfig,
96
+ call(arg: (z.output<T> extends string ? string : never) | z.input<T> | ToolCall, configArg?: Callbacks | RunnableConfig,
69
97
  /** @deprecated */
70
- tags?: string[]): Promise<string>;
71
- abstract name: string;
72
- abstract description: string;
73
- returnDirect: boolean;
98
+ tags?: string[]): Promise<ToolReturnType>;
74
99
  }
75
- export interface ToolInterface extends StructuredToolInterface {
100
+ export interface ToolInterface<T extends ZodAny = ZodAny> extends StructuredToolInterface<T> {
76
101
  /**
77
102
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
78
103
  *
@@ -82,12 +107,12 @@ export interface ToolInterface extends StructuredToolInterface {
82
107
  * @param callbacks Optional callbacks for the tool.
83
108
  * @returns A Promise that resolves with a string.
84
109
  */
85
- call(arg: string | undefined | z.input<this["schema"]>, callbacks?: Callbacks | RunnableConfig): Promise<string>;
110
+ call(arg: string | undefined | z.input<this["schema"]> | ToolCall, callbacks?: Callbacks | RunnableConfig): Promise<ToolReturnType>;
86
111
  }
87
112
  /**
88
113
  * Base class for Tools that accept input as a string.
89
114
  */
90
- export declare abstract class Tool extends StructuredTool {
115
+ export declare abstract class Tool extends StructuredTool<ZodAny> {
91
116
  schema: z.ZodEffects<z.ZodObject<{
92
117
  input: z.ZodOptional<z.ZodString>;
93
118
  }, "strip", z.ZodTypeAny, {
@@ -107,7 +132,7 @@ export declare abstract class Tool extends StructuredTool {
107
132
  * @param callbacks Optional callbacks for the tool.
108
133
  * @returns A Promise that resolves with a string.
109
134
  */
110
- call(arg: string | undefined | z.input<this["schema"]>, callbacks?: Callbacks | RunnableConfig): Promise<string>;
135
+ call(arg: string | undefined | z.input<this["schema"]> | ToolCall, callbacks?: Callbacks | RunnableConfig): Promise<ToolReturnType>;
111
136
  }
112
137
  export interface BaseDynamicToolInput extends ToolParams {
113
138
  name: string;
@@ -118,13 +143,13 @@ export interface BaseDynamicToolInput extends ToolParams {
118
143
  * Interface for the input parameters of the DynamicTool class.
119
144
  */
120
145
  export interface DynamicToolInput extends BaseDynamicToolInput {
121
- func: (input: string, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<string>;
146
+ func: (input: string, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
122
147
  }
123
148
  /**
124
149
  * Interface for the input parameters of the DynamicStructuredTool class.
125
150
  */
126
151
  export interface DynamicStructuredToolInput<T extends ZodAny = ZodAny> extends BaseDynamicToolInput {
127
- func: (input: z.infer<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<string>;
152
+ func: (input: BaseDynamicToolInput["responseFormat"] extends "content_and_artifact" ? ToolCall : z.infer<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
128
153
  schema: T;
129
154
  }
130
155
  /**
@@ -139,9 +164,9 @@ export declare class DynamicTool extends Tool {
139
164
  /**
140
165
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
141
166
  */
142
- call(arg: string | undefined | z.input<this["schema"]>, configArg?: RunnableConfig | Callbacks): Promise<string>;
167
+ call(arg: string | undefined | z.input<this["schema"]> | ToolCall, configArg?: RunnableConfig | Callbacks): Promise<ToolReturnType>;
143
168
  /** @ignore */
144
- _call(input: string, runManager?: CallbackManagerForToolRun, config?: RunnableConfig): Promise<string>;
169
+ _call(input: string, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
145
170
  }
146
171
  /**
147
172
  * A tool that can be created dynamically from a function, name, and
@@ -159,10 +184,10 @@ export declare class DynamicStructuredTool<T extends ZodAny = ZodAny> extends St
159
184
  /**
160
185
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
161
186
  */
162
- call(arg: z.output<T>, configArg?: RunnableConfig | Callbacks,
187
+ call(arg: z.output<T> | ToolCall, configArg?: RunnableConfig | Callbacks,
163
188
  /** @deprecated */
164
- tags?: string[]): Promise<string>;
165
- protected _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig): Promise<string>;
189
+ tags?: string[]): Promise<ToolReturnType>;
190
+ protected _call(arg: z.output<T> | ToolCall, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
166
191
  }
167
192
  /**
168
193
  * Abstract base class for toolkits in LangChain. Toolkits are collections
@@ -176,6 +201,7 @@ export declare abstract class BaseToolkit {
176
201
  /**
177
202
  * Parameters for the tool function.
178
203
  * @template {ZodAny} RunInput The input schema for the tool.
204
+ * @template {any} RunOutput The output type for the tool.
179
205
  */
180
206
  interface ToolWrapperParams<RunInput extends ZodAny = ZodAny> extends ToolParams {
181
207
  /**
@@ -194,19 +220,31 @@ interface ToolWrapperParams<RunInput extends ZodAny = ZodAny> extends ToolParams
194
220
  * for.
195
221
  */
196
222
  schema?: RunInput;
223
+ /**
224
+ * The tool response format.
225
+ *
226
+ * If "content" then the output of the tool is interpreted as the contents of a
227
+ * ToolMessage. If "content_and_artifact" then the output is expected to be a
228
+ * two-tuple corresponding to the (content, artifact) of a ToolMessage.
229
+ *
230
+ * @default "content"
231
+ */
232
+ responseFormat?: ResponseFormat;
197
233
  }
198
234
  /**
199
235
  * Creates a new StructuredTool instance with the provided function, name, description, and schema.
200
236
  * @function
201
- * @template {ZodAny} RunInput The input schema for the tool.
237
+ * @template {RunInput extends ZodAny = ZodAny} RunInput The input schema for the tool. This corresponds to the input type when the tool is invoked.
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.
202
240
  *
203
- * @param {RunnableFunc<RunInput, string>} func - The function to invoke when the tool is called.
241
+ * @param {RunnableFunc<z.infer<RunInput> | ToolCall, RunOutput>} func - The function to invoke when the tool is called.
204
242
  * @param fields - An object containing the following properties:
205
243
  * @param {string} fields.name The name of the tool.
206
244
  * @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
207
245
  * @param {z.ZodObject<any, any, any, any>} fields.schema The Zod schema defining the input for the tool.
208
246
  *
209
- * @returns {StructuredTool<RunInput, string>} A new StructuredTool instance.
247
+ * @returns {DynamicStructuredTool<RunInput, RunOutput>} A new StructuredTool instance.
210
248
  */
211
- export declare function tool<RunInput extends ZodAny = ZodAny>(func: RunnableFunc<z.infer<RunInput>, string>, fields: ToolWrapperParams<RunInput>): DynamicStructuredTool<RunInput>;
249
+ export declare function tool<T extends ZodAny = ZodAny>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
212
250
  export {};