Skip to content

Commit

Permalink
feat: [codegeex] modified ui of codegeex
Browse files Browse the repository at this point in the history
Log: as title
  • Loading branch information
LiHua000 authored and deepin-mozart committed Oct 31, 2024
1 parent cefd736 commit c826c56
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 309 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/plugins/codegeex/codegeex.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<file>builtin/texts/codegeex_edit_32px.svg</file>
<file>builtin/texts/codegeex_internet_16px.svg</file>
<file>builtin/texts/codegeex_files_16px.svg</file>
<file>builtin/texts/uc_codegeex_project_chat_16px.svg</file>
<file>builtin/texts/uc_codegeex_accept_128px.svg</file>
<file>builtin/texts/uc_codegeex_quickquestion_128px.svg</file>
<file>builtin/texts/uc_codegeex_reject_128px.svg</file>
Expand Down
40 changes: 0 additions & 40 deletions src/plugins/codegeex/codegeex/copilotapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ void CopilotApi::postGenerate(const QString &url, const QString &prefix, const Q
});
}

void CopilotApi::postTranslate(const QString &url,
const QString &code,
const QString &dst_lang,
const QString &locale)
{
QByteArray body = assembleTranslateBody(code, dst_lang, locale);
QNetworkReply *reply = postMessage(url, CodeGeeXManager::instance()->getSessionId(), body);
reply->setProperty("responseType", CopilotApi::multilingual_code_translate);
processResponse(reply);
}

void CopilotApi::postComment(const QString &url,
const QString &code,
const QString &locale)
Expand Down Expand Up @@ -175,21 +164,6 @@ QByteArray CopilotApi::assembleGenerateBody(const QString &prefix, const QString
return doc.toJson();
}

QByteArray CopilotApi::assembleTranslateBody(const QString &code, const QString &dst_lang, const QString &locale)
{
QJsonObject json;
json.insert("ide", qApp->applicationName());
json.insert("ide_version", version());
json.insert("lang", dst_lang);
json.insert("code", code);
json.insert("command", "translation");
json.insert("locale", locale);
json.insert("model", chatModel);

QJsonDocument doc(json);
return doc.toJson();
}

QByteArray CopilotApi::assembleInlineChatBody(const QString &prompt, const InlineChatInfo &info, const QString &locale)
{
auto file = getCurrentFileInfo();
Expand Down Expand Up @@ -307,20 +281,6 @@ void CopilotApi::slotReadReply(QNetworkReply *reply)
if (code.split('\n', QString::SkipEmptyParts).isEmpty())
return;
emit response(CopilotApi::inline_completions, code, "");
} else if (type == CopilotApi::multilingual_code_translate) {
auto codeLines = jsonObject.value("text").toString().split('\n');
QString lang = codeLines.first();

if (lang.startsWith("```"))
lang = lang.mid(3);
codeLines.removeFirst();
while (codeLines.last() == "")
codeLines.removeLast();
if (codeLines.last() == "```")
codeLines.removeLast(); //remove ```lang ```

code = codeLines.join('\n');
emit response(CopilotApi::multilingual_code_translate, code, lang);
} else if (type == CopilotApi::multilingual_code_comment) {
code = jsonObject.value("text").toString();
emit response(CopilotApi::multilingual_code_comment, code, "");
Expand Down
10 changes: 0 additions & 10 deletions src/plugins/codegeex/codegeex/copilotapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,6 @@ class CopilotApi : public QObject
const InlineChatInfo &info,
const QString &locale); // codegeex: this api is non-streaming. url need to be xxx/?stream=false

void postTranslate(const QString &url,
const QString &code,
const QString &dst_lang,
const QString &locale);

void postCommit(const QString &url,
const CommitMessage &message,
const QString &locale);
Expand All @@ -194,7 +189,6 @@ class CopilotApi : public QObject
inline_completions,
inline_chat,
multilingual_code_comment,
multilingual_code_translate,
receiving_by_stream
};

Expand All @@ -218,10 +212,6 @@ public slots:
const QString &suffix,
GenerateType type);

QByteArray assembleTranslateBody(const QString &code,
const QString &dst_lang,
const QString &locale);

QByteArray assembleInlineChatBody(const QString &prompt,
const InlineChatInfo &info,
const QString &locale);
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/codegeex/codegeexmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,6 @@ void CodeGeeXManager::recevieLoginState(AskApi::LoginState loginState)
}
}

void CodeGeeXManager::recevieToTranslate(const QString &codeText)
{
if (isLogin && !codeText.isEmpty())
Q_EMIT requestToTransCode(codeText);
}

void CodeGeeXManager::recevieSessionRecords(const QVector<AskApi::SessionRecord> &records)
{
sessionRecordList.clear();
Expand Down Expand Up @@ -352,7 +346,6 @@ void CodeGeeXManager::initConnections()
connect(&askApi, &AskApi::getMessageListResult, this, &CodeGeeXManager::showHistoryMessage);
connect(&askApi, &AskApi::noChunksFounded, this, &CodeGeeXManager::showIndexingWidget);

connect(Copilot::instance(), &Copilot::translatingText, this, &CodeGeeXManager::recevieToTranslate);
connect(Copilot::instance(), &Copilot::response, this, &CodeGeeXManager::onResponse);
connect(Copilot::instance(), &Copilot::messageSended, this, &CodeGeeXManager::startReceiving);

Expand Down
2 changes: 0 additions & 2 deletions src/plugins/codegeex/codegeexmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class CodeGeeXManager : public QObject
void logoutSuccessed();
void createdNewSession();
void requestMessageUpdate(const MessageData &msg);
void requestToTransCode(const QString &code);
void chatStarted();
void crawledWebsite(const QString &msgID, const QList<CodeGeeX::websiteReference> &websites);
void searching(const QString &searchText);
Expand All @@ -124,7 +123,6 @@ public Q_SLOTS:
void onSessionCreated(const QString &talkId, bool isSuccessful);
void onResponse(const QString &msgID, const QString &data, const QString &event);
void recevieLoginState(CodeGeeX::AskApi::LoginState loginState);
void recevieToTranslate(const QString &codeText);
void recevieSessionRecords(const QVector<CodeGeeX::AskApi::SessionRecord> &records);
void recevieDeleteResult(const QStringList &talkIds, bool success);
void showHistoryMessage(const QVector<CodeGeeX::AskApi::MessageRecord> &records);
Expand Down
18 changes: 0 additions & 18 deletions src/plugins/codegeex/copilot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ Copilot::Copilot(QObject *parent)
emit completionProvider->finished();
}
break;
case CopilotApi::multilingual_code_translate:
emit translatedResult(response, dstLang);
break;
default:;
}
});
Expand Down Expand Up @@ -114,23 +111,20 @@ QMenu *Copilot::getMenu()
menu->setTitle("CodeGeeX");

QAction *addComment = new QAction(tr("Add Comment"));
QAction *translate = new QAction(tr("Translate"));
QAction *fixBug = new QAction(tr("Fix Bug"));
QAction *explain = new QAction(tr("Explain Code"));
QAction *review = new QAction(tr("Review Code"));
QAction *tests = new QAction(tr("Generate Unit Tests"));
QAction *commits = new QAction(tr("Generate git commits"));

menu->addAction(addComment);
menu->addAction(translate);
menu->addAction(fixBug);
menu->addAction(explain);
menu->addAction(review);
menu->addAction(tests);
menu->addAction(commits);

connect(addComment, &QAction::triggered, this, &Copilot::addComment);
connect(translate, &QAction::triggered, this, &Copilot::translate);
connect(fixBug, &QAction::triggered, this, &Copilot::fixBug);
connect(explain, &QAction::triggered, this, &Copilot::explain);
connect(review, &QAction::triggered, this, &Copilot::review);
Expand All @@ -140,12 +134,6 @@ QMenu *Copilot::getMenu()
return menu;
}

void Copilot::translateCode(const QString &code, const QString &dstLanguage)
{
QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once
copilotApi.postTranslate(url, code, dstLanguage, locale);
}

void Copilot::replaceSelectedText(const QString &text)
{
if (editorService->replaceSelectedText)
Expand Down Expand Up @@ -248,12 +236,6 @@ void Copilot::login()
{
}

void Copilot::translate()
{
emit translatingText(selectedText());
switchToCodegeexPage();
}

void Copilot::fixBug()
{
QString url = QString(kUrlSSEChat) + "?stream=true";
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/codegeex/copilot.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Copilot : public QObject
static Copilot *instance();
QMenu *getMenu();

void translateCode(const QString &code, const QString &dstLanguage);
void replaceSelectedText(const QString &text);
void insterText(const QString &text);
void setGenerateCodeEnabled(bool enabled);
Expand All @@ -42,11 +41,6 @@ class Copilot : public QObject
void handleInlineWidgetClosed();

signals:
// the code will be tranlated.
void translatingText(const QString &text);
// the result has been tranlated.
void translatedResult(const QString &result, const QString &dstLang);

void response(const QString &msgID, const QString &response, const QString &event);
void messageSended();
void requestStop();
Expand All @@ -55,7 +49,6 @@ public slots:
void addComment();
void generateCode();
void login();
void translate();
void fixBug();
void explain();
void review();
Expand Down
56 changes: 1 addition & 55 deletions src/plugins/codegeex/widgets/codegeexwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "codegeexwidget.h"
#include "askpagewidget.h"
#include "historylistwidget.h"
#include "translationpagewidget.h"
#include "codegeexmanager.h"
#include "copilot.h"

Expand Down Expand Up @@ -64,26 +63,6 @@ void CodeGeeXWidget::onNewSessionCreated()
askPage->setIntroPage();
}

void CodeGeeXWidget::toTranslateCode(const QString &code)
{
switchPage(pageState::TrasnlatePage);
transPage->setInputEditText(code);
transPage->cleanOutputEdit();
}

void CodeGeeXWidget::switchPage(pageState state)
{
if (state == pageState::TrasnlatePage) {
tabBar->buttonList().at(0)->setChecked(false);
tabBar->buttonList().at(1)->setChecked(true);
stackWidget->setCurrentWidget(transPage);
} else {
tabBar->buttonList().at(0)->setChecked(true);
tabBar->buttonList().at(1)->setChecked(false);
stackWidget->setCurrentWidget(askPage);
}
}

void CodeGeeXWidget::onCloseHistoryWidget()
{
historyWidgetAnimation->setStartValue(QRect(0, 0, historyWidget->width(), historyWidget->height()));
Expand Down Expand Up @@ -185,23 +164,10 @@ void CodeGeeXWidget::initConnection()
connect(CodeGeeXManager::instance(), &CodeGeeXManager::loginSuccessed, this, &CodeGeeXWidget::onLoginSuccessed);
connect(CodeGeeXManager::instance(), &CodeGeeXManager::logoutSuccessed, this, &CodeGeeXWidget::onLogOut);
connect(CodeGeeXManager::instance(), &CodeGeeXManager::createdNewSession, this, &CodeGeeXWidget::onNewSessionCreated);
connect(CodeGeeXManager::instance(), &CodeGeeXManager::requestToTransCode, this, &CodeGeeXWidget::toTranslateCode);
connect(CodeGeeXManager::instance(), &CodeGeeXManager::requestMessageUpdate, this, [=](){switchPage(pageState::AskPage);});
}

void CodeGeeXWidget::initAskWidget()
{
QHBoxLayout *tabLayout = new QHBoxLayout;
tabLayout->setContentsMargins(0, 20, 0, 20);
tabLayout->setAlignment(Qt::AlignHCenter);

//套一层DWidget,用以登出时删除现有窗口。 直接使用layout会删不掉。
DWidget *tabWidget = new DWidget(this);
tabWidget->setLayout(tabLayout);

tabBar = new DButtonBox(this);
tabLayout->addWidget(tabBar, 0);

stackWidget = new QStackedWidget(this);
stackWidget->setContentsMargins(0, 0, 0, 0);
stackWidget->setFrameShape(QFrame::NoFrame);
Expand All @@ -211,10 +177,8 @@ void CodeGeeXWidget::initAskWidget()
mainLayout->setMargin(0);
mainLayout->setSpacing(0);

mainLayout->addWidget(tabWidget, 0);
mainLayout->addWidget(stackWidget, 1);
mainLayout->addWidget(stackWidget);

initTabBar();
initStackWidget();
initAskWidgetConnection();
}
Expand All @@ -232,20 +196,9 @@ void CodeGeeXWidget::initHistoryWidget()
initHistoryWidgetConnection();
}

void CodeGeeXWidget::initTabBar()
{
DButtonBoxButton *askbtn = new DButtonBoxButton(tr("Ask CodeGeeX"), this);
askbtn->setCheckable(true);
askbtn->setChecked(true);
DButtonBoxButton *trsbtn = new DButtonBoxButton(tr("Translation"), this);

tabBar->setButtonList({ askbtn, trsbtn }, true);
}

void CodeGeeXWidget::initStackWidget()
{
askPage = new AskPageWidget(this);
transPage = new TranslationPageWidget(this);

DWidget *creatingSessionWidget = new DWidget(this);
QHBoxLayout *layout = new QHBoxLayout;
Expand All @@ -258,18 +211,11 @@ void CodeGeeXWidget::initStackWidget()

stackWidget->insertWidget(0, creatingSessionWidget);
stackWidget->insertWidget(1, askPage);
stackWidget->insertWidget(2, transPage);
stackWidget->setCurrentIndex(0);
}

void CodeGeeXWidget::initAskWidgetConnection()
{
connect(tabBar, &DButtonBox::buttonClicked, stackWidget, [=](QAbstractButton *button) {
if (button->text() == tr("Ask CodeGeeX"))
stackWidget->setCurrentWidget(askPage);
else
stackWidget->setCurrentWidget(transPage);
});
connect(askPage, &AskPageWidget::requestShowHistoryPage, this, &CodeGeeXWidget::onShowHistoryWidget);
}

Expand Down
12 changes: 0 additions & 12 deletions src/plugins/codegeex/widgets/codegeexwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,16 @@

class AskPageWidget;
class HistoryListWidget;
class TranslationPageWidget;
class CodeGeeXWidget : public DTK_WIDGET_NAMESPACE::DFrame
{
Q_OBJECT

enum pageState : uint8_t {
AskPage,
TrasnlatePage
};

public:
explicit CodeGeeXWidget(QWidget *parent = nullptr);

public Q_SLOTS:
void onLoginSuccessed();
void onLogOut();
void onNewSessionCreated();
void toTranslateCode(const QString &code);
void onCloseHistoryWidget();
void onShowHistoryWidget();

Expand All @@ -40,23 +32,19 @@ public Q_SLOTS:
private:
void initUI();
void initConnection();
void switchPage(pageState state);

void initAskWidget();
void initHistoryWidget();
void initTabBar();
void initStackWidget();
void initAskWidgetConnection();
void initHistoryWidgetConnection();

DTK_WIDGET_NAMESPACE::DButtonBox *tabBar { nullptr };
DTK_WIDGET_NAMESPACE::DStackedWidget *stackWidget { nullptr };

QPropertyAnimation *historyWidgetAnimation { nullptr };

AskPageWidget *askPage { nullptr };
HistoryListWidget *historyWidget { nullptr };
TranslationPageWidget *transPage { nullptr };

bool historyShowed { false };
};
Expand Down
Loading

0 comments on commit c826c56

Please sign in to comment.