mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-05-30 02:49:12 -04:00
feat: Add agents and agents settings
This commit is contained in:
22
sources/providers/CMakeLists.txt
Normal file
22
sources/providers/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
add_library(Providers STATIC
|
||||
ProviderID.hpp
|
||||
Provider.hpp Provider.cpp
|
||||
ProviderFactory.hpp ProviderFactory.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(Providers
|
||||
PUBLIC
|
||||
Qt::Core
|
||||
Qt::Network
|
||||
QtCreator::Core
|
||||
QtCreator::Utils
|
||||
LLMQore
|
||||
Common
|
||||
PRIVATE
|
||||
QodeAssistLogger
|
||||
)
|
||||
|
||||
target_include_directories(Providers
|
||||
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
PRIVATE ${CMAKE_SOURCE_DIR}/sources/templates
|
||||
)
|
||||
86
sources/providers/Provider.cpp
Normal file
86
sources/providers/Provider.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
// Copyright (C) 2024-2026 Petr Mironychev
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "Provider.hpp"
|
||||
|
||||
#include "PromptTemplate.hpp"
|
||||
|
||||
#include <LLMQore/BaseClient.hpp>
|
||||
#include <LLMQore/ToolsManager.hpp>
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include <Logger.hpp>
|
||||
|
||||
namespace QodeAssist::Providers {
|
||||
|
||||
Provider::Provider(QObject *parent)
|
||||
: QObject(parent)
|
||||
{}
|
||||
|
||||
bool Provider::prepareRequest(
|
||||
QJsonObject &request,
|
||||
PromptTemplate *prompt,
|
||||
const ContextData &context,
|
||||
bool isToolsEnabled,
|
||||
bool isThinkingEnabled)
|
||||
{
|
||||
if (!prompt) {
|
||||
LOG_MESSAGE(QString("Provider '%1': null template").arg(name()));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!prompt->isSupportProvider(providerID())) {
|
||||
LOG_MESSAGE(QString("Template '%1' doesn't support provider '%2'")
|
||||
.arg(prompt->name(), name()));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!prompt->buildFullRequest(request, context, isThinkingEnabled)) {
|
||||
LOG_MESSAGE(
|
||||
QString("Provider '%1': template '%2' failed to build request")
|
||||
.arg(name(), prompt->name()));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isToolsEnabled) {
|
||||
const auto toolsDefinitions = toolsManager()->getToolsDefinitions();
|
||||
if (!toolsDefinitions.isEmpty()) {
|
||||
request["tools"] = toolsDefinitions;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
RequestID Provider::sendRequest(
|
||||
const QUrl &url, const QJsonObject &payload, const QString &endpoint)
|
||||
{
|
||||
auto *c = client();
|
||||
|
||||
c->setUrl(url.toString());
|
||||
c->setApiKey(apiKey());
|
||||
|
||||
auto requestId = c->sendMessage(payload, endpoint);
|
||||
|
||||
LOG_MESSAGE(
|
||||
QString("%1: Sending request %2 to %3%4").arg(name(), requestId, url.toString(), endpoint));
|
||||
LOG_MESSAGE(
|
||||
QString("%1: Payload:\n%2")
|
||||
.arg(name(), QString::fromUtf8(QJsonDocument(payload).toJson(QJsonDocument::Indented))));
|
||||
|
||||
return requestId;
|
||||
}
|
||||
|
||||
void Provider::cancelRequest(const RequestID &requestId)
|
||||
{
|
||||
LOG_MESSAGE(QString("%1: Cancelling request %2").arg(name(), requestId));
|
||||
client()->cancelRequest(requestId);
|
||||
}
|
||||
|
||||
::LLMQore::ToolsManager *Provider::toolsManager() const
|
||||
{
|
||||
return client()->tools();
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::Providers
|
||||
80
sources/providers/Provider.hpp
Normal file
80
sources/providers/Provider.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
// Copyright (C) 2024-2026 Petr Mironychev
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QFlags>
|
||||
#include <QFuture>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <utils/environment.h>
|
||||
|
||||
#include "ContextData.hpp"
|
||||
#include "ProviderID.hpp"
|
||||
#include "LLMQore/BaseClient.hpp"
|
||||
|
||||
namespace LLMQore {
|
||||
class BaseClient;
|
||||
class ToolsManager;
|
||||
}
|
||||
|
||||
namespace QodeAssist::Templates {
|
||||
class PromptTemplate;
|
||||
}
|
||||
|
||||
class QJsonObject;
|
||||
|
||||
namespace QodeAssist::Providers {
|
||||
|
||||
using Templates::ContextData;
|
||||
using Templates::PromptTemplate;
|
||||
using LLMQore::RequestID;
|
||||
|
||||
enum class ProviderCapability {
|
||||
Tools = 0x1,
|
||||
Thinking = 0x2,
|
||||
Image = 0x4,
|
||||
ModelListing = 0x8,
|
||||
};
|
||||
Q_DECLARE_FLAGS(ProviderCapabilities, ProviderCapability)
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS(ProviderCapabilities)
|
||||
|
||||
class Provider : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY_MOVE(Provider)
|
||||
public:
|
||||
explicit Provider(QObject *parent = nullptr);
|
||||
|
||||
virtual ~Provider() = default;
|
||||
|
||||
virtual QString name() const = 0;
|
||||
|
||||
virtual QString url() const { return m_url; }
|
||||
virtual QString apiKey() const { return m_apiKey; }
|
||||
void setUrl(const QString &url) { m_url = url; }
|
||||
void setApiKey(const QString &apiKey) { m_apiKey = apiKey; }
|
||||
|
||||
[[nodiscard]] virtual bool prepareRequest(
|
||||
QJsonObject &request,
|
||||
PromptTemplate *prompt,
|
||||
const ContextData &context,
|
||||
bool isToolsEnabled,
|
||||
bool isThinkingEnabled);
|
||||
virtual QFuture<QList<QString>> getInstalledModels(const QString &url) = 0;
|
||||
virtual ProviderID providerID() const = 0;
|
||||
virtual ProviderCapabilities capabilities() const { return {}; }
|
||||
|
||||
virtual ::LLMQore::BaseClient *client() const = 0;
|
||||
|
||||
virtual RequestID sendRequest(
|
||||
const QUrl &url, const QJsonObject &payload, const QString &endpoint);
|
||||
void cancelRequest(const RequestID &requestId);
|
||||
::LLMQore::ToolsManager *toolsManager() const;
|
||||
|
||||
private:
|
||||
QString m_url;
|
||||
QString m_apiKey;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Providers
|
||||
43
sources/providers/ProviderFactory.cpp
Normal file
43
sources/providers/ProviderFactory.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright (C) 2024-2026 Petr Mironychev
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "ProviderFactory.hpp"
|
||||
|
||||
#include <QHash>
|
||||
|
||||
namespace QodeAssist::Providers::ProviderFactory {
|
||||
|
||||
namespace {
|
||||
|
||||
QHash<QString, FactoryFn> &table()
|
||||
{
|
||||
static QHash<QString, FactoryFn> t;
|
||||
return t;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void registerType(const QString &name, FactoryFn fn)
|
||||
{
|
||||
if (name.isEmpty() || !fn) return;
|
||||
table().insert(name, std::move(fn));
|
||||
}
|
||||
|
||||
Provider *create(const QString &name, QObject *parent)
|
||||
{
|
||||
auto it = table().constFind(name);
|
||||
if (it == table().constEnd()) return nullptr;
|
||||
return it.value()(parent);
|
||||
}
|
||||
|
||||
QStringList knownNames()
|
||||
{
|
||||
return table().keys();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
table().clear();
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::Providers::ProviderFactory
|
||||
25
sources/providers/ProviderFactory.hpp
Normal file
25
sources/providers/ProviderFactory.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright (C) 2024-2026 Petr Mironychev
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <functional>
|
||||
|
||||
namespace QodeAssist::Providers {
|
||||
|
||||
class Provider;
|
||||
|
||||
namespace ProviderFactory {
|
||||
|
||||
using FactoryFn = std::function<Provider *(QObject *parent)>;
|
||||
|
||||
void registerType(const QString &name, FactoryFn fn);
|
||||
Provider *create(const QString &name, QObject *parent);
|
||||
QStringList knownNames();
|
||||
void clear(); // for tests / shutdown
|
||||
|
||||
} // namespace ProviderFactory
|
||||
|
||||
} // namespace QodeAssist::Providers
|
||||
22
sources/providers/ProviderID.hpp
Normal file
22
sources/providers/ProviderID.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright (C) 2024-2026 Petr Mironychev
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace QodeAssist::Providers {
|
||||
|
||||
enum class ProviderID : int {
|
||||
Any,
|
||||
Ollama,
|
||||
LMStudio,
|
||||
Claude,
|
||||
OpenAI,
|
||||
OpenAICompatible,
|
||||
OpenAIResponses,
|
||||
MistralAI,
|
||||
OpenRouter,
|
||||
GoogleAI,
|
||||
LlamaCpp,
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Providers
|
||||
Reference in New Issue
Block a user