handles missing api key
This commit is contained in:
parent
2489e46d23
commit
708eb51755
7 changed files with 85 additions and 13 deletions
2
App.cpp
2
App.cpp
|
@ -38,6 +38,8 @@ void App::AboutRequested() {
|
||||||
about->Show();
|
about->Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
App *app = new App();
|
App *app = new App();
|
||||||
app->Run();
|
app->Run();
|
||||||
|
|
7
App.h
7
App.h
|
@ -21,6 +21,13 @@ public:
|
||||||
MainWindow* mainWindow = nullptr;
|
MainWindow* mainWindow = nullptr;
|
||||||
|
|
||||||
|
|
||||||
|
void ReadyToRun()
|
||||||
|
{
|
||||||
|
printf("ready to Run");
|
||||||
|
// mainWindow->checkValidKey();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <FindDirectory.h>
|
#include <FindDirectory.h>
|
||||||
#include <Path.h>
|
#include <Path.h>
|
||||||
#include <String.h>
|
#include <String.h>
|
||||||
|
#include <Alert.h>
|
||||||
|
|
||||||
#include <Url.h>
|
#include <Url.h>
|
||||||
#include <MimeType.h>
|
#include <MimeType.h>
|
||||||
|
@ -178,6 +179,9 @@ void Conversation::ask(const std::string& prompt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BString Conversation::ReadOpenAIKey() {
|
BString Conversation::ReadOpenAIKey() {
|
||||||
|
|
||||||
// /boot/home/config/openai_key
|
// /boot/home/config/openai_key
|
||||||
|
@ -187,13 +191,16 @@ BString Conversation::ReadOpenAIKey() {
|
||||||
if (find_directory(B_USER_SETTINGS_DIRECTORY, &configPath) != B_OK)
|
if (find_directory(B_USER_SETTINGS_DIRECTORY, &configPath) != B_OK)
|
||||||
return "error: couldn't find config directory";
|
return "error: couldn't find config directory";
|
||||||
|
|
||||||
configPath.Append("openai_key");
|
configPath.Append("openai_keyxxx");
|
||||||
|
|
||||||
BFile file(configPath.Path(), B_READ_ONLY);
|
BFile file(configPath.Path(), B_READ_ONLY);
|
||||||
|
|
||||||
printf("full path:%s\n", configPath.Path());
|
printf("full path:%s\n", configPath.Path());
|
||||||
if (file.InitCheck() != B_OK)
|
if (file.InitCheck() != B_OK)
|
||||||
|
{
|
||||||
|
validKey=false;
|
||||||
return "error: couldn't open key file ";
|
return "error: couldn't open key file ";
|
||||||
|
}
|
||||||
|
|
||||||
off_t size;
|
off_t size;
|
||||||
file.GetSize(&size);
|
file.GetSize(&size);
|
||||||
|
@ -205,6 +212,8 @@ BString Conversation::ReadOpenAIKey() {
|
||||||
BString result(buffer);
|
BString result(buffer);
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
|
|
||||||
|
validKey=true;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,12 +47,16 @@ using namespace BPrivate::Network;
|
||||||
class Conversation : public BHandler {
|
class Conversation : public BHandler {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
bool validKey = false;
|
||||||
|
|
||||||
Conversation(BHandler* replyTo);
|
Conversation(BHandler* replyTo);
|
||||||
virtual ~Conversation() ;
|
virtual ~Conversation() ;
|
||||||
virtual void ask(const std::string& prompt);
|
virtual void ask(const std::string& prompt);
|
||||||
virtual void MessageReceived(BMessage *msg);
|
virtual void MessageReceived(BMessage *msg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
||||||
void sendReply(BMessage message);
|
void sendReply(BMessage message);
|
||||||
BHandler* replyTarget;
|
BHandler* replyTarget;
|
||||||
BString ReadOpenAIKey();
|
BString ReadOpenAIKey();
|
||||||
|
|
BIN
DumBer
BIN
DumBer
Binary file not shown.
|
@ -19,6 +19,7 @@ static int progressAnim = 0;
|
||||||
#include <StringView.h>
|
#include <StringView.h>
|
||||||
|
|
||||||
#include <View.h>
|
#include <View.h>
|
||||||
|
#include <Alert.h>
|
||||||
#include <MessageRunner.h>
|
#include <MessageRunner.h>
|
||||||
|
|
||||||
#include "Conversation.h"
|
#include "Conversation.h"
|
||||||
|
@ -26,15 +27,6 @@ static int progressAnim = 0;
|
||||||
#undef B_TRANSLATION_CONTEXT
|
#undef B_TRANSLATION_CONTEXT
|
||||||
#define B_TRANSLATION_CONTEXT "Window"
|
#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()
|
MainWindow::MainWindow()
|
||||||
: BWindow(BRect(100, 100, 600, 400), B_TRANSLATE("BeDumb"), B_TITLED_WINDOW,
|
: BWindow(BRect(100, 100, 600, 400), B_TRANSLATE("BeDumb"), B_TITLED_WINDOW,
|
||||||
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) {
|
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) {
|
||||||
|
@ -120,20 +112,57 @@ MainWindow::MainWindow()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Just to animate progress
|
||||||
BMessageRunner* runner = new BMessageRunner(
|
BMessageRunner* runner = new BMessageRunner(
|
||||||
this, // target BHandler
|
this, // target BHandler
|
||||||
new BMessage(kPulse),
|
new BMessage(kPulse),
|
||||||
50000 // interval in μs (0 ms)
|
50000 // interval in μs (0 ms)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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() {}
|
MainWindow::~MainWindow() {}
|
||||||
|
|
||||||
void MainWindow::MessageReceived(BMessage *message) {
|
void MainWindow::MessageReceived(BMessage *message) {
|
||||||
|
|
||||||
switch (message->what) {
|
switch (message->what) {
|
||||||
|
|
||||||
|
case kCheckKey: {
|
||||||
|
checkValidKey();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case kPulse: {
|
case kPulse: {
|
||||||
|
|
||||||
|
@ -141,8 +170,9 @@ void MainWindow::MessageReceived(BMessage *message) {
|
||||||
_progress->SetTo(progressAnim);
|
_progress->SetTo(progressAnim);
|
||||||
progressAnim++;
|
progressAnim++;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
// case kMsgNewFile: {
|
// case kMsgNewFile: {
|
||||||
// fSaveMenuItem->SetEnabled(false);
|
// fSaveMenuItem->SetEnabled(false);
|
||||||
// printf("New\n");
|
// printf("New\n");
|
||||||
|
@ -182,7 +212,7 @@ void MainWindow::MessageReceived(BMessage *message) {
|
||||||
|
|
||||||
const char* text;
|
const char* text;
|
||||||
if (message->FindString("text", &text) == B_OK) {
|
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)
|
// Do something with text (e.g., set it to a BTextView)
|
||||||
_answerView->SetText(text);
|
_answerView->SetText(text);
|
||||||
} else {
|
} else {
|
||||||
|
|
20
MainWindow.h
20
MainWindow.h
|
@ -11,10 +11,25 @@
|
||||||
#include <StatusBar.h>
|
#include <StatusBar.h>
|
||||||
#include <Window.h>
|
#include <Window.h>
|
||||||
#include <String.h>
|
#include <String.h>
|
||||||
|
#include <MessageRunner.h>
|
||||||
|
|
||||||
|
|
||||||
#include "Conversation.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 {
|
class MainWindow : public BWindow {
|
||||||
public:
|
public:
|
||||||
|
@ -25,8 +40,13 @@ public:
|
||||||
|
|
||||||
Conversation* _conversation = new Conversation(this);
|
Conversation* _conversation = new Conversation(this);
|
||||||
|
|
||||||
|
void checkValidKey();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
void ShowMissingKeyAlertAndQuit();
|
||||||
|
|
||||||
BMenuBar *_BuildMenu();
|
BMenuBar *_BuildMenu();
|
||||||
BTextView * _answerView;
|
BTextView * _answerView;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue