handles missing api key

This commit is contained in:
Santiago Lema 2025-05-07 19:26:56 -03:00
parent 2489e46d23
commit 708eb51755
7 changed files with 85 additions and 13 deletions

View file

@ -19,6 +19,7 @@ static int progressAnim = 0;
#include <StringView.h>
#include <View.h>
#include <Alert.h>
#include <MessageRunner.h>
#include "Conversation.h"
@ -26,15 +27,6 @@ static int progressAnim = 0;
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Window"
static const uint32 kMsgNewFile = 'fnew';
static const uint32 kMsgOpenFile = 'fopn';
static const uint32 kMsgSaveFile = 'fsav';
static const uint32 kPulse = 'plse';
static const uint32 kSendPrompt = 'kspt';
static const uint32 kQuestionChanged = 'kqch';
MainWindow::MainWindow()
: BWindow(BRect(100, 100, 600, 400), B_TRANSLATE("BeDumb"), B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) {
@ -118,22 +110,59 @@ MainWindow::MainWindow()
.SetInsets(5, 5, 5, 5)
.End();
//Just to animate progress
BMessageRunner* runner = new BMessageRunner(
this, // target BHandler
new BMessage(kPulse),
50000 // interval in μs (0ms)
);
PostMessage(kCheckKey);
}
void MainWindow::checkValidKey() {
if (!_conversation->validKey)
{
_infoView->SetText("MISSING API KEY");
ShowMissingKeyAlertAndQuit();
return;
}
else
_infoView->SetText("API Key loaded.");
}
void MainWindow::ShowMissingKeyAlertAndQuit()
{
BAlert* alert = new BAlert("Missing key file!", "Create a file named 'openai_key' containing a valid OpenAI Token on one line in \n\n/boot/home/config/settings/openai_key .\n\nThen relaunch the app.\n\nBe aware that this is not a safe storage so don't use valuable keys.", "Oh, no", "Sigh", "Just give up", B_WIDTH_AS_USUAL,B_WARNING_ALERT);
alert->SetType(B_INFO_ALERT);
uint32 result = alert->Go();
PostMessage(B_QUIT_REQUESTED);
}
MainWindow::~MainWindow() {}
void MainWindow::MessageReceived(BMessage *message) {
switch (message->what) {
case kCheckKey: {
checkValidKey();
}
break;
case kPulse: {
@ -141,8 +170,9 @@ void MainWindow::MessageReceived(BMessage *message) {
_progress->SetTo(progressAnim);
progressAnim++;
}
break;
}
break;
// case kMsgNewFile: {
// fSaveMenuItem->SetEnabled(false);
// printf("New\n");
@ -182,7 +212,7 @@ void MainWindow::MessageReceived(BMessage *message) {
const char* text;
if (message->FindString("text", &text) == B_OK) {
printf("Received text: %s\n", text);
// printf("Received text: %s\n", text);
// Do something with text (e.g., set it to a BTextView)
_answerView->SetText(text);
} else {