refactor: Move to agent architecture

This commit is contained in:
Petr Mironychev
2026-05-30 14:50:49 +02:00
parent 34ce787320
commit ccc2ec2e80
364 changed files with 10801 additions and 19020 deletions

View File

@@ -2,6 +2,8 @@ add_library(Providers STATIC
ProviderID.hpp
Provider.hpp Provider.cpp
ProviderFactory.hpp ProviderFactory.cpp
GenericProvider.hpp GenericProvider.cpp
ClaudeCacheControl.hpp
)
target_link_libraries(Providers

View File

@@ -0,0 +1,95 @@
// Copyright (C) 2024-2026 Petr Mironychev
// SPDX-License-Identifier: GPL-3.0-or-later
// Additional attribution terms under GPLv3 §7(b) apply — see LICENSE
#pragma once
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QString>
#include <QStringList>
namespace QodeAssist::Providers::ClaudeCacheControl {
inline QJsonObject buildBreakpoint(bool extendedTtl)
{
QJsonObject cacheControl{{"type", "ephemeral"}};
if (extendedTtl)
cacheControl["ttl"] = "1h";
return cacheControl;
}
inline void markLastBlock(QJsonArray &blocks, const QJsonObject &cacheControl)
{
if (blocks.isEmpty())
return;
QJsonObject last = blocks.last().toObject();
last["cache_control"] = cacheControl;
blocks.replace(blocks.size() - 1, last);
}
inline void applyToSystem(QJsonObject &request, const QJsonObject &cacheControl)
{
if (!request.contains("system"))
return;
const QJsonValue sys = request.value("system");
if (sys.isString()) {
const QString text = sys.toString();
if (!text.isEmpty()) {
request["system"] = QJsonArray{QJsonObject{
{"type", "text"}, {"text", text}, {"cache_control", cacheControl}}};
}
} else if (sys.isArray()) {
QJsonArray blocks = sys.toArray();
markLastBlock(blocks, cacheControl);
request["system"] = blocks;
}
}
inline void applyToTools(QJsonObject &request, const QJsonObject &cacheControl)
{
if (!request.contains("tools"))
return;
QJsonArray tools = request.value("tools").toArray();
markLastBlock(tools, cacheControl);
request["tools"] = tools;
}
inline void applyToHistory(QJsonObject &request, const QJsonObject &cacheControl)
{
if (!request.contains("messages"))
return;
QJsonArray messages = request.value("messages").toArray();
if (messages.size() < 2)
return;
const int idx = messages.size() - 2;
QJsonObject msg = messages[idx].toObject();
const QJsonValue content = msg.value("content");
if (content.isString()) {
msg["content"] = QJsonArray{QJsonObject{
{"type", "text"}, {"text", content.toString()}, {"cache_control", cacheControl}}};
} else if (content.isArray()) {
QJsonArray blocks = content.toArray();
markLastBlock(blocks, cacheControl);
msg["content"] = blocks;
}
messages.replace(idx, msg);
request["messages"] = messages;
}
inline void apply(QJsonObject &request, bool extendedTtl, const QStringList &breakpoints)
{
const QJsonObject cacheControl = buildBreakpoint(extendedTtl);
const bool all = breakpoints.isEmpty();
if (all || breakpoints.contains(QStringLiteral("system")))
applyToSystem(request, cacheControl);
if (all || breakpoints.contains(QStringLiteral("tools")))
applyToTools(request, cacheControl);
if (all || breakpoints.contains(QStringLiteral("history")))
applyToHistory(request, cacheControl);
}
} // namespace QodeAssist::Providers::ClaudeCacheControl

View File

@@ -0,0 +1,110 @@
// Copyright (C) 2024-2026 Petr Mironychev
// SPDX-License-Identifier: GPL-3.0-or-later
#include "GenericProvider.hpp"
#include <utility>
#include <QJsonObject>
#include <LLMQore/BaseClient.hpp>
#include <LLMQore/ClaudeClient.hpp>
#include <LLMQore/GoogleAIClient.hpp>
#include <LLMQore/LlamaCppClient.hpp>
#include <LLMQore/MistralClient.hpp>
#include <LLMQore/OllamaClient.hpp>
#include <LLMQore/OpenAIClient.hpp>
#include <LLMQore/OpenAIResponsesClient.hpp>
#include "ProviderFactory.hpp"
namespace QodeAssist::Providers {
GenericProvider::GenericProvider(
QString name, ProviderID id, const ClientFactory &clientFactory, QObject *parent)
: Provider(parent)
, m_name(std::move(name))
, m_id(id)
, m_client(clientFactory(this))
{}
QString GenericProvider::name() const
{
return m_name;
}
ProviderID GenericProvider::providerID() const
{
return m_id;
}
::LLMQore::BaseClient *GenericProvider::client() const
{
return m_client;
}
QFuture<QList<QString>> GenericProvider::getInstalledModels(const QString &url)
{
m_client->setUrl(url);
m_client->setApiKey(apiKey());
return m_client->listModels();
}
RequestID GenericProvider::sendRequest(
const QUrl &url, const QJsonObject &payload, const QString &endpoint)
{
// Gemini carries the model in the URL and rejects unknown body fields, so
// the model/stream keys injected by the generic pipeline must be dropped.
if (m_id == ProviderID::GoogleAI) {
QJsonObject cleaned = payload;
cleaned.remove("model");
cleaned.remove("stream");
return Provider::sendRequest(url, cleaned, endpoint);
}
return Provider::sendRequest(url, payload, endpoint);
}
namespace {
template<typename ClientT>
GenericProvider::ClientFactory makeFactory()
{
return [](QObject *parent) -> ::LLMQore::BaseClient * {
return new ClientT(QString(), QString(), QString(), parent);
};
}
} // namespace
void registerBuiltinProviders()
{
const auto reg = [](const QString &api,
ProviderID id,
GenericProvider::ClientFactory factory) {
ProviderFactory::registerType(api, [=](QObject *parent) -> Provider * {
return new GenericProvider(api, id, factory, parent);
});
};
reg("Claude", ProviderID::Claude, makeFactory<::LLMQore::ClaudeClient>());
reg("Google AI", ProviderID::GoogleAI, makeFactory<::LLMQore::GoogleAIClient>());
reg("llama.cpp", ProviderID::LlamaCpp, makeFactory<::LLMQore::LlamaCppClient>());
reg("LM Studio (Chat Completions)", ProviderID::LMStudio,
makeFactory<::LLMQore::OpenAIClient>());
reg("LM Studio (Responses API)", ProviderID::OpenAIResponses,
makeFactory<::LLMQore::OpenAIResponsesClient>());
reg("Mistral AI", ProviderID::MistralAI, makeFactory<::LLMQore::MistralClient>());
reg("Codestral", ProviderID::MistralAI, makeFactory<::LLMQore::MistralClient>());
reg("Ollama (Native)", ProviderID::Ollama, makeFactory<::LLMQore::OllamaClient>());
reg("Ollama (OpenAI-compatible)", ProviderID::OpenAICompatible,
makeFactory<::LLMQore::OpenAIClient>());
reg("OpenAI (Chat Completions)", ProviderID::OpenAI,
makeFactory<::LLMQore::OpenAIClient>());
reg("OpenAI (Responses API)", ProviderID::OpenAIResponses,
makeFactory<::LLMQore::OpenAIResponsesClient>());
reg("OpenAI Compatible", ProviderID::OpenAICompatible,
makeFactory<::LLMQore::OpenAIClient>());
reg("OpenRouter", ProviderID::OpenRouter, makeFactory<::LLMQore::OpenAIClient>());
}
} // namespace QodeAssist::Providers

View File

@@ -0,0 +1,50 @@
// Copyright (C) 2024-2026 Petr Mironychev
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <functional>
#include "Provider.hpp"
namespace LLMQore {
class BaseClient;
}
namespace QodeAssist::Providers {
// A configuration-driven provider: it owns an LLMQore client and exposes a
// fixed identity. Concrete behaviour (request shape) comes from the agent's
// prompt template via Provider::prepareRequest, so a single class covers
// every client_api by varying the client factory + metadata.
class GenericProvider : public Provider
{
Q_OBJECT
public:
using ClientFactory = std::function<::LLMQore::BaseClient *(QObject *)>;
GenericProvider(
QString name,
ProviderID id,
const ClientFactory &clientFactory,
QObject *parent = nullptr);
QString name() const override;
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
ProviderID providerID() const override;
::LLMQore::BaseClient *client() const override;
RequestID sendRequest(
const QUrl &url, const QJsonObject &payload, const QString &endpoint) override;
private:
QString m_name;
ProviderID m_id;
::LLMQore::BaseClient *m_client;
};
// Registers every built-in client_api into ProviderFactory. Must be called once
// at plugin startup before any agent/session is created.
void registerBuiltinProviders();
} // namespace QodeAssist::Providers

View File

@@ -4,9 +4,11 @@
#include "Provider.hpp"
#include "ClaudeCacheControl.hpp"
#include "PromptTemplate.hpp"
#include <LLMQore/BaseClient.hpp>
#include <LLMQore/ClaudeClient.hpp>
#include <LLMQore/ToolsManager.hpp>
#include <QJsonArray>
@@ -25,24 +27,27 @@ bool Provider::prepareRequest(
PromptTemplate *prompt,
const ContextData &context,
bool isToolsEnabled,
bool isThinkingEnabled)
QString *errorOut)
{
if (!prompt) {
LOG_MESSAGE(QString("Provider '%1': null template").arg(name()));
const auto fail = [errorOut](const QString &message) {
LOG_MESSAGE(message);
if (errorOut)
*errorOut = message;
return false;
}
};
if (!prompt)
return fail(QString("Provider '%1': null template").arg(name()));
if (!prompt->isSupportProvider(providerID())) {
LOG_MESSAGE(QString("Template '%1' doesn't support provider '%2'")
return fail(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")
if (!prompt->buildFullRequest(request, context)) {
return fail(
QString("Provider '%1': template '%2' failed to build request (see log)")
.arg(name(), prompt->name()));
return false;
}
if (isToolsEnabled) {
@@ -51,9 +56,23 @@ bool Provider::prepareRequest(
request["tools"] = toolsDefinitions;
}
}
if (m_promptCachingEnabled)
ClaudeCacheControl::apply(
request, m_promptCachingExtendedTtl, m_promptCacheBreakpoints);
return true;
}
void Provider::setPromptCaching(bool enabled, bool extendedTtl, const QStringList &breakpoints)
{
m_promptCachingEnabled = enabled;
m_promptCachingExtendedTtl = enabled && extendedTtl;
m_promptCacheBreakpoints = breakpoints;
if (auto *claude = qobject_cast<::LLMQore::ClaudeClient *>(client()))
claude->setUseExtendedCacheTTL(m_promptCachingExtendedTtl);
}
RequestID Provider::sendRequest(
const QUrl &url, const QJsonObject &payload, const QString &endpoint)
{

View File

@@ -4,10 +4,10 @@
#pragma once
#include <QFlags>
#include <QFuture>
#include <QObject>
#include <QString>
#include <QStringList>
#include <utils/environment.h>
#include "ContextData.hpp"
@@ -31,15 +31,6 @@ 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
@@ -61,10 +52,9 @@ public:
PromptTemplate *prompt,
const ContextData &context,
bool isToolsEnabled,
bool isThinkingEnabled);
QString *errorOut = nullptr);
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;
@@ -73,9 +63,15 @@ public:
void cancelRequest(const RequestID &requestId);
::LLMQore::ToolsManager *toolsManager() const;
void setPromptCaching(
bool enabled, bool extendedTtl, const QStringList &breakpoints = {});
private:
QString m_url;
QString m_apiKey;
bool m_promptCachingEnabled = false;
bool m_promptCachingExtendedTtl = false;
QStringList m_promptCacheBreakpoints;
};
} // namespace QodeAssist::Providers