UNPKG

@langchain/core

Version:
1 lines 7.89 kB
{"version":3,"file":"context.cjs","names":["name: PropertyKey","value: T","getGlobalAsyncLocalStorageInstance","_CONTEXT_VARIABLES_KEY","RunTree","config: ConfigureHook"],"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 // eslint-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// eslint-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 // eslint-disable-next-line @typescript-eslint/no-explicit-any\n handlerClass?: new (...args: any[]) => BaseCallbackHandler;\n envVar?: string;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAgB,mBAAsBA,MAAmBC,OAAgB;CAEvE,MAAM,4BAA4BC,oDAAoC;AACtE,KAAI,8BAA8B,OAChC,OAAM,IAAI,MACR,CAAC,oFAAoF,CAAC;CAG1F,MAAM,UAAU,0BAA0B,UAAU;CACpD,MAAM,cAAc,EAAE,GAAG,UAAUC,wCAAyB;CAC5D,YAAY,QAAQ;CACpB,IAAI,WAAW,CAAE;AACjB,wCAAc,QAAQ,EACpB,WAAW,IAAIC,4BAAQ;CAGxB,SAAiBD,0CAA0B;CAC5C,0BAA0B,UAAU,SAAS;AAC9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDD,SAAgB,mBAA4BH,MAAkC;CAE5E,MAAM,4BAA4BE,oDAAoC;AACtE,KAAI,8BAA8B,OAChC,QAAO;CAET,MAAM,UAAU,0BAA0B,UAAU;AACpD,QAAO,UAAUC,0CAA0B;AAC5C;AAED,MAAM,yBAAyB,OAAO,qBAAqB;AAE3D,MAAa,qBAAqB,MAChC,mBAAoC,uBAAuB,IAAI,CAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CnE,MAAa,wBAAwB,CAACE,WAA0B;AAC9D,KAAI,OAAO,UAAU,CAAC,OAAO,aAC3B,OAAM,IAAI,MACR;CAGJ,mBAAmB,wBAAwB,CAAC,GAAG,oBAAoB,EAAE,MAAO,EAAC;AAC9E"}