kateai/kateaiconfigpage.cpp
2024-06-11 14:15:08 +02:00

107 lines
3.3 KiB
C++

#include "kateaiconfigpage.h"
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
#include <QVBoxLayout>
#include <QLabel>
#include <qlabel.h>
#include <qradiobutton.h>
#include "backend.h"
KateAiConfigPage::KateAiConfigPage(QWidget *parent, KateAiPlugin *plugin)
: KTextEditor::ConfigPage(parent)
, m_plugin(plugin)
{
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
std::vector<QString> backends = AiBackend::getAvailableBackendNames();
for(const QString& name : backends)
cmbxServerType.addItem(name);
layout->addWidget(&cmbxServerType);
QHBoxLayout* lineLayout = new QHBoxLayout(this);
QLabel* lineEditLabel = new QLabel(i18n("Url for the Ai server:"), this);
lineEditLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
lineLayout->addWidget(lineEditLabel);
lineLayout->addWidget(&lineUrl);
layout->addLayout(lineLayout);
QHBoxLayout* contextSpinLayout = new QHBoxLayout(this);
QLabel* contextSpinLabel = new QLabel(i18n("Maximum context:"), this);
contextSpinLayout->addWidget(contextSpinLabel);
contextSpinBox.setMinimum(100);
contextSpinBox.setMaximum(32768);
contextSpinLayout->addWidget(&contextSpinBox);
layout->addLayout(contextSpinLayout);
btnCompletion.setText(i18n("Use the Ai to generate a completion"));
btnInstruct.setText(i18n("Use the Ai to insert a response to an instruction"));
layout->addWidget(&btnCompletion);
layout->addWidget(&btnInstruct);
QHBoxLayout* systemPromptLayout = new QHBoxLayout(this);
systemPromptLabel.setText(i18n("System Prompt:"));
systemPromptLabel.setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
systemPromptLayout->addWidget(&systemPromptLabel);
systemPromptLayout->addWidget(&lineSystemPrompt);
layout->addLayout(systemPromptLayout);
layout->addStretch();
connect(&btnInstruct, &QRadioButton::toggled, this, &KateAiConfigPage::instructBtnToggeled);
reset();
}
QString KateAiConfigPage::name() const
{
return i18n("Ai");
}
QString KateAiConfigPage::fullName() const
{
return i18n("Ai Settings");
}
QIcon KateAiConfigPage::icon() const
{
return QIcon::fromTheme(QStringLiteral("text-x-generic"));
}
void KateAiConfigPage::apply()
{
KConfigGroup config(KSharedConfig::openConfig(), "Ai");
config.writeEntry("Url", lineUrl.text());
config.writeEntry("Instruct", btnInstruct.isChecked());
config.writeEntry("SystemPrompt", lineSystemPrompt.text());
config.writeEntry("Context", contextSpinBox.value());
config.writeEntry("Backend", cmbxServerType.currentText());
config.sync();
m_plugin->readConfig();
}
void KateAiConfigPage::reset()
{
KConfigGroup config(KSharedConfig::openConfig(), "Ai");
lineSystemPrompt.setText(config.readEntry("SystemPrompt", "ws://localhost:8642"));
lineUrl.setText(config.readEntry("Url", "You are an intelligent programming assistant."));
btnInstruct.setChecked(config.readEntry("Instruct", false));
btnCompletion.setChecked(!btnInstruct.isChecked());
contextSpinBox.setValue(config.readEntry("Context", 1024));
cmbxServerType.setCurrentText(config.readEntry("Backend", "KoboldAI"));
lineSystemPrompt.setEnabled(btnInstruct.isChecked());
systemPromptLabel.setEnabled(btnInstruct.isChecked());
}
void KateAiConfigPage::instructBtnToggeled(bool checked)
{
lineSystemPrompt.setEnabled(checked);
systemPromptLabel.setEnabled(checked);
}