Skip to content

Commit d5fba38

Browse files
committed
chat fix
1 parent 68dd656 commit d5fba38

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

services/web/src/components/bot/Bot.tsx

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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: {

services/web/src/components/bot/chatbot.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,27 @@
143143
.rcb-chat-footer-container {
144144
display: none;
145145
}
146+
147+
input[type="password"] {
148+
width: 79%;
149+
padding: 12px 16px;
150+
border-radius: 16px;
151+
border: 1px solid #e5e7eb;
152+
background-color: #f3f4f6;
153+
font-size: 14px;
154+
color: #374151;
155+
margin: 8px 0;
156+
outline: none;
157+
transition: all 0.2s ease;
158+
}
159+
160+
input[type="password"]:focus {
161+
border-color: #8b5cf6;
162+
background-color: #ffffff;
163+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
164+
}
165+
166+
input[type="password"]::placeholder {
167+
color: #a3a3a3;
168+
font-style: italic;
169+
}

0 commit comments

Comments
 (0)