diff --git a/src/plugins/codegeex/codegeex/copilotapi.cpp b/src/plugins/codegeex/codegeex/copilotapi.cpp index 032fbddce..2ab413fed 100644 --- a/src/plugins/codegeex/codegeex/copilotapi.cpp +++ b/src/plugins/codegeex/codegeex/copilotapi.cpp @@ -26,8 +26,12 @@ CopilotApi::CopilotApi(QObject *parent) void CopilotApi::postGenerate(const QString &url, const QString &code, const QString &suffix) { + if (completionReply) + completionReply->close(); + QByteArray body = assembleGenerateBody(code, suffix); QNetworkReply *reply = postMessage(url, CodeGeeXManager::instance()->getSessionId(), body); + completionReply = reply; reply->setProperty("responseType", CopilotApi::inline_completions); processResponse(reply); } @@ -116,7 +120,7 @@ QByteArray CopilotApi::assembleGenerateBody(const QString &prefix, const QString json.insert("context", context); json.insert("model", completionModel); json.insert("lang", file.second); - json.insert("max_new_tokens", 64); + json.insert("max_new_tokens", 128); QJsonDocument doc(json); return doc.toJson(); @@ -185,6 +189,10 @@ void CopilotApi::slotReadReply(QNetworkReply *reply) if (type == CopilotApi::inline_completions) { auto content = jsonObject.value("inline_completions").toArray().at(0).toObject(); code = content.value("text").toString(); + // Cut the first code segment + auto codeLines = code.split('\n'); + code = codeLines.mid(0, codeLines.indexOf("", 1)).join('\n') + '\n'; + completionReply = nullptr; emit response(CopilotApi::inline_completions, code, ""); } else if (type == CopilotApi::multilingual_code_translate) { auto codeLines = jsonObject.value("text").toString().split('\n'); diff --git a/src/plugins/codegeex/codegeex/copilotapi.h b/src/plugins/codegeex/codegeex/copilotapi.h index 40e69571d..60acbb27c 100644 --- a/src/plugins/codegeex/codegeex/copilotapi.h +++ b/src/plugins/codegeex/codegeex/copilotapi.h @@ -136,8 +136,7 @@ class CopilotApi : public QObject const QString &locale, const QString &command); - enum ResponseType - { + enum ResponseType { inline_completions, multilingual_code_comment, multilingual_code_translate, @@ -158,15 +157,15 @@ public slots: QNetworkReply *postMessage(const QString &url, const QString &token, const QByteArray &body); QByteArray assembleGenerateBody(const QString &prefix, - const QString &suffix); + const QString &suffix); QByteArray assembleTranslateBody(const QString &code, - const QString &dst_lang, - const QString &locale); + const QString &dst_lang, + const QString &locale); QByteArray assembleCommandBody(const QString &code, - const QString &locale, - const QString &command); + const QString &locale, + const QString &command); void processResponse(QNetworkReply *reply); QPair getCurrentFileInfo(); @@ -174,9 +173,11 @@ public slots: QNetworkAccessManager *manager = nullptr; QString chatModel = chatModelLite; QString completionModel = completionModelLite; + + QNetworkReply *completionReply = nullptr; }; -}// end namespace +} // end namespace Q_DECLARE_METATYPE(CodeGeeX::CopilotApi::ResponseType) -#endif // COPILOT_H +#endif // COPILOT_H diff --git a/src/plugins/codegeex/copilot.cpp b/src/plugins/codegeex/copilot.cpp index c53af6340..067f2cf4e 100644 --- a/src/plugins/codegeex/copilot.cpp +++ b/src/plugins/codegeex/copilot.cpp @@ -52,12 +52,6 @@ Copilot::Copilot(QObject *parent) connect(&copilotApi, &CopilotApi::responseByStream, this, &Copilot::response); connect(&copilotApi, &CopilotApi::messageSended, this, &Copilot::messageSended); - - timer.setSingleShot(true); - - connect(&timer, &QTimer::timeout, [this]() { - generateCode(); - }); } QString Copilot::selectedText() const @@ -119,7 +113,7 @@ QMenu *Copilot::getMenu() void Copilot::translateCode(const QString &code, const QString &dstLanguage) { - QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once + QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once copilotApi.postTranslate(url, code, dstLanguage, locale); } @@ -159,13 +153,13 @@ void Copilot::handleTextChanged() { // start generate code. QMetaObject::invokeMethod(this, [this]() { - timer.start(200); + generateCode(); }); } void Copilot::addComment() { - QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once + QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once copilotApi.postComment(url, selectedText(), locale); @@ -237,7 +231,7 @@ void Copilot::commits() auto workingDirectory = prjInfo.workspaceFolder(); process.setWorkingDirectory(workingDirectory); - connect(&process, QOverload::of(&QProcess::finished), this, [this, &process](int exitCode, QProcess::ExitStatus exitStatus){ + connect(&process, QOverload::of(&QProcess::finished), this, [this, &process](int exitCode, QProcess::ExitStatus exitStatus) { Q_UNUSED(exitStatus) if (exitCode != 0) diff --git a/src/plugins/codegeex/copilot.h b/src/plugins/codegeex/copilot.h index 3fa6a3353..632f908ae 100644 --- a/src/plugins/codegeex/copilot.h +++ b/src/plugins/codegeex/copilot.h @@ -63,7 +63,6 @@ public slots: CodeGeeX::CopilotApi copilotApi; dpfservice::EditorService *editorService = nullptr; QString generateResponse; - QTimer timer; QMutex mutexResponse; bool generateCodeEnabled = true; };