@langchain/core 0.3.29 → 0.3.31

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.
@@ -35,16 +35,10 @@ const config_js_1 = require("../../runnables/config.cjs");
35
35
  async function dispatchCustomEvent(name,
36
36
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
37
  payload, config) {
38
- const callbackManager = await (0, config_js_1.getCallbackManagerForConfig)(config);
39
- const parentRunId = callbackManager?.getParentRunId();
40
- // We want to get the callback manager for the parent run.
41
- // This is a work-around for now to be able to dispatch adhoc events from
42
- // within a tool or a lambda and have the metadata events associated
43
- // with the parent run rather than have a new run id generated for each.
44
- if (callbackManager === undefined || parentRunId === undefined) {
38
+ if (config === undefined) {
45
39
  throw new Error([
46
40
  "Unable to dispatch a custom event without a parent run id.",
47
- "This function can only be called from within an existing run (e.g.,",
41
+ `"dispatchCustomEvent" can only be called from within an existing run (e.g.,`,
48
42
  "inside a tool or a RunnableLambda).",
49
43
  `\n\nIf you continue to see this error, please import from "@langchain/core/callbacks/dispatch/web"`,
50
44
  "and explicitly pass in a config parameter.",
@@ -54,8 +48,12 @@ payload, config) {
54
48
  "\n",
55
49
  ].join(" "));
56
50
  }
51
+ const callbackManager = await (0, config_js_1.getCallbackManagerForConfig)(config);
52
+ const parentRunId = callbackManager?.getParentRunId();
57
53
  // We pass parent id as the current run id here intentionally since events dispatch
58
54
  // from within things like RunnableLambda
59
- await callbackManager.handleCustomEvent?.(name, payload, parentRunId);
55
+ if (callbackManager !== undefined && parentRunId !== undefined) {
56
+ await callbackManager.handleCustomEvent?.(name, payload, parentRunId);
57
+ }
60
58
  }
61
59
  exports.dispatchCustomEvent = dispatchCustomEvent;
@@ -32,16 +32,10 @@ import { getCallbackManagerForConfig, } from "../../runnables/config.js";
32
32
  export async function dispatchCustomEvent(name,
33
33
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
34
  payload, config) {
35
- const callbackManager = await getCallbackManagerForConfig(config);
36
- const parentRunId = callbackManager?.getParentRunId();
37
- // We want to get the callback manager for the parent run.
38
- // This is a work-around for now to be able to dispatch adhoc events from
39
- // within a tool or a lambda and have the metadata events associated
40
- // with the parent run rather than have a new run id generated for each.
41
- if (callbackManager === undefined || parentRunId === undefined) {
35
+ if (config === undefined) {
42
36
  throw new Error([
43
37
  "Unable to dispatch a custom event without a parent run id.",
44
- "This function can only be called from within an existing run (e.g.,",
38
+ `"dispatchCustomEvent" can only be called from within an existing run (e.g.,`,
45
39
  "inside a tool or a RunnableLambda).",
46
40
  `\n\nIf you continue to see this error, please import from "@langchain/core/callbacks/dispatch/web"`,
47
41
  "and explicitly pass in a config parameter.",
@@ -51,7 +45,11 @@ payload, config) {
51
45
  "\n",
52
46
  ].join(" "));
53
47
  }
48
+ const callbackManager = await getCallbackManagerForConfig(config);
49
+ const parentRunId = callbackManager?.getParentRunId();
54
50
  // We pass parent id as the current run id here intentionally since events dispatch
55
51
  // from within things like RunnableLambda
56
- await callbackManager.handleCustomEvent?.(name, payload, parentRunId);
52
+ if (callbackManager !== undefined && parentRunId !== undefined) {
53
+ await callbackManager.handleCustomEvent?.(name, payload, parentRunId);
54
+ }
57
55
  }
@@ -81,7 +81,7 @@ function _mergeMessageRuns(messages) {
81
81
  }
82
82
  const merged = [];
83
83
  for (const msg of messages) {
84
- const curr = msg; // Create a shallow copy of the message
84
+ const curr = msg;
85
85
  const last = merged.pop();
86
86
  if (!last) {
87
87
  merged.push(curr);
@@ -113,7 +113,9 @@ function trimMessages(messagesOrOptions, options) {
113
113
  }
114
114
  else {
115
115
  const trimmerOptions = messagesOrOptions;
116
- return base_js_1.RunnableLambda.from((input) => _trimMessagesHelper(input, trimmerOptions));
116
+ return base_js_1.RunnableLambda.from((input) => _trimMessagesHelper(input, trimmerOptions)).withConfig({
117
+ runName: "trim_messages",
118
+ });
117
119
  }
118
120
  }
119
121
  exports.trimMessages = trimMessages;
@@ -256,17 +258,19 @@ async function _firstMaxTokens(messages, options) {
256
258
  }
257
259
  async function _lastMaxTokens(messages, options) {
258
260
  const { allowPartial = false, includeSystem = false, endOn, startOn, ...rest } = options;
261
+ // Create a copy of messages to avoid mutation
262
+ let messagesCopy = [...messages];
259
263
  if (endOn) {
260
264
  const endOnArr = Array.isArray(endOn) ? endOn : [endOn];
261
- while (messages &&
262
- !_isMessageType(messages[messages.length - 1], endOnArr)) {
263
- messages.pop();
265
+ while (messagesCopy.length > 0 &&
266
+ !_isMessageType(messagesCopy[messagesCopy.length - 1], endOnArr)) {
267
+ messagesCopy = messagesCopy.slice(0, -1);
264
268
  }
265
269
  }
266
- const swappedSystem = includeSystem && messages[0]._getType() === "system";
270
+ const swappedSystem = includeSystem && messagesCopy[0]?._getType() === "system";
267
271
  let reversed_ = swappedSystem
268
- ? messages.slice(0, 1).concat(messages.slice(1).reverse())
269
- : messages.reverse();
272
+ ? messagesCopy.slice(0, 1).concat(messagesCopy.slice(1).reverse())
273
+ : messagesCopy.reverse();
270
274
  reversed_ = await _firstMaxTokens(reversed_, {
271
275
  ...rest,
272
276
  partialStrategy: allowPartial ? "last" : undefined,
@@ -76,7 +76,7 @@ function _mergeMessageRuns(messages) {
76
76
  }
77
77
  const merged = [];
78
78
  for (const msg of messages) {
79
- const curr = msg; // Create a shallow copy of the message
79
+ const curr = msg;
80
80
  const last = merged.pop();
81
81
  if (!last) {
82
82
  merged.push(curr);
@@ -108,7 +108,9 @@ export function trimMessages(messagesOrOptions, options) {
108
108
  }
109
109
  else {
110
110
  const trimmerOptions = messagesOrOptions;
111
- return RunnableLambda.from((input) => _trimMessagesHelper(input, trimmerOptions));
111
+ return RunnableLambda.from((input) => _trimMessagesHelper(input, trimmerOptions)).withConfig({
112
+ runName: "trim_messages",
113
+ });
112
114
  }
113
115
  }
114
116
  async function _trimMessagesHelper(messages, options) {
@@ -250,17 +252,19 @@ async function _firstMaxTokens(messages, options) {
250
252
  }
251
253
  async function _lastMaxTokens(messages, options) {
252
254
  const { allowPartial = false, includeSystem = false, endOn, startOn, ...rest } = options;
255
+ // Create a copy of messages to avoid mutation
256
+ let messagesCopy = [...messages];
253
257
  if (endOn) {
254
258
  const endOnArr = Array.isArray(endOn) ? endOn : [endOn];
255
- while (messages &&
256
- !_isMessageType(messages[messages.length - 1], endOnArr)) {
257
- messages.pop();
259
+ while (messagesCopy.length > 0 &&
260
+ !_isMessageType(messagesCopy[messagesCopy.length - 1], endOnArr)) {
261
+ messagesCopy = messagesCopy.slice(0, -1);
258
262
  }
259
263
  }
260
- const swappedSystem = includeSystem && messages[0]._getType() === "system";
264
+ const swappedSystem = includeSystem && messagesCopy[0]?._getType() === "system";
261
265
  let reversed_ = swappedSystem
262
- ? messages.slice(0, 1).concat(messages.slice(1).reverse())
263
- : messages.reverse();
266
+ ? messagesCopy.slice(0, 1).concat(messagesCopy.slice(1).reverse())
267
+ : messagesCopy.reverse();
264
268
  reversed_ = await _firstMaxTokens(reversed_, {
265
269
  ...rest,
266
270
  partialStrategy: allowPartial ? "last" : undefined,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.29",
3
+ "version": "0.3.31",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {