mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-02-26 17:03:01 -05:00
refactor: Improve http client (#319)
This commit is contained in:
@ -22,8 +22,6 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkReply>
|
||||
#include <QtCore/qeventloop.h>
|
||||
|
||||
#include "llmcore/ValidationUtils.hpp"
|
||||
#include "logger/Logger.hpp"
|
||||
@ -147,35 +145,25 @@ void OllamaProvider::prepareRequest(
|
||||
}
|
||||
}
|
||||
|
||||
QList<QString> OllamaProvider::getInstalledModels(const QString &url)
|
||||
QFuture<QList<QString>> OllamaProvider::getInstalledModels(const QString &url)
|
||||
{
|
||||
QList<QString> models;
|
||||
QNetworkAccessManager manager;
|
||||
QNetworkRequest request(QString("%1%2").arg(url, "/api/tags"));
|
||||
prepareNetworkRequest(request);
|
||||
QNetworkReply *reply = manager.get(request);
|
||||
|
||||
QEventLoop loop;
|
||||
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QByteArray responseData = reply->readAll();
|
||||
QJsonDocument jsonResponse = QJsonDocument::fromJson(responseData);
|
||||
QJsonObject jsonObject = jsonResponse.object();
|
||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
||||
QList<QString> models;
|
||||
QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
||||
QJsonArray modelArray = jsonObject["models"].toArray();
|
||||
|
||||
for (const QJsonValue &value : modelArray) {
|
||||
QJsonObject modelObject = value.toObject();
|
||||
QString modelName = modelObject["name"].toString();
|
||||
models.append(modelName);
|
||||
models.append(modelObject["name"].toString());
|
||||
}
|
||||
} else {
|
||||
LOG_MESSAGE(QString("Error fetching models: %1").arg(reply->errorString()));
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
return models;
|
||||
return models;
|
||||
}).onFailed([](const std::exception &e) {
|
||||
LOG_MESSAGE(QString("Error fetching models: %1").arg(e.what()));
|
||||
return QList<QString>{};
|
||||
});
|
||||
}
|
||||
|
||||
QList<QString> OllamaProvider::validateRequest(const QJsonObject &request, LLMCore::TemplateType type)
|
||||
@ -248,12 +236,9 @@ void OllamaProvider::sendRequest(
|
||||
QNetworkRequest networkRequest(url);
|
||||
prepareNetworkRequest(networkRequest);
|
||||
|
||||
LLMCore::HttpRequest
|
||||
request{.networkRequest = networkRequest, .requestId = requestId, .payload = payload};
|
||||
|
||||
LOG_MESSAGE(QString("OllamaProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
||||
|
||||
emit httpClient()->sendRequest(request);
|
||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
||||
}
|
||||
|
||||
bool OllamaProvider::supportsTools() const
|
||||
@ -312,11 +297,11 @@ void OllamaProvider::onDataReceived(
|
||||
}
|
||||
|
||||
void OllamaProvider::onRequestFinished(
|
||||
const QodeAssist::LLMCore::RequestID &requestId, bool success, const QString &error)
|
||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
||||
{
|
||||
if (!success) {
|
||||
LOG_MESSAGE(QString("OllamaProvider request %1 failed: %2").arg(requestId, error));
|
||||
emit requestFailed(requestId, error);
|
||||
if (error) {
|
||||
LOG_MESSAGE(QString("OllamaProvider request %1 failed: %2").arg(requestId, *error));
|
||||
emit requestFailed(requestId, *error);
|
||||
cleanupRequest(requestId);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user