@@ -109,6 +109,8 @@ <h2 class="text-xl font-semibold mb-4">AI Configuration</h2>
109109 </ svg >
110110 </ button >
111111 </ div >
112+ < button id ="openai-connect-button "
113+ class ="bg-green-500 hover:bg-green-600 text-white rounded-md px-5 py-2 mt-2 "> Connect AI</ button >
112114 </ div >
113115
114116
@@ -467,7 +469,18 @@ <h2 class="text-xl font-semibold mb-4">Debug Output</h2>
467469 }
468470
469471 // check connection
470- check_connection ( ) ;
472+ // check_connection();
473+
474+ // attach connection handler to the OpenAI connect button
475+ const openaiConnectButton = document . getElementById ( "openai-connect-button" ) ;
476+ if ( openaiConnectButton ) {
477+ openaiConnectButton . addEventListener ( "click" , ( ) => {
478+ check_connection ( ) ;
479+ } ) ;
480+ }
481+
482+ // expose check_connection globally for other modules
483+ window . check_openai_connection = check_connection ;
471484
472485 // global variable to store wakeup messages
473486 let wakeup_schedule = [ ] ;
@@ -1890,9 +1903,11 @@ <h2 class="text-xl font-semibold mb-4">Debug Output</h2>
18901903
18911904 < script type ="module ">
18921905 import OpenAI from "https://cdn.jsdelivr.net/npm/[email protected] /+esm" ; 1893-
1894- // Access the global OpenAI instance safely
1895- const openai = window . openai ;
1906+
1907+ // Helper to access the global OpenAI instance
1908+ function getOpenAIInstance ( ) {
1909+ return window . openai ;
1910+ }
18961911
18971912 // add text to chat box
18981913 function add_text_to_chat ( text , role = "assistant" ) {
@@ -1953,6 +1968,8 @@ <h2 class="text-xl font-semibold mb-4">Debug Output</h2>
19531968 }
19541969
19551970 // add a message to the thread
1971+ const openai = getOpenAIInstance ( ) ;
1972+
19561973 const message = await openai . beta . threads . messages . create ( openai_thread_id , { role : "user" , content : input } ) ;
19571974
19581975 // run the assistant
@@ -2055,15 +2072,28 @@ <h2 class="text-xl font-semibold mb-4">Debug Output</h2>
20552072
20562073 async function handleTranscription ( blob ) {
20572074 try {
2075+ // ensure OpenAI connection exists
2076+ let openai = getOpenAIInstance ( ) ;
2077+ if ( ! openai && window . check_openai_connection ) {
2078+ const ok = await window . check_openai_connection ( ) ;
2079+ if ( ! ok ) {
2080+ add_text_to_debug ( "Unable to connect to OpenAI" ) ;
2081+ return ;
2082+ }
2083+ openai = getOpenAIInstance ( ) ;
2084+ }
2085+
20582086 const formData = new FormData ( ) ;
20592087 formData . append ( "file" , blob , "recording.webm" ) ;
20602088 formData . append ( "model" , "whisper-1" ) ;
20612089 // update this static language option with dynamic in future , let user choose language
20622090 formData . append ( "language" , "en" ) ;
20632091
2092+ const apiKey = openai ? openai . apiKey : document . getElementById ( "openai_api_key" ) . value . trim ( ) ;
2093+
20642094 const resp = await fetch ( "https://api.openai.com/v1/audio/transcriptions" , {
20652095 method : "POST" ,
2066- headers : { "Authorization" : `Bearer ${ openai . apiKey } ` } ,
2096+ headers : { "Authorization" : `Bearer ${ apiKey } ` } ,
20672097 body : formData
20682098 } ) ;
20692099 const data = await resp . json ( ) ;
0 commit comments