@@ -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,25 @@ 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 ) {
311- await params . injectMessage (
312- "API key cannot be empty. Please enter a valid OpenAI API key:" ,
313- ) ;
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 ( "API key cannot be empty. Please enter a valid OpenAI API key and enter 'Submit' in the chat." ) ;
321+ return "initialize" ;
322+ }
323+ if ( params . userInput . toLowerCase ( ) !== "submit" ) {
324+ await params . injectMessage ( "Please type 'Submit' to confirm your API key." ) ;
314325 return ;
315326 }
316-
317- await params . injectMessage ( "Initializing chatbot with your API key..." ) ;
318-
319- const success = await handleInitialization ( apiKey ) ;
320-
321- if ( success ) {
322- await params . injectMessage ( "✅ Chatbot initialized successfully!" ) ;
323-
327+ const success = await handleInitialization ( APIKey ) ;
328+ if ( success ) {
324329 // Fetch chat history after successful initialization
325330 const chatHistory = await fetchChatHistory ( ) ;
326331 setChatbotState ( ( prev ) => ( {
@@ -331,21 +336,21 @@ What would you like to do next?`);
331336
332337 if ( chatHistory . length > 0 ) {
333338 await params . simulateStreamMessage (
334- `Loaded ${ chatHistory . length } previous messages. You can now start chatting!` ,
339+ `✅ Chatbot initialized successfully! Loaded ${ chatHistory . length } previous messages. You can now start chatting!` ,
335340 ) ;
336341 } else {
337342 await params . injectMessage (
338- "Ready to chat! Ask me anything about crAPI." ,
343+ "✅ Chatbot initialized successfully! Ready to chat! Ask me anything about crAPI." ,
339344 ) ;
340345 }
346+ return "chat" ;
341347 } else {
342348 await params . injectMessage (
343349 "❌ Failed to initialize chatbot. Please check your API key and try again:" ,
344350 ) ;
345- return ;
351+ return "show_options" ;
346352 }
347353 } ,
348- path : "chat" ,
349354 renderMarkdown : [ "BOT" ] ,
350355 } ,
351356 chat : {
0 commit comments