config.js 8.7 KB

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