@langchain/core 1.2.0 → 1.2.1
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 +10 -0
- package/dist/language_models/base.cjs +1 -1
- package/dist/language_models/base.js +1 -1
- package/dist/tools/index.cjs +4 -1
- package/dist/tools/index.cjs.map +1 -1
- package/dist/tools/index.d.cts.map +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +4 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/utils/async_caller.cjs +129 -6
- package/dist/utils/async_caller.cjs.map +1 -1
- package/dist/utils/async_caller.d.cts +9 -1
- package/dist/utils/async_caller.d.cts.map +1 -1
- package/dist/utils/async_caller.d.ts +9 -1
- package/dist/utils/async_caller.d.ts.map +1 -1
- package/dist/utils/async_caller.js +128 -7
- package/dist/utils/async_caller.js.map +1 -1
- package/dist/utils/p-retry/index.cjs +3 -1
- package/dist/utils/p-retry/index.cjs.map +1 -1
- package/dist/utils/p-retry/index.js +3 -1
- package/dist/utils/p-retry/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @langchain/core
|
|
2
2
|
|
|
3
|
+
## 1.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#10674](https://github.com/langchain-ai/langchainjs/pull/10674) [`f017708`](https://github.com/langchain-ai/langchainjs/commit/f01770895c06621b469a6c6b5244747f6efdfbf7) Thanks [@christian-bromann](https://github.com/christian-bromann)! - fix: classify provider 429s before retrying
|
|
8
|
+
|
|
9
|
+
- [#11092](https://github.com/langchain-ai/langchainjs/pull/11092) [`7918bbd`](https://github.com/langchain-ai/langchainjs/commit/7918bbdd2eaf8d9aff736b122f359a555267e1e7) Thanks [@aolsenjazz](https://github.com/aolsenjazz)! - fix(core): only treat arrays of content blocks as ToolMessage content
|
|
10
|
+
|
|
11
|
+
Fix tool outputs that are arrays of plain objects being forwarded as malformed message content. An array is now only treated as message content blocks when every element is an object with a `type`; otherwise it is JSON-stringified.
|
|
12
|
+
|
|
3
13
|
## 1.2.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
|
@@ -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.
|
|
142
|
+
this._addVersion("@langchain/core", "1.2.1");
|
|
143
143
|
}
|
|
144
144
|
_addVersion(pkg, version) {
|
|
145
145
|
const existing = this.metadata?.versions;
|
|
@@ -138,7 +138,7 @@ var BaseLangChain = 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.
|
|
141
|
+
this._addVersion("@langchain/core", "1.2.1");
|
|
142
142
|
}
|
|
143
143
|
_addVersion(pkg, version) {
|
|
144
144
|
const existing = this.metadata?.versions;
|
package/dist/tools/index.cjs
CHANGED
|
@@ -328,9 +328,12 @@ function tool(func, fields) {
|
|
|
328
328
|
}
|
|
329
329
|
});
|
|
330
330
|
}
|
|
331
|
+
function _isMessageContentBlockShaped(item) {
|
|
332
|
+
return typeof item === "object" && item !== null && "type" in item;
|
|
333
|
+
}
|
|
331
334
|
function _formatToolOutput(params) {
|
|
332
335
|
const { content, artifact, toolCallId, metadata } = params;
|
|
333
|
-
if (toolCallId && !require_messages_tool.isDirectToolOutput(content)) if (typeof content === "string" || Array.isArray(content) && content.every(
|
|
336
|
+
if (toolCallId && !require_messages_tool.isDirectToolOutput(content)) if (typeof content === "string" || Array.isArray(content) && content.every(_isMessageContentBlockShaped)) return new require_messages_tool.ToolMessage({
|
|
334
337
|
status: "success",
|
|
335
338
|
content,
|
|
336
339
|
artifact,
|
package/dist/tools/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["BaseLangChain","ensureConfig","mergeConfigs","_isToolCall","isInteropZodSchema","interopParseAsync","isInteropZodError","z4","ToolInputParsingException","parseCallbackConfigArg","CallbackManager","_configHasToolCallId","isAsyncGenerator","consumeAsyncGenerator","z","isSimpleStringZodSchema","validatesOnlyStrings","patchConfig","AsyncLocalStorageProviderSingleton","pickRunnableConfigKeys","getAbortSignalError","isDirectToolOutput","ToolMessage"],"sources":["../../src/tools/index.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { z as z4, ZodError } from \"zod/v4\";\nimport {\n validate,\n type Schema as ValidationSchema,\n} from \"@cfworker/json-schema\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n parseCallbackConfigArg,\n} from \"../callbacks/manager.js\";\nimport { BaseLangChain } from \"../language_models/base.js\";\nimport {\n mergeConfigs,\n ensureConfig,\n patchConfig,\n pickRunnableConfigKeys,\n type RunnableConfig,\n} from \"../runnables/config.js\";\nimport type { RunnableFunc } from \"../runnables/base.js\";\nimport { isDirectToolOutput, ToolCall, ToolMessage } from \"../messages/tool.js\";\nimport { AsyncLocalStorageProviderSingleton } from \"../singletons/index.js\";\nimport type { RunnableToolLike } from \"../runnables/base.js\";\nimport {\n _configHasToolCallId,\n _isToolCall,\n ToolInputParsingException,\n} from \"./utils.js\";\nimport {\n type InferInteropZodInput,\n type InferInteropZodOutput,\n type InteropZodObject,\n type InteropZodType,\n interopParseAsync,\n isSimpleStringZodSchema,\n isInteropZodError,\n isInteropZodSchema,\n type ZodStringV3,\n type ZodStringV4,\n type ZodObjectV3,\n type ZodObjectV4,\n} from \"../utils/types/zod.js\";\nimport { getAbortSignalError } from \"../utils/signal.js\";\nimport type {\n StructuredToolCallInput,\n ToolInputSchemaBase,\n ToolReturnType,\n ResponseFormat,\n ToolInputSchemaInputType,\n ToolInputSchemaOutputType,\n ToolParams,\n ToolRunnableConfig,\n StructuredToolInterface,\n DynamicToolInput,\n DynamicStructuredToolInput,\n StringInputToolSchema,\n ToolInterface,\n ToolOutputType,\n ToolRuntime,\n ToolEventType,\n InferToolEventFromFunc,\n InferToolOutputFromFunc,\n} from \"./types.js\";\nimport { type JSONSchema, validatesOnlyStrings } from \"../utils/json_schema.js\";\nimport { consumeAsyncGenerator, isAsyncGenerator } from \"../runnables/iter.js\";\n\nexport type {\n BaseDynamicToolInput,\n ContentAndArtifact,\n DynamicToolInput,\n DynamicStructuredToolInput,\n ResponseFormat,\n StructuredToolCallInput,\n StructuredToolInterface,\n StructuredToolParams,\n ToolInterface,\n ToolParams,\n ToolReturnType,\n ToolRunnableConfig,\n ToolInputSchemaBase as ToolSchemaBase,\n} from \"./types.js\";\n\nexport {\n isLangChainTool,\n isRunnableToolLike,\n isStructuredTool,\n isStructuredToolParams,\n type ToolRuntime,\n} from \"./types.js\";\n\nexport { ToolInputParsingException };\n/**\n * Base class for Tools that accept input of any shape defined by a Zod schema.\n */\nexport abstract class StructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends BaseLangChain<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolOutputT | ToolMessage\n >\n implements StructuredToolInterface<SchemaT, SchemaInputT, ToolOutputT>\n{\n abstract name: string;\n\n abstract description: string;\n\n abstract schema: SchemaT;\n\n /**\n * Optional provider-specific extra fields for the tool.\n *\n * This is used to pass provider-specific configuration that doesn't fit into\n * standard tool fields.\n */\n extras?: Record<string, unknown>;\n\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect = false;\n\n verboseParsingErrors = false;\n\n get lc_namespace() {\n return [\"langchain\", \"tools\"];\n }\n\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat = \"content\";\n\n /**\n * Default config object for the tool runnable.\n */\n defaultConfig?: ToolRunnableConfig;\n\n constructor(fields?: ToolParams) {\n super(fields ?? {});\n\n this.verboseParsingErrors =\n fields?.verboseParsingErrors ?? this.verboseParsingErrors;\n this.responseFormat = fields?.responseFormat ?? this.responseFormat;\n this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;\n this.metadata = fields?.metadata ?? this.metadata;\n this.extras = fields?.extras ?? this.extras;\n }\n\n protected abstract _call(\n arg: SchemaOutputT,\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT>;\n\n /**\n * Invokes the tool with the provided input and configuration.\n * @param input The input for the tool.\n * @param config Optional configuration for the tool.\n * @returns A Promise that resolves with the tool's output.\n */\n async invoke<\n TInput extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n input: TInput,\n config?: TConfig\n ): Promise<ToolReturnType<TInput, TConfig, ToolOutputT>> {\n let toolInput: Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n\n let enrichedConfig: ToolRunnableConfig = ensureConfig(\n mergeConfigs(this.defaultConfig, config)\n );\n if (_isToolCall(input)) {\n toolInput = input.args as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n enrichedConfig = {\n ...enrichedConfig,\n toolCall: input,\n };\n } else {\n toolInput = input as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n }\n\n return this.call(toolInput, enrichedConfig) as Promise<\n ToolReturnType<TInput, TConfig, ToolOutputT>\n >;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument, configuration, and tags. It\n * parses the input according to the schema, handles any errors, and\n * manages callbacks.\n * @param arg The input argument for the tool.\n * @param configArg Optional configuration or callbacks for the tool.\n * @param tags Optional tags for the tool.\n * @returns A Promise that resolves with a string.\n */\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<TArg, TConfig, ToolOutputT>> {\n // Determine the actual input that needs parsing/validation.\n // If arg is a ToolCall, use its args; otherwise, use arg directly.\n const inputForValidation = _isToolCall(arg) ? arg.args : arg;\n\n let parsed: SchemaOutputT; // This will hold the successfully parsed input of the expected output type.\n if (isInteropZodSchema(this.schema)) {\n try {\n // Validate the inputForValidation - TS needs help here as it can't exclude ToolCall based on the check\n parsed = await interopParseAsync(\n this.schema as InteropZodType,\n inputForValidation as Exclude<TArg, ToolCall>\n );\n } catch (e) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${(e as Error).message}`;\n }\n if (isInteropZodError(e)) {\n message = `${message}\\n\\n${z4.prettifyError(e as ZodError)}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n } else {\n const result = validate(\n inputForValidation,\n this.schema as ValidationSchema\n );\n if (!result.valid) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${result.errors\n .map((e) => `${e.keywordLocation}: ${e.error}`)\n .join(\"\\n\")}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n // Assign the validated input to parsed\n // We cast here because validate() doesn't narrow the type sufficiently for TS, but we know it's valid.\n parsed = inputForValidation as SchemaOutputT;\n }\n\n const config = parseCallbackConfigArg(configArg);\n const callbackManager_ = CallbackManager.configure(\n config.callbacks,\n this.callbacks,\n config.tags || tags,\n this.tags,\n config.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n\n let toolCallId: string | undefined;\n // Extract toolCallId ONLY if the original arg was a ToolCall\n if (_isToolCall(arg)) {\n toolCallId = arg.id;\n }\n // Or if it was provided in the config's toolCall property\n if (!toolCallId && _configHasToolCallId(config)) {\n toolCallId = config.toolCall.id;\n }\n\n const runManager = await callbackManager_?.handleToolStart(\n this.toJSON(),\n // Log the original raw input arg\n typeof arg === \"string\" ? arg : JSON.stringify(arg),\n config.runId,\n undefined,\n undefined,\n undefined,\n config.runName,\n toolCallId\n );\n delete config.runId;\n\n let result;\n try {\n const raw = await this._call(parsed, runManager, config);\n result = isAsyncGenerator(raw)\n ? await consumeAsyncGenerator(raw, async (chunk) => {\n try {\n await runManager?.handleToolEvent(chunk);\n } catch (streamError) {\n await runManager?.handleToolError(streamError);\n }\n })\n : raw;\n } catch (e) {\n await runManager?.handleToolError(e);\n throw e;\n }\n\n let content;\n let artifact;\n if (this.responseFormat === \"content_and_artifact\") {\n if (Array.isArray(result) && result.length === 2) {\n [content, artifact] = result;\n } else {\n throw new Error(\n `Tool response format is \"content_and_artifact\" but the output was not a two-tuple.\\nResult: ${JSON.stringify(\n result\n )}`\n );\n }\n } else {\n content = result;\n }\n\n const formattedOutput = _formatToolOutput<ToolOutputT>({\n content,\n artifact,\n toolCallId,\n name: this.name,\n metadata: this.metadata,\n });\n await runManager?.handleToolEnd(formattedOutput);\n return formattedOutput as ToolReturnType<TArg, TConfig, ToolOutputT>;\n }\n}\n\n/**\n * Base class for Tools that accept input as a string.\n */\nexport abstract class Tool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends StructuredTool<\n StringInputToolSchema,\n ToolInputSchemaOutputType<StringInputToolSchema>,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT,\n ToolEventT\n >\n implements\n ToolInterface<\n StringInputToolSchema,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT\n >\n{\n schema = z\n .object({ input: z.string().optional() })\n .transform((obj) => obj.input);\n\n constructor(fields?: ToolParams) {\n super(fields);\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument and callbacks. It handles\n * string inputs specifically.\n * @param arg The input argument for the tool, which can be a string, undefined, or an input of the tool's schema.\n * @param callbacks Optional callbacks for the tool.\n * @returns A Promise that resolves with a string.\n */\n // Match the base class signature including the generics and conditional return type\n call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n callbacks?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n // Prepare the input for the base class call method.\n // If arg is string or undefined, wrap it; otherwise, pass ToolCall or { input: ... } directly.\n const structuredArg =\n typeof arg === \"string\" || arg == null ? { input: arg } : arg;\n\n // Ensure TConfig is passed to super.call\n return super.call(structuredArg, callbacks);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and description.\n */\nexport class DynamicTool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n> extends Tool<ToolOutputT, ToolEventT> {\n static lc_name() {\n return \"DynamicTool\";\n }\n\n name: string;\n\n description: string;\n\n func: DynamicToolInput<ToolOutputT, ToolEventT>[\"func\"];\n\n constructor(fields: DynamicToolInput<ToolOutputT, ToolEventT>) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n async call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n // Call the Tool class's call method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig);\n }\n\n /** @ignore */\n _call(\n input: string, // DynamicTool's _call specifically expects a string after schema transformation\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(input, runManager, parentConfig);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and\n * description, designed to work with structured data. It extends the\n * StructuredTool class and overrides the _call method to execute the\n * provided function when the tool is called.\n *\n * Schema can be passed as Zod or JSON schema. The tool will not validate\n * input if JSON schema is passed.\n *\n * @template SchemaT The input schema type for the tool (Zod schema or JSON schema). Defaults to `ToolInputSchemaBase`.\n * @template SchemaOutputT The output type derived from the schema after parsing/validation. Defaults to `ToolInputSchemaOutputType<SchemaT>`.\n * @template SchemaInputT The input type derived from the schema before parsing. Defaults to `ToolInputSchemaInputType<SchemaT>`.\n * @template ToolOutputT The return type of the tool's function. Defaults to `ToolOutputType`.\n * @template NameT The literal type of the tool name (for discriminated union support). Defaults to `string`.\n */\nexport class DynamicStructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n NameT extends string = string,\n> extends StructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT\n> {\n static lc_name() {\n return \"DynamicStructuredTool\";\n }\n\n declare name: NameT;\n\n description: string;\n\n func: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"];\n\n schema: SchemaT;\n\n constructor(\n fields: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n > & {\n name: NameT;\n }\n ) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n this.schema = fields.schema;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n // Match the base class signature\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n\n // Call the base class method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig, tags);\n }\n\n protected _call(\n arg: Parameters<\n DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"]\n >[0],\n runManager?: CallbackManagerForToolRun,\n parentConfig?: RunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(arg, runManager, parentConfig);\n }\n}\n\n/**\n * Abstract base class for toolkits in LangChain. Toolkits are collections\n * of tools that agents can use. Subclasses must implement the `tools`\n * property to provide the specific tools for the toolkit.\n */\nexport abstract class BaseToolkit {\n abstract tools: StructuredToolInterface[];\n\n getTools(): StructuredToolInterface[] {\n return this.tools;\n }\n}\n\n/**\n * Parameters for the tool function.\n * Schema can be provided as Zod or JSON schema.\n * Both schema types will be validated.\n * @template {ToolInputSchemaBase} RunInput The input schema for the tool.\n * @template {string} NameT The literal name type for discriminated union support.\n */\nexport interface ToolWrapperParams<\n RunInput = ToolInputSchemaBase | undefined,\n NameT extends string = string,\n> extends ToolParams {\n /**\n * The name of the tool. If using with an LLM, this\n * will be passed as the tool name.\n */\n name: NameT;\n /**\n * The description of the tool.\n * @default `${fields.name} tool`\n */\n description?: string;\n /**\n * The input schema for the tool. If using an LLM, this\n * will be passed as the tool schema to generate arguments\n * for.\n */\n schema?: RunInput;\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat;\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect?: boolean;\n}\n\n/**\n * Creates a new StructuredTool instance with the provided function, name, description, and schema.\n *\n * Schema can be provided as Zod or JSON schema, and both will be validated.\n *\n * @function\n * @template {ToolInputSchemaBase} SchemaT The input schema for the tool.\n * @template {ToolReturnType} ToolOutputT The output type of the tool.\n *\n * @param {RunnableFunc<z.output<SchemaT>, ToolOutputT>} func - The function to invoke when the tool is called.\n * @param {ToolWrapperParams<SchemaT>} fields - An object containing the following properties:\n * @param {string} fields.name The name of the tool.\n * @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.\n * @param {z.AnyZodObject | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.\n *\n * @returns {DynamicStructuredTool<SchemaT>} A new StructuredTool instance.\n */\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n >\n | DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\n// Overloads with ToolRuntime as CallOptions\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n >\n | DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >\n | DynamicTool<ToolOutputT, ToolEventT> {\n const isSimpleStringSchema = isSimpleStringZodSchema(fields.schema);\n const isStringJSONSchema = validatesOnlyStrings(fields.schema);\n\n // If the schema is not provided, or it's a simple string schema, create a DynamicTool\n if (!fields.schema || isSimpleStringSchema || isStringJSONSchema) {\n return new DynamicTool<ToolOutputT, ToolEventT>({\n ...fields,\n description:\n fields.description ??\n (fields.schema as { description?: string } | undefined)?.description ??\n `${fields.name} tool`,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(func(input as any, childConfig as any));\n } catch (e) {\n reject(e);\n }\n }\n );\n });\n },\n });\n }\n\n const schema = fields.schema as InteropZodObject | JSONSchema;\n\n const description =\n fields.description ??\n (fields.schema as { description?: string }).description ??\n `${fields.name} tool`;\n\n return new DynamicStructuredTool<\n typeof schema,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >({\n ...fields,\n description,\n schema,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n let listener: (() => void) | undefined;\n const cleanup = () => {\n if (config?.signal && listener) {\n config.signal.removeEventListener(\"abort\", listener);\n }\n };\n\n if (config?.signal) {\n listener = () => {\n cleanup();\n reject(getAbortSignalError(config.signal));\n };\n config.signal.addEventListener(\"abort\", listener, { once: true });\n }\n\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const result = await func(input as any, childConfig as any);\n if (isAsyncGenerator(result)) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(result as any);\n return;\n }\n\n /**\n * If the signal is aborted, we don't want to resolve the promise\n * as the promise is already rejected.\n */\n if (config?.signal?.aborted) {\n cleanup();\n return;\n }\n\n cleanup();\n resolve(result);\n } catch (e) {\n cleanup();\n reject(e);\n }\n }\n );\n });\n },\n }) as DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >;\n}\n\nfunction _formatToolOutput<TOutput extends ToolOutputType>(params: {\n content: TOutput;\n name: string;\n artifact?: unknown;\n toolCallId?: string;\n metadata?: Record<string, unknown>;\n}): ToolMessage | TOutput {\n const { content, artifact, toolCallId, metadata } = params;\n if (toolCallId && !isDirectToolOutput(content)) {\n if (\n typeof content === \"string\" ||\n (Array.isArray(content) &&\n content.every((item) => typeof item === \"object\"))\n ) {\n return new ToolMessage({\n status: \"success\",\n content,\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n } else {\n return new ToolMessage({\n status: \"success\",\n content: _stringify(content),\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n }\n } else {\n return content;\n }\n}\n\nfunction _stringify(content: unknown): string {\n try {\n return JSON.stringify(content) ?? \"\";\n } catch (_noOp) {\n return `${content}`;\n }\n}\n\nexport type ServerTool = Record<string, unknown>;\nexport type ClientTool =\n | StructuredToolInterface\n | DynamicTool\n | RunnableToolLike;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8FA,IAAsB,iBAAtB,cAOUA,6BAAAA,cAKV;;;;;;;CAaE;;;;;;;CAQA,eAAe;CAEf,uBAAuB;CAEvB,IAAI,eAAe;AACjB,SAAO,CAAC,aAAa,QAAQ;;;;;;;;;;;CAY/B,iBAAkC;;;;CAKlC;CAEA,YAAY,QAAqB;AAC/B,QAAM,UAAU,EAAE,CAAC;AAEnB,OAAK,uBACH,QAAQ,wBAAwB,KAAK;AACvC,OAAK,iBAAiB,QAAQ,kBAAkB,KAAK;AACrD,OAAK,gBAAgB,QAAQ,iBAAiB,KAAK;AACnD,OAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,OAAK,SAAS,QAAQ,UAAU,KAAK;;;;;;;;CAevC,MAAM,OAIJ,OACA,QACuD;EACvD,IAAI;EAKJ,IAAI,iBAAqCC,eAAAA,aACvCC,eAAAA,aAAa,KAAK,eAAe,OAAO,CACzC;AACD,MAAIC,cAAAA,YAAY,MAAM,EAAE;AACtB,eAAY,MAAM;AAIlB,oBAAiB;IACf,GAAG;IACH,UAAU;IACX;QAED,aAAY;AAMd,SAAO,KAAK,KAAK,WAAW,eAAe;;;;;;;;;;;;;CAgB7C,MAAM,KAIJ,KACA,WAEA,MACqD;EAGrD,MAAM,qBAAqBA,cAAAA,YAAY,IAAI,GAAG,IAAI,OAAO;EAEzD,IAAI;AACJ,MAAIC,YAAAA,mBAAmB,KAAK,OAAO,CACjC,KAAI;AAEF,YAAS,MAAMC,YAAAA,kBACb,KAAK,QACL,mBACD;WACM,GA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EF,QAAO,IAAIA,sBAAAA,YAAY;EACrB,QAAQ;EACR,SAAS,WAAW,QAAQ;EAC5B;EACA,cAAc;EACd,MAAM,OAAO;EACb;EACD,CAAC;KAGJ,QAAO;;AAIX,SAAS,WAAW,SAA0B;AAC5C,KAAI;AACF,SAAO,KAAK,UAAU,QAAQ,IAAI;UAC3B,OAAO;AACd,SAAO,GAAG"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["BaseLangChain","ensureConfig","mergeConfigs","_isToolCall","isInteropZodSchema","interopParseAsync","isInteropZodError","z4","ToolInputParsingException","parseCallbackConfigArg","CallbackManager","_configHasToolCallId","isAsyncGenerator","consumeAsyncGenerator","z","isSimpleStringZodSchema","validatesOnlyStrings","patchConfig","AsyncLocalStorageProviderSingleton","pickRunnableConfigKeys","getAbortSignalError","isDirectToolOutput","ToolMessage"],"sources":["../../src/tools/index.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { z as z4, ZodError } from \"zod/v4\";\nimport {\n validate,\n type Schema as ValidationSchema,\n} from \"@cfworker/json-schema\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n parseCallbackConfigArg,\n} from \"../callbacks/manager.js\";\nimport { BaseLangChain } from \"../language_models/base.js\";\nimport {\n mergeConfigs,\n ensureConfig,\n patchConfig,\n pickRunnableConfigKeys,\n type RunnableConfig,\n} from \"../runnables/config.js\";\nimport type { RunnableFunc } from \"../runnables/base.js\";\nimport { isDirectToolOutput, ToolCall, ToolMessage } from \"../messages/tool.js\";\nimport { AsyncLocalStorageProviderSingleton } from \"../singletons/index.js\";\nimport type { RunnableToolLike } from \"../runnables/base.js\";\nimport {\n _configHasToolCallId,\n _isToolCall,\n ToolInputParsingException,\n} from \"./utils.js\";\nimport {\n type InferInteropZodInput,\n type InferInteropZodOutput,\n type InteropZodObject,\n type InteropZodType,\n interopParseAsync,\n isSimpleStringZodSchema,\n isInteropZodError,\n isInteropZodSchema,\n type ZodStringV3,\n type ZodStringV4,\n type ZodObjectV3,\n type ZodObjectV4,\n} from \"../utils/types/zod.js\";\nimport { getAbortSignalError } from \"../utils/signal.js\";\nimport type {\n StructuredToolCallInput,\n ToolInputSchemaBase,\n ToolReturnType,\n ResponseFormat,\n ToolInputSchemaInputType,\n ToolInputSchemaOutputType,\n ToolParams,\n ToolRunnableConfig,\n StructuredToolInterface,\n DynamicToolInput,\n DynamicStructuredToolInput,\n StringInputToolSchema,\n ToolInterface,\n ToolOutputType,\n ToolRuntime,\n ToolEventType,\n InferToolEventFromFunc,\n InferToolOutputFromFunc,\n} from \"./types.js\";\nimport { type JSONSchema, validatesOnlyStrings } from \"../utils/json_schema.js\";\nimport { consumeAsyncGenerator, isAsyncGenerator } from \"../runnables/iter.js\";\n\nexport type {\n BaseDynamicToolInput,\n ContentAndArtifact,\n DynamicToolInput,\n DynamicStructuredToolInput,\n ResponseFormat,\n StructuredToolCallInput,\n StructuredToolInterface,\n StructuredToolParams,\n ToolInterface,\n ToolParams,\n ToolReturnType,\n ToolRunnableConfig,\n ToolInputSchemaBase as ToolSchemaBase,\n} from \"./types.js\";\n\nexport {\n isLangChainTool,\n isRunnableToolLike,\n isStructuredTool,\n isStructuredToolParams,\n type ToolRuntime,\n} from \"./types.js\";\n\nexport { ToolInputParsingException };\n/**\n * Base class for Tools that accept input of any shape defined by a Zod schema.\n */\nexport abstract class StructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends BaseLangChain<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolOutputT | ToolMessage\n >\n implements StructuredToolInterface<SchemaT, SchemaInputT, ToolOutputT>\n{\n abstract name: string;\n\n abstract description: string;\n\n abstract schema: SchemaT;\n\n /**\n * Optional provider-specific extra fields for the tool.\n *\n * This is used to pass provider-specific configuration that doesn't fit into\n * standard tool fields.\n */\n extras?: Record<string, unknown>;\n\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect = false;\n\n verboseParsingErrors = false;\n\n get lc_namespace() {\n return [\"langchain\", \"tools\"];\n }\n\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat = \"content\";\n\n /**\n * Default config object for the tool runnable.\n */\n defaultConfig?: ToolRunnableConfig;\n\n constructor(fields?: ToolParams) {\n super(fields ?? {});\n\n this.verboseParsingErrors =\n fields?.verboseParsingErrors ?? this.verboseParsingErrors;\n this.responseFormat = fields?.responseFormat ?? this.responseFormat;\n this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;\n this.metadata = fields?.metadata ?? this.metadata;\n this.extras = fields?.extras ?? this.extras;\n }\n\n protected abstract _call(\n arg: SchemaOutputT,\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT>;\n\n /**\n * Invokes the tool with the provided input and configuration.\n * @param input The input for the tool.\n * @param config Optional configuration for the tool.\n * @returns A Promise that resolves with the tool's output.\n */\n async invoke<\n TInput extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n input: TInput,\n config?: TConfig\n ): Promise<ToolReturnType<TInput, TConfig, ToolOutputT>> {\n let toolInput: Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n\n let enrichedConfig: ToolRunnableConfig = ensureConfig(\n mergeConfigs(this.defaultConfig, config)\n );\n if (_isToolCall(input)) {\n toolInput = input.args as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n enrichedConfig = {\n ...enrichedConfig,\n toolCall: input,\n };\n } else {\n toolInput = input as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n }\n\n return this.call(toolInput, enrichedConfig) as Promise<\n ToolReturnType<TInput, TConfig, ToolOutputT>\n >;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument, configuration, and tags. It\n * parses the input according to the schema, handles any errors, and\n * manages callbacks.\n * @param arg The input argument for the tool.\n * @param configArg Optional configuration or callbacks for the tool.\n * @param tags Optional tags for the tool.\n * @returns A Promise that resolves with a string.\n */\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<TArg, TConfig, ToolOutputT>> {\n // Determine the actual input that needs parsing/validation.\n // If arg is a ToolCall, use its args; otherwise, use arg directly.\n const inputForValidation = _isToolCall(arg) ? arg.args : arg;\n\n let parsed: SchemaOutputT; // This will hold the successfully parsed input of the expected output type.\n if (isInteropZodSchema(this.schema)) {\n try {\n // Validate the inputForValidation - TS needs help here as it can't exclude ToolCall based on the check\n parsed = await interopParseAsync(\n this.schema as InteropZodType,\n inputForValidation as Exclude<TArg, ToolCall>\n );\n } catch (e) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${(e as Error).message}`;\n }\n if (isInteropZodError(e)) {\n message = `${message}\\n\\n${z4.prettifyError(e as ZodError)}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n } else {\n const result = validate(\n inputForValidation,\n this.schema as ValidationSchema\n );\n if (!result.valid) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${result.errors\n .map((e) => `${e.keywordLocation}: ${e.error}`)\n .join(\"\\n\")}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n // Assign the validated input to parsed\n // We cast here because validate() doesn't narrow the type sufficiently for TS, but we know it's valid.\n parsed = inputForValidation as SchemaOutputT;\n }\n\n const config = parseCallbackConfigArg(configArg);\n const callbackManager_ = CallbackManager.configure(\n config.callbacks,\n this.callbacks,\n config.tags || tags,\n this.tags,\n config.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n\n let toolCallId: string | undefined;\n // Extract toolCallId ONLY if the original arg was a ToolCall\n if (_isToolCall(arg)) {\n toolCallId = arg.id;\n }\n // Or if it was provided in the config's toolCall property\n if (!toolCallId && _configHasToolCallId(config)) {\n toolCallId = config.toolCall.id;\n }\n\n const runManager = await callbackManager_?.handleToolStart(\n this.toJSON(),\n // Log the original raw input arg\n typeof arg === \"string\" ? arg : JSON.stringify(arg),\n config.runId,\n undefined,\n undefined,\n undefined,\n config.runName,\n toolCallId\n );\n delete config.runId;\n\n let result;\n try {\n const raw = await this._call(parsed, runManager, config);\n result = isAsyncGenerator(raw)\n ? await consumeAsyncGenerator(raw, async (chunk) => {\n try {\n await runManager?.handleToolEvent(chunk);\n } catch (streamError) {\n await runManager?.handleToolError(streamError);\n }\n })\n : raw;\n } catch (e) {\n await runManager?.handleToolError(e);\n throw e;\n }\n\n let content;\n let artifact;\n if (this.responseFormat === \"content_and_artifact\") {\n if (Array.isArray(result) && result.length === 2) {\n [content, artifact] = result;\n } else {\n throw new Error(\n `Tool response format is \"content_and_artifact\" but the output was not a two-tuple.\\nResult: ${JSON.stringify(\n result\n )}`\n );\n }\n } else {\n content = result;\n }\n\n const formattedOutput = _formatToolOutput<ToolOutputT>({\n content,\n artifact,\n toolCallId,\n name: this.name,\n metadata: this.metadata,\n });\n await runManager?.handleToolEnd(formattedOutput);\n return formattedOutput as ToolReturnType<TArg, TConfig, ToolOutputT>;\n }\n}\n\n/**\n * Base class for Tools that accept input as a string.\n */\nexport abstract class Tool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends StructuredTool<\n StringInputToolSchema,\n ToolInputSchemaOutputType<StringInputToolSchema>,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT,\n ToolEventT\n >\n implements\n ToolInterface<\n StringInputToolSchema,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT\n >\n{\n schema = z\n .object({ input: z.string().optional() })\n .transform((obj) => obj.input);\n\n constructor(fields?: ToolParams) {\n super(fields);\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument and callbacks. It handles\n * string inputs specifically.\n * @param arg The input argument for the tool, which can be a string, undefined, or an input of the tool's schema.\n * @param callbacks Optional callbacks for the tool.\n * @returns A Promise that resolves with a string.\n */\n // Match the base class signature including the generics and conditional return type\n call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n callbacks?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n // Prepare the input for the base class call method.\n // If arg is string or undefined, wrap it; otherwise, pass ToolCall or { input: ... } directly.\n const structuredArg =\n typeof arg === \"string\" || arg == null ? { input: arg } : arg;\n\n // Ensure TConfig is passed to super.call\n return super.call(structuredArg, callbacks);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and description.\n */\nexport class DynamicTool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n> extends Tool<ToolOutputT, ToolEventT> {\n static lc_name() {\n return \"DynamicTool\";\n }\n\n name: string;\n\n description: string;\n\n func: DynamicToolInput<ToolOutputT, ToolEventT>[\"func\"];\n\n constructor(fields: DynamicToolInput<ToolOutputT, ToolEventT>) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n async call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n // Call the Tool class's call method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig);\n }\n\n /** @ignore */\n _call(\n input: string, // DynamicTool's _call specifically expects a string after schema transformation\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(input, runManager, parentConfig);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and\n * description, designed to work with structured data. It extends the\n * StructuredTool class and overrides the _call method to execute the\n * provided function when the tool is called.\n *\n * Schema can be passed as Zod or JSON schema. The tool will not validate\n * input if JSON schema is passed.\n *\n * @template SchemaT The input schema type for the tool (Zod schema or JSON schema). Defaults to `ToolInputSchemaBase`.\n * @template SchemaOutputT The output type derived from the schema after parsing/validation. Defaults to `ToolInputSchemaOutputType<SchemaT>`.\n * @template SchemaInputT The input type derived from the schema before parsing. Defaults to `ToolInputSchemaInputType<SchemaT>`.\n * @template ToolOutputT The return type of the tool's function. Defaults to `ToolOutputType`.\n * @template NameT The literal type of the tool name (for discriminated union support). Defaults to `string`.\n */\nexport class DynamicStructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n NameT extends string = string,\n> extends StructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT\n> {\n static lc_name() {\n return \"DynamicStructuredTool\";\n }\n\n declare name: NameT;\n\n description: string;\n\n func: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"];\n\n schema: SchemaT;\n\n constructor(\n fields: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n > & {\n name: NameT;\n }\n ) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n this.schema = fields.schema;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n // Match the base class signature\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n\n // Call the base class method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig, tags);\n }\n\n protected _call(\n arg: Parameters<\n DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"]\n >[0],\n runManager?: CallbackManagerForToolRun,\n parentConfig?: RunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(arg, runManager, parentConfig);\n }\n}\n\n/**\n * Abstract base class for toolkits in LangChain. Toolkits are collections\n * of tools that agents can use. Subclasses must implement the `tools`\n * property to provide the specific tools for the toolkit.\n */\nexport abstract class BaseToolkit {\n abstract tools: StructuredToolInterface[];\n\n getTools(): StructuredToolInterface[] {\n return this.tools;\n }\n}\n\n/**\n * Parameters for the tool function.\n * Schema can be provided as Zod or JSON schema.\n * Both schema types will be validated.\n * @template {ToolInputSchemaBase} RunInput The input schema for the tool.\n * @template {string} NameT The literal name type for discriminated union support.\n */\nexport interface ToolWrapperParams<\n RunInput = ToolInputSchemaBase | undefined,\n NameT extends string = string,\n> extends ToolParams {\n /**\n * The name of the tool. If using with an LLM, this\n * will be passed as the tool name.\n */\n name: NameT;\n /**\n * The description of the tool.\n * @default `${fields.name} tool`\n */\n description?: string;\n /**\n * The input schema for the tool. If using an LLM, this\n * will be passed as the tool schema to generate arguments\n * for.\n */\n schema?: RunInput;\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat;\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect?: boolean;\n}\n\n/**\n * Creates a new StructuredTool instance with the provided function, name, description, and schema.\n *\n * Schema can be provided as Zod or JSON schema, and both will be validated.\n *\n * @function\n * @template {ToolInputSchemaBase} SchemaT The input schema for the tool.\n * @template {ToolReturnType} ToolOutputT The output type of the tool.\n *\n * @param {RunnableFunc<z.output<SchemaT>, ToolOutputT>} func - The function to invoke when the tool is called.\n * @param {ToolWrapperParams<SchemaT>} fields - An object containing the following properties:\n * @param {string} fields.name The name of the tool.\n * @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.\n * @param {z.AnyZodObject | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.\n *\n * @returns {DynamicStructuredTool<SchemaT>} A new StructuredTool instance.\n */\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n >\n | DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\n// Overloads with ToolRuntime as CallOptions\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n >\n | DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >\n | DynamicTool<ToolOutputT, ToolEventT> {\n const isSimpleStringSchema = isSimpleStringZodSchema(fields.schema);\n const isStringJSONSchema = validatesOnlyStrings(fields.schema);\n\n // If the schema is not provided, or it's a simple string schema, create a DynamicTool\n if (!fields.schema || isSimpleStringSchema || isStringJSONSchema) {\n return new DynamicTool<ToolOutputT, ToolEventT>({\n ...fields,\n description:\n fields.description ??\n (fields.schema as { description?: string } | undefined)?.description ??\n `${fields.name} tool`,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(func(input as any, childConfig as any));\n } catch (e) {\n reject(e);\n }\n }\n );\n });\n },\n });\n }\n\n const schema = fields.schema as InteropZodObject | JSONSchema;\n\n const description =\n fields.description ??\n (fields.schema as { description?: string }).description ??\n `${fields.name} tool`;\n\n return new DynamicStructuredTool<\n typeof schema,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >({\n ...fields,\n description,\n schema,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n let listener: (() => void) | undefined;\n const cleanup = () => {\n if (config?.signal && listener) {\n config.signal.removeEventListener(\"abort\", listener);\n }\n };\n\n if (config?.signal) {\n listener = () => {\n cleanup();\n reject(getAbortSignalError(config.signal));\n };\n config.signal.addEventListener(\"abort\", listener, { once: true });\n }\n\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const result = await func(input as any, childConfig as any);\n if (isAsyncGenerator(result)) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(result as any);\n return;\n }\n\n /**\n * If the signal is aborted, we don't want to resolve the promise\n * as the promise is already rejected.\n */\n if (config?.signal?.aborted) {\n cleanup();\n return;\n }\n\n cleanup();\n resolve(result);\n } catch (e) {\n cleanup();\n reject(e);\n }\n }\n );\n });\n },\n }) as DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >;\n}\n\nfunction _isMessageContentBlockShaped(item: unknown) {\n return typeof item === \"object\" && item !== null && \"type\" in item;\n}\n\nfunction _formatToolOutput<TOutput extends ToolOutputType>(params: {\n content: TOutput;\n name: string;\n artifact?: unknown;\n toolCallId?: string;\n metadata?: Record<string, unknown>;\n}): ToolMessage | TOutput {\n const { content, artifact, toolCallId, metadata } = params;\n if (toolCallId && !isDirectToolOutput(content)) {\n if (\n typeof content === \"string\" ||\n (Array.isArray(content) && content.every(_isMessageContentBlockShaped))\n ) {\n return new ToolMessage({\n status: \"success\",\n content,\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n } else {\n return new ToolMessage({\n status: \"success\",\n content: _stringify(content),\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n }\n } else {\n return content;\n }\n}\n\nfunction _stringify(content: unknown): string {\n try {\n return JSON.stringify(content) ?? \"\";\n } catch (_noOp) {\n return `${content}`;\n }\n}\n\nexport type ServerTool = Record<string, unknown>;\nexport type ClientTool =\n | StructuredToolInterface\n | DynamicTool\n | RunnableToolLike;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8FA,IAAsB,iBAAtB,cAOUA,6BAAAA,cAKV;;;;;;;CAaE;;;;;;;CAQA,eAAe;CAEf,uBAAuB;CAEvB,IAAI,eAAe;AACjB,SAAO,CAAC,aAAa,QAAQ;;;;;;;;;;;CAY/B,iBAAkC;;;;CAKlC;CAEA,YAAY,QAAqB;AAC/B,QAAM,UAAU,EAAE,CAAC;AAEnB,OAAK,uBACH,QAAQ,wBAAwB,KAAK;AACvC,OAAK,iBAAiB,QAAQ,kBAAkB,KAAK;AACrD,OAAK,gBAAgB,QAAQ,iBAAiB,KAAK;AACnD,OAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,OAAK,SAAS,QAAQ,UAAU,KAAK;;;;;;;;CAevC,MAAM,OAIJ,OACA,QACuD;EACvD,IAAI;EAKJ,IAAI,iBAAqCC,eAAAA,aACvCC,eAAAA,aAAa,KAAK,eAAe,OAAO,CACzC;AACD,MAAIC,cAAAA,YAAY,MAAM,EAAE;AACtB,eAAY,MAAM;AAIlB,oBAAiB;IACf,GAAG;IACH,UAAU;IACX;QAED,aAAY;AAMd,SAAO,KAAK,KAAK,WAA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A,GACE,KAAA,EAAO,aAAA,EACP,OAAA,EAAS,WAAA,CAAY,MAAA,EAAQ,QAAA,MAC1B,WAAA,GAAc,OAAA,CAAQ,WAAA,GAC3B,MAAA,EAAQ,iBAAA,CAAkB,OAAA,EAAS,KAAA,IAEjC,qBAAA,CACE,OAAA,EACA,aAAA,EACA,YAAA,EACA,WAAA,EACA,aAAA,EACA,KAAA,IAEF,WAAA,CAAY,WAAA;AAAA,KA6LJ,UAAA,GAAa,MAAA;AAAA,KACb,UAAA,GACR,uBAAA,GACA,WAAA,GACA,gBAAA"}
|
package/dist/tools/index.js
CHANGED
|
@@ -327,9 +327,12 @@ function tool(func, fields) {
|
|
|
327
327
|
}
|
|
328
328
|
});
|
|
329
329
|
}
|
|
330
|
+
function _isMessageContentBlockShaped(item) {
|
|
331
|
+
return typeof item === "object" && item !== null && "type" in item;
|
|
332
|
+
}
|
|
330
333
|
function _formatToolOutput(params) {
|
|
331
334
|
const { content, artifact, toolCallId, metadata } = params;
|
|
332
|
-
if (toolCallId && !isDirectToolOutput(content)) if (typeof content === "string" || Array.isArray(content) && content.every(
|
|
335
|
+
if (toolCallId && !isDirectToolOutput(content)) if (typeof content === "string" || Array.isArray(content) && content.every(_isMessageContentBlockShaped)) return new ToolMessage({
|
|
333
336
|
status: "success",
|
|
334
337
|
content,
|
|
335
338
|
artifact,
|
package/dist/tools/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["z4"],"sources":["../../src/tools/index.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { z as z4, ZodError } from \"zod/v4\";\nimport {\n validate,\n type Schema as ValidationSchema,\n} from \"@cfworker/json-schema\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n parseCallbackConfigArg,\n} from \"../callbacks/manager.js\";\nimport { BaseLangChain } from \"../language_models/base.js\";\nimport {\n mergeConfigs,\n ensureConfig,\n patchConfig,\n pickRunnableConfigKeys,\n type RunnableConfig,\n} from \"../runnables/config.js\";\nimport type { RunnableFunc } from \"../runnables/base.js\";\nimport { isDirectToolOutput, ToolCall, ToolMessage } from \"../messages/tool.js\";\nimport { AsyncLocalStorageProviderSingleton } from \"../singletons/index.js\";\nimport type { RunnableToolLike } from \"../runnables/base.js\";\nimport {\n _configHasToolCallId,\n _isToolCall,\n ToolInputParsingException,\n} from \"./utils.js\";\nimport {\n type InferInteropZodInput,\n type InferInteropZodOutput,\n type InteropZodObject,\n type InteropZodType,\n interopParseAsync,\n isSimpleStringZodSchema,\n isInteropZodError,\n isInteropZodSchema,\n type ZodStringV3,\n type ZodStringV4,\n type ZodObjectV3,\n type ZodObjectV4,\n} from \"../utils/types/zod.js\";\nimport { getAbortSignalError } from \"../utils/signal.js\";\nimport type {\n StructuredToolCallInput,\n ToolInputSchemaBase,\n ToolReturnType,\n ResponseFormat,\n ToolInputSchemaInputType,\n ToolInputSchemaOutputType,\n ToolParams,\n ToolRunnableConfig,\n StructuredToolInterface,\n DynamicToolInput,\n DynamicStructuredToolInput,\n StringInputToolSchema,\n ToolInterface,\n ToolOutputType,\n ToolRuntime,\n ToolEventType,\n InferToolEventFromFunc,\n InferToolOutputFromFunc,\n} from \"./types.js\";\nimport { type JSONSchema, validatesOnlyStrings } from \"../utils/json_schema.js\";\nimport { consumeAsyncGenerator, isAsyncGenerator } from \"../runnables/iter.js\";\n\nexport type {\n BaseDynamicToolInput,\n ContentAndArtifact,\n DynamicToolInput,\n DynamicStructuredToolInput,\n ResponseFormat,\n StructuredToolCallInput,\n StructuredToolInterface,\n StructuredToolParams,\n ToolInterface,\n ToolParams,\n ToolReturnType,\n ToolRunnableConfig,\n ToolInputSchemaBase as ToolSchemaBase,\n} from \"./types.js\";\n\nexport {\n isLangChainTool,\n isRunnableToolLike,\n isStructuredTool,\n isStructuredToolParams,\n type ToolRuntime,\n} from \"./types.js\";\n\nexport { ToolInputParsingException };\n/**\n * Base class for Tools that accept input of any shape defined by a Zod schema.\n */\nexport abstract class StructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends BaseLangChain<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolOutputT | ToolMessage\n >\n implements StructuredToolInterface<SchemaT, SchemaInputT, ToolOutputT>\n{\n abstract name: string;\n\n abstract description: string;\n\n abstract schema: SchemaT;\n\n /**\n * Optional provider-specific extra fields for the tool.\n *\n * This is used to pass provider-specific configuration that doesn't fit into\n * standard tool fields.\n */\n extras?: Record<string, unknown>;\n\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect = false;\n\n verboseParsingErrors = false;\n\n get lc_namespace() {\n return [\"langchain\", \"tools\"];\n }\n\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat = \"content\";\n\n /**\n * Default config object for the tool runnable.\n */\n defaultConfig?: ToolRunnableConfig;\n\n constructor(fields?: ToolParams) {\n super(fields ?? {});\n\n this.verboseParsingErrors =\n fields?.verboseParsingErrors ?? this.verboseParsingErrors;\n this.responseFormat = fields?.responseFormat ?? this.responseFormat;\n this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;\n this.metadata = fields?.metadata ?? this.metadata;\n this.extras = fields?.extras ?? this.extras;\n }\n\n protected abstract _call(\n arg: SchemaOutputT,\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT>;\n\n /**\n * Invokes the tool with the provided input and configuration.\n * @param input The input for the tool.\n * @param config Optional configuration for the tool.\n * @returns A Promise that resolves with the tool's output.\n */\n async invoke<\n TInput extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n input: TInput,\n config?: TConfig\n ): Promise<ToolReturnType<TInput, TConfig, ToolOutputT>> {\n let toolInput: Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n\n let enrichedConfig: ToolRunnableConfig = ensureConfig(\n mergeConfigs(this.defaultConfig, config)\n );\n if (_isToolCall(input)) {\n toolInput = input.args as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n enrichedConfig = {\n ...enrichedConfig,\n toolCall: input,\n };\n } else {\n toolInput = input as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n }\n\n return this.call(toolInput, enrichedConfig) as Promise<\n ToolReturnType<TInput, TConfig, ToolOutputT>\n >;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument, configuration, and tags. It\n * parses the input according to the schema, handles any errors, and\n * manages callbacks.\n * @param arg The input argument for the tool.\n * @param configArg Optional configuration or callbacks for the tool.\n * @param tags Optional tags for the tool.\n * @returns A Promise that resolves with a string.\n */\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<TArg, TConfig, ToolOutputT>> {\n // Determine the actual input that needs parsing/validation.\n // If arg is a ToolCall, use its args; otherwise, use arg directly.\n const inputForValidation = _isToolCall(arg) ? arg.args : arg;\n\n let parsed: SchemaOutputT; // This will hold the successfully parsed input of the expected output type.\n if (isInteropZodSchema(this.schema)) {\n try {\n // Validate the inputForValidation - TS needs help here as it can't exclude ToolCall based on the check\n parsed = await interopParseAsync(\n this.schema as InteropZodType,\n inputForValidation as Exclude<TArg, ToolCall>\n );\n } catch (e) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${(e as Error).message}`;\n }\n if (isInteropZodError(e)) {\n message = `${message}\\n\\n${z4.prettifyError(e as ZodError)}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n } else {\n const result = validate(\n inputForValidation,\n this.schema as ValidationSchema\n );\n if (!result.valid) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${result.errors\n .map((e) => `${e.keywordLocation}: ${e.error}`)\n .join(\"\\n\")}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n // Assign the validated input to parsed\n // We cast here because validate() doesn't narrow the type sufficiently for TS, but we know it's valid.\n parsed = inputForValidation as SchemaOutputT;\n }\n\n const config = parseCallbackConfigArg(configArg);\n const callbackManager_ = CallbackManager.configure(\n config.callbacks,\n this.callbacks,\n config.tags || tags,\n this.tags,\n config.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n\n let toolCallId: string | undefined;\n // Extract toolCallId ONLY if the original arg was a ToolCall\n if (_isToolCall(arg)) {\n toolCallId = arg.id;\n }\n // Or if it was provided in the config's toolCall property\n if (!toolCallId && _configHasToolCallId(config)) {\n toolCallId = config.toolCall.id;\n }\n\n const runManager = await callbackManager_?.handleToolStart(\n this.toJSON(),\n // Log the original raw input arg\n typeof arg === \"string\" ? arg : JSON.stringify(arg),\n config.runId,\n undefined,\n undefined,\n undefined,\n config.runName,\n toolCallId\n );\n delete config.runId;\n\n let result;\n try {\n const raw = await this._call(parsed, runManager, config);\n result = isAsyncGenerator(raw)\n ? await consumeAsyncGenerator(raw, async (chunk) => {\n try {\n await runManager?.handleToolEvent(chunk);\n } catch (streamError) {\n await runManager?.handleToolError(streamError);\n }\n })\n : raw;\n } catch (e) {\n await runManager?.handleToolError(e);\n throw e;\n }\n\n let content;\n let artifact;\n if (this.responseFormat === \"content_and_artifact\") {\n if (Array.isArray(result) && result.length === 2) {\n [content, artifact] = result;\n } else {\n throw new Error(\n `Tool response format is \"content_and_artifact\" but the output was not a two-tuple.\\nResult: ${JSON.stringify(\n result\n )}`\n );\n }\n } else {\n content = result;\n }\n\n const formattedOutput = _formatToolOutput<ToolOutputT>({\n content,\n artifact,\n toolCallId,\n name: this.name,\n metadata: this.metadata,\n });\n await runManager?.handleToolEnd(formattedOutput);\n return formattedOutput as ToolReturnType<TArg, TConfig, ToolOutputT>;\n }\n}\n\n/**\n * Base class for Tools that accept input as a string.\n */\nexport abstract class Tool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends StructuredTool<\n StringInputToolSchema,\n ToolInputSchemaOutputType<StringInputToolSchema>,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT,\n ToolEventT\n >\n implements\n ToolInterface<\n StringInputToolSchema,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT\n >\n{\n schema = z\n .object({ input: z.string().optional() })\n .transform((obj) => obj.input);\n\n constructor(fields?: ToolParams) {\n super(fields);\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument and callbacks. It handles\n * string inputs specifically.\n * @param arg The input argument for the tool, which can be a string, undefined, or an input of the tool's schema.\n * @param callbacks Optional callbacks for the tool.\n * @returns A Promise that resolves with a string.\n */\n // Match the base class signature including the generics and conditional return type\n call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n callbacks?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n // Prepare the input for the base class call method.\n // If arg is string or undefined, wrap it; otherwise, pass ToolCall or { input: ... } directly.\n const structuredArg =\n typeof arg === \"string\" || arg == null ? { input: arg } : arg;\n\n // Ensure TConfig is passed to super.call\n return super.call(structuredArg, callbacks);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and description.\n */\nexport class DynamicTool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n> extends Tool<ToolOutputT, ToolEventT> {\n static lc_name() {\n return \"DynamicTool\";\n }\n\n name: string;\n\n description: string;\n\n func: DynamicToolInput<ToolOutputT, ToolEventT>[\"func\"];\n\n constructor(fields: DynamicToolInput<ToolOutputT, ToolEventT>) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n async call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n // Call the Tool class's call method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig);\n }\n\n /** @ignore */\n _call(\n input: string, // DynamicTool's _call specifically expects a string after schema transformation\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(input, runManager, parentConfig);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and\n * description, designed to work with structured data. It extends the\n * StructuredTool class and overrides the _call method to execute the\n * provided function when the tool is called.\n *\n * Schema can be passed as Zod or JSON schema. The tool will not validate\n * input if JSON schema is passed.\n *\n * @template SchemaT The input schema type for the tool (Zod schema or JSON schema). Defaults to `ToolInputSchemaBase`.\n * @template SchemaOutputT The output type derived from the schema after parsing/validation. Defaults to `ToolInputSchemaOutputType<SchemaT>`.\n * @template SchemaInputT The input type derived from the schema before parsing. Defaults to `ToolInputSchemaInputType<SchemaT>`.\n * @template ToolOutputT The return type of the tool's function. Defaults to `ToolOutputType`.\n * @template NameT The literal type of the tool name (for discriminated union support). Defaults to `string`.\n */\nexport class DynamicStructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n NameT extends string = string,\n> extends StructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT\n> {\n static lc_name() {\n return \"DynamicStructuredTool\";\n }\n\n declare name: NameT;\n\n description: string;\n\n func: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"];\n\n schema: SchemaT;\n\n constructor(\n fields: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n > & {\n name: NameT;\n }\n ) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n this.schema = fields.schema;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n // Match the base class signature\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n\n // Call the base class method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig, tags);\n }\n\n protected _call(\n arg: Parameters<\n DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"]\n >[0],\n runManager?: CallbackManagerForToolRun,\n parentConfig?: RunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(arg, runManager, parentConfig);\n }\n}\n\n/**\n * Abstract base class for toolkits in LangChain. Toolkits are collections\n * of tools that agents can use. Subclasses must implement the `tools`\n * property to provide the specific tools for the toolkit.\n */\nexport abstract class BaseToolkit {\n abstract tools: StructuredToolInterface[];\n\n getTools(): StructuredToolInterface[] {\n return this.tools;\n }\n}\n\n/**\n * Parameters for the tool function.\n * Schema can be provided as Zod or JSON schema.\n * Both schema types will be validated.\n * @template {ToolInputSchemaBase} RunInput The input schema for the tool.\n * @template {string} NameT The literal name type for discriminated union support.\n */\nexport interface ToolWrapperParams<\n RunInput = ToolInputSchemaBase | undefined,\n NameT extends string = string,\n> extends ToolParams {\n /**\n * The name of the tool. If using with an LLM, this\n * will be passed as the tool name.\n */\n name: NameT;\n /**\n * The description of the tool.\n * @default `${fields.name} tool`\n */\n description?: string;\n /**\n * The input schema for the tool. If using an LLM, this\n * will be passed as the tool schema to generate arguments\n * for.\n */\n schema?: RunInput;\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat;\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect?: boolean;\n}\n\n/**\n * Creates a new StructuredTool instance with the provided function, name, description, and schema.\n *\n * Schema can be provided as Zod or JSON schema, and both will be validated.\n *\n * @function\n * @template {ToolInputSchemaBase} SchemaT The input schema for the tool.\n * @template {ToolReturnType} ToolOutputT The output type of the tool.\n *\n * @param {RunnableFunc<z.output<SchemaT>, ToolOutputT>} func - The function to invoke when the tool is called.\n * @param {ToolWrapperParams<SchemaT>} fields - An object containing the following properties:\n * @param {string} fields.name The name of the tool.\n * @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.\n * @param {z.AnyZodObject | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.\n *\n * @returns {DynamicStructuredTool<SchemaT>} A new StructuredTool instance.\n */\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n >\n | DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\n// Overloads with ToolRuntime as CallOptions\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n >\n | DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >\n | DynamicTool<ToolOutputT, ToolEventT> {\n const isSimpleStringSchema = isSimpleStringZodSchema(fields.schema);\n const isStringJSONSchema = validatesOnlyStrings(fields.schema);\n\n // If the schema is not provided, or it's a simple string schema, create a DynamicTool\n if (!fields.schema || isSimpleStringSchema || isStringJSONSchema) {\n return new DynamicTool<ToolOutputT, ToolEventT>({\n ...fields,\n description:\n fields.description ??\n (fields.schema as { description?: string } | undefined)?.description ??\n `${fields.name} tool`,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(func(input as any, childConfig as any));\n } catch (e) {\n reject(e);\n }\n }\n );\n });\n },\n });\n }\n\n const schema = fields.schema as InteropZodObject | JSONSchema;\n\n const description =\n fields.description ??\n (fields.schema as { description?: string }).description ??\n `${fields.name} tool`;\n\n return new DynamicStructuredTool<\n typeof schema,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >({\n ...fields,\n description,\n schema,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n let listener: (() => void) | undefined;\n const cleanup = () => {\n if (config?.signal && listener) {\n config.signal.removeEventListener(\"abort\", listener);\n }\n };\n\n if (config?.signal) {\n listener = () => {\n cleanup();\n reject(getAbortSignalError(config.signal));\n };\n config.signal.addEventListener(\"abort\", listener, { once: true });\n }\n\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const result = await func(input as any, childConfig as any);\n if (isAsyncGenerator(result)) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(result as any);\n return;\n }\n\n /**\n * If the signal is aborted, we don't want to resolve the promise\n * as the promise is already rejected.\n */\n if (config?.signal?.aborted) {\n cleanup();\n return;\n }\n\n cleanup();\n resolve(result);\n } catch (e) {\n cleanup();\n reject(e);\n }\n }\n );\n });\n },\n }) as DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >;\n}\n\nfunction _formatToolOutput<TOutput extends ToolOutputType>(params: {\n content: TOutput;\n name: string;\n artifact?: unknown;\n toolCallId?: string;\n metadata?: Record<string, unknown>;\n}): ToolMessage | TOutput {\n const { content, artifact, toolCallId, metadata } = params;\n if (toolCallId && !isDirectToolOutput(content)) {\n if (\n typeof content === \"string\" ||\n (Array.isArray(content) &&\n content.every((item) => typeof item === \"object\"))\n ) {\n return new ToolMessage({\n status: \"success\",\n content,\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n } else {\n return new ToolMessage({\n status: \"success\",\n content: _stringify(content),\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n }\n } else {\n return content;\n }\n}\n\nfunction _stringify(content: unknown): string {\n try {\n return JSON.stringify(content) ?? \"\";\n } catch (_noOp) {\n return `${content}`;\n }\n}\n\nexport type ServerTool = Record<string, unknown>;\nexport type ClientTool =\n | StructuredToolInterface\n | DynamicTool\n | RunnableToolLike;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8FA,IAAsB,iBAAtB,cAOU,cAKV;;;;;;;CAaE;;;;;;;CAQA,eAAe;CAEf,uBAAuB;CAEvB,IAAI,eAAe;AACjB,SAAO,CAAC,aAAa,QAAQ;;;;;;;;;;;CAY/B,iBAAkC;;;;CAKlC;CAEA,YAAY,QAAqB;AAC/B,QAAM,UAAU,EAAE,CAAC;AAEnB,OAAK,uBACH,QAAQ,wBAAwB,KAAK;AACvC,OAAK,iBAAiB,QAAQ,kBAAkB,KAAK;AACrD,OAAK,gBAAgB,QAAQ,iBAAiB,KAAK;AACnD,OAAK,WAAW,QAA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;CACxB,MAAM,EAAE,SAAS,UAAU,YAAY,aAAa;AACpD,KAAI,cAAc,CAAC,mBAAmB,QAAQ,CAC5C,KACE,OAAO,YAAY,YAClB,MAAM,QAAQ,QAAQ,IACrB,QAAQ,OAAO,SAAS,OAAO,SAAS,SAAS,CAEnD,QAAO,IAAI,YAAY;EACrB,QAAQ;EACR;EACA;EACA,cAAc;EACd,MAAM,OAAO;EACb;EACD,CAAC;KAEF,QAAO,IAAI,YAAY;EACrB,QAAQ;EACR,SAAS,WAAW,QAAQ;EAC5B;EACA,cAAc;EACd,MAAM,OAAO;EACb;EACD,CAAC;KAGJ,QAAO;;AAIX,SAAS,WAAW,SAA0B;AAC5C,KAAI;AACF,SAAO,KAAK,UAAU,QAAQ,IAAI;UAC3B,OAAO;AACd,SAAO,GAAG"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["z4"],"sources":["../../src/tools/index.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { z as z4, ZodError } from \"zod/v4\";\nimport {\n validate,\n type Schema as ValidationSchema,\n} from \"@cfworker/json-schema\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n parseCallbackConfigArg,\n} from \"../callbacks/manager.js\";\nimport { BaseLangChain } from \"../language_models/base.js\";\nimport {\n mergeConfigs,\n ensureConfig,\n patchConfig,\n pickRunnableConfigKeys,\n type RunnableConfig,\n} from \"../runnables/config.js\";\nimport type { RunnableFunc } from \"../runnables/base.js\";\nimport { isDirectToolOutput, ToolCall, ToolMessage } from \"../messages/tool.js\";\nimport { AsyncLocalStorageProviderSingleton } from \"../singletons/index.js\";\nimport type { RunnableToolLike } from \"../runnables/base.js\";\nimport {\n _configHasToolCallId,\n _isToolCall,\n ToolInputParsingException,\n} from \"./utils.js\";\nimport {\n type InferInteropZodInput,\n type InferInteropZodOutput,\n type InteropZodObject,\n type InteropZodType,\n interopParseAsync,\n isSimpleStringZodSchema,\n isInteropZodError,\n isInteropZodSchema,\n type ZodStringV3,\n type ZodStringV4,\n type ZodObjectV3,\n type ZodObjectV4,\n} from \"../utils/types/zod.js\";\nimport { getAbortSignalError } from \"../utils/signal.js\";\nimport type {\n StructuredToolCallInput,\n ToolInputSchemaBase,\n ToolReturnType,\n ResponseFormat,\n ToolInputSchemaInputType,\n ToolInputSchemaOutputType,\n ToolParams,\n ToolRunnableConfig,\n StructuredToolInterface,\n DynamicToolInput,\n DynamicStructuredToolInput,\n StringInputToolSchema,\n ToolInterface,\n ToolOutputType,\n ToolRuntime,\n ToolEventType,\n InferToolEventFromFunc,\n InferToolOutputFromFunc,\n} from \"./types.js\";\nimport { type JSONSchema, validatesOnlyStrings } from \"../utils/json_schema.js\";\nimport { consumeAsyncGenerator, isAsyncGenerator } from \"../runnables/iter.js\";\n\nexport type {\n BaseDynamicToolInput,\n ContentAndArtifact,\n DynamicToolInput,\n DynamicStructuredToolInput,\n ResponseFormat,\n StructuredToolCallInput,\n StructuredToolInterface,\n StructuredToolParams,\n ToolInterface,\n ToolParams,\n ToolReturnType,\n ToolRunnableConfig,\n ToolInputSchemaBase as ToolSchemaBase,\n} from \"./types.js\";\n\nexport {\n isLangChainTool,\n isRunnableToolLike,\n isStructuredTool,\n isStructuredToolParams,\n type ToolRuntime,\n} from \"./types.js\";\n\nexport { ToolInputParsingException };\n/**\n * Base class for Tools that accept input of any shape defined by a Zod schema.\n */\nexport abstract class StructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends BaseLangChain<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolOutputT | ToolMessage\n >\n implements StructuredToolInterface<SchemaT, SchemaInputT, ToolOutputT>\n{\n abstract name: string;\n\n abstract description: string;\n\n abstract schema: SchemaT;\n\n /**\n * Optional provider-specific extra fields for the tool.\n *\n * This is used to pass provider-specific configuration that doesn't fit into\n * standard tool fields.\n */\n extras?: Record<string, unknown>;\n\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect = false;\n\n verboseParsingErrors = false;\n\n get lc_namespace() {\n return [\"langchain\", \"tools\"];\n }\n\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat = \"content\";\n\n /**\n * Default config object for the tool runnable.\n */\n defaultConfig?: ToolRunnableConfig;\n\n constructor(fields?: ToolParams) {\n super(fields ?? {});\n\n this.verboseParsingErrors =\n fields?.verboseParsingErrors ?? this.verboseParsingErrors;\n this.responseFormat = fields?.responseFormat ?? this.responseFormat;\n this.defaultConfig = fields?.defaultConfig ?? this.defaultConfig;\n this.metadata = fields?.metadata ?? this.metadata;\n this.extras = fields?.extras ?? this.extras;\n }\n\n protected abstract _call(\n arg: SchemaOutputT,\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT>;\n\n /**\n * Invokes the tool with the provided input and configuration.\n * @param input The input for the tool.\n * @param config Optional configuration for the tool.\n * @returns A Promise that resolves with the tool's output.\n */\n async invoke<\n TInput extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n input: TInput,\n config?: TConfig\n ): Promise<ToolReturnType<TInput, TConfig, ToolOutputT>> {\n let toolInput: Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n\n let enrichedConfig: ToolRunnableConfig = ensureConfig(\n mergeConfigs(this.defaultConfig, config)\n );\n if (_isToolCall(input)) {\n toolInput = input.args as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n enrichedConfig = {\n ...enrichedConfig,\n toolCall: input,\n };\n } else {\n toolInput = input as Exclude<\n StructuredToolCallInput<SchemaT, SchemaInputT>,\n ToolCall\n >;\n }\n\n return this.call(toolInput, enrichedConfig) as Promise<\n ToolReturnType<TInput, TConfig, ToolOutputT>\n >;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument, configuration, and tags. It\n * parses the input according to the schema, handles any errors, and\n * manages callbacks.\n * @param arg The input argument for the tool.\n * @param configArg Optional configuration or callbacks for the tool.\n * @param tags Optional tags for the tool.\n * @returns A Promise that resolves with a string.\n */\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<TArg, TConfig, ToolOutputT>> {\n // Determine the actual input that needs parsing/validation.\n // If arg is a ToolCall, use its args; otherwise, use arg directly.\n const inputForValidation = _isToolCall(arg) ? arg.args : arg;\n\n let parsed: SchemaOutputT; // This will hold the successfully parsed input of the expected output type.\n if (isInteropZodSchema(this.schema)) {\n try {\n // Validate the inputForValidation - TS needs help here as it can't exclude ToolCall based on the check\n parsed = await interopParseAsync(\n this.schema as InteropZodType,\n inputForValidation as Exclude<TArg, ToolCall>\n );\n } catch (e) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${(e as Error).message}`;\n }\n if (isInteropZodError(e)) {\n message = `${message}\\n\\n${z4.prettifyError(e as ZodError)}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n } else {\n const result = validate(\n inputForValidation,\n this.schema as ValidationSchema\n );\n if (!result.valid) {\n let message = `Received tool input did not match expected schema`;\n if (this.verboseParsingErrors) {\n message = `${message}\\nDetails: ${result.errors\n .map((e) => `${e.keywordLocation}: ${e.error}`)\n .join(\"\\n\")}`;\n }\n // Pass the original raw input arg to the exception\n throw new ToolInputParsingException(message, JSON.stringify(arg));\n }\n // Assign the validated input to parsed\n // We cast here because validate() doesn't narrow the type sufficiently for TS, but we know it's valid.\n parsed = inputForValidation as SchemaOutputT;\n }\n\n const config = parseCallbackConfigArg(configArg);\n const callbackManager_ = CallbackManager.configure(\n config.callbacks,\n this.callbacks,\n config.tags || tags,\n this.tags,\n config.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n\n let toolCallId: string | undefined;\n // Extract toolCallId ONLY if the original arg was a ToolCall\n if (_isToolCall(arg)) {\n toolCallId = arg.id;\n }\n // Or if it was provided in the config's toolCall property\n if (!toolCallId && _configHasToolCallId(config)) {\n toolCallId = config.toolCall.id;\n }\n\n const runManager = await callbackManager_?.handleToolStart(\n this.toJSON(),\n // Log the original raw input arg\n typeof arg === \"string\" ? arg : JSON.stringify(arg),\n config.runId,\n undefined,\n undefined,\n undefined,\n config.runName,\n toolCallId\n );\n delete config.runId;\n\n let result;\n try {\n const raw = await this._call(parsed, runManager, config);\n result = isAsyncGenerator(raw)\n ? await consumeAsyncGenerator(raw, async (chunk) => {\n try {\n await runManager?.handleToolEvent(chunk);\n } catch (streamError) {\n await runManager?.handleToolError(streamError);\n }\n })\n : raw;\n } catch (e) {\n await runManager?.handleToolError(e);\n throw e;\n }\n\n let content;\n let artifact;\n if (this.responseFormat === \"content_and_artifact\") {\n if (Array.isArray(result) && result.length === 2) {\n [content, artifact] = result;\n } else {\n throw new Error(\n `Tool response format is \"content_and_artifact\" but the output was not a two-tuple.\\nResult: ${JSON.stringify(\n result\n )}`\n );\n }\n } else {\n content = result;\n }\n\n const formattedOutput = _formatToolOutput<ToolOutputT>({\n content,\n artifact,\n toolCallId,\n name: this.name,\n metadata: this.metadata,\n });\n await runManager?.handleToolEnd(formattedOutput);\n return formattedOutput as ToolReturnType<TArg, TConfig, ToolOutputT>;\n }\n}\n\n/**\n * Base class for Tools that accept input as a string.\n */\nexport abstract class Tool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n>\n extends StructuredTool<\n StringInputToolSchema,\n ToolInputSchemaOutputType<StringInputToolSchema>,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT,\n ToolEventT\n >\n implements\n ToolInterface<\n StringInputToolSchema,\n ToolInputSchemaInputType<StringInputToolSchema>,\n ToolOutputT\n >\n{\n schema = z\n .object({ input: z.string().optional() })\n .transform((obj) => obj.input);\n\n constructor(fields?: ToolParams) {\n super(fields);\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n *\n * Calls the tool with the provided argument and callbacks. It handles\n * string inputs specifically.\n * @param arg The input argument for the tool, which can be a string, undefined, or an input of the tool's schema.\n * @param callbacks Optional callbacks for the tool.\n * @returns A Promise that resolves with a string.\n */\n // Match the base class signature including the generics and conditional return type\n call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n callbacks?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n // Prepare the input for the base class call method.\n // If arg is string or undefined, wrap it; otherwise, pass ToolCall or { input: ... } directly.\n const structuredArg =\n typeof arg === \"string\" || arg == null ? { input: arg } : arg;\n\n // Ensure TConfig is passed to super.call\n return super.call(structuredArg, callbacks);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and description.\n */\nexport class DynamicTool<\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n> extends Tool<ToolOutputT, ToolEventT> {\n static lc_name() {\n return \"DynamicTool\";\n }\n\n name: string;\n\n description: string;\n\n func: DynamicToolInput<ToolOutputT, ToolEventT>[\"func\"];\n\n constructor(fields: DynamicToolInput<ToolOutputT, ToolEventT>) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n async call<\n TArg extends string | undefined | z.input<this[\"schema\"]> | ToolCall,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n // Call the Tool class's call method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig);\n }\n\n /** @ignore */\n _call(\n input: string, // DynamicTool's _call specifically expects a string after schema transformation\n runManager?: CallbackManagerForToolRun,\n parentConfig?: ToolRunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(input, runManager, parentConfig);\n }\n}\n\n/**\n * A tool that can be created dynamically from a function, name, and\n * description, designed to work with structured data. It extends the\n * StructuredTool class and overrides the _call method to execute the\n * provided function when the tool is called.\n *\n * Schema can be passed as Zod or JSON schema. The tool will not validate\n * input if JSON schema is passed.\n *\n * @template SchemaT The input schema type for the tool (Zod schema or JSON schema). Defaults to `ToolInputSchemaBase`.\n * @template SchemaOutputT The output type derived from the schema after parsing/validation. Defaults to `ToolInputSchemaOutputType<SchemaT>`.\n * @template SchemaInputT The input type derived from the schema before parsing. Defaults to `ToolInputSchemaInputType<SchemaT>`.\n * @template ToolOutputT The return type of the tool's function. Defaults to `ToolOutputType`.\n * @template NameT The literal type of the tool name (for discriminated union support). Defaults to `string`.\n */\nexport class DynamicStructuredTool<\n SchemaT = ToolInputSchemaBase,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n NameT extends string = string,\n> extends StructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT\n> {\n static lc_name() {\n return \"DynamicStructuredTool\";\n }\n\n declare name: NameT;\n\n description: string;\n\n func: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"];\n\n schema: SchemaT;\n\n constructor(\n fields: DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n > & {\n name: NameT;\n }\n ) {\n super(fields);\n this.name = fields.name;\n this.description = fields.description;\n this.func = fields.func;\n this.returnDirect = fields.returnDirect ?? this.returnDirect;\n this.schema = fields.schema;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.3.0.\n */\n // Match the base class signature\n async call<\n TArg extends StructuredToolCallInput<SchemaT, SchemaInputT>,\n TConfig extends ToolRunnableConfig | undefined,\n >(\n arg: TArg,\n configArg?: TConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<ToolReturnType<NonNullable<TArg>, TConfig, ToolOutputT>> {\n const config = parseCallbackConfigArg(configArg);\n if (config.runName === undefined) {\n config.runName = this.name;\n }\n\n // Call the base class method, passing generics through\n // Cast config to TConfig to satisfy the super.call signature\n return super.call<TArg, TConfig>(arg, config as TConfig, tags);\n }\n\n protected _call(\n arg: Parameters<\n DynamicStructuredToolInput<\n SchemaT,\n SchemaOutputT,\n ToolOutputT,\n ToolEventT\n >[\"func\"]\n >[0],\n runManager?: CallbackManagerForToolRun,\n parentConfig?: RunnableConfig\n ): Promise<ToolOutputT> | AsyncGenerator<ToolEventT, ToolOutputT> {\n return this.func(arg, runManager, parentConfig);\n }\n}\n\n/**\n * Abstract base class for toolkits in LangChain. Toolkits are collections\n * of tools that agents can use. Subclasses must implement the `tools`\n * property to provide the specific tools for the toolkit.\n */\nexport abstract class BaseToolkit {\n abstract tools: StructuredToolInterface[];\n\n getTools(): StructuredToolInterface[] {\n return this.tools;\n }\n}\n\n/**\n * Parameters for the tool function.\n * Schema can be provided as Zod or JSON schema.\n * Both schema types will be validated.\n * @template {ToolInputSchemaBase} RunInput The input schema for the tool.\n * @template {string} NameT The literal name type for discriminated union support.\n */\nexport interface ToolWrapperParams<\n RunInput = ToolInputSchemaBase | undefined,\n NameT extends string = string,\n> extends ToolParams {\n /**\n * The name of the tool. If using with an LLM, this\n * will be passed as the tool name.\n */\n name: NameT;\n /**\n * The description of the tool.\n * @default `${fields.name} tool`\n */\n description?: string;\n /**\n * The input schema for the tool. If using an LLM, this\n * will be passed as the tool schema to generate arguments\n * for.\n */\n schema?: RunInput;\n /**\n * The tool response format.\n *\n * If \"content\" then the output of the tool is interpreted as the contents of a\n * ToolMessage. If \"content_and_artifact\" then the output is expected to be a\n * two-tuple corresponding to the (content, artifact) of a ToolMessage.\n *\n * @default \"content\"\n */\n responseFormat?: ResponseFormat;\n /**\n * Whether to return the tool's output directly.\n *\n * Setting this to true means that after the tool is called,\n * an agent should stop looping.\n */\n returnDirect?: boolean;\n}\n\n/**\n * Creates a new StructuredTool instance with the provided function, name, description, and schema.\n *\n * Schema can be provided as Zod or JSON schema, and both will be validated.\n *\n * @function\n * @template {ToolInputSchemaBase} SchemaT The input schema for the tool.\n * @template {ToolReturnType} ToolOutputT The output type of the tool.\n *\n * @param {RunnableFunc<z.output<SchemaT>, ToolOutputT>} func - The function to invoke when the tool is called.\n * @param {ToolWrapperParams<SchemaT>} fields - An object containing the following properties:\n * @param {string} fields.name The name of the tool.\n * @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.\n * @param {z.AnyZodObject | z.ZodString | undefined} fields.schema The Zod schema defining the input for the tool. If undefined, it will default to a Zod string schema.\n *\n * @returns {DynamicStructuredTool<SchemaT>} A new StructuredTool instance.\n */\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n InferInteropZodOutput<SchemaT>,\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n > = RunnableFunc<\n Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n ToolOutputT,\n ToolRunnableConfig\n >,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n FuncT extends RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig> =\n RunnableFunc<SchemaOutputT, ToolOutputT, ToolRunnableConfig>,\n>(\n func: FuncT,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n InferToolOutputFromFunc<FuncT>,\n InferToolEventFromFunc<FuncT>,\n NameT\n >\n | DynamicTool<InferToolOutputFromFunc<FuncT>, InferToolEventFromFunc<FuncT>>;\n\n// Overloads with ToolRuntime as CallOptions\nexport function tool<\n SchemaT extends ZodStringV3,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodStringV4,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: InferInteropZodOutput<SchemaT>,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT>\n): DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends ZodObjectV3,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends ZodObjectV4,\n NameT extends string,\n SchemaOutputT = InferInteropZodOutput<SchemaT>,\n SchemaInputT = InferInteropZodInput<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends JSONSchema,\n NameT extends string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: Parameters<DynamicStructuredToolInput<SchemaT>[\"func\"]>[0],\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n): DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventType,\n NameT\n >\n | DynamicTool<ToolOutputT>;\n\nexport function tool<\n SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =\n InteropZodObject,\n NameT extends string = string,\n SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,\n SchemaInputT = ToolInputSchemaInputType<SchemaT>,\n ToolOutputT = ToolOutputType,\n ToolEventT = ToolEventType,\n TState = unknown,\n TContext = unknown,\n>(\n func: (\n input: SchemaOutputT,\n runtime: ToolRuntime<TState, TContext>\n ) => ToolOutputT | Promise<ToolOutputT>,\n fields: ToolWrapperParams<SchemaT, NameT>\n):\n | DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >\n | DynamicTool<ToolOutputT, ToolEventT> {\n const isSimpleStringSchema = isSimpleStringZodSchema(fields.schema);\n const isStringJSONSchema = validatesOnlyStrings(fields.schema);\n\n // If the schema is not provided, or it's a simple string schema, create a DynamicTool\n if (!fields.schema || isSimpleStringSchema || isStringJSONSchema) {\n return new DynamicTool<ToolOutputT, ToolEventT>({\n ...fields,\n description:\n fields.description ??\n (fields.schema as { description?: string } | undefined)?.description ??\n `${fields.name} tool`,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(func(input as any, childConfig as any));\n } catch (e) {\n reject(e);\n }\n }\n );\n });\n },\n });\n }\n\n const schema = fields.schema as InteropZodObject | JSONSchema;\n\n const description =\n fields.description ??\n (fields.schema as { description?: string }).description ??\n `${fields.name} tool`;\n\n return new DynamicStructuredTool<\n typeof schema,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >({\n ...fields,\n description,\n schema,\n func: async (input, runManager, config) => {\n return new Promise<ToolOutputT>((resolve, reject) => {\n let listener: (() => void) | undefined;\n const cleanup = () => {\n if (config?.signal && listener) {\n config.signal.removeEventListener(\"abort\", listener);\n }\n };\n\n if (config?.signal) {\n listener = () => {\n cleanup();\n reject(getAbortSignalError(config.signal));\n };\n config.signal.addEventListener(\"abort\", listener, { once: true });\n }\n\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const result = await func(input as any, childConfig as any);\n if (isAsyncGenerator(result)) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n resolve(result as any);\n return;\n }\n\n /**\n * If the signal is aborted, we don't want to resolve the promise\n * as the promise is already rejected.\n */\n if (config?.signal?.aborted) {\n cleanup();\n return;\n }\n\n cleanup();\n resolve(result);\n } catch (e) {\n cleanup();\n reject(e);\n }\n }\n );\n });\n },\n }) as DynamicStructuredTool<\n SchemaT,\n SchemaOutputT,\n SchemaInputT,\n ToolOutputT,\n ToolEventT,\n NameT\n >;\n}\n\nfunction _isMessageContentBlockShaped(item: unknown) {\n return typeof item === \"object\" && item !== null && \"type\" in item;\n}\n\nfunction _formatToolOutput<TOutput extends ToolOutputType>(params: {\n content: TOutput;\n name: string;\n artifact?: unknown;\n toolCallId?: string;\n metadata?: Record<string, unknown>;\n}): ToolMessage | TOutput {\n const { content, artifact, toolCallId, metadata } = params;\n if (toolCallId && !isDirectToolOutput(content)) {\n if (\n typeof content === \"string\" ||\n (Array.isArray(content) && content.every(_isMessageContentBlockShaped))\n ) {\n return new ToolMessage({\n status: \"success\",\n content,\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n } else {\n return new ToolMessage({\n status: \"success\",\n content: _stringify(content),\n artifact,\n tool_call_id: toolCallId,\n name: params.name,\n metadata,\n });\n }\n } else {\n return content;\n }\n}\n\nfunction _stringify(content: unknown): string {\n try {\n return JSON.stringify(content) ?? \"\";\n } catch (_noOp) {\n return `${content}`;\n }\n}\n\nexport type ServerTool = Record<string, unknown>;\nexport type ClientTool =\n | StructuredToolInterface\n | DynamicTool\n | RunnableToolLike;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8FA,IAAsB,iBAAtB,cAOU,cAKV;;;;;;;CAaE;;;;;;;CAQA,eAAe;CAEf,uBAAuB;CAEvB,IAAI,eAAe;AACjB,SAAO,CAAC,aAAa,QAAQ;;;;;;;;;;;CAY/B,iBAAkC;;;;CAKlC;CAEA,YAAY,QAAqB;AAC/B,QAAM,UAAU,EAAE,CAAC;AAEnB,OAAK,uBACH,QAAQ,wBAAwB,KAAK;AACvC,OAAK,iBAAiB,QAAQ,kBAAkB,KAAK;AACrD,OAAK,gBAAgB,QAAQ,iBAAiB,KAAK;AACnD,OAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,OAAK,SAAS,QAAQ,UAAU,KAAK;;;;;;;;CAevC,MAAM,OAIJ,OACA,QACuD;EACvD,IAAI;EAKJ,IAAI,iBAAqC,aACvC,aAAa,KAAK,eAAe,OAAO,CACzC;AACD,MAAI,YAAY,MAAM,EAAE;AACtB,eAAY,MAAM;AAIlB,oBAAiB;IACf,GAAG;IACH,UAAU;IACX;QAED,aAAY;AAMd,SAAO,KAAK,KAAK,WAAW,eAAe;;;;;;;;;;;;;CA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;EACb;EACD,CAAC;KAEF,QAAO,IAAI,YAAY;EACrB,QAAQ;EACR,SAAS,WAAW,QAAQ;EAC5B;EACA,cAAc;EACd,MAAM,OAAO;EACb;EACD,CAAC;KAGJ,QAAO;;AAIX,SAAS,WAAW,SAA0B;AAC5C,KAAI;AACF,SAAO,KAAK,UAAU,QAAQ,IAAI;UAC3B,OAAO;AACd,SAAO,GAAG"}
|
|
@@ -5,7 +5,11 @@ const require_index = require("./p-retry/index.cjs");
|
|
|
5
5
|
let p_queue = require("p-queue");
|
|
6
6
|
p_queue = require_runtime.__toESM(p_queue, 1);
|
|
7
7
|
//#region src/utils/async_caller.ts
|
|
8
|
-
var async_caller_exports = /* @__PURE__ */ require_runtime.__exportAll({
|
|
8
|
+
var async_caller_exports = /* @__PURE__ */ require_runtime.__exportAll({
|
|
9
|
+
AsyncCaller: () => AsyncCaller,
|
|
10
|
+
classifyRateLimitError: () => classifyRateLimitError,
|
|
11
|
+
parseRetryAfterMs: () => parseRetryAfterMs
|
|
12
|
+
});
|
|
9
13
|
const STATUS_NO_RETRY = [
|
|
10
14
|
400,
|
|
11
15
|
401,
|
|
@@ -17,6 +21,110 @@ const STATUS_NO_RETRY = [
|
|
|
17
21
|
407,
|
|
18
22
|
409
|
|
19
23
|
];
|
|
24
|
+
const RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 6e4;
|
|
25
|
+
const QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [
|
|
26
|
+
/insufficient[_ -]?quota/i,
|
|
27
|
+
/exceeded (?:your|the current|the available).+quota/i,
|
|
28
|
+
/usage quota/i,
|
|
29
|
+
/quota (?:has been )?exhausted/i,
|
|
30
|
+
/billing/i,
|
|
31
|
+
/credit balance/i,
|
|
32
|
+
/out of credits/i,
|
|
33
|
+
/will reset at/i
|
|
34
|
+
];
|
|
35
|
+
const RETRY_AFTER_MESSAGE_PATTERN = /(?:try again in|retry after)\s+(\d+(?:\.\d+)?)\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\b/i;
|
|
36
|
+
function getResponseStatus(error) {
|
|
37
|
+
return typeof error === "object" && error !== null && "response" in error && typeof error.response === "object" && error.response !== null && "status" in error.response && typeof error.response.status === "number" ? error.response.status : void 0;
|
|
38
|
+
}
|
|
39
|
+
function getDirectStatus(error) {
|
|
40
|
+
if (typeof error !== "object" || error === null) return;
|
|
41
|
+
if ("status" in error && typeof error.status === "number") return error.status;
|
|
42
|
+
if ("statusCode" in error && typeof error.statusCode === "number") return error.statusCode;
|
|
43
|
+
}
|
|
44
|
+
function getErrorMessage(error) {
|
|
45
|
+
return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string" ? error.message : void 0;
|
|
46
|
+
}
|
|
47
|
+
function getErrorCode(error) {
|
|
48
|
+
if (typeof error !== "object" || error === null) return;
|
|
49
|
+
if ("code" in error && typeof error.code === "string") return error.code;
|
|
50
|
+
return "error" in error && typeof error.error === "object" && error.error !== null && "code" in error.error && typeof error.error.code === "string" ? error.error.code : void 0;
|
|
51
|
+
}
|
|
52
|
+
function _getRetryAfterHeader(error) {
|
|
53
|
+
if (error?.headers) {
|
|
54
|
+
if (typeof error.headers.get === "function") return error.headers.get("retry-after");
|
|
55
|
+
return error.headers["retry-after"] ?? error.headers["Retry-After"];
|
|
56
|
+
}
|
|
57
|
+
if (error?.response?.headers) {
|
|
58
|
+
if (typeof error.response.headers.get === "function") return error.response.headers.get("retry-after");
|
|
59
|
+
return error.response.headers["retry-after"] ?? error.response.headers["Retry-After"];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function parseRetryAfterFromMessageMs(message) {
|
|
63
|
+
if (message == null) return;
|
|
64
|
+
const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);
|
|
65
|
+
if (!match) return;
|
|
66
|
+
const rawValue = Number(match[1]);
|
|
67
|
+
const unit = match[2]?.toLowerCase();
|
|
68
|
+
if (Number.isNaN(rawValue) || !unit) return;
|
|
69
|
+
if (unit === "ms" || unit.startsWith("millisecond")) return rawValue;
|
|
70
|
+
if (unit === "m" || unit.startsWith("min")) return rawValue * 6e4;
|
|
71
|
+
if (unit === "h" || unit.startsWith("hr") || unit.startsWith("hour")) return rawValue * 36e5;
|
|
72
|
+
return rawValue * 1e3;
|
|
73
|
+
}
|
|
74
|
+
function coerceError(error, fallbackMessage) {
|
|
75
|
+
if (error instanceof Error) return error;
|
|
76
|
+
const coerced = new Error(fallbackMessage);
|
|
77
|
+
if (typeof error === "object" && error !== null) Object.assign(coerced, error);
|
|
78
|
+
return coerced;
|
|
79
|
+
}
|
|
80
|
+
function setRateLimitMetadata(error, classification) {
|
|
81
|
+
if (typeof error !== "object" || error === null) return;
|
|
82
|
+
const mutableError = error;
|
|
83
|
+
mutableError.rateLimitType = classification.action;
|
|
84
|
+
mutableError.rateLimitReason = classification.reason;
|
|
85
|
+
if (classification.retryAfterMs !== void 0) mutableError.retryAfterMs = classification.retryAfterMs;
|
|
86
|
+
}
|
|
87
|
+
function parseRetryAfterMs(headerValue) {
|
|
88
|
+
if (headerValue == null) return;
|
|
89
|
+
const trimmed = headerValue.trim();
|
|
90
|
+
if (!trimmed) return;
|
|
91
|
+
const seconds = Number(trimmed);
|
|
92
|
+
if (!Number.isNaN(seconds) && seconds >= 0) return seconds * 1e3;
|
|
93
|
+
const date = Date.parse(trimmed);
|
|
94
|
+
if (!Number.isNaN(date)) {
|
|
95
|
+
const delayMs = date - Date.now();
|
|
96
|
+
return delayMs > 0 ? delayMs : 0;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function classifyRateLimitError(error) {
|
|
100
|
+
if ((getResponseStatus(error) ?? getDirectStatus(error)) !== 429) return;
|
|
101
|
+
if (getErrorCode(error) === "insufficient_quota") return {
|
|
102
|
+
action: "stop",
|
|
103
|
+
reason: "insufficient_quota"
|
|
104
|
+
};
|
|
105
|
+
const message = getErrorMessage(error);
|
|
106
|
+
if (message && QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))) return {
|
|
107
|
+
action: "stop",
|
|
108
|
+
reason: "quota_message"
|
|
109
|
+
};
|
|
110
|
+
const retryAfterMs = parseRetryAfterMs(_getRetryAfterHeader(error)) ?? parseRetryAfterFromMessageMs(message);
|
|
111
|
+
if (retryAfterMs !== void 0) {
|
|
112
|
+
if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) return {
|
|
113
|
+
action: "wait",
|
|
114
|
+
retryAfterMs,
|
|
115
|
+
reason: "retry_after_hint"
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
action: "capacity",
|
|
119
|
+
retryAfterMs,
|
|
120
|
+
reason: "retry_after_too_large"
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
action: "capacity",
|
|
125
|
+
reason: "headerless_429"
|
|
126
|
+
};
|
|
127
|
+
}
|
|
20
128
|
/**
|
|
21
129
|
* The default failed attempt handler for the AsyncCaller.
|
|
22
130
|
* @param error - The error to handle.
|
|
@@ -26,13 +134,26 @@ const defaultFailedAttemptHandler = (error) => {
|
|
|
26
134
|
if (typeof error !== "object" || error === null) return;
|
|
27
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;
|
|
28
136
|
if ("code" in error && typeof error.code === "string" && error.code === "ECONNABORTED") throw error;
|
|
29
|
-
const
|
|
30
|
-
const directStatus = "status" in error && typeof error.status === "number" ? error.status : void 0;
|
|
31
|
-
const status = responseStatus ?? directStatus;
|
|
137
|
+
const status = getResponseStatus(error) ?? getDirectStatus(error);
|
|
32
138
|
if (status && STATUS_NO_RETRY.includes(+status)) throw error;
|
|
33
|
-
if ((
|
|
34
|
-
const err =
|
|
139
|
+
if (getErrorCode(error) === "insufficient_quota") {
|
|
140
|
+
const err = coerceError(error, getErrorMessage(error) ?? "Insufficient quota");
|
|
35
141
|
err.name = "InsufficientQuotaError";
|
|
142
|
+
setRateLimitMetadata(err, {
|
|
143
|
+
action: "stop",
|
|
144
|
+
reason: "insufficient_quota"
|
|
145
|
+
});
|
|
146
|
+
throw err;
|
|
147
|
+
}
|
|
148
|
+
const rateLimitClassification = classifyRateLimitError(error);
|
|
149
|
+
if (rateLimitClassification) {
|
|
150
|
+
if (rateLimitClassification.action === "wait") {
|
|
151
|
+
setRateLimitMetadata(error, rateLimitClassification);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const err = coerceError(error, getErrorMessage(error) ?? "Rate limit exceeded");
|
|
155
|
+
if (err.name === "Error") err.name = rateLimitClassification.action === "stop" ? "RateLimitQuotaExhaustedError" : "RateLimitCapacityError";
|
|
156
|
+
setRateLimitMetadata(err, rateLimitClassification);
|
|
36
157
|
throw err;
|
|
37
158
|
}
|
|
38
159
|
};
|
|
@@ -97,5 +218,7 @@ Object.defineProperty(exports, "async_caller_exports", {
|
|
|
97
218
|
return async_caller_exports;
|
|
98
219
|
}
|
|
99
220
|
});
|
|
221
|
+
exports.classifyRateLimitError = classifyRateLimitError;
|
|
222
|
+
exports.parseRetryAfterMs = parseRetryAfterMs;
|
|
100
223
|
|
|
101
224
|
//# sourceMappingURL=async_caller.cjs.map
|
|
@@ -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\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 responseStatus =\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 // OpenAI SDK errors expose status directly on the error object\n const directStatus =\n \"status\" in error && typeof error.status === \"number\"\n ? error.status\n : undefined;\n\n const status = responseStatus ?? directStatus;\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code =\n \"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 if (code === \"insufficient_quota\") {\n const err = new Error(\n \"message\" in error && typeof error.message === \"string\"\n ? error.message\n : \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\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;CACD;;;;;;AAOD,MAAM,+BAA+B,UAAmB;AACtD,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC;AAGF,KACG,aAAa,SACZ,OAAO,MAAM,YAAY,aACxB,MAAM,QAAQ,WAAW,SAAS,IACjC,MAAM,QAAQ,WAAW,aAAa,KACzC,UAAU,SACT,OAAO,MAAM,SAAS,YACtB,MAAM,SAAS,aAEjB,OAAM;AAER,KACE,UAAU,SACV,OAAO,MAAM,SAAS,YACtB,MAAM,SAAS,eAEf,OAAM;CAER,MAAM,iBACJ,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WAAW,WAC7B,MAAM,SAAS,SACf,KAAA;CAGN,MAAM,eACJ,YAAY,SAAS,OAAO,MAAM,WAAW,WACzC,MAAM,SACN,KAAA;CAEN,MAAM,SAAS,kBAAkB;AACjC,KAAI,UAAU,gBAAgB,SAAS,CAAC,OAAO,CAC7C,OAAM;AAWR,MAPE,WAAW,SACX,OAAO,MAAM,UAAU,YACvB,MAAM,UAAU,QAChB,UAAU,MAAM,SAChB,OAAO,MAAM,MAAM,SAAS,WACxB,MAAM,MAAM,OACZ,KAAA,OACO,sBAAsB;EACjC,MAAM,MAAM,IAAI,MACd,aAAa,SAAS,OAAO,MA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AK,MAAM,QAAQ,OAAO,IAAI,CAAE,CACnE"}
|
|
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;CACD;AAED,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;AAC7D,QAAO,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;;AAGN,SAAS,gBAAgB,OAAoC;AAC3D,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC;AAGF,KAAI,YAAY,SAAS,OAAO,MAAM,WAAW,SAC/C,QAAO,MAAM;AAGf,KAAI,gBAAgB,SAAS,OAAO,MAAM,eAAe,SACvD,QAAO,MAAM;;AAMjB,SAAS,gBAAgB,OAAoC;AAC3D,QAAO,OAAO,UAAU,YACtB,UAAU,QACV,aAAa,SACb,OAAO,MAAM,YAAY,WACvB,MAAM,UACN,KAAA;;AAGN,SAAS,aAAa,OAAoC;AACxD,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC;AAGF,KAAI,UAAU,SAAS,OAAO,MAAM,SAAS,SAC3C,QAAO,MAAM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AY,KAAK,kBAAkB,MAAM;GAC7D,SAAS,KAAK;GACd,WAAW;GAGZ,CACF,EACH,EAAE,gBAAgB,MAAM,CACzB;;CAIH,gBACE,SACA,UACA,GAAG,MAC8B;AAGjC,MAAI,QAAQ,QAAQ;GAClB,IAAI;AACJ,UAAO,QAAQ,KAAK,CAClB,KAAK,KAAW,UAAU,GAAG,KAAK,EAClC,IAAI,SAAgB,GAAG,WAAW;AAChC,qBAAiB;AACf,YAAOC,eAAAA,oBAAoB,QAAQ,OAAO,CAAC;;AAE7C,YAAQ,QAAQ,iBAAiB,SAAS,UAAU,EAAE,MAAM,MAAM,CAAC;KACnE,CACH,CAAC,CAAC,cAAc;AACf,QAAI,QAAQ,UAAU,SACpB,SAAQ,OAAO,oBAAoB,SAAS,SAAS;KAEvD;;AAEJ,SAAO,KAAK,KAAW,UAAU,GAAG,KAAK;;CAG3C,MAAM,GAAG,MAA0D;AACjE,SAAO,KAAK,WACV,MAAM,GAAG,KAAK,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,IAAI,CAAE,CACnE"}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
//#region src/utils/async_caller.d.ts
|
|
2
|
+
type RateLimitAction = "wait" | "capacity" | "stop";
|
|
3
|
+
type RateLimitClassification = {
|
|
4
|
+
action: RateLimitAction;
|
|
5
|
+
retryAfterMs?: number;
|
|
6
|
+
reason: string;
|
|
7
|
+
};
|
|
8
|
+
declare function parseRetryAfterMs(headerValue: string | null | undefined): number | undefined;
|
|
9
|
+
declare function classifyRateLimitError(error: unknown): RateLimitClassification | undefined;
|
|
2
10
|
type FailedAttemptHandler = (error: any) => any;
|
|
3
11
|
interface AsyncCallerParams {
|
|
4
12
|
/**
|
|
@@ -45,5 +53,5 @@ declare class AsyncCaller {
|
|
|
45
53
|
fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
|
|
46
54
|
}
|
|
47
55
|
//#endregion
|
|
48
|
-
export { AsyncCaller, AsyncCallerCallOptions, AsyncCallerParams, FailedAttemptHandler };
|
|
56
|
+
export { AsyncCaller, AsyncCallerCallOptions, AsyncCallerParams, FailedAttemptHandler, classifyRateLimitError, parseRetryAfterMs };
|
|
49
57
|
//# sourceMappingURL=async_caller.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.d.cts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"async_caller.d.cts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAiCK,eAAA;AAAA,KAEA,uBAAA;EACH,MAAA,EAAQ,eAAA;EACR,YAAA;EACA,MAAA;AAAA;AAAA,iBA8Ic,iBAAA,CACd,WAAA;AAAA,iBAyBc,sBAAA,CACd,KAAA,YACC,uBAAA;AAAA,KAgHS,oBAAA,IAAwB,KAAA;AAAA,UAEnB,iBAAA;EA9Rf;;;;EAmSA,cAAA;EAjSM;AA8IR;;;EAwJE,UAAA;EAvJsC;AAyBxC;;;;EAoIE,eAAA,GAAkB,oBAAA;AAAA;AAAA,UAGH,sBAAA;EACf,MAAA,GAAS,WAAA;AAAA;;AApBX;;;;;;;;;;AAmBA;;cAiBa,WAAA;EAAA,UACD,cAAA,EAAgB,iBAAA;EAAA,UAEhB,UAAA,EAAY,iBAAA;EAAA,UAEZ,eAAA,EAAiB,iBAAA;EAAA,QAEnB,KAAA;EAER,WAAA,CAAY,MAAA,EAAQ,iBAAA;EAad,IAAA,gCAAoC,IAAA,EAAM,CAAA,KAAM,OAAA,MAAA,CACpD,QAAA,EAAU,CAAA,KACP,IAAA,EAAM,UAAA,CAAW,CAAA,IACnB,OAAA,CAAQ,OAAA,CAAQ,UAAA,CAAW,CAAA;EA0B9B,eAAA,gCAA+C,IAAA,EAAM,CAAA,KAAM,OAAA,MAAA,CACzD,OAAA,EAAS,sBAAA,EACT,QAAA,EAAU,CAAA,KACP,IAAA,EAAM,UAAA,CAAW,CAAA,IACnB,OAAA,CAAQ,OAAA,CAAQ,UAAA,CAAW,CAAA;EAsB9B,KAAA,CAAA,GAAS,IAAA,EAAM,UAAA,QAAkB,KAAA,IAAS,UAAA,QAAkB,KAAA;AAAA"}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
//#region src/utils/async_caller.d.ts
|
|
2
|
+
type RateLimitAction = "wait" | "capacity" | "stop";
|
|
3
|
+
type RateLimitClassification = {
|
|
4
|
+
action: RateLimitAction;
|
|
5
|
+
retryAfterMs?: number;
|
|
6
|
+
reason: string;
|
|
7
|
+
};
|
|
8
|
+
declare function parseRetryAfterMs(headerValue: string | null | undefined): number | undefined;
|
|
9
|
+
declare function classifyRateLimitError(error: unknown): RateLimitClassification | undefined;
|
|
2
10
|
type FailedAttemptHandler = (error: any) => any;
|
|
3
11
|
interface AsyncCallerParams {
|
|
4
12
|
/**
|
|
@@ -45,5 +53,5 @@ declare class AsyncCaller {
|
|
|
45
53
|
fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
|
|
46
54
|
}
|
|
47
55
|
//#endregion
|
|
48
|
-
export { AsyncCaller, AsyncCallerCallOptions, AsyncCallerParams, FailedAttemptHandler };
|
|
56
|
+
export { AsyncCaller, AsyncCallerCallOptions, AsyncCallerParams, FailedAttemptHandler, classifyRateLimitError, parseRetryAfterMs };
|
|
49
57
|
//# sourceMappingURL=async_caller.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.d.ts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"async_caller.d.ts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAiCK,eAAA;AAAA,KAEA,uBAAA;EACH,MAAA,EAAQ,eAAA;EACR,YAAA;EACA,MAAA;AAAA;AAAA,iBA8Ic,iBAAA,CACd,WAAA;AAAA,iBAyBc,sBAAA,CACd,KAAA,YACC,uBAAA;AAAA,KAgHS,oBAAA,IAAwB,KAAA;AAAA,UAEnB,iBAAA;EA9Rf;;;;EAmSA,cAAA;EAjSM;AA8IR;;;EAwJE,UAAA;EAvJsC;AAyBxC;;;;EAoIE,eAAA,GAAkB,oBAAA;AAAA;AAAA,UAGH,sBAAA;EACf,MAAA,GAAS,WAAA;AAAA;;AApBX;;;;;;;;;;AAmBA;;cAiBa,WAAA;EAAA,UACD,cAAA,EAAgB,iBAAA;EAAA,UAEhB,UAAA,EAAY,iBAAA;EAAA,UAEZ,eAAA,EAAiB,iBAAA;EAAA,QAEnB,KAAA;EAER,WAAA,CAAY,MAAA,EAAQ,iBAAA;EAad,IAAA,gCAAoC,IAAA,EAAM,CAAA,KAAM,OAAA,MAAA,CACpD,QAAA,EAAU,CAAA,KACP,IAAA,EAAM,UAAA,CAAW,CAAA,IACnB,OAAA,CAAQ,OAAA,CAAQ,UAAA,CAAW,CAAA;EA0B9B,eAAA,gCAA+C,IAAA,EAAM,CAAA,KAAM,OAAA,MAAA,CACzD,OAAA,EAAS,sBAAA,EACT,QAAA,EAAU,CAAA,KACP,IAAA,EAAM,UAAA,CAAW,CAAA,IACnB,OAAA,CAAQ,OAAA,CAAQ,UAAA,CAAW,CAAA;EAsB9B,KAAA,CAAA,GAAS,IAAA,EAAM,UAAA,QAAkB,KAAA,IAAS,UAAA,QAAkB,KAAA;AAAA"}
|
|
@@ -3,7 +3,11 @@ import { getAbortSignalError } from "./signal.js";
|
|
|
3
3
|
import pRetry from "./p-retry/index.js";
|
|
4
4
|
import PQueueMod from "p-queue";
|
|
5
5
|
//#region src/utils/async_caller.ts
|
|
6
|
-
var async_caller_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
var async_caller_exports = /* @__PURE__ */ __exportAll({
|
|
7
|
+
AsyncCaller: () => AsyncCaller,
|
|
8
|
+
classifyRateLimitError: () => classifyRateLimitError,
|
|
9
|
+
parseRetryAfterMs: () => parseRetryAfterMs
|
|
10
|
+
});
|
|
7
11
|
const STATUS_NO_RETRY = [
|
|
8
12
|
400,
|
|
9
13
|
401,
|
|
@@ -15,6 +19,110 @@ const STATUS_NO_RETRY = [
|
|
|
15
19
|
407,
|
|
16
20
|
409
|
|
17
21
|
];
|
|
22
|
+
const RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 6e4;
|
|
23
|
+
const QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [
|
|
24
|
+
/insufficient[_ -]?quota/i,
|
|
25
|
+
/exceeded (?:your|the current|the available).+quota/i,
|
|
26
|
+
/usage quota/i,
|
|
27
|
+
/quota (?:has been )?exhausted/i,
|
|
28
|
+
/billing/i,
|
|
29
|
+
/credit balance/i,
|
|
30
|
+
/out of credits/i,
|
|
31
|
+
/will reset at/i
|
|
32
|
+
];
|
|
33
|
+
const RETRY_AFTER_MESSAGE_PATTERN = /(?:try again in|retry after)\s+(\d+(?:\.\d+)?)\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\b/i;
|
|
34
|
+
function getResponseStatus(error) {
|
|
35
|
+
return typeof error === "object" && error !== null && "response" in error && typeof error.response === "object" && error.response !== null && "status" in error.response && typeof error.response.status === "number" ? error.response.status : void 0;
|
|
36
|
+
}
|
|
37
|
+
function getDirectStatus(error) {
|
|
38
|
+
if (typeof error !== "object" || error === null) return;
|
|
39
|
+
if ("status" in error && typeof error.status === "number") return error.status;
|
|
40
|
+
if ("statusCode" in error && typeof error.statusCode === "number") return error.statusCode;
|
|
41
|
+
}
|
|
42
|
+
function getErrorMessage(error) {
|
|
43
|
+
return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string" ? error.message : void 0;
|
|
44
|
+
}
|
|
45
|
+
function getErrorCode(error) {
|
|
46
|
+
if (typeof error !== "object" || error === null) return;
|
|
47
|
+
if ("code" in error && typeof error.code === "string") return error.code;
|
|
48
|
+
return "error" in error && typeof error.error === "object" && error.error !== null && "code" in error.error && typeof error.error.code === "string" ? error.error.code : void 0;
|
|
49
|
+
}
|
|
50
|
+
function _getRetryAfterHeader(error) {
|
|
51
|
+
if (error?.headers) {
|
|
52
|
+
if (typeof error.headers.get === "function") return error.headers.get("retry-after");
|
|
53
|
+
return error.headers["retry-after"] ?? error.headers["Retry-After"];
|
|
54
|
+
}
|
|
55
|
+
if (error?.response?.headers) {
|
|
56
|
+
if (typeof error.response.headers.get === "function") return error.response.headers.get("retry-after");
|
|
57
|
+
return error.response.headers["retry-after"] ?? error.response.headers["Retry-After"];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function parseRetryAfterFromMessageMs(message) {
|
|
61
|
+
if (message == null) return;
|
|
62
|
+
const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);
|
|
63
|
+
if (!match) return;
|
|
64
|
+
const rawValue = Number(match[1]);
|
|
65
|
+
const unit = match[2]?.toLowerCase();
|
|
66
|
+
if (Number.isNaN(rawValue) || !unit) return;
|
|
67
|
+
if (unit === "ms" || unit.startsWith("millisecond")) return rawValue;
|
|
68
|
+
if (unit === "m" || unit.startsWith("min")) return rawValue * 6e4;
|
|
69
|
+
if (unit === "h" || unit.startsWith("hr") || unit.startsWith("hour")) return rawValue * 36e5;
|
|
70
|
+
return rawValue * 1e3;
|
|
71
|
+
}
|
|
72
|
+
function coerceError(error, fallbackMessage) {
|
|
73
|
+
if (error instanceof Error) return error;
|
|
74
|
+
const coerced = new Error(fallbackMessage);
|
|
75
|
+
if (typeof error === "object" && error !== null) Object.assign(coerced, error);
|
|
76
|
+
return coerced;
|
|
77
|
+
}
|
|
78
|
+
function setRateLimitMetadata(error, classification) {
|
|
79
|
+
if (typeof error !== "object" || error === null) return;
|
|
80
|
+
const mutableError = error;
|
|
81
|
+
mutableError.rateLimitType = classification.action;
|
|
82
|
+
mutableError.rateLimitReason = classification.reason;
|
|
83
|
+
if (classification.retryAfterMs !== void 0) mutableError.retryAfterMs = classification.retryAfterMs;
|
|
84
|
+
}
|
|
85
|
+
function parseRetryAfterMs(headerValue) {
|
|
86
|
+
if (headerValue == null) return;
|
|
87
|
+
const trimmed = headerValue.trim();
|
|
88
|
+
if (!trimmed) return;
|
|
89
|
+
const seconds = Number(trimmed);
|
|
90
|
+
if (!Number.isNaN(seconds) && seconds >= 0) return seconds * 1e3;
|
|
91
|
+
const date = Date.parse(trimmed);
|
|
92
|
+
if (!Number.isNaN(date)) {
|
|
93
|
+
const delayMs = date - Date.now();
|
|
94
|
+
return delayMs > 0 ? delayMs : 0;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function classifyRateLimitError(error) {
|
|
98
|
+
if ((getResponseStatus(error) ?? getDirectStatus(error)) !== 429) return;
|
|
99
|
+
if (getErrorCode(error) === "insufficient_quota") return {
|
|
100
|
+
action: "stop",
|
|
101
|
+
reason: "insufficient_quota"
|
|
102
|
+
};
|
|
103
|
+
const message = getErrorMessage(error);
|
|
104
|
+
if (message && QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))) return {
|
|
105
|
+
action: "stop",
|
|
106
|
+
reason: "quota_message"
|
|
107
|
+
};
|
|
108
|
+
const retryAfterMs = parseRetryAfterMs(_getRetryAfterHeader(error)) ?? parseRetryAfterFromMessageMs(message);
|
|
109
|
+
if (retryAfterMs !== void 0) {
|
|
110
|
+
if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) return {
|
|
111
|
+
action: "wait",
|
|
112
|
+
retryAfterMs,
|
|
113
|
+
reason: "retry_after_hint"
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
action: "capacity",
|
|
117
|
+
retryAfterMs,
|
|
118
|
+
reason: "retry_after_too_large"
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
action: "capacity",
|
|
123
|
+
reason: "headerless_429"
|
|
124
|
+
};
|
|
125
|
+
}
|
|
18
126
|
/**
|
|
19
127
|
* The default failed attempt handler for the AsyncCaller.
|
|
20
128
|
* @param error - The error to handle.
|
|
@@ -24,13 +132,26 @@ const defaultFailedAttemptHandler = (error) => {
|
|
|
24
132
|
if (typeof error !== "object" || error === null) return;
|
|
25
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;
|
|
26
134
|
if ("code" in error && typeof error.code === "string" && error.code === "ECONNABORTED") throw error;
|
|
27
|
-
const
|
|
28
|
-
const directStatus = "status" in error && typeof error.status === "number" ? error.status : void 0;
|
|
29
|
-
const status = responseStatus ?? directStatus;
|
|
135
|
+
const status = getResponseStatus(error) ?? getDirectStatus(error);
|
|
30
136
|
if (status && STATUS_NO_RETRY.includes(+status)) throw error;
|
|
31
|
-
if ((
|
|
32
|
-
const err =
|
|
137
|
+
if (getErrorCode(error) === "insufficient_quota") {
|
|
138
|
+
const err = coerceError(error, getErrorMessage(error) ?? "Insufficient quota");
|
|
33
139
|
err.name = "InsufficientQuotaError";
|
|
140
|
+
setRateLimitMetadata(err, {
|
|
141
|
+
action: "stop",
|
|
142
|
+
reason: "insufficient_quota"
|
|
143
|
+
});
|
|
144
|
+
throw err;
|
|
145
|
+
}
|
|
146
|
+
const rateLimitClassification = classifyRateLimitError(error);
|
|
147
|
+
if (rateLimitClassification) {
|
|
148
|
+
if (rateLimitClassification.action === "wait") {
|
|
149
|
+
setRateLimitMetadata(error, rateLimitClassification);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const err = coerceError(error, getErrorMessage(error) ?? "Rate limit exceeded");
|
|
153
|
+
if (err.name === "Error") err.name = rateLimitClassification.action === "stop" ? "RateLimitQuotaExhaustedError" : "RateLimitCapacityError";
|
|
154
|
+
setRateLimitMetadata(err, rateLimitClassification);
|
|
34
155
|
throw err;
|
|
35
156
|
}
|
|
36
157
|
};
|
|
@@ -88,6 +209,6 @@ var AsyncCaller = class {
|
|
|
88
209
|
}
|
|
89
210
|
};
|
|
90
211
|
//#endregion
|
|
91
|
-
export { AsyncCaller, async_caller_exports };
|
|
212
|
+
export { AsyncCaller, async_caller_exports, classifyRateLimitError, parseRetryAfterMs };
|
|
92
213
|
|
|
93
214
|
//# sourceMappingURL=async_caller.js.map
|
|
@@ -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\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 responseStatus =\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 // OpenAI SDK errors expose status directly on the error object\n const directStatus =\n \"status\" in error && typeof error.status === \"number\"\n ? error.status\n : undefined;\n\n const status = responseStatus ?? directStatus;\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code =\n \"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 if (code === \"insufficient_quota\") {\n const err = new Error(\n \"message\" in error && typeof error.message === \"string\"\n ? error.message\n : \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\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;CACD;;;;;;AAOD,MAAM,+BAA+B,UAAmB;AACtD,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC;AAGF,KACG,aAAa,SACZ,OAAO,MAAM,YAAY,aACxB,MAAM,QAAQ,WAAW,SAAS,IACjC,MAAM,QAAQ,WAAW,aAAa,KACzC,UAAU,SACT,OAAO,MAAM,SAAS,YACtB,MAAM,SAAS,aAEjB,OAAM;AAER,KACE,UAAU,SACV,OAAO,MAAM,SAAS,YACtB,MAAM,SAAS,eAEf,OAAM;CAER,M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ACJ,UAAO,QAAQ,KAAK,CAClB,KAAK,KAAW,UAAU,GAAG,KAAK,EAClC,IAAI,SAAgB,GAAG,WAAW;AAChC,qBAAiB;AACf,YAAO,oBAAoB,QAAQ,OAAO,CAAC;;AAE7C,YAAQ,QAAQ,iBAAiB,SAAS,UAAU,EAAE,MAAM,MAAM,CAAC;KACnE,CACH,CAAC,CAAC,cAAc;AACf,QAAI,QAAQ,UAAU,SACpB,SAAQ,OAAO,oBAAoB,SAAS,SAAS;KAEvD;;AAEJ,SAAO,KAAK,KAAW,UAAU,GAAG,KAAK;;CAG3C,MAAM,GAAG,MAA0D;AACjE,SAAO,KAAK,WACV,MAAM,GAAG,KAAK,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,IAAI,CAAE,CACnE"}
|
|
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;CACD;AAED,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;AAC7D,QAAO,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;;AAGN,SAAS,gBAAgB,OAAoC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BAAkB;AAC/C,OAAK,aAAa,OAAO,cAAc;AACvC,OAAK,kBACH,OAAO,mBAAmB;EAE5B,MAAM,SACJ,aAAa,YAAY,UAAU,UAAU;AAE/C,OAAK,QAAQ,IAAI,OAAO,EAAE,aAAa,KAAK,gBAAgB,CAAC;;CAI/D,MAAM,KACJ,UACA,GAAG,MAC8B;AACjC,SAAO,KAAK,MAAM,UAEd,aAEI,SAAS,GAAG,KAAK,CAAC,OAAO,UAAU;AAEjC,OAAI,iBAAiB,MACnB,OAAM;OAEN,OAAM,IAAI,MAAM,MAAM;IAExB,EACJ;GACE,kBAAkB,EAAE,YAAY,KAAK,kBAAkB,MAAM;GAC7D,SAAS,KAAK;GACd,WAAW;GAGZ,CACF,EACH,EAAE,gBAAgB,MAAM,CACzB;;CAIH,gBACE,SACA,UACA,GAAG,MAC8B;AAGjC,MAAI,QAAQ,QAAQ;GAClB,IAAI;AACJ,UAAO,QAAQ,KAAK,CAClB,KAAK,KAAW,UAAU,GAAG,KAAK,EAClC,IAAI,SAAgB,GAAG,WAAW;AAChC,qBAAiB;AACf,YAAO,oBAAoB,QAAQ,OAAO,CAAC;;AAE7C,YAAQ,QAAQ,iBAAiB,SAAS,UAAU,EAAE,MAAM,MAAM,CAAC;KACnE,CACH,CAAC,CAAC,cAAc;AACf,QAAI,QAAQ,UAAU,SACpB,SAAQ,OAAO,oBAAoB,SAAS,SAAS;KAEvD;;AAEJ,SAAO,KAAK,KAAW,UAAU,GAAG,KAAK;;CAG3C,MAAM,GAAG,MAA0D;AACjE,SAAO,KAAK,WACV,MAAM,GAAG,KAAK,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,IAAI,CAAE,CACnE"}
|
|
@@ -63,7 +63,9 @@ async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTi
|
|
|
63
63
|
options.signal?.throwIfAborted();
|
|
64
64
|
return false;
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
let delayTime = calculateDelay(retriesConsumed, options);
|
|
67
|
+
const retryAfterMs = typeof normalizedError.retryAfterMs === "number" && normalizedError.retryAfterMs >= 0 ? normalizedError.retryAfterMs : void 0;
|
|
68
|
+
if (retryAfterMs !== void 0) delayTime = Math.max(delayTime, retryAfterMs);
|
|
67
69
|
const finalDelay = Math.min(delayTime, remainingTime);
|
|
68
70
|
if (finalDelay > 0) await new Promise((resolve, reject) => {
|
|
69
71
|
const onAbort = () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["isNetworkError"],"sources":["../../../src/utils/p-retry/index.js"],"sourcesContent":["/* oxlint-disable */\nimport isNetworkError from \"../is-network-error/index.js\";\n\nfunction validateRetries(retries) {\n if (typeof retries === \"number\") {\n if (retries < 0) {\n throw new TypeError(\"Expected `retries` to be a non-negative number.\");\n }\n\n if (Number.isNaN(retries)) {\n throw new TypeError(\n \"Expected `retries` to be a valid number or Infinity, got NaN.\"\n );\n }\n } else if (retries !== undefined) {\n throw new TypeError(\"Expected `retries` to be a number or Infinity.\");\n }\n}\n\nfunction validateNumberOption(\n name,\n value,\n { min = 0, allowInfinity = false } = {}\n) {\n if (value === undefined) {\n return;\n }\n\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n throw new TypeError(\n `Expected \\`${name}\\` to be a number${\n allowInfinity ? \" or Infinity\" : \"\"\n }.`\n );\n }\n\n if (!allowInfinity && !Number.isFinite(value)) {\n throw new TypeError(`Expected \\`${name}\\` to be a finite number.`);\n }\n\n if (value < min) {\n throw new TypeError(`Expected \\`${name}\\` to be \\u2265 ${min}.`);\n }\n}\n\nexport class AbortError extends Error {\n constructor(message) {\n super();\n\n if (message instanceof Error) {\n this.originalError = message;\n ({ message } = message);\n } else {\n this.originalError = new Error(message);\n this.originalError.stack = this.stack;\n }\n\n this.name = \"AbortError\";\n this.message = message;\n }\n}\n\nfunction calculateDelay(retriesConsumed, options) {\n const attempt = Math.max(1, retriesConsumed + 1);\n const random = options.randomize ? Math.random() + 1 : 1;\n\n let timeout = Math.round(\n random * options.minTimeout * options.factor ** (attempt - 1)\n );\n timeout = Math.min(timeout, options.maxTimeout);\n\n return timeout;\n}\n\nfunction calculateRemainingTime(start, max) {\n if (!Number.isFinite(max)) {\n return max;\n }\n\n return max - (performance.now() - start);\n}\n\nasync function onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n}) {\n const normalizedError =\n error instanceof Error\n ? error\n : new TypeError(\n `Non-error was thrown: \"${error}\". You should only throw errors.`\n );\n\n if (normalizedError instanceof AbortError) {\n throw normalizedError.originalError;\n }\n\n const retriesLeft = Number.isFinite(options.retries)\n ? Math.max(0, options.retries - retriesConsumed)\n : options.retries;\n\n const maxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;\n\n const context = Object.freeze({\n error: normalizedError,\n attemptNumber,\n retriesLeft,\n retriesConsumed,\n });\n\n await options.onFailedAttempt(context);\n\n if (calculateRemainingTime(startTime, maxRetryTime) <= 0) {\n throw normalizedError;\n }\n\n const consumeRetry = await options.shouldConsumeRetry(context);\n\n const remainingTime = calculateRemainingTime(startTime, maxRetryTime);\n\n if (remainingTime <= 0 || retriesLeft <= 0) {\n throw normalizedError;\n }\n\n if (\n normalizedError instanceof TypeError &&\n !isNetworkError(normalizedError)\n ) {\n if (consumeRetry) {\n throw normalizedError;\n }\n\n options.signal?.throwIfAborted();\n return false;\n }\n\n if (!(await options.shouldRetry(context))) {\n throw normalizedError;\n }\n\n if (!consumeRetry) {\n options.signal?.throwIfAborted();\n return false;\n }\n\n const delayTime = calculateDelay(retriesConsumed, options);\n const finalDelay = Math.min(delayTime, remainingTime);\n\n if (finalDelay > 0) {\n await new Promise((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timeoutToken);\n options.signal?.removeEventListener(\"abort\", onAbort);\n reject(options.signal.reason);\n };\n\n const timeoutToken = setTimeout(() => {\n options.signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, finalDelay);\n\n if (options.unref) {\n timeoutToken.unref?.();\n }\n\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n }\n\n options.signal?.throwIfAborted();\n\n return true;\n}\n\nexport default async function pRetry(input, options = {}) {\n options = { ...options };\n\n validateRetries(options.retries);\n\n if (Object.hasOwn(options, \"forever\")) {\n throw new Error(\n \"The `forever` option is no longer supported. For many use-cases, you can set `retries: Infinity` instead.\"\n );\n }\n\n options.retries ??= 10;\n options.factor ??= 2;\n options.minTimeout ??= 1000;\n options.maxTimeout ??= Number.POSITIVE_INFINITY;\n options.maxRetryTime ??= Number.POSITIVE_INFINITY;\n options.randomize ??= false;\n options.onFailedAttempt ??= () => {};\n options.shouldRetry ??= () => true;\n options.shouldConsumeRetry ??= () => true;\n\n // Validate numeric options and normalize edge cases\n validateNumberOption(\"factor\", options.factor, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"minTimeout\", options.minTimeout, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"maxTimeout\", options.maxTimeout, {\n min: 0,\n allowInfinity: true,\n });\n validateNumberOption(\"maxRetryTime\", options.maxRetryTime, {\n min: 0,\n allowInfinity: true,\n });\n\n // Treat non-positive factor as 1 to avoid zero backoff or negative behavior\n if (!(options.factor > 0)) {\n options.factor = 1;\n }\n\n options.signal?.throwIfAborted();\n\n let attemptNumber = 0;\n let retriesConsumed = 0;\n const startTime = performance.now();\n\n while (\n Number.isFinite(options.retries) ? retriesConsumed <= options.retries : true\n ) {\n attemptNumber++;\n\n try {\n options.signal?.throwIfAborted();\n\n const result = await input(attemptNumber);\n\n options.signal?.throwIfAborted();\n\n return result;\n } catch (error) {\n if (\n await onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n })\n ) {\n retriesConsumed++;\n }\n }\n }\n\n // Should not reach here, but in case it does, throw an error\n throw new Error(\"Retry attempts exhausted without throwing an error.\");\n}\n\nexport function makeRetriable(function_, options) {\n return function (...arguments_) {\n return pRetry(() => function_.apply(this, arguments_), options);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AAMN,OAAM,IAAI,MAAM,sDAAsD"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["isNetworkError"],"sources":["../../../src/utils/p-retry/index.js"],"sourcesContent":["/* oxlint-disable */\nimport isNetworkError from \"../is-network-error/index.js\";\n\nfunction validateRetries(retries) {\n if (typeof retries === \"number\") {\n if (retries < 0) {\n throw new TypeError(\"Expected `retries` to be a non-negative number.\");\n }\n\n if (Number.isNaN(retries)) {\n throw new TypeError(\n \"Expected `retries` to be a valid number or Infinity, got NaN.\"\n );\n }\n } else if (retries !== undefined) {\n throw new TypeError(\"Expected `retries` to be a number or Infinity.\");\n }\n}\n\nfunction validateNumberOption(\n name,\n value,\n { min = 0, allowInfinity = false } = {}\n) {\n if (value === undefined) {\n return;\n }\n\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n throw new TypeError(\n `Expected \\`${name}\\` to be a number${\n allowInfinity ? \" or Infinity\" : \"\"\n }.`\n );\n }\n\n if (!allowInfinity && !Number.isFinite(value)) {\n throw new TypeError(`Expected \\`${name}\\` to be a finite number.`);\n }\n\n if (value < min) {\n throw new TypeError(`Expected \\`${name}\\` to be \\u2265 ${min}.`);\n }\n}\n\nexport class AbortError extends Error {\n constructor(message) {\n super();\n\n if (message instanceof Error) {\n this.originalError = message;\n ({ message } = message);\n } else {\n this.originalError = new Error(message);\n this.originalError.stack = this.stack;\n }\n\n this.name = \"AbortError\";\n this.message = message;\n }\n}\n\nfunction calculateDelay(retriesConsumed, options) {\n const attempt = Math.max(1, retriesConsumed + 1);\n const random = options.randomize ? Math.random() + 1 : 1;\n\n let timeout = Math.round(\n random * options.minTimeout * options.factor ** (attempt - 1)\n );\n timeout = Math.min(timeout, options.maxTimeout);\n\n return timeout;\n}\n\nfunction calculateRemainingTime(start, max) {\n if (!Number.isFinite(max)) {\n return max;\n }\n\n return max - (performance.now() - start);\n}\n\nasync function onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n}) {\n const normalizedError =\n error instanceof Error\n ? error\n : new TypeError(\n `Non-error was thrown: \"${error}\". You should only throw errors.`\n );\n\n if (normalizedError instanceof AbortError) {\n throw normalizedError.originalError;\n }\n\n const retriesLeft = Number.isFinite(options.retries)\n ? Math.max(0, options.retries - retriesConsumed)\n : options.retries;\n\n const maxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;\n\n const context = Object.freeze({\n error: normalizedError,\n attemptNumber,\n retriesLeft,\n retriesConsumed,\n });\n\n await options.onFailedAttempt(context);\n\n if (calculateRemainingTime(startTime, maxRetryTime) <= 0) {\n throw normalizedError;\n }\n\n const consumeRetry = await options.shouldConsumeRetry(context);\n\n const remainingTime = calculateRemainingTime(startTime, maxRetryTime);\n\n if (remainingTime <= 0 || retriesLeft <= 0) {\n throw normalizedError;\n }\n\n if (\n normalizedError instanceof TypeError &&\n !isNetworkError(normalizedError)\n ) {\n if (consumeRetry) {\n throw normalizedError;\n }\n\n options.signal?.throwIfAborted();\n return false;\n }\n\n if (!(await options.shouldRetry(context))) {\n throw normalizedError;\n }\n\n if (!consumeRetry) {\n options.signal?.throwIfAborted();\n return false;\n }\n\n let delayTime = calculateDelay(retriesConsumed, options);\n const retryAfterMs =\n typeof normalizedError.retryAfterMs === \"number\" &&\n normalizedError.retryAfterMs >= 0\n ? normalizedError.retryAfterMs\n : undefined;\n\n if (retryAfterMs !== undefined) {\n delayTime = Math.max(delayTime, retryAfterMs);\n }\n\n const finalDelay = Math.min(delayTime, remainingTime);\n\n if (finalDelay > 0) {\n await new Promise((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timeoutToken);\n options.signal?.removeEventListener(\"abort\", onAbort);\n reject(options.signal.reason);\n };\n\n const timeoutToken = setTimeout(() => {\n options.signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, finalDelay);\n\n if (options.unref) {\n timeoutToken.unref?.();\n }\n\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n }\n\n options.signal?.throwIfAborted();\n\n return true;\n}\n\nexport default async function pRetry(input, options = {}) {\n options = { ...options };\n\n validateRetries(options.retries);\n\n if (Object.hasOwn(options, \"forever\")) {\n throw new Error(\n \"The `forever` option is no longer supported. For many use-cases, you can set `retries: Infinity` instead.\"\n );\n }\n\n options.retries ??= 10;\n options.factor ??= 2;\n options.minTimeout ??= 1000;\n options.maxTimeout ??= Number.POSITIVE_INFINITY;\n options.maxRetryTime ??= Number.POSITIVE_INFINITY;\n options.randomize ??= false;\n options.onFailedAttempt ??= () => {};\n options.shouldRetry ??= () => true;\n options.shouldConsumeRetry ??= () => true;\n\n // Validate numeric options and normalize edge cases\n validateNumberOption(\"factor\", options.factor, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"minTimeout\", options.minTimeout, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"maxTimeout\", options.maxTimeout, {\n min: 0,\n allowInfinity: true,\n });\n validateNumberOption(\"maxRetryTime\", options.maxRetryTime, {\n min: 0,\n allowInfinity: true,\n });\n\n // Treat non-positive factor as 1 to avoid zero backoff or negative behavior\n if (!(options.factor > 0)) {\n options.factor = 1;\n }\n\n options.signal?.throwIfAborted();\n\n let attemptNumber = 0;\n let retriesConsumed = 0;\n const startTime = performance.now();\n\n while (\n Number.isFinite(options.retries) ? retriesConsumed <= options.retries : true\n ) {\n attemptNumber++;\n\n try {\n options.signal?.throwIfAborted();\n\n const result = await input(attemptNumber);\n\n options.signal?.throwIfAborted();\n\n return result;\n } catch (error) {\n if (\n await onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n })\n ) {\n retriesConsumed++;\n }\n }\n }\n\n // Should not reach here, but in case it does, throw an error\n throw new Error(\"Retry attempts exhausted without throwing an error.\");\n}\n\nexport function makeRetriable(function_, options) {\n return function (...arguments_) {\n return pRetry(() => function_.apply(this, arguments_), options);\n };\n}\n"],"mappings":";;AAGA,SAAS,gBAAgB,SAAS;AAChC,KAAI,OAAO,YAAY,UAAU;AAC/B,MAAI,UAAU,EAC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M,iBAAiB;IACrB;IACA;IACA;IACA;IACA;IACD,CAAC,CAEF;;;AAMN,OAAM,IAAI,MAAM,sDAAsD"}
|
|
@@ -63,7 +63,9 @@ async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTi
|
|
|
63
63
|
options.signal?.throwIfAborted();
|
|
64
64
|
return false;
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
let delayTime = calculateDelay(retriesConsumed, options);
|
|
67
|
+
const retryAfterMs = typeof normalizedError.retryAfterMs === "number" && normalizedError.retryAfterMs >= 0 ? normalizedError.retryAfterMs : void 0;
|
|
68
|
+
if (retryAfterMs !== void 0) delayTime = Math.max(delayTime, retryAfterMs);
|
|
67
69
|
const finalDelay = Math.min(delayTime, remainingTime);
|
|
68
70
|
if (finalDelay > 0) await new Promise((resolve, reject) => {
|
|
69
71
|
const onAbort = () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../src/utils/p-retry/index.js"],"sourcesContent":["/* oxlint-disable */\nimport isNetworkError from \"../is-network-error/index.js\";\n\nfunction validateRetries(retries) {\n if (typeof retries === \"number\") {\n if (retries < 0) {\n throw new TypeError(\"Expected `retries` to be a non-negative number.\");\n }\n\n if (Number.isNaN(retries)) {\n throw new TypeError(\n \"Expected `retries` to be a valid number or Infinity, got NaN.\"\n );\n }\n } else if (retries !== undefined) {\n throw new TypeError(\"Expected `retries` to be a number or Infinity.\");\n }\n}\n\nfunction validateNumberOption(\n name,\n value,\n { min = 0, allowInfinity = false } = {}\n) {\n if (value === undefined) {\n return;\n }\n\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n throw new TypeError(\n `Expected \\`${name}\\` to be a number${\n allowInfinity ? \" or Infinity\" : \"\"\n }.`\n );\n }\n\n if (!allowInfinity && !Number.isFinite(value)) {\n throw new TypeError(`Expected \\`${name}\\` to be a finite number.`);\n }\n\n if (value < min) {\n throw new TypeError(`Expected \\`${name}\\` to be \\u2265 ${min}.`);\n }\n}\n\nexport class AbortError extends Error {\n constructor(message) {\n super();\n\n if (message instanceof Error) {\n this.originalError = message;\n ({ message } = message);\n } else {\n this.originalError = new Error(message);\n this.originalError.stack = this.stack;\n }\n\n this.name = \"AbortError\";\n this.message = message;\n }\n}\n\nfunction calculateDelay(retriesConsumed, options) {\n const attempt = Math.max(1, retriesConsumed + 1);\n const random = options.randomize ? Math.random() + 1 : 1;\n\n let timeout = Math.round(\n random * options.minTimeout * options.factor ** (attempt - 1)\n );\n timeout = Math.min(timeout, options.maxTimeout);\n\n return timeout;\n}\n\nfunction calculateRemainingTime(start, max) {\n if (!Number.isFinite(max)) {\n return max;\n }\n\n return max - (performance.now() - start);\n}\n\nasync function onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n}) {\n const normalizedError =\n error instanceof Error\n ? error\n : new TypeError(\n `Non-error was thrown: \"${error}\". You should only throw errors.`\n );\n\n if (normalizedError instanceof AbortError) {\n throw normalizedError.originalError;\n }\n\n const retriesLeft = Number.isFinite(options.retries)\n ? Math.max(0, options.retries - retriesConsumed)\n : options.retries;\n\n const maxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;\n\n const context = Object.freeze({\n error: normalizedError,\n attemptNumber,\n retriesLeft,\n retriesConsumed,\n });\n\n await options.onFailedAttempt(context);\n\n if (calculateRemainingTime(startTime, maxRetryTime) <= 0) {\n throw normalizedError;\n }\n\n const consumeRetry = await options.shouldConsumeRetry(context);\n\n const remainingTime = calculateRemainingTime(startTime, maxRetryTime);\n\n if (remainingTime <= 0 || retriesLeft <= 0) {\n throw normalizedError;\n }\n\n if (\n normalizedError instanceof TypeError &&\n !isNetworkError(normalizedError)\n ) {\n if (consumeRetry) {\n throw normalizedError;\n }\n\n options.signal?.throwIfAborted();\n return false;\n }\n\n if (!(await options.shouldRetry(context))) {\n throw normalizedError;\n }\n\n if (!consumeRetry) {\n options.signal?.throwIfAborted();\n return false;\n }\n\n const delayTime = calculateDelay(retriesConsumed, options);\n const finalDelay = Math.min(delayTime, remainingTime);\n\n if (finalDelay > 0) {\n await new Promise((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timeoutToken);\n options.signal?.removeEventListener(\"abort\", onAbort);\n reject(options.signal.reason);\n };\n\n const timeoutToken = setTimeout(() => {\n options.signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, finalDelay);\n\n if (options.unref) {\n timeoutToken.unref?.();\n }\n\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n }\n\n options.signal?.throwIfAborted();\n\n return true;\n}\n\nexport default async function pRetry(input, options = {}) {\n options = { ...options };\n\n validateRetries(options.retries);\n\n if (Object.hasOwn(options, \"forever\")) {\n throw new Error(\n \"The `forever` option is no longer supported. For many use-cases, you can set `retries: Infinity` instead.\"\n );\n }\n\n options.retries ??= 10;\n options.factor ??= 2;\n options.minTimeout ??= 1000;\n options.maxTimeout ??= Number.POSITIVE_INFINITY;\n options.maxRetryTime ??= Number.POSITIVE_INFINITY;\n options.randomize ??= false;\n options.onFailedAttempt ??= () => {};\n options.shouldRetry ??= () => true;\n options.shouldConsumeRetry ??= () => true;\n\n // Validate numeric options and normalize edge cases\n validateNumberOption(\"factor\", options.factor, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"minTimeout\", options.minTimeout, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"maxTimeout\", options.maxTimeout, {\n min: 0,\n allowInfinity: true,\n });\n validateNumberOption(\"maxRetryTime\", options.maxRetryTime, {\n min: 0,\n allowInfinity: true,\n });\n\n // Treat non-positive factor as 1 to avoid zero backoff or negative behavior\n if (!(options.factor > 0)) {\n options.factor = 1;\n }\n\n options.signal?.throwIfAborted();\n\n let attemptNumber = 0;\n let retriesConsumed = 0;\n const startTime = performance.now();\n\n while (\n Number.isFinite(options.retries) ? retriesConsumed <= options.retries : true\n ) {\n attemptNumber++;\n\n try {\n options.signal?.throwIfAborted();\n\n const result = await input(attemptNumber);\n\n options.signal?.throwIfAborted();\n\n return result;\n } catch (error) {\n if (\n await onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n })\n ) {\n retriesConsumed++;\n }\n }\n }\n\n // Should not reach here, but in case it does, throw an error\n throw new Error(\"Retry attempts exhausted without throwing an error.\");\n}\n\nexport function makeRetriable(function_, options) {\n return function (...arguments_) {\n return pRetry(() => function_.apply(this, arguments_), options);\n };\n}\n"],"mappings":";;AAGA,SAAS,gBAAgB,SAAS;AAChC,KAAI,OAAO,YAAY,UAAU;AAC/B,MAAI,UAAU,EACZ,OAAM,IAAI,UAAU,kDAAkD;AAGxE,MAAI,OAAO,MAAM,QAAQ,CACvB,OAAM,IAAI,UACR,gEACD;YAEM,YAAY,KAAA,EACrB,OAAM,IAAI,UAAU,iDAAiD;;AAIzE,SAAS,qBACP,MACA,OACA,EAAE,MAAM,GAAG,gBAAgB,UAAU,EAAE,EACvC;AACA,KAAI,UAAU,KAAA,EACZ;AAGF,KAAI,OAAO,UAAU,YAAY,OAAO,MAAM,MAAM,CAClD,OAAM,IAAI,UACR,cAAc,KAAK,mBACjB,gBAAgB,iBAAiB,GAClC,GACF;AAGH,KAAI,CAAC,iBAAiB,CAAC,OAAO,SAAS,MAAM,CAC3C,OAAM,IAAI,UAAU,cAAc,KAAK,2BAA2B;AAGpE,KAAI,QAAQ,IACV,OAAM,IAAI,UAAU,cAAc,KAAK,kBAAkB,IAAI,GAAG;;AAIpE,IAAa,aAAb,cAAgC,MAAM;CACpC,YAAY,SAAS;AACnB,SAAO;AAEP,MAAI,mBAAmB,OAAO;AAC5B,QAAK,gBAAgB;AACrB,IAAC,CAAE,WAAY;SACV;AACL,QAAK,gBAAgB,IAAI,MAAM,QAAQ;AACvC,QAAK,cAAc,QAAQ,KAAK;;AAGlC,OAAK,OAAO;AACZ,OAAK,UAAU;;;AAInB,SAAS,eAAe,iBAAiB,SAAS;CAChD,MAAM,UAAU,KAAK,IAAI,GAAG,kBAAkB,EAAE;CAChD,MA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,QAAQ,GAAG,mBAAmB,QAAQ,UAAU,MACxE;AACA;AAEA,MAAI;AACF,WAAQ,QAAQ,gBAAgB;GAEhC,MAAM,SAAS,MAAM,MAAM,cAAc;AAEzC,WAAQ,QAAQ,gBAAgB;AAEhC,UAAO;WACA,OAAO;AACd,OACE,MAAM,iBAAiB;IACrB;IACA;IACA;IACA;IACA;IACD,CAAC,CAEF;;;AAMN,OAAM,IAAI,MAAM,sDAAsD"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/utils/p-retry/index.js"],"sourcesContent":["/* oxlint-disable */\nimport isNetworkError from \"../is-network-error/index.js\";\n\nfunction validateRetries(retries) {\n if (typeof retries === \"number\") {\n if (retries < 0) {\n throw new TypeError(\"Expected `retries` to be a non-negative number.\");\n }\n\n if (Number.isNaN(retries)) {\n throw new TypeError(\n \"Expected `retries` to be a valid number or Infinity, got NaN.\"\n );\n }\n } else if (retries !== undefined) {\n throw new TypeError(\"Expected `retries` to be a number or Infinity.\");\n }\n}\n\nfunction validateNumberOption(\n name,\n value,\n { min = 0, allowInfinity = false } = {}\n) {\n if (value === undefined) {\n return;\n }\n\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n throw new TypeError(\n `Expected \\`${name}\\` to be a number${\n allowInfinity ? \" or Infinity\" : \"\"\n }.`\n );\n }\n\n if (!allowInfinity && !Number.isFinite(value)) {\n throw new TypeError(`Expected \\`${name}\\` to be a finite number.`);\n }\n\n if (value < min) {\n throw new TypeError(`Expected \\`${name}\\` to be \\u2265 ${min}.`);\n }\n}\n\nexport class AbortError extends Error {\n constructor(message) {\n super();\n\n if (message instanceof Error) {\n this.originalError = message;\n ({ message } = message);\n } else {\n this.originalError = new Error(message);\n this.originalError.stack = this.stack;\n }\n\n this.name = \"AbortError\";\n this.message = message;\n }\n}\n\nfunction calculateDelay(retriesConsumed, options) {\n const attempt = Math.max(1, retriesConsumed + 1);\n const random = options.randomize ? Math.random() + 1 : 1;\n\n let timeout = Math.round(\n random * options.minTimeout * options.factor ** (attempt - 1)\n );\n timeout = Math.min(timeout, options.maxTimeout);\n\n return timeout;\n}\n\nfunction calculateRemainingTime(start, max) {\n if (!Number.isFinite(max)) {\n return max;\n }\n\n return max - (performance.now() - start);\n}\n\nasync function onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n}) {\n const normalizedError =\n error instanceof Error\n ? error\n : new TypeError(\n `Non-error was thrown: \"${error}\". You should only throw errors.`\n );\n\n if (normalizedError instanceof AbortError) {\n throw normalizedError.originalError;\n }\n\n const retriesLeft = Number.isFinite(options.retries)\n ? Math.max(0, options.retries - retriesConsumed)\n : options.retries;\n\n const maxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;\n\n const context = Object.freeze({\n error: normalizedError,\n attemptNumber,\n retriesLeft,\n retriesConsumed,\n });\n\n await options.onFailedAttempt(context);\n\n if (calculateRemainingTime(startTime, maxRetryTime) <= 0) {\n throw normalizedError;\n }\n\n const consumeRetry = await options.shouldConsumeRetry(context);\n\n const remainingTime = calculateRemainingTime(startTime, maxRetryTime);\n\n if (remainingTime <= 0 || retriesLeft <= 0) {\n throw normalizedError;\n }\n\n if (\n normalizedError instanceof TypeError &&\n !isNetworkError(normalizedError)\n ) {\n if (consumeRetry) {\n throw normalizedError;\n }\n\n options.signal?.throwIfAborted();\n return false;\n }\n\n if (!(await options.shouldRetry(context))) {\n throw normalizedError;\n }\n\n if (!consumeRetry) {\n options.signal?.throwIfAborted();\n return false;\n }\n\n let delayTime = calculateDelay(retriesConsumed, options);\n const retryAfterMs =\n typeof normalizedError.retryAfterMs === \"number\" &&\n normalizedError.retryAfterMs >= 0\n ? normalizedError.retryAfterMs\n : undefined;\n\n if (retryAfterMs !== undefined) {\n delayTime = Math.max(delayTime, retryAfterMs);\n }\n\n const finalDelay = Math.min(delayTime, remainingTime);\n\n if (finalDelay > 0) {\n await new Promise((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timeoutToken);\n options.signal?.removeEventListener(\"abort\", onAbort);\n reject(options.signal.reason);\n };\n\n const timeoutToken = setTimeout(() => {\n options.signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, finalDelay);\n\n if (options.unref) {\n timeoutToken.unref?.();\n }\n\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n }\n\n options.signal?.throwIfAborted();\n\n return true;\n}\n\nexport default async function pRetry(input, options = {}) {\n options = { ...options };\n\n validateRetries(options.retries);\n\n if (Object.hasOwn(options, \"forever\")) {\n throw new Error(\n \"The `forever` option is no longer supported. For many use-cases, you can set `retries: Infinity` instead.\"\n );\n }\n\n options.retries ??= 10;\n options.factor ??= 2;\n options.minTimeout ??= 1000;\n options.maxTimeout ??= Number.POSITIVE_INFINITY;\n options.maxRetryTime ??= Number.POSITIVE_INFINITY;\n options.randomize ??= false;\n options.onFailedAttempt ??= () => {};\n options.shouldRetry ??= () => true;\n options.shouldConsumeRetry ??= () => true;\n\n // Validate numeric options and normalize edge cases\n validateNumberOption(\"factor\", options.factor, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"minTimeout\", options.minTimeout, {\n min: 0,\n allowInfinity: false,\n });\n validateNumberOption(\"maxTimeout\", options.maxTimeout, {\n min: 0,\n allowInfinity: true,\n });\n validateNumberOption(\"maxRetryTime\", options.maxRetryTime, {\n min: 0,\n allowInfinity: true,\n });\n\n // Treat non-positive factor as 1 to avoid zero backoff or negative behavior\n if (!(options.factor > 0)) {\n options.factor = 1;\n }\n\n options.signal?.throwIfAborted();\n\n let attemptNumber = 0;\n let retriesConsumed = 0;\n const startTime = performance.now();\n\n while (\n Number.isFinite(options.retries) ? retriesConsumed <= options.retries : true\n ) {\n attemptNumber++;\n\n try {\n options.signal?.throwIfAborted();\n\n const result = await input(attemptNumber);\n\n options.signal?.throwIfAborted();\n\n return result;\n } catch (error) {\n if (\n await onAttemptFailure({\n error,\n attemptNumber,\n retriesConsumed,\n startTime,\n options,\n })\n ) {\n retriesConsumed++;\n }\n }\n }\n\n // Should not reach here, but in case it does, throw an error\n throw new Error(\"Retry attempts exhausted without throwing an error.\");\n}\n\nexport function makeRetriable(function_, options) {\n return function (...arguments_) {\n return pRetry(() => function_.apply(this, arguments_), options);\n };\n}\n"],"mappings":";;AAGA,SAAS,gBAAgB,SAAS;AAChC,KAAI,OAAO,YAAY,UAAU;AAC/B,MAAI,UAAU,EACZ,OAAM,IAAI,UAAU,kDAAkD;AAGxE,MAAI,OAAO,MAAM,QAAQ,CACvB,OAAM,IAAI,UACR,gEACD;YAEM,YAAY,KAAA,EACrB,OAAM,IAAI,UAAU,iDAAiD;;AAIzE,SAAS,qBACP,MACA,OACA,EAAE,MAAM,GAAG,gBAAgB,UAAU,EAAE,EACvC;AACA,KAAI,UAAU,KAAA,EACZ;AAGF,KAAI,OAAO,UAAU,YAAY,OAAO,MAAM,MAAM,CAClD,OAAM,IAAI,UACR,cAAc,KAAK,mBACjB,gBAAgB,iBAAiB,GAClC,GACF;AAGH,KAAI,CAAC,iBAAiB,CAAC,OAAO,SAAS,MAAM,CAC3C,OAAM,IAAI,UAAU,cAAc,KAAK,2BAA2B;AAGpE,KAAI,QAAQ,IACV,OAAM,IAAI,UAAU,cAAc,KAAK,kBAAkB,IAAI,GAAG;;AAIpE,IAAa,aAAb,cAAgC,MAAM;CACpC,YAAY,SAAS;AACnB,SAAO;AAEP,MAAI,mBAAmB,OAAO;AAC5B,QAAK,gBAAgB;AACrB,IAAC,CAAE,WAAY;SACV;AACL,QAAK,gBAAgB,IAAI,MAAM,QAAQ;AACvC,QAAK,cAAc,QAAQ,KAAK;;AAGlC,OAAK,OAAO;AACZ,OAAK,UAAU;;;AAInB,SAAS,eAAe,iBAAiB,SAAS;CAChD,MAAM,UAAU,KAAK,IAAI,GAAG,kBAAkB,EAAE;CAChD,MAAM,SAAS,QAAQ,YAAY,KAAK,QAAQ,GAAG,IAAI;CAEvD,IAAI,UAAU,KAAK,MACjB,SAAS,QAAQ,aAAa,QAAQ,WAAW,UAAU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