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

@ -38,6 +38,8 @@ void App::AboutRequested() {
about->Show();
}
int main() {
App *app = new App();
app->Run();

7
App.h
View file

@ -21,6 +21,13 @@ public:
MainWindow* mainWindow = nullptr;
void ReadyToRun()
{
printf("ready to Run");
// mainWindow->checkValidKey();
}
private:
};

View file

@ -6,6 +6,7 @@
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
#include <Alert.h>
#include <Url.h>
#include <MimeType.h>
@ -178,6 +179,9 @@ void Conversation::ask(const std::string& prompt) {
}
BString Conversation::ReadOpenAIKey() {
// /boot/home/config/openai_key
@ -187,13 +191,16 @@ BString Conversation::ReadOpenAIKey() {
if (find_directory(B_USER_SETTINGS_DIRECTORY, &configPath) != B_OK)
return "error: couldn't find config directory";
configPath.Append("openai_key");
configPath.Append("openai_keyxxx");
BFile file(configPath.Path(), B_READ_ONLY);
printf("full path:%s\n", configPath.Path());
if (file.InitCheck() != B_OK)
{
validKey=false;
return "error: couldn't open key file ";
}
off_t size;
file.GetSize(&size);
@ -205,6 +212,8 @@ BString Conversation::ReadOpenAIKey() {
BString result(buffer);
delete[] buffer;
validKey=true;
return result;
}

View file

@ -47,12 +47,16 @@ using namespace BPrivate::Network;
class Conversation : public BHandler {
public:
bool validKey = false;
Conversation(BHandler* replyTo);
virtual ~Conversation() ;
virtual void ask(const std::string& prompt);
virtual void MessageReceived(BMessage *msg);
private:
void sendReply(BMessage message);
BHandler* replyTarget;
BString ReadOpenAIKey();

BIN
DumBer

Binary file not shown.

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) {
@ -120,20 +112,57 @@ MainWindow::MainWindow()
.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 {

View file

@ -11,10 +11,25 @@
#include <StatusBar.h>
#include <Window.h>
#include <String.h>
#include <MessageRunner.h>
#include "Conversation.h"
static const uint32 kCheckKey = 'chkk';
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';
class MainWindow : public BWindow {
public:
@ -25,8 +40,13 @@ public:
Conversation* _conversation = new Conversation(this);
void checkValidKey();
private:
void ShowMissingKeyAlertAndQuit();
BMenuBar *_BuildMenu();
BTextView * _answerView;