2023-04-14 14:44:28 -04:00
|
|
|
#include "network.h"
|
2023-06-26 11:42:11 -04:00
|
|
|
#include "llm.h"
|
2023-06-22 15:44:49 -04:00
|
|
|
#include "chatlistmodel.h"
|
2023-06-28 20:42:40 -04:00
|
|
|
#include "mysettings.h"
|
2023-04-14 14:44:28 -04:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2023-05-02 16:24:06 -04:00
|
|
|
#include <QGuiApplication>
|
2023-04-14 14:44:28 -04:00
|
|
|
#include <QUuid>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QNetworkRequest>
|
2023-05-02 16:24:06 -04:00
|
|
|
#include <QScreen>
|
2023-04-14 14:44:28 -04:00
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
#if defined(Q_OS_MAC)
|
2023-05-02 16:24:06 -04:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
std::string getCPUModel() {
|
|
|
|
char buffer[256];
|
|
|
|
size_t bufferlen = sizeof(buffer);
|
|
|
|
sysctlbyname("machdep.cpu.brand_string", &buffer, &bufferlen, NULL, 0);
|
|
|
|
return std::string(buffer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
class MyNetwork: public Network { };
|
|
|
|
Q_GLOBAL_STATIC(MyNetwork, networkInstance)
|
|
|
|
Network *Network::globalInstance()
|
|
|
|
{
|
|
|
|
return networkInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
Network::Network()
|
|
|
|
: QObject{nullptr}
|
2023-04-26 22:05:56 -04:00
|
|
|
, m_shouldSendStartup(false)
|
2023-04-14 14:44:28 -04:00
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
settings.sync();
|
|
|
|
m_uniqueId = settings.value("uniqueId", generateUniqueId()).toString();
|
|
|
|
settings.setValue("uniqueId", m_uniqueId);
|
|
|
|
settings.sync();
|
2023-06-28 20:42:40 -04:00
|
|
|
connect(MySettings::globalInstance(), &MySettings::networkIsActiveChanged, this, &Network::handleIsActiveChanged);
|
|
|
|
connect(MySettings::globalInstance(), &MySettings::networkUsageStatsActiveChanged, this, &Network::handleUsageStatsActiveChanged);
|
|
|
|
if (MySettings::globalInstance()->networkIsActive())
|
2023-04-28 10:54:05 -04:00
|
|
|
sendHealth();
|
2023-06-28 20:42:40 -04:00
|
|
|
if (MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-26 22:05:56 -04:00
|
|
|
sendIpify();
|
2023-04-24 17:52:19 -04:00
|
|
|
connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this,
|
|
|
|
&Network::handleSslErrors);
|
2023-04-14 14:44:28 -04:00
|
|
|
}
|
|
|
|
|
2023-06-28 20:42:40 -04:00
|
|
|
void Network::handleIsActiveChanged()
|
2023-04-14 14:44:28 -04:00
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-23 07:35:38 -04:00
|
|
|
sendHealth();
|
2023-04-14 14:44:28 -04:00
|
|
|
}
|
|
|
|
|
2023-06-28 20:42:40 -04:00
|
|
|
void Network::handleUsageStatsActiveChanged()
|
2023-04-28 10:54:05 -04:00
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-28 10:54:05 -04:00
|
|
|
sendOptOut();
|
|
|
|
else {
|
|
|
|
// model might be loaded already when user opt-in for first time
|
|
|
|
sendStartup();
|
|
|
|
sendIpify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
QString Network::generateUniqueId() const
|
|
|
|
{
|
|
|
|
return QUuid::createUuid().toString(QUuid::WithoutBraces);
|
|
|
|
}
|
|
|
|
|
2023-04-23 21:05:38 -04:00
|
|
|
bool Network::packageAndSendJson(const QString &ingestId, const QString &json)
|
2023-04-14 14:44:28 -04:00
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkIsActive())
|
2023-04-14 14:44:28 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QJsonParseError err;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8(), &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
|
|
|
qDebug() << "Couldn't parse: " << json << err.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_ASSERT(doc.isObject());
|
2023-06-22 15:44:49 -04:00
|
|
|
Q_ASSERT(ChatListModel::globalInstance()->currentChat());
|
2023-04-14 14:44:28 -04:00
|
|
|
QJsonObject object = doc.object();
|
|
|
|
object.insert("source", "gpt4all-chat");
|
2023-07-01 11:34:21 -04:00
|
|
|
object.insert("agent_id", ChatListModel::globalInstance()->currentChat()->modelInfo().filename());
|
2023-04-14 14:44:28 -04:00
|
|
|
object.insert("submitter_id", m_uniqueId);
|
2023-04-23 21:05:38 -04:00
|
|
|
object.insert("ingest_id", ingestId);
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2023-06-28 20:42:40 -04:00
|
|
|
QString attribution = MySettings::globalInstance()->networkAttribution();
|
2023-04-14 14:44:28 -04:00
|
|
|
if (!attribution.isEmpty())
|
2023-04-28 10:54:05 -04:00
|
|
|
object.insert("network/attribution", attribution);
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2023-07-01 11:34:21 -04:00
|
|
|
QString promptTemplate = ChatListModel::globalInstance()->currentChat()->modelInfo().promptTemplate();
|
2023-05-15 18:57:13 -04:00
|
|
|
object.insert("prompt_template", promptTemplate);
|
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
QJsonDocument newDoc;
|
|
|
|
newDoc.setObject(object);
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
2023-04-26 22:05:56 -04:00
|
|
|
printf("%s\n", qPrintable(newDoc.toJson(QJsonDocument::Indented)));
|
2023-04-14 14:44:28 -04:00
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2023-04-26 22:05:56 -04:00
|
|
|
|
2023-04-23 21:05:38 -04:00
|
|
|
QUrl jsonUrl("https://api.gpt4all.io/v1/ingest/chat");
|
2023-04-14 14:44:28 -04:00
|
|
|
QNetworkRequest request(jsonUrl);
|
2023-04-24 00:05:06 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-05-14 20:11:10 -04:00
|
|
|
QByteArray body(newDoc.toJson(QJsonDocument::Compact));
|
2023-04-14 14:44:28 -04:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
QNetworkReply *jsonReply = m_networkManager.post(request, body);
|
2023-07-11 12:37:21 -04:00
|
|
|
connect(qApp, &QCoreApplication::aboutToQuit, jsonReply, &QNetworkReply::abort);
|
2023-04-14 14:44:28 -04:00
|
|
|
connect(jsonReply, &QNetworkReply::finished, this, &Network::handleJsonUploadFinished);
|
|
|
|
m_activeUploads.append(jsonReply);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::handleJsonUploadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *jsonReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!jsonReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_activeUploads.removeAll(jsonReply);
|
|
|
|
|
|
|
|
QVariant response = jsonReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
Q_ASSERT(response.isValid());
|
|
|
|
bool ok;
|
|
|
|
int code = response.toInt(&ok);
|
|
|
|
if (!ok)
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: ingest invalid response.";
|
2023-04-23 07:35:38 -04:00
|
|
|
if (code != 200) {
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: ingest response != 200 code:" << code;
|
2023-04-23 07:35:38 -04:00
|
|
|
sendHealth();
|
|
|
|
}
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2023-06-02 15:46:41 -04:00
|
|
|
#if defined(DEBUG)
|
2023-04-14 14:44:28 -04:00
|
|
|
QByteArray jsonData = jsonReply->readAll();
|
|
|
|
QJsonParseError err;
|
2023-06-02 15:46:41 -04:00
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsonData, &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
|
|
|
qDebug() << "ERROR: Couldn't parse: " << jsonData << err.errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-26 22:05:56 -04:00
|
|
|
printf("%s\n", qPrintable(document.toJson(QJsonDocument::Indented)));
|
2023-04-14 14:44:28 -04:00
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
jsonReply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2023-04-24 17:52:19 -04:00
|
|
|
void Network::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
|
|
|
|
{
|
|
|
|
QUrl url = reply->request().url();
|
2023-06-02 15:46:41 -04:00
|
|
|
for (const auto &e : errors)
|
2023-04-24 17:52:19 -04:00
|
|
|
qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url;
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Network::sendOptOut()
|
|
|
|
{
|
|
|
|
QJsonObject properties;
|
|
|
|
properties.insert("token", "ce362e568ddaee16ed243eaffb5860a2");
|
|
|
|
properties.insert("time", QDateTime::currentSecsSinceEpoch());
|
|
|
|
properties.insert("distinct_id", m_uniqueId);
|
|
|
|
properties.insert("$insert_id", generateUniqueId());
|
|
|
|
|
|
|
|
QJsonObject event;
|
|
|
|
event.insert("event", "opt_out");
|
|
|
|
event.insert("properties", properties);
|
|
|
|
|
|
|
|
QJsonArray array;
|
|
|
|
array.append(event);
|
|
|
|
|
|
|
|
QJsonDocument doc;
|
|
|
|
doc.setArray(array);
|
2023-05-14 20:11:10 -04:00
|
|
|
sendMixpanel(doc.toJson(QJsonDocument::Compact), true /*isOptOut*/);
|
2023-04-28 10:54:05 -04:00
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("%s %s\n", qPrintable("opt_out"), qPrintable(doc.toJson(QJsonDocument::Indented)));
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-26 22:05:56 -04:00
|
|
|
void Network::sendModelLoaded()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("model_load");
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
void Network::sendResetContext(int conversationLength)
|
2023-04-26 22:05:56 -04:00
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
2023-05-02 20:31:17 -04:00
|
|
|
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("length");
|
|
|
|
kv.value = QJsonValue(conversationLength);
|
|
|
|
sendMixpanelEvent("reset_context", QVector<KeyValue>{kv});
|
2023-04-26 22:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendStartup()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
|
|
|
m_shouldSendStartup = true;
|
|
|
|
if (m_ipify.isEmpty())
|
|
|
|
return; // when it completes it will send
|
|
|
|
sendMixpanelEvent("startup");
|
|
|
|
}
|
|
|
|
|
2023-04-27 07:41:23 -04:00
|
|
|
void Network::sendCheckForUpdates()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-27 07:41:23 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("check_for_updates");
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
void Network::sendModelDownloaderDialog()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("download_dialog");
|
|
|
|
}
|
|
|
|
|
2023-05-14 20:12:15 -04:00
|
|
|
void Network::sendInstallModel(const QString &model)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-14 20:12:15 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("model");
|
|
|
|
kv.value = QJsonValue(model);
|
|
|
|
sendMixpanelEvent("install_model", QVector<KeyValue>{kv});
|
|
|
|
}
|
|
|
|
|
2023-05-16 09:32:01 -04:00
|
|
|
void Network::sendRemoveModel(const QString &model)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-16 09:32:01 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("model");
|
|
|
|
kv.value = QJsonValue(model);
|
|
|
|
sendMixpanelEvent("remove_model", QVector<KeyValue>{kv});
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
void Network::sendDownloadStarted(const QString &model)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("model");
|
|
|
|
kv.value = QJsonValue(model);
|
|
|
|
sendMixpanelEvent("download_started", QVector<KeyValue>{kv});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendDownloadCanceled(const QString &model)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("model");
|
|
|
|
kv.value = QJsonValue(model);
|
|
|
|
sendMixpanelEvent("download_canceled", QVector<KeyValue>{kv});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendDownloadError(const QString &model, int code, const QString &errorString)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("model");
|
|
|
|
kv.value = QJsonValue(model);
|
|
|
|
KeyValue kvCode;
|
|
|
|
kvCode.key = QString("code");
|
|
|
|
kvCode.value = QJsonValue(code);
|
|
|
|
KeyValue kvError;
|
|
|
|
kvError.key = QString("error");
|
|
|
|
kvError.value = QJsonValue(errorString);
|
|
|
|
sendMixpanelEvent("download_error", QVector<KeyValue>{kv, kvCode, kvError});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendDownloadFinished(const QString &model, bool success)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("model");
|
|
|
|
kv.value = QJsonValue(model);
|
|
|
|
KeyValue kvSuccess;
|
|
|
|
kvSuccess.key = QString("success");
|
|
|
|
kvSuccess.value = QJsonValue(success);
|
|
|
|
sendMixpanelEvent("download_finished", QVector<KeyValue>{kv, kvSuccess});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendSettingsDialog()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("settings_dialog");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendNetworkToggled(bool isActive)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("isActive");
|
|
|
|
kv.value = QJsonValue(isActive);
|
|
|
|
sendMixpanelEvent("network_toggled", QVector<KeyValue>{kv});
|
2023-05-05 12:30:11 -04:00
|
|
|
}
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
void Network::sendNewChat(int count)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("number_of_chats");
|
|
|
|
kv.value = QJsonValue(count);
|
|
|
|
sendMixpanelEvent("new_chat", QVector<KeyValue>{kv});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendRemoveChat()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("remove_chat");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendRenameChat()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("rename_chat");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendChatStarted()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("chat_started");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendRecalculatingContext(int conversationLength)
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
KeyValue kv;
|
|
|
|
kv.key = QString("length");
|
|
|
|
kv.value = QJsonValue(conversationLength);
|
|
|
|
sendMixpanelEvent("recalc_context", QVector<KeyValue>{kv});
|
|
|
|
}
|
|
|
|
|
2023-05-08 08:31:35 -04:00
|
|
|
void Network::sendNonCompatHardware()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-05-08 08:31:35 -04:00
|
|
|
return;
|
|
|
|
sendMixpanelEvent("noncompat_hardware");
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
void Network::sendMixpanelEvent(const QString &ev, const QVector<KeyValue> &values)
|
2023-04-26 22:05:56 -04:00
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
Q_ASSERT(ChatListModel::globalInstance()->currentChat());
|
2023-04-26 22:05:56 -04:00
|
|
|
QJsonObject properties;
|
|
|
|
properties.insert("token", "ce362e568ddaee16ed243eaffb5860a2");
|
|
|
|
properties.insert("time", QDateTime::currentSecsSinceEpoch());
|
|
|
|
properties.insert("distinct_id", m_uniqueId);
|
|
|
|
properties.insert("$insert_id", generateUniqueId());
|
|
|
|
properties.insert("$os", QSysInfo::prettyProductName());
|
|
|
|
if (!m_ipify.isEmpty())
|
|
|
|
properties.insert("ip", m_ipify);
|
|
|
|
properties.insert("name", QCoreApplication::applicationName() + " v"
|
|
|
|
+ QCoreApplication::applicationVersion());
|
2023-07-01 11:34:21 -04:00
|
|
|
properties.insert("model", ChatListModel::globalInstance()->currentChat()->modelInfo().filename());
|
2023-09-16 09:42:22 -04:00
|
|
|
properties.insert("requestedDevice", MySettings::globalInstance()->device());
|
|
|
|
properties.insert("actualDevice", ChatListModel::globalInstance()->currentChat()->device());
|
2023-04-26 22:05:56 -04:00
|
|
|
|
2023-05-02 16:24:06 -04:00
|
|
|
// Some additional startup information
|
|
|
|
if (ev == "startup") {
|
|
|
|
const QSize display = QGuiApplication::primaryScreen()->size();
|
|
|
|
properties.insert("display", QString("%1x%2").arg(display.width()).arg(display.height()));
|
2023-06-26 11:42:11 -04:00
|
|
|
properties.insert("ram", LLM::globalInstance()->systemTotalRAMInGB());
|
2023-05-02 20:31:17 -04:00
|
|
|
#if defined(Q_OS_MAC)
|
2023-05-02 16:24:06 -04:00
|
|
|
properties.insert("cpu", QString::fromStdString(getCPUModel()));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:46:41 -04:00
|
|
|
for (const auto& p : values)
|
2023-05-02 20:31:17 -04:00
|
|
|
properties.insert(p.key, p.value);
|
|
|
|
|
2023-04-26 22:05:56 -04:00
|
|
|
QJsonObject event;
|
|
|
|
event.insert("event", ev);
|
|
|
|
event.insert("properties", properties);
|
|
|
|
|
|
|
|
QJsonArray array;
|
|
|
|
array.append(event);
|
|
|
|
|
|
|
|
QJsonDocument doc;
|
|
|
|
doc.setArray(array);
|
2023-05-14 20:11:10 -04:00
|
|
|
sendMixpanel(doc.toJson(QJsonDocument::Compact));
|
2023-04-26 22:05:56 -04:00
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("%s %s\n", qPrintable(ev), qPrintable(doc.toJson(QJsonDocument::Indented)));
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendIpify()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive() || !m_ipify.isEmpty())
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
QUrl ipifyUrl("https://api.ipify.org");
|
|
|
|
QNetworkRequest request(ipifyUrl);
|
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
|
|
|
QNetworkReply *reply = m_networkManager.get(request);
|
2023-07-11 12:37:21 -04:00
|
|
|
connect(qApp, &QCoreApplication::aboutToQuit, reply, &QNetworkReply::abort);
|
2023-04-26 22:05:56 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, this, &Network::handleIpifyFinished);
|
|
|
|
}
|
|
|
|
|
2023-04-29 11:05:44 -04:00
|
|
|
void Network::sendMixpanel(const QByteArray &json, bool isOptOut)
|
2023-04-26 22:05:56 -04:00
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive() && !isOptOut)
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
QUrl trackUrl("https://api.mixpanel.com/track");
|
|
|
|
QNetworkRequest request(trackUrl);
|
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
QNetworkReply *trackReply = m_networkManager.post(request, json);
|
2023-07-11 12:37:21 -04:00
|
|
|
connect(qApp, &QCoreApplication::aboutToQuit, trackReply, &QNetworkReply::abort);
|
2023-04-26 22:05:56 -04:00
|
|
|
connect(trackReply, &QNetworkReply::finished, this, &Network::handleMixpanelFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::handleIpifyFinished()
|
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
Q_ASSERT(MySettings::globalInstance()->networkUsageStatsActive());
|
2023-04-26 22:05:56 -04:00
|
|
|
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QVariant response = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
Q_ASSERT(response.isValid());
|
|
|
|
bool ok;
|
|
|
|
int code = response.toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
qWarning() << "ERROR: ipify invalid response.";
|
|
|
|
if (code != 200)
|
|
|
|
qWarning() << "ERROR: ipify response != 200 code:" << code;
|
2024-03-06 17:14:54 -05:00
|
|
|
m_ipify = reply->readAll();
|
2023-04-26 22:05:56 -04:00
|
|
|
#if defined(DEBUG)
|
2024-03-06 17:14:54 -05:00
|
|
|
printf("ipify finished %s\n", qPrintable(m_ipify));
|
2023-04-26 22:05:56 -04:00
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (m_shouldSendStartup)
|
|
|
|
sendStartup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::handleMixpanelFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QVariant response = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
Q_ASSERT(response.isValid());
|
|
|
|
bool ok;
|
|
|
|
int code = response.toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
qWarning() << "ERROR: track invalid response.";
|
|
|
|
if (code != 200)
|
|
|
|
qWarning() << "ERROR: track response != 200 code:" << code;
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("mixpanel finished %s\n", qPrintable(reply->readAll()));
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2023-04-23 21:05:38 -04:00
|
|
|
bool Network::sendConversation(const QString &ingestId, const QString &conversation)
|
2023-04-14 14:44:28 -04:00
|
|
|
{
|
2023-04-23 21:05:38 -04:00
|
|
|
return packageAndSendJson(ingestId, conversation);
|
2023-04-14 14:44:28 -04:00
|
|
|
}
|
2023-04-23 07:35:38 -04:00
|
|
|
|
|
|
|
void Network::sendHealth()
|
|
|
|
{
|
2023-04-23 21:05:38 -04:00
|
|
|
QUrl healthUrl("https://api.gpt4all.io/v1/health");
|
2023-04-23 07:35:38 -04:00
|
|
|
QNetworkRequest request(healthUrl);
|
2023-04-24 00:05:06 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-04-23 07:35:38 -04:00
|
|
|
QNetworkReply *healthReply = m_networkManager.get(request);
|
2023-07-11 12:37:21 -04:00
|
|
|
connect(qApp, &QCoreApplication::aboutToQuit, healthReply, &QNetworkReply::abort);
|
2023-04-23 07:35:38 -04:00
|
|
|
connect(healthReply, &QNetworkReply::finished, this, &Network::handleHealthFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::handleHealthFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *healthReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!healthReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QVariant response = healthReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
Q_ASSERT(response.isValid());
|
|
|
|
bool ok;
|
|
|
|
int code = response.toInt(&ok);
|
|
|
|
if (!ok)
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: health invalid response.";
|
2023-04-23 07:35:38 -04:00
|
|
|
if (code != 200) {
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: health response != 200 code:" << code;
|
2023-04-23 07:35:38 -04:00
|
|
|
emit healthCheckFailed(code);
|
2023-06-28 20:42:40 -04:00
|
|
|
MySettings::globalInstance()->setNetworkIsActive(false);
|
2023-04-23 07:35:38 -04:00
|
|
|
}
|
|
|
|
healthReply->deleteLater();
|
|
|
|
}
|