env.js 7.1 KB

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