config.cjs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pickRunnableConfigKeys = exports.patchConfig = exports.ensureConfig = exports.mergeConfigs = exports.getCallbackManagerForConfig = exports.DEFAULT_RECURSION_LIMIT = void 0;
  4. const manager_js_1 = require("../callbacks/manager.cjs");
  5. const index_js_1 = require("../singletons/index.cjs");
  6. exports.DEFAULT_RECURSION_LIMIT = 25;
  7. async function getCallbackManagerForConfig(config) {
  8. return manager_js_1.CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
  9. }
  10. exports.getCallbackManagerForConfig = getCallbackManagerForConfig;
  11. function mergeConfigs(...configs) {
  12. // We do not want to call ensureConfig on the empty state here as this may cause
  13. // double loading of callbacks if async local storage is being used.
  14. const copy = {};
  15. for (const options of configs.filter((c) => !!c)) {
  16. for (const key of Object.keys(options)) {
  17. if (key === "metadata") {
  18. copy[key] = { ...copy[key], ...options[key] };
  19. }
  20. else if (key === "tags") {
  21. const baseKeys = copy[key] ?? [];
  22. copy[key] = [...new Set(baseKeys.concat(options[key] ?? []))];
  23. }
  24. else if (key === "configurable") {
  25. copy[key] = { ...copy[key], ...options[key] };
  26. }
  27. else if (key === "timeout") {
  28. if (copy.timeout === undefined) {
  29. copy.timeout = options.timeout;
  30. }
  31. else if (options.timeout !== undefined) {
  32. copy.timeout = Math.min(copy.timeout, options.timeout);
  33. }
  34. }
  35. else if (key === "signal") {
  36. if (copy.signal === undefined) {
  37. copy.signal = options.signal;
  38. }
  39. else if (options.signal !== undefined) {
  40. if ("any" in AbortSignal) {
  41. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  42. copy.signal = AbortSignal.any([
  43. copy.signal,
  44. options.signal,
  45. ]);
  46. }
  47. else {
  48. copy.signal = options.signal;
  49. }
  50. }
  51. }
  52. else if (key === "callbacks") {
  53. const baseCallbacks = copy.callbacks;
  54. const providedCallbacks = options.callbacks;
  55. // callbacks can be either undefined, Array<handler> or manager
  56. // so merging two callbacks values has 6 cases
  57. if (Array.isArray(providedCallbacks)) {
  58. if (!baseCallbacks) {
  59. copy.callbacks = providedCallbacks;
  60. }
  61. else if (Array.isArray(baseCallbacks)) {
  62. copy.callbacks = baseCallbacks.concat(providedCallbacks);
  63. }
  64. else {
  65. // baseCallbacks is a manager
  66. const manager = baseCallbacks.copy();
  67. for (const callback of providedCallbacks) {
  68. manager.addHandler((0, manager_js_1.ensureHandler)(callback), true);
  69. }
  70. copy.callbacks = manager;
  71. }
  72. }
  73. else if (providedCallbacks) {
  74. // providedCallbacks is a manager
  75. if (!baseCallbacks) {
  76. copy.callbacks = providedCallbacks;
  77. }
  78. else if (Array.isArray(baseCallbacks)) {
  79. const manager = providedCallbacks.copy();
  80. for (const callback of baseCallbacks) {
  81. manager.addHandler((0, manager_js_1.ensureHandler)(callback), true);
  82. }
  83. copy.callbacks = manager;
  84. }
  85. else {
  86. // baseCallbacks is also a manager
  87. copy.callbacks = new manager_js_1.CallbackManager(providedCallbacks._parentRunId, {
  88. handlers: baseCallbacks.handlers.concat(providedCallbacks.handlers),
  89. inheritableHandlers: baseCallbacks.inheritableHandlers.concat(providedCallbacks.inheritableHandlers),
  90. tags: Array.from(new Set(baseCallbacks.tags.concat(providedCallbacks.tags))),
  91. inheritableTags: Array.from(new Set(baseCallbacks.inheritableTags.concat(providedCallbacks.inheritableTags))),
  92. metadata: {
  93. ...baseCallbacks.metadata,
  94. ...providedCallbacks.metadata,
  95. },
  96. });
  97. }
  98. }
  99. }
  100. else {
  101. const typedKey = key;
  102. copy[typedKey] = options[typedKey] ?? copy[typedKey];
  103. }
  104. }
  105. }
  106. return copy;
  107. }
  108. exports.mergeConfigs = mergeConfigs;
  109. const PRIMITIVES = new Set(["string", "number", "boolean"]);
  110. /**
  111. * Ensure that a passed config is an object with all required keys present.
  112. */
  113. function ensureConfig(config) {
  114. const implicitConfig = index_js_1.AsyncLocalStorageProviderSingleton.getRunnableConfig();
  115. let empty = {
  116. tags: [],
  117. metadata: {},
  118. recursionLimit: 25,
  119. runId: undefined,
  120. };
  121. if (implicitConfig) {
  122. // Don't allow runId and runName to be loaded implicitly, as this can cause
  123. // child runs to improperly inherit their parents' run ids.
  124. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  125. const { runId, runName, ...rest } = implicitConfig;
  126. empty = Object.entries(rest).reduce(
  127. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  128. (currentConfig, [key, value]) => {
  129. if (value !== undefined) {
  130. // eslint-disable-next-line no-param-reassign
  131. currentConfig[key] = value;
  132. }
  133. return currentConfig;
  134. }, empty);
  135. }
  136. if (config) {
  137. empty = Object.entries(config).reduce(
  138. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  139. (currentConfig, [key, value]) => {
  140. if (value !== undefined) {
  141. // eslint-disable-next-line no-param-reassign
  142. currentConfig[key] = value;
  143. }
  144. return currentConfig;
  145. }, empty);
  146. }
  147. if (empty?.configurable) {
  148. for (const key of Object.keys(empty.configurable)) {
  149. if (PRIMITIVES.has(typeof empty.configurable[key]) &&
  150. !empty.metadata?.[key]) {
  151. if (!empty.metadata) {
  152. empty.metadata = {};
  153. }
  154. empty.metadata[key] = empty.configurable[key];
  155. }
  156. }
  157. }
  158. if (empty.timeout !== undefined) {
  159. if (empty.timeout <= 0) {
  160. throw new Error("Timeout must be a positive number");
  161. }
  162. const timeoutSignal = AbortSignal.timeout(empty.timeout);
  163. if (empty.signal !== undefined) {
  164. if ("any" in AbortSignal) {
  165. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  166. empty.signal = AbortSignal.any([empty.signal, timeoutSignal]);
  167. }
  168. }
  169. else {
  170. empty.signal = timeoutSignal;
  171. }
  172. delete empty.timeout;
  173. }
  174. return empty;
  175. }
  176. exports.ensureConfig = ensureConfig;
  177. /**
  178. * Helper function that patches runnable configs with updated properties.
  179. */
  180. function patchConfig(config = {}, { callbacks, maxConcurrency, recursionLimit, runName, configurable, runId, } = {}) {
  181. const newConfig = ensureConfig(config);
  182. if (callbacks !== undefined) {
  183. /**
  184. * If we're replacing callbacks we need to unset runName
  185. * since that should apply only to the same run as the original callbacks
  186. */
  187. delete newConfig.runName;
  188. newConfig.callbacks = callbacks;
  189. }
  190. if (recursionLimit !== undefined) {
  191. newConfig.recursionLimit = recursionLimit;
  192. }
  193. if (maxConcurrency !== undefined) {
  194. newConfig.maxConcurrency = maxConcurrency;
  195. }
  196. if (runName !== undefined) {
  197. newConfig.runName = runName;
  198. }
  199. if (configurable !== undefined) {
  200. newConfig.configurable = { ...newConfig.configurable, ...configurable };
  201. }
  202. if (runId !== undefined) {
  203. delete newConfig.runId;
  204. }
  205. return newConfig;
  206. }
  207. exports.patchConfig = patchConfig;
  208. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  209. function pickRunnableConfigKeys(config) {
  210. return config
  211. ? {
  212. configurable: config.configurable,
  213. recursionLimit: config.recursionLimit,
  214. callbacks: config.callbacks,
  215. tags: config.tags,
  216. metadata: config.metadata,
  217. maxConcurrency: config.maxConcurrency,
  218. timeout: config.timeout,
  219. signal: config.signal,
  220. }
  221. : undefined;
  222. }
  223. exports.pickRunnableConfigKeys = pickRunnableConfigKeys;