general ui work
This commit is contained in:
72
kateai.cpp
72
kateai.cpp
@ -8,6 +8,7 @@
|
||||
#include <qdebug.h>
|
||||
#include <qhash.h>
|
||||
#include <qjsonobject.h>
|
||||
#include <qmessagebox.h>
|
||||
#include <qnamespace.h>
|
||||
#include <QString>
|
||||
#include <KActionCollection>
|
||||
@ -18,6 +19,7 @@
|
||||
#include <QTextCodec>
|
||||
#include <limits>
|
||||
#include <QMessageBox>
|
||||
#include <QInputDialog>
|
||||
#include <KLocalizedString>
|
||||
#include <KSharedConfig>
|
||||
|
||||
@ -62,12 +64,15 @@ void KateAiPlugin::readConfig()
|
||||
KConfigGroup config(KSharedConfig::openConfig(), "Ai");
|
||||
m_serverUrl = QUrl(config.readEntry("Url", "ws://localhost:8642"));
|
||||
reconnect();
|
||||
|
||||
KateAiPluginView::setInstruct(config.readEntry("Instruct", false));
|
||||
KateAiPluginView::setSystemPrompt(config.readEntry("SystemPrompt", "You are an intelligent programming assistant."));
|
||||
KateAiPluginView::setMaxContext(config.readEntry("Context", 1024));
|
||||
}
|
||||
|
||||
KateAiPluginView::KateAiPluginView(KateAiPlugin *plugin, KTextEditor::MainWindow *mainwindow, bool instruct)
|
||||
KateAiPluginView::KateAiPluginView(KateAiPlugin *plugin, KTextEditor::MainWindow *mainwindow)
|
||||
: QObject(plugin)
|
||||
, m_mainWindow(mainwindow)
|
||||
, m_useInstruct(instruct)
|
||||
{
|
||||
KXMLGUIClient::setComponentName(QStringLiteral("kateaiplugin"), QStringLiteral("Kate Ai"));
|
||||
setXMLFile(QStringLiteral("ui.rc"));
|
||||
@ -143,21 +148,28 @@ QStringList KateAiPluginView::getIncludePaths(const QString& text)
|
||||
return paths;
|
||||
}
|
||||
|
||||
QString KateAiPluginView::assembleContext(QPointer<KTextEditor::Document> document, const KTextEditor::Cursor& cursor)
|
||||
QString KateAiPluginView::assembleContext(QPointer<KTextEditor::Document> document,
|
||||
const KTextEditor::Cursor& cursor, const QString& instruction)
|
||||
{
|
||||
QString mime = document->mimeType();
|
||||
QString context;
|
||||
QString baseText;
|
||||
QString documentText = document->text(KTextEditor::Range(KTextEditor::Cursor(0, 0), cursor));
|
||||
|
||||
if(m_useInstruct)
|
||||
{
|
||||
context.append(QStringLiteral("### System Prompt\n"));
|
||||
context.append(m_systemPrompt);
|
||||
context.push_back(u'\n');
|
||||
context.append(QStringLiteral("### User Message\n"));
|
||||
context.append(instruction);
|
||||
context.push_back(QStringLiteral("\n### Assistant\n"));
|
||||
}
|
||||
|
||||
if(!m_useInstruct)
|
||||
baseText = document->text(KTextEditor::Range(KTextEditor::Cursor(0, 0), cursor));
|
||||
else
|
||||
baseText = document->text();
|
||||
if(mime == QStringLiteral("text/x-c++src") || mime == QStringLiteral("text/x-csrc"))
|
||||
{
|
||||
QFileInfo documentFileInfo(document->url().path());
|
||||
QString directory = documentFileInfo.absolutePath();
|
||||
QStringList paths = getIncludePaths(baseText);
|
||||
QStringList paths = getIncludePaths(documentText);
|
||||
qDebug()<<__func__<<"Directory:"<<directory<<"Paths:"<<paths;
|
||||
|
||||
for(QString& path : paths)
|
||||
@ -173,7 +185,7 @@ QString KateAiPluginView::assembleContext(QPointer<KTextEditor::Document> docume
|
||||
}
|
||||
}
|
||||
|
||||
context.append(baseText);
|
||||
context.append(documentText);
|
||||
|
||||
return context;
|
||||
}
|
||||
@ -185,15 +197,30 @@ void KateAiPluginView::generate()
|
||||
{
|
||||
KTextEditor::Cursor cursor = getCurrentCursor();
|
||||
QPointer<KTextEditor::Document> document = activeDocument();
|
||||
QString text = assembleContext(document, cursor);
|
||||
QString instruction;
|
||||
|
||||
AiBackend::Request request(text);
|
||||
m_ai->generate(request);
|
||||
m_requests.insert(request.getId(), {cursor, document});
|
||||
if(m_useInstruct)
|
||||
{
|
||||
bool ok;
|
||||
instruction = QInputDialog::getMultiLineText(m_mainWindow->activeView(), i18n("Input instruction"),
|
||||
i18n("Instruction"), m_lastInstruct, &ok);
|
||||
if(!ok)
|
||||
return;
|
||||
}
|
||||
|
||||
QString text = assembleContext(document, cursor, instruction);
|
||||
|
||||
AiBackend::Request request(text, AiBackend::Request::INFERENCE);
|
||||
bool ret = m_ai->generate(request);
|
||||
if(!ret)
|
||||
QMessageBox::warning(m_mainWindow->activeView(), i18n("Failure"),
|
||||
i18n("The Ai backend was unable to process this request"));
|
||||
else
|
||||
m_requests.insert(request.getId(), {cursor, document});
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox box;
|
||||
QMessageBox box(m_mainWindow->activeView());
|
||||
box.setText(i18n("The AI server is not connected."));
|
||||
box.setInformativeText(i18n("would you like to try and reconnect?"));
|
||||
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
@ -214,5 +241,20 @@ void KateAiPluginView::setInstruct(bool instruct)
|
||||
m_useInstruct = instruct;
|
||||
}
|
||||
|
||||
void KateAiPluginView::setSystemPrompt(const QString& in)
|
||||
{
|
||||
m_systemPrompt = in;
|
||||
}
|
||||
|
||||
void KateAiPluginView::setAi(QPointer<AiBackend> ai)
|
||||
{
|
||||
m_ai = ai;
|
||||
}
|
||||
|
||||
void KateAiPluginView::setMaxContext(int maxContext)
|
||||
{
|
||||
m_maxContext = maxContext;
|
||||
}
|
||||
|
||||
#include "kateai.moc"
|
||||
#include "moc_kateai.cpp"
|
||||
|
Reference in New Issue
Block a user