Skip to content

Commit

Permalink
feat: add codegeex plugin
Browse files Browse the repository at this point in the history
Log: new plugin
Change-Id: Ieb1d5313a6a8222af35874a91986648e8d189547
  • Loading branch information
deepin-mozart committed Nov 1, 2023
1 parent ab3c543 commit c9ebf1e
Show file tree
Hide file tree
Showing 15 changed files with 939 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ add_subdirectory(actionanalyse)
add_subdirectory(valgrind)
add_subdirectory(binarytools)
add_subdirectory(commandproxy)
add_subdirectory(codegeex)
38 changes: 38 additions & 0 deletions src/plugins/codegeex/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.0.2)

project(plugin-codegeex)

set(CXX_CPP
codegeex.cpp
eventreceiver.cpp
codegeex/askapi.cpp
codegeex/copilot.cpp
askpage/askpage.cpp
codegeex.json
)

set(CXX_H
codegeex.h
eventreceiver.h
codegeex/askapi.h
codegeex/copilot.h
askpage/askpage.h
)

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

target_link_libraries(${PROJECT_NAME}
framework # plug-in framework, must be set
base
services
common
${QtUseModules}
${PkgUserModules}
)

install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${PLUGIN_INSTALL_PATH})
236 changes: 236 additions & 0 deletions src/plugins/codegeex/askpage/askpage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
// SPDX-FileCopyrightText: 2022 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "askpage.h"
#include "common/util/custompaths.h"

#include <QtCore/QVariant>
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QScrollBar>
#include <QUuid>
#include <QSysInfo>
#include <QTimer>
#include <QJsonObject>
#include <QJsonDocument>


// TODO(mozart):get those variable from config pane.
static const char *kApiKey = "f30ea902c3824ee88e221a32363c0823";
static const char *kUrlGenerateOneLine = "https://tianqi.aminer.cn/api/v2/multilingual_code_generate";
static const char *kUrlGenerateMultiLine = "https://tianqi.aminer.cn/api/v2/multilingual_code_generate_adapt";
static const char *kUrlComment = "https://tianqi.aminer.cn/api/v2/multilingual_code_explain";
static const char *kUrlTranslate = "https://tianqi.aminer.cn/api/v2/multilingual_code_translate";
static const char *kUrlBugfix = "https://tianqi.aminer.cn/api/v2/multilingual_code_bugfix";
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;
AskPage::AskPage(QWidget *parent) : QWidget(parent)
, timer(new QTimer(this))
{
setupUi(this);

connect(&askApi, &AskApi::loginState, [this](AskApi::LoginState loginState){
if (loginState == AskApi::LoginState::kLoginFailed) {
loginSuccess = false;
this->label->setText("login failed");
// switch to login ui.
} else if (loginState == AskApi::LoginState::kLoginSuccess) {
loginSuccess = true;
this->label->setText("login success");
// switch to ask page.
}
});

loadConfig();

connect(timer, SIGNAL(timeout()), this, SLOT(queryLoginState()));
timer->start(1000);

connect(&askApi, &AskApi::response, [this](const QString &response, const QString &event){
int responseSize = response.size();
if (responseSize < totalResponseSize || responseSize == 0)
return;

if (event == "finish") {
totalResponseSize = 0;
} else if (event == "add"){
totalResponseSize = responseSize;
}

outputpage->setPlainText(response);

// scroll to bottom.
outputpage->verticalScrollBar()->setValue(outputpage->verticalScrollBar()->maximum());
});
}


QString uuid()
{
QUuid uuid = QUuid::createUuid();
return uuid.toString().replace("{", "").replace("}", "").replace("-", "");;
}

void AskPage::on_btnLogin_clicked()
{
if (sessionId.isEmpty() || userId.isEmpty()) {
sessionId = uuid();
userId = uuid();
saveConfig(sessionId, userId);
}
QString machineId = QSysInfo::machineUniqueId();
askApi.sendLoginRequest(sessionId, machineId, userId);
}

void AskPage::on_btnDelete_clicked()
{

}

void AskPage::on_btnHistory_clicked()
{

}

void AskPage::on_btnNewSession_clicked()
{
QString prompt("title");
QString taskid(uuid());
askApi.postNewSession(kUrlNewSession, sessionId, prompt, taskid);
}

void AskPage::on_btnSend_clicked()
{
QString prompt = lineEditInput->text();
QMultiMap<QString, QString> history;
QString machineId = QSysInfo::machineUniqueId();
askApi.postSSEChat(kUrlSSEChat, sessionId, prompt, machineId, history);
}

void AskPage::setupUi(QWidget *AskPage)
{
if (AskPage->objectName().isEmpty())
AskPage->setObjectName(QStringLiteral("AskPage"));
AskPage->resize(400, 300);
verticalLayout = new QVBoxLayout(AskPage);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
toolLayout = new QHBoxLayout();
toolLayout->setSpacing(6);
toolLayout->setObjectName(QStringLiteral("toolLayout"));
btnLogin = new QPushButton(AskPage);
btnLogin->setObjectName(QStringLiteral("btnLogin"));

toolLayout->addWidget(btnLogin);

btnDelete = new QPushButton(AskPage);
btnDelete->setObjectName(QStringLiteral("btnDelete"));

toolLayout->addWidget(btnDelete);

btnHistory = new QPushButton(AskPage);
btnHistory->setObjectName(QStringLiteral("btnHistory"));

toolLayout->addWidget(btnHistory);

btnNewSession = new QPushButton(AskPage);
btnNewSession->setObjectName(QStringLiteral("btnNewSession"));

toolLayout->addWidget(btnNewSession);

verticalLayout->addLayout(toolLayout);

label = new QLabel(AskPage);
label->setObjectName(QStringLiteral("label"));

verticalLayout->addWidget(label);

outputpage = new QPlainTextEdit(AskPage);
outputpage->setObjectName(QStringLiteral("outputpage"));

verticalLayout->addWidget(outputpage);

inputLayout = new QHBoxLayout();
inputLayout->setSpacing(6);
inputLayout->setObjectName(QStringLiteral("inputLayout"));
lineEditInput = new QLineEdit(AskPage);
lineEditInput->setObjectName(QStringLiteral("lineEditInput"));

QObject::connect(lineEditInput, &QLineEdit::returnPressed, [=]() {
on_btnSend_clicked();
lineEditInput->clear();
});

inputLayout->addWidget(lineEditInput);

btnSend = new QPushButton(AskPage);
btnSend->setObjectName(QStringLiteral("btnSend"));

inputLayout->addWidget(btnSend);

verticalLayout->addLayout(inputLayout);

AskPage->setWindowTitle(QApplication::translate("AskPage", "AskPage", nullptr));
btnLogin->setText(QApplication::translate("AskPage", "login", nullptr));
btnDelete->setText(QApplication::translate("AskPage", "delete", nullptr));
btnHistory->setText(QApplication::translate("AskPage", "history", nullptr));
btnNewSession->setText(QApplication::translate("AskPage", "new session", nullptr));
label->setText(QApplication::translate("AskPage", "TextLabel", nullptr));
btnSend->setText(QApplication::translate("AskPage", "Send", nullptr));

QMetaObject::connectSlotsByName(AskPage);
}

void AskPage::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 AskPage::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();
}
}

QString AskPage::configFilePath() const
{
return CustomPaths::user(CustomPaths::Configures) + "/codegeexcfg.json";
}

void AskPage::queryLoginState()
{
if (!sessionId.isEmpty()) {
askApi.sendQueryRequest(sessionId);
}
}
65 changes: 65 additions & 0 deletions src/plugins/codegeex/askpage/askpage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2022 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef ASKPAGE_H
#define ASKPAGE_H

#include "codegeex/askapi.h"

#include <QWidget>

class QVBoxLayout;
class QHBoxLayout;
class QPushButton;
class QLabel;
class QPlainTextEdit;
class QLineEdit;
class QTimer;
class AskPage : public QWidget
{
Q_OBJECT
public:
explicit AskPage(QWidget *parent = nullptr);

signals:

private slots:
void on_btnLogin_clicked();

void on_btnDelete_clicked();

void on_btnHistory_clicked();

void on_btnNewSession_clicked();

void on_btnSend_clicked();

void queryLoginState();

private:
void setupUi(QWidget *AskPage);
void saveConfig(const QString &sessionId, const QString &userId);
void loadConfig();
QString configFilePath() const;

QVBoxLayout *verticalLayout = nullptr;
QHBoxLayout *toolLayout = nullptr;
QPushButton *btnLogin = nullptr;
QPushButton *btnDelete = nullptr;
QPushButton *btnHistory = nullptr;
QPushButton *btnNewSession = nullptr;
QLabel *label = nullptr;
QPlainTextEdit *outputpage = nullptr;
QHBoxLayout *inputLayout = nullptr;
QLineEdit *lineEditInput = nullptr;
QPushButton *btnSend = nullptr;

CodeGeeX::AskApi askApi;
QString sessionId;
QString userId;
QTimer *timer = nullptr;
bool loginSuccess = false;
int totalResponseSize = 0;
};

#endif // ASKPAGE_H
43 changes: 43 additions & 0 deletions src/plugins/codegeex/codegeex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "codegeex.h"
#include "askpage/askpage.h"

#include "common/common.h"
#include "services/window/windowservice.h"

#include "base/abstractwidget.h"
#include "base/abstractaction.h"
#include "base/abstractcentral.h"
#include "base/abstractmenu.h"

#include <QAction>
#include <QIcon>

void CodeGeex::initialize()
{
}

bool CodeGeex::start()
{
auto windowService = dpfGetService(dpfservice::WindowService);

if (windowService) {
QString title = "CodeGeex";

// Add widget to left bar
if (windowService->addCentralNavigation) {
auto askPage = new AskPage();
windowService->addActionNavigation(title, new AbstractAction(new QAction(QIcon(":/CodeGeex/images/navigation.png"), QAction::tr("CodeGeex"))));
windowService->addCentralNavigation(title, new AbstractCentral(askPage));
}
}
return true;
}

dpf::Plugin::ShutdownFlag CodeGeex::stop()
{
return Sync;
}
Loading

0 comments on commit c9ebf1e

Please sign in to comment.