Skip to content

Commit

Permalink
feat: [CodeGeeX]Widgets of CodeGeeX
Browse files Browse the repository at this point in the history
add widgets of CodeGeeX plugin

Log: Add CodeGeeX UI
Task:
  • Loading branch information
Lighto-Ku authored and deepin-mozart committed Nov 10, 2023
1 parent 1751c82 commit d3a5258
Show file tree
Hide file tree
Showing 21 changed files with 1,235 additions and 31 deletions.
32 changes: 7 additions & 25 deletions src/plugins/codegeex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,17 @@ cmake_minimum_required(VERSION 3.0.2)

project(plugin-codegeex)

set(CXX_CPP
codegeex.cpp
eventreceiver.cpp
codegeex/askapi.cpp
codegeex/copilotapi.cpp
askpage/askpage.cpp
option/codegeexoptionwidget.cpp
option/detailwidget.cpp
option/optioncodegeexgenerator.cpp
copilot.cpp
codegeex.json
)

set(CXX_H
codegeex.h
eventreceiver.h
codegeex/askapi.h
codegeex/copilotapi.h
copilot.h
askpage/askpage.h
option/codegeexoptionwidget.h
option/detailwidget.h
option/optioncodegeexgenerator.h
FILE(GLOB CODEGEEX_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/*/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/*.json"
)

add_library(${PROJECT_NAME}
SHARED
${CXX_H}
${CXX_CPP}
${CODEGEEX_FILES}
codegeex.qrc
)

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codegeex/askpage/askpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ AskPage::AskPage(QWidget *parent) : QWidget(parent)
connect(timer, SIGNAL(timeout()), this, SLOT(queryLoginState()));
timer->start(1000);

connect(&askApi, &AskApi::response, [this](const QString &response, const QString &event){
connect(&askApi, &AskApi::response, [this](const QString &msgID, const QString &response, const QString &event){
int responseSize = response.size();
if (responseSize < totalResponseSize || responseSize == 0)
return;
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/codegeex/codegeex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "codegeex.h"
#include "askpage/askpage.h"
#include "option/optioncodegeexgenerator.h"
#include "widgets/codegeexwidget.h"
#include "codegeexmanager.h"

#include "common/common.h"
#include "services/window/windowservice.h"
Expand All @@ -31,8 +33,11 @@ bool CodeGeex::start()

// Add widget to left bar
if (windowService->addCentralNavigation) {
auto askPage = new AskPage();
windowService->addWidgetWorkspace(title, new AbstractWidget(askPage));
// auto askPage = new AskPage();
// windowService->addWidgetWorkspace(title, new AbstractWidget(askPage));

auto codeGeeXWidget = new CodeGeeXWidget();
windowService->addWidgetWorkspace(title, new AbstractWidget(codeGeeXWidget));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/codegeex/codegeex/askapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,18 @@ void AskApi::processResponse(QNetworkReply *reply)
QStringList lines = replyMsg.split('\n');
QByteArray data;
QString event;
QString id;
for (const auto& line : lines) {
if (line.startsWith("event:add")) {
event = "add";
} else if (line.startsWith("event:finish")) {
event = "finish";
} else if (line.startsWith("id:")) {
continue;
id = line.mid(3);
} else if (line.startsWith("data:")) {
data += line.mid(5) + '\n';
} else if (line == "") {
emit response(data, event);
emit response(id, data, event);
data.clear();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codegeex/codegeex/askapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AskApi : public QObject

signals:
void loginState(LoginState loginState);
void response(const QString &response, const QString &event);
void response(const QString &msgID, const QString &response, const QString &event);
void getSessionListResult(const QVector<SessionRecord> &records);
void getChatRecordResult(const QVector<ChatRecord> &record);
void sessionDeleted(const QStringList &talkId, bool isSuccessful);
Expand Down
158 changes: 158 additions & 0 deletions src/plugins/codegeex/codegeexmanager.cpp
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("-", "");;
}
57 changes: 57 additions & 0 deletions src/plugins/codegeex/codegeexmanager.h
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
60 changes: 60 additions & 0 deletions src/plugins/codegeex/data/messagedata.cpp
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;
}
Loading

0 comments on commit d3a5258

Please sign in to comment.