restored animation async from request
This commit is contained in:
parent
c8523bc115
commit
25eb366397
4 changed files with 53 additions and 41 deletions
|
@ -22,7 +22,17 @@ Conversation::Conversation(BHandler* replyTo) {
|
||||||
Conversation::~Conversation() {
|
Conversation::~Conversation() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Conversation::sendReply(BMessage message) {
|
||||||
|
BLooper* looper = replyTarget->Looper(); // get the looper it's attached to
|
||||||
|
|
||||||
|
if (looper != nullptr) {
|
||||||
|
BMessenger messenger(replyTarget, looper);
|
||||||
|
messenger.SendMessage(&message);
|
||||||
|
} else {
|
||||||
|
printf("Handler not attached to a looper.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Conversation::MessageReceived(BMessage* message) {
|
void Conversation::MessageReceived(BMessage* message) {
|
||||||
switch (message->what) {
|
switch (message->what) {
|
||||||
//.. case B_HTTP_DATA_RECEIVED: {
|
//.. case B_HTTP_DATA_RECEIVED: {
|
||||||
|
@ -67,50 +77,23 @@ void Conversation::MessageReceived(BMessage* message) {
|
||||||
BHttpBody body = _lastResult->Body();
|
BHttpBody body = _lastResult->Body();
|
||||||
if (body.text.has_value())
|
if (body.text.has_value())
|
||||||
{
|
{
|
||||||
|
|
||||||
json parsed = json::parse(body.text.value().String());
|
json parsed = json::parse(body.text.value().String());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string content = parsed["choices"][0]["message"]["content"];
|
std::string content = parsed["choices"][0]["message"]["content"];
|
||||||
|
|
||||||
// printf("we got content:%s",content.c_str());
|
// printf("we got content:%s",content.c_str());
|
||||||
|
|
||||||
|
|
||||||
BMessage message(kSendReply);
|
BMessage message(kSendReply);
|
||||||
message.AddString("text", BString(content.c_str()));
|
message.AddString("text", BString(content.c_str()));
|
||||||
|
sendReply(message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BMessage message(kSendReply);
|
||||||
|
message.AddString("text", "EMPTY BODY");
|
||||||
|
sendReply(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BLooper* looper = replyTarget->Looper(); // get the looper it's attached to
|
break;
|
||||||
|
|
||||||
if (looper != nullptr) {
|
|
||||||
BMessenger messenger(replyTarget, looper);
|
|
||||||
messenger.SendMessage(&message);
|
|
||||||
} else {
|
|
||||||
printf("Handler not attached to a looper.\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// status_t err = appMessenger.SendMessage(&message);
|
|
||||||
//if (err != B_OK)
|
|
||||||
// printf("SendMessage failed: %s\n", strerror(err));
|
|
||||||
//else
|
|
||||||
//printf("SendMessage OK");
|
|
||||||
|
|
||||||
// _answerView->SetText(content.c_str());
|
|
||||||
|
|
||||||
// _answerView->SetText(body.text.value());
|
|
||||||
|
|
||||||
}
|
|
||||||
// else
|
|
||||||
//_answerView->SetText("nuthin'");
|
|
||||||
}
|
|
||||||
|
|
||||||
//_infoView->SetText("Completed");
|
|
||||||
//_progress->SetMaxValue(100);
|
|
||||||
//_progress->SetTo(100);
|
|
||||||
|
|
||||||
} break;
|
|
||||||
|
|
||||||
case UrlEvent::HttpStatus: {
|
case UrlEvent::HttpStatus: {
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ public:
|
||||||
virtual void MessageReceived(BMessage *msg);
|
virtual void MessageReceived(BMessage *msg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void sendReply(BMessage message);
|
||||||
BHandler* replyTarget;
|
BHandler* replyTarget;
|
||||||
BString ReadOpenAIKey();
|
BString ReadOpenAIKey();
|
||||||
BString _apiKey;
|
BString _apiKey;
|
||||||
|
|
BIN
DumBer
BIN
DumBer
Binary file not shown.
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include "MainWindow.h"
|
#include "MainWindow.h"
|
||||||
|
|
||||||
|
static int progressAnim = 0;
|
||||||
|
|
||||||
|
|
||||||
#include <Application.h>
|
#include <Application.h>
|
||||||
|
@ -18,6 +19,7 @@
|
||||||
#include <StringView.h>
|
#include <StringView.h>
|
||||||
|
|
||||||
#include <View.h>
|
#include <View.h>
|
||||||
|
#include <MessageRunner.h>
|
||||||
|
|
||||||
#include "Conversation.h"
|
#include "Conversation.h"
|
||||||
|
|
||||||
|
@ -28,6 +30,8 @@ static const uint32 kMsgNewFile = 'fnew';
|
||||||
static const uint32 kMsgOpenFile = 'fopn';
|
static const uint32 kMsgOpenFile = 'fopn';
|
||||||
static const uint32 kMsgSaveFile = 'fsav';
|
static const uint32 kMsgSaveFile = 'fsav';
|
||||||
|
|
||||||
|
static const uint32 kPulse = 'plse';
|
||||||
|
|
||||||
static const uint32 kSendPrompt = 'kspt';
|
static const uint32 kSendPrompt = 'kspt';
|
||||||
static const uint32 kQuestionChanged = 'kqch';
|
static const uint32 kQuestionChanged = 'kqch';
|
||||||
|
|
||||||
|
@ -114,6 +118,14 @@ MainWindow::MainWindow()
|
||||||
.SetInsets(5, 5, 5, 5)
|
.SetInsets(5, 5, 5, 5)
|
||||||
|
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
|
|
||||||
|
BMessageRunner* runner = new BMessageRunner(
|
||||||
|
this, // target BHandler
|
||||||
|
new BMessage(kPulse),
|
||||||
|
100000 // interval in μs (100 ms)
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow() {}
|
MainWindow::~MainWindow() {}
|
||||||
|
@ -122,7 +134,16 @@ void MainWindow::MessageReceived(BMessage *message) {
|
||||||
|
|
||||||
switch (message->what) {
|
switch (message->what) {
|
||||||
|
|
||||||
// case kMsgNewFile: {
|
|
||||||
|
case kPulse: {
|
||||||
|
|
||||||
|
if (progressAnim >=1 && progressAnim <= 70) {
|
||||||
|
_progress->SetTo(progressAnim);
|
||||||
|
progressAnim++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// case kMsgNewFile: {
|
||||||
// fSaveMenuItem->SetEnabled(false);
|
// fSaveMenuItem->SetEnabled(false);
|
||||||
// printf("New\n");
|
// printf("New\n");
|
||||||
// } break;
|
// } break;
|
||||||
|
@ -139,7 +160,12 @@ void MainWindow::MessageReceived(BMessage *message) {
|
||||||
case kSendPrompt: {
|
case kSendPrompt: {
|
||||||
|
|
||||||
_progress->SetMaxValue(100);
|
_progress->SetMaxValue(100);
|
||||||
_progress->SetTo(5);
|
_progress->SetTo(0);
|
||||||
|
_answerView->SetText("...");
|
||||||
|
progressAnim = 1;//will trigger animation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
printf("Button Pressed\n");
|
printf("Button Pressed\n");
|
||||||
_infoView->SetText("Asking...");
|
_infoView->SetText("Asking...");
|
||||||
|
@ -151,6 +177,8 @@ void MainWindow::MessageReceived(BMessage *message) {
|
||||||
printf("Conversation returned!\n");
|
printf("Conversation returned!\n");
|
||||||
_infoView->SetText("Answer Received");
|
_infoView->SetText("Answer Received");
|
||||||
|
|
||||||
|
progressAnim = 100;
|
||||||
|
|
||||||
|
|
||||||
const char* text;
|
const char* text;
|
||||||
if (message->FindString("text", &text) == B_OK) {
|
if (message->FindString("text", &text) == B_OK) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue