@@ -86,46 +86,13 @@ export function getRuntimeEnvironment(): RuntimeEnvironment {
8686}
8787
8888/**
89- * Retrieves the LangChain-specific environment variables from the current runtime environment.
90- * Sensitive keys (containing the word "key", "token", or "secret") have their values redacted for security.
89+ * Retrieves the LangSmith-specific metadata from the current runtime environment.
9190 *
9291 * @returns {Record<string, string> }
93- * - A record of LangChain -specific environment variables.
92+ * - A record of LangSmith -specific metadata environment variables.
9493 */
95- export function getLangChainEnvVars ( ) : Record < string , string > {
96- const allEnvVars = getEnvironmentVariables ( ) || { } ;
97- const envVars : Record < string , string > = { } ;
98-
99- for ( const [ key , value ] of Object . entries ( allEnvVars ) ) {
100- if ( key . startsWith ( "LANGCHAIN_" ) && typeof value === "string" ) {
101- envVars [ key ] = value ;
102- }
103- }
104-
105- for ( const key in envVars ) {
106- if (
107- ( key . toLowerCase ( ) . includes ( "key" ) ||
108- key . toLowerCase ( ) . includes ( "secret" ) ||
109- key . toLowerCase ( ) . includes ( "token" ) ) &&
110- typeof envVars [ key ] === "string"
111- ) {
112- const value = envVars [ key ] ;
113- envVars [ key ] =
114- value . slice ( 0 , 2 ) + "*" . repeat ( value . length - 4 ) + value . slice ( - 2 ) ;
115- }
116- }
117-
118- return envVars ;
119- }
120-
121- /**
122- * Retrieves the LangChain-specific metadata from the current runtime environment.
123- *
124- * @returns {Record<string, string> }
125- * - A record of LangChain-specific metadata environment variables.
126- */
127- export function getLangChainEnvVarsMetadata ( ) : Record < string , string > {
128- const allEnvVars = getEnvironmentVariables ( ) || { } ;
94+ export function getLangSmithEnvVarsMetadata ( ) : Record < string , string > {
95+ const allEnvVars = getLangSmithEnvironmentVariables ( ) ;
12996 const envVars : Record < string , string > = { } ;
13097 const excluded = [
13198 "LANGCHAIN_API_KEY" ,
@@ -142,7 +109,6 @@ export function getLangChainEnvVarsMetadata(): Record<string, string> {
142109
143110 for ( const [ key , value ] of Object . entries ( allEnvVars ) ) {
144111 if (
145- ( key . startsWith ( "LANGCHAIN_" ) || key . startsWith ( "LANGSMITH_" ) ) &&
146112 typeof value === "string" &&
147113 ! excluded . includes ( key ) &&
148114 ! key . toLowerCase ( ) . includes ( "key" ) &&
@@ -161,36 +127,46 @@ export function getLangChainEnvVarsMetadata(): Record<string, string> {
161127}
162128
163129/**
164- * Retrieves the environment variables from the current runtime environment.
130+ * Retrieves only the LangChain/LangSmith-prefixed environment variables from the current runtime environment.
131+ * This is more efficient than copying all environment variables.
165132 *
166- * This function is designed to operate in a variety of JS environments,
167- * including Node.js, Deno, browsers, etc.
168- *
169- * @returns {Record<string, string> | undefined }
170- * - A record of environment variables if available.
171- * - `undefined` if the environment does not support or allows access to environment variables.
133+ * @returns {Record<string, string> }
134+ * - A record of LangChain/LangSmith environment variables.
172135 */
173- export function getEnvironmentVariables ( ) : Record < string , string > | undefined {
136+ export function getLangSmithEnvironmentVariables ( ) : Record < string , string > {
137+ const envVars : Record < string , string > = { } ;
138+
174139 try {
175140 // Check for Node.js environment
176141 // eslint-disable-next-line no-process-env
177142 if ( typeof process !== "undefined" && process . env ) {
178143 // eslint-disable-next-line no-process-env
179- return Object . entries ( process . env ) . reduce (
180- ( acc : { [ key : string ] : string } , [ key , value ] ) => {
181- acc [ key ] = String ( value ) ;
182- return acc ;
183- } ,
184- { }
185- ) ;
144+ for ( const [ key , value ] of Object . entries ( process . env ) ) {
145+ if (
146+ ( key . startsWith ( "LANGCHAIN_" ) || key . startsWith ( "LANGSMITH_" ) ) &&
147+ value != null
148+ ) {
149+ if (
150+ ( key . toLowerCase ( ) . includes ( "key" ) ||
151+ key . toLowerCase ( ) . includes ( "secret" ) ||
152+ key . toLowerCase ( ) . includes ( "token" ) ) &&
153+ typeof value === "string"
154+ ) {
155+ envVars [ key ] =
156+ value . slice ( 0 , 2 ) +
157+ "*" . repeat ( value . length - 4 ) +
158+ value . slice ( - 2 ) ;
159+ } else {
160+ envVars [ key ] = value ;
161+ }
162+ }
163+ }
186164 }
187- // For browsers and other environments, we may not have direct access to env variables
188- // Return undefined or any other fallback as required.
189- return undefined ;
190165 } catch ( e ) {
191166 // Catch any errors that might occur while trying to access environment variables
192- return undefined ;
193167 }
168+
169+ return envVars ;
194170}
195171
196172export function getEnvironmentVariable ( name : string ) : string | undefined {
0 commit comments