@langchain/core 1.2.6 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/dist/errors/index.cjs +39 -1
- package/dist/errors/index.cjs.map +1 -1
- package/dist/errors/index.d.cts +19 -1
- package/dist/errors/index.d.cts.map +1 -1
- package/dist/errors/index.d.ts +19 -1
- package/dist/errors/index.d.ts.map +1 -1
- package/dist/errors/index.js +38 -2
- package/dist/errors/index.js.map +1 -1
- package/dist/language_models/base.cjs +1 -1
- package/dist/language_models/base.cjs.map +1 -1
- package/dist/language_models/base.d.cts +4 -0
- package/dist/language_models/base.d.cts.map +1 -1
- package/dist/language_models/base.d.ts +4 -0
- package/dist/language_models/base.d.ts.map +1 -1
- package/dist/language_models/base.js +1 -1
- package/dist/language_models/base.js.map +1 -1
- package/dist/utils/async_caller.cjs +16 -8
- package/dist/utils/async_caller.cjs.map +1 -1
- package/dist/utils/async_caller.d.cts +2 -0
- package/dist/utils/async_caller.d.cts.map +1 -1
- package/dist/utils/async_caller.d.ts +2 -0
- package/dist/utils/async_caller.d.ts.map +1 -1
- package/dist/utils/async_caller.js +16 -8
- package/dist/utils/async_caller.js.map +1 -1
- package/dist/utils/gateway.cjs +5 -1
- package/dist/utils/gateway.cjs.map +1 -1
- package/dist/utils/gateway.d.cts +2 -1
- package/dist/utils/gateway.d.cts.map +1 -1
- package/dist/utils/gateway.d.ts +2 -1
- package/dist/utils/gateway.d.ts.map +1 -1
- package/dist/utils/gateway.js +5 -2
- package/dist/utils/gateway.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","names":[],"sources":["../../src/language_models/base.ts"],"sourcesContent":["import type { Tiktoken, TiktokenModel } from \"js-tiktoken/lite\";\nimport type { ZodV3Like, ZodV4Like } from \"../utils/types/zod.js\";\n\nimport { type BaseCache, InMemoryCache } from \"../caches/index.js\";\nimport {\n type BasePromptValueInterface,\n StringPromptValue,\n ChatPromptValue,\n} from \"../prompt_values.js\";\nimport {\n type BaseMessage,\n type BaseMessageLike,\n type MessageContent,\n} from \"../messages/base.js\";\nimport { coerceMessageLikeToMessage } from \"../messages/utils.js\";\nimport { type LLMResult } from \"../outputs.js\";\nimport { CallbackManager, Callbacks } from \"../callbacks/manager.js\";\nimport { AsyncCaller, AsyncCallerParams } from \"../utils/async_caller.js\";\nimport { encodingForModel } from \"../utils/tiktoken.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { JSONSchema } from \"../utils/json_schema.js\";\nimport {\n InferInteropZodOutput,\n InteropZodObject,\n InteropZodType,\n} from \"../utils/types/zod.js\";\nimport { ModelProfile } from \"./profile.js\";\nimport { type SerializableSchema } from \"../utils/standard_schema.js\";\n\n// https://www.npmjs.com/package/js-tiktoken\n\nexport const getModelNameForTiktoken = (modelName: string): TiktokenModel => {\n if (modelName.startsWith(\"gpt-5\")) {\n return \"gpt-5\" as TiktokenModel;\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-16k\")) {\n return \"gpt-3.5-turbo-16k\";\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-\")) {\n return \"gpt-3.5-turbo\";\n }\n\n if (modelName.startsWith(\"gpt-4-32k\")) {\n return \"gpt-4-32k\";\n }\n\n if (modelName.startsWith(\"gpt-4-\")) {\n return \"gpt-4\";\n }\n\n if (modelName.startsWith(\"gpt-4o\")) {\n return \"gpt-4o\";\n }\n\n return modelName as TiktokenModel;\n};\n\nexport const getEmbeddingContextSize = (modelName?: string): number => {\n switch (modelName) {\n case \"text-embedding-ada-002\":\n return 8191;\n default:\n return 2046;\n }\n};\n\n/**\n * Get the context window size (max input tokens) for a given model.\n *\n * Context window sizes are sourced from official model documentation:\n * - OpenAI: https://platform.openai.com/docs/models\n * - Anthropic: https://docs.anthropic.com/claude/docs/models-overview\n * - Google: https://ai.google.dev/gemini/docs/models/gemini\n *\n * @param modelName - The name of the model\n * @returns The context window size in tokens\n */\nexport const getModelContextSize = (modelName: string): number => {\n const normalizedName = getModelNameForTiktoken(modelName) as string;\n\n switch (normalizedName) {\n // GPT-5 series\n case \"gpt-5\":\n case \"gpt-5-turbo\":\n case \"gpt-5-turbo-preview\":\n return 400000;\n\n // GPT-4o series\n case \"gpt-4o\":\n case \"gpt-4o-mini\":\n case \"gpt-4o-2024-05-13\":\n case \"gpt-4o-2024-08-06\":\n return 128000;\n\n // GPT-4 Turbo series\n case \"gpt-4-turbo\":\n case \"gpt-4-turbo-preview\":\n case \"gpt-4-turbo-2024-04-09\":\n case \"gpt-4-0125-preview\":\n case \"gpt-4-1106-preview\":\n return 128000;\n\n // GPT-4 series\n case \"gpt-4-32k\":\n case \"gpt-4-32k-0314\":\n case \"gpt-4-32k-0613\":\n return 32768;\n case \"gpt-4\":\n case \"gpt-4-0314\":\n case \"gpt-4-0613\":\n return 8192;\n\n // GPT-3.5 Turbo series\n case \"gpt-3.5-turbo-16k\":\n case \"gpt-3.5-turbo-16k-0613\":\n return 16384;\n case \"gpt-3.5-turbo\":\n case \"gpt-3.5-turbo-0301\":\n case \"gpt-3.5-turbo-0613\":\n case \"gpt-3.5-turbo-1106\":\n case \"gpt-3.5-turbo-0125\":\n return 4096;\n\n // Legacy GPT-3 models\n case \"text-davinci-003\":\n case \"text-davinci-002\":\n return 4097;\n case \"text-davinci-001\":\n return 2049;\n case \"text-curie-001\":\n case \"text-babbage-001\":\n case \"text-ada-001\":\n return 2048;\n\n // Code models\n case \"code-davinci-002\":\n case \"code-davinci-001\":\n return 8000;\n case \"code-cushman-001\":\n return 2048;\n\n // Claude models (Anthropic)\n case \"claude-3-5-sonnet-20241022\":\n case \"claude-3-5-sonnet-20240620\":\n case \"claude-3-opus-20240229\":\n case \"claude-3-sonnet-20240229\":\n case \"claude-3-haiku-20240307\":\n case \"claude-2.1\":\n return 200000;\n case \"claude-2.0\":\n case \"claude-instant-1.2\":\n return 100000;\n\n // Gemini models (Google)\n case \"gemini-1.5-pro\":\n case \"gemini-1.5-pro-latest\":\n case \"gemini-1.5-flash\":\n case \"gemini-1.5-flash-latest\":\n return 1000000; // 1M tokens\n case \"gemini-pro\":\n case \"gemini-pro-vision\":\n return 32768;\n\n default:\n return 4097;\n }\n};\n\n/**\n * Whether or not the input matches the OpenAI tool definition.\n * @param {unknown} tool The input to check.\n * @returns {boolean} Whether the input is an OpenAI tool definition.\n */\nexport function isOpenAITool(tool: unknown): tool is ToolDefinition {\n if (typeof tool !== \"object\" || !tool) return false;\n if (\n \"type\" in tool &&\n tool.type === \"function\" &&\n \"function\" in tool &&\n typeof tool.function === \"object\" &&\n tool.function &&\n \"name\" in tool.function &&\n \"parameters\" in tool.function\n ) {\n return true;\n }\n return false;\n}\n\ninterface CalculateMaxTokenProps {\n prompt: string;\n modelName: TiktokenModel;\n}\n\nexport const calculateMaxTokens = async ({\n prompt,\n modelName,\n}: CalculateMaxTokenProps) => {\n let numTokens;\n\n try {\n numTokens = (\n await encodingForModel(getModelNameForTiktoken(modelName))\n ).encode(prompt).length;\n } catch {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\"\n );\n\n // fallback to approximate calculation if tiktoken is not available\n // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#\n numTokens = Math.ceil(prompt.length / 4);\n }\n\n const maxTokens = getModelContextSize(modelName);\n return maxTokens - numTokens;\n};\n\nconst getVerbosity = () => false;\n\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n} & Record<string, any>;\n\nexport interface BaseLangChainParams {\n verbose?: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Base class for language models, chains, tools.\n */\nexport abstract class BaseLangChain<\n RunInput,\n RunOutput,\n CallOptions extends RunnableConfig = RunnableConfig,\n>\n extends Runnable<RunInput, RunOutput, CallOptions>\n implements BaseLangChainParams\n{\n /**\n * Whether to print out response text.\n */\n verbose: boolean;\n\n callbacks?: Callbacks;\n\n tags?: string[];\n\n metadata?: Record<string, unknown>;\n\n get lc_attributes(): { [key: string]: undefined } | undefined {\n return {\n callbacks: undefined,\n verbose: undefined,\n };\n }\n\n constructor(params: BaseLangChainParams) {\n super(params);\n this.verbose = params.verbose ?? getVerbosity();\n this.callbacks = params.callbacks;\n this.tags = params.tags ?? [];\n this.metadata = params.metadata ?? {};\n this._addVersion(\"@langchain/core\", __PKG_VERSION__);\n }\n\n protected _addVersion(pkg: string, version: string) {\n const existing = this.metadata?.versions;\n this.metadata = {\n ...this.metadata,\n versions: {\n ...(typeof existing === \"object\" && existing !== null ? existing : {}),\n [pkg]: version,\n },\n };\n }\n}\n\n/**\n * Base interface for language model parameters.\n * A subclass of {@link BaseLanguageModel} should have a constructor that\n * takes in a parameter that extends this interface.\n */\nexport interface BaseLanguageModelParams\n extends AsyncCallerParams, BaseLangChainParams {\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n\n cache?: BaseCache | boolean;\n}\n\nexport interface BaseLanguageModelTracingCallOptions {\n /**\n * Describes the format of structured outputs.\n * This should be provided if an output is considered to be structured\n */\n ls_structured_output_format?: {\n /**\n * An object containing the method used for structured output (e.g., \"jsonMode\").\n */\n kwargs: { method: string };\n /**\n * The JSON schema describing the expected output structure.\n */\n schema?: JSONSchema;\n };\n}\n\nexport interface BaseLanguageModelCallOptions\n extends RunnableConfig, BaseLanguageModelTracingCallOptions {\n /**\n * Stop tokens to use for this call.\n * If not provided, the default stop tokens for the model will be used.\n */\n stop?: string[];\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for\n * examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown> | JSONSchema;\n\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallOption = {\n name: string;\n};\n\nexport interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {\n function_call?: FunctionCallOption;\n functions?: FunctionDefinition[];\n}\n\nexport type BaseLanguageModelInput =\n | BasePromptValueInterface\n | string\n | BaseMessageLike[];\n\nexport type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;\n\nexport type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =\n {\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\" | \"jsonSchema\" | string;\n includeRaw?: IncludeRaw;\n /** Whether to use strict mode. Currently only supported by OpenAI models. */\n strict?: boolean;\n };\n\n/** @deprecated Use StructuredOutputMethodOptions instead */\nexport type StructuredOutputMethodParams<\n RunOutput,\n IncludeRaw extends boolean = false,\n> = {\n /** @deprecated Pass schema in as the first argument */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: InteropZodType<RunOutput> | Record<string, any>;\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\";\n includeRaw?: IncludeRaw;\n};\n\nexport interface BaseLanguageModelInterface<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {\n get callKeys(): string[];\n\n generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | Partial<CallOptions>,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n _modelType(): string;\n\n _llmType(): string;\n\n getNumTokens(content: MessageContent): Promise<number>;\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any>;\n\n serialize(): SerializedLLM;\n}\n\nexport type LanguageModelOutput = BaseMessage | string;\n\nexport type LanguageModelLike = RunnableInterface<\n BaseLanguageModelInput,\n LanguageModelOutput\n>;\n\n/**\n * Base class for language models.\n */\nexport abstract class BaseLanguageModel<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n>\n extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions>\n implements\n BaseLanguageModelParams,\n BaseLanguageModelInterface<RunOutput, CallOptions>\n{\n /**\n * Keys that the language model accepts as call options.\n */\n get callKeys(): string[] {\n return [\"stop\", \"timeout\", \"signal\", \"tags\", \"metadata\", \"callbacks\"];\n }\n\n /**\n * The async caller should be used by subclasses to make any async calls,\n * which will thus benefit from the concurrency and retry logic.\n */\n caller: AsyncCaller;\n\n cache?: BaseCache;\n\n constructor({\n callbacks,\n callbackManager,\n ...params\n }: BaseLanguageModelParams) {\n const { cache, ...rest } = params;\n super({\n callbacks: callbacks ?? callbackManager,\n ...rest,\n });\n if (typeof cache === \"object\") {\n this.cache = cache;\n } else if (cache) {\n this.cache = InMemoryCache.global();\n } else {\n this.cache = undefined;\n }\n this.caller = new AsyncCaller(params ?? {});\n }\n\n abstract generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | CallOptions,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n abstract _modelType(): string;\n\n abstract _llmType(): string;\n\n private _encoding?: Tiktoken;\n\n /**\n * Get the number of tokens in the content.\n * @param content The content to get the number of tokens for.\n * @returns The number of tokens in the content.\n */\n async getNumTokens(content: MessageContent) {\n // Extract text content from MessageContent\n let textContent: string;\n if (typeof content === \"string\") {\n textContent = content;\n } else {\n /**\n * Content is an array of ContentBlock\n *\n * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.\n * We need to find a better way to do this.\n * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116\n */\n textContent = content\n .map((item) => {\n if (typeof item === \"string\") return item;\n if (item.type === \"text\" && \"text\" in item) return item.text;\n return \"\";\n })\n .join(\"\");\n }\n\n // fallback to approximate calculation if tiktoken is not available\n let numTokens = Math.ceil(textContent.length / 4);\n\n if (!this._encoding) {\n try {\n this._encoding = await encodingForModel(\n \"modelName\" in this\n ? getModelNameForTiktoken(this.modelName as string)\n : \"gpt2\"\n );\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n if (this._encoding) {\n try {\n numTokens = this._encoding.encode(textContent).length;\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n return numTokens;\n }\n\n protected static _convertInputToPromptValue(\n input: BaseLanguageModelInput\n ): BasePromptValueInterface {\n if (typeof input === \"string\") {\n return new StringPromptValue(input);\n } else if (Array.isArray(input)) {\n return new ChatPromptValue(input.map(coerceMessageLikeToMessage));\n } else {\n return input;\n }\n }\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any> {\n return {};\n }\n\n /**\n * Create a unique cache key for a specific call to a specific language model.\n * @param callOptions Call options for the model\n * @returns A unique cache key.\n */\n _getSerializedCacheKeyParametersForCall(\n // TODO: Fix when we remove the RunnableLambda backwards compatibility shim.\n {\n config,\n ...callOptions\n }: CallOptions & { config?: RunnableConfig }\n ): string {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const params: Record<string, any> = {\n ...this._identifyingParams(),\n ...callOptions,\n _type: this._llmType(),\n _model: this._modelType(),\n };\n const filteredEntries = Object.entries(params).filter(\n ([_, value]) => value !== undefined\n );\n const serializedEntries = filteredEntries\n .map(([key, value]) => `${key}:${JSON.stringify(value)}`)\n .sort()\n .join(\",\");\n return serializedEntries;\n }\n\n /**\n * @deprecated\n * Return a json-like object representing this LLM.\n */\n serialize(): SerializedLLM {\n return {\n ...this._identifyingParams(),\n _type: this._llmType(),\n _model: this._modelType(),\n };\n }\n\n /**\n * @deprecated\n * Load an LLM from a json-like object describing it.\n */\n static async deserialize(_data: SerializedLLM): Promise<BaseLanguageModel> {\n throw new Error(\"Use .toJSON() instead\");\n }\n\n /**\n * Return profiling information for the model.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n */\n get profile(): ModelProfile {\n return {};\n }\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n /**\n * Model wrapper that returns outputs formatted to match the given schema.\n *\n * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.\n * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.\n *\n * @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.\n * If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.\n * @param {string} name The name of the function to call.\n * @param {\"functionCalling\" | \"jsonMode\"} [method=functionCalling] The method to use for getting the structured output. Defaults to \"functionCalling\".\n * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.\n * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.\n */\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | InteropZodType<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n {\n raw: BaseMessage;\n parsed: RunOutput;\n }\n >;\n\n /**\n * Filter out large/inappropriate fields from invocation params for tracing metadata.\n * Removes fields like tools, functions, messages, response_format that can be large.\n */\n protected _filterInvocationParamsForTracing(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ): Record<string, any> {\n const { tools, functions, messages, response_format, ...rest } = params;\n return rest;\n }\n}\n\n/**\n * Shared interface for token usage\n * return type from LLM calls.\n */\nexport interface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgCA,MAAa,2BAA2B,cAAqC;CAC3E,IAAI,UAAU,WAAW,OAAO,GAC9B,OAAO;CAGT,IAAI,UAAU,WAAW,mBAAmB,GAC1C,OAAO;CAGT,IAAI,UAAU,WAAW,gBAAgB,GACvC,OAAO;CAGT,IAAI,UAAU,WAAW,WAAW,GAClC,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,OAAO;AACT;AAEA,MAAa,2BAA2B,cAA+B;CACrE,QAAQ,WAAR;EACE,KAAK,0BACH,OAAO;EACT,SACE,OAAO;CACX;AACF;;;;;;;;;;;;AAaA,MAAa,uBAAuB,cAA8B;CAGhE,QAFuB,wBAAwB,SAE1B,GAArB;EAEE,KAAK;EACL,KAAK;EACL,KAAK,uBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,qBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK,kBACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,cACH,OAAO;EAGT,KAAK;EACL,KAAK,0BACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK,oBACH,OAAO;EACT,KAAK,oBACH,OAAO;EACT,KAAK;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I,OAAO,UAAU,UACnB,OAAO,IAAI,kBAAkB,KAAK;OAC7B,IAAI,MAAM,QAAQ,KAAK,GAC5B,OAAO,IAAI,gBAAgB,MAAM,IAAI,0BAA0B,CAAC;OAEhE,OAAO;CAEX;;;;CAMA,qBAA0C;EACxC,OAAO,CAAC;CACV;;;;;;CAOA,wCAEE,EACE,QACA,GAAG,eAEG;EAER,MAAM,SAA8B;GAClC,GAAG,KAAK,mBAAmB;GAC3B,GAAG;GACH,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;EAQA,OAPwB,OAAO,QAAQ,MAAM,CAAC,CAAC,QAC5C,CAAC,GAAG,WAAW,UAAU,KAAA,CAEY,CAAC,CACtC,KAAK,CAAC,KAAK,WAAW,GAAG,IAAI,GAAG,KAAK,UAAU,KAAK,GAAG,CAAC,CACxD,KAAK,CAAC,CACN,KAAK,GACe;CACzB;;;;;CAMA,YAA2B;EACzB,OAAO;GACL,GAAG,KAAK,mBAAmB;GAC3B,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;CACF;;;;;CAMA,aAAa,YAAY,OAAkD;EACzE,MAAM,IAAI,MAAM,uBAAuB;CACzC;;;;;;CAOA,IAAI,UAAwB;EAC1B,OAAO,CAAC;CACV;;;;;CAkGA,kCAEE,QAEqB;EACrB,MAAM,EAAE,OAAO,WAAW,UAAU,iBAAiB,GAAG,SAAS;EACjE,OAAO;CACT;AACF"}
|
|
1
|
+
{"version":3,"file":"base.js","names":[],"sources":["../../src/language_models/base.ts"],"sourcesContent":["import type { Tiktoken, TiktokenModel } from \"js-tiktoken/lite\";\nimport type { ZodV3Like, ZodV4Like } from \"../utils/types/zod.js\";\n\nimport { type BaseCache, InMemoryCache } from \"../caches/index.js\";\nimport {\n type BasePromptValueInterface,\n StringPromptValue,\n ChatPromptValue,\n} from \"../prompt_values.js\";\nimport {\n type BaseMessage,\n type BaseMessageLike,\n type MessageContent,\n} from \"../messages/base.js\";\nimport { coerceMessageLikeToMessage } from \"../messages/utils.js\";\nimport { type LLMResult } from \"../outputs.js\";\nimport { CallbackManager, Callbacks } from \"../callbacks/manager.js\";\nimport { AsyncCaller, AsyncCallerParams } from \"../utils/async_caller.js\";\nimport { encodingForModel } from \"../utils/tiktoken.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { JSONSchema } from \"../utils/json_schema.js\";\nimport {\n InferInteropZodOutput,\n InteropZodObject,\n InteropZodType,\n} from \"../utils/types/zod.js\";\nimport { ModelProfile } from \"./profile.js\";\nimport { type SerializableSchema } from \"../utils/standard_schema.js\";\n\n// https://www.npmjs.com/package/js-tiktoken\n\nexport const getModelNameForTiktoken = (modelName: string): TiktokenModel => {\n if (modelName.startsWith(\"gpt-5\")) {\n return \"gpt-5\" as TiktokenModel;\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-16k\")) {\n return \"gpt-3.5-turbo-16k\";\n }\n\n if (modelName.startsWith(\"gpt-3.5-turbo-\")) {\n return \"gpt-3.5-turbo\";\n }\n\n if (modelName.startsWith(\"gpt-4-32k\")) {\n return \"gpt-4-32k\";\n }\n\n if (modelName.startsWith(\"gpt-4-\")) {\n return \"gpt-4\";\n }\n\n if (modelName.startsWith(\"gpt-4o\")) {\n return \"gpt-4o\";\n }\n\n return modelName as TiktokenModel;\n};\n\nexport const getEmbeddingContextSize = (modelName?: string): number => {\n switch (modelName) {\n case \"text-embedding-ada-002\":\n return 8191;\n default:\n return 2046;\n }\n};\n\n/**\n * Get the context window size (max input tokens) for a given model.\n *\n * Context window sizes are sourced from official model documentation:\n * - OpenAI: https://platform.openai.com/docs/models\n * - Anthropic: https://docs.anthropic.com/claude/docs/models-overview\n * - Google: https://ai.google.dev/gemini/docs/models/gemini\n *\n * @param modelName - The name of the model\n * @returns The context window size in tokens\n */\nexport const getModelContextSize = (modelName: string): number => {\n const normalizedName = getModelNameForTiktoken(modelName) as string;\n\n switch (normalizedName) {\n // GPT-5 series\n case \"gpt-5\":\n case \"gpt-5-turbo\":\n case \"gpt-5-turbo-preview\":\n return 400000;\n\n // GPT-4o series\n case \"gpt-4o\":\n case \"gpt-4o-mini\":\n case \"gpt-4o-2024-05-13\":\n case \"gpt-4o-2024-08-06\":\n return 128000;\n\n // GPT-4 Turbo series\n case \"gpt-4-turbo\":\n case \"gpt-4-turbo-preview\":\n case \"gpt-4-turbo-2024-04-09\":\n case \"gpt-4-0125-preview\":\n case \"gpt-4-1106-preview\":\n return 128000;\n\n // GPT-4 series\n case \"gpt-4-32k\":\n case \"gpt-4-32k-0314\":\n case \"gpt-4-32k-0613\":\n return 32768;\n case \"gpt-4\":\n case \"gpt-4-0314\":\n case \"gpt-4-0613\":\n return 8192;\n\n // GPT-3.5 Turbo series\n case \"gpt-3.5-turbo-16k\":\n case \"gpt-3.5-turbo-16k-0613\":\n return 16384;\n case \"gpt-3.5-turbo\":\n case \"gpt-3.5-turbo-0301\":\n case \"gpt-3.5-turbo-0613\":\n case \"gpt-3.5-turbo-1106\":\n case \"gpt-3.5-turbo-0125\":\n return 4096;\n\n // Legacy GPT-3 models\n case \"text-davinci-003\":\n case \"text-davinci-002\":\n return 4097;\n case \"text-davinci-001\":\n return 2049;\n case \"text-curie-001\":\n case \"text-babbage-001\":\n case \"text-ada-001\":\n return 2048;\n\n // Code models\n case \"code-davinci-002\":\n case \"code-davinci-001\":\n return 8000;\n case \"code-cushman-001\":\n return 2048;\n\n // Claude models (Anthropic)\n case \"claude-3-5-sonnet-20241022\":\n case \"claude-3-5-sonnet-20240620\":\n case \"claude-3-opus-20240229\":\n case \"claude-3-sonnet-20240229\":\n case \"claude-3-haiku-20240307\":\n case \"claude-2.1\":\n return 200000;\n case \"claude-2.0\":\n case \"claude-instant-1.2\":\n return 100000;\n\n // Gemini models (Google)\n case \"gemini-1.5-pro\":\n case \"gemini-1.5-pro-latest\":\n case \"gemini-1.5-flash\":\n case \"gemini-1.5-flash-latest\":\n return 1000000; // 1M tokens\n case \"gemini-pro\":\n case \"gemini-pro-vision\":\n return 32768;\n\n default:\n return 4097;\n }\n};\n\n/**\n * Whether or not the input matches the OpenAI tool definition.\n * @param {unknown} tool The input to check.\n * @returns {boolean} Whether the input is an OpenAI tool definition.\n */\nexport function isOpenAITool(tool: unknown): tool is ToolDefinition {\n if (typeof tool !== \"object\" || !tool) return false;\n if (\n \"type\" in tool &&\n tool.type === \"function\" &&\n \"function\" in tool &&\n typeof tool.function === \"object\" &&\n tool.function &&\n \"name\" in tool.function &&\n \"parameters\" in tool.function\n ) {\n return true;\n }\n return false;\n}\n\ninterface CalculateMaxTokenProps {\n prompt: string;\n modelName: TiktokenModel;\n}\n\nexport const calculateMaxTokens = async ({\n prompt,\n modelName,\n}: CalculateMaxTokenProps) => {\n let numTokens;\n\n try {\n numTokens = (\n await encodingForModel(getModelNameForTiktoken(modelName))\n ).encode(prompt).length;\n } catch {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\"\n );\n\n // fallback to approximate calculation if tiktoken is not available\n // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#\n numTokens = Math.ceil(prompt.length / 4);\n }\n\n const maxTokens = getModelContextSize(modelName);\n return maxTokens - numTokens;\n};\n\nconst getVerbosity = () => false;\n\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n} & Record<string, any>;\n\nexport interface BaseLangChainParams {\n verbose?: boolean;\n callbacks?: Callbacks;\n tags?: string[];\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Base class for language models, chains, tools.\n */\nexport abstract class BaseLangChain<\n RunInput,\n RunOutput,\n CallOptions extends RunnableConfig = RunnableConfig,\n>\n extends Runnable<RunInput, RunOutput, CallOptions>\n implements BaseLangChainParams\n{\n /**\n * Whether to print out response text.\n */\n verbose: boolean;\n\n callbacks?: Callbacks;\n\n tags?: string[];\n\n metadata?: Record<string, unknown>;\n\n get lc_attributes(): { [key: string]: undefined } | undefined {\n return {\n callbacks: undefined,\n verbose: undefined,\n };\n }\n\n constructor(params: BaseLangChainParams) {\n super(params);\n this.verbose = params.verbose ?? getVerbosity();\n this.callbacks = params.callbacks;\n this.tags = params.tags ?? [];\n this.metadata = params.metadata ?? {};\n this._addVersion(\"@langchain/core\", __PKG_VERSION__);\n }\n\n protected _addVersion(pkg: string, version: string) {\n const existing = this.metadata?.versions;\n this.metadata = {\n ...this.metadata,\n versions: {\n ...(typeof existing === \"object\" && existing !== null ? existing : {}),\n [pkg]: version,\n },\n };\n }\n}\n\n/**\n * Base interface for language model parameters.\n * A subclass of {@link BaseLanguageModel} should have a constructor that\n * takes in a parameter that extends this interface.\n */\nexport interface BaseLanguageModelParams\n extends AsyncCallerParams, BaseLangChainParams {\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n\n cache?: BaseCache | boolean;\n}\n\nexport interface BaseLanguageModelTracingCallOptions {\n /**\n * Describes the format of structured outputs.\n * This should be provided if an output is considered to be structured\n */\n ls_structured_output_format?: {\n /**\n * An object containing the method used for structured output (e.g., \"jsonMode\").\n */\n kwargs: { method: string };\n /**\n * The JSON schema describing the expected output structure.\n */\n schema?: JSONSchema;\n };\n}\n\nexport interface BaseLanguageModelCallOptions\n extends RunnableConfig, BaseLanguageModelTracingCallOptions {\n /**\n * Stop tokens to use for this call.\n * If not provided, the default stop tokens for the model will be used.\n */\n stop?: string[];\n /**\n * Overrides the model's configured `maxRetries` for this call only.\n */\n maxRetries?: number;\n}\n\nexport interface FunctionDefinition {\n /**\n * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain\n * underscores and dashes, with a maximum length of 64.\n */\n name: string;\n\n /**\n * The parameters the functions accepts, described as a JSON Schema object. See the\n * [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for\n * examples, and the\n * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for\n * documentation about the format.\n *\n * To describe a function that accepts no parameters, provide the value\n * `{\"type\": \"object\", \"properties\": {}}`.\n */\n parameters: Record<string, unknown> | JSONSchema;\n\n /**\n * A description of what the function does, used by the model to choose when and\n * how to call the function.\n */\n description?: string;\n}\n\nexport interface ToolDefinition {\n type: \"function\";\n function: FunctionDefinition;\n}\n\nexport type FunctionCallOption = {\n name: string;\n};\n\nexport interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {\n function_call?: FunctionCallOption;\n functions?: FunctionDefinition[];\n}\n\nexport type BaseLanguageModelInput =\n | BasePromptValueInterface\n | string\n | BaseMessageLike[];\n\nexport type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;\n\nexport type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =\n {\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\" | \"jsonSchema\" | string;\n includeRaw?: IncludeRaw;\n /** Whether to use strict mode. Currently only supported by OpenAI models. */\n strict?: boolean;\n };\n\n/** @deprecated Use StructuredOutputMethodOptions instead */\nexport type StructuredOutputMethodParams<\n RunOutput,\n IncludeRaw extends boolean = false,\n> = {\n /** @deprecated Pass schema in as the first argument */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n schema: InteropZodType<RunOutput> | Record<string, any>;\n name?: string;\n method?: \"functionCalling\" | \"jsonMode\";\n includeRaw?: IncludeRaw;\n};\n\nexport interface BaseLanguageModelInterface<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {\n get callKeys(): string[];\n\n generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | Partial<CallOptions>,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n _modelType(): string;\n\n _llmType(): string;\n\n getNumTokens(content: MessageContent): Promise<number>;\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any>;\n\n serialize(): SerializedLLM;\n}\n\nexport type LanguageModelOutput = BaseMessage | string;\n\nexport type LanguageModelLike = RunnableInterface<\n BaseLanguageModelInput,\n LanguageModelOutput\n>;\n\n/**\n * Base class for language models.\n */\nexport abstract class BaseLanguageModel<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput = any,\n CallOptions extends BaseLanguageModelCallOptions =\n BaseLanguageModelCallOptions,\n>\n extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions>\n implements\n BaseLanguageModelParams,\n BaseLanguageModelInterface<RunOutput, CallOptions>\n{\n /**\n * Keys that the language model accepts as call options.\n */\n get callKeys(): string[] {\n return [\"stop\", \"timeout\", \"signal\", \"tags\", \"metadata\", \"callbacks\"];\n }\n\n /**\n * The async caller should be used by subclasses to make any async calls,\n * which will thus benefit from the concurrency and retry logic.\n */\n caller: AsyncCaller;\n\n cache?: BaseCache;\n\n constructor({\n callbacks,\n callbackManager,\n ...params\n }: BaseLanguageModelParams) {\n const { cache, ...rest } = params;\n super({\n callbacks: callbacks ?? callbackManager,\n ...rest,\n });\n if (typeof cache === \"object\") {\n this.cache = cache;\n } else if (cache) {\n this.cache = InMemoryCache.global();\n } else {\n this.cache = undefined;\n }\n this.caller = new AsyncCaller(params ?? {});\n }\n\n abstract generatePrompt(\n promptValues: BasePromptValueInterface[],\n options?: string[] | CallOptions,\n callbacks?: Callbacks\n ): Promise<LLMResult>;\n\n abstract _modelType(): string;\n\n abstract _llmType(): string;\n\n private _encoding?: Tiktoken;\n\n /**\n * Get the number of tokens in the content.\n * @param content The content to get the number of tokens for.\n * @returns The number of tokens in the content.\n */\n async getNumTokens(content: MessageContent) {\n // Extract text content from MessageContent\n let textContent: string;\n if (typeof content === \"string\") {\n textContent = content;\n } else {\n /**\n * Content is an array of ContentBlock\n *\n * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.\n * We need to find a better way to do this.\n * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116\n */\n textContent = content\n .map((item) => {\n if (typeof item === \"string\") return item;\n if (item.type === \"text\" && \"text\" in item) return item.text;\n return \"\";\n })\n .join(\"\");\n }\n\n // fallback to approximate calculation if tiktoken is not available\n let numTokens = Math.ceil(textContent.length / 4);\n\n if (!this._encoding) {\n try {\n this._encoding = await encodingForModel(\n \"modelName\" in this\n ? getModelNameForTiktoken(this.modelName as string)\n : \"gpt2\"\n );\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n if (this._encoding) {\n try {\n numTokens = this._encoding.encode(textContent).length;\n } catch (error) {\n console.warn(\n \"Failed to calculate number of tokens, falling back to approximate count\",\n error\n );\n }\n }\n\n return numTokens;\n }\n\n protected static _convertInputToPromptValue(\n input: BaseLanguageModelInput\n ): BasePromptValueInterface {\n if (typeof input === \"string\") {\n return new StringPromptValue(input);\n } else if (Array.isArray(input)) {\n return new ChatPromptValue(input.map(coerceMessageLikeToMessage));\n } else {\n return input;\n }\n }\n\n /**\n * Get the identifying parameters of the LLM.\n */\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n _identifyingParams(): Record<string, any> {\n return {};\n }\n\n /**\n * Create a unique cache key for a specific call to a specific language model.\n * @param callOptions Call options for the model\n * @returns A unique cache key.\n */\n _getSerializedCacheKeyParametersForCall(\n // TODO: Fix when we remove the RunnableLambda backwards compatibility shim.\n {\n config,\n ...callOptions\n }: CallOptions & { config?: RunnableConfig }\n ): string {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const params: Record<string, any> = {\n ...this._identifyingParams(),\n ...callOptions,\n _type: this._llmType(),\n _model: this._modelType(),\n };\n const filteredEntries = Object.entries(params).filter(\n ([_, value]) => value !== undefined\n );\n const serializedEntries = filteredEntries\n .map(([key, value]) => `${key}:${JSON.stringify(value)}`)\n .sort()\n .join(\",\");\n return serializedEntries;\n }\n\n /**\n * @deprecated\n * Return a json-like object representing this LLM.\n */\n serialize(): SerializedLLM {\n return {\n ...this._identifyingParams(),\n _type: this._llmType(),\n _model: this._modelType(),\n };\n }\n\n /**\n * @deprecated\n * Load an LLM from a json-like object describing it.\n */\n static async deserialize(_data: SerializedLLM): Promise<BaseLanguageModel> {\n throw new Error(\"Use .toJSON() instead\");\n }\n\n /**\n * Return profiling information for the model.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n */\n get profile(): ModelProfile {\n return {};\n }\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema: SerializableSchema<RunOutput>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV3Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | ZodV4Like<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n /**\n * Model wrapper that returns outputs formatted to match the given schema.\n *\n * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.\n * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.\n *\n * @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.\n * If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.\n * @param {string} name The name of the function to call.\n * @param {\"functionCalling\" | \"jsonMode\"} [method=functionCalling] The method to use for getting the structured output. Defaults to \"functionCalling\".\n * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.\n * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.\n */\n withStructuredOutput?<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n schema:\n | InteropZodType<RunOutput>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n {\n raw: BaseMessage;\n parsed: RunOutput;\n }\n >;\n\n /**\n * Filter out large/inappropriate fields from invocation params for tracing metadata.\n * Removes fields like tools, functions, messages, response_format that can be large.\n */\n protected _filterInvocationParamsForTracing(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n params: Record<string, any>\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n ): Record<string, any> {\n const { tools, functions, messages, response_format, ...rest } = params;\n return rest;\n }\n}\n\n/**\n * Shared interface for token usage\n * return type from LLM calls.\n */\nexport interface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgCA,MAAa,2BAA2B,cAAqC;CAC3E,IAAI,UAAU,WAAW,OAAO,GAC9B,OAAO;CAGT,IAAI,UAAU,WAAW,mBAAmB,GAC1C,OAAO;CAGT,IAAI,UAAU,WAAW,gBAAgB,GACvC,OAAO;CAGT,IAAI,UAAU,WAAW,WAAW,GAClC,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,IAAI,UAAU,WAAW,QAAQ,GAC/B,OAAO;CAGT,OAAO;AACT;AAEA,MAAa,2BAA2B,cAA+B;CACrE,QAAQ,WAAR;EACE,KAAK,0BACH,OAAO;EACT,SACE,OAAO;CACX;AACF;;;;;;;;;;;;AAaA,MAAa,uBAAuB,cAA8B;CAGhE,QAFuB,wBAAwB,SAE1B,GAArB;EAEE,KAAK;EACL,KAAK;EACL,KAAK,uBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,qBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK;EACL,KAAK,kBACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,cACH,OAAO;EAGT,KAAK;EACL,KAAK,0BACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,sBACH,OAAO;EAGT,KAAK;EACL,KAAK,oBACH,OAAO;EACT,KAAK,oBACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,gBACH,OAAO;EAGT,KAAK;EACL,KAAK,oBACH,OAAO;EACT,KAA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K,GAC5B,OAAO,IAAI,gBAAgB,MAAM,IAAI,0BAA0B,CAAC;OAEhE,OAAO;CAEX;;;;CAMA,qBAA0C;EACxC,OAAO,CAAC;CACV;;;;;;CAOA,wCAEE,EACE,QACA,GAAG,eAEG;EAER,MAAM,SAA8B;GAClC,GAAG,KAAK,mBAAmB;GAC3B,GAAG;GACH,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;EAQA,OAPwB,OAAO,QAAQ,MAAM,CAAC,CAAC,QAC5C,CAAC,GAAG,WAAW,UAAU,KAAA,CAEY,CAAC,CACtC,KAAK,CAAC,KAAK,WAAW,GAAG,IAAI,GAAG,KAAK,UAAU,KAAK,GAAG,CAAC,CACxD,KAAK,CAAC,CACN,KAAK,GACe;CACzB;;;;;CAMA,YAA2B;EACzB,OAAO;GACL,GAAG,KAAK,mBAAmB;GAC3B,OAAO,KAAK,SAAS;GACrB,QAAQ,KAAK,WAAW;EAC1B;CACF;;;;;CAMA,aAAa,YAAY,OAAkD;EACzE,MAAM,IAAI,MAAM,uBAAuB;CACzC;;;;;;CAOA,IAAI,UAAwB;EAC1B,OAAO,CAAC;CACV;;;;;CAkGA,kCAEE,QAEqB;EACrB,MAAM,EAAE,OAAO,WAAW,UAAU,iBAAiB,GAAG,SAAS;EACjE,OAAO;CACT;AACF"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
const require_errors_index = require("../errors/index.cjs");
|
|
3
4
|
const require_signal = require("./signal.cjs");
|
|
4
5
|
const require_index = require("./p-retry/index.cjs");
|
|
5
6
|
let p_queue = require("p-queue");
|
|
@@ -19,7 +20,8 @@ const STATUS_NO_RETRY = [
|
|
|
19
20
|
405,
|
|
20
21
|
406,
|
|
21
22
|
407,
|
|
22
|
-
409
|
|
23
|
+
409,
|
|
24
|
+
413
|
|
23
25
|
];
|
|
24
26
|
const RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 6e4;
|
|
25
27
|
const QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [
|
|
@@ -132,10 +134,11 @@ function classifyRateLimitError(error) {
|
|
|
132
134
|
*/
|
|
133
135
|
const defaultFailedAttemptHandler = (error) => {
|
|
134
136
|
if (typeof error !== "object" || error === null) return;
|
|
135
|
-
if (
|
|
137
|
+
if (require_errors_index.getRetryable(error) === false) throw error;
|
|
138
|
+
if ("message" in error && typeof error.message === "string" && (error.message.startsWith("Cancel") || error.message.startsWith("AbortError")) || "name" in error && typeof error.name === "string" && error.name === "AbortError") throw require_errors_index.stampRetryable(error, false);
|
|
136
139
|
if ("code" in error && typeof error.code === "string" && error.code === "ECONNABORTED") throw error;
|
|
137
140
|
const status = getResponseStatus(error) ?? getDirectStatus(error);
|
|
138
|
-
if (status && STATUS_NO_RETRY.includes(+status)) throw error;
|
|
141
|
+
if (status && STATUS_NO_RETRY.includes(+status)) throw require_errors_index.stampRetryable(error, false);
|
|
139
142
|
if (getErrorCode(error) === "insufficient_quota") {
|
|
140
143
|
const err = coerceError(error, getErrorMessage(error) ?? "Insufficient quota");
|
|
141
144
|
err.name = "InsufficientQuotaError";
|
|
@@ -143,18 +146,19 @@ const defaultFailedAttemptHandler = (error) => {
|
|
|
143
146
|
action: "stop",
|
|
144
147
|
reason: "insufficient_quota"
|
|
145
148
|
});
|
|
146
|
-
throw err;
|
|
149
|
+
throw require_errors_index.stampRetryable(err, false);
|
|
147
150
|
}
|
|
148
151
|
const rateLimitClassification = classifyRateLimitError(error);
|
|
149
152
|
if (rateLimitClassification) {
|
|
150
153
|
if (rateLimitClassification.action === "wait") {
|
|
151
154
|
setRateLimitMetadata(error, rateLimitClassification);
|
|
155
|
+
require_errors_index.stampRetryable(error, true);
|
|
152
156
|
return;
|
|
153
157
|
}
|
|
154
158
|
const err = coerceError(error, getErrorMessage(error) ?? "Rate limit exceeded");
|
|
155
159
|
if (err.name === "Error") err.name = rateLimitClassification.action === "stop" ? "RateLimitQuotaExhaustedError" : "RateLimitCapacityError";
|
|
156
160
|
setRateLimitMetadata(err, rateLimitClassification);
|
|
157
|
-
throw err;
|
|
161
|
+
throw require_errors_index.stampRetryable(err, rateLimitClassification.action !== "stop");
|
|
158
162
|
}
|
|
159
163
|
};
|
|
160
164
|
/**
|
|
@@ -183,19 +187,23 @@ var AsyncCaller = class {
|
|
|
183
187
|
this.queue = new PQueue({ concurrency: this.maxConcurrency });
|
|
184
188
|
}
|
|
185
189
|
async call(callable, ...args) {
|
|
190
|
+
return this.callWithRetries(this.maxRetries, callable, args);
|
|
191
|
+
}
|
|
192
|
+
callWithRetries(retries, callable, args) {
|
|
186
193
|
return this.queue.add(() => require_index.default(() => callable(...args).catch((error) => {
|
|
187
194
|
if (error instanceof Error) throw error;
|
|
188
195
|
else throw new Error(error);
|
|
189
196
|
}), {
|
|
190
197
|
onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),
|
|
191
|
-
retries
|
|
198
|
+
retries,
|
|
192
199
|
randomize: true
|
|
193
200
|
}), { throwOnTimeout: true });
|
|
194
201
|
}
|
|
195
202
|
callWithOptions(options, callable, ...args) {
|
|
203
|
+
const retries = options.maxRetries ?? this.maxRetries;
|
|
196
204
|
if (options.signal) {
|
|
197
205
|
let listener;
|
|
198
|
-
return Promise.race([this.
|
|
206
|
+
return Promise.race([this.callWithRetries(retries, callable, args), new Promise((_, reject) => {
|
|
199
207
|
listener = () => {
|
|
200
208
|
reject(require_signal.getAbortSignalError(options.signal));
|
|
201
209
|
};
|
|
@@ -204,7 +212,7 @@ var AsyncCaller = class {
|
|
|
204
212
|
if (options.signal && listener) options.signal.removeEventListener("abort", listener);
|
|
205
213
|
});
|
|
206
214
|
}
|
|
207
|
-
return this.
|
|
215
|
+
return this.callWithRetries(retries, callable, args);
|
|
208
216
|
}
|
|
209
217
|
fetch(...args) {
|
|
210
218
|
return this.call(() => fetch(...args).then((res) => res.ok ? res : Promise.reject(res)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.cjs","names":["PQueueMod","pRetry","getAbortSignalError"],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n throw err;\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n throw err;\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries: this.maxRetries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.call<A, T>(callable, ...args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.call<A, T>(callable, ...args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;AAKA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;CAC7D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WAAW,WAC/B,MAAM,SAAS,SACf,KAAA;AACN;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAGF,IAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAC/C,OAAO,MAAM;CAGf,IAAI,gBAAgB,SAAS,OAAO,MAAM,eAAe,UACvD,OAAO,MAAM;AAIjB;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,aAAa,SACb,OAAO,MAAM,YAAY,WACvB,MAAM,UACN,KAAA;AACN;AAEA,SAAS,aAAa,OAAoC;CACxD,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAGF,IAAI,UAAU,SAAS,OAAO,MAAM,SAAS,UAC3C,OAA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nB,MAAM;QAEN,MAAM,IAAI,MAAM,KAAK;EAEzB,CAAC,GACH;GACE,kBAAkB,EAAE,YAAY,KAAK,kBAAkB,KAAK;GAC5D,SAAS,KAAK;GACd,WAAW;EAGb,CACF,GACF,EAAE,gBAAgB,KAAK,CACzB;CACF;CAGA,gBACE,SACA,UACA,GAAG,MAC8B;EAGjC,IAAI,QAAQ,QAAQ;GAClB,IAAI;GACJ,OAAO,QAAQ,KAAK,CAClB,KAAK,KAAW,UAAU,GAAG,IAAI,GACjC,IAAI,SAAgB,GAAG,WAAW;IAChC,iBAAiB;KACf,OAAOC,eAAAA,oBAAoB,QAAQ,MAAM,CAAC;IAC5C;IACA,QAAQ,QAAQ,iBAAiB,SAAS,UAAU,EAAE,MAAM,KAAK,CAAC;GACpE,CAAC,CACH,CAAC,CAAC,CAAC,cAAc;IACf,IAAI,QAAQ,UAAU,UACpB,QAAQ,OAAO,oBAAoB,SAAS,QAAQ;GAExD,CAAC;EACH;EACA,OAAO,KAAK,KAAW,UAAU,GAAG,IAAI;CAC1C;CAEA,MAAM,GAAG,MAA0D;EACjE,OAAO,KAAK,WACV,MAAM,GAAG,IAAI,CAAC,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,GAAG,CAAE,CACnE;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"async_caller.cjs","names":["getRetryable","stampRetryable","PQueueMod","pRetry","getAbortSignalError"],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getRetryable, stampRetryable } from \"../errors/index.js\";\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n 413, // Payload Too Large\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n // Honor a verdict already reached inside the callable, e.g. by a provider.\n if (getRetryable(error) === false) {\n throw error;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n // Deliberate cancellation, not a failure worth another attempt.\n throw stampRetryable(error, false);\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n // Deterministic client error; retrying it unchanged fails identically.\n throw stampRetryable(error, false);\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n // Exhausted quota needs an account action, not another attempt.\n throw stampRetryable(err, false);\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n stampRetryable(error, true);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n // Only \"stop\" is exhausted quota; \"capacity\" can still succeed later.\n throw stampRetryable(err, rateLimitClassification.action !== \"stop\");\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n maxRetries?: number;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.callWithRetries(this.maxRetries, callable, args);\n }\n\n private callWithRetries<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n A extends any[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n T extends (...args: A) => Promise<any>,\n >(\n retries: AsyncCallerParams[\"maxRetries\"],\n callable: T,\n args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n const retries = options.maxRetries ?? this.maxRetries;\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.callWithRetries<A, T>(retries, callable, args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.callWithRetries<A, T>(retries, callable, args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAMA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;CAC7D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WAAW,WAC/B,MAAM,SAAS,SACf,KAAA;AACN;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,IAAI,OAAO,UAAU,YAAY,UAAU,MACzC;CAGF,IAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAC/C,OAAO,MAAM;CAGf,IAAI,gBAAgB,SAAS,OAAO,MAAM,eAAe,UACvD,OAAO,MAAM;AAIjB;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,aAAa,SACb,OAAO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S,IAAI,KAAK,MAAM,QAAQ,OAAO,GAAG,CAAE,CACnE;CACF;AACF"}
|
|
@@ -28,6 +28,7 @@ interface AsyncCallerParams {
|
|
|
28
28
|
}
|
|
29
29
|
interface AsyncCallerCallOptions {
|
|
30
30
|
signal?: AbortSignal;
|
|
31
|
+
maxRetries?: number;
|
|
31
32
|
}
|
|
32
33
|
/**
|
|
33
34
|
* A class that can be used to make async calls with concurrency and retry logic.
|
|
@@ -49,6 +50,7 @@ declare class AsyncCaller {
|
|
|
49
50
|
private queue;
|
|
50
51
|
constructor(params: AsyncCallerParams);
|
|
51
52
|
call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
53
|
+
private callWithRetries;
|
|
52
54
|
callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
53
55
|
fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
|
|
54
56
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.d.cts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"async_caller.d.cts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAmCK;KAEA;EACH,QAAQ;EACR;EACA;;iBA8Ic,kBACd;iBAyBc,uBACd,iBACC;KA0HS,wBAAwB;UAEnB;;;;;EAKf;;;;;EAKA;;;;;;EAMA,kBAAkB;;UAGH;EACf,SAAS;EACT;;;;;;;;;;;;;;;cAgBW;YACD,gBAAgB;YAEhB,YAAY;YAEZ,iBAAiB;UAEnB;EAER,YAAY,QAAQ;EAad,KAAK,iBAAiB,cAAc,MAAM,MAAM,cACpD,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;UAItB;EAmCR,gBAAgB,iBAAiB,cAAc,MAAM,MAAM,cACzD,SAAS,wBACT,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EAuB9B,SAAS,MAAM,kBAAkB,SAAS,kBAAkB"}
|
|
@@ -28,6 +28,7 @@ interface AsyncCallerParams {
|
|
|
28
28
|
}
|
|
29
29
|
interface AsyncCallerCallOptions {
|
|
30
30
|
signal?: AbortSignal;
|
|
31
|
+
maxRetries?: number;
|
|
31
32
|
}
|
|
32
33
|
/**
|
|
33
34
|
* A class that can be used to make async calls with concurrency and retry logic.
|
|
@@ -49,6 +50,7 @@ declare class AsyncCaller {
|
|
|
49
50
|
private queue;
|
|
50
51
|
constructor(params: AsyncCallerParams);
|
|
51
52
|
call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
53
|
+
private callWithRetries;
|
|
52
54
|
callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
53
55
|
fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
|
|
54
56
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.d.ts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"async_caller.d.ts","names":[],"sources":["../../src/utils/async_caller.ts"],"mappings":";KAmCK;KAEA;EACH,QAAQ;EACR;EACA;;iBA8Ic,kBACd;iBAyBc,uBACd,iBACC;KA0HS,wBAAwB;UAEnB;;;;;EAKf;;;;;EAKA;;;;;;EAMA,kBAAkB;;UAGH;EACf,SAAS;EACT;;;;;;;;;;;;;;;cAgBW;YACD,gBAAgB;YAEhB,YAAY;YAEZ,iBAAiB;UAEnB;EAER,YAAY,QAAQ;EAad,KAAK,iBAAiB,cAAc,MAAM,MAAM,cACpD,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;UAItB;EAmCR,gBAAgB,iBAAiB,cAAc,MAAM,MAAM,cACzD,SAAS,wBACT,UAAU,MACP,MAAM,WAAW,KACnB,QAAQ,QAAQ,WAAW;EAuB9B,SAAS,MAAM,kBAAkB,SAAS,kBAAkB"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { getRetryable, stampRetryable } from "../errors/index.js";
|
|
2
3
|
import { getAbortSignalError } from "./signal.js";
|
|
3
4
|
import pRetry from "./p-retry/index.js";
|
|
4
5
|
import PQueueMod from "p-queue";
|
|
@@ -17,7 +18,8 @@ const STATUS_NO_RETRY = [
|
|
|
17
18
|
405,
|
|
18
19
|
406,
|
|
19
20
|
407,
|
|
20
|
-
409
|
|
21
|
+
409,
|
|
22
|
+
413
|
|
21
23
|
];
|
|
22
24
|
const RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 6e4;
|
|
23
25
|
const QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [
|
|
@@ -130,10 +132,11 @@ function classifyRateLimitError(error) {
|
|
|
130
132
|
*/
|
|
131
133
|
const defaultFailedAttemptHandler = (error) => {
|
|
132
134
|
if (typeof error !== "object" || error === null) return;
|
|
133
|
-
if (
|
|
135
|
+
if (getRetryable(error) === false) throw error;
|
|
136
|
+
if ("message" in error && typeof error.message === "string" && (error.message.startsWith("Cancel") || error.message.startsWith("AbortError")) || "name" in error && typeof error.name === "string" && error.name === "AbortError") throw stampRetryable(error, false);
|
|
134
137
|
if ("code" in error && typeof error.code === "string" && error.code === "ECONNABORTED") throw error;
|
|
135
138
|
const status = getResponseStatus(error) ?? getDirectStatus(error);
|
|
136
|
-
if (status && STATUS_NO_RETRY.includes(+status)) throw error;
|
|
139
|
+
if (status && STATUS_NO_RETRY.includes(+status)) throw stampRetryable(error, false);
|
|
137
140
|
if (getErrorCode(error) === "insufficient_quota") {
|
|
138
141
|
const err = coerceError(error, getErrorMessage(error) ?? "Insufficient quota");
|
|
139
142
|
err.name = "InsufficientQuotaError";
|
|
@@ -141,18 +144,19 @@ const defaultFailedAttemptHandler = (error) => {
|
|
|
141
144
|
action: "stop",
|
|
142
145
|
reason: "insufficient_quota"
|
|
143
146
|
});
|
|
144
|
-
throw err;
|
|
147
|
+
throw stampRetryable(err, false);
|
|
145
148
|
}
|
|
146
149
|
const rateLimitClassification = classifyRateLimitError(error);
|
|
147
150
|
if (rateLimitClassification) {
|
|
148
151
|
if (rateLimitClassification.action === "wait") {
|
|
149
152
|
setRateLimitMetadata(error, rateLimitClassification);
|
|
153
|
+
stampRetryable(error, true);
|
|
150
154
|
return;
|
|
151
155
|
}
|
|
152
156
|
const err = coerceError(error, getErrorMessage(error) ?? "Rate limit exceeded");
|
|
153
157
|
if (err.name === "Error") err.name = rateLimitClassification.action === "stop" ? "RateLimitQuotaExhaustedError" : "RateLimitCapacityError";
|
|
154
158
|
setRateLimitMetadata(err, rateLimitClassification);
|
|
155
|
-
throw err;
|
|
159
|
+
throw stampRetryable(err, rateLimitClassification.action !== "stop");
|
|
156
160
|
}
|
|
157
161
|
};
|
|
158
162
|
/**
|
|
@@ -181,19 +185,23 @@ var AsyncCaller = class {
|
|
|
181
185
|
this.queue = new PQueue({ concurrency: this.maxConcurrency });
|
|
182
186
|
}
|
|
183
187
|
async call(callable, ...args) {
|
|
188
|
+
return this.callWithRetries(this.maxRetries, callable, args);
|
|
189
|
+
}
|
|
190
|
+
callWithRetries(retries, callable, args) {
|
|
184
191
|
return this.queue.add(() => pRetry(() => callable(...args).catch((error) => {
|
|
185
192
|
if (error instanceof Error) throw error;
|
|
186
193
|
else throw new Error(error);
|
|
187
194
|
}), {
|
|
188
195
|
onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),
|
|
189
|
-
retries
|
|
196
|
+
retries,
|
|
190
197
|
randomize: true
|
|
191
198
|
}), { throwOnTimeout: true });
|
|
192
199
|
}
|
|
193
200
|
callWithOptions(options, callable, ...args) {
|
|
201
|
+
const retries = options.maxRetries ?? this.maxRetries;
|
|
194
202
|
if (options.signal) {
|
|
195
203
|
let listener;
|
|
196
|
-
return Promise.race([this.
|
|
204
|
+
return Promise.race([this.callWithRetries(retries, callable, args), new Promise((_, reject) => {
|
|
197
205
|
listener = () => {
|
|
198
206
|
reject(getAbortSignalError(options.signal));
|
|
199
207
|
};
|
|
@@ -202,7 +210,7 @@ var AsyncCaller = class {
|
|
|
202
210
|
if (options.signal && listener) options.signal.removeEventListener("abort", listener);
|
|
203
211
|
});
|
|
204
212
|
}
|
|
205
|
-
return this.
|
|
213
|
+
return this.callWithRetries(retries, callable, args);
|
|
206
214
|
}
|
|
207
215
|
fetch(...args) {
|
|
208
216
|
return this.call(() => fetch(...args).then((res) => res.ok ? res : Promise.reject(res)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async_caller.js","names":[],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n throw error;\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n throw error;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n throw err;\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n throw err;\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries: this.maxRetries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.call<A, T>(callable, ...args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.call<A, T>(callable, ...args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;AAKA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;CAC7D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WAAW,WAC/B,MAAM,SAAS,SACf,KAAA;AACN;AAEA,SAAS,gBAAgB,OAAoC;CAC3D,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ts"],"sourcesContent":["import PQueueMod from \"p-queue\";\n\nimport { getRetryable, stampRetryable } from \"../errors/index.js\";\nimport { getAbortSignalError } from \"./signal.js\";\nimport pRetry from \"./p-retry/index.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n 413, // Payload Too Large\n];\n\nconst RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS = 60_000;\n\nconst QUOTA_EXHAUSTED_MESSAGE_PATTERNS = [\n /insufficient[_ -]?quota/i,\n /exceeded (?:your|the current|the available).+quota/i,\n /usage quota/i,\n /quota (?:has been )?exhausted/i,\n /billing/i,\n /credit balance/i,\n /out of credits/i,\n /will reset at/i,\n];\n\nconst RETRY_AFTER_MESSAGE_PATTERN =\n /(?:try again in|retry after)\\s+(\\d+(?:\\.\\d+)?)\\s*(milliseconds?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h)\\b/i;\n\ntype RateLimitAction = \"wait\" | \"capacity\" | \"stop\";\n\ntype RateLimitClassification = {\n action: RateLimitAction;\n retryAfterMs?: number;\n reason: string;\n};\n\nfunction getResponseStatus(error: unknown): number | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"response\" in error &&\n typeof error.response === \"object\" &&\n error.response !== null &&\n \"status\" in error.response &&\n typeof error.response.status === \"number\"\n ? error.response.status\n : undefined;\n}\n\nfunction getDirectStatus(error: unknown): number | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"status\" in error && typeof error.status === \"number\") {\n return error.status;\n }\n\n if (\"statusCode\" in error && typeof error.statusCode === \"number\") {\n return error.statusCode;\n }\n\n return undefined;\n}\n\nfunction getErrorMessage(error: unknown): string | undefined {\n return typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof error.message === \"string\"\n ? error.message\n : undefined;\n}\n\nfunction getErrorCode(error: unknown): string | undefined {\n if (typeof error !== \"object\" || error === null) {\n return undefined;\n }\n\n if (\"code\" in error && typeof error.code === \"string\") {\n return error.code;\n }\n\n return \"error\" in error &&\n typeof error.error === \"object\" &&\n error.error !== null &&\n \"code\" in error.error &&\n typeof error.error.code === \"string\"\n ? error.error.code\n : undefined;\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _getRetryAfterHeader(error: any): string | null | undefined {\n if (error?.headers) {\n if (typeof error.headers.get === \"function\") {\n return error.headers.get(\"retry-after\");\n }\n return error.headers[\"retry-after\"] ?? error.headers[\"Retry-After\"];\n }\n\n if (error?.response?.headers) {\n if (typeof error.response.headers.get === \"function\") {\n return error.response.headers.get(\"retry-after\");\n }\n return (\n error.response.headers[\"retry-after\"] ??\n error.response.headers[\"Retry-After\"]\n );\n }\n\n return undefined;\n}\n\nfunction parseRetryAfterFromMessageMs(\n message: string | undefined\n): number | undefined {\n if (message == null) {\n return undefined;\n }\n\n const match = RETRY_AFTER_MESSAGE_PATTERN.exec(message);\n if (!match) {\n return undefined;\n }\n\n const rawValue = Number(match[1]);\n const unit = match[2]?.toLowerCase();\n if (Number.isNaN(rawValue) || !unit) {\n return undefined;\n }\n\n if (unit === \"ms\" || unit.startsWith(\"millisecond\")) {\n return rawValue;\n }\n\n if (unit === \"m\" || unit.startsWith(\"min\")) {\n return rawValue * 60_000;\n }\n\n if (unit === \"h\" || unit.startsWith(\"hr\") || unit.startsWith(\"hour\")) {\n return rawValue * 3_600_000;\n }\n\n return rawValue * 1000;\n}\n\nfunction coerceError(error: unknown, fallbackMessage: string): Error {\n if (error instanceof Error) {\n return error;\n }\n\n const coerced = new Error(fallbackMessage);\n if (typeof error === \"object\" && error !== null) {\n Object.assign(coerced, error);\n }\n return coerced;\n}\n\nfunction setRateLimitMetadata(\n error: unknown,\n classification: RateLimitClassification\n) {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n const mutableError = error as Record<string, unknown>;\n mutableError.rateLimitType = classification.action;\n mutableError.rateLimitReason = classification.reason;\n\n if (classification.retryAfterMs !== undefined) {\n mutableError.retryAfterMs = classification.retryAfterMs;\n }\n}\n\nexport function parseRetryAfterMs(\n headerValue: string | null | undefined\n): number | undefined {\n if (headerValue == null) {\n return undefined;\n }\n\n const trimmed = headerValue.trim();\n if (!trimmed) {\n return undefined;\n }\n\n const seconds = Number(trimmed);\n if (!Number.isNaN(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const date = Date.parse(trimmed);\n if (!Number.isNaN(date)) {\n const delayMs = date - Date.now();\n return delayMs > 0 ? delayMs : 0;\n }\n\n return undefined;\n}\n\nexport function classifyRateLimitError(\n error: unknown\n): RateLimitClassification | undefined {\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status !== 429) {\n return undefined;\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n return { action: \"stop\", reason: \"insufficient_quota\" };\n }\n\n const message = getErrorMessage(error);\n if (\n message &&\n QUOTA_EXHAUSTED_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))\n ) {\n return { action: \"stop\", reason: \"quota_message\" };\n }\n\n const retryAfterMs =\n parseRetryAfterMs(_getRetryAfterHeader(error)) ??\n parseRetryAfterFromMessageMs(message);\n\n if (retryAfterMs !== undefined) {\n if (retryAfterMs <= RETRY_AFTER_AUTO_RETRY_THRESHOLD_MS) {\n return {\n action: \"wait\",\n retryAfterMs,\n reason: \"retry_after_hint\",\n };\n }\n\n return {\n action: \"capacity\",\n retryAfterMs,\n reason: \"retry_after_too_large\",\n };\n }\n\n return { action: \"capacity\", reason: \"headerless_429\" };\n}\n\n/**\n * The default failed attempt handler for the AsyncCaller.\n * @param error - The error to handle.\n * @returns void\n */\nconst defaultFailedAttemptHandler = (error: unknown) => {\n if (typeof error !== \"object\" || error === null) {\n return;\n }\n\n // Honor a verdict already reached inside the callable, e.g. by a provider.\n if (getRetryable(error) === false) {\n throw error;\n }\n\n if (\n (\"message\" in error &&\n typeof error.message === \"string\" &&\n (error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\"))) ||\n (\"name\" in error &&\n typeof error.name === \"string\" &&\n error.name === \"AbortError\")\n ) {\n // Deliberate cancellation, not a failure worth another attempt.\n throw stampRetryable(error, false);\n }\n if (\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code === \"ECONNABORTED\"\n ) {\n throw error;\n }\n const status = getResponseStatus(error) ?? getDirectStatus(error);\n if (status && STATUS_NO_RETRY.includes(+status)) {\n // Deterministic client error; retrying it unchanged fails identically.\n throw stampRetryable(error, false);\n }\n\n const code = getErrorCode(error);\n if (code === \"insufficient_quota\") {\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Insufficient quota\"\n );\n err.name = \"InsufficientQuotaError\";\n setRateLimitMetadata(err, {\n action: \"stop\",\n reason: \"insufficient_quota\",\n });\n // Exhausted quota needs an account action, not another attempt.\n throw stampRetryable(err, false);\n }\n\n const rateLimitClassification = classifyRateLimitError(error);\n if (rateLimitClassification) {\n if (rateLimitClassification.action === \"wait\") {\n setRateLimitMetadata(error, rateLimitClassification);\n stampRetryable(error, true);\n return;\n }\n\n const err = coerceError(\n error,\n getErrorMessage(error) ?? \"Rate limit exceeded\"\n );\n if (err.name === \"Error\") {\n err.name =\n rateLimitClassification.action === \"stop\"\n ? \"RateLimitQuotaExhaustedError\"\n : \"RateLimitCapacityError\";\n }\n setRateLimitMetadata(err, rateLimitClassification);\n // Only \"stop\" is exhausted quota; \"capacity\" can still succeed later.\n throw stampRetryable(err, rateLimitClassification.action !== \"stop\");\n }\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type FailedAttemptHandler = (error: any) => any;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n /**\n * Custom handler to handle failed attempts. Takes the originally thrown\n * error object as input, and should itself throw an error if the input\n * error is not retryable.\n */\n onFailedAttempt?: FailedAttemptHandler;\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n maxRetries?: number;\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 6. This\n * means that by default, each call will be retried up to 6 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n protected onFailedAttempt: AsyncCallerParams[\"onFailedAttempt\"];\n\n private queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 6;\n this.onFailedAttempt =\n params.onFailedAttempt ?? defaultFailedAttemptHandler;\n\n const PQueue = (\n \"default\" in PQueueMod ? PQueueMod.default : PQueueMod\n ) as typeof PQueueMod;\n this.queue = new PQueue({ concurrency: this.maxConcurrency });\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n async call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.callWithRetries(this.maxRetries, callable, args);\n }\n\n private callWithRetries<\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n A extends any[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n T extends (...args: A) => Promise<any>,\n >(\n retries: AsyncCallerParams[\"maxRetries\"],\n callable: T,\n args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...args).catch((error) => {\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else {\n throw new Error(error);\n }\n }),\n {\n onFailedAttempt: ({ error }) => this.onFailedAttempt?.(error),\n retries,\n randomize: true,\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n const retries = options.maxRetries ?? this.maxRetries;\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n let listener: (() => void) | undefined;\n return Promise.race([\n this.callWithRetries<A, T>(retries, callable, args),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(getAbortSignalError(options.signal));\n };\n options.signal?.addEventListener(\"abort\", listener, { once: true });\n }),\n ]).finally(() => {\n if (options.signal && listener) {\n options.signal.removeEventListener(\"abort\", listener);\n }\n });\n }\n return this.callWithRetries<A, T>(retries, callable, args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n return this.call(() =>\n fetch(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;AAMA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,sCAAsC;AAE5C,MAAM,mCAAmC;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,8BACJ;AAUF,SAAS,kBAAkB,OAAoC;CAC7D,OAAO,OAAO,UAAU,YACtB,UAAU,QACV,cAAc,SACd,OAAO,MAAM,aAAa,YAC1B,MAAM,aAAa,QACnB,YAAY,MAAM,YAClB,OAAO,MAAM,SAAS,WA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C,cAAc;IACf,IAAI,QAAQ,UAAU,UACpB,QAAQ,OAAO,oBAAoB,SAAS,QAAQ;GAExD,CAAC;EACH;EACA,OAAO,KAAK,gBAAsB,SAAS,UAAU,IAAI;CAC3D;CAEA,MAAM,GAAG,MAA0D;EACjE,OAAO,KAAK,WACV,MAAM,GAAG,IAAI,CAAC,CAAC,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO,GAAG,CAAE,CACnE;CACF;AACF"}
|
package/dist/utils/gateway.cjs
CHANGED
|
@@ -2,7 +2,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
|
|
3
3
|
const require_utils_env = require("./env.cjs");
|
|
4
4
|
//#region src/utils/gateway.ts
|
|
5
|
-
var gateway_exports = /* @__PURE__ */ require_runtime.__exportAll({
|
|
5
|
+
var gateway_exports = /* @__PURE__ */ require_runtime.__exportAll({
|
|
6
|
+
DEFAULT_LANGSMITH_GATEWAY: () => DEFAULT_LANGSMITH_GATEWAY,
|
|
7
|
+
resolveLangSmithGatewayConfig: () => resolveLangSmithGatewayConfig
|
|
8
|
+
});
|
|
6
9
|
const LANGSMITH_GATEWAY = "LANGSMITH_GATEWAY";
|
|
7
10
|
const LANGSMITH_GATEWAY_API_KEY = "LANGSMITH_GATEWAY_API_KEY";
|
|
8
11
|
const LANGSMITH_API_KEY = "LANGSMITH_API_KEY";
|
|
@@ -32,6 +35,7 @@ function resolveLangSmithGatewayConfig({ baseURL, providerPath }) {
|
|
|
32
35
|
};
|
|
33
36
|
}
|
|
34
37
|
//#endregion
|
|
38
|
+
exports.DEFAULT_LANGSMITH_GATEWAY = DEFAULT_LANGSMITH_GATEWAY;
|
|
35
39
|
Object.defineProperty(exports, "gateway_exports", {
|
|
36
40
|
enumerable: true,
|
|
37
41
|
get: function() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gateway.cjs","names":["getEnvironmentVariable"],"sources":["../../src/utils/gateway.ts"],"sourcesContent":["import { getEnvironmentVariable } from \"./env.js\";\n\nconst LANGSMITH_GATEWAY = \"LANGSMITH_GATEWAY\";\nconst LANGSMITH_GATEWAY_API_KEY = \"LANGSMITH_GATEWAY_API_KEY\";\nconst LANGSMITH_API_KEY = \"LANGSMITH_API_KEY\";\
|
|
1
|
+
{"version":3,"file":"gateway.cjs","names":["getEnvironmentVariable"],"sources":["../../src/utils/gateway.ts"],"sourcesContent":["import { getEnvironmentVariable } from \"./env.js\";\n\nconst LANGSMITH_GATEWAY = \"LANGSMITH_GATEWAY\";\nconst LANGSMITH_GATEWAY_API_KEY = \"LANGSMITH_GATEWAY_API_KEY\";\nconst LANGSMITH_API_KEY = \"LANGSMITH_API_KEY\";\nexport const DEFAULT_LANGSMITH_GATEWAY = \"https://gateway.smith.langchain.com\";\nconst TRUE_VALUES = [\"true\", \"1\", \"yes\"];\nconst FALSE_VALUES = [\"false\", \"0\", \"no\"];\n\nexport interface LangSmithGatewayConfigOptions {\n baseURL?: string;\n providerPath: string;\n}\n\nexport interface LangSmithGatewayConfig {\n baseURL?: string;\n apiKey?: string;\n}\n\nfunction resolveLangSmithGatewayBaseURL(\n providerPath: string\n): string | undefined {\n const value = getEnvironmentVariable(LANGSMITH_GATEWAY);\n if (!value || FALSE_VALUES.includes(value.toLowerCase())) {\n return undefined;\n }\n const baseURL = TRUE_VALUES.includes(value.toLowerCase())\n ? DEFAULT_LANGSMITH_GATEWAY\n : value.replace(/\\/+$/, \"\");\n return `${baseURL}/${providerPath}`;\n}\n\nexport function resolveLangSmithGatewayConfig({\n baseURL,\n providerPath,\n}: LangSmithGatewayConfigOptions): LangSmithGatewayConfig {\n if (baseURL !== undefined) {\n return { baseURL };\n }\n\n const gatewayBaseURL = resolveLangSmithGatewayBaseURL(providerPath);\n if (gatewayBaseURL === undefined) {\n return {};\n }\n\n return {\n baseURL: gatewayBaseURL,\n apiKey:\n getEnvironmentVariable(LANGSMITH_GATEWAY_API_KEY) ||\n getEnvironmentVariable(LANGSMITH_API_KEY),\n };\n}\n"],"mappings":";;;;;;;;AAEA,MAAM,oBAAoB;AAC1B,MAAM,4BAA4B;AAClC,MAAM,oBAAoB;AAC1B,MAAa,4BAA4B;AACzC,MAAM,cAAc;CAAC;CAAQ;CAAK;AAAK;AACvC,MAAM,eAAe;CAAC;CAAS;CAAK;AAAI;AAYxC,SAAS,+BACP,cACoB;CACpB,MAAM,QAAQA,kBAAAA,uBAAuB,iBAAiB;CACtD,IAAI,CAAC,SAAS,aAAa,SAAS,MAAM,YAAY,CAAC,GACrD;CAKF,OAAO,GAHS,YAAY,SAAS,MAAM,YAAY,CAAC,IACpD,4BACA,MAAM,QAAQ,QAAQ,EAAE,EACV,GAAG;AACvB;AAEA,SAAgB,8BAA8B,EAC5C,SACA,gBACwD;CACxD,IAAI,YAAY,KAAA,GACd,OAAO,EAAE,QAAQ;CAGnB,MAAM,iBAAiB,+BAA+B,YAAY;CAClE,IAAI,mBAAmB,KAAA,GACrB,OAAO,CAAC;CAGV,OAAO;EACL,SAAS;EACT,QACEA,kBAAAA,uBAAuB,yBAAyB,KAChDA,kBAAAA,uBAAuB,iBAAiB;CAC5C;AACF"}
|
package/dist/utils/gateway.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//#region src/utils/gateway.d.ts
|
|
2
|
+
declare const DEFAULT_LANGSMITH_GATEWAY = "https://gateway.smith.langchain.com";
|
|
2
3
|
interface LangSmithGatewayConfigOptions {
|
|
3
4
|
baseURL?: string;
|
|
4
5
|
providerPath: string;
|
|
@@ -9,5 +10,5 @@ interface LangSmithGatewayConfig {
|
|
|
9
10
|
}
|
|
10
11
|
declare function resolveLangSmithGatewayConfig({ baseURL, providerPath }: LangSmithGatewayConfigOptions): LangSmithGatewayConfig;
|
|
11
12
|
//#endregion
|
|
12
|
-
export { LangSmithGatewayConfig, LangSmithGatewayConfigOptions, resolveLangSmithGatewayConfig };
|
|
13
|
+
export { DEFAULT_LANGSMITH_GATEWAY, LangSmithGatewayConfig, LangSmithGatewayConfigOptions, resolveLangSmithGatewayConfig };
|
|
13
14
|
//# sourceMappingURL=gateway.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gateway.d.cts","names":[],"sources":["../../src/utils/gateway.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"gateway.d.cts","names":[],"sources":["../../src/utils/gateway.ts"],"mappings":";cAKa;UAII;EACf;EACA;;UAGe;EACf;EACA;;iBAgBc,gCACd,SACA,gBACC,gCAAgC"}
|