@langchain/core 1.2.7 → 1.2.8

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @langchain/core
2
2
 
3
+ ## 1.2.8
4
+
5
+ ### Patch Changes
6
+
7
+ - [#11369](https://github.com/langchain-ai/langchainjs/pull/11369) [`d6ad973`](https://github.com/langchain-ai/langchainjs/commit/d6ad9735640a6c729f81ef79361acc5e83c526f1) Thanks [@hntrl](https://github.com/hntrl)! - fix(langchain): use unified endpoint for gateway
8
+
9
+ - [#11342](https://github.com/langchain-ai/langchainjs/pull/11342) [`3b0e4c4`](https://github.com/langchain-ai/langchainjs/commit/3b0e4c48a31811031a460c4d95519a7c1163dc41) Thanks [@thushanth-bengre-langchain](https://github.com/thushanth-bengre-langchain)! - feat(core): mark errors as retryable or not, and stop retrying the ones that aren't
10
+
11
+ Retry middleware retried every failure up to `maxRetries`, including deterministic ones like a bad API key or an unknown model. Retries also nest, so a single such failure could cost dozens of API calls.
12
+
13
+ `@langchain/core/errors` adds `stampRetryable(error, retryable)` and `getRetryable(error)`. Marking an error leaves its class and shape untouched, so a provider SDK error can be classified without breaking `instanceof`. `getRetryable` returns `undefined` for errors nobody classified, and both are exported so tool authors can mark their own failures.
14
+
15
+ `modelRetryMiddleware` and `toolRetryMiddleware` now respect the mark by default, and retries stop as soon as one is found rather than each layer spending its own budget. Aborted calls, context overflow, and oversized payloads are marked non-retryable out of the box.
16
+ Models accept a per-call `maxRetries` so a surrounding retry loop can take over.
17
+
18
+ **Behavior change:** errors marked non-retryable now fail on the first attempt. Unclassified errors — including any from third-party integrations or custom tools — retry exactly as before. Pass `retryOn: () => true` to restore the old default. A custom `onFailedAttempt` replaces the built-in handler and opts out of marking.
19
+
3
20
  ## 1.2.7
4
21
 
5
22
  ### Patch Changes
@@ -7,7 +7,9 @@ var errors_exports = /* @__PURE__ */ require_runtime.__exportAll({
7
7
  LangChainError: () => LangChainError,
8
8
  ModelAbortError: () => ModelAbortError,
9
9
  addLangChainErrorFields: () => addLangChainErrorFields,
10
- ns: () => ns
10
+ getRetryable: () => getRetryable,
11
+ ns: () => ns,
12
+ stampRetryable: () => stampRetryable
11
13
  });
12
14
  /** @deprecated Subclass LangChainError instead */
13
15
  function addLangChainErrorFields(error, lc_error_code) {
@@ -17,6 +19,38 @@ function addLangChainErrorFields(error, lc_error_code) {
17
19
  }
18
20
  /** The error namespace for all LangChain errors */
19
21
  const ns = require_namespace.ns.sub("error");
22
+ /** Registered globally so duplicate copies of core in one dependency tree agree. */
23
+ const retryableSymbol = Symbol.for("langchain.errors.retryable");
24
+ /**
25
+ * Mark an error as safe or unsafe to retry.
26
+ *
27
+ * Sets a non-enumerable symbol on the error itself, leaving its class and
28
+ * shape untouched, so it is safe to apply to a provider SDK's own error.
29
+ *
30
+ * @param error - The error to mark. Non-objects are returned untouched.
31
+ * @param retryable - `true` if retrying may succeed.
32
+ * @returns The same error instance, for chaining.
33
+ */
34
+ function stampRetryable(error, retryable) {
35
+ if (typeof error !== "object" || error === null) return error;
36
+ try {
37
+ Object.defineProperty(error, retryableSymbol, {
38
+ value: retryable,
39
+ configurable: true
40
+ });
41
+ } catch {}
42
+ return error;
43
+ }
44
+ /**
45
+ * Read an error's retryability mark.
46
+ *
47
+ * @returns `true`/`false` when marked, `undefined` when unclassified —
48
+ * supply your own default, e.g. `getRetryable(error) ?? true`.
49
+ */
50
+ function getRetryable(error) {
51
+ if (typeof error !== "object" || error === null) return;
52
+ return Object.getOwnPropertyDescriptor(error, retryableSymbol)?.value;
53
+ }
20
54
  /**
21
55
  * Base error class for all LangChain errors.
22
56
  *
@@ -87,6 +121,7 @@ var ModelAbortError = class extends ns.brand(LangChainError, "model-abort") {
87
121
  constructor(message, partialOutput) {
88
122
  super(message);
89
123
  this.partialOutput = partialOutput;
124
+ stampRetryable(this, false);
90
125
  }
91
126
  };
92
127
  /**
@@ -132,6 +167,7 @@ var ContextOverflowError = class ContextOverflowError extends ns.brand(LangChain
132
167
  cause;
133
168
  constructor(message) {
134
169
  super(message ?? "Input exceeded the model's context window.");
170
+ stampRetryable(this, false);
135
171
  }
136
172
  /**
137
173
  * Creates a new {@link ContextOverflowError} instance from an existing error.
@@ -169,6 +205,8 @@ Object.defineProperty(exports, "errors_exports", {
169
205
  return errors_exports;
170
206
  }
171
207
  });
208
+ exports.getRetryable = getRetryable;
172
209
  exports.ns = ns;
210
+ exports.stampRetryable = stampRetryable;
173
211
 
174
212
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["baseNs"],"sources":["../../src/errors/index.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\n\nimport type { AIMessageChunk } from \"../messages/ai.js\";\nimport { ns as baseNs } from \"../utils/namespace.js\";\n\nexport type LangChainErrorCodes =\n | \"CONTEXT_OVERFLOW\"\n | \"INVALID_PROMPT_INPUT\"\n | \"INVALID_TOOL_RESULTS\"\n | \"MESSAGE_COERCION_FAILURE\"\n | \"MODEL_AUTHENTICATION\"\n | \"MODEL_NOT_FOUND\"\n | \"MODEL_RATE_LIMIT\"\n | \"OUTPUT_PARSING_FAILURE\"\n | \"MODEL_ABORTED\";\n\n/** @deprecated Subclass LangChainError instead */\nexport function addLangChainErrorFields(\n error: any,\n lc_error_code: LangChainErrorCodes\n) {\n (error as any).lc_error_code = lc_error_code;\n error.message = `${error.message}\\n\\nTroubleshooting URL: https://docs.langchain.com/oss/javascript/langchain/errors/${lc_error_code}/\\n`;\n return error;\n}\n\n/** The error namespace for all LangChain errors */\nexport const ns = baseNs.sub(\"error\");\n\n/**\n * Base error class for all LangChain errors.\n *\n * All LangChain error classes should extend this class (directly or\n * indirectly). Use `LangChainError.isInstance(obj)` to check if an\n * object is any LangChain error.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(\"hello\");\n * } catch (error) {\n * if (LangChainError.isInstance(error)) {\n * console.log(\"Got a LangChain error:\", error.message);\n * }\n * }\n * ```\n */\nexport class LangChainError extends ns.brand(Error) {\n readonly name: string = \"LangChainError\";\n\n constructor(message?: string) {\n super(message);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n}\n\n/**\n * Error class representing an aborted model operation in LangChain.\n *\n * This error is thrown when a model operation (such as invocation, streaming, or batching)\n * is cancelled before it completes, commonly due to a user-initiated abort signal\n * (e.g., via an AbortController) or an upstream cancellation event.\n *\n * The ModelAbortError provides access to any partial output the model may have produced\n * before the operation was interrupted, which can be useful for resuming work, debugging,\n * or presenting incomplete results to users.\n *\n * @remarks\n * - The `partialOutput` field includes message content that was generated prior to the abort,\n * such as a partial AIMessageChunk.\n * - This error extends the {@link LangChainError} base class with the marker `\"model-abort\"`.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input, { signal: abortController.signal });\n * } catch (err) {\n * if (ModelAbortError.isInstance(err)) {\n * // Handle user cancellation, check err.partialOutput if needed\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ModelAbortError extends ns.brand(LangChainError, \"model-abort\") {\n readonly name = \"ModelAbortError\";\n\n /**\n * The partial message output that was produced before the operation was aborted.\n * This is typically an AIMessageChunk, or could be undefined if no output was available.\n */\n readonly partialOutput?: AIMessageChunk;\n\n /**\n * Constructs a new ModelAbortError instance.\n *\n * @param message - A human-readable message describing the abort event.\n * @param partialOutput - Any partial model output generated before the abort (optional).\n */\n constructor(message: string, partialOutput?: AIMessageChunk) {\n super(message);\n this.partialOutput = partialOutput;\n }\n}\n\n/**\n * Error class representing a context window overflow in a language model operation.\n *\n * This error is thrown when the combined input to a language model (such as prompt tokens,\n * historical messages, and/or instructions) exceeds the maximum context window or token limit\n * that the model can process in a single request. Most models have defined upper limits for the number of\n * tokens or characters allowed in a context, and exceeding this limit will prevent\n * the operation from proceeding.\n *\n * The {@link ContextOverflowError} extends the {@link LangChainError} base class with\n * the marker `\"context-overflow\"`.\n *\n * @remarks\n * - Use this error to programmatically identify cases where a user request, prompt, or input\n * sequence is too long to be handled by the target model.\n * - Model providers and framework integrations should throw this error if they detect\n * a request cannot be processed due to its size.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(veryLongInput);\n * } catch (err) {\n * if (ContextOverflowError.isInstance(err)) {\n * // Handle overflow, e.g., prompt user to shorten input or truncate text\n * console.warn(\"Model context overflow:\", err.message);\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ContextOverflowError extends ns.brand(\n LangChainError,\n \"context-overflow\"\n) {\n readonly name = \"ContextOverflowError\";\n\n /**\n * The underlying error that caused this {@link ContextOverflowError}, if any.\n *\n * This property is optionally set when wrapping a lower-level error using {@link ContextOverflowError.fromError}.\n * It allows error handlers to access or inspect the original error that led to the context overflow.\n */\n cause?: Error;\n\n constructor(message?: string) {\n super(message ?? \"Input exceeded the model's context window.\");\n }\n\n /**\n * Creates a new {@link ContextOverflowError} instance from an existing error.\n *\n * This static utility copies the message from the provided error and\n * attaches the original error as the {@link ContextOverflowError.cause} property,\n * enabling error handlers to inspect or propagate the original failure.\n *\n * @param obj - The original error object causing the context overflow.\n * @returns A new {@link ContextOverflowError} instance with the original error set as its cause.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input);\n * } catch (err) {\n * throw ContextOverflowError.fromError(err);\n * }\n * ```\n */\n static fromError(obj: Error): ContextOverflowError {\n const error = new ContextOverflowError(obj.message);\n error.cause = obj;\n return error;\n }\n}\n"],"mappings":";;;;;;;;;;;;AAiBA,SAAgB,wBACd,OACA,eACA;CACA,MAAe,gBAAgB;CAC/B,MAAM,UAAU,GAAG,MAAM,QAAQ,sFAAsF,cAAc;CACrI,OAAO;AACT;;AAGA,MAAa,KAAKA,kBAAAA,GAAO,IAAI,OAAO;;;;;;;;;;;;;;;;;;;AAoBpC,IAAa,iBAAb,cAAoC,GAAG,MAAM,KAAK,CAAC,CAAC;CAClD,OAAwB;CAExB,YAAY,SAAkB;EAC5B,MAAM,OAAO;EACb,IAAI,MAAM,mBACR,MAAM,kBAAkB,MAAM,KAAK,WAAW;CAElD;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,IAAa,kBAAb,cAAqC,GAAG,MAAM,gBAAgB,aAAa,CAAC,CAAC;CAC3E,OAAgB;;;;;CAMhB;;;;;;;CAQA,YAAY,SAAiB,eAAgC;EAC3D,MAAM,OAAO;EACb,KAAK,gBAAgB;CACvB;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,uBAAb,MAAa,6BAA6B,GAAG,MAC3C,gBACA,kBACF,CAAC,CAAC;CACA,OAAgB;;;;;;;CAQhB;CAEA,YAAY,SAAkB;EAC5B,MAAM,WAAW,4CAA4C;CAC/D;;;;;;;;;;;;;;;;;;;;CAqBA,OAAO,UAAU,KAAkC;EACjD,MAAM,QAAQ,IAAI,qBAAqB,IAAI,OAAO;EAClD,MAAM,QAAQ;EACd,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["baseNs"],"sources":["../../src/errors/index.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\n\nimport type { AIMessageChunk } from \"../messages/ai.js\";\nimport { ns as baseNs } from \"../utils/namespace.js\";\n\nexport type LangChainErrorCodes =\n | \"CONTEXT_OVERFLOW\"\n | \"INVALID_PROMPT_INPUT\"\n | \"INVALID_TOOL_RESULTS\"\n | \"MESSAGE_COERCION_FAILURE\"\n | \"MODEL_AUTHENTICATION\"\n | \"MODEL_NOT_FOUND\"\n | \"MODEL_RATE_LIMIT\"\n | \"OUTPUT_PARSING_FAILURE\"\n | \"MODEL_ABORTED\";\n\n/** @deprecated Subclass LangChainError instead */\nexport function addLangChainErrorFields(\n error: any,\n lc_error_code: LangChainErrorCodes\n) {\n (error as any).lc_error_code = lc_error_code;\n error.message = `${error.message}\\n\\nTroubleshooting URL: https://docs.langchain.com/oss/javascript/langchain/errors/${lc_error_code}/\\n`;\n return error;\n}\n\n/** The error namespace for all LangChain errors */\nexport const ns = baseNs.sub(\"error\");\n\n/** Registered globally so duplicate copies of core in one dependency tree agree. */\nconst retryableSymbol = Symbol.for(\"langchain.errors.retryable\");\n\n/**\n * Mark an error as safe or unsafe to retry.\n *\n * Sets a non-enumerable symbol on the error itself, leaving its class and\n * shape untouched, so it is safe to apply to a provider SDK's own error.\n *\n * @param error - The error to mark. Non-objects are returned untouched.\n * @param retryable - `true` if retrying may succeed.\n * @returns The same error instance, for chaining.\n */\nexport function stampRetryable<T>(error: T, retryable: boolean): T {\n if (typeof error !== \"object\" || error === null) {\n return error;\n }\n try {\n Object.defineProperty(error, retryableSymbol, {\n value: retryable,\n configurable: true,\n });\n } catch {\n // Frozen or sealed error object; leave it unmarked rather than throwing.\n }\n return error;\n}\n\n/**\n * Read an error's retryability mark.\n *\n * @returns `true`/`false` when marked, `undefined` when unclassified —\n * supply your own default, e.g. `getRetryable(error) ?? true`.\n */\nexport function getRetryable(error: unknown): boolean | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n return Object.getOwnPropertyDescriptor(error, retryableSymbol)?.value as\n | boolean\n | undefined;\n}\n\n/**\n * Base error class for all LangChain errors.\n *\n * All LangChain error classes should extend this class (directly or\n * indirectly). Use `LangChainError.isInstance(obj)` to check if an\n * object is any LangChain error.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(\"hello\");\n * } catch (error) {\n * if (LangChainError.isInstance(error)) {\n * console.log(\"Got a LangChain error:\", error.message);\n * }\n * }\n * ```\n */\nexport class LangChainError extends ns.brand(Error) {\n readonly name: string = \"LangChainError\";\n\n constructor(message?: string) {\n super(message);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n}\n\n/**\n * Error class representing an aborted model operation in LangChain.\n *\n * This error is thrown when a model operation (such as invocation, streaming, or batching)\n * is cancelled before it completes, commonly due to a user-initiated abort signal\n * (e.g., via an AbortController) or an upstream cancellation event.\n *\n * The ModelAbortError provides access to any partial output the model may have produced\n * before the operation was interrupted, which can be useful for resuming work, debugging,\n * or presenting incomplete results to users.\n *\n * @remarks\n * - The `partialOutput` field includes message content that was generated prior to the abort,\n * such as a partial AIMessageChunk.\n * - This error extends the {@link LangChainError} base class with the marker `\"model-abort\"`.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input, { signal: abortController.signal });\n * } catch (err) {\n * if (ModelAbortError.isInstance(err)) {\n * // Handle user cancellation, check err.partialOutput if needed\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ModelAbortError extends ns.brand(LangChainError, \"model-abort\") {\n readonly name = \"ModelAbortError\";\n\n /**\n * The partial message output that was produced before the operation was aborted.\n * This is typically an AIMessageChunk, or could be undefined if no output was available.\n */\n readonly partialOutput?: AIMessageChunk;\n\n /**\n * Constructs a new ModelAbortError instance.\n *\n * @param message - A human-readable message describing the abort event.\n * @param partialOutput - Any partial model output generated before the abort (optional).\n */\n constructor(message: string, partialOutput?: AIMessageChunk) {\n super(message);\n this.partialOutput = partialOutput;\n // Retrying would mean ignoring the caller's explicit abort signal.\n stampRetryable(this, false);\n }\n}\n\n/**\n * Error class representing a context window overflow in a language model operation.\n *\n * This error is thrown when the combined input to a language model (such as prompt tokens,\n * historical messages, and/or instructions) exceeds the maximum context window or token limit\n * that the model can process in a single request. Most models have defined upper limits for the number of\n * tokens or characters allowed in a context, and exceeding this limit will prevent\n * the operation from proceeding.\n *\n * The {@link ContextOverflowError} extends the {@link LangChainError} base class with\n * the marker `\"context-overflow\"`.\n *\n * @remarks\n * - Use this error to programmatically identify cases where a user request, prompt, or input\n * sequence is too long to be handled by the target model.\n * - Model providers and framework integrations should throw this error if they detect\n * a request cannot be processed due to its size.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(veryLongInput);\n * } catch (err) {\n * if (ContextOverflowError.isInstance(err)) {\n * // Handle overflow, e.g., prompt user to shorten input or truncate text\n * console.warn(\"Model context overflow:\", err.message);\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ContextOverflowError extends ns.brand(\n LangChainError,\n \"context-overflow\"\n) {\n readonly name = \"ContextOverflowError\";\n\n /**\n * The underlying error that caused this {@link ContextOverflowError}, if any.\n *\n * This property is optionally set when wrapping a lower-level error using {@link ContextOverflowError.fromError}.\n * It allows error handlers to access or inspect the original error that led to the context overflow.\n */\n cause?: Error;\n\n constructor(message?: string) {\n super(message ?? \"Input exceeded the model's context window.\");\n // The same oversized input fails identically; it needs trimming, not another attempt.\n stampRetryable(this, false);\n }\n\n /**\n * Creates a new {@link ContextOverflowError} instance from an existing error.\n *\n * This static utility copies the message from the provided error and\n * attaches the original error as the {@link ContextOverflowError.cause} property,\n * enabling error handlers to inspect or propagate the original failure.\n *\n * @param obj - The original error object causing the context overflow.\n * @returns A new {@link ContextOverflowError} instance with the original error set as its cause.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input);\n * } catch (err) {\n * throw ContextOverflowError.fromError(err);\n * }\n * ```\n */\n static fromError(obj: Error): ContextOverflowError {\n const error = new ContextOverflowError(obj.message);\n error.cause = obj;\n return error;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAiBA,SAAgB,wBACd,OACA,eACA;CACA,MAAe,gBAAgB;CAC/B,MAAM,UAAU,GAAG,MAAM,QAAQ,sFAAsF,cAAc;CACrI,OAAO;AACT;;AAGA,MAAa,KAAKA,kBAAAA,GAAO,IAAI,OAAO;;AAGpC,MAAM,kBAAkB,OAAO,IAAI,4BAA4B;;;;;;;;;;;AAY/D,SAAgB,eAAkB,OAAU,WAAuB;CACjE,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC,OAAO;CAET,IAAI;EACF,OAAO,eAAe,OAAO,iBAAiB;GAC5C,OAAO;GACP,cAAc;EAChB,CAAC;CACH,QAAQ,CAER;CACA,OAAO;AACT;;;;;;;AAQA,SAAgB,aAAa,OAAqC;CAChE,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAEF,OAAO,OAAO,yBAAyB,OAAO,eAAe,CAAC,EAAE;AAGlE;;;;;;;;;;;;;;;;;;;AAoBA,IAAa,iBAAb,cAAoC,GAAG,MAAM,KAAK,CAAC,CAAC;CAClD,OAAwB;CAExB,YAAY,SAAkB;EAC5B,MAAM,OAAO;EACb,IAAI,MAAM,mBACR,MAAM,kBAAkB,MAAM,KAAK,WAAW;CAElD;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,IAAa,kBAAb,cAAqC,GAAG,MAAM,gBAAgB,aAAa,CAAC,CAAC;CAC3E,OAAgB;;;;;CAMhB;;;;;;;CAQA,YAAY,SAAiB,eAAgC;EAC3D,MAAM,OAAO;EACb,KAAK,gBAAgB;EAErB,eAAe,MAAM,KAAK;CAC5B;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,uBAAb,MAAa,6BAA6B,GAAG,MAC3C,gBACA,kBACF,CAAC,CAAC;CACA,OAAgB;;;;;;;CAQhB;CAEA,YAAY,SAAkB;EAC5B,MAAM,WAAW,4CAA4C;EAE7D,eAAe,MAAM,KAAK;CAC5B;;;;;;;;;;;;;;;;;;;;CAqBA,OAAO,UAAU,KAAkC;EACjD,MAAM,QAAQ,IAAI,qBAAqB,IAAI,OAAO;EAClD,MAAM,QAAQ;EACd,OAAO;CACT;AACF"}
@@ -6,6 +6,24 @@ type LangChainErrorCodes = "CONTEXT_OVERFLOW" | "INVALID_PROMPT_INPUT" | "INVALI
6
6
  declare function addLangChainErrorFields(error: any, lc_error_code: LangChainErrorCodes): any;
7
7
  /** The error namespace for all LangChain errors */
8
8
  declare const ns: Namespace;
9
+ /**
10
+ * Mark an error as safe or unsafe to retry.
11
+ *
12
+ * Sets a non-enumerable symbol on the error itself, leaving its class and
13
+ * shape untouched, so it is safe to apply to a provider SDK's own error.
14
+ *
15
+ * @param error - The error to mark. Non-objects are returned untouched.
16
+ * @param retryable - `true` if retrying may succeed.
17
+ * @returns The same error instance, for chaining.
18
+ */
19
+ declare function stampRetryable<T>(error: T, retryable: boolean): T;
20
+ /**
21
+ * Read an error's retryability mark.
22
+ *
23
+ * @returns `true`/`false` when marked, `undefined` when unclassified —
24
+ * supply your own default, e.g. `getRetryable(error) ?? true`.
25
+ */
26
+ declare function getRetryable(error: unknown): boolean | undefined;
9
27
  declare const LangChainError_base: ErrorConstructor & {
10
28
  isInstance: <T extends abstract new (...args: any[]) => any>(this: T, obj: unknown) => obj is InstanceType<T>;
11
29
  };
@@ -145,5 +163,5 @@ declare class ContextOverflowError extends ContextOverflowError_base {
145
163
  static fromError(obj: Error): ContextOverflowError;
146
164
  }
147
165
  //#endregion
148
- export { ContextOverflowError, LangChainError, LangChainErrorCodes, ModelAbortError, addLangChainErrorFields, ns };
166
+ export { ContextOverflowError, LangChainError, LangChainErrorCodes, ModelAbortError, addLangChainErrorFields, getRetryable, ns, stampRetryable };
149
167
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/errors/index.ts"],"mappings":";;;KAKY;;iBAYI,wBACd,YACA,eAAe;;cAQJ,IAAA;;;;;;;;;;;;;;;;;;;;;;cAoBA,uBAAuB;WACzB;EAET,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAqCD,wBAAwB;WAC1B;;;;;WAMA,gBAAgB;;;;;;;EAQzB,YAAY,iBAAiB,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsClC,6BAA6B;WAI/B;;;;;;;EAQT,QAAQ;EAER,YAAY;;;;;;;;;;;;;;;;;;;;SAuBL,UAAU,KAAK,QAAQ"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/errors/index.ts"],"mappings":";;;KAKY;;iBAYI,wBACd,YACA,eAAe;;cAQJ,IAAA;;;;;;;;;;;iBAeG,eAAe,GAAG,OAAO,GAAG,qBAAqB;;;;;;;iBAqBjD,aAAa;;;;;;;;;;;;;;;;;;;;;;cA2BhB,uBAAuB;WACzB;EAET,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAqCD,wBAAwB;WAC1B;;;;;WAMA,gBAAgB;;;;;;;EAQzB,YAAY,iBAAiB,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAwClC,6BAA6B;WAI/B;;;;;;;EAQT,QAAQ;EAER,YAAY;;;;;;;;;;;;;;;;;;;;SAyBL,UAAU,KAAK,QAAQ"}
@@ -6,6 +6,24 @@ type LangChainErrorCodes = "CONTEXT_OVERFLOW" | "INVALID_PROMPT_INPUT" | "INVALI
6
6
  declare function addLangChainErrorFields(error: any, lc_error_code: LangChainErrorCodes): any;
7
7
  /** The error namespace for all LangChain errors */
8
8
  declare const ns: Namespace;
9
+ /**
10
+ * Mark an error as safe or unsafe to retry.
11
+ *
12
+ * Sets a non-enumerable symbol on the error itself, leaving its class and
13
+ * shape untouched, so it is safe to apply to a provider SDK's own error.
14
+ *
15
+ * @param error - The error to mark. Non-objects are returned untouched.
16
+ * @param retryable - `true` if retrying may succeed.
17
+ * @returns The same error instance, for chaining.
18
+ */
19
+ declare function stampRetryable<T>(error: T, retryable: boolean): T;
20
+ /**
21
+ * Read an error's retryability mark.
22
+ *
23
+ * @returns `true`/`false` when marked, `undefined` when unclassified —
24
+ * supply your own default, e.g. `getRetryable(error) ?? true`.
25
+ */
26
+ declare function getRetryable(error: unknown): boolean | undefined;
9
27
  declare const LangChainError_base: ErrorConstructor & {
10
28
  isInstance: <T extends abstract new (...args: any[]) => any>(this: T, obj: unknown) => obj is InstanceType<T>;
11
29
  };
@@ -145,5 +163,5 @@ declare class ContextOverflowError extends ContextOverflowError_base {
145
163
  static fromError(obj: Error): ContextOverflowError;
146
164
  }
147
165
  //#endregion
148
- export { ContextOverflowError, LangChainError, LangChainErrorCodes, ModelAbortError, addLangChainErrorFields, ns };
166
+ export { ContextOverflowError, LangChainError, LangChainErrorCodes, ModelAbortError, addLangChainErrorFields, getRetryable, ns, stampRetryable };
149
167
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/errors/index.ts"],"mappings":";;;KAKY;;iBAYI,wBACd,YACA,eAAe;;cAQJ,IAAA;;;;;;;;;;;;;;;;;;;;;;cAoBA,uBAAuB;WACzB;EAET,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAqCD,wBAAwB;WAC1B;;;;;WAMA,gBAAgB;;;;;;;EAQzB,YAAY,iBAAiB,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsClC,6BAA6B;WAI/B;;;;;;;EAQT,QAAQ;EAER,YAAY;;;;;;;;;;;;;;;;;;;;SAuBL,UAAU,KAAK,QAAQ"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/errors/index.ts"],"mappings":";;;KAKY;;iBAYI,wBACd,YACA,eAAe;;cAQJ,IAAA;;;;;;;;;;;iBAeG,eAAe,GAAG,OAAO,GAAG,qBAAqB;;;;;;;iBAqBjD,aAAa;;;;;;;;;;;;;;;;;;;;;;cA2BhB,uBAAuB;WACzB;EAET,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAqCD,wBAAwB;WAC1B;;;;;WAMA,gBAAgB;;;;;;;EAQzB,YAAY,iBAAiB,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAwClC,6BAA6B;WAI/B;;;;;;;EAQT,QAAQ;EAER,YAAY;;;;;;;;;;;;;;;;;;;;SAyBL,UAAU,KAAK,QAAQ"}
@@ -6,7 +6,9 @@ var errors_exports = /* @__PURE__ */ __exportAll({
6
6
  LangChainError: () => LangChainError,
7
7
  ModelAbortError: () => ModelAbortError,
8
8
  addLangChainErrorFields: () => addLangChainErrorFields,
9
- ns: () => ns
9
+ getRetryable: () => getRetryable,
10
+ ns: () => ns,
11
+ stampRetryable: () => stampRetryable
10
12
  });
11
13
  /** @deprecated Subclass LangChainError instead */
12
14
  function addLangChainErrorFields(error, lc_error_code) {
@@ -16,6 +18,38 @@ function addLangChainErrorFields(error, lc_error_code) {
16
18
  }
17
19
  /** The error namespace for all LangChain errors */
18
20
  const ns = ns$1.sub("error");
21
+ /** Registered globally so duplicate copies of core in one dependency tree agree. */
22
+ const retryableSymbol = Symbol.for("langchain.errors.retryable");
23
+ /**
24
+ * Mark an error as safe or unsafe to retry.
25
+ *
26
+ * Sets a non-enumerable symbol on the error itself, leaving its class and
27
+ * shape untouched, so it is safe to apply to a provider SDK's own error.
28
+ *
29
+ * @param error - The error to mark. Non-objects are returned untouched.
30
+ * @param retryable - `true` if retrying may succeed.
31
+ * @returns The same error instance, for chaining.
32
+ */
33
+ function stampRetryable(error, retryable) {
34
+ if (typeof error !== "object" || error === null) return error;
35
+ try {
36
+ Object.defineProperty(error, retryableSymbol, {
37
+ value: retryable,
38
+ configurable: true
39
+ });
40
+ } catch {}
41
+ return error;
42
+ }
43
+ /**
44
+ * Read an error's retryability mark.
45
+ *
46
+ * @returns `true`/`false` when marked, `undefined` when unclassified —
47
+ * supply your own default, e.g. `getRetryable(error) ?? true`.
48
+ */
49
+ function getRetryable(error) {
50
+ if (typeof error !== "object" || error === null) return;
51
+ return Object.getOwnPropertyDescriptor(error, retryableSymbol)?.value;
52
+ }
19
53
  /**
20
54
  * Base error class for all LangChain errors.
21
55
  *
@@ -86,6 +120,7 @@ var ModelAbortError = class extends ns.brand(LangChainError, "model-abort") {
86
120
  constructor(message, partialOutput) {
87
121
  super(message);
88
122
  this.partialOutput = partialOutput;
123
+ stampRetryable(this, false);
89
124
  }
90
125
  };
91
126
  /**
@@ -131,6 +166,7 @@ var ContextOverflowError = class ContextOverflowError extends ns.brand(LangChain
131
166
  cause;
132
167
  constructor(message) {
133
168
  super(message ?? "Input exceeded the model's context window.");
169
+ stampRetryable(this, false);
134
170
  }
135
171
  /**
136
172
  * Creates a new {@link ContextOverflowError} instance from an existing error.
@@ -158,6 +194,6 @@ var ContextOverflowError = class ContextOverflowError extends ns.brand(LangChain
158
194
  }
159
195
  };
160
196
  //#endregion
161
- export { ContextOverflowError, LangChainError, ModelAbortError, addLangChainErrorFields, errors_exports, ns };
197
+ export { ContextOverflowError, LangChainError, ModelAbortError, addLangChainErrorFields, errors_exports, getRetryable, ns, stampRetryable };
162
198
 
163
199
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["baseNs"],"sources":["../../src/errors/index.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\n\nimport type { AIMessageChunk } from \"../messages/ai.js\";\nimport { ns as baseNs } from \"../utils/namespace.js\";\n\nexport type LangChainErrorCodes =\n | \"CONTEXT_OVERFLOW\"\n | \"INVALID_PROMPT_INPUT\"\n | \"INVALID_TOOL_RESULTS\"\n | \"MESSAGE_COERCION_FAILURE\"\n | \"MODEL_AUTHENTICATION\"\n | \"MODEL_NOT_FOUND\"\n | \"MODEL_RATE_LIMIT\"\n | \"OUTPUT_PARSING_FAILURE\"\n | \"MODEL_ABORTED\";\n\n/** @deprecated Subclass LangChainError instead */\nexport function addLangChainErrorFields(\n error: any,\n lc_error_code: LangChainErrorCodes\n) {\n (error as any).lc_error_code = lc_error_code;\n error.message = `${error.message}\\n\\nTroubleshooting URL: https://docs.langchain.com/oss/javascript/langchain/errors/${lc_error_code}/\\n`;\n return error;\n}\n\n/** The error namespace for all LangChain errors */\nexport const ns = baseNs.sub(\"error\");\n\n/**\n * Base error class for all LangChain errors.\n *\n * All LangChain error classes should extend this class (directly or\n * indirectly). Use `LangChainError.isInstance(obj)` to check if an\n * object is any LangChain error.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(\"hello\");\n * } catch (error) {\n * if (LangChainError.isInstance(error)) {\n * console.log(\"Got a LangChain error:\", error.message);\n * }\n * }\n * ```\n */\nexport class LangChainError extends ns.brand(Error) {\n readonly name: string = \"LangChainError\";\n\n constructor(message?: string) {\n super(message);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n}\n\n/**\n * Error class representing an aborted model operation in LangChain.\n *\n * This error is thrown when a model operation (such as invocation, streaming, or batching)\n * is cancelled before it completes, commonly due to a user-initiated abort signal\n * (e.g., via an AbortController) or an upstream cancellation event.\n *\n * The ModelAbortError provides access to any partial output the model may have produced\n * before the operation was interrupted, which can be useful for resuming work, debugging,\n * or presenting incomplete results to users.\n *\n * @remarks\n * - The `partialOutput` field includes message content that was generated prior to the abort,\n * such as a partial AIMessageChunk.\n * - This error extends the {@link LangChainError} base class with the marker `\"model-abort\"`.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input, { signal: abortController.signal });\n * } catch (err) {\n * if (ModelAbortError.isInstance(err)) {\n * // Handle user cancellation, check err.partialOutput if needed\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ModelAbortError extends ns.brand(LangChainError, \"model-abort\") {\n readonly name = \"ModelAbortError\";\n\n /**\n * The partial message output that was produced before the operation was aborted.\n * This is typically an AIMessageChunk, or could be undefined if no output was available.\n */\n readonly partialOutput?: AIMessageChunk;\n\n /**\n * Constructs a new ModelAbortError instance.\n *\n * @param message - A human-readable message describing the abort event.\n * @param partialOutput - Any partial model output generated before the abort (optional).\n */\n constructor(message: string, partialOutput?: AIMessageChunk) {\n super(message);\n this.partialOutput = partialOutput;\n }\n}\n\n/**\n * Error class representing a context window overflow in a language model operation.\n *\n * This error is thrown when the combined input to a language model (such as prompt tokens,\n * historical messages, and/or instructions) exceeds the maximum context window or token limit\n * that the model can process in a single request. Most models have defined upper limits for the number of\n * tokens or characters allowed in a context, and exceeding this limit will prevent\n * the operation from proceeding.\n *\n * The {@link ContextOverflowError} extends the {@link LangChainError} base class with\n * the marker `\"context-overflow\"`.\n *\n * @remarks\n * - Use this error to programmatically identify cases where a user request, prompt, or input\n * sequence is too long to be handled by the target model.\n * - Model providers and framework integrations should throw this error if they detect\n * a request cannot be processed due to its size.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(veryLongInput);\n * } catch (err) {\n * if (ContextOverflowError.isInstance(err)) {\n * // Handle overflow, e.g., prompt user to shorten input or truncate text\n * console.warn(\"Model context overflow:\", err.message);\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ContextOverflowError extends ns.brand(\n LangChainError,\n \"context-overflow\"\n) {\n readonly name = \"ContextOverflowError\";\n\n /**\n * The underlying error that caused this {@link ContextOverflowError}, if any.\n *\n * This property is optionally set when wrapping a lower-level error using {@link ContextOverflowError.fromError}.\n * It allows error handlers to access or inspect the original error that led to the context overflow.\n */\n cause?: Error;\n\n constructor(message?: string) {\n super(message ?? \"Input exceeded the model's context window.\");\n }\n\n /**\n * Creates a new {@link ContextOverflowError} instance from an existing error.\n *\n * This static utility copies the message from the provided error and\n * attaches the original error as the {@link ContextOverflowError.cause} property,\n * enabling error handlers to inspect or propagate the original failure.\n *\n * @param obj - The original error object causing the context overflow.\n * @returns A new {@link ContextOverflowError} instance with the original error set as its cause.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input);\n * } catch (err) {\n * throw ContextOverflowError.fromError(err);\n * }\n * ```\n */\n static fromError(obj: Error): ContextOverflowError {\n const error = new ContextOverflowError(obj.message);\n error.cause = obj;\n return error;\n }\n}\n"],"mappings":";;;;;;;;;;;AAiBA,SAAgB,wBACd,OACA,eACA;CACA,MAAe,gBAAgB;CAC/B,MAAM,UAAU,GAAG,MAAM,QAAQ,sFAAsF,cAAc;CACrI,OAAO;AACT;;AAGA,MAAa,KAAKA,KAAO,IAAI,OAAO;;;;;;;;;;;;;;;;;;;AAoBpC,IAAa,iBAAb,cAAoC,GAAG,MAAM,KAAK,CAAC,CAAC;CAClD,OAAwB;CAExB,YAAY,SAAkB;EAC5B,MAAM,OAAO;EACb,IAAI,MAAM,mBACR,MAAM,kBAAkB,MAAM,KAAK,WAAW;CAElD;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,IAAa,kBAAb,cAAqC,GAAG,MAAM,gBAAgB,aAAa,CAAC,CAAC;CAC3E,OAAgB;;;;;CAMhB;;;;;;;CAQA,YAAY,SAAiB,eAAgC;EAC3D,MAAM,OAAO;EACb,KAAK,gBAAgB;CACvB;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,uBAAb,MAAa,6BAA6B,GAAG,MAC3C,gBACA,kBACF,CAAC,CAAC;CACA,OAAgB;;;;;;;CAQhB;CAEA,YAAY,SAAkB;EAC5B,MAAM,WAAW,4CAA4C;CAC/D;;;;;;;;;;;;;;;;;;;;CAqBA,OAAO,UAAU,KAAkC;EACjD,MAAM,QAAQ,IAAI,qBAAqB,IAAI,OAAO;EAClD,MAAM,QAAQ;EACd,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"index.js","names":["baseNs"],"sources":["../../src/errors/index.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\n\nimport type { AIMessageChunk } from \"../messages/ai.js\";\nimport { ns as baseNs } from \"../utils/namespace.js\";\n\nexport type LangChainErrorCodes =\n | \"CONTEXT_OVERFLOW\"\n | \"INVALID_PROMPT_INPUT\"\n | \"INVALID_TOOL_RESULTS\"\n | \"MESSAGE_COERCION_FAILURE\"\n | \"MODEL_AUTHENTICATION\"\n | \"MODEL_NOT_FOUND\"\n | \"MODEL_RATE_LIMIT\"\n | \"OUTPUT_PARSING_FAILURE\"\n | \"MODEL_ABORTED\";\n\n/** @deprecated Subclass LangChainError instead */\nexport function addLangChainErrorFields(\n error: any,\n lc_error_code: LangChainErrorCodes\n) {\n (error as any).lc_error_code = lc_error_code;\n error.message = `${error.message}\\n\\nTroubleshooting URL: https://docs.langchain.com/oss/javascript/langchain/errors/${lc_error_code}/\\n`;\n return error;\n}\n\n/** The error namespace for all LangChain errors */\nexport const ns = baseNs.sub(\"error\");\n\n/** Registered globally so duplicate copies of core in one dependency tree agree. */\nconst retryableSymbol = Symbol.for(\"langchain.errors.retryable\");\n\n/**\n * Mark an error as safe or unsafe to retry.\n *\n * Sets a non-enumerable symbol on the error itself, leaving its class and\n * shape untouched, so it is safe to apply to a provider SDK's own error.\n *\n * @param error - The error to mark. Non-objects are returned untouched.\n * @param retryable - `true` if retrying may succeed.\n * @returns The same error instance, for chaining.\n */\nexport function stampRetryable<T>(error: T, retryable: boolean): T {\n if (typeof error !== \"object\" || error === null) {\n return error;\n }\n try {\n Object.defineProperty(error, retryableSymbol, {\n value: retryable,\n configurable: true,\n });\n } catch {\n // Frozen or sealed error object; leave it unmarked rather than throwing.\n }\n return error;\n}\n\n/**\n * Read an error's retryability mark.\n *\n * @returns `true`/`false` when marked, `undefined` when unclassified —\n * supply your own default, e.g. `getRetryable(error) ?? true`.\n */\nexport function getRetryable(error: unknown): boolean | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n return Object.getOwnPropertyDescriptor(error, retryableSymbol)?.value as\n | boolean\n | undefined;\n}\n\n/**\n * Base error class for all LangChain errors.\n *\n * All LangChain error classes should extend this class (directly or\n * indirectly). Use `LangChainError.isInstance(obj)` to check if an\n * object is any LangChain error.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(\"hello\");\n * } catch (error) {\n * if (LangChainError.isInstance(error)) {\n * console.log(\"Got a LangChain error:\", error.message);\n * }\n * }\n * ```\n */\nexport class LangChainError extends ns.brand(Error) {\n readonly name: string = \"LangChainError\";\n\n constructor(message?: string) {\n super(message);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor);\n }\n }\n}\n\n/**\n * Error class representing an aborted model operation in LangChain.\n *\n * This error is thrown when a model operation (such as invocation, streaming, or batching)\n * is cancelled before it completes, commonly due to a user-initiated abort signal\n * (e.g., via an AbortController) or an upstream cancellation event.\n *\n * The ModelAbortError provides access to any partial output the model may have produced\n * before the operation was interrupted, which can be useful for resuming work, debugging,\n * or presenting incomplete results to users.\n *\n * @remarks\n * - The `partialOutput` field includes message content that was generated prior to the abort,\n * such as a partial AIMessageChunk.\n * - This error extends the {@link LangChainError} base class with the marker `\"model-abort\"`.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input, { signal: abortController.signal });\n * } catch (err) {\n * if (ModelAbortError.isInstance(err)) {\n * // Handle user cancellation, check err.partialOutput if needed\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ModelAbortError extends ns.brand(LangChainError, \"model-abort\") {\n readonly name = \"ModelAbortError\";\n\n /**\n * The partial message output that was produced before the operation was aborted.\n * This is typically an AIMessageChunk, or could be undefined if no output was available.\n */\n readonly partialOutput?: AIMessageChunk;\n\n /**\n * Constructs a new ModelAbortError instance.\n *\n * @param message - A human-readable message describing the abort event.\n * @param partialOutput - Any partial model output generated before the abort (optional).\n */\n constructor(message: string, partialOutput?: AIMessageChunk) {\n super(message);\n this.partialOutput = partialOutput;\n // Retrying would mean ignoring the caller's explicit abort signal.\n stampRetryable(this, false);\n }\n}\n\n/**\n * Error class representing a context window overflow in a language model operation.\n *\n * This error is thrown when the combined input to a language model (such as prompt tokens,\n * historical messages, and/or instructions) exceeds the maximum context window or token limit\n * that the model can process in a single request. Most models have defined upper limits for the number of\n * tokens or characters allowed in a context, and exceeding this limit will prevent\n * the operation from proceeding.\n *\n * The {@link ContextOverflowError} extends the {@link LangChainError} base class with\n * the marker `\"context-overflow\"`.\n *\n * @remarks\n * - Use this error to programmatically identify cases where a user request, prompt, or input\n * sequence is too long to be handled by the target model.\n * - Model providers and framework integrations should throw this error if they detect\n * a request cannot be processed due to its size.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(veryLongInput);\n * } catch (err) {\n * if (ContextOverflowError.isInstance(err)) {\n * // Handle overflow, e.g., prompt user to shorten input or truncate text\n * console.warn(\"Model context overflow:\", err.message);\n * } else {\n * throw err;\n * }\n * }\n * ```\n */\nexport class ContextOverflowError extends ns.brand(\n LangChainError,\n \"context-overflow\"\n) {\n readonly name = \"ContextOverflowError\";\n\n /**\n * The underlying error that caused this {@link ContextOverflowError}, if any.\n *\n * This property is optionally set when wrapping a lower-level error using {@link ContextOverflowError.fromError}.\n * It allows error handlers to access or inspect the original error that led to the context overflow.\n */\n cause?: Error;\n\n constructor(message?: string) {\n super(message ?? \"Input exceeded the model's context window.\");\n // The same oversized input fails identically; it needs trimming, not another attempt.\n stampRetryable(this, false);\n }\n\n /**\n * Creates a new {@link ContextOverflowError} instance from an existing error.\n *\n * This static utility copies the message from the provided error and\n * attaches the original error as the {@link ContextOverflowError.cause} property,\n * enabling error handlers to inspect or propagate the original failure.\n *\n * @param obj - The original error object causing the context overflow.\n * @returns A new {@link ContextOverflowError} instance with the original error set as its cause.\n *\n * @example\n * ```typescript\n * try {\n * await model.invoke(input);\n * } catch (err) {\n * throw ContextOverflowError.fromError(err);\n * }\n * ```\n */\n static fromError(obj: Error): ContextOverflowError {\n const error = new ContextOverflowError(obj.message);\n error.cause = obj;\n return error;\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,SAAgB,wBACd,OACA,eACA;CACA,MAAe,gBAAgB;CAC/B,MAAM,UAAU,GAAG,MAAM,QAAQ,sFAAsF,cAAc;CACrI,OAAO;AACT;;AAGA,MAAa,KAAKA,KAAO,IAAI,OAAO;;AAGpC,MAAM,kBAAkB,OAAO,IAAI,4BAA4B;;;;;;;;;;;AAY/D,SAAgB,eAAkB,OAAU,WAAuB;CACjE,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC,OAAO;CAET,IAAI;EACF,OAAO,eAAe,OAAO,iBAAiB;GAC5C,OAAO;GACP,cAAc;EAChB,CAAC;CACH,QAAQ,CAER;CACA,OAAO;AACT;;;;;;;AAQA,SAAgB,aAAa,OAAqC;CAChE,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAEF,OAAO,OAAO,yBAAyB,OAAO,eAAe,CAAC,EAAE;AAGlE;;;;;;;;;;;;;;;;;;;AAoBA,IAAa,iBAAb,cAAoC,GAAG,MAAM,KAAK,CAAC,CAAC;CAClD,OAAwB;CAExB,YAAY,SAAkB;EAC5B,MAAM,OAAO;EACb,IAAI,MAAM,mBACR,MAAM,kBAAkB,MAAM,KAAK,WAAW;CAElD;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,IAAa,kBAAb,cAAqC,GAAG,MAAM,gBAAgB,aAAa,CAAC,CAAC;CAC3E,OAAgB;;;;;CAMhB;;;;;;;CAQA,YAAY,SAAiB,eAAgC;EAC3D,MAAM,OAAO;EACb,KAAK,gBAAgB;EAErB,eAAe,MAAM,KAAK;CAC5B;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,uBAAb,MAAa,6BAA6B,GAAG,MAC3C,gBACA,kBACF,CAAC,CAAC;CACA,OAAgB;;;;;;;CAQhB;CAEA,YAAY,SAAkB;EAC5B,MAAM,WAAW,4CAA4C;EAE7D,eAAe,MAAM,KAAK;CAC5B;;;;;;;;;;;;;;;;;;;;CAqBA,OAAO,UAAU,KAAkC;EACjD,MAAM,QAAQ,IAAI,qBAAqB,IAAI,OAAO;EAClD,MAAM,QAAQ;EACd,OAAO;CACT;AACF"}
@@ -139,7 +139,7 @@ var BaseLangChain = class extends require_base.Runnable {
139
139
  this.callbacks = params.callbacks;
140
140
  this.tags = params.tags ?? [];
141
141
  this.metadata = params.metadata ?? {};
142
- this._addVersion("@langchain/core", "1.2.7");
142
+ this._addVersion("@langchain/core", "1.2.8");
143
143
  }
144
144
  _addVersion(pkg, version) {
145
145
  const existing = this.metadata?.versions;
@@ -1 +1 @@
1
- {"version":3,"file":"base.cjs","names":["encodingForModel","Runnable","InMemoryCache","AsyncCaller","StringPromptValue","ChatPromptValue","coerceMessageLikeToMessage"],"sources":["../../src/language_models/base.ts"],"sourcesContent":["import type { Tiktoken, TiktokenModel } from \"js-tiktoken/lite\";\nimport type { ZodV3Like, ZodV4Like } from \"../utils/types/zod.js\";\n\nimport { type BaseCache, InMemoryCache } from \"../caches/index.js\";\nimport {\n type BasePromptValueInterface,\n StringPromptValue,\n ChatPromptValue,\n} from \"../prompt_values.js\";\nimport {\n type BaseMessage,\n type BaseMessageLike,\n type MessageContent,\n} from \"../messages/base.js\";\nimport { coerceMessageLikeToMessage } from \"../messages/utils.js\";\nimport { type LLMResult } from \"../outputs.js\";\nimport { CallbackManager, Callbacks } from \"../callbacks/manager.js\";\nimport { AsyncCaller, AsyncCallerParams } from \"../utils/async_caller.js\";\nimport { encodingForModel } from \"../utils/tiktoken.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { JSONSchema } from \"../utils/json_schema.js\";\nimport {\n InferInteropZodOutput,\n InteropZodObject,\n InteropZodType,\n} from \"../utils/types/zod.js\";\nimport { ModelProfile } from \"./profile.js\";\nimport { type SerializableSchema } from \"../utils/standard_schema.js\";\n\n// https://www.npmjs.com/package/js-tiktoken\n\nexport const getModelNameForTiktoken = (modelName: string): TiktokenModel => {\n if (modelName.startsWith(\"gpt-5\")) {\n return \"gpt-5\" as TiktokenModel;\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-16k\")) {\n return \"gpt-3.5-turbo-16k\";\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-\")) {\n return \"gpt-3.5-turbo\";\n }\n\n if (modelName.startsWith(\"gpt-4-32k\")) {\n return \"gpt-4-32k\";\n }\n\n if (modelName.startsWith(\"gpt-4-\")) {\n return \"gpt-4\";\n }\n\n if (modelName.startsWith(\"gpt-4o\")) {\n return \"gpt-4o\";\n }\n\n return modelName as TiktokenModel;\n};\n\nexport const getEmbeddingContextSize = (modelName?: string): number => {\n switch (modelName) {\n case \"text-embedding-ada-002\":\n return 8191;\n default:\n return 2046;\n }\n};\n\n/**\n * Get the context window size (max input tokens) for a given model.\n *\n * Context window sizes are sourced from official model documentation:\n * - OpenAI: https://platform.openai.com/docs/models\n * - Anthropic: https://docs.anthropic.com/claude/docs/models-overview\n * - Google: https://ai.google.dev/gemini/docs/models/gemini\n *\n * @param modelName - The name of the model\n * @returns The context window size in tokens\n */\nexport const getModelContextSize = (modelName: string): number => {\n const normalizedName = getModelNameForTiktoken(modelName) as string;\n\n switch (normalizedName) {\n // GPT-5 series\n case \"gpt-5\":\n case \"gpt-5-turbo\":\n case \"gpt-5-turbo-preview\":\n return 400000;\n\n // GPT-4o series\n case \"gpt-4o\":\n case \"gpt-4o-mini\":\n case \"gpt-4o-2024-05-13\":\n case \"gpt-4o-2024-08-06\":\n return 128000;\n\n // GPT-4 Turbo series\n case \"gpt-4-turbo\":\n case \"gpt-4-turbo-preview\":\n case \"gpt-4-turbo-2024-04-09\":\n case \"gpt-4-0125-preview\":\n case \"gpt-4-1106-preview\":\n return 128000;\n\n // GPT-4 series\n case \"gpt-4-32k\":\n case \"gpt-4-32k-0314\":\n case \"gpt-4-32k-0613\":\n return 32768;\n case \"gpt-4\":\n case \"gpt-4-0314\":\n case \"gpt-4-0613\":\n return 8192;\n\n // GPT-3.5 Turbo series\n case \"gpt-3.5-turbo-16k\":\n case \"gpt-3.5-turbo-16k-0613\":\n return 16384;\n case \"gpt-3.5-turbo\":\n case \"gpt-3.5-turbo-0301\":\n case \"gpt-3.5-turbo-0613\":\n case \"gpt-3.5-turbo-1106\":\n case \"gpt-3.5-turbo-0125\":\n return 4096;\n\n // Legacy GPT-3 models\n case \"text-davinci-003\":\n case \"text-davinci-002\":\n return 4097;\n case \"text-davinci-001\":\n return 2049;\n case \"text-curie-001\":\n case \"text-babbage-001\":\n case \"text-ada-001\":\n return 2048;\n\n // Code models\n case \"code-davinci-002\":\n case \"code-davinci-001\":\n return 8000;\n case \"code-cushman-001\":\n return 2048;\n\n // Claude models (Anthropic)\n case \"claude-3-5-sonnet-20241022\":\n case \"claude-3-5-sonnet-20240620\":\n case \"claude-3-opus-20240229\":\n case \"claude-3-sonnet-20240229\":\n case \"claude-3-haiku-20240307\":\n case \"claude-2.1\":\n return 200000;\n case \"claude-2.0\":\n case \"claude-instant-1.2\":\n return 100000;\n\n // Gemini models (Google)\n case \"gemini-1.5-pro\":\n case \"gemini-1.5-pro-latest\":\n case \"gemini-1.5-flash\":\n case \"gemini-1.5-flash-latest\":\n return 1000000; // 1M tokens\n case \"gemini-pro\":\n case \"gemini-pro-vision\":\n return 32768;\n\n default:\n return 4097;\n }\n};\n\n/**\n * Whether or not the input matches the OpenAI tool definition.\n * @param {unknown} tool The input to check.\n * @returns {boolean} Whether the input is an OpenAI tool definition.\n */\nexport function isOpenAITool(tool: unknown): tool is ToolDefinition {\n if (typeof tool !== \"object\" || !tool) return false;\n if (\n \"type\" in tool &&\n tool.type === \"function\" &&\n \"function\" in tool &&\n typeof tool.function === \"object\" &&\n tool.function &&\n \"name\" in tool.function &&\n \"parameters\" in tool.function\n ) {\n return true;\n }\n return false;\n}\n\ninterface CalculateMaxTokenProps {\n prompt: string;\n modelName: TiktokenModel;\n}\n\nexport const calculateMaxTokens = async ({\n prompt,\n modelName,\n}: CalculateMaxTokenProps) => {\n let numTokens;\n\n try {\n numTokens = (\n await encodingForModel(getModelNameForTiktoken(modelName))\n ).encode(prompt).length;\n } catch {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\"\n );\n\n // fallback to approximate calculation if tiktoken is not available\n // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#\n numTokens = Math.ceil(prompt.length / 4);\n }\n\n const maxTokens = getModelContextSize(modelName);\n return maxTokens - numTokens;\n};\n\nconst getVerbosity = () => false;\n\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n} & Record<string, any>;\n\nexport interface BaseLangChainParams {\n verbose?: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Base class for language models, chains, tools.\n */\nexport abstract class BaseLangChain<\n RunInput,\n RunOutput,\n CallOptions extends RunnableConfig = RunnableConfig,\n>\n extends Runnable<RunInput, RunOutput, CallOptions>\n implements BaseLangChainParams\n{\n /**\n * Whether to print out response text.\n */\n verbose: boolean;\n\n callbacks?: Callbacks;\n\n tags?: string[];\n\n metadata?: Record<string, unknown>;\n\n get lc_attributes(): { [key: string]: undefined } | undefined {\n return {\n callbacks: undefined,\n verbose: undefined,\n };\n }\n\n constructor(params: BaseLangChainParams) {\n super(params);\n this.verbose = params.verbose ?? getVerbosity();\n this.callbacks = params.callbacks;\n this.tags = params.tags ?? [];\n this.metadata = params.metadata ?? {};\n this._addVersion(\"@langchain/core\", __PKG_VERSION__);\n }\n\n protected _addVersion(pkg: string, version: string) {\n const existing = this.metadata?.versions;\n this.metadata = {\n ...this.metadata,\n versions: {\n ...(typeof existing === \"object\" && existing !== null ? existing : {}),\n [pkg]: version,\n },\n };\n }\n}\n\n/**\n * Base interface for language model parameters.\n * A subclass of {@link BaseLanguageModel} should have a constructor that\n * takes in a parameter that extends this interface.\n */\nexport interface BaseLanguageModelParams\n extends AsyncCallerParams, BaseLangChainParams {\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n\n cache?: BaseCache | boolean;\n}\n\nexport interface BaseLanguageModelTracingCallOptions {\n /**\n * Describes the format of structured outputs.\n * This should be provided if an output is considered to be structured\n */\n ls_structured_output_format?: {\n /**\n * An object containing the method used for structured output (e.g., \"jsonMode\").\n */\n kwargs: { method: string };\n /**\n * The JSON schema describing the expected output structure.\n */\n schema?: JSONSchema;\n };\n}\n\nexport interface BaseLanguageModelCallOptions\n extends RunnableConfig, BaseLanguageModelTracingCallOptions {\n /**\n * Stop tokens to use for this call.\n * If not provided, the default stop tokens for the model will be used.\n */\n stop?: string[];\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for\n * examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown> | JSONSchema;\n\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallOption = {\n name: string;\n};\n\nexport interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {\n function_call?: FunctionCallOption;\n functions?: FunctionDefinition[];\n}\n\nexport type BaseLanguageModelInput =\n | BasePromptValueInterface\n | string\n | BaseMessageLike[];\n\nexport type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;\n\nexport type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =\n {\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\" | \"jsonSchema\" | string;\n includeRaw?: IncludeRaw;\n /** Whether to use strict mode. Currently only supported by OpenAI models. */\n strict?: boolean;\n };\n\n/** @deprecated Use StructuredOutputMethodOptions instead */\nexport type StructuredOutputMethodParams<\n RunOutput,\n IncludeRaw extends boolean = false,\n> = {\n /** @deprecated Pass schema in as the first argument */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: InteropZodType<RunOutput> | Record<string, any>;\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\";\n includeRaw?: IncludeRaw;\n};\n\nexport interface BaseLanguageModelInterface<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {\n get callKeys(): string[];\n\n generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | Partial<CallOptions>,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n _modelType(): string;\n\n _llmType(): string;\n\n getNumTokens(content: MessageContent): Promise<number>;\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any>;\n\n serialize(): SerializedLLM;\n}\n\nexport type LanguageModelOutput = BaseMessage | string;\n\nexport type LanguageModelLike = RunnableInterface<\n BaseLanguageModelInput,\n LanguageModelOutput\n>;\n\n/**\n * Base class for language models.\n */\nexport abstract class BaseLanguageModel<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n>\n extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions>\n implements\n BaseLanguageModelParams,\n BaseLanguageModelInterface<RunOutput, CallOptions>\n{\n /**\n * Keys that the language model accepts as call options.\n */\n get callKeys(): string[] {\n return [\"stop\", \"timeout\", \"signal\", \"tags\", \"metadata\", \"callbacks\"];\n }\n\n /**\n * The async caller should be used by subclasses to make any async calls,\n * which will thus benefit from the concurrency and retry logic.\n */\n caller: AsyncCaller;\n\n cache?: BaseCache;\n\n constructor({\n callbacks,\n callbackManager,\n ...params\n }: BaseLanguageModelParams) {\n const { cache, ...rest } = params;\n super({\n callbacks: callbacks ?? callbackManager,\n ...rest,\n });\n if (typeof cache === \"object\") {\n this.cache = cache;\n } else if (cache) {\n this.cache = InMemoryCache.global();\n } else {\n this.cache = undefined;\n }\n this.caller = new AsyncCaller(params ?? {});\n }\n\n abstract generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | CallOptions,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n abstract _modelType(): string;\n\n abstract _llmType(): string;\n\n private _encoding?: Tiktoken;\n\n /**\n * Get the number of tokens in the content.\n * @param content The content to get the number of tokens for.\n * @returns The number of tokens in the content.\n */\n async getNumTokens(content: MessageContent) {\n // Extract text content from MessageContent\n let textContent: string;\n if (typeof content === \"string\") {\n textContent = content;\n } else {\n /**\n * Content is an array of ContentBlock\n *\n * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.\n * We need to find a better way to do this.\n * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116\n */\n textContent = content\n .map((item) => {\n if (typeof item === \"string\") return item;\n if (item.type === \"text\" && \"text\" in item) return item.text;\n return \"\";\n })\n .join(\"\");\n }\n\n // fallback to approximate calculation if tiktoken is not available\n let numTokens = Math.ceil(textContent.length / 4);\n\n if (!this._encoding) {\n try {\n this._encoding = await encodingForModel(\n \"modelName\" in this\n ? getModelNameForTiktoken(this.modelName as string)\n : \"gpt2\"\n );\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n if (this._encoding) {\n try {\n numTokens = this._encoding.encode(textContent).length;\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n return numTokens;\n }\n\n protected static _convertInputToPromptValue(\n input: BaseLanguageModelInput\n ): BasePromptValueInterface {\n if (typeof input === \"string\") {\n return new StringPromptValue(input);\n } else if (Array.isArray(input)) {\n return new ChatPromptValue(input.map(coerceMessageLikeToMessage));\n } else {\n return input;\n }\n }\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any> {\n return {};\n }\n\n /**\n * Create a unique cache key for a specific call to a specific language model.\n * @param callOptions Call options for the model\n * @returns A unique cache key.\n */\n _getSerializedCacheKeyParametersForCall(\n // TODO: Fix when we remove the RunnableLambda backwards compatibility shim.\n {\n config,\n ...callOptions\n }: CallOptions & { config?: RunnableConfig }\n ): string {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const params: Record<string, any> = {\n ...this._identifyingParams(),\n ...callOptions,\n _type: this._llmType(),\n _model: this._modelType(),\n };\n const filteredEntries = Object.entries(params).filter(\n ([_, value]) => value !== undefined\n );\n const serializedEntries = filteredEntries\n .map(([key, value]) => `${key}:${JSON.stringify(value)}`)\n .sort()\n .join(\",\");\n return serializedEntries;\n }\n\n /**\n * @deprecated\n * Return a json-like object representing this LLM.\n */\n serialize(): SerializedLLM {\n return {\n ...this._identifyingParams(),\n _type: this._llmType(),\n _model: this._modelType(),\n };\n }\n\n /**\n * @deprecated\n * Load an LLM from a json-like object describing it.\n */\n static async deserialize(_data: SerializedLLM): Promise<BaseLanguageModel> {\n throw new Error(\"Use .toJSON() instead\");\n }\n\n /**\n * Return profiling information for the model.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n */\n get profile(): ModelProfile {\n return {};\n }\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n /**\n * Model wrapper that returns outputs formatted to match the given schema.\n *\n * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.\n * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.\n *\n * @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.\n * If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.\n * @param {string} name The name of the function to call.\n * @param {\"functionCalling\" | \"jsonMode\"} [method=functionCalling] The method to use for getting the structured output. Defaults to \"functionCalling\".\n * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.\n * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.\n */\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | InteropZodType<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n {\n raw: BaseMessage;\n parsed: RunOutput;\n }\n >;\n\n /**\n * Filter out large/inappropriate fields from invocation params for tracing metadata.\n * Removes fields like tools, functions, messages, response_format that can be large.\n */\n protected _filterInvocationParamsForTracing(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ): Record<string, any> {\n const { tools, functions, messages, response_format, ...rest } = params;\n return rest;\n }\n}\n\n/**\n * Shared interface for token usage\n * return type from LLM calls.\n */\nexport interface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgCA,MAAa,2BAA2B,cAAqC;CAC3E,IAAI,UAAU,WAAW,OAAO,GAC9B,OAAO;CAGT,IAAI,UAAU,WAAW,mBAAmB,GAC1C,OAAO;CAGT,IAAI,UAAU,WAAW,gBAAgB,GACvC,OAAO;CAGT,IAAI,UAAU,WAAW,WAAW,GAClC,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,OAAO;AACT;AAEA,MAAa,2BAA2B,cAA+B;CACrE,QAAQ,WAAR;EACE,KAAK,0BACH,OAAO;EACT,SACE,OAAO;CACX;AACF;;;;;;;;;;;;AAaA,MAAa,uBAAuB,cAA8B;CAGhE,QAFuB,wBAAwB,SAE1B,GAArB;EAEE,KAAK;EACL,KAAK;EACL,KAAK,uBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,qBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK,kBACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,cACH,OAAO;EAGT,KAAK;EACL,KAAK,0BACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK,oBACH,OAAO;EACT,KAAK,oBACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,gBACH,OAAO;EAGT,KAAK;EACL,KAAK,oBACH,OAAO;EACT,KAAK,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BAAAA,kBAAkB,KAAK;OAC7B,IAAI,MAAM,QAAQ,KAAK,GAC5B,OAAO,IAAIC,sBAAAA,gBAAgB,MAAM,IAAIC,cAAAA,0BAA0B,CAAC;OAEhE,OAAO;CAEX;;;;CAMA,qBAA0C;EACxC,OAAO,CAAC;CACV;;;;;;CAOA,wCAEE,EACE,QACA,GAAG,eAEG;EAER,MAAM,SAA8B;GAClC,GAAG,KAAK,mBAAmB;GAC3B,GAAG;GACH,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;EAQA,OAPwB,OAAO,QAAQ,MAAM,CAAC,CAAC,QAC5C,CAAC,GAAG,WAAW,UAAU,KAAA,CAEY,CAAC,CACtC,KAAK,CAAC,KAAK,WAAW,GAAG,IAAI,GAAG,KAAK,UAAU,KAAK,GAAG,CAAC,CACxD,KAAK,CAAC,CACN,KAAK,GACe;CACzB;;;;;CAMA,YAA2B;EACzB,OAAO;GACL,GAAG,KAAK,mBAAmB;GAC3B,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;CACF;;;;;CAMA,aAAa,YAAY,OAAkD;EACzE,MAAM,IAAI,MAAM,uBAAuB;CACzC;;;;;;CAOA,IAAI,UAAwB;EAC1B,OAAO,CAAC;CACV;;;;;CAkGA,kCAEE,QAEqB;EACrB,MAAM,EAAE,OAAO,WAAW,UAAU,iBAAiB,GAAG,SAAS;EACjE,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"base.cjs","names":["encodingForModel","Runnable","InMemoryCache","AsyncCaller","StringPromptValue","ChatPromptValue","coerceMessageLikeToMessage"],"sources":["../../src/language_models/base.ts"],"sourcesContent":["import type { Tiktoken, TiktokenModel } from \"js-tiktoken/lite\";\nimport type { ZodV3Like, ZodV4Like } from \"../utils/types/zod.js\";\n\nimport { type BaseCache, InMemoryCache } from \"../caches/index.js\";\nimport {\n type BasePromptValueInterface,\n StringPromptValue,\n ChatPromptValue,\n} from \"../prompt_values.js\";\nimport {\n type BaseMessage,\n type BaseMessageLike,\n type MessageContent,\n} from \"../messages/base.js\";\nimport { coerceMessageLikeToMessage } from \"../messages/utils.js\";\nimport { type LLMResult } from \"../outputs.js\";\nimport { CallbackManager, Callbacks } from \"../callbacks/manager.js\";\nimport { AsyncCaller, AsyncCallerParams } from \"../utils/async_caller.js\";\nimport { encodingForModel } from \"../utils/tiktoken.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { JSONSchema } from \"../utils/json_schema.js\";\nimport {\n InferInteropZodOutput,\n InteropZodObject,\n InteropZodType,\n} from \"../utils/types/zod.js\";\nimport { ModelProfile } from \"./profile.js\";\nimport { type SerializableSchema } from \"../utils/standard_schema.js\";\n\n// https://www.npmjs.com/package/js-tiktoken\n\nexport const getModelNameForTiktoken = (modelName: string): TiktokenModel => {\n if (modelName.startsWith(\"gpt-5\")) {\n return \"gpt-5\" as TiktokenModel;\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-16k\")) {\n return \"gpt-3.5-turbo-16k\";\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-\")) {\n return \"gpt-3.5-turbo\";\n }\n\n if (modelName.startsWith(\"gpt-4-32k\")) {\n return \"gpt-4-32k\";\n }\n\n if (modelName.startsWith(\"gpt-4-\")) {\n return \"gpt-4\";\n }\n\n if (modelName.startsWith(\"gpt-4o\")) {\n return \"gpt-4o\";\n }\n\n return modelName as TiktokenModel;\n};\n\nexport const getEmbeddingContextSize = (modelName?: string): number => {\n switch (modelName) {\n case \"text-embedding-ada-002\":\n return 8191;\n default:\n return 2046;\n }\n};\n\n/**\n * Get the context window size (max input tokens) for a given model.\n *\n * Context window sizes are sourced from official model documentation:\n * - OpenAI: https://platform.openai.com/docs/models\n * - Anthropic: https://docs.anthropic.com/claude/docs/models-overview\n * - Google: https://ai.google.dev/gemini/docs/models/gemini\n *\n * @param modelName - The name of the model\n * @returns The context window size in tokens\n */\nexport const getModelContextSize = (modelName: string): number => {\n const normalizedName = getModelNameForTiktoken(modelName) as string;\n\n switch (normalizedName) {\n // GPT-5 series\n case \"gpt-5\":\n case \"gpt-5-turbo\":\n case \"gpt-5-turbo-preview\":\n return 400000;\n\n // GPT-4o series\n case \"gpt-4o\":\n case \"gpt-4o-mini\":\n case \"gpt-4o-2024-05-13\":\n case \"gpt-4o-2024-08-06\":\n return 128000;\n\n // GPT-4 Turbo series\n case \"gpt-4-turbo\":\n case \"gpt-4-turbo-preview\":\n case \"gpt-4-turbo-2024-04-09\":\n case \"gpt-4-0125-preview\":\n case \"gpt-4-1106-preview\":\n return 128000;\n\n // GPT-4 series\n case \"gpt-4-32k\":\n case \"gpt-4-32k-0314\":\n case \"gpt-4-32k-0613\":\n return 32768;\n case \"gpt-4\":\n case \"gpt-4-0314\":\n case \"gpt-4-0613\":\n return 8192;\n\n // GPT-3.5 Turbo series\n case \"gpt-3.5-turbo-16k\":\n case \"gpt-3.5-turbo-16k-0613\":\n return 16384;\n case \"gpt-3.5-turbo\":\n case \"gpt-3.5-turbo-0301\":\n case \"gpt-3.5-turbo-0613\":\n case \"gpt-3.5-turbo-1106\":\n case \"gpt-3.5-turbo-0125\":\n return 4096;\n\n // Legacy GPT-3 models\n case \"text-davinci-003\":\n case \"text-davinci-002\":\n return 4097;\n case \"text-davinci-001\":\n return 2049;\n case \"text-curie-001\":\n case \"text-babbage-001\":\n case \"text-ada-001\":\n return 2048;\n\n // Code models\n case \"code-davinci-002\":\n case \"code-davinci-001\":\n return 8000;\n case \"code-cushman-001\":\n return 2048;\n\n // Claude models (Anthropic)\n case \"claude-3-5-sonnet-20241022\":\n case \"claude-3-5-sonnet-20240620\":\n case \"claude-3-opus-20240229\":\n case \"claude-3-sonnet-20240229\":\n case \"claude-3-haiku-20240307\":\n case \"claude-2.1\":\n return 200000;\n case \"claude-2.0\":\n case \"claude-instant-1.2\":\n return 100000;\n\n // Gemini models (Google)\n case \"gemini-1.5-pro\":\n case \"gemini-1.5-pro-latest\":\n case \"gemini-1.5-flash\":\n case \"gemini-1.5-flash-latest\":\n return 1000000; // 1M tokens\n case \"gemini-pro\":\n case \"gemini-pro-vision\":\n return 32768;\n\n default:\n return 4097;\n }\n};\n\n/**\n * Whether or not the input matches the OpenAI tool definition.\n * @param {unknown} tool The input to check.\n * @returns {boolean} Whether the input is an OpenAI tool definition.\n */\nexport function isOpenAITool(tool: unknown): tool is ToolDefinition {\n if (typeof tool !== \"object\" || !tool) return false;\n if (\n \"type\" in tool &&\n tool.type === \"function\" &&\n \"function\" in tool &&\n typeof tool.function === \"object\" &&\n tool.function &&\n \"name\" in tool.function &&\n \"parameters\" in tool.function\n ) {\n return true;\n }\n return false;\n}\n\ninterface CalculateMaxTokenProps {\n prompt: string;\n modelName: TiktokenModel;\n}\n\nexport const calculateMaxTokens = async ({\n prompt,\n modelName,\n}: CalculateMaxTokenProps) => {\n let numTokens;\n\n try {\n numTokens = (\n await encodingForModel(getModelNameForTiktoken(modelName))\n ).encode(prompt).length;\n } catch {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\"\n );\n\n // fallback to approximate calculation if tiktoken is not available\n // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#\n numTokens = Math.ceil(prompt.length / 4);\n }\n\n const maxTokens = getModelContextSize(modelName);\n return maxTokens - numTokens;\n};\n\nconst getVerbosity = () => false;\n\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n} & Record<string, any>;\n\nexport interface BaseLangChainParams {\n verbose?: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Base class for language models, chains, tools.\n */\nexport abstract class BaseLangChain<\n RunInput,\n RunOutput,\n CallOptions extends RunnableConfig = RunnableConfig,\n>\n extends Runnable<RunInput, RunOutput, CallOptions>\n implements BaseLangChainParams\n{\n /**\n * Whether to print out response text.\n */\n verbose: boolean;\n\n callbacks?: Callbacks;\n\n tags?: string[];\n\n metadata?: Record<string, unknown>;\n\n get lc_attributes(): { [key: string]: undefined } | undefined {\n return {\n callbacks: undefined,\n verbose: undefined,\n };\n }\n\n constructor(params: BaseLangChainParams) {\n super(params);\n this.verbose = params.verbose ?? getVerbosity();\n this.callbacks = params.callbacks;\n this.tags = params.tags ?? [];\n this.metadata = params.metadata ?? {};\n this._addVersion(\"@langchain/core\", __PKG_VERSION__);\n }\n\n protected _addVersion(pkg: string, version: string) {\n const existing = this.metadata?.versions;\n this.metadata = {\n ...this.metadata,\n versions: {\n ...(typeof existing === \"object\" && existing !== null ? existing : {}),\n [pkg]: version,\n },\n };\n }\n}\n\n/**\n * Base interface for language model parameters.\n * A subclass of {@link BaseLanguageModel} should have a constructor that\n * takes in a parameter that extends this interface.\n */\nexport interface BaseLanguageModelParams\n extends AsyncCallerParams, BaseLangChainParams {\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n\n cache?: BaseCache | boolean;\n}\n\nexport interface BaseLanguageModelTracingCallOptions {\n /**\n * Describes the format of structured outputs.\n * This should be provided if an output is considered to be structured\n */\n ls_structured_output_format?: {\n /**\n * An object containing the method used for structured output (e.g., \"jsonMode\").\n */\n kwargs: { method: string };\n /**\n * The JSON schema describing the expected output structure.\n */\n schema?: JSONSchema;\n };\n}\n\nexport interface BaseLanguageModelCallOptions\n extends RunnableConfig, BaseLanguageModelTracingCallOptions {\n /**\n * Stop tokens to use for this call.\n * If not provided, the default stop tokens for the model will be used.\n */\n stop?: string[];\n /**\n * Overrides the model's configured `maxRetries` for this call only.\n */\n maxRetries?: number;\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for\n * examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown> | JSONSchema;\n\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallOption = {\n name: string;\n};\n\nexport interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {\n function_call?: FunctionCallOption;\n functions?: FunctionDefinition[];\n}\n\nexport type BaseLanguageModelInput =\n | BasePromptValueInterface\n | string\n | BaseMessageLike[];\n\nexport type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;\n\nexport type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =\n {\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\" | \"jsonSchema\" | string;\n includeRaw?: IncludeRaw;\n /** Whether to use strict mode. Currently only supported by OpenAI models. */\n strict?: boolean;\n };\n\n/** @deprecated Use StructuredOutputMethodOptions instead */\nexport type StructuredOutputMethodParams<\n RunOutput,\n IncludeRaw extends boolean = false,\n> = {\n /** @deprecated Pass schema in as the first argument */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: InteropZodType<RunOutput> | Record<string, any>;\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\";\n includeRaw?: IncludeRaw;\n};\n\nexport interface BaseLanguageModelInterface<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {\n get callKeys(): string[];\n\n generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | Partial<CallOptions>,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n _modelType(): string;\n\n _llmType(): string;\n\n getNumTokens(content: MessageContent): Promise<number>;\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any>;\n\n serialize(): SerializedLLM;\n}\n\nexport type LanguageModelOutput = BaseMessage | string;\n\nexport type LanguageModelLike = RunnableInterface<\n BaseLanguageModelInput,\n LanguageModelOutput\n>;\n\n/**\n * Base class for language models.\n */\nexport abstract class BaseLanguageModel<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n>\n extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions>\n implements\n BaseLanguageModelParams,\n BaseLanguageModelInterface<RunOutput, CallOptions>\n{\n /**\n * Keys that the language model accepts as call options.\n */\n get callKeys(): string[] {\n return [\"stop\", \"timeout\", \"signal\", \"tags\", \"metadata\", \"callbacks\"];\n }\n\n /**\n * The async caller should be used by subclasses to make any async calls,\n * which will thus benefit from the concurrency and retry logic.\n */\n caller: AsyncCaller;\n\n cache?: BaseCache;\n\n constructor({\n callbacks,\n callbackManager,\n ...params\n }: BaseLanguageModelParams) {\n const { cache, ...rest } = params;\n super({\n callbacks: callbacks ?? callbackManager,\n ...rest,\n });\n if (typeof cache === \"object\") {\n this.cache = cache;\n } else if (cache) {\n this.cache = InMemoryCache.global();\n } else {\n this.cache = undefined;\n }\n this.caller = new AsyncCaller(params ?? {});\n }\n\n abstract generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | CallOptions,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n abstract _modelType(): string;\n\n abstract _llmType(): string;\n\n private _encoding?: Tiktoken;\n\n /**\n * Get the number of tokens in the content.\n * @param content The content to get the number of tokens for.\n * @returns The number of tokens in the content.\n */\n async getNumTokens(content: MessageContent) {\n // Extract text content from MessageContent\n let textContent: string;\n if (typeof content === \"string\") {\n textContent = content;\n } else {\n /**\n * Content is an array of ContentBlock\n *\n * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.\n * We need to find a better way to do this.\n * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116\n */\n textContent = content\n .map((item) => {\n if (typeof item === \"string\") return item;\n if (item.type === \"text\" && \"text\" in item) return item.text;\n return \"\";\n })\n .join(\"\");\n }\n\n // fallback to approximate calculation if tiktoken is not available\n let numTokens = Math.ceil(textContent.length / 4);\n\n if (!this._encoding) {\n try {\n this._encoding = await encodingForModel(\n \"modelName\" in this\n ? getModelNameForTiktoken(this.modelName as string)\n : \"gpt2\"\n );\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n if (this._encoding) {\n try {\n numTokens = this._encoding.encode(textContent).length;\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n return numTokens;\n }\n\n protected static _convertInputToPromptValue(\n input: BaseLanguageModelInput\n ): BasePromptValueInterface {\n if (typeof input === \"string\") {\n return new StringPromptValue(input);\n } else if (Array.isArray(input)) {\n return new ChatPromptValue(input.map(coerceMessageLikeToMessage));\n } else {\n return input;\n }\n }\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any> {\n return {};\n }\n\n /**\n * Create a unique cache key for a specific call to a specific language model.\n * @param callOptions Call options for the model\n * @returns A unique cache key.\n */\n _getSerializedCacheKeyParametersForCall(\n // TODO: Fix when we remove the RunnableLambda backwards compatibility shim.\n {\n config,\n ...callOptions\n }: CallOptions & { config?: RunnableConfig }\n ): string {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const params: Record<string, any> = {\n ...this._identifyingParams(),\n ...callOptions,\n _type: this._llmType(),\n _model: this._modelType(),\n };\n const filteredEntries = Object.entries(params).filter(\n ([_, value]) => value !== undefined\n );\n const serializedEntries = filteredEntries\n .map(([key, value]) => `${key}:${JSON.stringify(value)}`)\n .sort()\n .join(\",\");\n return serializedEntries;\n }\n\n /**\n * @deprecated\n * Return a json-like object representing this LLM.\n */\n serialize(): SerializedLLM {\n return {\n ...this._identifyingParams(),\n _type: this._llmType(),\n _model: this._modelType(),\n };\n }\n\n /**\n * @deprecated\n * Load an LLM from a json-like object describing it.\n */\n static async deserialize(_data: SerializedLLM): Promise<BaseLanguageModel> {\n throw new Error(\"Use .toJSON() instead\");\n }\n\n /**\n * Return profiling information for the model.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n */\n get profile(): ModelProfile {\n return {};\n }\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n /**\n * Model wrapper that returns outputs formatted to match the given schema.\n *\n * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.\n * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.\n *\n * @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.\n * If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.\n * @param {string} name The name of the function to call.\n * @param {\"functionCalling\" | \"jsonMode\"} [method=functionCalling] The method to use for getting the structured output. Defaults to \"functionCalling\".\n * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.\n * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.\n */\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | InteropZodType<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n {\n raw: BaseMessage;\n parsed: RunOutput;\n }\n >;\n\n /**\n * Filter out large/inappropriate fields from invocation params for tracing metadata.\n * Removes fields like tools, functions, messages, response_format that can be large.\n */\n protected _filterInvocationParamsForTracing(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ): Record<string, any> {\n const { tools, functions, messages, response_format, ...rest } = params;\n return rest;\n }\n}\n\n/**\n * Shared interface for token usage\n * return type from LLM calls.\n */\nexport interface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgCA,MAAa,2BAA2B,cAAqC;CAC3E,IAAI,UAAU,WAAW,OAAO,GAC9B,OAAO;CAGT,IAAI,UAAU,WAAW,mBAAmB,GAC1C,OAAO;CAGT,IAAI,UAAU,WAAW,gBAAgB,GACvC,OAAO;CAGT,IAAI,UAAU,WAAW,WAAW,GAClC,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,OAAO;AACT;AAEA,MAAa,2BAA2B,cAA+B;CACrE,QAAQ,WAAR;EACE,KAAK,0BACH,OAAO;EACT,SACE,OAAO;CACX;AACF;;;;;;;;;;;;AAaA,MAAa,uBAAuB,cAA8B;CAGhE,QAFuB,wBAAwB,SAE1B,GAArB;EAEE,KAAK;EACL,KAAK;EACL,KAAK,uBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,qBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK,kBACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,cACH,OAAO;EAGT,KAAK;EACL,KAAK,0BACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK,oBACH,OAAO;EACT,KAAK,oBACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,K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AiB,2BACf,OAC0B;EAC1B,IAAI,OAAO,UAAU,UACnB,OAAO,IAAII,sBAAAA,kBAAkB,KAAK;OAC7B,IAAI,MAAM,QAAQ,KAAK,GAC5B,OAAO,IAAIC,sBAAAA,gBAAgB,MAAM,IAAIC,cAAAA,0BAA0B,CAAC;OAEhE,OAAO;CAEX;;;;CAMA,qBAA0C;EACxC,OAAO,CAAC;CACV;;;;;;CAOA,wCAEE,EACE,QACA,GAAG,eAEG;EAER,MAAM,SAA8B;GAClC,GAAG,KAAK,mBAAmB;GAC3B,GAAG;GACH,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;EAQA,OAPwB,OAAO,QAAQ,MAAM,CAAC,CAAC,QAC5C,CAAC,GAAG,WAAW,UAAU,KAAA,CAEY,CAAC,CACtC,KAAK,CAAC,KAAK,WAAW,GAAG,IAAI,GAAG,KAAK,UAAU,KAAK,GAAG,CAAC,CACxD,KAAK,CAAC,CACN,KAAK,GACe;CACzB;;;;;CAMA,YAA2B;EACzB,OAAO;GACL,GAAG,KAAK,mBAAmB;GAC3B,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;CACF;;;;;CAMA,aAAa,YAAY,OAAkD;EACzE,MAAM,IAAI,MAAM,uBAAuB;CACzC;;;;;;CAOA,IAAI,UAAwB;EAC1B,OAAO,CAAC;CACV;;;;;CAkGA,kCAEE,QAEqB;EACrB,MAAM,EAAE,OAAO,WAAW,UAAU,iBAAiB,GAAG,SAAS;EACjE,OAAO;CACT;AACF"}
@@ -102,6 +102,10 @@ interface BaseLanguageModelCallOptions extends RunnableConfig, BaseLanguageModel
102
102
  * If not provided, the default stop tokens for the model will be used.
103
103
  */
104
104
  stop?: string[];
105
+ /**
106
+ * Overrides the model's configured `maxRetries` for this call only.
107
+ */
108
+ maxRetries?: number;
105
109
  }
106
110
  interface FunctionDefinition {
107
111
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.cts","names":[],"sources":["../../src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EAER,cACE,WACA,oBACG,UACF;WAgBM,eACP,cAAc,4BACd,qBAAqB,aACrB,YAAY,YACX,QAAQ;WAEF;WAEA;UAED;;;;;;EAOF,aAAa,SAAS,iBAAc;mBAsDzB,2BACf,OAAO,yBACN;;;;EAcH,sBAAsB;;;;;;EAStB,0CAGI,WACG,eACF;IAAgB,SAAS;;;;;;EAuB9B,aAAa;;;;;SAYA,YAAY,OAAO,gBAAgB,QAAQ;;;;;;MASpD,WAAW;EAIf,sBAEE,kBAAkB,sBAAsB,qBAExC,QAAQ,mBAAmB,YAC3B,SAAS,uCACR,SAAS,wBAAwB;EAEpC,sBAEE,kBAAkB,sBAAsB,qBAExC,QAAQ,mBAAmB,YAC3B,SAAS,sCACR,SAAS;IAA0B,KAAK;IAAa,QAAQ;;EAEhE,sBAEE,kBAAkB,sBAAsB,qBAExC,QACI,UAAU,aAEV,qBACJ,SAAS,uCACR,SAAS,wBAAwB;EAEpC,sBAEE,kBAAkB,sBAAsB,qBAExC,QACI,UAAU,aAEV,qBACJ,SAAS,sCACR,SAAS;IAA0B,KAAK;IAAa,QAAQ;;EAEhE,sBAEE,kBAAkB,sBAAsB,qBAExC,QACI,UAAU,aAEV,qBACJ,SAAS,uCACR,SAAS,wBAAwB;EAEpC,sBAEE,kBAAkB,sBAAsB,qBAExC,QACI,UAAU,aAEV,qBACJ,SAAS,sCACR,SAAS;IAA0B,KAAK;IAAa,QAAQ;;;;;;;;;;;;;;;EAehE,sBAEE,kBAAkB,sBAAsB,qBAExC,QACI,eAAe,aAEf,qBACJ,SAAS,yCAEP,SAAS,wBAAwB,aACjC,SACE;IAEE,KAAK;IACL,QAAQ;;;;;;YAQN,kCAER,QAAQ,sBAEP;;;;;;UAUY;EACf;EACA;EACA"}
@@ -102,6 +102,10 @@ interface BaseLanguageModelCallOptions extends RunnableConfig, BaseLanguageModel
102
102
  * If not provided, the default stop tokens for the model will be used.
103
103
  */
104
104
  stop?: string[];
105
+ /**
106
+ * Overrides the model's configured `maxRetries` for this call only.
107
+ */
108
+ maxRetries?: number;
105
109
  }
106
110
  interface FunctionDefinition {
107
111
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.ts","names":[],"sources":["../../src/language_models/base.ts"],"mappings":";;;;;;;;;;;;;;;;cAgCa,0BAAuB,sBAAwB;cA4B/C,0BAAuB;;;;;;;;;;;;cAoBvB,sBAAmB;;;;;;iBAgGhB,aAAa,gBAAgB,QAAQ;UAgB3C;EACR;EACA,WAAW;;cAGA,uBAAkB,QAAA,aAG5B,2BAAsB;KAuBb;EACV;EACA;IAEE;UAEa;EACf;EACA,YAAY;EACZ;EACA,WAAW;;;;;uBAMS,cACpB,UACA,WACA,oBAAoB,iBAAiB,wBAE7B,SAAS,UAAU,WAAW,wBAC3B;;;;EAKX;EAEA,YAAY;EAEZ;EAEA,WAAW;MAEP;KAAoB;;EAOxB,YAAY,QAAQ;YASV,YAAY,aAAa;;;;;;;UAiBpB,gCACP,mBAAmB;;;;EAI3B,kBAAkB;EAElB,QAAQ;;UAGO;;;;;EAKf;;;;IAIE;MAAU;;;;;IAIV,SAAS;;;UAII,qCACP,gBAAgB;;;;;EAKxB;;UAGe;;;;;EAKf;;;;;;;;;;;EAYA,YAAY,0BAA0B;;;;;EAMtC;;UAGe;EACf;EACA,UAAU;;KAGA;EACV;;UAGe,gCAAgC;EAC/C,gBAAgB;EAChB,YAAY;;KAGF,yBACR,oCAEA;KAEQ,uBAAuB,sBAAsB;KAE7C,8BAA8B;EAEtC;EACA;EACA,aAAa;;EAEb;;;KAIQ,6BACV,WACA;;EAIA,QAAQ,eAAe,aAAa;EACpC;EACA;EACA,aAAa;;UAGE,2BAEf,iBACA,oBAAoB,+BAClB,sCACM,kBAAkB,wBAAwB,WAAW;MACzD;EAEJ,eACE,cAAc,4BACd,qBAAqB,QAAQ,cAC7B,YAAY,YACX,QAAQ;EAEX;EAEA;EAEA,aAAa,SAAS,iBAAiB;;;;EAMvC,sBAAsB;EAEtB,aAAa;;KAGH,sBA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aseLangChain = class extends Runnable {
138
138
  this.callbacks = params.callbacks;
139
139
  this.tags = params.tags ?? [];
140
140
  this.metadata = params.metadata ?? {};
141
- this._addVersion("@langchain/core", "1.2.7");
141
+ this._addVersion("@langchain/core", "1.2.8");
142
142
  }
143
143
  _addVersion(pkg, version) {
144
144
  const existing = this.metadata?.versions;
@@ -1 +1 @@
1
- {"version":3,"file":"base.js","names":[],"sources":["../../src/language_models/base.ts"],"sourcesContent":["import type { Tiktoken, TiktokenModel } from \"js-tiktoken/lite\";\nimport type { ZodV3Like, ZodV4Like } from \"../utils/types/zod.js\";\n\nimport { type BaseCache, InMemoryCache } from \"../caches/index.js\";\nimport {\n type BasePromptValueInterface,\n StringPromptValue,\n ChatPromptValue,\n} from \"../prompt_values.js\";\nimport {\n type BaseMessage,\n type BaseMessageLike,\n type MessageContent,\n} from \"../messages/base.js\";\nimport { coerceMessageLikeToMessage } from \"../messages/utils.js\";\nimport { type LLMResult } from \"../outputs.js\";\nimport { CallbackManager, Callbacks } from \"../callbacks/manager.js\";\nimport { AsyncCaller, AsyncCallerParams } from \"../utils/async_caller.js\";\nimport { encodingForModel } from \"../utils/tiktoken.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { JSONSchema } from \"../utils/json_schema.js\";\nimport {\n InferInteropZodOutput,\n InteropZodObject,\n InteropZodType,\n} from \"../utils/types/zod.js\";\nimport { ModelProfile } from \"./profile.js\";\nimport { type SerializableSchema } from \"../utils/standard_schema.js\";\n\n// https://www.npmjs.com/package/js-tiktoken\n\nexport const getModelNameForTiktoken = (modelName: string): TiktokenModel => {\n if (modelName.startsWith(\"gpt-5\")) {\n return \"gpt-5\" as TiktokenModel;\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-16k\")) {\n return \"gpt-3.5-turbo-16k\";\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-\")) {\n return \"gpt-3.5-turbo\";\n }\n\n if (modelName.startsWith(\"gpt-4-32k\")) {\n return \"gpt-4-32k\";\n }\n\n if (modelName.startsWith(\"gpt-4-\")) {\n return \"gpt-4\";\n }\n\n if (modelName.startsWith(\"gpt-4o\")) {\n return \"gpt-4o\";\n }\n\n return modelName as TiktokenModel;\n};\n\nexport const getEmbeddingContextSize = (modelName?: string): number => {\n switch (modelName) {\n case \"text-embedding-ada-002\":\n return 8191;\n default:\n return 2046;\n }\n};\n\n/**\n * Get the context window size (max input tokens) for a given model.\n *\n * Context window sizes are sourced from official model documentation:\n * - OpenAI: https://platform.openai.com/docs/models\n * - Anthropic: https://docs.anthropic.com/claude/docs/models-overview\n * - Google: https://ai.google.dev/gemini/docs/models/gemini\n *\n * @param modelName - The name of the model\n * @returns The context window size in tokens\n */\nexport const getModelContextSize = (modelName: string): number => {\n const normalizedName = getModelNameForTiktoken(modelName) as string;\n\n switch (normalizedName) {\n // GPT-5 series\n case \"gpt-5\":\n case \"gpt-5-turbo\":\n case \"gpt-5-turbo-preview\":\n return 400000;\n\n // GPT-4o series\n case \"gpt-4o\":\n case \"gpt-4o-mini\":\n case \"gpt-4o-2024-05-13\":\n case \"gpt-4o-2024-08-06\":\n return 128000;\n\n // GPT-4 Turbo series\n case \"gpt-4-turbo\":\n case \"gpt-4-turbo-preview\":\n case \"gpt-4-turbo-2024-04-09\":\n case \"gpt-4-0125-preview\":\n case \"gpt-4-1106-preview\":\n return 128000;\n\n // GPT-4 series\n case \"gpt-4-32k\":\n case \"gpt-4-32k-0314\":\n case \"gpt-4-32k-0613\":\n return 32768;\n case \"gpt-4\":\n case \"gpt-4-0314\":\n case \"gpt-4-0613\":\n return 8192;\n\n // GPT-3.5 Turbo series\n case \"gpt-3.5-turbo-16k\":\n case \"gpt-3.5-turbo-16k-0613\":\n return 16384;\n case \"gpt-3.5-turbo\":\n case \"gpt-3.5-turbo-0301\":\n case \"gpt-3.5-turbo-0613\":\n case \"gpt-3.5-turbo-1106\":\n case \"gpt-3.5-turbo-0125\":\n return 4096;\n\n // Legacy GPT-3 models\n case \"text-davinci-003\":\n case \"text-davinci-002\":\n return 4097;\n case \"text-davinci-001\":\n return 2049;\n case \"text-curie-001\":\n case \"text-babbage-001\":\n case \"text-ada-001\":\n return 2048;\n\n // Code models\n case \"code-davinci-002\":\n case \"code-davinci-001\":\n return 8000;\n case \"code-cushman-001\":\n return 2048;\n\n // Claude models (Anthropic)\n case \"claude-3-5-sonnet-20241022\":\n case \"claude-3-5-sonnet-20240620\":\n case \"claude-3-opus-20240229\":\n case \"claude-3-sonnet-20240229\":\n case \"claude-3-haiku-20240307\":\n case \"claude-2.1\":\n return 200000;\n case \"claude-2.0\":\n case \"claude-instant-1.2\":\n return 100000;\n\n // Gemini models (Google)\n case \"gemini-1.5-pro\":\n case \"gemini-1.5-pro-latest\":\n case \"gemini-1.5-flash\":\n case \"gemini-1.5-flash-latest\":\n return 1000000; // 1M tokens\n case \"gemini-pro\":\n case \"gemini-pro-vision\":\n return 32768;\n\n default:\n return 4097;\n }\n};\n\n/**\n * Whether or not the input matches the OpenAI tool definition.\n * @param {unknown} tool The input to check.\n * @returns {boolean} Whether the input is an OpenAI tool definition.\n */\nexport function isOpenAITool(tool: unknown): tool is ToolDefinition {\n if (typeof tool !== \"object\" || !tool) return false;\n if (\n \"type\" in tool &&\n tool.type === \"function\" &&\n \"function\" in tool &&\n typeof tool.function === \"object\" &&\n tool.function &&\n \"name\" in tool.function &&\n \"parameters\" in tool.function\n ) {\n return true;\n }\n return false;\n}\n\ninterface CalculateMaxTokenProps {\n prompt: string;\n modelName: TiktokenModel;\n}\n\nexport const calculateMaxTokens = async ({\n prompt,\n modelName,\n}: CalculateMaxTokenProps) => {\n let numTokens;\n\n try {\n numTokens = (\n await encodingForModel(getModelNameForTiktoken(modelName))\n ).encode(prompt).length;\n } catch {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\"\n );\n\n // fallback to approximate calculation if tiktoken is not available\n // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#\n numTokens = Math.ceil(prompt.length / 4);\n }\n\n const maxTokens = getModelContextSize(modelName);\n return maxTokens - numTokens;\n};\n\nconst getVerbosity = () => false;\n\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n} & Record<string, any>;\n\nexport interface BaseLangChainParams {\n verbose?: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Base class for language models, chains, tools.\n */\nexport abstract class BaseLangChain<\n RunInput,\n RunOutput,\n CallOptions extends RunnableConfig = RunnableConfig,\n>\n extends Runnable<RunInput, RunOutput, CallOptions>\n implements BaseLangChainParams\n{\n /**\n * Whether to print out response text.\n */\n verbose: boolean;\n\n callbacks?: Callbacks;\n\n tags?: string[];\n\n metadata?: Record<string, unknown>;\n\n get lc_attributes(): { [key: string]: undefined } | undefined {\n return {\n callbacks: undefined,\n verbose: undefined,\n };\n }\n\n constructor(params: BaseLangChainParams) {\n super(params);\n this.verbose = params.verbose ?? getVerbosity();\n this.callbacks = params.callbacks;\n this.tags = params.tags ?? [];\n this.metadata = params.metadata ?? {};\n this._addVersion(\"@langchain/core\", __PKG_VERSION__);\n }\n\n protected _addVersion(pkg: string, version: string) {\n const existing = this.metadata?.versions;\n this.metadata = {\n ...this.metadata,\n versions: {\n ...(typeof existing === \"object\" && existing !== null ? existing : {}),\n [pkg]: version,\n },\n };\n }\n}\n\n/**\n * Base interface for language model parameters.\n * A subclass of {@link BaseLanguageModel} should have a constructor that\n * takes in a parameter that extends this interface.\n */\nexport interface BaseLanguageModelParams\n extends AsyncCallerParams, BaseLangChainParams {\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n\n cache?: BaseCache | boolean;\n}\n\nexport interface BaseLanguageModelTracingCallOptions {\n /**\n * Describes the format of structured outputs.\n * This should be provided if an output is considered to be structured\n */\n ls_structured_output_format?: {\n /**\n * An object containing the method used for structured output (e.g., \"jsonMode\").\n */\n kwargs: { method: string };\n /**\n * The JSON schema describing the expected output structure.\n */\n schema?: JSONSchema;\n };\n}\n\nexport interface BaseLanguageModelCallOptions\n extends RunnableConfig, BaseLanguageModelTracingCallOptions {\n /**\n * Stop tokens to use for this call.\n * If not provided, the default stop tokens for the model will be used.\n */\n stop?: string[];\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for\n * examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown> | JSONSchema;\n\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallOption = {\n name: string;\n};\n\nexport interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {\n function_call?: FunctionCallOption;\n functions?: FunctionDefinition[];\n}\n\nexport type BaseLanguageModelInput =\n | BasePromptValueInterface\n | string\n | BaseMessageLike[];\n\nexport type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;\n\nexport type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =\n {\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\" | \"jsonSchema\" | string;\n includeRaw?: IncludeRaw;\n /** Whether to use strict mode. Currently only supported by OpenAI models. */\n strict?: boolean;\n };\n\n/** @deprecated Use StructuredOutputMethodOptions instead */\nexport type StructuredOutputMethodParams<\n RunOutput,\n IncludeRaw extends boolean = false,\n> = {\n /** @deprecated Pass schema in as the first argument */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: InteropZodType<RunOutput> | Record<string, any>;\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\";\n includeRaw?: IncludeRaw;\n};\n\nexport interface BaseLanguageModelInterface<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {\n get callKeys(): string[];\n\n generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | Partial<CallOptions>,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n _modelType(): string;\n\n _llmType(): string;\n\n getNumTokens(content: MessageContent): Promise<number>;\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any>;\n\n serialize(): SerializedLLM;\n}\n\nexport type LanguageModelOutput = BaseMessage | string;\n\nexport type LanguageModelLike = RunnableInterface<\n BaseLanguageModelInput,\n LanguageModelOutput\n>;\n\n/**\n * Base class for language models.\n */\nexport abstract class BaseLanguageModel<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n>\n extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions>\n implements\n BaseLanguageModelParams,\n BaseLanguageModelInterface<RunOutput, CallOptions>\n{\n /**\n * Keys that the language model accepts as call options.\n */\n get callKeys(): string[] {\n return [\"stop\", \"timeout\", \"signal\", \"tags\", \"metadata\", \"callbacks\"];\n }\n\n /**\n * The async caller should be used by subclasses to make any async calls,\n * which will thus benefit from the concurrency and retry logic.\n */\n caller: AsyncCaller;\n\n cache?: BaseCache;\n\n constructor({\n callbacks,\n callbackManager,\n ...params\n }: BaseLanguageModelParams) {\n const { cache, ...rest } = params;\n super({\n callbacks: callbacks ?? callbackManager,\n ...rest,\n });\n if (typeof cache === \"object\") {\n this.cache = cache;\n } else if (cache) {\n this.cache = InMemoryCache.global();\n } else {\n this.cache = undefined;\n }\n this.caller = new AsyncCaller(params ?? {});\n }\n\n abstract generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | CallOptions,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n abstract _modelType(): string;\n\n abstract _llmType(): string;\n\n private _encoding?: Tiktoken;\n\n /**\n * Get the number of tokens in the content.\n * @param content The content to get the number of tokens for.\n * @returns The number of tokens in the content.\n */\n async getNumTokens(content: MessageContent) {\n // Extract text content from MessageContent\n let textContent: string;\n if (typeof content === \"string\") {\n textContent = content;\n } else {\n /**\n * Content is an array of ContentBlock\n *\n * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.\n * We need to find a better way to do this.\n * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116\n */\n textContent = content\n .map((item) => {\n if (typeof item === \"string\") return item;\n if (item.type === \"text\" && \"text\" in item) return item.text;\n return \"\";\n })\n .join(\"\");\n }\n\n // fallback to approximate calculation if tiktoken is not available\n let numTokens = Math.ceil(textContent.length / 4);\n\n if (!this._encoding) {\n try {\n this._encoding = await encodingForModel(\n \"modelName\" in this\n ? getModelNameForTiktoken(this.modelName as string)\n : \"gpt2\"\n );\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n if (this._encoding) {\n try {\n numTokens = this._encoding.encode(textContent).length;\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n return numTokens;\n }\n\n protected static _convertInputToPromptValue(\n input: BaseLanguageModelInput\n ): BasePromptValueInterface {\n if (typeof input === \"string\") {\n return new StringPromptValue(input);\n } else if (Array.isArray(input)) {\n return new ChatPromptValue(input.map(coerceMessageLikeToMessage));\n } else {\n return input;\n }\n }\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any> {\n return {};\n }\n\n /**\n * Create a unique cache key for a specific call to a specific language model.\n * @param callOptions Call options for the model\n * @returns A unique cache key.\n */\n _getSerializedCacheKeyParametersForCall(\n // TODO: Fix when we remove the RunnableLambda backwards compatibility shim.\n {\n config,\n ...callOptions\n }: CallOptions & { config?: RunnableConfig }\n ): string {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const params: Record<string, any> = {\n ...this._identifyingParams(),\n ...callOptions,\n _type: this._llmType(),\n _model: this._modelType(),\n };\n const filteredEntries = Object.entries(params).filter(\n ([_, value]) => value !== undefined\n );\n const serializedEntries = filteredEntries\n .map(([key, value]) => `${key}:${JSON.stringify(value)}`)\n .sort()\n .join(\",\");\n return serializedEntries;\n }\n\n /**\n * @deprecated\n * Return a json-like object representing this LLM.\n */\n serialize(): SerializedLLM {\n return {\n ...this._identifyingParams(),\n _type: this._llmType(),\n _model: this._modelType(),\n };\n }\n\n /**\n * @deprecated\n * Load an LLM from a json-like object describing it.\n */\n static async deserialize(_data: SerializedLLM): Promise<BaseLanguageModel> {\n throw new Error(\"Use .toJSON() instead\");\n }\n\n /**\n * Return profiling information for the model.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n */\n get profile(): ModelProfile {\n return {};\n }\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n /**\n * Model wrapper that returns outputs formatted to match the given schema.\n *\n * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.\n * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.\n *\n * @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.\n * If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.\n * @param {string} name The name of the function to call.\n * @param {\"functionCalling\" | \"jsonMode\"} [method=functionCalling] The method to use for getting the structured output. Defaults to \"functionCalling\".\n * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.\n * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.\n */\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | InteropZodType<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n {\n raw: BaseMessage;\n parsed: RunOutput;\n }\n >;\n\n /**\n * Filter out large/inappropriate fields from invocation params for tracing metadata.\n * Removes fields like tools, functions, messages, response_format that can be large.\n */\n protected _filterInvocationParamsForTracing(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ): Record<string, any> {\n const { tools, functions, messages, response_format, ...rest } = params;\n return rest;\n }\n}\n\n/**\n * Shared interface for token usage\n * return type from LLM calls.\n */\nexport interface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgCA,MAAa,2BAA2B,cAAqC;CAC3E,IAAI,UAAU,WAAW,OAAO,GAC9B,OAAO;CAGT,IAAI,UAAU,WAAW,mBAAmB,GAC1C,OAAO;CAGT,IAAI,UAAU,WAAW,gBAAgB,GACvC,OAAO;CAGT,IAAI,UAAU,WAAW,WAAW,GAClC,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,OAAO;AACT;AAEA,MAAa,2BAA2B,cAA+B;CACrE,QAAQ,WAAR;EACE,KAAK,0BACH,OAAO;EACT,SACE,OAAO;CACX;AACF;;;;;;;;;;;;AAaA,MAAa,uBAAuB,cAA8B;CAGhE,QAFuB,wBAAwB,SAE1B,GAArB;EAEE,KAA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,QAEqB;EACrB,MAAM,EAAE,OAAO,WAAW,UAAU,iBAAiB,GAAG,SAAS;EACjE,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"base.js","names":[],"sources":["../../src/language_models/base.ts"],"sourcesContent":["import type { Tiktoken, TiktokenModel } from \"js-tiktoken/lite\";\nimport type { ZodV3Like, ZodV4Like } from \"../utils/types/zod.js\";\n\nimport { type BaseCache, InMemoryCache } from \"../caches/index.js\";\nimport {\n type BasePromptValueInterface,\n StringPromptValue,\n ChatPromptValue,\n} from \"../prompt_values.js\";\nimport {\n type BaseMessage,\n type BaseMessageLike,\n type MessageContent,\n} from \"../messages/base.js\";\nimport { coerceMessageLikeToMessage } from \"../messages/utils.js\";\nimport { type LLMResult } from \"../outputs.js\";\nimport { CallbackManager, Callbacks } from \"../callbacks/manager.js\";\nimport { AsyncCaller, AsyncCallerParams } from \"../utils/async_caller.js\";\nimport { encodingForModel } from \"../utils/tiktoken.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { JSONSchema } from \"../utils/json_schema.js\";\nimport {\n InferInteropZodOutput,\n InteropZodObject,\n InteropZodType,\n} from \"../utils/types/zod.js\";\nimport { ModelProfile } from \"./profile.js\";\nimport { type SerializableSchema } from \"../utils/standard_schema.js\";\n\n// https://www.npmjs.com/package/js-tiktoken\n\nexport const getModelNameForTiktoken = (modelName: string): TiktokenModel => {\n if (modelName.startsWith(\"gpt-5\")) {\n return \"gpt-5\" as TiktokenModel;\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-16k\")) {\n return \"gpt-3.5-turbo-16k\";\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-\")) {\n return \"gpt-3.5-turbo\";\n }\n\n if (modelName.startsWith(\"gpt-4-32k\")) {\n return \"gpt-4-32k\";\n }\n\n if (modelName.startsWith(\"gpt-4-\")) {\n return \"gpt-4\";\n }\n\n if (modelName.startsWith(\"gpt-4o\")) {\n return \"gpt-4o\";\n }\n\n return modelName as TiktokenModel;\n};\n\nexport const getEmbeddingContextSize = (modelName?: string): number => {\n switch (modelName) {\n case \"text-embedding-ada-002\":\n return 8191;\n default:\n return 2046;\n }\n};\n\n/**\n * Get the context window size (max input tokens) for a given model.\n *\n * Context window sizes are sourced from official model documentation:\n * - OpenAI: https://platform.openai.com/docs/models\n * - Anthropic: https://docs.anthropic.com/claude/docs/models-overview\n * - Google: https://ai.google.dev/gemini/docs/models/gemini\n *\n * @param modelName - The name of the model\n * @returns The context window size in tokens\n */\nexport const getModelContextSize = (modelName: string): number => {\n const normalizedName = getModelNameForTiktoken(modelName) as string;\n\n switch (normalizedName) {\n // GPT-5 series\n case \"gpt-5\":\n case \"gpt-5-turbo\":\n case \"gpt-5-turbo-preview\":\n return 400000;\n\n // GPT-4o series\n case \"gpt-4o\":\n case \"gpt-4o-mini\":\n case \"gpt-4o-2024-05-13\":\n case \"gpt-4o-2024-08-06\":\n return 128000;\n\n // GPT-4 Turbo series\n case \"gpt-4-turbo\":\n case \"gpt-4-turbo-preview\":\n case \"gpt-4-turbo-2024-04-09\":\n case \"gpt-4-0125-preview\":\n case \"gpt-4-1106-preview\":\n return 128000;\n\n // GPT-4 series\n case \"gpt-4-32k\":\n case \"gpt-4-32k-0314\":\n case \"gpt-4-32k-0613\":\n return 32768;\n case \"gpt-4\":\n case \"gpt-4-0314\":\n case \"gpt-4-0613\":\n return 8192;\n\n // GPT-3.5 Turbo series\n case \"gpt-3.5-turbo-16k\":\n case \"gpt-3.5-turbo-16k-0613\":\n return 16384;\n case \"gpt-3.5-turbo\":\n case \"gpt-3.5-turbo-0301\":\n case \"gpt-3.5-turbo-0613\":\n case \"gpt-3.5-turbo-1106\":\n case \"gpt-3.5-turbo-0125\":\n return 4096;\n\n // Legacy GPT-3 models\n case \"text-davinci-003\":\n case \"text-davinci-002\":\n return 4097;\n case \"text-davinci-001\":\n return 2049;\n case \"text-curie-001\":\n case \"text-babbage-001\":\n case \"text-ada-001\":\n return 2048;\n\n // Code models\n case \"code-davinci-002\":\n case \"code-davinci-001\":\n return 8000;\n case \"code-cushman-001\":\n return 2048;\n\n // Claude models (Anthropic)\n case \"claude-3-5-sonnet-20241022\":\n case \"claude-3-5-sonnet-20240620\":\n case \"claude-3-opus-20240229\":\n case \"claude-3-sonnet-20240229\":\n case \"claude-3-haiku-20240307\":\n case \"claude-2.1\":\n return 200000;\n case \"claude-2.0\":\n case \"claude-instant-1.2\":\n return 100000;\n\n // Gemini models (Google)\n case \"gemini-1.5-pro\":\n case \"gemini-1.5-pro-latest\":\n case \"gemini-1.5-flash\":\n case \"gemini-1.5-flash-latest\":\n return 1000000; // 1M tokens\n case \"gemini-pro\":\n case \"gemini-pro-vision\":\n return 32768;\n\n default:\n return 4097;\n }\n};\n\n/**\n * Whether or not the input matches the OpenAI tool definition.\n * @param {unknown} tool The input to check.\n * @returns {boolean} Whether the input is an OpenAI tool definition.\n */\nexport function isOpenAITool(tool: unknown): tool is ToolDefinition {\n if (typeof tool !== \"object\" || !tool) return false;\n if (\n \"type\" in tool &&\n tool.type === \"function\" &&\n \"function\" in tool &&\n typeof tool.function === \"object\" &&\n tool.function &&\n \"name\" in tool.function &&\n \"parameters\" in tool.function\n ) {\n return true;\n }\n return false;\n}\n\ninterface CalculateMaxTokenProps {\n prompt: string;\n modelName: TiktokenModel;\n}\n\nexport const calculateMaxTokens = async ({\n prompt,\n modelName,\n}: CalculateMaxTokenProps) => {\n let numTokens;\n\n try {\n numTokens = (\n await encodingForModel(getModelNameForTiktoken(modelName))\n ).encode(prompt).length;\n } catch {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\"\n );\n\n // fallback to approximate calculation if tiktoken is not available\n // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#\n numTokens = Math.ceil(prompt.length / 4);\n }\n\n const maxTokens = getModelContextSize(modelName);\n return maxTokens - numTokens;\n};\n\nconst getVerbosity = () => false;\n\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n} & Record<string, any>;\n\nexport interface BaseLangChainParams {\n verbose?: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Base class for language models, chains, tools.\n */\nexport abstract class BaseLangChain<\n RunInput,\n RunOutput,\n CallOptions extends RunnableConfig = RunnableConfig,\n>\n extends Runnable<RunInput, RunOutput, CallOptions>\n implements BaseLangChainParams\n{\n /**\n * Whether to print out response text.\n */\n verbose: boolean;\n\n callbacks?: Callbacks;\n\n tags?: string[];\n\n metadata?: Record<string, unknown>;\n\n get lc_attributes(): { [key: string]: undefined } | undefined {\n return {\n callbacks: undefined,\n verbose: undefined,\n };\n }\n\n constructor(params: BaseLangChainParams) {\n super(params);\n this.verbose = params.verbose ?? getVerbosity();\n this.callbacks = params.callbacks;\n this.tags = params.tags ?? [];\n this.metadata = params.metadata ?? {};\n this._addVersion(\"@langchain/core\", __PKG_VERSION__);\n }\n\n protected _addVersion(pkg: string, version: string) {\n const existing = this.metadata?.versions;\n this.metadata = {\n ...this.metadata,\n versions: {\n ...(typeof existing === \"object\" && existing !== null ? existing : {}),\n [pkg]: version,\n },\n };\n }\n}\n\n/**\n * Base interface for language model parameters.\n * A subclass of {@link BaseLanguageModel} should have a constructor that\n * takes in a parameter that extends this interface.\n */\nexport interface BaseLanguageModelParams\n extends AsyncCallerParams, BaseLangChainParams {\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n\n cache?: BaseCache | boolean;\n}\n\nexport interface BaseLanguageModelTracingCallOptions {\n /**\n * Describes the format of structured outputs.\n * This should be provided if an output is considered to be structured\n */\n ls_structured_output_format?: {\n /**\n * An object containing the method used for structured output (e.g., \"jsonMode\").\n */\n kwargs: { method: string };\n /**\n * The JSON schema describing the expected output structure.\n */\n schema?: JSONSchema;\n };\n}\n\nexport interface BaseLanguageModelCallOptions\n extends RunnableConfig, BaseLanguageModelTracingCallOptions {\n /**\n * Stop tokens to use for this call.\n * If not provided, the default stop tokens for the model will be used.\n */\n stop?: string[];\n /**\n * Overrides the model's configured `maxRetries` for this call only.\n */\n maxRetries?: number;\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for\n * examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown> | JSONSchema;\n\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallOption = {\n name: string;\n};\n\nexport interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {\n function_call?: FunctionCallOption;\n functions?: FunctionDefinition[];\n}\n\nexport type BaseLanguageModelInput =\n | BasePromptValueInterface\n | string\n | BaseMessageLike[];\n\nexport type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;\n\nexport type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =\n {\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\" | \"jsonSchema\" | string;\n includeRaw?: IncludeRaw;\n /** Whether to use strict mode. Currently only supported by OpenAI models. */\n strict?: boolean;\n };\n\n/** @deprecated Use StructuredOutputMethodOptions instead */\nexport type StructuredOutputMethodParams<\n RunOutput,\n IncludeRaw extends boolean = false,\n> = {\n /** @deprecated Pass schema in as the first argument */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: InteropZodType<RunOutput> | Record<string, any>;\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\";\n includeRaw?: IncludeRaw;\n};\n\nexport interface BaseLanguageModelInterface<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {\n get callKeys(): string[];\n\n generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | Partial<CallOptions>,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n _modelType(): string;\n\n _llmType(): string;\n\n getNumTokens(content: MessageContent): Promise<number>;\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any>;\n\n serialize(): SerializedLLM;\n}\n\nexport type LanguageModelOutput = BaseMessage | string;\n\nexport type LanguageModelLike = RunnableInterface<\n BaseLanguageModelInput,\n LanguageModelOutput\n>;\n\n/**\n * Base class for language models.\n */\nexport abstract class BaseLanguageModel<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n>\n extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions>\n implements\n BaseLanguageModelParams,\n BaseLanguageModelInterface<RunOutput, CallOptions>\n{\n /**\n * Keys that the language model accepts as call options.\n */\n get callKeys(): string[] {\n return [\"stop\", \"timeout\", \"signal\", \"tags\", \"metadata\", \"callbacks\"];\n }\n\n /**\n * The async caller should be used by subclasses to make any async calls,\n * which will thus benefit from the concurrency and retry logic.\n */\n caller: AsyncCaller;\n\n cache?: BaseCache;\n\n constructor({\n callbacks,\n callbackManager,\n ...params\n }: BaseLanguageModelParams) {\n const { cache, ...rest } = params;\n super({\n callbacks: callbacks ?? callbackManager,\n ...rest,\n });\n if (typeof cache === \"object\") {\n this.cache = cache;\n } else if (cache) {\n this.cache = InMemoryCache.global();\n } else {\n this.cache = undefined;\n }\n this.caller = new AsyncCaller(params ?? {});\n }\n\n abstract generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | CallOptions,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n abstract _modelType(): string;\n\n abstract _llmType(): string;\n\n private _encoding?: Tiktoken;\n\n /**\n * Get the number of tokens in the content.\n * @param content The content to get the number of tokens for.\n * @returns The number of tokens in the content.\n */\n async getNumTokens(content: MessageContent) {\n // Extract text content from MessageContent\n let textContent: string;\n if (typeof content === \"string\") {\n textContent = content;\n } else {\n /**\n * Content is an array of ContentBlock\n *\n * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.\n * We need to find a better way to do this.\n * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116\n */\n textContent = content\n .map((item) => {\n if (typeof item === \"string\") return item;\n if (item.type === \"text\" && \"text\" in item) return item.text;\n return \"\";\n })\n .join(\"\");\n }\n\n // fallback to approximate calculation if tiktoken is not available\n let numTokens = Math.ceil(textContent.length / 4);\n\n if (!this._encoding) {\n try {\n this._encoding = await encodingForModel(\n \"modelName\" in this\n ? getModelNameForTiktoken(this.modelName as string)\n : \"gpt2\"\n );\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n if (this._encoding) {\n try {\n numTokens = this._encoding.encode(textContent).length;\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n return numTokens;\n }\n\n protected static _convertInputToPromptValue(\n input: BaseLanguageModelInput\n ): BasePromptValueInterface {\n if (typeof input === \"string\") {\n return new StringPromptValue(input);\n } else if (Array.isArray(input)) {\n return new ChatPromptValue(input.map(coerceMessageLikeToMessage));\n } else {\n return input;\n }\n }\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any> {\n return {};\n }\n\n /**\n * Create a unique cache key for a specific call to a specific language model.\n * @param callOptions Call options for the model\n * @returns A unique cache key.\n */\n _getSerializedCacheKeyParametersForCall(\n // TODO: Fix when we remove the RunnableLambda backwards compatibility shim.\n {\n config,\n ...callOptions\n }: CallOptions & { config?: RunnableConfig }\n ): string {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const params: Record<string, any> = {\n ...this._identifyingParams(),\n ...callOptions,\n _type: this._llmType(),\n _model: this._modelType(),\n };\n const filteredEntries = Object.entries(params).filter(\n ([_, value]) => value !== undefined\n );\n const serializedEntries = filteredEntries\n .map(([key, value]) => `${key}:${JSON.stringify(value)}`)\n .sort()\n .join(\",\");\n return serializedEntries;\n }\n\n /**\n * @deprecated\n * Return a json-like object representing this LLM.\n */\n serialize(): SerializedLLM {\n return {\n ...this._identifyingParams(),\n _type: this._llmType(),\n _model: this._modelType(),\n };\n }\n\n /**\n * @deprecated\n * Load an LLM from a json-like object describing it.\n */\n static async deserialize(_data: SerializedLLM): Promise<BaseLanguageModel> {\n throw new Error(\"Use .toJSON() instead\");\n }\n\n /**\n * Return profiling information for the model.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n */\n get profile(): ModelProfile {\n return {};\n }\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n /**\n * Model wrapper that returns outputs formatted to match the given schema.\n *\n * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.\n * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.\n *\n * @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.\n * If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.\n * @param {string} name The name of the function to call.\n * @param {\"functionCalling\" | \"jsonMode\"} [method=functionCalling] The method to use for getting the structured output. Defaults to \"functionCalling\".\n * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.\n * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.\n */\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | InteropZodType<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n {\n raw: BaseMessage;\n parsed: RunOutput;\n }\n >;\n\n /**\n * Filter out large/inappropriate fields from invocation params for tracing metadata.\n * Removes fields like tools, functions, messages, response_format that can be large.\n */\n protected _filterInvocationParamsForTracing(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ): Record<string, any> {\n const { tools, functions, messages, response_format, ...rest } = params;\n return rest;\n }\n}\n\n/**\n * Shared interface for token usage\n * return type from LLM calls.\n */\nexport interface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgCA,MAAa,2BAA2B,cAAqC;CAC3E,IAAI,UAAU,WAAW,OAAO,GAC9B,OAAO;CAGT,IAAI,UAAU,WAAW,mBAAmB,GAC1C,OAAO;CAGT,IAAI,UAAU,WAAW,gBAAgB,GACvC,OAAO;CAGT,IAAI,UAAU,WAAW,WAAW,GAClC,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,OAAO;AACT;AAEA,MAAa,2BAA2B,cAA+B;CACrE,QAAQ,WAAR;EACE,KAAK,0BACH,OAAO;EACT,SACE,OAAO;CACX;AACF;;;;;;;;;;;;AAaA,MAAa,uBAAuB,cAA8B;CAGhE,QAFuB,wBAAwB,SAE1B,GAArB;EAEE,KAAK;EACL,KAAK;EACL,KAAK,uBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,qBAC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F"}
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
3
+ const require_errors_index = require("../errors/index.cjs");
3
4
  const require_signal = require("./signal.cjs");
4
5
  const require_index = require("./p-retry/index.cjs");
5
6
  let p_queue = require("p-queue");
@@ -19,7 +20,8 @@ const STATUS_NO_RETRY = [
19
20
  405,
20
21
  406,
21
22
  407,
22
- 409
23
+ 409,
24
+ 413
23
25
  ];
24
26
  const RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 6e4;
25
27
  const QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [
@@ -132,10 +134,11 @@ function classifyRateLimitError(error) {
132
134
  */
133
135
  const defaultFailedAttemptHandler = (error) => {
134
136
  if (typeof error !== "object" || error === null) return;
135
- if ("message" in error && typeof error.message === "string" && (error.message.startsWith("Cancel") || error.message.startsWith("AbortError")) || "name" in error && typeof error.name === "string" && error.name === "AbortError") throw error;
137
+ if (require_errors_index.getRetryable(error) === false) throw error;
138
+ if ("message" in error && typeof error.message === "string" && (error.message.startsWith("Cancel") || error.message.startsWith("AbortError")) || "name" in error && typeof error.name === "string" && error.name === "AbortError") throw require_errors_index.stampRetryable(error, false);
136
139
  if ("code" in error && typeof error.code === "string" && error.code === "ECONNABORTED") throw error;
137
140
  const status = getResponseStatus(error) ?? getDirectStatus(error);
138
- if (status && STATUS_NO_RETRY.includes(+status)) throw error;
141
+ if (status && STATUS_NO_RETRY.includes(+status)) throw require_errors_index.stampRetryable(error, false);
139
142
  if (getErrorCode(error) === "insufficient_quota") {
140
143
  const err = coerceError(error, getErrorMessage(error) ?? "Insufficient quota");
141
144
  err.name = "InsufficientQuotaError";
@@ -143,18 +146,19 @@ const defaultFailedAttemptHandler = (error) => {
143
146
  action: "stop",
144
147
  reason: "insufficient_quota"
145
148
  });
146
- throw err;
149
+ throw require_errors_index.stampRetryable(err, false);
147
150
  }
148
151
  const rateLimitClassification = classifyRateLimitError(error);
149
152
  if (rateLimitClassification) {
150
153
  if (rateLimitClassification.action === "wait") {
151
154
  setRateLimitMetadata(error, rateLimitClassification);
155
+ require_errors_index.stampRetryable(error, true);
152
156
  return;
153
157
  }
154
158
  const err = coerceError(error, getErrorMessage(error) ?? "Rate limit exceeded");
155
159
  if (err.name === "Error") err.name = rateLimitClassification.action === "stop" ? "RateLimitQuotaExhaustedError" : "RateLimitCapacityError";
156
160
  setRateLimitMetadata(err, rateLimitClassification);
157
- throw err;
161
+ throw require_errors_index.stampRetryable(err, rateLimitClassification.action !== "stop");
158
162
  }
159
163
  };
160
164
  /**
@@ -183,19 +187,23 @@ var AsyncCaller = class {
183
187
  this.queue = new PQueue({ concurrency: this.maxConcurrency });
184
188
  }
185
189
  async call(callable, ...args) {
190
+ return this.callWithRetries(this.maxRetries, callable, args);
191
+ }
192
+ callWithRetries(retries, callable, args) {
186
193
  return this.queue.add(() => require_index.default(() => callable(...args).catch((error) => {
187
194
  if (error instanceof Error) throw error;
188
195
  else throw new Error(error);
189
196
  }), {
190
197
  onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),
191
- retries: this.maxRetries,
198
+ retries,
192
199
  randomize: true
193
200
  }), { throwOnTimeout: true });
194
201
  }
195
202
  callWithOptions(options, callable, ...args) {
203
+ const retries = options.maxRetries ?? this.maxRetries;
196
204
  if (options.signal) {
197
205
  let listener;
198
- return Promise.race([this.call(callable, ...args), new Promise((_, reject) => {
206
+ return Promise.race([this.callWithRetries(retries, callable, args), new Promise((_, reject) => {
199
207
  listener = () => {
200
208
  reject(require_signal.getAbortSignalError(options.signal));
201
209
  };
@@ -204,7 +212,7 @@ var AsyncCaller = class {
204
212
  if (options.signal && listener) options.signal.removeEventListener("abort", listener);
205
213
  });
206
214
  }
207
- return this.call(callable, ...args);
215
+ return this.callWithRetries(retries, callable, args);
208
216
  }
209
217
  fetch(...args) {
210
218
  return this.call(() => fetch(...args).then((res) => res.ok ? res : Promise.reject(res)));
@@ -1 +1 @@
1
- {"version":3,"file":"async_caller.cjs","names":["PQueueMod","pRetry","getAbortSignalError"],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n throw err;\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n throw err;\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries: this.maxRetries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.call<A, T>(callable, ...args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.call<A, T>(callable, ...args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;AAKA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;CAC7D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WAAW,WAC/B,MAAM,SAAS,SACf,KAAA;AACN;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAGF,IAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAC/C,OAAO,MAAM;CAGf,IAAI,gBAAgB,SAAS,OAAO,MAAM,eAAe,UACvD,OAAO,MAAM;AAIjB;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,aAAa,SACb,OAAO,MAAM,YAAY,WACvB,MAAM,UACN,KAAA;AACN;AAEA,SAAS,aAAa,OAAoC;CACxD,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAGF,IAAI,UAAU,SAAS,OAAO,MAAM,SAAS,UAC3C,OAAO,MAAM;CAGf,OAAO,WAAW,SAChB,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,KAAK;EAEzB,CAAC,GACH;GACE,kBAAkB,EAAE,YAAY,KAAK,kBAAkB,KAAK;GAC5D,SAAS,KAAK;GACd,WAAW;EAGb,CACF,GACF,EAAE,gBAAgB,KAAK,CACzB;CACF;CAGA,gBACE,SACA,UACA,GAAG,MAC8B;EAGjC,IAAI,QAAQ,QAAQ;GAClB,IAAI;GACJ,OAAO,QAAQ,KAAK,CAClB,KAAK,KAAW,UAAU,GAAG,IAAI,GACjC,IAAI,SAAgB,GAAG,WAAW;IAChC,iBAAiB;KACf,OAAOC,eAAAA,oBAAoB,QAAQ,MAAM,CAAC;IAC5C;IACA,QAAQ,QAAQ,iBAAiB,SAAS,UAAU,EAAE,MAAM,KAAK,CAAC;GACpE,CAAC,CACH,CAAC,CAAC,CAAC,cAAc;IACf,IAAI,QAAQ,UAAU,UACpB,QAAQ,OAAO,oBAAoB,SAAS,QAAQ;GAExD,CAAC;EACH;EACA,OAAO,KAAK,KAAW,UAAU,GAAG,IAAI;CAC1C;CAEA,MAAM,GAAG,MAA0D;EACjE,OAAO,KAAK,WACV,MAAM,GAAG,IAAI,CAAC,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,GAAG,CAAE,CACnE;CACF;AACF"}
1
+ {"version":3,"file":"async_caller.cjs","names":["getRetryable","stampRetryable","PQueueMod","pRetry","getAbortSignalError"],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getRetryable, stampRetryable } from \"../errors/index.js\";\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n 413, // Payload Too Large\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n // Honor a verdict already reached inside the callable, e.g. by a provider.\n if (getRetryable(error) === false) {\n throw error;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n // Deliberate cancellation, not a failure worth another attempt.\n throw stampRetryable(error, false);\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n // Deterministic client error; retrying it unchanged fails identically.\n throw stampRetryable(error, false);\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n // Exhausted quota needs an account action, not another attempt.\n throw stampRetryable(err, false);\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n stampRetryable(error, true);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n // Only \"stop\" is exhausted quota; \"capacity\" can still succeed later.\n throw stampRetryable(err, rateLimitClassification.action !== \"stop\");\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n maxRetries?: number;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.callWithRetries(this.maxRetries, callable, args);\n }\n\n private callWithRetries<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n A extends any[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n T extends (...args: A) => Promise<any>,\n >(\n retries: AsyncCallerParams[\"maxRetries\"],\n callable: T,\n args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n const retries = options.maxRetries ?? this.maxRetries;\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.callWithRetries<A, T>(retries, callable, args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.callWithRetries<A, T>(retries, callable, args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAMA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;CAC7D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WAAW,WAC/B,MAAM,SAAS,SACf,KAAA;AACN;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAGF,IAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAC/C,OAAO,MAAM;CAGf,IAAI,gBAAgB,SAAS,OAAO,MAAM,eAAe,UACvD,OAAO,MAAM;AAIjB;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,aAAa,SACb,OAAO,MAAM,YAAY,WACvB,MAAM,UACN,KAAA;AACN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CAAE,CACnE;CACF;AACF"}
@@ -28,6 +28,7 @@ interface AsyncCallerParams {
28
28
  }
29
29
  interface AsyncCallerCallOptions {
30
30
  signal?: AbortSignal;
31
+ maxRetries?: number;
31
32
  }
32
33
  /**
33
34
  * A class that can be used to make async calls with concurrency and retry logic.
@@ -49,6 +50,7 @@ declare class AsyncCaller {
49
50
  private queue;
50
51
  constructor(params: AsyncCallerParams);
51
52
  call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
53
+ private callWithRetries;
52
54
  callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
53
55
  fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
54
56
  }
@@ -1 +1 @@
1
- {"version":3,"file":"async_caller.d.cts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAiCK;KAEA;EACH,QAAQ;EACR;EACA;;iBA8Ic,kBACd;iBAyBc,uBACd,iBACC;KAgHS,wBAAwB;UAEnB;;;;;EAKf;;;;;EAKA;;;;;;EAMA,kBAAkB;;UAGH;EACf,SAAS;;;;;;;;;;;;;;;cAgBE;YACD,gBAAgB;YAEhB,YAAY;YAEZ,iBAAiB;UAEnB;EAER,YAAY,QAAQ;EAad,KAAK,iBAAiB,cAAc,MAAM,MAAM,cACpD,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EA0B9B,gBAAgB,iBAAiB,cAAc,MAAM,MAAM,cACzD,SAAS,wBACT,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EAsB9B,SAAS,MAAM,kBAAkB,SAAS,kBAAkB"}
1
+ {"version":3,"file":"async_caller.d.cts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAmCK;KAEA;EACH,QAAQ;EACR;EACA;;iBA8Ic,kBACd;iBAyBc,uBACd,iBACC;KA0HS,wBAAwB;UAEnB;;;;;EAKf;;;;;EAKA;;;;;;EAMA,kBAAkB;;UAGH;EACf,SAAS;EACT;;;;;;;;;;;;;;;cAgBW;YACD,gBAAgB;YAEhB,YAAY;YAEZ,iBAAiB;UAEnB;EAER,YAAY,QAAQ;EAad,KAAK,iBAAiB,cAAc,MAAM,MAAM,cACpD,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;UAItB;EAmCR,gBAAgB,iBAAiB,cAAc,MAAM,MAAM,cACzD,SAAS,wBACT,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EAuB9B,SAAS,MAAM,kBAAkB,SAAS,kBAAkB"}
@@ -28,6 +28,7 @@ interface AsyncCallerParams {
28
28
  }
29
29
  interface AsyncCallerCallOptions {
30
30
  signal?: AbortSignal;
31
+ maxRetries?: number;
31
32
  }
32
33
  /**
33
34
  * A class that can be used to make async calls with concurrency and retry logic.
@@ -49,6 +50,7 @@ declare class AsyncCaller {
49
50
  private queue;
50
51
  constructor(params: AsyncCallerParams);
51
52
  call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
53
+ private callWithRetries;
52
54
  callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
53
55
  fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
54
56
  }
@@ -1 +1 @@
1
- {"version":3,"file":"async_caller.d.ts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAiCK;KAEA;EACH,QAAQ;EACR;EACA;;iBA8Ic,kBACd;iBAyBc,uBACd,iBACC;KAgHS,wBAAwB;UAEnB;;;;;EAKf;;;;;EAKA;;;;;;EAMA,kBAAkB;;UAGH;EACf,SAAS;;;;;;;;;;;;;;;cAgBE;YACD,gBAAgB;YAEhB,YAAY;YAEZ,iBAAiB;UAEnB;EAER,YAAY,QAAQ;EAad,KAAK,iBAAiB,cAAc,MAAM,MAAM,cACpD,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EA0B9B,gBAAgB,iBAAiB,cAAc,MAAM,MAAM,cACzD,SAAS,wBACT,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EAsB9B,SAAS,MAAM,kBAAkB,SAAS,kBAAkB"}
1
+ {"version":3,"file":"async_caller.d.ts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAmCK;KAEA;EACH,QAAQ;EACR;EACA;;iBA8Ic,kBACd;iBAyBc,uBACd,iBACC;KA0HS,wBAAwB;UAEnB;;;;;EAKf;;;;;EAKA;;;;;;EAMA,kBAAkB;;UAGH;EACf,SAAS;EACT;;;;;;;;;;;;;;;cAgBW;YACD,gBAAgB;YAEhB,YAAY;YAEZ,iBAAiB;UAEnB;EAER,YAAY,QAAQ;EAad,KAAK,iBAAiB,cAAc,MAAM,MAAM,cACpD,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;UAItB;EAmCR,gBAAgB,iBAAiB,cAAc,MAAM,MAAM,cACzD,SAAS,wBACT,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EAuB9B,SAAS,MAAM,kBAAkB,SAAS,kBAAkB"}
@@ -1,4 +1,5 @@
1
1
  import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { getRetryable, stampRetryable } from "../errors/index.js";
2
3
  import { getAbortSignalError } from "./signal.js";
3
4
  import pRetry from "./p-retry/index.js";
4
5
  import PQueueMod from "p-queue";
@@ -17,7 +18,8 @@ const STATUS_NO_RETRY = [
17
18
  405,
18
19
  406,
19
20
  407,
20
- 409
21
+ 409,
22
+ 413
21
23
  ];
22
24
  const RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 6e4;
23
25
  const QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [
@@ -130,10 +132,11 @@ function classifyRateLimitError(error) {
130
132
  */
131
133
  const defaultFailedAttemptHandler = (error) => {
132
134
  if (typeof error !== "object" || error === null) return;
133
- if ("message" in error && typeof error.message === "string" && (error.message.startsWith("Cancel") || error.message.startsWith("AbortError")) || "name" in error && typeof error.name === "string" && error.name === "AbortError") throw error;
135
+ if (getRetryable(error) === false) throw error;
136
+ if ("message" in error && typeof error.message === "string" && (error.message.startsWith("Cancel") || error.message.startsWith("AbortError")) || "name" in error && typeof error.name === "string" && error.name === "AbortError") throw stampRetryable(error, false);
134
137
  if ("code" in error && typeof error.code === "string" && error.code === "ECONNABORTED") throw error;
135
138
  const status = getResponseStatus(error) ?? getDirectStatus(error);
136
- if (status && STATUS_NO_RETRY.includes(+status)) throw error;
139
+ if (status && STATUS_NO_RETRY.includes(+status)) throw stampRetryable(error, false);
137
140
  if (getErrorCode(error) === "insufficient_quota") {
138
141
  const err = coerceError(error, getErrorMessage(error) ?? "Insufficient quota");
139
142
  err.name = "InsufficientQuotaError";
@@ -141,18 +144,19 @@ const defaultFailedAttemptHandler = (error) => {
141
144
  action: "stop",
142
145
  reason: "insufficient_quota"
143
146
  });
144
- throw err;
147
+ throw stampRetryable(err, false);
145
148
  }
146
149
  const rateLimitClassification = classifyRateLimitError(error);
147
150
  if (rateLimitClassification) {
148
151
  if (rateLimitClassification.action === "wait") {
149
152
  setRateLimitMetadata(error, rateLimitClassification);
153
+ stampRetryable(error, true);
150
154
  return;
151
155
  }
152
156
  const err = coerceError(error, getErrorMessage(error) ?? "Rate limit exceeded");
153
157
  if (err.name === "Error") err.name = rateLimitClassification.action === "stop" ? "RateLimitQuotaExhaustedError" : "RateLimitCapacityError";
154
158
  setRateLimitMetadata(err, rateLimitClassification);
155
- throw err;
159
+ throw stampRetryable(err, rateLimitClassification.action !== "stop");
156
160
  }
157
161
  };
158
162
  /**
@@ -181,19 +185,23 @@ var AsyncCaller = class {
181
185
  this.queue = new PQueue({ concurrency: this.maxConcurrency });
182
186
  }
183
187
  async call(callable, ...args) {
188
+ return this.callWithRetries(this.maxRetries, callable, args);
189
+ }
190
+ callWithRetries(retries, callable, args) {
184
191
  return this.queue.add(() => pRetry(() => callable(...args).catch((error) => {
185
192
  if (error instanceof Error) throw error;
186
193
  else throw new Error(error);
187
194
  }), {
188
195
  onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),
189
- retries: this.maxRetries,
196
+ retries,
190
197
  randomize: true
191
198
  }), { throwOnTimeout: true });
192
199
  }
193
200
  callWithOptions(options, callable, ...args) {
201
+ const retries = options.maxRetries ?? this.maxRetries;
194
202
  if (options.signal) {
195
203
  let listener;
196
- return Promise.race([this.call(callable, ...args), new Promise((_, reject) => {
204
+ return Promise.race([this.callWithRetries(retries, callable, args), new Promise((_, reject) => {
197
205
  listener = () => {
198
206
  reject(getAbortSignalError(options.signal));
199
207
  };
@@ -202,7 +210,7 @@ var AsyncCaller = class {
202
210
  if (options.signal && listener) options.signal.removeEventListener("abort", listener);
203
211
  });
204
212
  }
205
- return this.call(callable, ...args);
213
+ return this.callWithRetries(retries, callable, args);
206
214
  }
207
215
  fetch(...args) {
208
216
  return this.call(() => fetch(...args).then((res) => res.ok ? res : Promise.reject(res)));
@@ -1 +1 @@
1
- {"version":3,"file":"async_caller.js","names":[],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n throw err;\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n throw err;\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries: this.maxRetries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.call<A, T>(callable, ...args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.call<A, T>(callable, ...args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;AAKA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;C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;EACH;EACA,OAAO,KAAK,KAAW,UAAU,GAAG,IAAI;CAC1C;CAEA,MAAM,GAAG,MAA0D;EACjE,OAAO,KAAK,WACV,MAAM,GAAG,IAAI,CAAC,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,GAAG,CAAE,CACnE;CACF;AACF"}
1
+ {"version":3,"file":"async_caller.js","names":[],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getRetryable, stampRetryable } from \"../errors/index.js\";\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n 413, // Payload Too Large\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n // Honor a verdict already reached inside the callable, e.g. by a provider.\n if (getRetryable(error) === false) {\n throw error;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n // Deliberate cancellation, not a failure worth another attempt.\n throw stampRetryable(error, false);\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n // Deterministic client error; retrying it unchanged fails identically.\n throw stampRetryable(error, false);\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n // Exhausted quota needs an account action, not another attempt.\n throw stampRetryable(err, false);\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n stampRetryable(error, true);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n // Only \"stop\" is exhausted quota; \"capacity\" can still succeed later.\n throw stampRetryable(err, rateLimitClassification.action !== \"stop\");\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n maxRetries?: number;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.callWithRetries(this.maxRetries, callable, args);\n }\n\n private callWithRetries<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n A extends any[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n T extends (...args: A) => Promise<any>,\n >(\n retries: AsyncCallerParams[\"maxRetries\"],\n callable: T,\n args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n const retries = options.maxRetries ?? this.maxRetries;\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.callWithRetries<A, T>(retries, callable, args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.callWithRetries<A, T>(retries, callable, args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;AAMA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,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AAI;GACJ,OAAO,QAAQ,KAAK,CAClB,KAAK,gBAAsB,SAAS,UAAU,IAAI,GAClD,IAAI,SAAgB,GAAG,WAAW;IAChC,iBAAiB;KACf,OAAO,oBAAoB,QAAQ,MAAM,CAAC;IAC5C;IACA,QAAQ,QAAQ,iBAAiB,SAAS,UAAU,EAAE,MAAM,KAAK,CAAC;GACpE,CAAC,CACH,CAAC,CAAC,CAAC,cAAc;IACf,IAAI,QAAQ,UAAU,UACpB,QAAQ,OAAO,oBAAoB,SAAS,QAAQ;GAExD,CAAC;EACH;EACA,OAAO,KAAK,gBAAsB,SAAS,UAAU,IAAI;CAC3D;CAEA,MAAM,GAAG,MAA0D;EACjE,OAAO,KAAK,WACV,MAAM,GAAG,IAAI,CAAC,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,GAAG,CAAE,CACnE;CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {