-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add widgets of CodeGeeX plugin Log: Add CodeGeeX UI Task:
- Loading branch information
1 parent
1751c82
commit d3a5258
Showing
21 changed files
with
1,235 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#include "codegeexmanager.h" | ||
#include "common/util/custompaths.h" | ||
|
||
#include <QDebug> | ||
#include <QFile> | ||
#include <QJsonObject> | ||
#include <QJsonDocument> | ||
#include <QMultiMap> | ||
#include <QUuid> | ||
#include <QTimer> | ||
#include <QDateTime> | ||
|
||
static const char *kUrlSSEChat = "https://codegeex.cn/prod/code/chatGlmSse/chat"; | ||
static const char *kUrlNewSession = "https://codegeex.cn/prod/code/chatGlmTalk/insert"; | ||
|
||
using namespace CodeGeeX; | ||
|
||
CodeGeeXManager *CodeGeeXManager::instance() | ||
{ | ||
static CodeGeeXManager ins; | ||
return &ins; | ||
} | ||
|
||
void CodeGeeXManager::login() | ||
{ | ||
if (sessionId.isEmpty() || userId.isEmpty()) { | ||
sessionId = uuid(); | ||
userId = uuid(); | ||
saveConfig(sessionId, userId); | ||
} | ||
|
||
QString machineId = QSysInfo::machineUniqueId(); | ||
askApi.sendLoginRequest(sessionId, machineId, userId); | ||
|
||
queryLoginState(); | ||
} | ||
|
||
void CodeGeeXManager::saveConfig(const QString &sessionId, const QString &userId) | ||
{ | ||
QJsonObject config; | ||
config["sessionId"] = sessionId; | ||
config["userId"] = userId; | ||
|
||
QJsonDocument document(config); | ||
|
||
QFile file(configFilePath()); | ||
file.open(QIODevice::WriteOnly); | ||
file.write(document.toJson()); | ||
file.close(); | ||
} | ||
|
||
void CodeGeeXManager::loadConfig() | ||
{ | ||
QFile file(configFilePath()); | ||
if (!file.exists()) | ||
return; | ||
|
||
file.open(QIODevice::ReadOnly); | ||
QString data = QString::fromUtf8(file.readAll()); | ||
file.close(); | ||
|
||
QJsonDocument document = QJsonDocument::fromJson(data.toUtf8()); | ||
QJsonObject config = document.object(); | ||
if (!config.empty()) { | ||
sessionId = config["sessionId"].toString(); | ||
userId = config["userId"].toString(); | ||
} | ||
} | ||
|
||
void CodeGeeXManager::sendMessage(const QString &prompt) | ||
{ | ||
QString askId = "User" + QString::number(QDateTime::currentMSecsSinceEpoch()); | ||
MessageData msgData(askId, MessageData::Ask); | ||
msgData.updateData(prompt); | ||
Q_EMIT requestMessageUpdate(msgData); | ||
|
||
QMultiMap<QString, QString> history {}; | ||
for (auto msgData : curSessionMsg) { | ||
history.insert(msgData.messageID(), msgData.messageData()); | ||
} | ||
QString machineId = QSysInfo::machineUniqueId(); | ||
askApi.postSSEChat(kUrlSSEChat, sessionId, prompt, machineId, history); | ||
} | ||
|
||
void CodeGeeXManager::onResponse(const QString &msgID, const QString &data, const QString &event) | ||
{ | ||
if (msgID.isEmpty()) | ||
return; | ||
|
||
// qInfo() << "resp msg:" << msgID << data << event; | ||
if (!curSessionMsg.contains(msgID)) | ||
curSessionMsg.insert(msgID, MessageData(msgID, MessageData::Anwser)); | ||
|
||
if (!data.isEmpty()) { | ||
curSessionMsg[msgID].updateData(data); | ||
Q_EMIT requestMessageUpdate(curSessionMsg[msgID]); | ||
} | ||
|
||
if (event == "finish") { | ||
|
||
} else if (event == "add"){ | ||
|
||
} | ||
} | ||
|
||
void CodeGeeXManager::recevieLoginState(AskApi::LoginState loginState) | ||
{ | ||
if (loginState == AskApi::LoginState::kLoginFailed) { | ||
qWarning() << "CodeGeeX login failed!"; | ||
// switch to login ui. | ||
} else if (loginState == AskApi::LoginState::kLoginSuccess) { | ||
Q_EMIT loginSuccessed(); | ||
// switch to ask page. | ||
} | ||
} | ||
|
||
CodeGeeXManager::CodeGeeXManager(QObject *parent) | ||
: QObject(parent) | ||
{ | ||
initConnections(); | ||
loadConfig(); | ||
} | ||
|
||
void CodeGeeXManager::initConnections() | ||
{ | ||
connect(&askApi, &AskApi::response, this, &CodeGeeXManager::onResponse); | ||
connect(&askApi, &AskApi::loginState, this, &CodeGeeXManager::recevieLoginState); | ||
} | ||
|
||
void CodeGeeXManager::queryLoginState() | ||
{ | ||
if (!queryTimer) { | ||
queryTimer = new QTimer(this); | ||
queryTimer->setSingleShot(true); | ||
|
||
connect(queryTimer, &QTimer::timeout, this, [ = ] { | ||
if (!sessionId.isEmpty()) | ||
askApi.sendQueryRequest(sessionId); | ||
}); | ||
} | ||
|
||
queryTimer->start(1000); | ||
} | ||
|
||
QString CodeGeeXManager::configFilePath() const | ||
{ | ||
return CustomPaths::user(CustomPaths::Configures) + "/codegeexcfg.json"; | ||
} | ||
|
||
QString CodeGeeXManager::uuid() | ||
{ | ||
QUuid uuid = QUuid::createUuid(); | ||
return uuid.toString().replace("{", "").replace("}", "").replace("-", "");; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#ifndef CODEGEEXMANAGER_H | ||
#define CODEGEEXMANAGER_H | ||
|
||
#include "codegeex/askapi.h" | ||
#include "data/messagedata.h" | ||
|
||
#include <QObject> | ||
#include <QMap> | ||
|
||
QT_BEGIN_NAMESPACE | ||
class QTimer; | ||
QT_END_NAMESPACE | ||
|
||
class CodeGeeXManager : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
static CodeGeeXManager *instance(); | ||
|
||
void login(); | ||
|
||
void saveConfig(const QString &sessionId, const QString &userId); | ||
void loadConfig(); | ||
|
||
void sendMessage(const QString &prompt); | ||
void queryLoginState(); | ||
|
||
Q_SIGNALS: | ||
void loginSuccessed(); | ||
void requestMessageUpdate(const MessageData &msg); | ||
|
||
public Q_SLOTS: | ||
void onResponse(const QString &msgID, const QString &data, const QString &event); | ||
void recevieLoginState(CodeGeeX::AskApi::LoginState loginState); | ||
|
||
private: | ||
explicit CodeGeeXManager(QObject *parent = nullptr); | ||
|
||
void initConnections(); | ||
|
||
QString configFilePath() const; | ||
QString uuid(); | ||
|
||
CodeGeeX::AskApi askApi; | ||
QString sessionId; | ||
QString userId; | ||
|
||
QMap<QString, MessageData> curSessionMsg {}; | ||
|
||
QTimer *queryTimer; | ||
}; | ||
|
||
#endif // CODEGEEXMANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#include "messagedata.h" | ||
|
||
#include <QDebug> | ||
|
||
MessageData::MessageData() | ||
{ | ||
} | ||
|
||
MessageData::MessageData(const QString &id, Type type) | ||
: msgId(id), | ||
msgType(type) | ||
{ | ||
} | ||
|
||
void MessageData::updateData(const QString &data) | ||
{ | ||
QStringList lines = data.split("\n"); | ||
if (lines.last().isEmpty()) | ||
lines.removeLast(); | ||
|
||
if (lines.length() < msgDataLines.length()) | ||
return; | ||
|
||
// QStringList newLines = lines.mid(msgDataLines.length()); | ||
// for (auto line : newLines) { | ||
// if (line.startsWith("```")) { | ||
// CodeData data; | ||
// data.langueage = line.mid(3); | ||
// codeDataList.append(data); | ||
// } | ||
// } | ||
|
||
msgData = data; | ||
msgDataLines = lines; | ||
// qInfo() << "update msg line" << msgDataLines; | ||
} | ||
|
||
QString MessageData::messageID() const | ||
{ | ||
return msgId; | ||
} | ||
|
||
MessageData::Type MessageData::messageType() const | ||
{ | ||
return msgType; | ||
} | ||
|
||
QString MessageData::messageData() const | ||
{ | ||
return msgData; | ||
} | ||
|
||
QStringList MessageData::messageLines() const | ||
{ | ||
return msgDataLines; | ||
} |
Oops, something went wrong.