{"version":3,"file":"context.cjs","names":["getGlobalAsyncLocalStorageInstance","_CONTEXT_VARIABLES_KEY","RunTree"],"sources":["../../../src/singletons/async_local_storage/context.ts"],"sourcesContent":["import { isRunTree, RunTree } from \"langsmith/run_trees\";\nimport { BaseCallbackHandler } from \"../../callbacks/base.js\";\nimport {\n  _CONTEXT_VARIABLES_KEY,\n  getGlobalAsyncLocalStorageInstance,\n} from \"./globals.js\";\n\n/**\n * Set a context variable. Context variables are scoped to any\n * child runnables called by the current runnable, or globally if set outside\n * of any runnable.\n *\n * @remarks\n * This function is only supported in environments that support AsyncLocalStorage,\n * including Node.js, Deno, and Cloudflare Workers.\n *\n * @example\n * ```ts\n * import { RunnableLambda } from \"@langchain/core/runnables\";\n * import {\n *   getContextVariable,\n *   setContextVariable\n * } from \"@langchain/core/context\";\n *\n * const nested = RunnableLambda.from(() => {\n *   // \"bar\" because it was set by a parent\n *   console.log(getContextVariable(\"foo\"));\n *\n *   // Override to \"baz\", but only for child runnables\n *   setContextVariable(\"foo\", \"baz\");\n *\n *   // Now \"baz\", but only for child runnables\n *   return getContextVariable(\"foo\");\n * });\n *\n * const runnable = RunnableLambda.from(async () => {\n *   // Set a context variable named \"foo\"\n *   setContextVariable(\"foo\", \"bar\");\n *\n *   const res = await nested.invoke({});\n *\n *   // Still \"bar\" since child changes do not affect parents\n *   console.log(getContextVariable(\"foo\"));\n *\n *   return res;\n * });\n *\n * // undefined, because context variable has not been set yet\n * console.log(getContextVariable(\"foo\"));\n *\n * // Final return value is \"baz\"\n * const result = await runnable.invoke({});\n * ```\n *\n * @param name The name of the context variable.\n * @param value The value to set.\n */\nexport function setContextVariable<T>(name: PropertyKey, value: T): void {\n  // Avoid using global singleton due to circuluar dependency issues\n  const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();\n  if (asyncLocalStorageInstance === undefined) {\n    throw new Error(\n      `Internal error: Global shared async local storage instance has not been initialized.`\n    );\n  }\n  const runTree = asyncLocalStorageInstance.getStore();\n  const contextVars = { ...runTree?.[_CONTEXT_VARIABLES_KEY] };\n  contextVars[name] = value;\n  let newValue = {};\n  if (isRunTree(runTree)) {\n    newValue = new RunTree(runTree);\n  }\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  (newValue as any)[_CONTEXT_VARIABLES_KEY] = contextVars;\n  asyncLocalStorageInstance.enterWith(newValue);\n}\n\n/**\n * Get the value of a previously set context variable. Context variables\n * are scoped to any child runnables called by the current runnable,\n * or globally if set outside of any runnable.\n *\n * @remarks\n * This function is only supported in environments that support AsyncLocalStorage,\n * including Node.js, Deno, and Cloudflare Workers.\n *\n * @example\n * ```ts\n * import { RunnableLambda } from \"@langchain/core/runnables\";\n * import {\n *   getContextVariable,\n *   setContextVariable\n * } from \"@langchain/core/context\";\n *\n * const nested = RunnableLambda.from(() => {\n *   // \"bar\" because it was set by a parent\n *   console.log(getContextVariable(\"foo\"));\n *\n *   // Override to \"baz\", but only for child runnables\n *   setContextVariable(\"foo\", \"baz\");\n *\n *   // Now \"baz\", but only for child runnables\n *   return getContextVariable(\"foo\");\n * });\n *\n * const runnable = RunnableLambda.from(async () => {\n *   // Set a context variable named \"foo\"\n *   setContextVariable(\"foo\", \"bar\");\n *\n *   const res = await nested.invoke({});\n *\n *   // Still \"bar\" since child changes do not affect parents\n *   console.log(getContextVariable(\"foo\"));\n *\n *   return res;\n * });\n *\n * // undefined, because context variable has not been set yet\n * console.log(getContextVariable(\"foo\"));\n *\n * // Final return value is \"baz\"\n * const result = await runnable.invoke({});\n * ```\n *\n * @param name The name of the context variable.\n */\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport function getContextVariable<T = any>(name: PropertyKey): T | undefined {\n  // Avoid using global singleton due to circuluar dependency issues\n  const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();\n  if (asyncLocalStorageInstance === undefined) {\n    return undefined;\n  }\n  const runTree = asyncLocalStorageInstance.getStore();\n  return runTree?.[_CONTEXT_VARIABLES_KEY]?.[name];\n}\n\nconst LC_CONFIGURE_HOOKS_KEY = Symbol(\"lc:configure_hooks\");\n\nexport const _getConfigureHooks = () =>\n  getContextVariable<ConfigureHook[]>(LC_CONFIGURE_HOOKS_KEY) || [];\n\n/**\n * Register a callback configure hook to automatically add callback handlers to all runs.\n *\n * There are two ways to use this:\n *\n * 1. Using a context variable:\n *    - Set `contextVar` to specify the variable name\n *    - Use `setContextVariable()` to store your handler instance\n *\n * 2. Using an environment variable:\n *    - Set both `envVar` and `handlerClass`\n *    - The handler will be instantiated when the env var is set to \"true\".\n *\n * @example\n * ```typescript\n * // Method 1: Using context variable\n * import {\n *   registerConfigureHook,\n *   setContextVariable\n * } from \"@langchain/core/context\";\n *\n * const tracer = new MyCallbackHandler();\n * registerConfigureHook({\n *   contextVar: \"my_tracer\",\n * });\n * setContextVariable(\"my_tracer\", tracer);\n *\n * // ...run code here\n *\n * // Method 2: Using environment variable\n * registerConfigureHook({\n *   handlerClass: MyCallbackHandler,\n *   envVar: \"MY_TRACER_ENABLED\",\n * });\n * process.env.MY_TRACER_ENABLED = \"true\";\n *\n * // ...run code here\n * ```\n *\n * @param config Configuration object for the hook\n * @param config.contextVar Name of the context variable containing the handler instance\n * @param config.inheritable Whether child runs should inherit this handler\n * @param config.handlerClass Optional callback handler class (required if using envVar)\n * @param config.envVar Optional environment variable name to control handler activation\n */\nexport const registerConfigureHook = (config: ConfigureHook) => {\n  if (config.envVar && !config.handlerClass) {\n    throw new Error(\n      \"If envVar is set, handlerClass must also be set to a non-None value.\"\n    );\n  }\n  setContextVariable(LC_CONFIGURE_HOOKS_KEY, [..._getConfigureHooks(), config]);\n};\n\nexport type ConfigureHook = {\n  contextVar?: string;\n  inheritable?: boolean;\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  handlerClass?: new (...args: any[]) => BaseCallbackHandler;\n  envVar?: string;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAgB,mBAAsB,MAAmB,OAAgB;CAEvE,MAAM,4BAA4BA,gBAAAA,oCAAoC;AACtE,KAAI,8BAA8B,KAAA,EAChC,OAAM,IAAI,MACR,uFACD;CAEH,MAAM,UAAU,0BAA0B,UAAU;CACpD,MAAM,cAAc,EAAE,GAAG,UAAUC,gBAAAA,yBAAyB;AAC5D,aAAY,QAAQ;CACpB,IAAI,WAAW,EAAE;AACjB,MAAA,GAAA,oBAAA,WAAc,QAAQ,CACpB,YAAW,IAAIC,oBAAAA,QAAQ,QAAQ;AAGhC,UAAiBD,gBAAAA,0BAA0B;AAC5C,2BAA0B,UAAU,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqD/C,SAAgB,mBAA4B,MAAkC;CAE5E,MAAM,4BAA4BD,gBAAAA,oCAAoC;AACtE,KAAI,8BAA8B,KAAA,EAChC;AAGF,QADgB,0BAA0B,UAAU,GACnCC,gBAAAA,0BAA0B;;AAG7C,MAAM,yBAAyB,OAAO,qBAAqB;AAE3D,MAAa,2BACX,mBAAoC,uBAAuB,IAAI,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CnE,MAAa,yBAAyB,WAA0B;AAC9D,KAAI,OAAO,UAAU,CAAC,OAAO,aAC3B,OAAM,IAAI,MACR,uEACD;AAEH,oBAAmB,wBAAwB,CAAC,GAAG,oBAAoB,EAAE,OAAO,CAAC"}