@@ -12,16 +12,15 @@ let assistantId = null;
1212let currentThreadId = null ;
1313let fileId ;
1414let documentTitle = document . title ; //title change => signals new file upload
15-
15+ let apiKey = null ; // Store the API key in memory
1616
1717//makes mutiple checks crucial for the assistant to work
1818async function connectIfNeeded ( ) {
19- const openai_API_KEY = sessionStorage . getItem ( 'openai-api-key' ) ; //if there an api key is saved, it will be in session storage
20- if ( ! openai_API_KEY )
19+ if ( ! apiKey )
2120 throw new Error ( 'openai API key not configured.' ) ;
2221 if ( ! openai ) {
2322 // instantiate openai object
24- openai = new Openai ( { apiKey : openai_API_KEY , dangerouslyAllowBrowser : true } ) ;
23+ openai = new Openai ( { apiKey, dangerouslyAllowBrowser : true } ) ;
2524 if ( ! openai ) {
2625 throw new Error ( 'Could not connect to open AI' ) ;
2726 }
@@ -349,8 +348,7 @@ async function upgradeAssistant() {
349348
350349//save the api key in the session storage, user will have to re-enter it when tab is closed
351350function saveAPIKey ( ) {
352- const apiKey = document . getElementById ( 'openai-api-key' ) . value . trim ( ) ;
353- sessionStorage . setItem ( 'openai-api-key' , apiKey ) ;
351+ apiKey = document . getElementById ( 'openai-api-key' ) . value . trim ( ) ;
354352}
355353
356354//toggle chat window
@@ -414,8 +412,6 @@ async function init(){
414412 document . getElementById ( "ai-chat-close-button" ) . addEventListener ( 'click' , ( ) => toggleChat ( false ) ) ;
415413 document . getElementById ( "ai-chat-input-area" ) . addEventListener ( 'submit' , sendMessage ) ;
416414 document . getElementById ( 'save-api-key' ) . addEventListener ( 'click' , saveAPIKey ) ;
417- //in case an api key was previously saved, add UI feedback to signal that to the user
418- document . getElementById ( 'openai-api-key' ) . value = sessionStorage . getItem ( 'openai-api-key' ) ;
419415 document . getElementById ( 'upgrade-assistant' ) . addEventListener ( 'click' , upgradeAssistant ) ;
420416}
421417
0 commit comments