UNPKG

@langchain/langgraph

Version:

LangGraph

1 lines 7.82 kB
{"version":3,"file":"retry.cjs","names":["patchConfigurable","e: unknown","isParentCommand","ns: string","Command","getParentCheckpointNamespace","isGraphBubbleUp","CONFIG_KEY_RESUMING"],"sources":["../../src/pregel/retry.ts"],"sourcesContent":["import { Command, CONFIG_KEY_RESUMING } from \"../constants.js\";\nimport { isGraphBubbleUp, isParentCommand } from \"../errors.js\";\nimport { PregelExecutableTask } from \"./types.js\";\nimport { getParentCheckpointNamespace } from \"./utils/config.js\";\nimport { patchConfigurable, type RetryPolicy } from \"./utils/index.js\";\n\nexport const DEFAULT_INITIAL_INTERVAL = 500;\nexport const DEFAULT_BACKOFF_FACTOR = 2;\nexport const DEFAULT_MAX_INTERVAL = 128000;\nexport const DEFAULT_MAX_RETRIES = 3;\n\nconst DEFAULT_STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment Required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 409, // Conflict\n];\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst DEFAULT_RETRY_ON_HANDLER = (error: any) => {\n if (\n error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"AbortError\") ||\n error.name === \"AbortError\"\n ) {\n return false;\n }\n\n // Thrown when interrupt is called without a checkpointer\n if (error.name === \"GraphValueError\") {\n return false;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n if ((error as any)?.code === \"ECONNABORTED\") {\n return false;\n }\n\n const status =\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (error as any)?.response?.status ?? (error as any)?.status;\n if (status && DEFAULT_STATUS_NO_RETRY.includes(+status)) {\n return false;\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n if ((error as any)?.error?.code === \"insufficient_quota\") {\n return false;\n }\n return true;\n};\n\nexport type SettledPregelTask = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n task: PregelExecutableTask<any, any>;\n error: Error;\n signalAborted?: boolean;\n};\n\nexport async function _runWithRetry<\n N extends PropertyKey,\n C extends PropertyKey\n>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n pregelTask: PregelExecutableTask<N, C>,\n retryPolicy?: RetryPolicy,\n configurable?: Record<string, unknown>,\n signal?: AbortSignal\n): Promise<{\n task: PregelExecutableTask<N, C>;\n result: unknown;\n error: Error | undefined;\n signalAborted?: boolean;\n}> {\n const resolvedRetryPolicy = pregelTask.retry_policy ?? retryPolicy;\n let interval =\n resolvedRetryPolicy !== undefined\n ? resolvedRetryPolicy.initialInterval ?? DEFAULT_INITIAL_INTERVAL\n : 0;\n let attempts = 0;\n let error;\n let result;\n\n let { config } = pregelTask;\n if (configurable) config = patchConfigurable(config, configurable);\n config = { ...config, signal };\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n if (signal?.aborted) {\n // no need to throw here - we'll throw from the runner, instead.\n // there's just no point in retrying if the user has requested an abort.\n break;\n }\n // Clear any writes from previous attempts\n pregelTask.writes.splice(0, pregelTask.writes.length);\n error = undefined;\n try {\n result = await pregelTask.proc.invoke(pregelTask.input, config);\n break;\n } catch (e: unknown) {\n error = e;\n (error as { pregelTaskId: string }).pregelTaskId = pregelTask.id;\n if (isParentCommand(error)) {\n const ns: string = config?.configurable?.checkpoint_ns;\n const cmd = error.command;\n if (cmd.graph === ns) {\n // this command is for the current graph, handle it\n for (const writer of pregelTask.writers) {\n await writer.invoke(cmd, config);\n }\n error = undefined;\n break;\n } else if (cmd.graph === Command.PARENT) {\n // this command is for the parent graph, assign it to the parent\n const parentNs = getParentCheckpointNamespace(ns);\n error.command = new Command({\n ...error.command,\n graph: parentNs,\n });\n }\n }\n if (isGraphBubbleUp(error)) {\n break;\n }\n if (resolvedRetryPolicy === undefined) {\n break;\n }\n attempts += 1;\n // check if we should give up\n if (\n attempts >= (resolvedRetryPolicy.maxAttempts ?? DEFAULT_MAX_RETRIES)\n ) {\n break;\n }\n const retryOn = resolvedRetryPolicy.retryOn ?? DEFAULT_RETRY_ON_HANDLER;\n if (!retryOn(error)) {\n break;\n }\n interval = Math.min(\n resolvedRetryPolicy.maxInterval ?? DEFAULT_MAX_INTERVAL,\n interval * (resolvedRetryPolicy.backoffFactor ?? DEFAULT_BACKOFF_FACTOR)\n );\n const intervalWithJitter = resolvedRetryPolicy.jitter\n ? Math.floor(interval + Math.random() * 1000)\n : interval;\n // sleep before retrying\n // eslint-disable-next-line no-promise-executor-return\n await new Promise((resolve) => setTimeout(resolve, intervalWithJitter));\n // log the retry\n const errorName =\n (error as Error).name ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ((error as Error).constructor as any).unminifiable_name ??\n (error as Error).constructor.name;\n if (resolvedRetryPolicy?.logWarning ?? true) {\n console.log(\n `Retrying task \"${String(pregelTask.name)}\" after ${interval.toFixed(\n 2\n )}ms (attempt ${attempts}) after ${errorName}: ${error}`\n );\n }\n\n // signal subgraphs to resume (if available)\n config = patchConfigurable(config, { [CONFIG_KEY_RESUMING]: true });\n }\n }\n return {\n task: pregelTask,\n result,\n error: error as Error | undefined,\n signalAborted: signal?.aborted,\n };\n}\n"],"mappings":";;;;;;AAMA,MAAa,2BAA2B;AACxC,MAAa,yBAAyB;AACtC,MAAa,uBAAuB;AACpC,MAAa,sBAAsB;AAEnC,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAIF,MAAM,4BAA4B,UAAe;AAC/C,KACE,MAAM,QAAQ,WAAW,aACzB,MAAM,QAAQ,WAAW,iBACzB,MAAM,SAAS,aAEf,QAAO;AAIT,KAAI,MAAM,SAAS,kBACjB,QAAO;AAIT,KAAK,OAAe,SAAS,eAC3B,QAAO;CAGT,MAAM,SAEH,OAAe,UAAU,UAAW,OAAe;AACtD,KAAI,UAAU,wBAAwB,SAAS,CAAC,QAC9C,QAAO;AAGT,KAAK,OAAe,OAAO,SAAS,qBAClC,QAAO;AA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