Add sharing current file with model

This commit is contained in:
Petr Mironychev
2024-11-11 00:38:55 +01:00
parent bc93bce03b
commit 61f1f0ae4f
9 changed files with 89 additions and 11 deletions

View File

@ -22,6 +22,7 @@
#include <utils/theme/theme.h>
#include <utils/utilsicons.h>
#include "ChatAssistantSettings.hpp"
#include "GeneralSettings.hpp"
namespace QodeAssist::Chat {
@ -37,6 +38,12 @@ ChatRootView::ChatRootView(QQuickItem *parent)
&Utils::BaseAspect::changed,
this,
&ChatRootView::currentTemplateChanged);
connect(&Settings::chatAssistantSettings().sharingCurrentFile,
&Utils::BaseAspect::changed,
this,
&ChatRootView::isSharingCurrentFileChanged);
generateColors();
}
@ -50,9 +57,9 @@ QColor ChatRootView::backgroundColor() const
return Utils::creatorColor(Utils::Theme::BackgroundColorNormal);
}
void ChatRootView::sendMessage(const QString &message) const
void ChatRootView::sendMessage(const QString &message, bool sharingCurrentFile) const
{
m_clientInterface->sendMessage(message);
m_clientInterface->sendMessage(message, sharingCurrentFile);
}
void ChatRootView::copyToClipboard(const QString &text)
@ -129,4 +136,9 @@ QColor ChatRootView::codeColor() const
return m_codeColor;
}
bool ChatRootView::isSharingCurrentFile() const
{
return Settings::chatAssistantSettings().sharingCurrentFile();
}
} // namespace QodeAssist::Chat

View File

@ -35,6 +35,8 @@ class ChatRootView : public QQuickItem
Q_PROPERTY(QColor primaryColor READ primaryColor CONSTANT FINAL)
Q_PROPERTY(QColor secondaryColor READ secondaryColor CONSTANT FINAL)
Q_PROPERTY(QColor codeColor READ codeColor CONSTANT FINAL)
Q_PROPERTY(bool isSharingCurrentFile READ isSharingCurrentFile NOTIFY
isSharingCurrentFileChanged FINAL)
QML_ELEMENT
public:
@ -49,8 +51,10 @@ public:
QColor codeColor() const;
bool isSharingCurrentFile() const;
public slots:
void sendMessage(const QString &message) const;
void sendMessage(const QString &message, bool sharingCurrentFile = false) const;
void copyToClipboard(const QString &text);
void cancelRequest();
@ -58,6 +62,8 @@ signals:
void chatModelChanged();
void currentTemplateChanged();
void isSharingCurrentFileChanged();
private:
void generateColors();
QColor generateColor(const QColor &baseColor,

View File

@ -19,9 +19,18 @@
#include "ClientInterface.hpp"
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QUuid>
#include <texteditor/textdocument.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/idocument.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
#include "ChatAssistantSettings.hpp"
#include "GeneralSettings.hpp"
@ -55,7 +64,7 @@ ClientInterface::ClientInterface(ChatModel *chatModel, QObject *parent)
ClientInterface::~ClientInterface() = default;
void ClientInterface::sendMessage(const QString &message)
void ClientInterface::sendMessage(const QString &message, bool includeCurrentFile)
{
cancelRequest();
@ -71,8 +80,20 @@ void ClientInterface::sendMessage(const QString &message)
LLMCore::ContextData context;
context.prefix = message;
context.suffix = "";
if (chatAssistantSettings.useSystemPrompt())
context.systemPrompt = chatAssistantSettings.systemPrompt();
QString systemPrompt = chatAssistantSettings.systemPrompt();
if (includeCurrentFile) {
QString fileContext = getCurrentFileContext();
if (!fileContext.isEmpty()) {
context.systemPrompt = QString("%1\n\n%2").arg(systemPrompt, fileContext);
LOG_MESSAGE("Using system prompt with file context");
} else {
context.systemPrompt = systemPrompt;
LOG_MESSAGE("Failed to get file context, using default system prompt");
}
} else {
context.systemPrompt = systemPrompt;
}
QJsonObject providerRequest;
providerRequest["model"] = Settings::generalSettings().caModel();
@ -128,4 +149,28 @@ void ClientInterface::handleLLMResponse(const QString &response,
}
}
QString ClientInterface::getCurrentFileContext() const
{
auto currentEditor = Core::EditorManager::currentEditor();
if (!currentEditor) {
LOG_MESSAGE("No active editor found");
return QString();
}
auto textDocument = qobject_cast<TextEditor::TextDocument *>(currentEditor->document());
if (!textDocument) {
LOG_MESSAGE("Current document is not a text document");
return QString();
}
QString fileInfo = QString("Language: %1\nFile: %2\n\n")
.arg(textDocument->mimeType(), textDocument->filePath().toString());
QString content = textDocument->document()->toPlainText();
LOG_MESSAGE(QString("Got context from file: %1").arg(textDocument->filePath().toString()));
return QString("Current file context:\n%1\nFile content:\n%2").arg(fileInfo, content);
}
} // namespace QodeAssist::Chat

View File

@ -36,7 +36,7 @@ public:
explicit ClientInterface(ChatModel *chatModel, QObject *parent = nullptr);
~ClientInterface();
void sendMessage(const QString &message);
void sendMessage(const QString &message, bool includeCurrentFile = false);
void clearMessages();
void cancelRequest();
@ -45,6 +45,7 @@ signals:
private:
void handleLLMResponse(const QString &response, const QJsonObject &request, bool isComplete);
QString getCurrentFileContext() const;
LLMCore::RequestHandler *m_requestHandler;
ChatModel *m_chatModel;

View File

@ -137,6 +137,13 @@ ChatRootView {
text: qsTr("Clear Chat")
onClicked: clearChat()
}
CheckBox {
id: sharingCurrentFile
text: "Share current file with models"
checked: root.isSharingCurrentFile
}
}
}
@ -169,7 +176,7 @@ ChatRootView {
}
function sendChatMessage() {
root.sendMessage(messageInput.text);
root.sendMessage(messageInput.text, sharingCurrentFile.checked)
messageInput.text = ""
scrollToBottom()
}