Skip to content

Commit

Permalink
Merge pull request #7 from Milansuman/js_extensions
Browse files Browse the repository at this point in the history
[FEAT] add ollama to spotlight dialog
  • Loading branch information
Milansuman authored Aug 4, 2024
2 parents ebce20a + 3d0f023 commit 1598dea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/api/OllamaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <QUrl>
#include <QJsonObject>

OllamaApi::OllamaApi(QObject *parent): QObject(parent){}
OllamaApi::OllamaApi(QObject *parent): QObject(parent){
this->manager = new QNetworkAccessManager(this);
}

void OllamaApi::generate(QString model, QString prompt){
QJsonDocument params;
Expand All @@ -21,14 +23,15 @@ void OllamaApi::generate(QString model, QString prompt){
QNetworkRequest request(QUrl("http://127.0.0.1:11434/api/generate"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

this->manager = new QNetworkAccessManager(this);
this->manager->post(request, params.toJson());

this->connect(this->manager, &QNetworkAccessManager::finished, this, [=](QNetworkReply *reply){
QByteArray responseData = reply->readAll();
QJsonDocument result = QJsonDocument::fromJson(responseData);

emit this->responseGenerated(result);
if(reply->request().url() == QUrl("http://127.0.0.1:11434/api/generate")){
QByteArray responseData = reply->readAll();
QJsonDocument result = QJsonDocument::fromJson(responseData);

emit this->responseGenerated(result);
}
reply->deleteLater();
});
}
Expand Down
14 changes: 14 additions & 0 deletions src/windows/SpotlightDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ SpotlightDialog::SpotlightDialog(QWidget *parent): QDialog(parent){
this->dialogApi = new DialogApi();
this->channel->registerObject("dialog", this->dialogApi);

this->ollamaApi = new OllamaApi();
this->channel->registerObject("ollama", this->ollamaApi);

this->connect(this->dialogApi, &DialogApi::closeDialogRequested, this, [=](){
this->accept();
});
Expand All @@ -66,6 +69,17 @@ SpotlightDialog::SpotlightDialog(QWidget *parent): QDialog(parent){
window.tabs = channel.objects.tabs;
window.fs = channel.objects.fs;
window.dialog = channel.objects.dialog;
window.ai = {
generate: (model, prompt) => {
return new Promise((resolve, reject) => {
channel.objects.ollama.generate(model, prompt);
channel.objects.ollama.responseGenerated.connect((response) => {
resolve(response);
})
});
}
};
});
};
document.getElementsByTagName('head')[0].appendChild(script);
Expand Down
2 changes: 2 additions & 0 deletions src/windows/SpotlightDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../api/TabApi.h"
#include "../api/FileApi.h"
#include "../api/DialogApi.h"
#include "../api/OllamaApi.h"

class SpotlightDialog: public QDialog{
Q_OBJECT
Expand All @@ -20,6 +21,7 @@ class SpotlightDialog: public QDialog{
TabsApi *tabsApi;
FileApi *fileApi;
DialogApi *dialogApi;
OllamaApi *ollamaApi;
public:
SpotlightDialog(QWidget *parent=nullptr);
void open(int pos, int group);
Expand Down

0 comments on commit 1598dea

Please sign in to comment.