@@ -67,6 +67,7 @@ const ChatBotComponent: React.FC<ChatBotComponentProps> = (props) => {
6767 const [ expanded , setExpanded ] = useState < boolean > ( false ) ;
6868 const [ chatResetKey , setChatResetKey ] = useState < number > ( 0 ) ;
6969 const helpOptions = [ "Initialize" , "Clear" , "Help" ] ;
70+ const [ apiKey , setApiKey ] = useState ( "" ) ;
7071
7172 const [ chatbotState , setChatbotState ] = useState < ChatBotState > ( {
7273 openapiKey : localStorage . getItem ( "openapi_key" ) ,
@@ -78,6 +79,10 @@ const ChatBotComponent: React.FC<ChatBotComponentProps> = (props) => {
7879 messages : [ ] ,
7980 } ) ;
8081
82+ const handleApiKey = ( event : React . ChangeEvent < HTMLInputElement > ) => {
83+ setApiKey ( event . target . value ) ; // Update state
84+ } ;
85+
8186 // Handle initialization
8287 const handleInitialization = async ( apiKey : string ) => {
8388 try {
@@ -87,7 +92,7 @@ const ChatBotComponent: React.FC<ChatBotComponentProps> = (props) => {
8792 . set ( "Accept" , "application/json" )
8893 . set ( "Content-Type" , "application/json" )
8994 . set ( "Authorization" , `Bearer ${ props . accessToken } ` )
90- . send ( { openai_key : apiKey } ) ;
95+ . send ( { openai_api_key : apiKey } ) ;
9196
9297 console . log ( "Initialization response:" , response . body ) ;
9398 return response . body . success || response . status === 200 ;
@@ -262,7 +267,7 @@ const ChatBotComponent: React.FC<ChatBotComponentProps> = (props) => {
262267 switch ( params . userInput ) {
263268 case "Initialize" :
264269 await params . injectMessage (
265- "Please enter your OpenAI API key to initialize the chatbot: " ,
270+ "Please type your OpenAI API key below and enter 'Submit' in the chat to initialize the chatbot. " ,
266271 ) ;
267272 return "initialize" ;
268273 case "Clear" :
@@ -302,25 +307,29 @@ What would you like to do next?`);
302307 renderMarkdown : [ "BOT" ] ,
303308 } ,
304309 initialize : {
305- message : "Please paste your OpenAI API key below:" ,
306- isSensitive : true ,
307- function : async ( params : Params ) => {
308- const apiKey = params . userInput . trim ( ) ;
309-
310- if ( ! apiKey ) {
310+ component : (
311+ < input
312+ type = "password"
313+ placeholder = "Please paste your OpenAI API key here"
314+ onChange = { handleApiKey }
315+ />
316+ ) ,
317+ path : async ( params : Params ) => {
318+ const APIKey = apiKey . trim ( ) ;
319+ if ( ! APIKey ) {
320+ await params . injectMessage (
321+ "API key cannot be empty. Please enter a valid OpenAI API key and enter 'Submit' in the chat." ,
322+ ) ;
323+ return "initialize" ;
324+ }
325+ if ( params . userInput . toLowerCase ( ) !== "submit" ) {
311326 await params . injectMessage (
312- "API key cannot be empty. Please enter a valid OpenAI API key: " ,
327+ "Please type 'Submit' to confirm your API key. " ,
313328 ) ;
314329 return ;
315330 }
316-
317- await params . injectMessage ( "Initializing chatbot with your API key..." ) ;
318-
319- const success = await handleInitialization ( apiKey ) ;
320-
331+ const success = await handleInitialization ( APIKey ) ;
321332 if ( success ) {
322- await params . injectMessage ( "✅ Chatbot initialized successfully!" ) ;
323-
324333 // Fetch chat history after successful initialization
325334 const chatHistory = await fetchChatHistory ( ) ;
326335 setChatbotState ( ( prev ) => ( {
@@ -331,21 +340,21 @@ What would you like to do next?`);
331340
332341 if ( chatHistory . length > 0 ) {
333342 await params . simulateStreamMessage (
334- `Loaded ${ chatHistory . length } previous messages. You can now start chatting!` ,
343+ `✅ Chatbot initialized successfully! Loaded ${ chatHistory . length } previous messages. You can now start chatting!` ,
335344 ) ;
336345 } else {
337346 await params . injectMessage (
338- "Ready to chat! Ask me anything about crAPI." ,
347+ "✅ Chatbot initialized successfully! Ready to chat! Ask me anything about crAPI." ,
339348 ) ;
340349 }
350+ return "chat" ;
341351 } else {
342352 await params . injectMessage (
343353 "❌ Failed to initialize chatbot. Please check your API key and try again:" ,
344354 ) ;
345- return ;
355+ return "show_options" ;
346356 }
347357 } ,
348- path : "chat" ,
349358 renderMarkdown : [ "BOT" ] ,
350359 } ,
351360 chat : {
0 commit comments