@langchain/core 0.3.34 → 0.3.36

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) {
@@ -19,13 +19,18 @@ class StructuredTool extends base_js_1.BaseLangChain {
19
19
  }
20
20
  constructor(fields) {
21
21
  super(fields ?? {});
22
+ /**
23
+ * Whether to return the tool's output directly.
24
+ *
25
+ * Setting this to true means that after the tool is called,
26
+ * an agent should stop looping.
27
+ */
22
28
  Object.defineProperty(this, "returnDirect", {
23
29
  enumerable: true,
24
30
  configurable: true,
25
31
  writable: true,
26
32
  value: false
27
33
  });
28
- // TODO: Make default in 0.3
29
34
  Object.defineProperty(this, "verboseParsingErrors", {
30
35
  enumerable: true,
31
36
  configurable: true,
@@ -74,6 +74,12 @@ export interface StructuredToolInterface<T extends ZodObjectAny = ZodObjectAny>
74
74
  * A description of the tool.
75
75
  */
76
76
  description: string;
77
+ /**
78
+ * Whether to return the tool's output directly.
79
+ *
80
+ * Setting this to true means that after the tool is called,
81
+ * an agent should stop looping.
82
+ */
77
83
  returnDirect: boolean;
78
84
  }
79
85
  /**
@@ -83,6 +89,12 @@ export declare abstract class StructuredTool<T extends ZodObjectAny = ZodObjectA
83
89
  abstract name: string;
84
90
  abstract description: string;
85
91
  abstract schema: T | z.ZodEffects<T>;
92
+ /**
93
+ * Whether to return the tool's output directly.
94
+ *
95
+ * Setting this to true means that after the tool is called,
96
+ * an agent should stop looping.
97
+ */
86
98
  returnDirect: boolean;
87
99
  verboseParsingErrors: boolean;
88
100
  get lc_namespace(): string[];
@@ -160,6 +172,12 @@ export declare abstract class Tool extends StructuredTool<ZodObjectAny> {
160
172
  export interface BaseDynamicToolInput extends ToolParams {
161
173
  name: string;
162
174
  description: string;
175
+ /**
176
+ * Whether to return the tool's output directly.
177
+ *
178
+ * Setting this to true means that after the tool is called,
179
+ * an agent should stop looping.
180
+ */
163
181
  returnDirect?: boolean;
164
182
  }
165
183
  /**
@@ -257,6 +275,13 @@ interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString | Record
257
275
  * @default "content"
258
276
  */
259
277
  responseFormat?: ResponseFormat;
278
+ /**
279
+ * Whether to return the tool's output directly.
280
+ *
281
+ * Setting this to true means that after the tool is called,
282
+ * an agent should stop looping.
283
+ */
284
+ returnDirect?: boolean;
260
285
  }
261
286
  /**
262
287
  * Creates a new StructuredTool instance with the provided function, name, description, and schema.
@@ -16,13 +16,18 @@ export class StructuredTool extends BaseLangChain {
16
16
  }
17
17
  constructor(fields) {
18
18
  super(fields ?? {});
19
+ /**
20
+ * Whether to return the tool's output directly.
21
+ *
22
+ * Setting this to true means that after the tool is called,
23
+ * an agent should stop looping.
24
+ */
19
25
  Object.defineProperty(this, "returnDirect", {
20
26
  enumerable: true,
21
27
  configurable: true,
22
28
  writable: true,
23
29
  value: false
24
30
  });
25
- // TODO: Make default in 0.3
26
31
  Object.defineProperty(this, "verboseParsingErrors", {
27
32
  enumerable: true,
28
33
  configurable: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.34",
3
+ "version": "0.3.36",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {