Skip to content

Commit

Permalink
feat: add session features
Browse files Browse the repository at this point in the history
could add session、delete session、query history

Log: new session features
Change-Id: Ib63786b70ad4935fa0c3a35de2ff215a0b1009b3
  • Loading branch information
deepin-mozart committed Nov 9, 2023
1 parent 1d9371b commit 6303de1
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 23 deletions.
58 changes: 56 additions & 2 deletions src/plugins/codegeex/askpage/askpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
#include <QTimer>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>

// TODO(mozart):get those variable from config pane.
static const char *kUrlSSEChat = "https://codegeex.cn/prod/code/chatGlmSse/chat";
static const char *kUrlNewSession = "https://codegeex.cn/prod/code/chatGlmTalk/insert";
static const char *kUrlQuerySession = "https://codegeex.cn/prod/code/chatGmlMsg/selectList";
static const char *kUrlDeleteSession = "https://codegeex.cn/prod/code/chatGlmTalk/delete";

using namespace CodeGeeX;
AskPage::AskPage(QWidget *parent) : QWidget(parent)
Expand Down Expand Up @@ -63,6 +66,55 @@ AskPage::AskPage(QWidget *parent) : QWidget(parent)
// scroll to bottom.
outputpage->verticalScrollBar()->setValue(outputpage->verticalScrollBar()->maximum());
});

connect(&askApi, &AskApi::getSessionListResult, [this](const QVector<AskApi::SessionRecord> &records){
// display these session information.
if (records.isEmpty())
return;

for (auto record : records) {
outputpage->appendPlainText(record.talkId);
outputpage->appendPlainText(record.prompt);
outputpage->appendPlainText(record.createdTime);
outputpage->appendPlainText("----------------------------------");
}

askApi.getChatRecordByTalkId(kUrlQuerySession, sessionId, records.first().talkId, 1, 10);
});

connect(&askApi, &AskApi::getChatRecordResult, [this](const QVector<AskApi::ChatRecord> &records){
// display these session information.
for (auto record : records) {
outputpage->appendPlainText("**********************************");
outputpage->appendPlainText(record.talkId);
outputpage->appendPlainText(record.prompt);
outputpage->appendPlainText(record.outputText);
}
});

connect(&askApi, &AskApi::sessionCreated, [this](const QString &talkId, bool success){
QString displayText;
if (success) {
displayText = QString(talkId + " created success.");
} else {
displayText = QString(talkId + " created failed.");
}
outputpage->setPlainText(displayText);
});

connect(&askApi, &AskApi::sessionDeleted, [this](const QStringList &talkIds, bool success){
QString displayText;
for (auto talkId : talkIds) {
displayText.append(talkId);
displayText.append("\n");
}
if (success) {
displayText.append(" success!");
} else {
displayText.append(" failed!");
}
outputpage->setPlainText(displayText);
});
}


Expand All @@ -85,12 +137,14 @@ void AskPage::on_btnLogin_clicked()

void AskPage::on_btnDelete_clicked()
{

QStringList talkIds;
// TODO(Liu):get talks id.
askApi.deleteSessions(kUrlDeleteSession, sessionId, talkIds);
}

void AskPage::on_btnHistory_clicked()
{

askApi.getSessionList(kUrlQuerySession, sessionId, 1, 10);
}

void AskPage::on_btnNewSession_clicked()
Expand Down
140 changes: 121 additions & 19 deletions src/plugins/codegeex/codegeex/askapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace CodeGeeX {

static int kCode_Success = 200;

AskApi::AskApi(QObject *parent) :
QObject(parent),
manager(new QNetworkAccessManager(this))
Expand All @@ -34,24 +36,18 @@ void AskApi::sendQueryRequest(const QString &codeToken)
{
QString url = "https://codegeex.cn/prod/code/oauth/getUserInfo";

QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setRawHeader("code-token", codeToken.toUtf8());

QNetworkReply *reply = manager->get(request);
QNetworkReply *reply = getMessage(url, codeToken);
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error()) {
qCritical() << "Error:" << reply->errorString();
return;
}
QJsonObject jsonObject = toJsonOBject(reply);
int code = jsonObject["code"].toInt();
if (code == kCode_Success) {
emit loginState(kLoginSuccess);
} else {
QString response = QString::fromUtf8(reply->readAll());
QJsonDocument document = QJsonDocument::fromJson(response.toUtf8());
QJsonObject jsonObject = document.object();
int code = jsonObject["code"].toInt();
if (code == 401) {
emit loginState(kLoginFailed);
} else if (code == 200) {
emit loginState(kLoginSuccess);
}
emit loginState(kLoginFailed);
}
});
}
Expand Down Expand Up @@ -89,11 +85,89 @@ void AskApi::postSSEChat(const QString &url,
void AskApi::postNewSession(const QString &url,
const QString &token,
const QString &prompt,
const QString &taskId)
const QString &talkId)
{
QByteArray body = assembleNewSessionBody(prompt, taskId);
QByteArray body = assembleNewSessionBody(prompt, talkId);
QNetworkReply *reply = postMessage(url, token, body);
processResponse(reply);
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error()) {
qCritical() << "Error:" << reply->errorString();
return;
}
QJsonObject jsonObject = toJsonOBject(reply);
int code = jsonObject["code"].toInt();

emit sessionCreated(talkId, code == kCode_Success);
});
}

void AskApi::getSessionList(const QString &url, const QString &token, int pageNumber, int pageSize)
{
QString urlWithParameter = QString(url + "?pageNum=%1&pageSize=%2").arg(pageNumber).arg(pageSize);
QNetworkReply *reply = getMessage(urlWithParameter, token);
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error()) {
qCritical() << "Error:" << reply->errorString();
return;
}
QJsonObject jsonObject = toJsonOBject(reply);
int code = jsonObject["code"].toInt();
if (code == kCode_Success) {
QJsonArray listArray = jsonObject.value("data").toObject().value("list").toArray();
QVector<SessionRecord> records;
for (int i = 0; i < listArray.size(); ++i) {
SessionRecord record;
QJsonObject item = listArray[i].toObject();
record.talkId = item.value("talkId").toString();
record.createdTime = item.value("createTime").toString();
record.prompt = item.value("prompt").toString();

records.append(record);
}
emit getSessionListResult(records);
}
});
}

void AskApi::getChatRecordByTalkId(const QString &url, const QString &token, const QString &talkId, int pageNumber, int pageSize)
{
QString urlWithParameter = QString(url + "?pageNum=%1&pageSize=%2&talkId=%3").arg(pageNumber).arg(pageSize).arg(talkId);
QNetworkReply *reply = getMessage(urlWithParameter, token);
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error()) {
qCritical() << "Error:" << reply->errorString();
return;
}
QJsonObject jsonObject = toJsonOBject(reply);
int code = jsonObject["code"].toInt();
if (code == kCode_Success) {
QJsonArray listArray = jsonObject.value("data").toObject().value("list").toArray();
QVector<ChatRecord> records;
for (int i = 0; i < listArray.size(); ++i) {
ChatRecord record;
QJsonObject item = listArray[i].toObject();
record.prompt = item.value("prompt").toString();
record.outputText = item.value("outputText").toString();
records.push_back(record);
}
emit getChatRecordResult(records);
}
});
}

void AskApi::deleteSessions(const QString &url, const QString &token, const QStringList &talkIds)
{
QByteArray body = assembleDelSessionBody(talkIds);
QNetworkReply *reply = postMessage(url, token, body);
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error()) {
qCritical() << "Error:" << reply->errorString();
return;
}
QJsonObject jsonObject = toJsonOBject(reply);
int code = jsonObject["code"].toInt();
emit sessionDeleted(talkIds, code == kCode_Success);
});
}

QNetworkReply *AskApi::postMessage(const QString &url, const QString &token, const QByteArray &body)
Expand All @@ -105,6 +179,15 @@ QNetworkReply *AskApi::postMessage(const QString &url, const QString &token, con
return manager->post(request, body);
}

QNetworkReply *AskApi::getMessage(const QString &url, const QString &token)
{
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setRawHeader("code-token", token.toUtf8());

return manager->get(request);
}

void AskApi::processResponse(QNetworkReply *reply)
{
connect(reply, &QNetworkReply::readyRead, [=]() {
Expand Down Expand Up @@ -146,11 +229,23 @@ QByteArray AskApi::assembleSSEChatBody(const QString &prompt,
}

QByteArray AskApi::assembleNewSessionBody(const QString &prompt,
const QString &taskId)
const QString &talkId)
{
QJsonObject jsonObject;
jsonObject.insert("prompt", prompt);
jsonObject.insert("taskid", taskId);
jsonObject.insert("talkId", talkId);

return jsonToByteArray(jsonObject);
}

QByteArray AskApi::assembleDelSessionBody(const QStringList &talkIds)
{
QJsonObject jsonObject;
QJsonArray array;
for (auto talkId : talkIds) {
array.push_back(talkId);
}
jsonObject.insert("talkId", array);

return jsonToByteArray(jsonObject);
}
Expand All @@ -160,4 +255,11 @@ QByteArray AskApi::jsonToByteArray(const QJsonObject &jsonObject)
QJsonDocument doc(jsonObject);
return doc.toJson();
}

QJsonObject AskApi::toJsonOBject(QNetworkReply *reply)
{
QString response = QString::fromUtf8(reply->readAll());
QJsonDocument document = QJsonDocument::fromJson(response.toUtf8());
return document.object();
}
} // end namespace
39 changes: 37 additions & 2 deletions src/plugins/codegeex/codegeex/askapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,36 @@ class AskApi : public QObject
const QString &url,
const QString &token,
const QString &prompt,
const QString &taskId
const QString &talkId
);

struct SessionRecord
{
QString prompt;
QString talkId;
QString createdTime;
};
void getSessionList(const QString &url,
const QString &token,
int pageNumber = 1,
int pageSize = 10);

struct ChatRecord
{
QString prompt;
QString outputText;
QString talkId;
};
void getChatRecordByTalkId(const QString &url,
const QString &token,
const QString &talkId,
int pageNumber = 1,
int pageSize = 10);

void deleteSessions(const QString &url,
const QString &token,
const QStringList &talkIds);

enum LoginState
{
kLoginFailed,
Expand All @@ -45,22 +72,30 @@ class AskApi : public QObject
signals:
void loginState(LoginState loginState);
void response(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);
void sessionCreated(const QString &talkId, bool isSuccessful);

public slots:

private:
QNetworkReply *postMessage(const QString &url, const QString &token, const QByteArray &body);
QNetworkReply *getMessage(const QString &url, const QString &token);
void processResponse(QNetworkReply *reply);

QByteArray assembleSSEChatBody(const QString &prompt,
const QString &machineId,
const QJsonArray &history);

QByteArray assembleNewSessionBody(const QString &prompt,
const QString &taskId);
const QString &talkId);

QByteArray assembleDelSessionBody(const QStringList &talkIds);

QByteArray jsonToByteArray(const QJsonObject &jsonObject);
QJsonObject toJsonOBject(QNetworkReply *reply);


QNetworkAccessManager *manager = nullptr;
};
Expand Down

0 comments on commit 6303de1

Please sign in to comment.