env.cjs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getEnv = exports.isNode = exports.isDeno = exports.isJsDom = exports.isWebWorker = exports.isBrowser = void 0;
  4. exports.getRuntimeEnvironment = getRuntimeEnvironment;
  5. exports.getLangChainEnvVars = getLangChainEnvVars;
  6. exports.getLangChainEnvVarsMetadata = getLangChainEnvVarsMetadata;
  7. exports.getEnvironmentVariables = getEnvironmentVariables;
  8. exports.getEnvironmentVariable = getEnvironmentVariable;
  9. exports.getLangSmithEnvironmentVariable = getLangSmithEnvironmentVariable;
  10. exports.setEnvironmentVariable = setEnvironmentVariable;
  11. exports.getShas = getShas;
  12. // Inlined from https://github.com/flexdinesh/browser-or-node
  13. const index_js_1 = require("../index.cjs");
  14. let globalEnv;
  15. const isBrowser = () => typeof window !== "undefined" && typeof window.document !== "undefined";
  16. exports.isBrowser = isBrowser;
  17. const isWebWorker = () => typeof globalThis === "object" &&
  18. globalThis.constructor &&
  19. globalThis.constructor.name === "DedicatedWorkerGlobalScope";
  20. exports.isWebWorker = isWebWorker;
  21. const isJsDom = () => (typeof window !== "undefined" && window.name === "nodejs") ||
  22. (typeof navigator !== "undefined" &&
  23. (navigator.userAgent.includes("Node.js") ||
  24. navigator.userAgent.includes("jsdom")));
  25. exports.isJsDom = isJsDom;
  26. // Supabase Edge Function provides a `Deno` global object
  27. // without `version` property
  28. const isDeno = () => typeof Deno !== "undefined";
  29. exports.isDeno = isDeno;
  30. // Mark not-as-node if in Supabase Edge Function
  31. const isNode = () => typeof process !== "undefined" &&
  32. typeof process.versions !== "undefined" &&
  33. typeof process.versions.node !== "undefined" &&
  34. !(0, exports.isDeno)();
  35. exports.isNode = isNode;
  36. const getEnv = () => {
  37. if (globalEnv) {
  38. return globalEnv;
  39. }
  40. if ((0, exports.isBrowser)()) {
  41. globalEnv = "browser";
  42. }
  43. else if ((0, exports.isNode)()) {
  44. globalEnv = "node";
  45. }
  46. else if ((0, exports.isWebWorker)()) {
  47. globalEnv = "webworker";
  48. }
  49. else if ((0, exports.isJsDom)()) {
  50. globalEnv = "jsdom";
  51. }
  52. else if ((0, exports.isDeno)()) {
  53. globalEnv = "deno";
  54. }
  55. else {
  56. globalEnv = "other";
  57. }
  58. return globalEnv;
  59. };
  60. exports.getEnv = getEnv;
  61. let runtimeEnvironment;
  62. function getRuntimeEnvironment() {
  63. if (runtimeEnvironment === undefined) {
  64. const env = (0, exports.getEnv)();
  65. const releaseEnv = getShas();
  66. runtimeEnvironment = {
  67. library: "langsmith",
  68. runtime: env,
  69. sdk: "langsmith-js",
  70. sdk_version: index_js_1.__version__,
  71. ...releaseEnv,
  72. };
  73. }
  74. return runtimeEnvironment;
  75. }
  76. /**
  77. * Retrieves the LangChain-specific environment variables from the current runtime environment.
  78. * Sensitive keys (containing the word "key", "token", or "secret") have their values redacted for security.
  79. *
  80. * @returns {Record<string, string>}
  81. * - A record of LangChain-specific environment variables.
  82. */
  83. function getLangChainEnvVars() {
  84. const allEnvVars = getEnvironmentVariables() || {};
  85. const envVars = {};
  86. for (const [key, value] of Object.entries(allEnvVars)) {
  87. if (key.startsWith("LANGCHAIN_") && typeof value === "string") {
  88. envVars[key] = value;
  89. }
  90. }
  91. for (const key in envVars) {
  92. if ((key.toLowerCase().includes("key") ||
  93. key.toLowerCase().includes("secret") ||
  94. key.toLowerCase().includes("token")) &&
  95. typeof envVars[key] === "string") {
  96. const value = envVars[key];
  97. envVars[key] =
  98. value.slice(0, 2) + "*".repeat(value.length - 4) + value.slice(-2);
  99. }
  100. }
  101. return envVars;
  102. }
  103. /**
  104. * Retrieves the LangChain-specific metadata from the current runtime environment.
  105. *
  106. * @returns {Record<string, string>}
  107. * - A record of LangChain-specific metadata environment variables.
  108. */
  109. function getLangChainEnvVarsMetadata() {
  110. const allEnvVars = getEnvironmentVariables() || {};
  111. const envVars = {};
  112. const excluded = [
  113. "LANGCHAIN_API_KEY",
  114. "LANGCHAIN_ENDPOINT",
  115. "LANGCHAIN_TRACING_V2",
  116. "LANGCHAIN_PROJECT",
  117. "LANGCHAIN_SESSION",
  118. "LANGSMITH_API_KEY",
  119. "LANGSMITH_ENDPOINT",
  120. "LANGSMITH_TRACING_V2",
  121. "LANGSMITH_PROJECT",
  122. "LANGSMITH_SESSION",
  123. ];
  124. for (const [key, value] of Object.entries(allEnvVars)) {
  125. if ((key.startsWith("LANGCHAIN_") || key.startsWith("LANGSMITH_")) &&
  126. typeof value === "string" &&
  127. !excluded.includes(key) &&
  128. !key.toLowerCase().includes("key") &&
  129. !key.toLowerCase().includes("secret") &&
  130. !key.toLowerCase().includes("token")) {
  131. if (key === "LANGCHAIN_REVISION_ID") {
  132. envVars["revision_id"] = value;
  133. }
  134. else {
  135. envVars[key] = value;
  136. }
  137. }
  138. }
  139. return envVars;
  140. }
  141. /**
  142. * Retrieves the environment variables from the current runtime environment.
  143. *
  144. * This function is designed to operate in a variety of JS environments,
  145. * including Node.js, Deno, browsers, etc.
  146. *
  147. * @returns {Record<string, string> | undefined}
  148. * - A record of environment variables if available.
  149. * - `undefined` if the environment does not support or allows access to environment variables.
  150. */
  151. function getEnvironmentVariables() {
  152. try {
  153. // Check for Node.js environment
  154. // eslint-disable-next-line no-process-env
  155. if (typeof process !== "undefined" && process.env) {
  156. // eslint-disable-next-line no-process-env
  157. return Object.entries(process.env).reduce((acc, [key, value]) => {
  158. acc[key] = String(value);
  159. return acc;
  160. }, {});
  161. }
  162. // For browsers and other environments, we may not have direct access to env variables
  163. // Return undefined or any other fallback as required.
  164. return undefined;
  165. }
  166. catch (e) {
  167. // Catch any errors that might occur while trying to access environment variables
  168. return undefined;
  169. }
  170. }
  171. function getEnvironmentVariable(name) {
  172. // Certain Deno setups will throw an error if you try to access environment variables
  173. // https://github.com/hwchase17/langchainjs/issues/1412
  174. try {
  175. return typeof process !== "undefined"
  176. ? // eslint-disable-next-line no-process-env
  177. process.env?.[name]
  178. : undefined;
  179. }
  180. catch (e) {
  181. return undefined;
  182. }
  183. }
  184. function getLangSmithEnvironmentVariable(name) {
  185. return (getEnvironmentVariable(`LANGSMITH_${name}`) ||
  186. getEnvironmentVariable(`LANGCHAIN_${name}`));
  187. }
  188. function setEnvironmentVariable(name, value) {
  189. if (typeof process !== "undefined") {
  190. // eslint-disable-next-line no-process-env
  191. process.env[name] = value;
  192. }
  193. }
  194. let cachedCommitSHAs;
  195. /**
  196. * Get the Git commit SHA from common environment variables
  197. * used by different CI/CD platforms.
  198. * @returns {string | undefined} The Git commit SHA or undefined if not found.
  199. */
  200. function getShas() {
  201. if (cachedCommitSHAs !== undefined) {
  202. return cachedCommitSHAs;
  203. }
  204. const common_release_envs = [
  205. "VERCEL_GIT_COMMIT_SHA",
  206. "NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA",
  207. "COMMIT_REF",
  208. "RENDER_GIT_COMMIT",
  209. "CI_COMMIT_SHA",
  210. "CIRCLE_SHA1",
  211. "CF_PAGES_COMMIT_SHA",
  212. "REACT_APP_GIT_SHA",
  213. "SOURCE_VERSION",
  214. "GITHUB_SHA",
  215. "TRAVIS_COMMIT",
  216. "GIT_COMMIT",
  217. "BUILD_VCS_NUMBER",
  218. "bamboo_planRepository_revision",
  219. "Build.SourceVersion",
  220. "BITBUCKET_COMMIT",
  221. "DRONE_COMMIT_SHA",
  222. "SEMAPHORE_GIT_SHA",
  223. "BUILDKITE_COMMIT",
  224. ];
  225. const shas = {};
  226. for (const env of common_release_envs) {
  227. const envVar = getEnvironmentVariable(env);
  228. if (envVar !== undefined) {
  229. shas[env] = envVar;
  230. }
  231. }
  232. cachedCommitSHAs = shas;
  233. return shas;
  234. }