@langchain/core 0.3.34 → 0.3.35

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.
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultTextSplitter = exports.trimMessages = exports.mergeMessageRuns = exports.filterMessages = void 0;
4
4
  const base_js_1 = require("../runnables/base.cjs");
5
5
  const ai_js_1 = require("./ai.cjs");
6
+ const base_js_2 = require("./base.cjs");
6
7
  const chat_js_1 = require("./chat.cjs");
7
8
  const function_js_1 = require("./function.cjs");
8
9
  const human_js_1 = require("./human.cjs");
@@ -18,14 +19,14 @@ const _isMessageType = (msg, types) => {
18
19
  }
19
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
21
  const instantiatedMsgClass = new t({});
21
- if (!("_getType" in instantiatedMsgClass) ||
22
- typeof instantiatedMsgClass._getType !== "function") {
22
+ if (!("getType" in instantiatedMsgClass) ||
23
+ typeof instantiatedMsgClass.getType !== "function") {
23
24
  throw new Error("Invalid type provided.");
24
25
  }
25
- return instantiatedMsgClass._getType();
26
+ return instantiatedMsgClass.getType();
26
27
  })),
27
28
  ];
28
- const msgType = msg._getType();
29
+ const msgType = msg.getType();
29
30
  return typesAsStrings.some((t) => t === msgType);
30
31
  };
31
32
  function filterMessages(messagesOrOptions, options) {
@@ -86,8 +87,8 @@ function _mergeMessageRuns(messages) {
86
87
  if (!last) {
87
88
  merged.push(curr);
88
89
  }
89
- else if (curr._getType() === "tool" ||
90
- !(curr._getType() === last._getType())) {
90
+ else if (curr.getType() === "tool" ||
91
+ !(curr.getType() === last.getType())) {
91
92
  merged.push(last, curr);
92
93
  }
93
94
  else {
@@ -197,7 +198,7 @@ async function _firstMaxTokens(messages, options) {
197
198
  ? reversedContent.slice(0, i)
198
199
  : reversedContent.slice(-i);
199
200
  const fields = Object.fromEntries(Object.entries(excluded).filter(([k]) => k !== "type" && !k.startsWith("lc_")));
200
- const updatedMessage = _switchTypeToMessage(excluded._getType(), {
201
+ const updatedMessage = _switchTypeToMessage(excluded.getType(), {
201
202
  ...fields,
202
203
  content: partialContent,
203
204
  });
@@ -259,7 +260,10 @@ async function _firstMaxTokens(messages, options) {
259
260
  async function _lastMaxTokens(messages, options) {
260
261
  const { allowPartial = false, includeSystem = false, endOn, startOn, ...rest } = options;
261
262
  // Create a copy of messages to avoid mutation
262
- let messagesCopy = [...messages];
263
+ let messagesCopy = messages.map((message) => {
264
+ const fields = Object.fromEntries(Object.entries(message).filter(([k]) => k !== "type" && !k.startsWith("lc_")));
265
+ return _switchTypeToMessage(message.getType(), fields, (0, base_js_2.isBaseMessageChunk)(message));
266
+ });
263
267
  if (endOn) {
264
268
  const endOnArr = Array.isArray(endOn) ? endOn : [endOn];
265
269
  while (messagesCopy.length > 0 &&
@@ -267,7 +271,7 @@ async function _lastMaxTokens(messages, options) {
267
271
  messagesCopy = messagesCopy.slice(0, -1);
268
272
  }
269
273
  }
270
- const swappedSystem = includeSystem && messagesCopy[0]?._getType() === "system";
274
+ const swappedSystem = includeSystem && messagesCopy[0]?.getType() === "system";
271
275
  let reversed_ = swappedSystem
272
276
  ? messagesCopy.slice(0, 1).concat(messagesCopy.slice(1).reverse())
273
277
  : messagesCopy.reverse();
@@ -428,7 +432,7 @@ function _switchTypeToMessage(messageType, fields, returnChunk) {
428
432
  throw new Error(`Unrecognized message type ${messageType}`);
429
433
  }
430
434
  function _chunkToMsg(chunk) {
431
- const chunkType = chunk._getType();
435
+ const chunkType = chunk.getType();
432
436
  let msg;
433
437
  const fields = Object.fromEntries(Object.entries(chunk).filter(([k]) => !["type", "tool_call_chunks"].includes(k) && !k.startsWith("lc_")));
434
438
  if (chunkType in _MSG_CHUNK_MAP) {
@@ -1,5 +1,6 @@
1
1
  import { RunnableLambda } from "../runnables/base.js";
2
2
  import { AIMessage, AIMessageChunk } from "./ai.js";
3
+ import { isBaseMessageChunk, } from "./base.js";
3
4
  import { ChatMessage, ChatMessageChunk, } from "./chat.js";
4
5
  import { FunctionMessage, FunctionMessageChunk, } from "./function.js";
5
6
  import { HumanMessage, HumanMessageChunk } from "./human.js";
@@ -15,14 +16,14 @@ const _isMessageType = (msg, types) => {
15
16
  }
16
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
18
  const instantiatedMsgClass = new t({});
18
- if (!("_getType" in instantiatedMsgClass) ||
19
- typeof instantiatedMsgClass._getType !== "function") {
19
+ if (!("getType" in instantiatedMsgClass) ||
20
+ typeof instantiatedMsgClass.getType !== "function") {
20
21
  throw new Error("Invalid type provided.");
21
22
  }
22
- return instantiatedMsgClass._getType();
23
+ return instantiatedMsgClass.getType();
23
24
  })),
24
25
  ];
25
- const msgType = msg._getType();
26
+ const msgType = msg.getType();
26
27
  return typesAsStrings.some((t) => t === msgType);
27
28
  };
28
29
  export function filterMessages(messagesOrOptions, options) {
@@ -81,8 +82,8 @@ function _mergeMessageRuns(messages) {
81
82
  if (!last) {
82
83
  merged.push(curr);
83
84
  }
84
- else if (curr._getType() === "tool" ||
85
- !(curr._getType() === last._getType())) {
85
+ else if (curr.getType() === "tool" ||
86
+ !(curr.getType() === last.getType())) {
86
87
  merged.push(last, curr);
87
88
  }
88
89
  else {
@@ -191,7 +192,7 @@ async function _firstMaxTokens(messages, options) {
191
192
  ? reversedContent.slice(0, i)
192
193
  : reversedContent.slice(-i);
193
194
  const fields = Object.fromEntries(Object.entries(excluded).filter(([k]) => k !== "type" && !k.startsWith("lc_")));
194
- const updatedMessage = _switchTypeToMessage(excluded._getType(), {
195
+ const updatedMessage = _switchTypeToMessage(excluded.getType(), {
195
196
  ...fields,
196
197
  content: partialContent,
197
198
  });
@@ -253,7 +254,10 @@ async function _firstMaxTokens(messages, options) {
253
254
  async function _lastMaxTokens(messages, options) {
254
255
  const { allowPartial = false, includeSystem = false, endOn, startOn, ...rest } = options;
255
256
  // Create a copy of messages to avoid mutation
256
- let messagesCopy = [...messages];
257
+ let messagesCopy = messages.map((message) => {
258
+ const fields = Object.fromEntries(Object.entries(message).filter(([k]) => k !== "type" && !k.startsWith("lc_")));
259
+ return _switchTypeToMessage(message.getType(), fields, isBaseMessageChunk(message));
260
+ });
257
261
  if (endOn) {
258
262
  const endOnArr = Array.isArray(endOn) ? endOn : [endOn];
259
263
  while (messagesCopy.length > 0 &&
@@ -261,7 +265,7 @@ async function _lastMaxTokens(messages, options) {
261
265
  messagesCopy = messagesCopy.slice(0, -1);
262
266
  }
263
267
  }
264
- const swappedSystem = includeSystem && messagesCopy[0]?._getType() === "system";
268
+ const swappedSystem = includeSystem && messagesCopy[0]?.getType() === "system";
265
269
  let reversed_ = swappedSystem
266
270
  ? messagesCopy.slice(0, 1).concat(messagesCopy.slice(1).reverse())
267
271
  : messagesCopy.reverse();
@@ -422,7 +426,7 @@ function _switchTypeToMessage(messageType, fields, returnChunk) {
422
426
  throw new Error(`Unrecognized message type ${messageType}`);
423
427
  }
424
428
  function _chunkToMsg(chunk) {
425
- const chunkType = chunk._getType();
429
+ const chunkType = chunk.getType();
426
430
  let msg;
427
431
  const fields = Object.fromEntries(Object.entries(chunk).filter(([k]) => !["type", "tool_call_chunks"].includes(k) && !k.startsWith("lc_")));
428
432
  if (chunkType in _MSG_CHUNK_MAP) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.34",
3
+ "version": "0.3.35",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {