@langchain/core 0.3.64 → 0.3.66

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.
@@ -155,9 +155,10 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
155
155
  for (const [id, chunks] of Object.entries(groupedToolCallChunk)) {
156
156
  let parsedArgs = {};
157
157
  const name = chunks[0]?.name ?? "";
158
- const argStr = chunks.map((c) => c.args || "").join("");
158
+ const joinedArgs = chunks.map((c) => c.args || "").join("");
159
+ const argsStr = joinedArgs.length ? joinedArgs : "{}";
159
160
  try {
160
- parsedArgs = (0, json_js_1.parsePartialJson)(argStr);
161
+ parsedArgs = (0, json_js_1.parsePartialJson)(argsStr);
161
162
  if (parsedArgs === null ||
162
163
  typeof parsedArgs !== "object" ||
163
164
  Array.isArray(parsedArgs)) {
@@ -173,7 +174,7 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
173
174
  catch (e) {
174
175
  invalidToolCalls.push({
175
176
  name,
176
- args: argStr,
177
+ args: argsStr,
177
178
  id,
178
179
  error: "Malformed args.",
179
180
  type: "invalid_tool_call",
@@ -149,9 +149,10 @@ export class AIMessageChunk extends BaseMessageChunk {
149
149
  for (const [id, chunks] of Object.entries(groupedToolCallChunk)) {
150
150
  let parsedArgs = {};
151
151
  const name = chunks[0]?.name ?? "";
152
- const argStr = chunks.map((c) => c.args || "").join("");
152
+ const joinedArgs = chunks.map((c) => c.args || "").join("");
153
+ const argsStr = joinedArgs.length ? joinedArgs : "{}";
153
154
  try {
154
- parsedArgs = parsePartialJson(argStr);
155
+ parsedArgs = parsePartialJson(argsStr);
155
156
  if (parsedArgs === null ||
156
157
  typeof parsedArgs !== "object" ||
157
158
  Array.isArray(parsedArgs)) {
@@ -167,7 +168,7 @@ export class AIMessageChunk extends BaseMessageChunk {
167
168
  catch (e) {
168
169
  invalidToolCalls.push({
169
170
  name,
170
- args: argStr,
171
+ args: argsStr,
171
172
  id,
172
173
  error: "Malformed args.",
173
174
  type: "invalid_tool_call",
@@ -60,9 +60,19 @@ class StructuredTool extends base_js_1.BaseLangChain {
60
60
  writable: true,
61
61
  value: "content"
62
62
  });
63
+ /**
64
+ * Default config object for the tool runnable.
65
+ */
66
+ Object.defineProperty(this, "defaultConfig", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: void 0
71
+ });
63
72
  this.verboseParsingErrors =
64
73
  fields?.verboseParsingErrors ?? this.verboseParsingErrors;
65
74
  this.responseFormat = fields?.responseFormat ?? this.responseFormat;
75
+ this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;
66
76
  }
67
77
  /**
68
78
  * Invokes the tool with the provided input and configuration.
@@ -72,7 +82,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
72
82
  */
73
83
  async invoke(input, config) {
74
84
  let toolInput;
75
- let enrichedConfig = (0, config_js_1.ensureConfig)(config);
85
+ let enrichedConfig = (0, config_js_1.ensureConfig)((0, config_js_1.mergeConfigs)(this.defaultConfig, config));
76
86
  if ((0, utils_js_1._isToolCall)(input)) {
77
87
  toolInput = input.args;
78
88
  enrichedConfig = {
@@ -37,6 +37,10 @@ export declare abstract class StructuredTool<SchemaT = ToolInputSchemaBase, Sche
37
37
  * @default "content"
38
38
  */
39
39
  responseFormat?: ResponseFormat;
40
+ /**
41
+ * Default config object for the tool runnable.
42
+ */
43
+ defaultConfig?: ToolRunnableConfig;
40
44
  constructor(fields?: ToolParams);
41
45
  protected abstract _call(arg: SchemaOutputT, runManager?: CallbackManagerForToolRun, parentConfig?: ToolRunnableConfig): Promise<ToolOutputT>;
42
46
  /**
@@ -2,7 +2,7 @@ import { z } from "zod/v3";
2
2
  import { validate, } from "@cfworker/json-schema";
3
3
  import { CallbackManager, parseCallbackConfigArg, } from "../callbacks/manager.js";
4
4
  import { BaseLangChain } from "../language_models/base.js";
5
- import { ensureConfig, patchConfig, pickRunnableConfigKeys, } from "../runnables/config.js";
5
+ import { mergeConfigs, ensureConfig, patchConfig, pickRunnableConfigKeys, } from "../runnables/config.js";
6
6
  import { isDirectToolOutput, ToolMessage } from "../messages/tool.js";
7
7
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
8
8
  import { _configHasToolCallId, _isToolCall, ToolInputParsingException, } from "./utils.js";
@@ -52,9 +52,19 @@ export class StructuredTool extends BaseLangChain {
52
52
  writable: true,
53
53
  value: "content"
54
54
  });
55
+ /**
56
+ * Default config object for the tool runnable.
57
+ */
58
+ Object.defineProperty(this, "defaultConfig", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: void 0
63
+ });
55
64
  this.verboseParsingErrors =
56
65
  fields?.verboseParsingErrors ?? this.verboseParsingErrors;
57
66
  this.responseFormat = fields?.responseFormat ?? this.responseFormat;
67
+ this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;
58
68
  }
59
69
  /**
60
70
  * Invokes the tool with the provided input and configuration.
@@ -64,7 +74,7 @@ export class StructuredTool extends BaseLangChain {
64
74
  */
65
75
  async invoke(input, config) {
66
76
  let toolInput;
67
- let enrichedConfig = ensureConfig(config);
77
+ let enrichedConfig = ensureConfig(mergeConfigs(this.defaultConfig, config));
68
78
  if (_isToolCall(input)) {
69
79
  toolInput = input.args;
70
80
  enrichedConfig = {
@@ -48,6 +48,10 @@ export interface ToolParams extends BaseLangChainParams {
48
48
  * @default "content"
49
49
  */
50
50
  responseFormat?: ResponseFormat;
51
+ /**
52
+ * Default config object for the tool runnable.
53
+ */
54
+ defaultConfig?: ToolRunnableConfig;
51
55
  /**
52
56
  * Whether to show full details in the thrown parsing errors.
53
57
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.64",
3
+ "version": "0.3.66",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {