@langchain/core 0.2.16 → 0.2.18-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/callbacks/manager.cjs +71 -23
  2. package/dist/callbacks/manager.js +71 -23
  3. package/dist/callbacks/tests/callbacks.test.js +3 -0
  4. package/dist/language_models/chat_models.d.ts +18 -1
  5. package/dist/messages/ai.cjs +17 -0
  6. package/dist/messages/ai.d.ts +2 -0
  7. package/dist/messages/ai.js +17 -0
  8. package/dist/messages/base.cjs +50 -0
  9. package/dist/messages/base.d.ts +4 -1
  10. package/dist/messages/base.js +50 -0
  11. package/dist/messages/chat.cjs +12 -0
  12. package/dist/messages/chat.d.ts +2 -0
  13. package/dist/messages/chat.js +12 -0
  14. package/dist/messages/index.cjs +1 -0
  15. package/dist/messages/index.d.ts +1 -0
  16. package/dist/messages/index.js +1 -0
  17. package/dist/messages/modifier.cjs +35 -0
  18. package/dist/messages/modifier.d.ts +19 -0
  19. package/dist/messages/modifier.js +31 -0
  20. package/dist/messages/tool.cjs +14 -0
  21. package/dist/messages/tool.d.ts +2 -0
  22. package/dist/messages/tool.js +14 -0
  23. package/dist/messages/transformers.cjs +5 -0
  24. package/dist/messages/transformers.d.ts +3 -2
  25. package/dist/messages/transformers.js +5 -0
  26. package/dist/runnables/base.cjs +35 -4
  27. package/dist/runnables/base.d.ts +52 -5
  28. package/dist/runnables/base.js +35 -4
  29. package/dist/runnables/config.cjs +4 -7
  30. package/dist/runnables/config.d.ts +3 -17
  31. package/dist/runnables/config.js +5 -8
  32. package/dist/runnables/graph.d.ts +1 -1
  33. package/dist/runnables/iter.cjs +2 -4
  34. package/dist/runnables/iter.js +2 -4
  35. package/dist/runnables/tests/runnable_stream_events_v2.test.js +52 -48
  36. package/dist/runnables/tests/runnable_tools.test.js +38 -0
  37. package/dist/runnables/types.d.ts +14 -1
  38. package/dist/singletons/index.cjs +35 -5
  39. package/dist/singletons/index.d.ts +2 -0
  40. package/dist/singletons/index.js +34 -4
  41. package/dist/singletons/tests/async_local_storage.test.js +2 -3
  42. package/dist/tools/index.cjs +17 -45
  43. package/dist/tools/index.d.ts +19 -28
  44. package/dist/tools/index.js +15 -42
  45. package/dist/tools/tests/tools.test.js +11 -0
  46. package/dist/tools/utils.cjs +28 -0
  47. package/dist/tools/utils.d.ts +11 -0
  48. package/dist/tools/utils.js +23 -0
  49. package/dist/tracers/base.cjs +67 -13
  50. package/dist/tracers/base.d.ts +176 -1
  51. package/dist/tracers/base.js +65 -12
  52. package/dist/tracers/event_stream.cjs +15 -2
  53. package/dist/tracers/event_stream.js +15 -2
  54. package/dist/tracers/tests/langsmith_interop.test.d.ts +1 -0
  55. package/dist/tracers/tests/langsmith_interop.test.js +551 -0
  56. package/dist/tracers/tracer_langchain.cjs +73 -31
  57. package/dist/tracers/tracer_langchain.d.ts +3 -1
  58. package/dist/tracers/tracer_langchain.js +73 -31
  59. package/dist/types/zod.d.ts +1 -1
  60. package/dist/utils/stream.cjs +8 -9
  61. package/dist/utils/stream.js +8 -9
  62. package/package.json +2 -2
@@ -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
  }
@@ -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");
@@ -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";
@@ -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
+ }
@@ -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(
@@ -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.
@@ -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;
@@ -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 &&
@@ -1507,7 +1508,7 @@ class RunnableLambda extends Runnable {
1507
1508
  callbacks: runManager?.getChild(),
1508
1509
  recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
1509
1510
  });
1510
- void index_js_1.AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
1511
+ void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
1511
1512
  try {
1512
1513
  let output = await this.func(input, {
1513
1514
  ...childConfig,
@@ -1593,7 +1594,7 @@ class RunnableLambda extends Runnable {
1593
1594
  recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
1594
1595
  });
1595
1596
  const output = await new Promise((resolve, reject) => {
1596
- void index_js_1.AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
1597
+ void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
1597
1598
  try {
1598
1599
  const res = await this.func(finalChunk, {
1599
1600
  ...childConfig,
@@ -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: fields.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.description;
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,
@@ -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. */
@@ -201,11 +202,11 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
201
202
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
202
203
  * | on_llm_end | [model name] | | 'Hello human!' | |
203
204
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
204
- * | on_chain_start | format_docs | | | |
205
+ * | on_chain_start | some_runnable | | | |
205
206
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
206
- * | on_chain_stream | format_docs | "hello world!, goodbye world!" | | |
207
+ * | on_chain_stream | some_runnable | "hello world!, goodbye world!" | | |
207
208
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
208
- * | on_chain_end | format_docs | | [Document(...)] | "hello world!, goodbye world!" |
209
+ * | on_chain_end | some_runnable | | [Document(...)] | "hello world!, goodbye world!" |
209
210
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
210
211
  * | on_tool_start | some_tool | | {"x": 1, "y": "2"} | |
211
212
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
@@ -219,6 +220,52 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
219
220
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
220
221
  * | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) |
221
222
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
223
+ *
224
+ * The "on_chain_*" events are the default for Runnables that don't fit one of the above categories.
225
+ *
226
+ * In addition to the standard events above, users can also dispatch custom events.
227
+ *
228
+ * Custom events will be only be surfaced with in the `v2` version of the API!
229
+ *
230
+ * A custom event has following format:
231
+ *
232
+ * +-----------+------+-----------------------------------------------------------------------------------------------------------+
233
+ * | Attribute | Type | Description |
234
+ * +===========+======+===========================================================================================================+
235
+ * | name | str | A user defined name for the event. |
236
+ * +-----------+------+-----------------------------------------------------------------------------------------------------------+
237
+ * | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. |
238
+ * +-----------+------+-----------------------------------------------------------------------------------------------------------+
239
+ *
240
+ * Here's an example:
241
+ * @example
242
+ * ```ts
243
+ * import { RunnableLambda } from "@langchain/core/runnables";
244
+ * import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch";
245
+ * // Use this import for web environments that don't support "async_hooks"
246
+ * // and manually pass config to child runs.
247
+ * // import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch/web";
248
+ *
249
+ * const slowThing = RunnableLambda.from(async (someInput: string) => {
250
+ * // Placeholder for some slow operation
251
+ * await new Promise((resolve) => setTimeout(resolve, 100));
252
+ * await dispatchCustomEvent("progress_event", {
253
+ * message: "Finished step 1 of 2",
254
+ * });
255
+ * await new Promise((resolve) => setTimeout(resolve, 100));
256
+ * return "Done";
257
+ * });
258
+ *
259
+ * const eventStream = await slowThing.streamEvents("hello world", {
260
+ * version: "v2",
261
+ * });
262
+ *
263
+ * for await (const event of eventStream) {
264
+ * if (event.event === "on_custom_event") {
265
+ * console.log(event);
266
+ * }
267
+ * }
268
+ * ```
222
269
  */
223
270
  streamEvents(input: RunInput, options: Partial<CallOptions> & {
224
271
  version: "v1" | "v2";
@@ -262,7 +309,7 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
262
309
  name?: string;
263
310
  description?: string;
264
311
  schema: z.ZodType<T>;
265
- }): RunnableToolLike<z.ZodType<T>, RunOutput>;
312
+ }): RunnableToolLike<z.ZodType<T | ToolCall>, RunOutput>;
266
313
  }
267
314
  export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = {
268
315
  bound: Runnable<RunInput, RunOutput, CallOptions>;
@@ -608,4 +655,4 @@ export declare function convertRunnableToTool<RunInput, RunOutput>(runnable: Run
608
655
  name?: string;
609
656
  description?: string;
610
657
  schema: z.ZodType<RunInput>;
611
- }): RunnableToolLike<z.ZodType<RunInput>, RunOutput>;
658
+ }): RunnableToolLike<z.ZodType<RunInput | ToolCall>, RunOutput>;
@@ -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 &&
@@ -1493,7 +1494,7 @@ export class RunnableLambda extends Runnable {
1493
1494
  callbacks: runManager?.getChild(),
1494
1495
  recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
1495
1496
  });
1496
- void AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
1497
+ void AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
1497
1498
  try {
1498
1499
  let output = await this.func(input, {
1499
1500
  ...childConfig,
@@ -1579,7 +1580,7 @@ export class RunnableLambda extends Runnable {
1579
1580
  recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
1580
1581
  });
1581
1582
  const output = await new Promise((resolve, reject) => {
1582
- void AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
1583
+ void AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
1583
1584
  try {
1584
1585
  const res = await this.func(finalChunk, {
1585
1586
  ...childConfig,
@@ -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: fields.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.description;
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,
@@ -5,7 +5,7 @@ const manager_js_1 = require("../callbacks/manager.cjs");
5
5
  const index_js_1 = require("../singletons/index.cjs");
6
6
  exports.DEFAULT_RECURSION_LIMIT = 25;
7
7
  async function getCallbackManagerForConfig(config) {
8
- return manager_js_1.CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
8
+ return manager_js_1.CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
9
9
  }
10
10
  exports.getCallbackManagerForConfig = getCallbackManagerForConfig;
11
11
  function mergeConfigs(...configs) {
@@ -84,12 +84,9 @@ exports.mergeConfigs = mergeConfigs;
84
84
  const PRIMITIVES = new Set(["string", "number", "boolean"]);
85
85
  /**
86
86
  * Ensure that a passed config is an object with all required keys present.
87
- *
88
- * Note: To make sure async local storage loading works correctly, this
89
- * should not be called with a default or prepopulated config argument.
90
87
  */
91
88
  function ensureConfig(config) {
92
- const implicitConfig = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
89
+ const implicitConfig = index_js_1.AsyncLocalStorageProviderSingleton.getRunnableConfig();
93
90
  let empty = {
94
91
  tags: [],
95
92
  metadata: {},
@@ -97,10 +94,10 @@ function ensureConfig(config) {
97
94
  runId: undefined,
98
95
  };
99
96
  if (implicitConfig) {
100
- // Don't allow runId to be loaded implicitly, as this can cause
97
+ // Don't allow runId and runName to be loaded implicitly, as this can cause
101
98
  // child runs to improperly inherit their parents' run ids.
102
99
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
103
- const { runId, ...rest } = implicitConfig;
100
+ const { runId, runName, ...rest } = implicitConfig;
104
101
  empty = Object.entries(rest).reduce(
105
102
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
103
  (currentConfig, [key, value]) => {
@@ -1,25 +1,11 @@
1
- import { type BaseCallbackConfig, CallbackManager } from "../callbacks/manager.js";
1
+ import { CallbackManager } from "../callbacks/manager.js";
2
+ import { RunnableConfig } from "./types.js";
2
3
  export declare const DEFAULT_RECURSION_LIMIT = 25;
3
- export interface RunnableConfig extends BaseCallbackConfig {
4
- /**
5
- * Runtime values for attributes previously made configurable on this Runnable,
6
- * or sub-Runnables.
7
- */
8
- configurable?: Record<string, any>;
9
- /**
10
- * Maximum number of times a call can recurse. If not provided, defaults to 25.
11
- */
12
- recursionLimit?: number;
13
- /** Maximum number of parallel calls to make. */
14
- maxConcurrency?: number;
15
- }
4
+ export { type RunnableConfig };
16
5
  export declare function getCallbackManagerForConfig(config?: RunnableConfig): Promise<CallbackManager | undefined>;
17
6
  export declare function mergeConfigs<CallOptions extends RunnableConfig>(...configs: (CallOptions | RunnableConfig | undefined | null)[]): Partial<CallOptions>;
18
7
  /**
19
8
  * Ensure that a passed config is an object with all required keys present.
20
- *
21
- * Note: To make sure async local storage loading works correctly, this
22
- * should not be called with a default or prepopulated config argument.
23
9
  */
24
10
  export declare function ensureConfig<CallOptions extends RunnableConfig>(config?: CallOptions): CallOptions;
25
11
  /**
@@ -1,8 +1,8 @@
1
- import { CallbackManager, ensureHandler, } from "../callbacks/manager.js";
1
+ import { CallbackManager, ensureHandler } from "../callbacks/manager.js";
2
2
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
3
3
  export const DEFAULT_RECURSION_LIMIT = 25;
4
4
  export async function getCallbackManagerForConfig(config) {
5
- return CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
5
+ return CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
6
6
  }
7
7
  export function mergeConfigs(...configs) {
8
8
  // We do not want to call ensureConfig on the empty state here as this may cause
@@ -79,12 +79,9 @@ export function mergeConfigs(...configs) {
79
79
  const PRIMITIVES = new Set(["string", "number", "boolean"]);
80
80
  /**
81
81
  * Ensure that a passed config is an object with all required keys present.
82
- *
83
- * Note: To make sure async local storage loading works correctly, this
84
- * should not be called with a default or prepopulated config argument.
85
82
  */
86
83
  export function ensureConfig(config) {
87
- const implicitConfig = AsyncLocalStorageProviderSingleton.getInstance().getStore();
84
+ const implicitConfig = AsyncLocalStorageProviderSingleton.getRunnableConfig();
88
85
  let empty = {
89
86
  tags: [],
90
87
  metadata: {},
@@ -92,10 +89,10 @@ export function ensureConfig(config) {
92
89
  runId: undefined,
93
90
  };
94
91
  if (implicitConfig) {
95
- // Don't allow runId to be loaded implicitly, as this can cause
92
+ // Don't allow runId and runName to be loaded implicitly, as this can cause
96
93
  // child runs to improperly inherit their parents' run ids.
97
94
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
98
- const { runId, ...rest } = implicitConfig;
95
+ const { runId, runName, ...rest } = implicitConfig;
99
96
  empty = Object.entries(rest).reduce(
100
97
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
98
  (currentConfig, [key, value]) => {
@@ -15,7 +15,7 @@ export declare class Graph {
15
15
  */
16
16
  extend(graph: Graph, prefix?: string): ({
17
17
  id: string;
18
- data: RunnableIOSchema | RunnableInterface<any, any, import("./config.js").RunnableConfig>;
18
+ data: RunnableIOSchema | RunnableInterface<any, any, import("./types.js").RunnableConfig>;
19
19
  } | undefined)[];
20
20
  trimFirstNode(): void;
21
21
  trimLastNode(): void;
@@ -23,9 +23,8 @@ function isAsyncIterable(thing) {
23
23
  }
24
24
  exports.isAsyncIterable = isAsyncIterable;
25
25
  function* consumeIteratorInContext(context, iter) {
26
- const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
27
26
  while (true) {
28
- const { value, done } = storage.run(context, iter.next.bind(iter));
27
+ const { value, done } = index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(context, iter.next.bind(iter), true);
29
28
  if (done) {
30
29
  break;
31
30
  }
@@ -36,10 +35,9 @@ function* consumeIteratorInContext(context, iter) {
36
35
  }
37
36
  exports.consumeIteratorInContext = consumeIteratorInContext;
38
37
  async function* consumeAsyncIterableInContext(context, iter) {
39
- const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
40
38
  const iterator = iter[Symbol.asyncIterator]();
41
39
  while (true) {
42
- const { value, done } = await storage.run(context, iterator.next.bind(iter));
40
+ const { value, done } = await index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(context, iterator.next.bind(iter), true);
43
41
  if (done) {
44
42
  break;
45
43
  }