@langchain/core 0.2.23 → 0.2.25

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.
@@ -187,6 +187,7 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
187
187
  *
188
188
  * **ATTENTION** This reference table is for the V2 version of the schema.
189
189
  *
190
+ * ```md
190
191
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
191
192
  * | event | name | chunk | input | output |
192
193
  * +======================+==================+=================================+===============================================+=================================================+
@@ -220,6 +221,7 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
220
221
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
221
222
  * | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) |
222
223
  * +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+
224
+ * ```
223
225
  *
224
226
  * The "on_chain_*" events are the default for Runnables that don't fit one of the above categories.
225
227
  *
@@ -229,6 +231,7 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
229
231
  *
230
232
  * A custom event has following format:
231
233
  *
234
+ * ```md
232
235
  * +-----------+------+-----------------------------------------------------------------------------------------------------------+
233
236
  * | Attribute | Type | Description |
234
237
  * +===========+======+===========================================================================================================+
@@ -236,9 +239,10 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
236
239
  * +-----------+------+-----------------------------------------------------------------------------------------------------------+
237
240
  * | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. |
238
241
  * +-----------+------+-----------------------------------------------------------------------------------------------------------+
242
+ * ```
239
243
  *
240
244
  * Here's an example:
241
- * @example
245
+ *
242
246
  * ```ts
243
247
  * import { RunnableLambda } from "@langchain/core/runnables";
244
248
  * import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch";
@@ -257,12 +257,17 @@ class RemoteRunnable extends base_js_1.Runnable {
257
257
  writable: true,
258
258
  value: void 0
259
259
  });
260
+ // Wrap the default fetch call due to issues with illegal invocations
261
+ // from the browser:
262
+ // https://stackoverflow.com/questions/69876859/why-does-bind-fix-failed-to-execute-fetch-on-window-illegal-invocation-err
260
263
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
261
264
  Object.defineProperty(this, "fetchImplementation", {
262
265
  enumerable: true,
263
266
  configurable: true,
264
267
  writable: true,
265
- value: fetch
268
+ value: (...args) =>
269
+ // @ts-expect-error Broad typing to support a range of fetch implementations
270
+ fetch(...args)
266
271
  });
267
272
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
268
273
  Object.defineProperty(this, "fetchRequestOptions", {
@@ -254,12 +254,17 @@ export class RemoteRunnable extends Runnable {
254
254
  writable: true,
255
255
  value: void 0
256
256
  });
257
+ // Wrap the default fetch call due to issues with illegal invocations
258
+ // from the browser:
259
+ // https://stackoverflow.com/questions/69876859/why-does-bind-fix-failed-to-execute-fetch-on-window-illegal-invocation-err
257
260
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
258
261
  Object.defineProperty(this, "fetchImplementation", {
259
262
  enumerable: true,
260
263
  configurable: true,
261
264
  writable: true,
262
- value: fetch
265
+ value: (...args) =>
266
+ // @ts-expect-error Broad typing to support a range of fetch implementations
267
+ fetch(...args)
263
268
  });
264
269
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
265
270
  Object.defineProperty(this, "fetchRequestOptions", {
@@ -250,7 +250,7 @@ class DynamicStructuredTool extends StructuredTool {
250
250
  this.description = fields.description;
251
251
  this.func = fields.func;
252
252
  this.returnDirect = fields.returnDirect ?? this.returnDirect;
253
- this.schema = ((0, is_zod_schema_js_1.isZodSchema)(fields.schema) ? fields.schema : zod_1.z.object({}));
253
+ this.schema = ((0, is_zod_schema_js_1.isZodSchema)(fields.schema) ? fields.schema : zod_1.z.object({}).passthrough());
254
254
  }
255
255
  /**
256
256
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -283,7 +283,9 @@ class BaseToolkit {
283
283
  exports.BaseToolkit = BaseToolkit;
284
284
  function tool(func, fields) {
285
285
  // If the schema is not provided, or it's a string schema, create a DynamicTool
286
- if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
286
+ if (!fields.schema ||
287
+ ((0, is_zod_schema_js_1.isZodSchema)(fields.schema) &&
288
+ (!("shape" in fields.schema) || !fields.schema.shape))) {
287
289
  return new DynamicTool({
288
290
  ...fields,
289
291
  description: fields.description ??
@@ -244,7 +244,7 @@ export class DynamicStructuredTool extends StructuredTool {
244
244
  this.description = fields.description;
245
245
  this.func = fields.func;
246
246
  this.returnDirect = fields.returnDirect ?? this.returnDirect;
247
- this.schema = (isZodSchema(fields.schema) ? fields.schema : z.object({}));
247
+ this.schema = (isZodSchema(fields.schema) ? fields.schema : z.object({}).passthrough());
248
248
  }
249
249
  /**
250
250
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -275,7 +275,9 @@ export class BaseToolkit {
275
275
  }
276
276
  export function tool(func, fields) {
277
277
  // If the schema is not provided, or it's a string schema, create a DynamicTool
278
- if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
278
+ if (!fields.schema ||
279
+ (isZodSchema(fields.schema) &&
280
+ (!("shape" in fields.schema) || !fields.schema.shape))) {
279
281
  return new DynamicTool({
280
282
  ...fields,
281
283
  description: fields.description ??
@@ -5,6 +5,7 @@ async function raceWithSignal(promise, signal) {
5
5
  if (signal === undefined) {
6
6
  return promise;
7
7
  }
8
+ let listener;
8
9
  return Promise.race([
9
10
  promise.catch((err) => {
10
11
  if (!signal?.aborted) {
@@ -15,14 +16,15 @@ async function raceWithSignal(promise, signal) {
15
16
  }
16
17
  }),
17
18
  new Promise((_, reject) => {
18
- signal.addEventListener("abort", () => {
19
+ listener = () => {
19
20
  reject(new Error("Aborted"));
20
- });
21
+ };
22
+ signal.addEventListener("abort", listener);
21
23
  // Must be here inside the promise to avoid a race condition
22
24
  if (signal.aborted) {
23
25
  reject(new Error("Aborted"));
24
26
  }
25
27
  }),
26
- ]);
28
+ ]).finally(() => signal.removeEventListener("abort", listener));
27
29
  }
28
30
  exports.raceWithSignal = raceWithSignal;
@@ -2,6 +2,7 @@ export async function raceWithSignal(promise, signal) {
2
2
  if (signal === undefined) {
3
3
  return promise;
4
4
  }
5
+ let listener;
5
6
  return Promise.race([
6
7
  promise.catch((err) => {
7
8
  if (!signal?.aborted) {
@@ -12,13 +13,14 @@ export async function raceWithSignal(promise, signal) {
12
13
  }
13
14
  }),
14
15
  new Promise((_, reject) => {
15
- signal.addEventListener("abort", () => {
16
+ listener = () => {
16
17
  reject(new Error("Aborted"));
17
- });
18
+ };
19
+ signal.addEventListener("abort", listener);
18
20
  // Must be here inside the promise to avoid a race condition
19
21
  if (signal.aborted) {
20
22
  reject(new Error("Aborted"));
21
23
  }
22
24
  }),
23
- ]);
25
+ ]).finally(() => signal.removeEventListener("abort", listener));
24
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.2.23",
3
+ "version": "0.2.25",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {