Skip to content

Commit 1bbcd3c

Browse files
authored
chat fix (#318)
* chat fix * prettier formatting
1 parent 68dd656 commit 1bbcd3c

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

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

Lines changed: 29 additions & 20 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,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: {

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)